aboutsummaryrefslogtreecommitdiff
path: root/src/parser
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-01-06 14:16:00 +0000
committerAleksey Kladov <[email protected]>2018-01-06 14:16:00 +0000
commitd0900b3ca7be669418e185c0eea0d92550d83d4d (patch)
tree41f9ee7063958d05d9b5a7a559a4e7fe58d298f9 /src/parser
parent55602727c8adfa12c026a8c7881a8bc57fba9db8 (diff)
G: struct fields
Diffstat (limited to 'src/parser')
-rw-r--r--src/parser/event_parser/grammar.rs38
-rw-r--r--src/parser/event_parser/mod.rs3
-rw-r--r--src/parser/event_parser/parser.rs27
3 files changed, 61 insertions, 7 deletions
diff --git a/src/parser/event_parser/grammar.rs b/src/parser/event_parser/grammar.rs
index be61483ec..77596fea6 100644
--- a/src/parser/event_parser/grammar.rs
+++ b/src/parser/event_parser/grammar.rs
@@ -1,4 +1,3 @@
1use super::Event;
2use super::parser::Parser; 1use super::parser::Parser;
3 2
4use syntax_kinds::*; 3use syntax_kinds::*;
@@ -50,10 +49,26 @@ fn item(p: &mut Parser) -> Result {
50 ERR 49 ERR
51} 50}
52 51
53fn struct_item(p: &mut Parser) -> Result{ 52fn struct_item(p: &mut Parser) -> Result {
54 p.expect(IDENT)?; 53 p.expect(IDENT)?;
55 p.expect(L_CURLY)?; 54 p.curly_block(|p| {
56 p.expect(R_CURLY) 55 comma_list(p, struct_field)
56 })
57}
58
59fn struct_field(p: &mut Parser) -> Result {
60 if !p.current_is(IDENT) {
61 return ERR;
62 }
63 p.start(STRUCT_FIELD);
64 p.bump();
65 ignore_errors(|| {
66 p.expect(COLON)?;
67 p.expect(IDENT)?;
68 OK
69 });
70 p.finish();
71 OK
57} 72}
58 73
59// Paths, types, attributes, and stuff // 74// Paths, types, attributes, and stuff //
@@ -78,4 +93,19 @@ fn skip_one_token(p: &mut Parser) {
78 p.start(ERROR); 93 p.start(ERROR);
79 p.bump().unwrap(); 94 p.bump().unwrap();
80 p.finish(); 95 p.finish();
96}
97
98fn ignore_errors<F: FnOnce() -> Result>(f: F) {
99 drop(f());
100}
101
102fn comma_list<F: Fn(&mut Parser) -> Result>(p: &mut Parser, element: F) {
103 loop {
104 if element(p).is_err() {
105 return
106 }
107 if p.expect(COMMA).is_err() {
108 return
109 }
110 }
81} \ No newline at end of file 111} \ No newline at end of file
diff --git a/src/parser/event_parser/mod.rs b/src/parser/event_parser/mod.rs
index 1228236a9..3c3654b6b 100644
--- a/src/parser/event_parser/mod.rs
+++ b/src/parser/event_parser/mod.rs
@@ -1,6 +1,5 @@
1use {Token, TextUnit, SyntaxKind}; 1use {Token, SyntaxKind};
2 2
3use syntax_kinds::*;
4mod grammar; 3mod grammar;
5mod parser; 4mod parser;
6 5
diff --git a/src/parser/event_parser/parser.rs b/src/parser/event_parser/parser.rs
index 36452ef67..04ef4fb28 100644
--- a/src/parser/event_parser/parser.rs
+++ b/src/parser/event_parser/parser.rs
@@ -1,7 +1,7 @@
1use {Token, SyntaxKind, TextUnit}; 1use {Token, SyntaxKind, TextUnit};
2use super::{Event}; 2use super::{Event};
3use super::super::is_insignificant; 3use super::super::is_insignificant;
4use syntax_kinds::{WHITESPACE, COMMENT}; 4use syntax_kinds::{L_CURLY, R_CURLY, ERROR};
5 5
6pub struct Parser<'t> { 6pub struct Parser<'t> {
7 text: &'t str, 7 text: &'t str,
@@ -10,6 +10,7 @@ pub struct Parser<'t> {
10 10
11 pos: usize, 11 pos: usize,
12 events: Vec<Event>, 12 events: Vec<Event>,
13 curly_level: i32,
13} 14}
14 15
15impl<'t> Parser<'t> { 16impl<'t> Parser<'t> {
@@ -30,6 +31,7 @@ impl<'t> Parser<'t> {
30 31
31 pos: 0, 32 pos: 0,
32 events: Vec::new(), 33 events: Vec::new(),
34 curly_level: 0,
33 } 35 }
34 } 36 }
35 37
@@ -64,6 +66,11 @@ impl<'t> Parser<'t> {
64 66
65 pub(crate) fn bump(&mut self) -> Option<SyntaxKind> { 67 pub(crate) fn bump(&mut self) -> Option<SyntaxKind> {
66 let kind = self.current()?; 68 let kind = self.current()?;
69 match kind {
70 L_CURLY => self.curly_level += 1,
71 R_CURLY => self.curly_level -= 1,
72 _ => (),
73 }
67 self.pos += 1; 74 self.pos += 1;
68 self.event(Event::Token { kind, n_raw_tokens: 1 }); 75 self.event(Event::Token { kind, n_raw_tokens: 1 });
69 Some(kind) 76 Some(kind)
@@ -78,6 +85,24 @@ impl<'t> Parser<'t> {
78 } 85 }
79 } 86 }
80 87
88 pub(crate) fn curly_block<F: FnOnce(&mut Parser)>(&mut self, f: F) -> Result<(), ()> {
89 let level = self.curly_level;
90 self.expect(L_CURLY)?;
91 f(self);
92 assert!(self.curly_level > level);
93 if self.expect(R_CURLY).is_ok() {
94 return Ok(());
95 }
96 self.start(ERROR);
97 while self.curly_level > level {
98 if self.bump().is_none() {
99 break;
100 }
101 }
102 self.finish();
103 Ok(()) //???
104 }
105
81 fn event(&mut self, event: Event) { 106 fn event(&mut self, event: Event) {
82 self.events.push(event) 107 self.events.push(event)
83 } 108 }