[parser] Fix parsing of ordering expressions.

Follow the grammar rule. The ordering operators are not associative.

Split previous tests into succeeding and failing ones.

Refresh expectations of old test files.

Add expectations of new test files.
This commit is contained in:
Alessandro Coglio 2022-05-10 23:32:46 -07:00
parent 521c04b4d6
commit 62f2267c2b
17 changed files with 79 additions and 957 deletions

View File

@ -152,12 +152,14 @@ impl ParserContext<'_> {
/// Returns an [`Expression`] AST node if the next tokens represent a
/// binary relational expression: less than, less than or equals, greater than, greater than or equals.
///
/// Otherwise, tries to parse the next token using [`parse_shift_expression`].
/// Otherwise, tries to parse the next token using [`parse_additive_expression`].
pub fn parse_ordering_expression(&mut self) -> Result<Expression> {
self.parse_bin_expr(
&[Token::Lt, Token::LtEq, Token::Gt, Token::GtEq],
Self::parse_additive_expression,
)
let mut expr = self.parse_additive_expression()?;
if let Some(op) = self.eat_bin_op(&[Token::Lt, Token::LtEq, Token::Gt, Token::GtEq]) {
let right = self.parse_additive_expression()?;
expr = Self::bin_expr(expr, right, op);
}
Ok(expr)
}
/// Returns an [`Expression`] AST node if the next tokens represent a

View File

@ -68,61 +68,6 @@ outputs:
col_stop: 8
path: ""
content: 2u8>3u8
- Binary:
left:
Binary:
left:
Value:
Integer:
- U8
- "1"
- span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 4
path: ""
content: 1u8 > 2u8 > 3u8
right:
Value:
Integer:
- U8
- "2"
- span:
line_start: 1
line_stop: 1
col_start: 7
col_stop: 10
path: ""
content: 1u8 > 2u8 > 3u8
op: Gt
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 10
path: ""
content: 1u8 > 2u8 > 3u8
right:
Value:
Integer:
- U8
- "3"
- span:
line_start: 1
line_stop: 1
col_start: 13
col_stop: 16
path: ""
content: 1u8 > 2u8 > 3u8
op: Gt
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 16
path: ""
content: 1u8 > 2u8 > 3u8
- Binary:
left:
Binary:
@ -200,179 +145,3 @@ outputs:
col_stop: 22
path: ""
content: 1u8 + 2u8 > 3u8 + 4u8
- Binary:
left:
Binary:
left:
Value:
Integer:
- U8
- "1"
- span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 4
path: ""
content: 1u8 > 2u8 > 3u8
right:
Value:
Integer:
- U8
- "2"
- span:
line_start: 1
line_stop: 1
col_start: 7
col_stop: 10
path: ""
content: 1u8 > 2u8 > 3u8
op: Gt
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 10
path: ""
content: 1u8 > 2u8 > 3u8
right:
Value:
Integer:
- U8
- "3"
- span:
line_start: 1
line_stop: 1
col_start: 13
col_stop: 16
path: ""
content: 1u8 > 2u8 > 3u8
op: Gt
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 16
path: ""
content: 1u8 > 2u8 > 3u8
- Binary:
left:
Binary:
left:
Binary:
left:
Value:
Integer:
- U8
- "1"
- span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 4
path: ""
content: 1u8 + 2u8 > 3u8 + 4u8 > 5u8 + 6u8
right:
Value:
Integer:
- U8
- "2"
- span:
line_start: 1
line_stop: 1
col_start: 7
col_stop: 10
path: ""
content: 1u8 + 2u8 > 3u8 + 4u8 > 5u8 + 6u8
op: Add
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 10
path: ""
content: 1u8 + 2u8 > 3u8 + 4u8 > 5u8 + 6u8
right:
Binary:
left:
Value:
Integer:
- U8
- "3"
- span:
line_start: 1
line_stop: 1
col_start: 13
col_stop: 16
path: ""
content: 1u8 + 2u8 > 3u8 + 4u8 > 5u8 + 6u8
right:
Value:
Integer:
- U8
- "4"
- span:
line_start: 1
line_stop: 1
col_start: 19
col_stop: 22
path: ""
content: 1u8 + 2u8 > 3u8 + 4u8 > 5u8 + 6u8
op: Add
span:
line_start: 1
line_stop: 1
col_start: 13
col_stop: 22
path: ""
content: 1u8 + 2u8 > 3u8 + 4u8 > 5u8 + 6u8
op: Gt
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 22
path: ""
content: 1u8 + 2u8 > 3u8 + 4u8 > 5u8 + 6u8
right:
Binary:
left:
Value:
Integer:
- U8
- "5"
- span:
line_start: 1
line_stop: 1
col_start: 25
col_stop: 28
path: ""
content: 1u8 + 2u8 > 3u8 + 4u8 > 5u8 + 6u8
right:
Value:
Integer:
- U8
- "6"
- span:
line_start: 1
line_stop: 1
col_start: 31
col_stop: 34
path: ""
content: 1u8 + 2u8 > 3u8 + 4u8 > 5u8 + 6u8
op: Add
span:
line_start: 1
line_stop: 1
col_start: 25
col_stop: 34
path: ""
content: 1u8 + 2u8 > 3u8 + 4u8 > 5u8 + 6u8
op: Gt
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 34
path: ""
content: 1u8 + 2u8 > 3u8 + 4u8 > 5u8 + 6u8

View File

@ -0,0 +1,7 @@
---
namespace: ParseExpression
expectation: Fail
outputs:
- "did not consume all input: '>' @ 1:11-12\n'3' @ 1:13-14\n'u8' @ 1:14-16\n"
- "did not consume all input: '>' @ 1:11-12\n'3' @ 1:13-14\n'u8' @ 1:14-16\n"
- "did not consume all input: '>' @ 1:23-24\n'5' @ 1:25-26\n'u8' @ 1:26-28\n'+' @ 1:29-30\n'6' @ 1:31-32\n'u8' @ 1:32-34\n"

View File

@ -68,61 +68,6 @@ outputs:
col_stop: 11
path: ""
content: 2u8 >= 3u8
- Binary:
left:
Binary:
left:
Value:
Integer:
- U8
- "1"
- span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 4
path: ""
content: 1u8 >= 2u8 >= 3u8
right:
Value:
Integer:
- U8
- "2"
- span:
line_start: 1
line_stop: 1
col_start: 8
col_stop: 11
path: ""
content: 1u8 >= 2u8 >= 3u8
op: Ge
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 11
path: ""
content: 1u8 >= 2u8 >= 3u8
right:
Value:
Integer:
- U8
- "3"
- span:
line_start: 1
line_stop: 1
col_start: 15
col_stop: 18
path: ""
content: 1u8 >= 2u8 >= 3u8
op: Ge
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 18
path: ""
content: 1u8 >= 2u8 >= 3u8
- Binary:
left:
Binary:
@ -200,179 +145,3 @@ outputs:
col_stop: 23
path: ""
content: 1u8 + 2u8 >= 3u8 + 4u8
- Binary:
left:
Binary:
left:
Value:
Integer:
- U8
- "1"
- span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 4
path: ""
content: 1u8 >= 2u8 >= 3u8
right:
Value:
Integer:
- U8
- "2"
- span:
line_start: 1
line_stop: 1
col_start: 8
col_stop: 11
path: ""
content: 1u8 >= 2u8 >= 3u8
op: Ge
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 11
path: ""
content: 1u8 >= 2u8 >= 3u8
right:
Value:
Integer:
- U8
- "3"
- span:
line_start: 1
line_stop: 1
col_start: 15
col_stop: 18
path: ""
content: 1u8 >= 2u8 >= 3u8
op: Ge
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 18
path: ""
content: 1u8 >= 2u8 >= 3u8
- Binary:
left:
Binary:
left:
Binary:
left:
Value:
Integer:
- U8
- "1"
- span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 4
path: ""
content: 1u8 + 2u8 >= 3u8 + 4u8 >= 5u8 + 6u8
right:
Value:
Integer:
- U8
- "2"
- span:
line_start: 1
line_stop: 1
col_start: 7
col_stop: 10
path: ""
content: 1u8 + 2u8 >= 3u8 + 4u8 >= 5u8 + 6u8
op: Add
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 10
path: ""
content: 1u8 + 2u8 >= 3u8 + 4u8 >= 5u8 + 6u8
right:
Binary:
left:
Value:
Integer:
- U8
- "3"
- span:
line_start: 1
line_stop: 1
col_start: 14
col_stop: 17
path: ""
content: 1u8 + 2u8 >= 3u8 + 4u8 >= 5u8 + 6u8
right:
Value:
Integer:
- U8
- "4"
- span:
line_start: 1
line_stop: 1
col_start: 20
col_stop: 23
path: ""
content: 1u8 + 2u8 >= 3u8 + 4u8 >= 5u8 + 6u8
op: Add
span:
line_start: 1
line_stop: 1
col_start: 14
col_stop: 23
path: ""
content: 1u8 + 2u8 >= 3u8 + 4u8 >= 5u8 + 6u8
op: Ge
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 23
path: ""
content: 1u8 + 2u8 >= 3u8 + 4u8 >= 5u8 + 6u8
right:
Binary:
left:
Value:
Integer:
- U8
- "5"
- span:
line_start: 1
line_stop: 1
col_start: 27
col_stop: 30
path: ""
content: 1u8 + 2u8 >= 3u8 + 4u8 >= 5u8 + 6u8
right:
Value:
Integer:
- U8
- "6"
- span:
line_start: 1
line_stop: 1
col_start: 33
col_stop: 36
path: ""
content: 1u8 + 2u8 >= 3u8 + 4u8 >= 5u8 + 6u8
op: Add
span:
line_start: 1
line_stop: 1
col_start: 27
col_stop: 36
path: ""
content: 1u8 + 2u8 >= 3u8 + 4u8 >= 5u8 + 6u8
op: Ge
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 36
path: ""
content: 1u8 + 2u8 >= 3u8 + 4u8 >= 5u8 + 6u8

View File

@ -0,0 +1,7 @@
---
namespace: ParseExpression
expectation: Fail
outputs:
- "did not consume all input: '>=' @ 1:12-14\n'3' @ 1:15-16\n'u8' @ 1:16-18\n"
- "did not consume all input: '>=' @ 1:12-14\n'3' @ 1:15-16\n'u8' @ 1:16-18\n"
- "did not consume all input: '>=' @ 1:24-26\n'5' @ 1:27-28\n'u8' @ 1:28-30\n'+' @ 1:31-32\n'6' @ 1:33-34\n'u8' @ 1:34-36\n"

View File

@ -68,61 +68,6 @@ outputs:
col_stop: 8
path: ""
content: 2u8<3u8
- Binary:
left:
Binary:
left:
Value:
Integer:
- U8
- "1"
- span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 4
path: ""
content: 1u8 < 2u8 < 3u8
right:
Value:
Integer:
- U8
- "2"
- span:
line_start: 1
line_stop: 1
col_start: 7
col_stop: 10
path: ""
content: 1u8 < 2u8 < 3u8
op: Lt
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 10
path: ""
content: 1u8 < 2u8 < 3u8
right:
Value:
Integer:
- U8
- "3"
- span:
line_start: 1
line_stop: 1
col_start: 13
col_stop: 16
path: ""
content: 1u8 < 2u8 < 3u8
op: Lt
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 16
path: ""
content: 1u8 < 2u8 < 3u8
- Binary:
left:
Binary:
@ -200,179 +145,3 @@ outputs:
col_stop: 22
path: ""
content: 1u8 + 2u8 < 3u8 + 4u8
- Binary:
left:
Binary:
left:
Value:
Integer:
- U8
- "1"
- span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 4
path: ""
content: 1u8 < 2u8 < 3u8
right:
Value:
Integer:
- U8
- "2"
- span:
line_start: 1
line_stop: 1
col_start: 7
col_stop: 10
path: ""
content: 1u8 < 2u8 < 3u8
op: Lt
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 10
path: ""
content: 1u8 < 2u8 < 3u8
right:
Value:
Integer:
- U8
- "3"
- span:
line_start: 1
line_stop: 1
col_start: 13
col_stop: 16
path: ""
content: 1u8 < 2u8 < 3u8
op: Lt
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 16
path: ""
content: 1u8 < 2u8 < 3u8
- Binary:
left:
Binary:
left:
Binary:
left:
Value:
Integer:
- U8
- "1"
- span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 4
path: ""
content: 1u8 + 2u8 < 3u8 + 4u8 < 5u8 + 6u8
right:
Value:
Integer:
- U8
- "2"
- span:
line_start: 1
line_stop: 1
col_start: 7
col_stop: 10
path: ""
content: 1u8 + 2u8 < 3u8 + 4u8 < 5u8 + 6u8
op: Add
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 10
path: ""
content: 1u8 + 2u8 < 3u8 + 4u8 < 5u8 + 6u8
right:
Binary:
left:
Value:
Integer:
- U8
- "3"
- span:
line_start: 1
line_stop: 1
col_start: 13
col_stop: 16
path: ""
content: 1u8 + 2u8 < 3u8 + 4u8 < 5u8 + 6u8
right:
Value:
Integer:
- U8
- "4"
- span:
line_start: 1
line_stop: 1
col_start: 19
col_stop: 22
path: ""
content: 1u8 + 2u8 < 3u8 + 4u8 < 5u8 + 6u8
op: Add
span:
line_start: 1
line_stop: 1
col_start: 13
col_stop: 22
path: ""
content: 1u8 + 2u8 < 3u8 + 4u8 < 5u8 + 6u8
op: Lt
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 22
path: ""
content: 1u8 + 2u8 < 3u8 + 4u8 < 5u8 + 6u8
right:
Binary:
left:
Value:
Integer:
- U8
- "5"
- span:
line_start: 1
line_stop: 1
col_start: 25
col_stop: 28
path: ""
content: 1u8 + 2u8 < 3u8 + 4u8 < 5u8 + 6u8
right:
Value:
Integer:
- U8
- "6"
- span:
line_start: 1
line_stop: 1
col_start: 31
col_stop: 34
path: ""
content: 1u8 + 2u8 < 3u8 + 4u8 < 5u8 + 6u8
op: Add
span:
line_start: 1
line_stop: 1
col_start: 25
col_stop: 34
path: ""
content: 1u8 + 2u8 < 3u8 + 4u8 < 5u8 + 6u8
op: Lt
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 34
path: ""
content: 1u8 + 2u8 < 3u8 + 4u8 < 5u8 + 6u8

View File

@ -0,0 +1,7 @@
---
namespace: ParseExpression
expectation: Fail
outputs:
- "did not consume all input: '<' @ 1:11-12\n'3' @ 1:13-14\n'u8' @ 1:14-16\n"
- "did not consume all input: '<' @ 1:11-12\n'3' @ 1:13-14\n'u8' @ 1:14-16\n"
- "did not consume all input: '<' @ 1:23-24\n'5' @ 1:25-26\n'u8' @ 1:26-28\n'+' @ 1:29-30\n'6' @ 1:31-32\n'u8' @ 1:32-34\n"

View File

@ -68,61 +68,6 @@ outputs:
col_stop: 11
path: ""
content: 2u8 <= 3u8
- Binary:
left:
Binary:
left:
Value:
Integer:
- U8
- "1"
- span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 4
path: ""
content: 1u8 <= 2u8 <= 3u8
right:
Value:
Integer:
- U8
- "2"
- span:
line_start: 1
line_stop: 1
col_start: 8
col_stop: 11
path: ""
content: 1u8 <= 2u8 <= 3u8
op: Le
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 11
path: ""
content: 1u8 <= 2u8 <= 3u8
right:
Value:
Integer:
- U8
- "3"
- span:
line_start: 1
line_stop: 1
col_start: 15
col_stop: 18
path: ""
content: 1u8 <= 2u8 <= 3u8
op: Le
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 18
path: ""
content: 1u8 <= 2u8 <= 3u8
- Binary:
left:
Binary:
@ -200,179 +145,3 @@ outputs:
col_stop: 23
path: ""
content: 1u8 + 2u8 <= 3u8 + 4u8
- Binary:
left:
Binary:
left:
Value:
Integer:
- U8
- "1"
- span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 4
path: ""
content: 1u8 <= 2u8 <= 3u8
right:
Value:
Integer:
- U8
- "2"
- span:
line_start: 1
line_stop: 1
col_start: 8
col_stop: 11
path: ""
content: 1u8 <= 2u8 <= 3u8
op: Le
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 11
path: ""
content: 1u8 <= 2u8 <= 3u8
right:
Value:
Integer:
- U8
- "3"
- span:
line_start: 1
line_stop: 1
col_start: 15
col_stop: 18
path: ""
content: 1u8 <= 2u8 <= 3u8
op: Le
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 18
path: ""
content: 1u8 <= 2u8 <= 3u8
- Binary:
left:
Binary:
left:
Binary:
left:
Value:
Integer:
- U8
- "1"
- span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 4
path: ""
content: 1u8 + 2u8 <= 3u8 + 4u8 <= 5u8 + 6u8
right:
Value:
Integer:
- U8
- "2"
- span:
line_start: 1
line_stop: 1
col_start: 7
col_stop: 10
path: ""
content: 1u8 + 2u8 <= 3u8 + 4u8 <= 5u8 + 6u8
op: Add
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 10
path: ""
content: 1u8 + 2u8 <= 3u8 + 4u8 <= 5u8 + 6u8
right:
Binary:
left:
Value:
Integer:
- U8
- "3"
- span:
line_start: 1
line_stop: 1
col_start: 14
col_stop: 17
path: ""
content: 1u8 + 2u8 <= 3u8 + 4u8 <= 5u8 + 6u8
right:
Value:
Integer:
- U8
- "4"
- span:
line_start: 1
line_stop: 1
col_start: 20
col_stop: 23
path: ""
content: 1u8 + 2u8 <= 3u8 + 4u8 <= 5u8 + 6u8
op: Add
span:
line_start: 1
line_stop: 1
col_start: 14
col_stop: 23
path: ""
content: 1u8 + 2u8 <= 3u8 + 4u8 <= 5u8 + 6u8
op: Le
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 23
path: ""
content: 1u8 + 2u8 <= 3u8 + 4u8 <= 5u8 + 6u8
right:
Binary:
left:
Value:
Integer:
- U8
- "5"
- span:
line_start: 1
line_stop: 1
col_start: 27
col_stop: 30
path: ""
content: 1u8 + 2u8 <= 3u8 + 4u8 <= 5u8 + 6u8
right:
Value:
Integer:
- U8
- "6"
- span:
line_start: 1
line_stop: 1
col_start: 33
col_stop: 36
path: ""
content: 1u8 + 2u8 <= 3u8 + 4u8 <= 5u8 + 6u8
op: Add
span:
line_start: 1
line_stop: 1
col_start: 27
col_stop: 36
path: ""
content: 1u8 + 2u8 <= 3u8 + 4u8 <= 5u8 + 6u8
op: Le
span:
line_start: 1
line_stop: 1
col_start: 1
col_stop: 36
path: ""
content: 1u8 + 2u8 <= 3u8 + 4u8 <= 5u8 + 6u8

View File

@ -0,0 +1,7 @@
---
namespace: ParseExpression
expectation: Fail
outputs:
- "did not consume all input: '<=' @ 1:12-14\n'3' @ 1:15-16\n'u8' @ 1:16-18\n"
- "did not consume all input: '<=' @ 1:12-14\n'3' @ 1:15-16\n'u8' @ 1:16-18\n"
- "did not consume all input: '<=' @ 1:24-26\n'5' @ 1:27-28\n'u8' @ 1:28-30\n'+' @ 1:31-32\n'6' @ 1:33-34\n'u8' @ 1:34-36\n"

View File

@ -7,10 +7,4 @@ expectation: Pass
2u8>3u8
1u8 > 2u8 > 3u8
1u8 + 2u8 > 3u8 + 4u8
1u8 > 2u8 > 3u8
1u8 + 2u8 > 3u8 + 4u8 > 5u8 + 6u8
1u8 + 2u8 > 3u8 + 4u8

View File

@ -0,0 +1,10 @@
/*
namespace: ParseExpression
expectation: Fail
*/
1u8 > 2u8 > 3u8
1u8 > 2u8 > 3u8
1u8 + 2u8 > 3u8 + 4u8 > 5u8 + 6u8

View File

@ -7,10 +7,4 @@ expectation: Pass
2u8 >= 3u8
1u8 >= 2u8 >= 3u8
1u8 + 2u8 >= 3u8 + 4u8
1u8 >= 2u8 >= 3u8
1u8 + 2u8 >= 3u8 + 4u8 >= 5u8 + 6u8
1u8 + 2u8 >= 3u8 + 4u8

View File

@ -0,0 +1,10 @@
/*
namespace: ParseExpression
expectation: Fail
*/
1u8 >= 2u8 >= 3u8
1u8 >= 2u8 >= 3u8
1u8 + 2u8 >= 3u8 + 4u8 >= 5u8 + 6u8

View File

@ -7,10 +7,4 @@ expectation: Pass
2u8<3u8
1u8 < 2u8 < 3u8
1u8 + 2u8 < 3u8 + 4u8
1u8 < 2u8 < 3u8
1u8 + 2u8 < 3u8 + 4u8 < 5u8 + 6u8
1u8 + 2u8 < 3u8 + 4u8

View File

@ -0,0 +1,10 @@
/*
namespace: ParseExpression
expectation: Fail
*/
1u8 < 2u8 < 3u8
1u8 < 2u8 < 3u8
1u8 + 2u8 < 3u8 + 4u8 < 5u8 + 6u8

View File

@ -7,10 +7,4 @@ expectation: Pass
2u8 <= 3u8
1u8 <= 2u8 <= 3u8
1u8 + 2u8 <= 3u8 + 4u8
1u8 <= 2u8 <= 3u8
1u8 + 2u8 <= 3u8 + 4u8 <= 5u8 + 6u8
1u8 + 2u8 <= 3u8 + 4u8

View File

@ -0,0 +1,10 @@
/*
namespace: ParseExpression
expectation: Fail
*/
1u8 <= 2u8 <= 3u8
1u8 <= 2u8 <= 3u8
1u8 + 2u8 <= 3u8 + 4u8 <= 5u8 + 6u8