Introduce ErrorStatement

This commit is contained in:
Shuhei Takahashi 2025-02-04 23:30:11 +09:00
parent 1cb1444a23
commit 876bca27e8
5 changed files with 51 additions and 22 deletions

View file

@ -197,8 +197,7 @@ fn collect_symbols(node: &dyn Node, line_index: &LineIndex) -> Vec<DocumentSymbo
symbols.push(top_symbol);
}
Statement::Unknown(_) => {}
Statement::UnmatchedBrace(_) => {}
Statement::Error(_) => {}
}
} else {
for child in node.children() {
@ -544,7 +543,7 @@ impl Analyzer {
events.push(AnalyzedEvent::Conditions(condition_blocks));
Ok(events)
}
Statement::Unknown(_) | Statement::UnmatchedBrace(_) => Ok(Vec::new()),
Statement::Error(_) => Ok(Vec::new()),
}
})
.collect::<std::io::Result<Vec<_>>>()?

View file

@ -303,7 +303,7 @@ impl ShallowAnalyzer {
}
}
}
Statement::Unknown(_) | Statement::UnmatchedBrace(_) => {}
Statement::Error(_) => {}
}
}

View file

@ -32,7 +32,7 @@ fn test_analyze_smoke() {
.ast_root
.statements
.iter()
.all(|s| !matches!(s, Statement::Unknown(_) | Statement::UnmatchedBrace(_))));
.all(|s| !matches!(s, Statement::Error(_))));
// Inspect the top-level scope.
let scope = file.scope_at(0);

View file

@ -62,6 +62,7 @@ impl<'i, 'n> Iterator for Walk<'i, 'n> {
Some(node)
}
}
pub struct FilterWalk<'i, 'n, T> {
#[allow(clippy::type_complexity)]
inner: std::iter::FilterMap<Walk<'i, 'n>, fn(&'n dyn Node<'i>) -> Option<&'n T>>,
@ -88,8 +89,7 @@ pub enum Statement<'i> {
Assignment(Box<Assignment<'i>>),
Call(Box<Call<'i>>),
Condition(Box<Condition<'i>>),
Unknown(Box<UnknownStatement<'i>>),
UnmatchedBrace(Box<UnmatchedBrace<'i>>),
Error(Box<ErrorStatement<'i>>),
}
impl<'i> Node<'i> for Statement<'i> {
@ -102,8 +102,7 @@ impl<'i> Node<'i> for Statement<'i> {
Statement::Assignment(assignment) => vec![assignment.as_node()],
Statement::Call(call) => vec![call.as_node()],
Statement::Condition(condition) => vec![condition.as_node()],
Statement::Unknown(unknown_statement) => vec![unknown_statement.as_node()],
Statement::UnmatchedBrace(unmatched_brace) => vec![unmatched_brace.as_node()],
Statement::Error(error) => vec![error.as_node()],
}
}
@ -112,8 +111,7 @@ impl<'i> Node<'i> for Statement<'i> {
Statement::Assignment(assignment) => assignment.span,
Statement::Call(call) => call.span,
Statement::Condition(condition) => condition.span,
Statement::Unknown(unknown_statement) => unknown_statement.span,
Statement::UnmatchedBrace(unmatched_brace) => unmatched_brace.span,
Statement::Error(error) => error.span(),
}
}
@ -585,6 +583,32 @@ impl<'i> Node<'i> for ListLiteral<'i> {
}
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum ErrorStatement<'i> {
UnknownStatement(Box<UnknownStatement<'i>>),
UnmatchedBrace(Box<UnmatchedBrace<'i>>),
}
impl<'i> Node<'i> for ErrorStatement<'i> {
fn as_node(&self) -> &dyn Node<'i> {
self
}
fn children(&self) -> Vec<&dyn Node<'i>> {
match self {
ErrorStatement::UnknownStatement(unknown) => vec![unknown.as_node()],
ErrorStatement::UnmatchedBrace(unmatched_brace) => vec![unmatched_brace.as_node()],
}
}
fn span(&self) -> Span<'i> {
match self {
ErrorStatement::UnknownStatement(unknown) => unknown.span,
ErrorStatement::UnmatchedBrace(unmatched_brace) => unmatched_brace.span,
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct UnknownStatement<'i> {
pub text: &'i str,

View file

@ -129,10 +129,12 @@ fn convert_block(pair: Pair<Rule>) -> Block {
.into_inner()
.filter_map(|pair| match pair.as_rule() {
Rule::statement => Some(convert_statement(pair, std::mem::take(&mut comments))),
Rule::error => Some(Statement::Unknown(Box::new(UnknownStatement {
text: pair.as_str(),
span: pair.as_span(),
}))),
Rule::error => Some(Statement::Error(Box::new(
ErrorStatement::UnknownStatement(Box::new(UnknownStatement {
text: pair.as_str(),
span: pair.as_span(),
})),
))),
Rule::comment => {
comments
.lines
@ -319,13 +321,17 @@ fn convert_file(pair: Pair<Rule>) -> Block {
.into_inner()
.filter_map(|pair| match pair.as_rule() {
Rule::statement => Some(convert_statement(pair, std::mem::take(&mut comments))),
Rule::error => Some(Statement::Unknown(Box::new(UnknownStatement {
text: pair.as_str(),
span: pair.as_span(),
}))),
Rule::unmatched_brace => Some(Statement::UnmatchedBrace(Box::new(UnmatchedBrace {
span: pair.as_span(),
}))),
Rule::error => Some(Statement::Error(Box::new(
ErrorStatement::UnknownStatement(Box::new(UnknownStatement {
text: pair.as_str(),
span: pair.as_span(),
})),
))),
Rule::unmatched_brace => Some(Statement::Error(Box::new(
ErrorStatement::UnmatchedBrace(Box::new(UnmatchedBrace {
span: pair.as_span(),
})),
))),
Rule::comment => {
comments
.lines