Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle operator associavity #75

Merged
merged 3 commits into from
Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ where
self.parse_fn()
}
}
fn handle_operation(
fn handle_operation_left(
&mut self,
once: bool,
next: fn(&mut Self) -> Checkpoint,
Expand All @@ -684,17 +684,31 @@ where
}
checkpoint
}
fn handle_operation_right(
&mut self,
next: fn(&mut Self) -> Checkpoint,
ops: &[SyntaxKind],
) -> Checkpoint {
let checkpoint = next(self);
if self.peek().map(|t| ops.contains(&t)).unwrap_or(false) {
self.start_node_at(checkpoint, NODE_BIN_OP);
self.bump();
self.handle_operation_right(next, ops);
self.finish_node();
}
checkpoint
}
fn parse_isset(&mut self) -> Checkpoint {
self.handle_operation(false, Self::parse_negate, &[TOKEN_QUESTION])
self.handle_operation_left(false, Self::parse_negate, &[TOKEN_QUESTION])
}
fn parse_concat(&mut self) -> Checkpoint {
self.handle_operation(false, Self::parse_isset, &[TOKEN_CONCAT])
self.handle_operation_right(Self::parse_isset, &[TOKEN_CONCAT])
}
fn parse_mul(&mut self) -> Checkpoint {
self.handle_operation(false, Self::parse_concat, &[TOKEN_MUL, TOKEN_DIV])
self.handle_operation_left(false, Self::parse_concat, &[TOKEN_MUL, TOKEN_DIV])
}
fn parse_add(&mut self) -> Checkpoint {
self.handle_operation(false, Self::parse_mul, &[TOKEN_ADD, TOKEN_SUB])
self.handle_operation_left(false, Self::parse_mul, &[TOKEN_ADD, TOKEN_SUB])
}
fn parse_invert(&mut self) -> Checkpoint {
if self.peek() == Some(TOKEN_INVERT) {
Expand All @@ -709,26 +723,26 @@ where
}
}
fn parse_merge(&mut self) -> Checkpoint {
self.handle_operation(false, Self::parse_invert, &[TOKEN_UPDATE])
self.handle_operation_right(Self::parse_invert, &[TOKEN_UPDATE])
}
fn parse_compare(&mut self) -> Checkpoint {
self.handle_operation(
self.handle_operation_left(
true,
Self::parse_merge,
&[TOKEN_LESS, TOKEN_LESS_OR_EQ, TOKEN_MORE, TOKEN_MORE_OR_EQ],
)
}
fn parse_equal(&mut self) -> Checkpoint {
self.handle_operation(true, Self::parse_compare, &[TOKEN_EQUAL, TOKEN_NOT_EQUAL])
self.handle_operation_left(true, Self::parse_compare, &[TOKEN_EQUAL, TOKEN_NOT_EQUAL])
}
fn parse_and(&mut self) -> Checkpoint {
self.handle_operation(false, Self::parse_equal, &[TOKEN_AND])
self.handle_operation_left(false, Self::parse_equal, &[TOKEN_AND])
}
fn parse_or(&mut self) -> Checkpoint {
self.handle_operation(false, Self::parse_and, &[TOKEN_OR])
self.handle_operation_left(false, Self::parse_and, &[TOKEN_OR])
}
fn parse_implication(&mut self) -> Checkpoint {
self.handle_operation(false, Self::parse_or, &[TOKEN_IMPLICATION])
self.handle_operation_right(Self::parse_or, &[TOKEN_IMPLICATION])
}
#[inline(always)]
fn parse_math(&mut self) -> Checkpoint {
Expand Down
38 changes: 19 additions & 19 deletions test_data/general/lists.expect
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ NODE_ROOT 0..62 {
TOKEN_ASSIGN("=") 38..39
TOKEN_WHITESPACE(" ") 39..40
NODE_BIN_OP 40..59 {
NODE_BIN_OP 40..52 {
NODE_LIST 40..43 {
TOKEN_SQUARE_B_OPEN("[") 40..41
NODE_LITERAL 41..42 {
TOKEN_INTEGER("1") 41..42
}
TOKEN_SQUARE_B_CLOSE("]") 42..43
NODE_LIST 40..43 {
TOKEN_SQUARE_B_OPEN("[") 40..41
NODE_LITERAL 41..42 {
TOKEN_INTEGER("1") 41..42
}
TOKEN_WHITESPACE(" ") 43..44
TOKEN_CONCAT("++") 44..46
TOKEN_WHITESPACE(" ") 46..47
TOKEN_SQUARE_B_CLOSE("]") 42..43
}
TOKEN_WHITESPACE(" ") 43..44
TOKEN_CONCAT("++") 44..46
TOKEN_WHITESPACE(" ") 46..47
NODE_BIN_OP 47..59 {
NODE_LIST 47..52 {
TOKEN_SQUARE_B_OPEN("[") 47..48
NODE_LITERAL 48..49 {
Expand All @@ -77,16 +77,16 @@ NODE_ROOT 0..62 {
}
TOKEN_SQUARE_B_CLOSE("]") 51..52
}
}
TOKEN_WHITESPACE(" ") 52..53
TOKEN_CONCAT("++") 53..55
TOKEN_WHITESPACE(" ") 55..56
NODE_LIST 56..59 {
TOKEN_SQUARE_B_OPEN("[") 56..57
NODE_LITERAL 57..58 {
TOKEN_INTEGER("4") 57..58
TOKEN_WHITESPACE(" ") 52..53
TOKEN_CONCAT("++") 53..55
TOKEN_WHITESPACE(" ") 55..56
NODE_LIST 56..59 {
TOKEN_SQUARE_B_OPEN("[") 56..57
NODE_LITERAL 57..58 {
TOKEN_INTEGER("4") 57..58
}
TOKEN_SQUARE_B_CLOSE("]") 58..59
}
TOKEN_SQUARE_B_CLOSE("]") 58..59
}
}
TOKEN_SEMICOLON(";") 59..60
Expand Down
111 changes: 111 additions & 0 deletions test_data/general/operators.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
NODE_ROOT 0..71 {
NODE_LIST 0..71 {
TOKEN_SQUARE_B_OPEN("[") 0..1
TOKEN_WHITESPACE("\n ") 1..4
NODE_PAREN 4..17 {
TOKEN_PAREN_OPEN("(") 4..5
NODE_BIN_OP 5..16 {
NODE_IDENT 5..6 {
TOKEN_IDENT("a") 5..6
}
TOKEN_WHITESPACE(" ") 6..7
TOKEN_IMPLICATION("->") 7..9
TOKEN_WHITESPACE(" ") 9..10
NODE_BIN_OP 10..16 {
NODE_IDENT 10..11 {
TOKEN_IDENT("b") 10..11
}
TOKEN_WHITESPACE(" ") 11..12
TOKEN_IMPLICATION("->") 12..14
TOKEN_WHITESPACE(" ") 14..15
NODE_IDENT 15..16 {
TOKEN_IDENT("c") 15..16
}
}
}
TOKEN_PAREN_CLOSE(")") 16..17
}
TOKEN_WHITESPACE("\n ") 17..20
NODE_PAREN 20..33 {
TOKEN_PAREN_OPEN("(") 20..21
NODE_BIN_OP 21..32 {
NODE_IDENT 21..22 {
TOKEN_IDENT("a") 21..22
}
TOKEN_WHITESPACE(" ") 22..23
TOKEN_CONCAT("++") 23..25
TOKEN_WHITESPACE(" ") 25..26
NODE_BIN_OP 26..32 {
NODE_IDENT 26..27 {
TOKEN_IDENT("b") 26..27
}
TOKEN_WHITESPACE(" ") 27..28
TOKEN_CONCAT("++") 28..30
TOKEN_WHITESPACE(" ") 30..31
NODE_IDENT 31..32 {
TOKEN_IDENT("c") 31..32
}
}
}
TOKEN_PAREN_CLOSE(")") 32..33
}
TOKEN_WHITESPACE("\n ") 33..36
NODE_PAREN 36..49 {
TOKEN_PAREN_OPEN("(") 36..37
NODE_BIN_OP 37..48 {
NODE_IDENT 37..38 {
TOKEN_IDENT("a") 37..38
}
TOKEN_WHITESPACE(" ") 38..39
TOKEN_UPDATE("//") 39..41
TOKEN_WHITESPACE(" ") 41..42
NODE_BIN_OP 42..48 {
NODE_IDENT 42..43 {
TOKEN_IDENT("b") 42..43
}
TOKEN_WHITESPACE(" ") 43..44
TOKEN_UPDATE("//") 44..46
TOKEN_WHITESPACE(" ") 46..47
NODE_IDENT 47..48 {
TOKEN_IDENT("c") 47..48
}
}
}
TOKEN_PAREN_CLOSE(")") 48..49
}
TOKEN_WHITESPACE("\n ") 49..52
NODE_PAREN 52..69 {
TOKEN_PAREN_OPEN("(") 52..53
NODE_BIN_OP 53..68 {
NODE_BIN_OP 53..63 {
NODE_BIN_OP 53..58 {
NODE_IDENT 53..54 {
TOKEN_IDENT("a") 53..54
}
TOKEN_WHITESPACE(" ") 54..55
TOKEN_ADD("+") 55..56
TOKEN_WHITESPACE(" ") 56..57
NODE_IDENT 57..58 {
TOKEN_IDENT("b") 57..58
}
}
TOKEN_WHITESPACE(" ") 58..59
TOKEN_UPDATE("//") 59..61
TOKEN_WHITESPACE(" ") 61..62
NODE_IDENT 62..63 {
TOKEN_IDENT("c") 62..63
}
}
TOKEN_WHITESPACE(" ") 63..64
TOKEN_IMPLICATION("->") 64..66
TOKEN_WHITESPACE(" ") 66..67
NODE_IDENT 67..68 {
TOKEN_IDENT("d") 67..68
}
}
TOKEN_PAREN_CLOSE(")") 68..69
}
TOKEN_WHITESPACE("\n") 69..70
TOKEN_SQUARE_B_CLOSE("]") 70..71
}
}
6 changes: 6 additions & 0 deletions test_data/general/operators.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
(a -> b -> c)
(a ++ b ++ c)
(a // b // c)
(a + b // c -> d)
]
30 changes: 15 additions & 15 deletions test_data/parser/list/2.expect
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
NODE_ROOT 0..15 {
NODE_BIN_OP 0..15 {
NODE_BIN_OP 0..10 {
NODE_LIST 0..3 {
TOKEN_SQUARE_B_OPEN("[") 0..1
NODE_LITERAL 1..2 {
TOKEN_INTEGER("1") 1..2
}
TOKEN_SQUARE_B_CLOSE("]") 2..3
NODE_LIST 0..3 {
TOKEN_SQUARE_B_OPEN("[") 0..1
NODE_LITERAL 1..2 {
TOKEN_INTEGER("1") 1..2
}
TOKEN_CONCAT("++") 3..5
TOKEN_SQUARE_B_CLOSE("]") 2..3
}
TOKEN_CONCAT("++") 3..5
NODE_BIN_OP 5..15 {
NODE_LIST 5..10 {
TOKEN_SQUARE_B_OPEN("[") 5..6
NODE_IDENT 6..9 {
TOKEN_IDENT("two") 6..9
}
TOKEN_SQUARE_B_CLOSE("]") 9..10
}
}
TOKEN_CONCAT("++") 10..12
NODE_LIST 12..15 {
TOKEN_SQUARE_B_OPEN("[") 12..13
NODE_LITERAL 13..14 {
TOKEN_INTEGER("3") 13..14
TOKEN_CONCAT("++") 10..12
NODE_LIST 12..15 {
TOKEN_SQUARE_B_OPEN("[") 12..13
NODE_LITERAL 13..14 {
TOKEN_INTEGER("3") 13..14
}
TOKEN_SQUARE_B_CLOSE("]") 14..15
}
TOKEN_SQUARE_B_CLOSE("]") 14..15
}
}
}