aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-07-31 15:35:54 +0100
committerAleksey Kladov <[email protected]>2018-07-31 15:35:54 +0100
commit9f87c9a3f927d3b7a3cbb92818c45c979ece693b (patch)
tree19096a70ffa38bd53071c910a89a995e54a4b765
parent9ce7e8110254e8db476c96bce2eecb2d16983159 (diff)
Field expr
-rw-r--r--src/grammar.ron2
-rw-r--r--src/parser/grammar/expressions.rs43
-rw-r--r--src/syntax_kinds/generated.rs4
-rw-r--r--tests/data/parser/inline/0051_method_call_expr.rs4
-rw-r--r--tests/data/parser/inline/0051_method_call_expr.txt50
-rw-r--r--tests/data/parser/inline/0052_field_expr.rs3
-rw-r--r--tests/data/parser/inline/0052_field_expr.txt29
-rw-r--r--tools/src/lib.rs1
8 files changed, 131 insertions, 5 deletions
diff --git a/src/grammar.ron b/src/grammar.ron
index a8c922040..fca29f1ef 100644
--- a/src/grammar.ron
+++ b/src/grammar.ron
@@ -126,6 +126,8 @@ Grammar(
126 "TUPLE_EXPR", 126 "TUPLE_EXPR",
127 "PATH_EXPR", 127 "PATH_EXPR",
128 "CALL_EXPR", 128 "CALL_EXPR",
129 "METHOD_CALL_EXPR",
130 "FIELD_EXPR",
129 "REF_EXPR", 131 "REF_EXPR",
130 132
131 "EXTERN_BLOCK", 133 "EXTERN_BLOCK",
diff --git a/src/parser/grammar/expressions.rs b/src/parser/grammar/expressions.rs
index 11bb3436b..3956df568 100644
--- a/src/parser/grammar/expressions.rs
+++ b/src/parser/grammar/expressions.rs
@@ -26,11 +26,20 @@ pub(super) fn literal(p: &mut Parser) -> Option<CompletedMarker> {
26} 26}
27 27
28pub(super) fn expr(p: &mut Parser) { 28pub(super) fn expr(p: &mut Parser) {
29 let mut lhs = prefix_expr(p); 29 let mut lhs = match prefix_expr(p) {
30 Some(lhs) => lhs,
31 None => return,
32 };
30 33
31 while let Some(m) = lhs { 34 loop {
32 match p.current() { 35 lhs = match p.current() {
33 L_PAREN => lhs = Some(call_expr(p, m)), 36 L_PAREN => call_expr(p, lhs),
37 DOT if p.nth(1) == IDENT =>
38 if p.nth(2) == L_PAREN {
39 method_call_expr(p, lhs)
40 } else {
41 field_expr(p, lhs)
42 }
34 _ => break, 43 _ => break,
35 } 44 }
36 } 45 }
@@ -95,6 +104,32 @@ fn call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
95 m.complete(p, CALL_EXPR) 104 m.complete(p, CALL_EXPR)
96} 105}
97 106
107// test method_call_expr
108// fn foo() {
109// x.foo();
110// y.bar(1, 2,);
111// }
112fn method_call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
113 assert!(p.at(DOT) && p.nth(1) == IDENT && p.nth(2) == L_PAREN);
114 let m = lhs.precede(p);
115 p.bump();
116 p.bump();
117 arg_list(p);
118 m.complete(p, METHOD_CALL_EXPR)
119}
120
121// test field_expr
122// fn foo() {
123// x.foo.bar;
124// }
125fn field_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
126 assert!(p.at(DOT) && p.nth(1) == IDENT);
127 let m = lhs.precede(p);
128 p.bump();
129 p.bump();
130 m.complete(p, FIELD_EXPR)
131}
132
98fn arg_list(p: &mut Parser) { 133fn arg_list(p: &mut Parser) {
99 assert!(p.at(L_PAREN)); 134 assert!(p.at(L_PAREN));
100 let m = p.start(); 135 let m = p.start();
diff --git a/src/syntax_kinds/generated.rs b/src/syntax_kinds/generated.rs
index c49ad9a59..699f5282e 100644
--- a/src/syntax_kinds/generated.rs
+++ b/src/syntax_kinds/generated.rs
@@ -117,6 +117,8 @@ pub enum SyntaxKind {
117 TUPLE_EXPR, 117 TUPLE_EXPR,
118 PATH_EXPR, 118 PATH_EXPR,
119 CALL_EXPR, 119 CALL_EXPR,
120 METHOD_CALL_EXPR,
121 FIELD_EXPR,
120 REF_EXPR, 122 REF_EXPR,
121 EXTERN_BLOCK, 123 EXTERN_BLOCK,
122 ENUM_VARIANT, 124 ENUM_VARIANT,
@@ -268,6 +270,8 @@ impl SyntaxKind {
268 TUPLE_EXPR => &SyntaxInfo { name: "TUPLE_EXPR" }, 270 TUPLE_EXPR => &SyntaxInfo { name: "TUPLE_EXPR" },
269 PATH_EXPR => &SyntaxInfo { name: "PATH_EXPR" }, 271 PATH_EXPR => &SyntaxInfo { name: "PATH_EXPR" },
270 CALL_EXPR => &SyntaxInfo { name: "CALL_EXPR" }, 272 CALL_EXPR => &SyntaxInfo { name: "CALL_EXPR" },
273 METHOD_CALL_EXPR => &SyntaxInfo { name: "METHOD_CALL_EXPR" },
274 FIELD_EXPR => &SyntaxInfo { name: "FIELD_EXPR" },
271 REF_EXPR => &SyntaxInfo { name: "REF_EXPR" }, 275 REF_EXPR => &SyntaxInfo { name: "REF_EXPR" },
272 EXTERN_BLOCK => &SyntaxInfo { name: "EXTERN_BLOCK" }, 276 EXTERN_BLOCK => &SyntaxInfo { name: "EXTERN_BLOCK" },
273 ENUM_VARIANT => &SyntaxInfo { name: "ENUM_VARIANT" }, 277 ENUM_VARIANT => &SyntaxInfo { name: "ENUM_VARIANT" },
diff --git a/tests/data/parser/inline/0051_method_call_expr.rs b/tests/data/parser/inline/0051_method_call_expr.rs
new file mode 100644
index 000000000..236d94e88
--- /dev/null
+++ b/tests/data/parser/inline/0051_method_call_expr.rs
@@ -0,0 +1,4 @@
1fn foo() {
2 x.foo();
3 y.bar(1, 2,);
4}
diff --git a/tests/data/parser/inline/0051_method_call_expr.txt b/tests/data/parser/inline/0051_method_call_expr.txt
new file mode 100644
index 000000000..69e13bd2a
--- /dev/null
+++ b/tests/data/parser/inline/0051_method_call_expr.txt
@@ -0,0 +1,50 @@
1FILE@[0; 44)
2 FN_ITEM@[0; 44)
3 FN_KW@[0; 2)
4 NAME@[2; 6)
5 WHITESPACE@[2; 3)
6 IDENT@[3; 6) "foo"
7 PARAM_LIST@[6; 9)
8 L_PAREN@[6; 7)
9 R_PAREN@[7; 8)
10 WHITESPACE@[8; 9)
11 BLOCK@[9; 44)
12 L_CURLY@[9; 10)
13 EXPR_STMT@[10; 28)
14 METHOD_CALL_EXPR@[10; 22)
15 PATH_EXPR@[10; 16)
16 PATH@[10; 16)
17 PATH_SEGMENT@[10; 16)
18 NAME_REF@[10; 16)
19 WHITESPACE@[10; 15)
20 IDENT@[15; 16) "x"
21 DOT@[16; 17)
22 IDENT@[17; 20) "foo"
23 ARG_LIST@[20; 22)
24 L_PAREN@[20; 21)
25 R_PAREN@[21; 22)
26 SEMI@[22; 23)
27 WHITESPACE@[23; 28)
28 EXPR_STMT@[28; 42)
29 METHOD_CALL_EXPR@[28; 40)
30 PATH_EXPR@[28; 29)
31 PATH@[28; 29)
32 PATH_SEGMENT@[28; 29)
33 NAME_REF@[28; 29)
34 IDENT@[28; 29) "y"
35 DOT@[29; 30)
36 IDENT@[30; 33) "bar"
37 ARG_LIST@[33; 40)
38 L_PAREN@[33; 34)
39 LITERAL@[34; 35)
40 INT_NUMBER@[34; 35)
41 COMMA@[35; 36)
42 LITERAL@[36; 38)
43 WHITESPACE@[36; 37)
44 INT_NUMBER@[37; 38)
45 COMMA@[38; 39)
46 R_PAREN@[39; 40)
47 SEMI@[40; 41)
48 WHITESPACE@[41; 42)
49 R_CURLY@[42; 43)
50 WHITESPACE@[43; 44)
diff --git a/tests/data/parser/inline/0052_field_expr.rs b/tests/data/parser/inline/0052_field_expr.rs
new file mode 100644
index 000000000..874887eae
--- /dev/null
+++ b/tests/data/parser/inline/0052_field_expr.rs
@@ -0,0 +1,3 @@
1fn foo() {
2 x.foo.bar;
3}
diff --git a/tests/data/parser/inline/0052_field_expr.txt b/tests/data/parser/inline/0052_field_expr.txt
new file mode 100644
index 000000000..0ef2c5836
--- /dev/null
+++ b/tests/data/parser/inline/0052_field_expr.txt
@@ -0,0 +1,29 @@
1FILE@[0; 28)
2 FN_ITEM@[0; 28)
3 FN_KW@[0; 2)
4 NAME@[2; 6)
5 WHITESPACE@[2; 3)
6 IDENT@[3; 6) "foo"
7 PARAM_LIST@[6; 9)
8 L_PAREN@[6; 7)
9 R_PAREN@[7; 8)
10 WHITESPACE@[8; 9)
11 BLOCK@[9; 28)
12 L_CURLY@[9; 10)
13 EXPR_STMT@[10; 26)
14 FIELD_EXPR@[10; 24)
15 FIELD_EXPR@[10; 20)
16 PATH_EXPR@[10; 16)
17 PATH@[10; 16)
18 PATH_SEGMENT@[10; 16)
19 NAME_REF@[10; 16)
20 WHITESPACE@[10; 15)
21 IDENT@[15; 16) "x"
22 DOT@[16; 17)
23 IDENT@[17; 20) "foo"
24 DOT@[20; 21)
25 IDENT@[21; 24) "bar"
26 SEMI@[24; 25)
27 WHITESPACE@[25; 26)
28 R_CURLY@[26; 27)
29 WHITESPACE@[27; 28)
diff --git a/tools/src/lib.rs b/tools/src/lib.rs
index 5a7d846ff..97a56a31f 100644
--- a/tools/src/lib.rs
+++ b/tools/src/lib.rs
@@ -1,7 +1,6 @@
1extern crate itertools; 1extern crate itertools;
2 2
3use itertools::Itertools; 3use itertools::Itertools;
4use std::hash;
5 4
6#[derive(Debug)] 5#[derive(Debug)]
7pub struct Test { 6pub struct Test {