aboutsummaryrefslogtreecommitdiff
path: root/src/parser
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-01-13 19:00:26 +0000
committerAleksey Kladov <[email protected]>2018-01-13 19:00:26 +0000
commit08f7c69f90bac772c69b3bf34877f3d9a845c541 (patch)
tree4866ac1ab988d1735ded745aacf1630f27a3220e /src/parser
parent55891be06a1f0a051638cb59f1d15167faf5ab82 (diff)
G: struct flavors
Diffstat (limited to 'src/parser')
-rw-r--r--src/parser/event_parser/grammar/items.rs86
-rw-r--r--src/parser/event_parser/grammar/mod.rs16
-rw-r--r--src/parser/event_parser/grammar/types.rs5
3 files changed, 97 insertions, 10 deletions
diff --git a/src/parser/event_parser/grammar/items.rs b/src/parser/event_parser/grammar/items.rs
index 2f64111ab..a14f6de77 100644
--- a/src/parser/event_parser/grammar/items.rs
+++ b/src/parser/event_parser/grammar/items.rs
@@ -56,8 +56,84 @@ fn item(p: &mut Parser) -> bool {
56} 56}
57 57
58fn struct_item(p: &mut Parser) { 58fn struct_item(p: &mut Parser) {
59 p.expect(IDENT) 59 if !p.expect(IDENT) {
60 && p.curly_block(|p| comma_list(p, EOF, struct_field)); 60 return
61 }
62 generic_parameters(p);
63 match p.current() {
64 WHERE_KW => {
65 where_clause(p);
66 match p.current() {
67 SEMI => {
68 p.bump();
69 return
70 }
71 L_CURLY => named_fields(p),
72 _ => { //TODO: special case `(` error message
73 p.error()
74 .message("expected `;` or `{`")
75 .emit();
76 return
77 }
78 }
79 }
80 SEMI => {
81 p.bump();
82 return
83 }
84 L_CURLY => named_fields(p),
85 L_PAREN => {
86 tuple_fields(p);
87 p.expect(SEMI);
88 },
89 _ => {
90 p.error()
91 .message("expected `;`, `{`, or `(`")
92 .emit();
93 return
94 }
95 }
96}
97
98fn named_fields(p: &mut Parser) {
99 p.curly_block(|p| comma_list(p, EOF, |p| {
100 named_field(p);
101 true
102 }));
103
104 fn named_field(p: &mut Parser) {
105 node(p, NAMED_FIELD, |p| {
106 visibility(p);
107 p.expect(IDENT) && p.expect(COLON) && {
108 types::type_ref(p);
109 true
110 };
111 })
112 }
113}
114
115fn tuple_fields(p: &mut Parser) {
116 if !p.expect(L_PAREN) {
117 return
118 }
119 comma_list(p, R_PAREN, |p| {
120 tuple_field(p);
121 true
122 });
123 p.expect(R_PAREN);
124
125 fn tuple_field(p: &mut Parser) {
126 node(p, POS_FIELD, |p| {
127 visibility(p);
128 types::type_ref(p);
129 })
130 }
131}
132
133fn generic_parameters(_: &mut Parser) {
134}
135
136fn where_clause(_: &mut Parser) {
61} 137}
62 138
63fn extern_crate_item(p: &mut Parser) { 139fn extern_crate_item(p: &mut Parser) {
@@ -133,11 +209,7 @@ fn use_item(p: &mut Parser) {
133 } 209 }
134} 210}
135 211
136fn struct_field(p: &mut Parser) -> bool { 212
137 node_if(p, IDENT, STRUCT_FIELD, |p| {
138 p.expect(COLON) && p.expect(IDENT);
139 })
140}
141 213
142fn fn_item(p: &mut Parser) { 214fn fn_item(p: &mut Parser) {
143 p.expect(IDENT) && p.expect(L_PAREN) && p.expect(R_PAREN) 215 p.expect(IDENT) && p.expect(L_PAREN) && p.expect(R_PAREN)
diff --git a/src/parser/event_parser/grammar/mod.rs b/src/parser/event_parser/grammar/mod.rs
index 1c57e0cb4..881bb3ef3 100644
--- a/src/parser/event_parser/grammar/mod.rs
+++ b/src/parser/event_parser/grammar/mod.rs
@@ -6,6 +6,7 @@ use syntax_kinds::*;
6mod items; 6mod items;
7mod attributes; 7mod attributes;
8mod expressions; 8mod expressions;
9mod types;
9mod paths; 10mod paths;
10 11
11pub(crate) fn file(p: &mut Parser) { 12pub(crate) fn file(p: &mut Parser) {
@@ -72,12 +73,21 @@ fn many<F: FnMut(&mut Parser) -> bool>(p: &mut Parser, mut f: F) {
72 73
73fn comma_list<F: Fn(&mut Parser) -> bool>(p: &mut Parser, end: SyntaxKind, f: F) { 74fn comma_list<F: Fn(&mut Parser) -> bool>(p: &mut Parser, end: SyntaxKind, f: F) {
74 many(p, |p| { 75 many(p, |p| {
75 if !f(p) || p.current() == end { 76 if p.current() == end {
76 false 77 return false
78 }
79 let pos = p.pos();
80 f(p);
81 if p.pos() == pos {
82 return false
83 }
84
85 if p.current() == end {
86 p.eat(COMMA);
77 } else { 87 } else {
78 p.expect(COMMA); 88 p.expect(COMMA);
79 true
80 } 89 }
90 true
81 }) 91 })
82} 92}
83 93
diff --git a/src/parser/event_parser/grammar/types.rs b/src/parser/event_parser/grammar/types.rs
new file mode 100644
index 000000000..c431643d7
--- /dev/null
+++ b/src/parser/event_parser/grammar/types.rs
@@ -0,0 +1,5 @@
1use super::*;
2
3pub(super) fn type_ref(p: &mut Parser) {
4 p.expect(IDENT);
5} \ No newline at end of file