Skip to content

Commit

Permalink
Re-add error recovery. This time, don't require the ; such that it ca…
Browse files Browse the repository at this point in the history
…n recover without greedy munching until the next semicolon.
  • Loading branch information
Ivorforce committed Apr 26, 2024
1 parent 3c7aca9 commit d3ad83a
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/monoteny_grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Block: Block = {
}

InnerBlock: Block = {
<statements: Box<Decorated<Positioned<(<Statement> ";")>>>*> => Block { <> }
<statements: Box<Decorated<Positioned<<Statement>>>>*> => Block { <> }
};

Struct: Struct = {
Expand Down Expand Up @@ -127,8 +127,15 @@ Conformance: TraitConformanceDeclaration = {

// =============================== Statement =====================================


Statement: Statement = {
<e: Positioned<!>> => {
errors.push(e.value.clone());
Statement::Error(derive_error(&e))
},
<StatementNoSemicolon> ";",
};

StatementNoSemicolon: Statement = {
<mutability: VariableDeclarationMutability> <identifier: Identifier> <type_declaration: ("'" <Box<Expression>>)?> <assignment: ("=" <Box<Expression>>)?> => Statement::VariableDeclaration { mutability, identifier, type_declaration, assignment },
"upd" <target: Box<Expression>> "=" <new_value: Box<Expression>> => Statement::VariableUpdate { <> },
"return" <Box<Expression>?> => Statement::Return(<>),
Expand Down
2 changes: 2 additions & 0 deletions src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub struct IfThenElse {

#[derive(Eq, PartialEq, Clone)]
pub enum Statement {
Error(RuntimeError),
VariableDeclaration {
mutability: Mutability,
identifier: String,
Expand Down Expand Up @@ -226,6 +227,7 @@ impl Display for TraitConformanceDeclaration {
impl Display for Statement {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
match self {
Statement::Error(error) => write!(fmt, "ERR"),
Statement::VariableDeclaration { mutability, identifier, type_declaration, assignment} => {
let mutability_string = mutability.variable_declaration_keyword();
write!(fmt, "{} {}", mutability_string, identifier)?;
Expand Down
1 change: 1 addition & 0 deletions src/resolver/conformance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl <'a, 'b> ConformanceResolver<'a, 'b> {
RuntimeError::error(format!("Expression {} not valid in a conformance context.", statement).as_str()).to_array()
);
}
ast::Statement::Error(err) => Err(err.clone().to_array())?,
_ => {
return Err(
RuntimeError::error(format!("Statement {} not valid in a conformance context.", statement).as_str()).to_array()
Expand Down
1 change: 1 addition & 0 deletions src/resolver/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ impl <'a> GlobalResolver<'a> {
RuntimeError::error(format!("Expression {} is not supported in a global context.", e).as_str()).to_array()
)
}
ast::Statement::Error(err) => Err(err.clone().to_array())?,
statement => {
return Err(
RuntimeError::error(format!("Statement {} is not supported in a global context.", statement).as_str()).to_array()
Expand Down
1 change: 1 addition & 0 deletions src/resolver/imperative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ impl <'a> ImperativeResolver<'a> {

self.resolve_expression(&expression, &scope)?
}
ast::Statement::Error(err) => Err(err.clone().to_array())?,
statement => {
return Err(
RuntimeError::error(format!("Statement {} is not supported in an imperative context.", statement).as_str()).to_array()
Expand Down
1 change: 1 addition & 0 deletions src/resolver/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl <'a> TraitResolver<'a> {
RuntimeError::error(format!("Expression {} not valid in a trait context.", statement).as_str()).to_array()
);
}
ast::Statement::Error(err) => Err(err.clone().to_array())?,
_ => {
return Err(
RuntimeError::error(format!("Statement {} not valid in a trait context.", statement).as_str()).to_array()
Expand Down

0 comments on commit d3ad83a

Please sign in to comment.