aboutsummaryrefslogtreecommitdiff
path: root/src/parser
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-01-01 19:13:04 +0000
committerAleksey Kladov <[email protected]>2018-01-01 19:13:04 +0000
commit46422f722bdcadbf4462dd5a9c65756434b2d97a (patch)
tree6b07f7e0ff1a6113630d8f3d66d38291589be03a /src/parser
parentcb362626f326a565aca34c1a11c95dcb7152b798 (diff)
Parser: first scraches
Diffstat (limited to 'src/parser')
-rw-r--r--src/parser/event_parser/grammar.rs6
-rw-r--r--src/parser/event_parser/mod.rs1
-rw-r--r--src/parser/event_parser/parser.rs22
-rw-r--r--src/parser/mod.rs41
4 files changed, 60 insertions, 10 deletions
diff --git a/src/parser/event_parser/grammar.rs b/src/parser/event_parser/grammar.rs
index 5219ed535..0896506fb 100644
--- a/src/parser/event_parser/grammar.rs
+++ b/src/parser/event_parser/grammar.rs
@@ -40,6 +40,12 @@ fn mod_items(p: &mut Parser) {
40fn item(p: &mut Parser) -> Result { 40fn item(p: &mut Parser) -> Result {
41 outer_attributes(p)?; 41 outer_attributes(p)?;
42 visibility(p)?; 42 visibility(p)?;
43 if p.current_is(STRUCT_KW) {
44 p.start(STRUCT_ITEM);
45 p.bump();
46 p.finish();
47 return OK;
48 }
43 ERR 49 ERR
44} 50}
45 51
diff --git a/src/parser/event_parser/mod.rs b/src/parser/event_parser/mod.rs
index bdfd23974..1228236a9 100644
--- a/src/parser/event_parser/mod.rs
+++ b/src/parser/event_parser/mod.rs
@@ -4,6 +4,7 @@ use syntax_kinds::*;
4mod grammar; 4mod grammar;
5mod parser; 5mod parser;
6 6
7#[derive(Debug)]
7pub(crate) enum Event { 8pub(crate) enum Event {
8 Start { kind: SyntaxKind }, 9 Start { kind: SyntaxKind },
9 Finish, 10 Finish,
diff --git a/src/parser/event_parser/parser.rs b/src/parser/event_parser/parser.rs
index 0e4d44b79..2d5418a29 100644
--- a/src/parser/event_parser/parser.rs
+++ b/src/parser/event_parser/parser.rs
@@ -1,5 +1,6 @@
1use {Token, SyntaxKind, TextUnit}; 1use {Token, SyntaxKind, TextUnit};
2use super::Event; 2use super::{Event};
3use super::super::is_insignificant;
3use syntax_kinds::{WHITESPACE, COMMENT}; 4use syntax_kinds::{WHITESPACE, COMMENT};
4 5
5pub struct Parser<'t> { 6pub struct Parser<'t> {
@@ -16,9 +17,8 @@ impl<'t> Parser<'t> {
16 let mut non_ws_tokens = Vec::new(); 17 let mut non_ws_tokens = Vec::new();
17 let mut len = TextUnit::new(0); 18 let mut len = TextUnit::new(0);
18 for (idx, &token) in raw_tokens.iter().enumerate() { 19 for (idx, &token) in raw_tokens.iter().enumerate() {
19 match token.kind { 20 if !is_insignificant(token.kind) {
20 WHITESPACE | COMMENT => (), 21 non_ws_tokens.push((idx, len))
21 _ => non_ws_tokens.push((idx, len)),
22 } 22 }
23 len += token.len; 23 len += token.len;
24 } 24 }
@@ -50,15 +50,25 @@ impl<'t> Parser<'t> {
50 self.event(Event::Finish); 50 self.event(Event::Finish);
51 } 51 }
52 52
53 pub(crate) fn bump(&mut self) -> Option<SyntaxKind> { 53 pub(crate) fn current(&self) -> Option<SyntaxKind> {
54 if self.is_eof() { 54 if self.is_eof() {
55 return None; 55 return None;
56 } 56 }
57 let idx = self.non_ws_tokens[self.pos].0; 57 let idx = self.non_ws_tokens[self.pos].0;
58 self.pos += 1;
59 Some(self.raw_tokens[idx].kind) 58 Some(self.raw_tokens[idx].kind)
60 } 59 }
61 60
61 pub(crate) fn current_is(&self, kind: SyntaxKind) -> bool {
62 self.current() == Some(kind)
63 }
64
65 pub(crate) fn bump(&mut self) -> Option<SyntaxKind> {
66 let kind = self.current()?;
67 self.pos += 1;
68 self.event(Event::Token { kind, n_raw_tokens: 1 });
69 Some(kind)
70 }
71
62 fn event(&mut self, event: Event) { 72 fn event(&mut self, event: Event) {
63 self.events.push(event) 73 self.events.push(event)
64 } 74 }
diff --git a/src/parser/mod.rs b/src/parser/mod.rs
index ccccd78f9..a632fbc01 100644
--- a/src/parser/mod.rs
+++ b/src/parser/mod.rs
@@ -8,17 +8,50 @@ use self::event_parser::Event;
8 8
9pub fn parse(text: String, tokens: &[Token]) -> File { 9pub fn parse(text: String, tokens: &[Token]) -> File {
10 let events = event_parser::parse(&text, tokens); 10 let events = event_parser::parse(&text, tokens);
11 from_events_to_file(text, events) 11 from_events_to_file(text, tokens, events)
12} 12}
13 13
14fn from_events_to_file(text: String, events: Vec<Event>) -> File { 14fn from_events_to_file(
15 text: String,
16 tokens: &[Token],
17 events: Vec<Event>,
18) -> File {
15 let mut builder = FileBuilder::new(text); 19 let mut builder = FileBuilder::new(text);
20 let mut idx = 0;
16 for event in events { 21 for event in events {
17 match event { 22 match event {
18 Event::Start { kind } => builder.start_internal(kind), 23 Event::Start { kind } => builder.start_internal(kind),
19 Event::Finish => builder.finish_internal(), 24 Event::Finish => {
20 Event::Token { .. } => unimplemented!(), 25 while idx < tokens.len() {
26 let token = tokens[idx];
27 if is_insignificant(token.kind) {
28 idx += 1;
29 builder.leaf(token.kind, token.len);
30 } else {
31 break;
32 }
33 }
34 builder.finish_internal()
35 },
36 Event::Token { kind, mut n_raw_tokens } => loop {
37 let token = tokens[idx];
38 if !is_insignificant(token.kind) {
39 n_raw_tokens -= 1;
40 }
41 idx += 1;
42 builder.leaf(token.kind, token.len);
43 if n_raw_tokens == 0 {
44 break;
45 }
46 },
21 } 47 }
22 } 48 }
23 builder.finish() 49 builder.finish()
24} 50}
51
52fn is_insignificant(kind: SyntaxKind) -> bool {
53 match kind {
54 WHITESPACE | COMMENT => true,
55 _ => false,
56 }
57} \ No newline at end of file