diff options
author | Aleksey Kladov <[email protected]> | 2018-01-01 15:58:46 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-01-01 15:58:46 +0000 |
commit | cb362626f326a565aca34c1a11c95dcb7152b798 (patch) | |
tree | 5a1cc081e36b4061f8e9275db9bf14ed71e924f9 /src/parser/event_parser | |
parent | 0af33a2587e4fb96e5001492792f1e926d576e27 (diff) |
Parser: guess what? Groundwork!
Diffstat (limited to 'src/parser/event_parser')
-rw-r--r-- | src/parser/event_parser/grammar.rs | 62 | ||||
-rw-r--r-- | src/parser/event_parser/parser.rs | 15 |
2 files changed, 75 insertions, 2 deletions
diff --git a/src/parser/event_parser/grammar.rs b/src/parser/event_parser/grammar.rs index c3496cccd..5219ed535 100644 --- a/src/parser/event_parser/grammar.rs +++ b/src/parser/event_parser/grammar.rs | |||
@@ -3,8 +3,68 @@ use super::parser::Parser; | |||
3 | 3 | ||
4 | use syntax_kinds::*; | 4 | use syntax_kinds::*; |
5 | 5 | ||
6 | // Items // | ||
7 | |||
6 | pub fn file(p: &mut Parser) { | 8 | pub fn file(p: &mut Parser) { |
7 | p.start(FILE); | 9 | p.start(FILE); |
8 | //TODO: parse_shebang | 10 | shebang(p); |
11 | inner_attributes(p); | ||
12 | mod_items(p); | ||
13 | p.finish(); | ||
14 | } | ||
15 | |||
16 | type Result = ::std::result::Result<(), ()>; | ||
17 | const OK: Result = Ok(()); | ||
18 | const ERR: Result = Err(()); | ||
19 | |||
20 | fn shebang(_: &mut Parser) { | ||
21 | //TODO | ||
22 | } | ||
23 | |||
24 | fn inner_attributes(_: &mut Parser) { | ||
25 | //TODO | ||
26 | } | ||
27 | |||
28 | fn mod_items(p: &mut Parser) { | ||
29 | loop { | ||
30 | skip_until_item(p); | ||
31 | if p.is_eof() { | ||
32 | return; | ||
33 | } | ||
34 | if item(p).is_err() { | ||
35 | skip_one_token(p); | ||
36 | } | ||
37 | } | ||
38 | } | ||
39 | |||
40 | fn item(p: &mut Parser) -> Result { | ||
41 | outer_attributes(p)?; | ||
42 | visibility(p)?; | ||
43 | ERR | ||
44 | } | ||
45 | |||
46 | |||
47 | |||
48 | // Paths, types, attributes, and stuff // | ||
49 | |||
50 | fn outer_attributes(_: &mut Parser) -> Result { | ||
51 | OK | ||
52 | } | ||
53 | |||
54 | fn visibility(_: &mut Parser) -> Result { | ||
55 | OK | ||
56 | } | ||
57 | |||
58 | // Expressions // | ||
59 | |||
60 | // Error recovery and high-order utils // | ||
61 | |||
62 | fn skip_until_item(_: &mut Parser) { | ||
63 | //TODO | ||
64 | } | ||
65 | |||
66 | fn skip_one_token(p: &mut Parser) { | ||
67 | p.start(ERROR); | ||
68 | p.bump().unwrap(); | ||
9 | p.finish(); | 69 | p.finish(); |
10 | } \ No newline at end of file | 70 | } \ No newline at end of file |
diff --git a/src/parser/event_parser/parser.rs b/src/parser/event_parser/parser.rs index 9592b90c9..0e4d44b79 100644 --- a/src/parser/event_parser/parser.rs +++ b/src/parser/event_parser/parser.rs | |||
@@ -34,10 +34,14 @@ impl<'t> Parser<'t> { | |||
34 | } | 34 | } |
35 | 35 | ||
36 | pub(crate) fn into_events(self) -> Vec<Event> { | 36 | pub(crate) fn into_events(self) -> Vec<Event> { |
37 | assert!(self.pos == self.non_ws_tokens.len()); | 37 | assert!(self.is_eof()); |
38 | self.events | 38 | self.events |
39 | } | 39 | } |
40 | 40 | ||
41 | pub(crate) fn is_eof(&self) -> bool { | ||
42 | self.pos == self.non_ws_tokens.len() | ||
43 | } | ||
44 | |||
41 | pub(crate) fn start(&mut self, kind: SyntaxKind) { | 45 | pub(crate) fn start(&mut self, kind: SyntaxKind) { |
42 | self.event(Event::Start { kind }); | 46 | self.event(Event::Start { kind }); |
43 | } | 47 | } |
@@ -46,6 +50,15 @@ impl<'t> Parser<'t> { | |||
46 | self.event(Event::Finish); | 50 | self.event(Event::Finish); |
47 | } | 51 | } |
48 | 52 | ||
53 | pub(crate) fn bump(&mut self) -> Option<SyntaxKind> { | ||
54 | if self.is_eof() { | ||
55 | return None; | ||
56 | } | ||
57 | let idx = self.non_ws_tokens[self.pos].0; | ||
58 | self.pos += 1; | ||
59 | Some(self.raw_tokens[idx].kind) | ||
60 | } | ||
61 | |||
49 | fn event(&mut self, event: Event) { | 62 | fn event(&mut self, event: Event) { |
50 | self.events.push(event) | 63 | self.events.push(event) |
51 | } | 64 | } |