1
0

Remove redundant check for syntax errors

This commit is contained in:
Jeff 2024-02-16 17:11:28 -05:00
parent a53f83f03a
commit 7003c37aac
2 changed files with 3 additions and 27 deletions

View File

@ -69,11 +69,11 @@ impl Display for SyntaxError {
[( [(
position.start_byte..position.end_byte, position.start_byte..position.end_byte,
format!( format!(
"Syntax error at ({}, {}) to ({}, {}).", "Invalid syntax from ({}, {}) to ({}, {}).",
position.start_row, position.start_row,
position.start_column, position.start_column,
position.end_row, position.end_column,
position.end_column position.end_row
), ),
(255, 100, 100), (255, 100, 100),
)], )],

View File

@ -124,31 +124,7 @@ impl Interpreter {
/// - generate an abstract tree from the source and syntax tree /// - generate an abstract tree from the source and syntax tree
/// - check the abstract tree for type errors /// - check the abstract tree for type errors
pub fn validate(&mut self, source: &str) -> Result<Root, Error> { pub fn validate(&mut self, source: &str) -> Result<Root, Error> {
fn check_for_error(
node: SyntaxNode,
source: &str,
cursor: &mut TreeCursor,
) -> Result<(), Error> {
if node.is_error() {
Err(Error::Syntax(SyntaxError::InvalidSource {
source: source[node.byte_range()].to_string(),
position: SourcePosition::from(node.range()),
}))
} else {
for child in node.children(&mut cursor.clone()) {
check_for_error(child, source, cursor)?;
}
Ok(())
}
}
let syntax_tree = self.parse(source)?; let syntax_tree = self.parse(source)?;
let root = syntax_tree.root_node();
let mut cursor = syntax_tree.root_node().walk();
check_for_error(root, source, &mut cursor)?;
let abstract_tree = Root::from_syntax(syntax_tree.root_node(), source, &self.context)?; let abstract_tree = Root::from_syntax(syntax_tree.root_node(), source, &self.context)?;
abstract_tree.validate(source, &self.context)?; abstract_tree.validate(source, &self.context)?;