diff options
author | Edwin Cheng <[email protected]> | 2019-04-18 20:49:56 +0100 |
---|---|---|
committer | Edwin Cheng <[email protected]> | 2019-04-18 20:49:56 +0100 |
commit | c0f19d70056fada5f381019694d893e0ffe8360a (patch) | |
tree | 4837619368155d49c7540f035c6eb76d8d50ea6b /crates/ra_parser/src | |
parent | 3ff5440a503f090032136c37c3d44375d6107db1 (diff) |
Add expr, pat, ty and macro_stmts
Diffstat (limited to 'crates/ra_parser/src')
-rw-r--r-- | crates/ra_parser/src/grammar.rs | 20 | ||||
-rw-r--r-- | crates/ra_parser/src/grammar/expressions.rs | 44 | ||||
-rw-r--r-- | crates/ra_parser/src/lib.rs | 4 |
3 files changed, 58 insertions, 10 deletions
diff --git a/crates/ra_parser/src/grammar.rs b/crates/ra_parser/src/grammar.rs index 1adc27b80..e1762633e 100644 --- a/crates/ra_parser/src/grammar.rs +++ b/crates/ra_parser/src/grammar.rs | |||
@@ -55,6 +55,21 @@ pub(crate) fn macro_items(p: &mut Parser) { | |||
55 | m.complete(p, MACRO_ITEMS); | 55 | m.complete(p, MACRO_ITEMS); |
56 | } | 56 | } |
57 | 57 | ||
58 | pub(crate) fn macro_stmts(p: &mut Parser) { | ||
59 | let m = p.start(); | ||
60 | |||
61 | while !p.at(EOF) { | ||
62 | if p.current() == SEMI { | ||
63 | p.bump(); | ||
64 | continue; | ||
65 | } | ||
66 | |||
67 | expressions::stmt(p, expressions::StmtWithSemi::Optional); | ||
68 | } | ||
69 | |||
70 | m.complete(p, MACRO_STMTS); | ||
71 | } | ||
72 | |||
58 | pub(crate) fn path(p: &mut Parser) { | 73 | pub(crate) fn path(p: &mut Parser) { |
59 | paths::type_path(p); | 74 | paths::type_path(p); |
60 | } | 75 | } |
@@ -72,6 +87,11 @@ pub(crate) fn pattern(p: &mut Parser) { | |||
72 | } | 87 | } |
73 | 88 | ||
74 | pub(crate) fn stmt(p: &mut Parser, with_semi: bool) { | 89 | pub(crate) fn stmt(p: &mut Parser, with_semi: bool) { |
90 | let with_semi = match with_semi { | ||
91 | true => expressions::StmtWithSemi::Yes, | ||
92 | false => expressions::StmtWithSemi::No, | ||
93 | }; | ||
94 | |||
75 | expressions::stmt(p, with_semi) | 95 | expressions::stmt(p, with_semi) |
76 | } | 96 | } |
77 | 97 | ||
diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs index 06f2b45b1..8df9035e9 100644 --- a/crates/ra_parser/src/grammar/expressions.rs +++ b/crates/ra_parser/src/grammar/expressions.rs | |||
@@ -4,6 +4,12 @@ pub(crate) use self::atom::match_arm_list; | |||
4 | pub(super) use self::atom::{literal, LITERAL_FIRST}; | 4 | pub(super) use self::atom::{literal, LITERAL_FIRST}; |
5 | use super::*; | 5 | use super::*; |
6 | 6 | ||
7 | pub(super) enum StmtWithSemi { | ||
8 | Yes, | ||
9 | No, | ||
10 | Optional, | ||
11 | } | ||
12 | |||
7 | const EXPR_FIRST: TokenSet = LHS_FIRST; | 13 | const EXPR_FIRST: TokenSet = LHS_FIRST; |
8 | 14 | ||
9 | pub(super) fn expr(p: &mut Parser) -> BlockLike { | 15 | pub(super) fn expr(p: &mut Parser) -> BlockLike { |
@@ -48,7 +54,7 @@ fn is_expr_stmt_attr_allowed(kind: SyntaxKind) -> bool { | |||
48 | } | 54 | } |
49 | } | 55 | } |
50 | 56 | ||
51 | pub(super) fn stmt(p: &mut Parser, with_semi: bool) { | 57 | pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi) { |
52 | // test block_items | 58 | // test block_items |
53 | // fn a() { fn b() {} } | 59 | // fn a() { fn b() {} } |
54 | let m = p.start(); | 60 | let m = p.start(); |
@@ -111,13 +117,23 @@ pub(super) fn stmt(p: &mut Parser, with_semi: bool) { | |||
111 | // } | 117 | // } |
112 | // test!{} | 118 | // test!{} |
113 | // } | 119 | // } |
114 | if with_semi { | 120 | |
115 | if blocklike.is_block() { | 121 | match with_semi { |
116 | p.eat(SEMI); | 122 | StmtWithSemi::Yes => { |
117 | } else { | 123 | if blocklike.is_block() { |
118 | p.expect(SEMI); | 124 | p.eat(SEMI); |
125 | } else { | ||
126 | p.expect(SEMI); | ||
127 | } | ||
128 | } | ||
129 | StmtWithSemi::No => {} | ||
130 | StmtWithSemi::Optional => { | ||
131 | if p.at(SEMI) { | ||
132 | p.eat(SEMI); | ||
133 | } | ||
119 | } | 134 | } |
120 | } | 135 | } |
136 | |||
121 | m.complete(p, EXPR_STMT); | 137 | m.complete(p, EXPR_STMT); |
122 | } | 138 | } |
123 | 139 | ||
@@ -128,7 +144,7 @@ pub(super) fn stmt(p: &mut Parser, with_semi: bool) { | |||
128 | // let c = 92; | 144 | // let c = 92; |
129 | // let d: i32 = 92; | 145 | // let d: i32 = 92; |
130 | // } | 146 | // } |
131 | fn let_stmt(p: &mut Parser, m: Marker, with_semi: bool) { | 147 | fn let_stmt(p: &mut Parser, m: Marker, with_semi: StmtWithSemi) { |
132 | assert!(p.at(LET_KW)); | 148 | assert!(p.at(LET_KW)); |
133 | p.bump(); | 149 | p.bump(); |
134 | patterns::pattern(p); | 150 | patterns::pattern(p); |
@@ -139,8 +155,16 @@ pub(super) fn stmt(p: &mut Parser, with_semi: bool) { | |||
139 | expressions::expr(p); | 155 | expressions::expr(p); |
140 | } | 156 | } |
141 | 157 | ||
142 | if with_semi { | 158 | match with_semi { |
143 | p.expect(SEMI); | 159 | StmtWithSemi::Yes => { |
160 | p.expect(SEMI); | ||
161 | } | ||
162 | StmtWithSemi::No => {} | ||
163 | StmtWithSemi::Optional => { | ||
164 | if p.at(SEMI) { | ||
165 | p.eat(SEMI); | ||
166 | } | ||
167 | } | ||
144 | } | 168 | } |
145 | m.complete(p, LET_STMT); | 169 | m.complete(p, LET_STMT); |
146 | } | 170 | } |
@@ -160,7 +184,7 @@ pub(crate) fn expr_block_contents(p: &mut Parser) { | |||
160 | continue; | 184 | continue; |
161 | } | 185 | } |
162 | 186 | ||
163 | stmt(p, true) | 187 | stmt(p, StmtWithSemi::Yes) |
164 | } | 188 | } |
165 | } | 189 | } |
166 | 190 | ||
diff --git a/crates/ra_parser/src/lib.rs b/crates/ra_parser/src/lib.rs index d6bcc4d8c..0ea942b6e 100644 --- a/crates/ra_parser/src/lib.rs +++ b/crates/ra_parser/src/lib.rs | |||
@@ -102,6 +102,10 @@ pub fn parse_macro_items(token_source: &dyn TokenSource, tree_sink: &mut dyn Tre | |||
102 | parse_from_tokens(token_source, tree_sink, grammar::macro_items); | 102 | parse_from_tokens(token_source, tree_sink, grammar::macro_items); |
103 | } | 103 | } |
104 | 104 | ||
105 | pub fn parse_macro_stmts(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) { | ||
106 | parse_from_tokens(token_source, tree_sink, grammar::macro_stmts); | ||
107 | } | ||
108 | |||
105 | /// A parsing function for a specific braced-block. | 109 | /// A parsing function for a specific braced-block. |
106 | pub struct Reparser(fn(&mut parser::Parser)); | 110 | pub struct Reparser(fn(&mut parser::Parser)); |
107 | 111 | ||