Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(480)

Side by Side Diff: utils/peg/pegparser_test.dart

Issue 8404031: IDL parser (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add copyright Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« utils/peg/pegparser.dart ('K') | « utils/peg/pegparser.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/env ./dart_bin
2 #import('pegparser.dart');
3
4 testParens() {
Jennifer Messerly 2011/10/28 01:21:10 tests, awesome :)
5 Grammar g = new Grammar();
6 Symbol a = g['A'];
7
8 a.def = ['(', MANY(a, min:0), ')', (a) => a];
9
10 check(g, a, "", null);
11 check(g, a, "()", '[]');
12 check(g, a, "(()())", '[[],[]]');
13 check(g, a, "(()((()))())", '[[],[[[]]],[]]');
14 }
15
16 testBlockComment() {
17
18 // Block comment in whitespace.
19
20 Grammar g = new Grammar();
21 Symbol blockComment = g['blockComment'];
22
23 blockComment.def =
24 ['/*',
25 MANY(OR([blockComment,
26 [NOT('*/'), CHAR()],
27 [END, ERROR('EOF in block comment')]
28 ]),
29 min: 0),
30 '*/'];
31 print(blockComment);
32
33 var a = MANY(TEXT('x'));
34
35 g.whitespace = OR([g.whitespace, blockComment]);
36
37 check(g, a, "x /**/ x", '[x,x]');
38 check(g, a, "x /*/**/*/ x", '[x,x]');
39 check(g, a, "x /*/***/ x", 'EOF in block comment');
40 check(g, a, "x /*/*/x**/**/ x", '[x,x]');
41
42 check(g, a, @"""
43 /* Comment */
44 /* Following comment with /* nested comment*/ */
45 x
46 /* x in comment */
47 x /* outside comment */
48 """,
49 '[x,x]');
50 }
51
52 testTEXT() {
53 Grammar g = new Grammar();
54
55 // TEXT grabs the parsed text,
56 check(g, TEXT(LEX(MANY(OR(['1','a'])))), ' 1a1 ', '1a1');
57
58 // Without the lexical context, TEXT will grab intervening whitespace.
59 check(g, TEXT(MANY(OR(['1','a']))), ' 1a1 ', '1a1');
60 check(g, TEXT(MANY(OR(['1','a']))), ' 1 a 1 ', '1 a 1');
61
62 // Custom processing of the TEXT substring.
63 var binaryNumber =
64 TEXT(LEX(MANY(OR(['0','1']))),
65 (str, start, end) {
66 var r = 0;
67 var zero = '0'.charCodeAt(0);
68 for (int i = start; i < end; i++)
69 r = r * 2 + (str.charCodeAt(i) - zero);
70 return r;
71 });
72
73 check(g, binaryNumber, ' 10101 ', 21);
74 check(g, binaryNumber, '1010111', 87);
75 check(g, binaryNumber, '1010 111', null);
76 }
77
78 testOR() {
79 // OR matches the first match.
80 Grammar g = new Grammar();
81 check(g, OR([['a', NOT(END), () => 1],
82 ['a', () => 2],
83 ['a', () => 3]]),
84 'a', 2);
85 }
86
87 testCODE() {
88 Grammar g = new Grammar();
89 var a = TEXT(LEX('thing', MANY(CHAR('bcd'))));
90
91 check(g, a, 'bbb', 'bbb');
92 check(g, a, 'ccc', 'ccc');
93 check(g, a, 'ddd', 'ddd');
94 check(g, a, 'bad', null); // a is outside range.
95 check(g, a, 'bed', null); // e is outside range.
96 }
97
98 testC() {
99 // Curried tree builders.
100 binary(operation) => (second) => (first) => [operation, first, second];
101 unary(operation) => () => (first) => [operation, first];
102 reform(a, fns) {
103 var r = a;
104 for (var fn in fns)
105 r = fn(r);
106 return r;
107 }
108
109 Grammar g = new Grammar();
110
111 Symbol expression = g['expression'];
112 Symbol postfix_e = g['postfix_e'];
113 Symbol unary_e = g['unary_e'];
114 Symbol cast_e = g['cast_e'];
115 Symbol mult_e = g['mult_e'];
116 Symbol add_e = g['add_e'];
117 Symbol shift_e = g['shift_e'];
118 Symbol relational_e = g['relational_e'];
119 Symbol equality_e = g['equality_e'];
120 Symbol cond_e = g['cond_e'];
121 Symbol assignment_e = g['assignment_e'];
122
123 // Lexical elements.
124 var idStartChar = CHAR(
125 @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
126 var idNextChar = CHAR(
127 @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789$_");
128
129 var id = TEXT(LEX('identifier', [idStartChar, MANY(idNextChar, min: 0)]));
130
131 var lit = TEXT(LEX('literal', MANY(CHAR('0123456789'))));
132
133
134 var type_name = id;
135
136
137 // Expression grammar.
138 var primary_e = OR([id,
139 lit,
140 ['(', expression, ')', (e) => e]
141 ]);
142
143 var postfixes = OR([['(', MANY(assignment_e, ',', 0), ')', binary('apply')],
144 ['++', unary('postinc')],
145 ['--', unary('postdec')],
146 ['.', id, binary('field')],
147 ['->', id, binary('ptr')],
148 ]);
149
150 postfix_e.def = [primary_e, MANY(postfixes, min:0), reform];
151
152
153 var unary_op = OR([['&', () => 'address'],
154 ['*', () => 'indir'],
155 ['!', () => 'not'],
156 ['~', () => 'not'],
157 ['-', () => 'negate'],
158 ['+', () => 'uplus'],
159 ]);
160 var sizeof = LEX('sizeof', ['sizeof', NOT(idNextChar)]);
161
162 Symbol unary_e_plain = g['unary_e_plain'];
163 unary_e_plain.def =
164 OR([ ['++', unary_e, (e) => ['preinc', e]],
165 ['--', unary_e, (e) => ['predec', e]],
166 [unary_op, cast_e, (o, e) => [o, e]],
167 [sizeof, unary_e, (e) => ['sizeof-expr', e]],
168 [sizeof, '(', type_name , ')', (t) => ['sizeof-type', t]],
169 postfix_e
170 ]);
171
172 unary_e.def = MEMO(unary_e_plain);
173 //unary_e.def = unary_e_plain;
174
175 cast_e.def = OR([ ['(', type_name, ')', cast_e, (t, e) => ['cast', t, e]],
176 unary_e,
177 ]);
178
179 var mult_ops = OR([['*', cast_e, binary('mult')],
180 ['/', cast_e, binary('div')],
181 ['%', cast_e, binary('rem')],
182 ]);
183 mult_e.def = [cast_e, MANY(mult_ops, min:0), reform];
184
185 var add_ops = OR([['+', mult_e, binary('add')],
186 ['-', mult_e, binary('sub')],
187 ]);
188 add_e.def = [mult_e, MANY(add_ops, min:0), reform];
189
190 var shift_ops = OR([['>>', add_e, binary('shl')],
191 ['<<', add_e, binary('shr')],
192 ]);
193 shift_e.def = [add_e, MANY(shift_ops, min:0), reform];
194
195 var relational_ops = OR([['<=', shift_e, binary('le')],
196 ['>=', shift_e, binary('ge')],
197 ['<', shift_e, binary('lt')],
198 ['>', shift_e, binary('gt')],
199 ]);
200 relational_e.def = [shift_e, MANY(relational_ops, min:0), reform];
201
202
203 var equality_ops = OR([['==', shift_e, binary('eq')],
204 ['!=', shift_e, binary('ne')],
205 ]);
206 equality_e.def = [relational_e, MANY(equality_ops, min:0), reform];
207
208
209 var bit_and_op = LEX('&', ['&', NOT('&')]); // Don't see '&&' and '&', '&'
210 var bit_or_op = LEX('|', ['|', NOT('|')]);
211
212 var and_e = [equality_e, MANY([bit_and_op, equality_e, binary('bitand')], min: 0), reform];
213 var xor_e = [and_e, MANY(['^', and_e, binary('bitxor')], min:0), reform];
214 var or_e = [xor_e, MANY([bit_or_op, xor_e, binary('bitor')], min:0), reform];
215
216 var log_and_e = [or_e, MANY(['&&', or_e, binary('and')], min:0), reform];
217
218 var log_or_e = [log_and_e, MANY(['||', log_and_e, binary('or')], min:0), refor m];
219
220 //cond_e.def = OR([ [log_or_e, '?', expression, ':', cond_e,
221 // (p,a,b) => ['cond', p, a, b]],
222 // log_or_e]);
223 // Alternate version avoids reparsing log_or_e.
224 cond_e.def = [log_or_e, MAYBE(['?', expression, ':', cond_e]),
225 (p, r) => r == null || r == false ? p : ['cond', p, r[0], r[1]]] ;
226
227 var assign_op = OR([['*=', () => 'mulassign'],
228 ['=', () => 'assign']]);
229
230 // TODO: Figure out how not to re-parse a unary_e.
231 // Order matters - cond_e can't go first since cond_e will succeed on, e.g. 'a '.
232 assignment_e.def = OR([[unary_e, assign_op, assignment_e,
233 (u, op, a) => [op, u, a]],
234 cond_e]);
235
236 expression.def = [assignment_e,
237 MANY([',', assignment_e, binary('comma')], min:0),
238 reform];
239
240 show(g, expression, 'a');
241 check(g, expression, 'a', 'a');
242 check(g, expression, '(a)', 'a');
243 check(g, expression, ' ( ( a ) ) ', 'a');
244
245 check(g, expression, 'a(~1,2)', '[apply,a,[[not,1],2]]');
246 check(g, expression, 'a(1)(x,2)', '[apply,[apply,a,[1]],[x,2]]');
247 check(g, expression, 'a(1,2())', '[apply,a,[1,[apply,2,[]]]]');
248
249 check(g, expression, '++a++', '[preinc,[postinc,a]]');
250 check(g, expression, 'a++++b', null);
251 check(g, expression, 'a++ ++b', null);
252 check(g, expression, 'a+ +++b', '[add,a,[preinc,[uplus,b]]]');
253 check(g, expression, 'a+ + ++b', '[add,a,[uplus,[preinc,b]]]');
254 check(g, expression, 'a+ + + +b', '[add,a,[uplus,[uplus,[uplus,b]]]]');
255 check(g, expression, 'a+ ++ +b', '[add,a,[preinc,[uplus,b]]]');
256 check(g, expression, 'a++ + +b', '[add,[postinc,a],[uplus,b]]');
257 check(g, expression, 'a+++ +b', '[add,[postinc,a],[uplus,b]]');
258
259 check(g, expression, '((T)f)(x)', '[apply,[cast,T,f],[x]]');
260 check(g, expression, '(T)f(x)', '[cast,T,[apply,f,[x]]]');
261
262 check(g, expression, 'a++*++b', '[mult,[postinc,a],[preinc,b]]');
263
264 check(g, expression, 'a<<1>>++b', '[shl,[shr,a,1],[preinc,b]]');
265
266 check(g, expression, 'a<1&&b', '[and,[lt,a,1],b]');
267
268 check(g, expression, 'a<1 & &b', '[bitand,[lt,a,1],[address,b]]');
269 check(g, expression,
270 'a ? b ? c : d : e ? f : g',
271 '[cond,a,[cond,b,c,d],[cond,e,f,g]]');
272
273 check(g, expression, 'a,b,c', '[comma,[comma,a,b],c]');
274 check(g, expression, 'a=1,b,c', '[comma,[comma,[assign,a,1],b],c]');
275
276 check(g, expression,
277 '((((((((((((a))))))))))))=1,b,c', '[comma,[comma,[assign,a,1],b],c]');
278
279 check(g, expression, 'sizeof a', '[sizeof-expr,a]');
280 check(g, expression, 'sizeofa', 'sizeofa');
281 check(g, expression, 'sizeof (a)', '[sizeof-expr,a]');
282 }
283
284
285 show(grammar, rule, input) {
286 print('show: "$input"');
287 var ast;
288 try {
289 ast = grammar.parse(rule, input);
290 } catch (var exception) {
291 if (exception is ParseError)
292 ast = exception;
293 else
294 throw;
295 }
296 print('${printList(ast)}');
297 }
298
299 void check(grammar, rule, input, expected) {
300 // If [expected] is String then the result is coerced to string.
301 // If [expected] is !String, the result is compared directly.
302 print('check: "$input"');
303 var ast;
304 try {
305 ast = grammar.parse(rule, input);
306 } catch (var exception) {
307 ast = exception;
308 }
309
310 var formatted = ast;
311 if (expected is String)
312 formatted = printList(ast);
313
314 Expect.equals(expected, formatted, "parse: $input");
315 }
316
317 // Prints the list in [1,2,3] notation, including nested lists.
318 void printList(item) {
319 if (item is List) {
320 StringBuffer sb = new StringBuffer();
321 sb.add('[');
322 var sep = '';
323 for (var x in item) {
324 sb.add(sep);
325 sb.add(printList(x));
326 sep = ',';
327 }
328 sb.add(']');
329 return sb.toString();
330 }
331 if (item == null)
332 return 'null';
333 return item.toString();
334 }
335
336 main() {
337 testCODE();
338 testParens();
339 testOR();
340 testTEXT();
341 testBlockComment();
342 testC();
343 }
OLDNEW
« utils/peg/pegparser.dart ('K') | « utils/peg/pegparser.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698