parser: get failing test cases passing on precedence changes

This commit is contained in:
Ryan Chandler 2022-09-14 12:01:10 +01:00
parent b384bb81ec
commit 2b7d41812f
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
4 changed files with 57 additions and 32 deletions

View File

@ -10,3 +10,6 @@ trunk_lexer = { path = "../trunk_lexer" }
[lib]
doctest = false
[dev-dependencies]
pretty_assertions = "1.3.0"

17
trunk_parser/src/main.rs Normal file
View File

@ -0,0 +1,17 @@
use trunk_lexer::Lexer;
use trunk_parser::Parser;
fn main() {
let file = std::env::args().nth(1).unwrap();
let contents = std::fs::read_to_string(&file).unwrap();
println!("> Parsing {}", file);
let mut lexer = Lexer::new(None);
let tokens = lexer.tokenize(contents.as_bytes()).unwrap();
let mut parser = Parser::new(None);
let ast = parser.parse(tokens).unwrap();
dbg!(ast);
}

View File

@ -1835,6 +1835,10 @@ impl Parser {
break;
}
if rpred == precedence && matches!(rpred.associativity(), Some(Associativity::Left)) {
break;
}
if rpred == precedence && matches!(rpred.associativity(), Some(Associativity::Non)) {
return Err(ParseError::UnexpectedToken(kind.to_string(), span));
}
@ -2218,6 +2222,7 @@ mod tests {
Catch, Expression, Identifier, Param, Statement, Type,
};
use trunk_lexer::Lexer;
use pretty_assertions::{assert_eq};
macro_rules! function {
($name:literal, $params:expr, $body:expr) => {
@ -2630,18 +2635,18 @@ mod tests {
assert_ast(
"<?php 'foo' . 'bar' . 'baz';",
&[expr!(Expression::Infix {
lhs: Box::new(Expression::ConstantString {
value: "foo".into()
}),
op: InfixOp::Concat,
rhs: Box::new(Expression::Infix {
lhs: Box::new(Expression::Infix {
lhs: Box::new(Expression::ConstantString {
value: "bar".into()
value: "foo".into()
}),
op: InfixOp::Concat,
rhs: Box::new(Expression::ConstantString {
value: "baz".into()
value: "bar".into()
}),
}),
op: InfixOp::Concat,
rhs: Box::new(Expression::ConstantString {
value: "baz".into()
})
})],
);

View File

@ -3,34 +3,34 @@ use trunk_lexer::TokenKind;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Precedence {
Lowest,
CloneNew,
Pow,
Prefix,
Instanceof,
Bang,
MulDivMod,
AddSub,
BitShift,
Concat,
LtGt,
Equality,
BitwiseAnd,
BitwiseXor,
BitwiseOr,
And,
Or,
NullCoalesce,
Ternary,
Assignment,
YieldFrom,
Yield,
Print,
KeyAnd,
KeyXor,
IncDec,
KeyOr,
KeyXor,
KeyAnd,
Print,
Yield,
YieldFrom,
Assignment,
Ternary,
NullCoalesce,
Or,
And,
BitwiseOr,
BitwiseXor,
BitwiseAnd,
Equality,
LtGt,
Concat,
BitShift,
AddSub,
MulDivMod,
Bang,
Instanceof,
Prefix,
Pow,
CloneNew,
CallDim,
ObjectAccess,
IncDec,
}
impl Precedence {