diff options
author | Aleksey Kladov <[email protected]> | 2018-01-13 19:00:26 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-01-13 19:00:26 +0000 |
commit | 08f7c69f90bac772c69b3bf34877f3d9a845c541 (patch) | |
tree | 4866ac1ab988d1735ded745aacf1630f27a3220e /src/parser | |
parent | 55891be06a1f0a051638cb59f1d15167faf5ab82 (diff) |
G: struct flavors
Diffstat (limited to 'src/parser')
-rw-r--r-- | src/parser/event_parser/grammar/items.rs | 86 | ||||
-rw-r--r-- | src/parser/event_parser/grammar/mod.rs | 16 | ||||
-rw-r--r-- | src/parser/event_parser/grammar/types.rs | 5 |
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 | ||
58 | fn struct_item(p: &mut Parser) { | 58 | fn 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 | |||
98 | fn 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 | |||
115 | fn 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 | |||
133 | fn generic_parameters(_: &mut Parser) { | ||
134 | } | ||
135 | |||
136 | fn where_clause(_: &mut Parser) { | ||
61 | } | 137 | } |
62 | 138 | ||
63 | fn extern_crate_item(p: &mut Parser) { | 139 | fn extern_crate_item(p: &mut Parser) { |
@@ -133,11 +209,7 @@ fn use_item(p: &mut Parser) { | |||
133 | } | 209 | } |
134 | } | 210 | } |
135 | 211 | ||
136 | fn 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 | ||
142 | fn fn_item(p: &mut Parser) { | 214 | fn 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::*; | |||
6 | mod items; | 6 | mod items; |
7 | mod attributes; | 7 | mod attributes; |
8 | mod expressions; | 8 | mod expressions; |
9 | mod types; | ||
9 | mod paths; | 10 | mod paths; |
10 | 11 | ||
11 | pub(crate) fn file(p: &mut Parser) { | 12 | pub(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 | ||
73 | fn comma_list<F: Fn(&mut Parser) -> bool>(p: &mut Parser, end: SyntaxKind, f: F) { | 74 | fn 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 @@ | |||
1 | use super::*; | ||
2 | |||
3 | pub(super) fn type_ref(p: &mut Parser) { | ||
4 | p.expect(IDENT); | ||
5 | } \ No newline at end of file | ||