diff --git a/src/parser/src/grammar.lalrpop b/src/parser/src/grammar.lalrpop index 8f37744..6a5ef93 100644 --- a/src/parser/src/grammar.lalrpop +++ b/src/parser/src/grammar.lalrpop @@ -74,23 +74,22 @@ ArgList: Vec> = { } VarDecs: Vec = { - "," => match e { - None => { - if v.len() != 0 { - let error = ErrorRecovery { - error: ParseError::User { - error: LexicalError::MissingLexeme(Span { - source: source.to_string(), - start: l, - end: r - }, "variable name".to_string()) - }, dropped_tokens: Vec::new(), - }; - errors.push(error); - } - v - }, - Some(e) => { v.push(e); v } + VarDecs "," ! => { + let error = ErrorRecovery { + error: ParseError::User { + error: LexicalError::MissingLexeme(Span { + source: source.to_string(), + start: l, + end: r + }, "variable name".to_string()) + }, dropped_tokens: Vec::new(), + }; + errors.push(error); + Vec::new() + }, + "," => { + v.push(e); + v }, => vec![e], } @@ -419,7 +418,7 @@ OpenExpr: tree::Expr = { error: LexicalError::MissingLexeme(Span { source: source.to_string(), start: l, - end: r + end: l + 1 }, "opening parenthesis '('".to_string()) }, dropped_tokens: Vec::new(), @@ -434,7 +433,7 @@ OpenExpr: tree::Expr = { error: LexicalError::MissingLexeme(Span { source: source.to_string(), start: l, - end: r + end: l + 1 }, "closing parenthesis ')'".to_string()) }, dropped_tokens: Vec::new(), @@ -442,27 +441,6 @@ OpenExpr: tree::Expr = { errors.push(error); tree::Expr::Error }, - - // TODO:remove this - => match &error.error { - ParseError::UnrecognizedToken { token, expected } => { - if token.1 == Token::KeywordElse { - let error = ErrorRecovery { - error: ParseError::User { - error: LexicalError::StatementError(Span { - source: source.to_string(), - start: l, - end: r - }, "no 'if' before 'else'".to_string()) - }, - dropped_tokens: Vec::new(), - }; - errors.push(error); - } - tree::Expr::Error - }, - _ => tree::Expr::Error - }, } CloseExpr: tree::Expr = { @@ -552,14 +530,14 @@ CloseExpr: tree::Expr = { errors.push(error); tree::Expr::Error }, - ! "=" CompExpr? ";" => { + ! => { let error = ErrorRecovery { error: ParseError::User { error: LexicalError::StatementError(Span { source: source.to_string(), start: l, end: r - }, "Unexpected statement".to_string()) + }, "No 'if' before 'else'".to_string()) }, dropped_tokens: Vec::new(), }; @@ -608,23 +586,11 @@ WhileExpr: tree::Expr = { ForExpr: tree::Expr = { "for" "(" ";" ";" ")" "{" "}" => { - let init = match init { - Some(init) => init, - None => tree::Expr::VarManagement(Vec::new()) - }; - let cond = match cond { - Some(cond) => cond, - None => Box::new(tree::CondExpr::Bool(true)) - }; - let update = match update { - Some(update) => update, - None => tree::Expr::VarManagement(Vec::new()) - }; tree::Expr::Loop( tree::Loop::ForExpr( - Box::new(init), - cond, - Box::new(update), + init.map(Box::new).unwrap_or(Box::new(tree::Expr::VarManagement(Vec::new()))), + cond.unwrap_or(Box::new(tree::CondExpr::Bool(true))), + update.map(Box::new).unwrap_or(Box::new(tree::Expr::VarManagement(Vec::new()))), body ) ) @@ -786,6 +752,21 @@ VarDec: tree::Variable = { errors.push(error); tree::Variable::Error }, + + ! "=" CompExpr => { + let error = ErrorRecovery { + error: ParseError::User { + error: LexicalError::StatementError(Span { + source: source.to_string(), + start: l, + end: r + }, "Unexpected statement".to_string()) + }, + dropped_tokens: Vec::new(), + }; + errors.push(error); + tree::Variable::Error + }, } StructDec: tree::Variable = { diff --git a/src/parser/src/lib.rs b/src/parser/src/lib.rs index 31c0911..483ef31 100644 --- a/src/parser/src/lib.rs +++ b/src/parser/src/lib.rs @@ -102,7 +102,7 @@ mod tests { Parser::BodyParser => assert_eq!(format!("{}", BodyParser::new().parse(&mut errors, file_path, lexer).unwrap()), expected), Parser::ProgramParser => { let result = ProgramParser::new().parse(&mut errors, file_path, lexer) - .unwrap_or_else(|_| panic!("Failed to parse file: {}", file_path)); + .unwrap_or_else(|e| panic!("Failed to parse file: {}\n{}", file_path, e)); if errors.len() > 0 { let mut error_str = Vec::new(); for error in &errors { diff --git a/src/test/phase1/extra/test_1_s08.out b/src/test/phase1/extra/test_1_s08.out index 7c3d495..c12a883 100644 --- a/src/test/phase1/extra/test_1_s08.out +++ b/src/test/phase1/extra/test_1_s08.out @@ -1 +1 @@ -Error type B at Line 11: no 'if' before 'else' \ No newline at end of file +Error type B at Line 11: No 'if' before 'else' \ No newline at end of file