aboutsummaryrefslogtreecommitdiff
path: root/src/parser/event_parser/grammar.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-01-07 18:09:05 +0000
committerAleksey Kladov <[email protected]>2018-01-07 18:09:05 +0000
commit5562931e4f3c6c57e9122c3ce34d941fb1ff6e5b (patch)
tree0e87cba8e91d4bf96d752ada65b4d4a73e4e0ac9 /src/parser/event_parser/grammar.rs
parentfc4d6cc298fe901d2bf92a30a3efd67233c67a3a (diff)
Introduce EOF token
Diffstat (limited to 'src/parser/event_parser/grammar.rs')
-rw-r--r--src/parser/event_parser/grammar.rs23
1 files changed, 8 insertions, 15 deletions
diff --git a/src/parser/event_parser/grammar.rs b/src/parser/event_parser/grammar.rs
index f676a183c..64c6718cb 100644
--- a/src/parser/event_parser/grammar.rs
+++ b/src/parser/event_parser/grammar.rs
@@ -1,5 +1,6 @@
1use super::parser::Parser; 1use super::parser::Parser;
2use {SyntaxKind}; 2use {SyntaxKind};
3use tree::EOF;
3use syntax_kinds::*; 4use syntax_kinds::*;
4 5
5// Items // 6// Items //
@@ -18,11 +19,7 @@ pub(crate) fn file(p: &mut Parser) {
18} 19}
19 20
20fn item_first(p: &Parser) -> bool { 21fn item_first(p: &Parser) -> bool {
21 let current = match p.current() { 22 match p.current() {
22 Some(c) => c,
23 None => return false,
24 };
25 match current {
26 STRUCT_KW | FN_KW => true, 23 STRUCT_KW | FN_KW => true,
27 _ => false, 24 _ => false,
28 } 25 }
@@ -79,7 +76,7 @@ fn visibility(_: &mut Parser) {
79// Error recovery and high-order utils // 76// Error recovery and high-order utils //
80 77
81fn node_if<F: FnOnce(&mut Parser)>(p: &mut Parser, first: SyntaxKind, node_kind: SyntaxKind, rest: F) -> bool { 78fn node_if<F: FnOnce(&mut Parser)>(p: &mut Parser, first: SyntaxKind, node_kind: SyntaxKind, rest: F) -> bool {
82 p.current_is(first) && { node(p, node_kind, |p| { p.bump(); rest(p); }); true } 79 p.current() == first && { node(p, node_kind, |p| { p.bump(); rest(p); }); true }
83} 80}
84 81
85fn node<F: FnOnce(&mut Parser)>(p: &mut Parser, node_kind: SyntaxKind, rest: F) { 82fn node<F: FnOnce(&mut Parser)>(p: &mut Parser, node_kind: SyntaxKind, rest: F) {
@@ -95,7 +92,7 @@ fn many<F: Fn(&mut Parser) -> bool>(p: &mut Parser, f: F) {
95fn comma_list<F: Fn(&mut Parser) -> bool>(p: &mut Parser, f: F) { 92fn comma_list<F: Fn(&mut Parser) -> bool>(p: &mut Parser, f: F) {
96 many(p, |p| { 93 many(p, |p| {
97 f(p); 94 f(p);
98 if p.is_eof() { 95 if p.current() == EOF {
99 false 96 false
100 } else { 97 } else {
101 p.expect(COMMA); 98 p.expect(COMMA);
@@ -119,7 +116,7 @@ where
119 f(p); 116 f(p);
120 return true; 117 return true;
121 } 118 }
122 if p.is_eof() { 119 if p.current() == EOF {
123 if skipped { 120 if skipped {
124 p.finish(); 121 p.finish();
125 } 122 }
@@ -131,18 +128,14 @@ where
131 .message(message) 128 .message(message)
132 .emit(); 129 .emit();
133 } 130 }
134 p.bump().unwrap(); 131 p.bump();
135 skipped = true; 132 skipped = true;
136 } 133 }
137} 134}
138 135
139impl<'p> Parser<'p> { 136impl<'p> Parser<'p> {
140 fn current_is(&self, kind: SyntaxKind) -> bool {
141 self.current() == Some(kind)
142 }
143
144 pub(crate) fn expect(&mut self, kind: SyntaxKind) -> bool { 137 pub(crate) fn expect(&mut self, kind: SyntaxKind) -> bool {
145 if self.current_is(kind) { 138 if self.current() == kind {
146 self.bump(); 139 self.bump();
147 true 140 true
148 } else { 141 } else {
@@ -154,7 +147,7 @@ impl<'p> Parser<'p> {
154 } 147 }
155 148
156 fn optional(&mut self, kind: SyntaxKind) { 149 fn optional(&mut self, kind: SyntaxKind) {
157 if self.current_is(kind) { 150 if self.current() == kind {
158 self.bump(); 151 self.bump();
159 } 152 }
160 } 153 }