Skip to content

Commit

Permalink
refactor: deprecate expect() in mod lexer(#196) (#1113)
Browse files Browse the repository at this point in the history
delete lexer.expect(...) in favour of expect_token!() macro

refactor the parser to use the expect_token!() macro that collects
diagnostics instead of returning a result. This will allow us to
gradually improve the robustness and recovery-capabilities
of the parser.

fixes #196
  • Loading branch information
yingmanwumen committed Mar 15, 2024
1 parent f51b011 commit 3e84974
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 140 deletions.
13 changes: 0 additions & 13 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,6 @@ impl<'a> ParseSession<'a> {
self.id_provider.next_id()
}

/// this function will be removed soon:
pub fn expect(&self, token: Token) -> Result<(), Diagnostic> {
if self.token != token {
Err(Diagnostic::unexpected_token_found(
format!("{token:?}").as_str(),
self.slice(),
self.location(),
))
} else {
Ok(())
}
}

/// Tries to consume the given token, returning false if it failed.
pub fn try_consume(&mut self, token: &Token) -> bool {
if self.token == *token {
Expand Down
38 changes: 18 additions & 20 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,9 +669,11 @@ fn parse_data_type_definition(
.with_location(lexer.last_location())
.with_error_code("E015"),
);
if let Err(diag) = lexer.expect(KeywordTo) {
lexer.accept_diagnostic(diag);
} else {
let expect_keyword_to = |lexer: &mut ParseSession| {
expect_token!(lexer, KeywordTo, None);
Some(())
};
if expect_keyword_to(lexer).is_some() {
lexer.advance();
}
parse_pointer_definition(lexer, name, start_pos)
Expand Down Expand Up @@ -987,13 +989,14 @@ pub fn parse_any_in_region<T, F: FnOnce(&mut ParseSession) -> T>(
}

fn parse_reference(lexer: &mut ParseSession) -> AstNode {
match expressions_parser::parse_call_statement(lexer) {
Ok(statement) => statement,
Err(diagnostic) => {
let statement = AstFactory::create_empty_statement(diagnostic.get_location(), lexer.next_id());
lexer.accept_diagnostic(diagnostic);
statement
}
if let Some(statement) = expressions_parser::parse_call_statement(lexer) {
statement
} else {
let statement = AstFactory::create_empty_statement(
lexer.diagnostics.last().map_or(SourceLocation::undefined(), |d| d.get_location()),
lexer.next_id(),
);
statement
}
}

Expand Down Expand Up @@ -1086,13 +1089,7 @@ fn parse_variable_line(lexer: &mut ParseSession) -> Vec<Variable> {
let address = if lexer.try_consume(&KeywordAt) {
//Look for a hardware address
if let HardwareAccess((direction, access_type)) = lexer.token {
match parse_hardware_access(lexer, direction, access_type) {
Ok(it) => Some(it),
Err(err) => {
lexer.accept_diagnostic(err);
None
}
}
parse_hardware_access(lexer, direction, access_type)
} else {
lexer.accept_diagnostic(Diagnostic::missing_token("Hardware Access", lexer.location()));
None
Expand Down Expand Up @@ -1129,7 +1126,7 @@ fn parse_hardware_access(
lexer: &mut ParseSession,
hardware_access_type: HardwareAccessType,
access_type: DirectAccessType,
) -> Result<AstNode, Diagnostic> {
) -> Option<AstNode> {
let start_location = lexer.last_location();
lexer.advance();
//Folowed by an integer
Expand All @@ -1144,14 +1141,15 @@ fn parse_hardware_access(
}
}
}
Ok(AstFactory::create_hardware_access(
Some(AstFactory::create_hardware_access(
access_type,
hardware_access_type,
address,
start_location.span(&lexer.last_location()),
lexer.next_id(),
))
} else {
Err(Diagnostic::missing_token("LiteralInteger", lexer.location()))
lexer.accept_diagnostic(Diagnostic::missing_token("LiteralInteger", lexer.location()));
None
}
}
Loading

0 comments on commit 3e84974

Please sign in to comment.