aboutsummaryrefslogtreecommitdiff
path: root/src/grammar/expressions
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-07 12:24:03 +0100
committerAleksey Kladov <[email protected]>2018-08-07 12:24:03 +0100
commit8908e51aeaee91d6b63684a21ed1ca16de49916f (patch)
tree30fa7b9ac02c7326d3c7fbd192ebe2ef46b82d58 /src/grammar/expressions
parent498098a5d968f06f08fa093f1fd5a43a9a61ecf3 (diff)
full precedence
Diffstat (limited to 'src/grammar/expressions')
-rw-r--r--src/grammar/expressions/mod.rs56
1 files changed, 30 insertions, 26 deletions
diff --git a/src/grammar/expressions/mod.rs b/src/grammar/expressions/mod.rs
index c137fe654..5b7228146 100644
--- a/src/grammar/expressions/mod.rs
+++ b/src/grammar/expressions/mod.rs
@@ -3,7 +3,7 @@ mod atom;
3use super::*; 3use super::*;
4pub(super) use self::atom::literal; 4pub(super) use self::atom::literal;
5 5
6const EXPR_FIRST: TokenSet = UNARY_EXPR_FIRST; 6const EXPR_FIRST: TokenSet = LHS_FIRST;
7 7
8pub(super) fn expr(p: &mut Parser) { 8pub(super) fn expr(p: &mut Parser) {
9 let r = Restrictions { forbid_structs: false }; 9 let r = Restrictions { forbid_structs: false };
@@ -38,26 +38,6 @@ enum Op {
38 Composite(SyntaxKind, u8), 38 Composite(SyntaxKind, u8),
39} 39}
40 40
41// test expr_binding_power
42// fn foo() {
43// 1 + 2 * 3 == 1 * 2 + 3;
44// *x = 1 + 1;
45// }
46
47// test range_binding_power
48// fn foo() {
49// .. 1 + 1;
50// .. z = 2;
51// x = false .. 1 == 1;
52// }
53
54// test compound_ops
55// fn foo() {
56// x += 1;
57// 1 + 1 <= 2 * 3;
58// z -= 3 >= 0;
59// true || true && false;
60// }
61fn current_op(p: &Parser) -> (u8, Op) { 41fn current_op(p: &Parser) -> (u8, Op) {
62 if p.at_compound2(PLUS, EQ) { 42 if p.at_compound2(PLUS, EQ) {
63 return (1, Op::Composite(PLUSEQ, 2)); 43 return (1, Op::Composite(PLUSEQ, 2));
@@ -65,6 +45,12 @@ fn current_op(p: &Parser) -> (u8, Op) {
65 if p.at_compound2(MINUS, EQ) { 45 if p.at_compound2(MINUS, EQ) {
66 return (1, Op::Composite(MINUSEQ, 2)); 46 return (1, Op::Composite(MINUSEQ, 2));
67 } 47 }
48 if p.at_compound3(L_ANGLE, L_ANGLE, EQ) {
49 return (1, Op::Composite(SHLEQ, 3));
50 }
51 if p.at_compound3(R_ANGLE, R_ANGLE, EQ) {
52 return (1, Op::Composite(SHREQ, 3));
53 }
68 if p.at_compound2(PIPE, PIPE) { 54 if p.at_compound2(PIPE, PIPE) {
69 return (3, Op::Composite(PIPEPIPE, 2)); 55 return (3, Op::Composite(PIPEPIPE, 2));
70 } 56 }
@@ -77,13 +63,22 @@ fn current_op(p: &Parser) -> (u8, Op) {
77 if p.at_compound2(R_ANGLE, EQ) { 63 if p.at_compound2(R_ANGLE, EQ) {
78 return (5, Op::Composite(GTEQ, 2)); 64 return (5, Op::Composite(GTEQ, 2));
79 } 65 }
66 if p.at_compound2(L_ANGLE, L_ANGLE) {
67 return (9, Op::Composite(SHL, 2));
68 }
69 if p.at_compound2(R_ANGLE, R_ANGLE) {
70 return (9, Op::Composite(SHR, 2));
71 }
80 72
81 let bp = match p.current() { 73 let bp = match p.current() {
82 EQ => 1, 74 EQ => 1,
83 DOTDOT => 2, 75 DOTDOT => 2,
84 EQEQ | NEQ => 5, 76 EQEQ | NEQ => 5,
85 MINUS | PLUS => 6, 77 PIPE => 6,
86 STAR | SLASH => 7, 78 CARET => 7,
79 AMP => 8,
80 MINUS | PLUS => 10,
81 STAR | SLASH | PERCENT => 11,
87 _ => 0, 82 _ => 0,
88 }; 83 };
89 (bp, Op::Simple) 84 (bp, Op::Simple)
@@ -107,13 +102,13 @@ fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) {
107 p.bump_compound(kind, n); 102 p.bump_compound(kind, n);
108 } 103 }
109 } 104 }
110 lhs = bin_expr(p, r, lhs, op_bp); 105 lhs = bin_expr(p, r, lhs, op_bp + 1);
111 } 106 }
112} 107}
113 108
114const UNARY_EXPR_FIRST: TokenSet = 109const LHS_FIRST: TokenSet =
115 token_set_union![ 110 token_set_union![
116 token_set![AMP, STAR, EXCL], 111 token_set![AMP, STAR, EXCL, DOTDOT, MINUS],
117 atom::ATOM_EXPR_FIRST, 112 atom::ATOM_EXPR_FIRST,
118 ]; 113 ];
119 114
@@ -149,6 +144,15 @@ fn lhs(p: &mut Parser, r: Restrictions) -> Option<CompletedMarker> {
149 p.bump(); 144 p.bump();
150 NOT_EXPR 145 NOT_EXPR
151 } 146 }
147 // test neg_expr
148 // fn foo() {
149 // --1;
150 // }
151 MINUS => {
152 m = p.start();
153 p.bump();
154 NEG_EXPR
155 }
152 DOTDOT => { 156 DOTDOT => {
153 m = p.start(); 157 m = p.start();
154 p.bump(); 158 p.bump();