Commit | Line | Data |
---|---|---|
492700bc EB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2013 Etienne Bergeron | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Etienne Bergeron - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.ctf.parser.tests; | |
14 | ||
15 | import static org.junit.Assert.assertEquals; | |
16 | import static org.junit.Assert.fail; | |
17 | ||
18 | import org.antlr.runtime.ANTLRStringStream; | |
19 | import org.antlr.runtime.CharStream; | |
20 | import org.antlr.runtime.CommonTokenStream; | |
21 | import org.antlr.runtime.RecognitionException; | |
22 | import org.antlr.runtime.tree.CommonTree; | |
23 | import org.eclipse.linuxtools.ctf.parser.CTFLexer; | |
24 | import org.eclipse.linuxtools.ctf.parser.CTFParser; | |
25 | import org.junit.Ignore; | |
26 | import org.junit.Test; | |
27 | ||
28 | /** | |
29 | * This test validates the CTF-Parser implementation. | |
30 | * | |
31 | * The goal of these tests is to validate syntactic rules and not the | |
32 | * CTF semantic. Each test parses a string with a given rule of the | |
33 | * compiled parser and validates the resulting tree by using match rules. | |
34 | * | |
35 | * @author Etienne Bergeron | |
36 | */ | |
37 | public class CtfParserTest { | |
38 | ||
39 | // ------------------------------------------------------------------------ | |
40 | // Attributes | |
41 | // ------------------------------------------------------------------------ | |
42 | ||
43 | private CTFParser parser; | |
44 | ||
45 | // ------------------------------------------------------------------------ | |
46 | // Matches - Helper class and functions to match a parsed tree. | |
47 | // ------------------------------------------------------------------------ | |
48 | ||
49 | private class TreeMatcher { | |
50 | int fType; | |
51 | String fText; | |
52 | TreeMatcher[] fChild; | |
53 | ||
54 | TreeMatcher(int type, String text, TreeMatcher child[]) { | |
55 | fType = type; | |
56 | fText = text; | |
57 | fChild = child; | |
58 | } | |
59 | ||
60 | void matches(CommonTree tree) { | |
61 | if (fType == -1) { | |
62 | return; | |
63 | } | |
64 | if (tree.getType() != fType) { | |
65 | fail("Type mismatch!" + | |
66 | " expected:" + fType + | |
67 | " actual:" + tree.getType()); | |
68 | } | |
69 | ||
70 | if (fText != null) { | |
71 | if (tree.getText().compareTo(fText) != 0) { | |
72 | fail("Text mismatch!" + | |
73 | " expected:" + fText + | |
74 | " actual:" + tree.getText()); | |
75 | } | |
76 | } | |
77 | ||
78 | if (fChild != null) { | |
79 | int size = fChild.length; | |
80 | if (tree.getChildren() == null) { | |
81 | if (size != 0) { | |
82 | fail("Invalid children!" | |
83 | + "Expect: " + size + "child"); | |
84 | } | |
85 | } else { | |
86 | if (tree.getChildren().size() != size) { | |
87 | fail("Invalid number of childs!" | |
88 | + " expected:" + size | |
89 | + " actual:" + tree.getChildren().size()); | |
90 | } | |
91 | ||
92 | for (int i = 0; i < size; ++i) { | |
93 | fChild[i].matches((CommonTree) tree.getChild(i)); | |
94 | } | |
95 | } | |
96 | } | |
97 | } | |
98 | } | |
99 | ||
100 | void Matches(TreeMatcher matcher, CommonTree tree) { | |
101 | if (tree == null) { | |
102 | fail("Parsing failed!"); | |
103 | } | |
104 | matcher.matches(tree); | |
105 | } | |
106 | ||
107 | TreeMatcher All() { | |
108 | return new TreeMatcher(-1, null, null); | |
109 | } | |
110 | ||
111 | TreeMatcher Node(int type, TreeMatcher... child) { | |
112 | return new TreeMatcher(type, null, child); | |
113 | } | |
114 | ||
115 | TreeMatcher Node(int type, String text, TreeMatcher... child) { | |
116 | return new TreeMatcher(type, text, child); | |
117 | } | |
118 | ||
119 | TreeMatcher List(TreeMatcher... child) { | |
120 | return new TreeMatcher(0, null, child); | |
121 | } | |
122 | ||
123 | // ------------------------------------------------------------------------ | |
124 | // Operations | |
125 | // ------------------------------------------------------------------------ | |
126 | ||
127 | private void setInput(String content) { | |
128 | CharStream cs = new ANTLRStringStream(content); | |
129 | CTFLexer lexer = new CTFLexer(cs); | |
130 | CommonTokenStream tokens = new CommonTokenStream(lexer); | |
131 | parser = new CTFParser(tokens, false); | |
132 | } | |
133 | ||
134 | private CommonTree primaryExpression(String content) { | |
135 | try { | |
136 | setInput(content); | |
bbbc7d98 | 137 | return parser.primaryExpression().getTree(); |
492700bc EB |
138 | } catch (RecognitionException e) { |
139 | return null; | |
140 | } | |
141 | } | |
142 | ||
143 | private CommonTree unaryExpression(String content) { | |
144 | try { | |
145 | setInput(content); | |
bbbc7d98 | 146 | return parser.unaryExpression().getTree(); |
492700bc EB |
147 | } catch (RecognitionException e) { |
148 | return null; | |
149 | } | |
150 | } | |
151 | ||
152 | private CommonTree declaration(String content) { | |
153 | try { | |
154 | setInput(content); | |
bbbc7d98 | 155 | return parser.declaration().getTree(); |
492700bc EB |
156 | } catch (RecognitionException e) { |
157 | return null; | |
158 | } | |
159 | } | |
160 | ||
161 | // ------------------------------------------------------------------------ | |
162 | // Test cases | |
163 | // ------------------------------------------------------------------------ | |
164 | ||
165 | ||
166 | /** | |
167 | * Validate that parsing of an empty expression is invalid. | |
168 | */ | |
169 | @Test | |
170 | public void testPrimaryExpression() { | |
171 | CommonTree tree_empty = primaryExpression(""); | |
172 | assertEquals(null, tree_empty); | |
173 | } | |
174 | ||
175 | /** | |
176 | * Validate parsing of literals through a primary expression | |
177 | */ | |
178 | @Test | |
179 | public void testIntegerLiteralPrimaryExpression() { | |
180 | Matches(Node(CTFParser.UNARY_EXPRESSION_DEC, | |
181 | Node(CTFParser.DECIMAL_LITERAL, "123")), | |
182 | primaryExpression("123")); | |
183 | ||
184 | Matches(Node(CTFParser.UNARY_EXPRESSION_HEX, | |
185 | Node(CTFParser.HEX_LITERAL, "0x123")), | |
186 | primaryExpression("0x123")); | |
187 | ||
188 | Matches(Node(CTFParser.UNARY_EXPRESSION_OCT, | |
189 | Node(CTFParser.OCTAL_LITERAL, "0123")), | |
190 | primaryExpression("0123")); | |
191 | ||
192 | Matches(Node(CTFParser.UNARY_EXPRESSION_DEC, | |
193 | Node(CTFParser.DECIMAL_LITERAL, "123"), | |
194 | Node(CTFParser.SIGN, "-")), | |
195 | primaryExpression("-123")); | |
196 | ||
197 | Matches(Node(CTFParser.UNARY_EXPRESSION_DEC, | |
198 | Node(CTFParser.DECIMAL_LITERAL, "123"), | |
199 | Node(CTFParser.SIGN, "-")), | |
200 | primaryExpression(" - 123")); | |
201 | ||
202 | Matches(Node(CTFParser.UNARY_EXPRESSION_DEC, | |
203 | Node(CTFParser.DECIMAL_LITERAL, "123"), | |
204 | Node(CTFParser.SIGN, "-"), | |
205 | Node(CTFParser.SIGN, "-"), | |
206 | Node(CTFParser.SIGN, "+")), | |
207 | primaryExpression(" - - + 123")); | |
208 | ||
209 | Matches(Node(CTFParser.UNARY_EXPRESSION_HEX, | |
210 | Node(CTFParser.HEX_LITERAL, "0x123"), | |
211 | Node(CTFParser.SIGN, "+"), | |
212 | Node(CTFParser.SIGN, "-")), | |
213 | primaryExpression("+ - 0x123")); | |
214 | ||
215 | Matches(Node(CTFParser.UNARY_EXPRESSION_OCT, | |
216 | Node(CTFParser.OCTAL_LITERAL, "0123"), | |
217 | Node(CTFParser.SIGN, "+"), | |
218 | Node(CTFParser.SIGN, "-")), | |
219 | primaryExpression("+ - 0123")); | |
220 | } | |
221 | ||
222 | /** | |
223 | * Validate parsing of a character literals through a primary expression | |
224 | */ | |
225 | @Test | |
226 | public void testCharacterLiteralPrimaryExpression() { | |
227 | Matches(Node(CTFParser.CHARACTER_LITERAL, "'a'"), | |
228 | primaryExpression("'a'")); | |
229 | ||
230 | Matches(Node(CTFParser.CHARACTER_LITERAL, "'\\n'"), | |
231 | primaryExpression("'\\n'")); | |
232 | } | |
233 | ||
234 | /** | |
235 | * Validate parsing of a string literals through a primary expression | |
236 | */ | |
237 | @Test | |
238 | public void testStringLiteralPrimaryExpression() { | |
239 | Matches(Node(CTFParser.UNARY_EXPRESSION_STRING_QUOTES, | |
240 | Node(CTFParser.STRING_LITERAL, "\"aaa\"")), | |
241 | primaryExpression("\"aaa\"")); | |
242 | ||
243 | Matches(Node(CTFParser.UNARY_EXPRESSION_STRING_QUOTES, | |
244 | Node(CTFParser.STRING_LITERAL, "L\"aaa\"")), | |
245 | primaryExpression("L\"aaa\"")); | |
246 | ||
247 | Matches(Node(CTFParser.UNARY_EXPRESSION_STRING_QUOTES, | |
248 | Node(CTFParser.STRING_LITERAL, "\"aaa\\n\"")), | |
249 | primaryExpression("\"aaa\\n\"")); | |
250 | } | |
251 | ||
252 | /** | |
253 | * Validate parsing of keywords through a primary expression | |
254 | */ | |
255 | @Test | |
256 | public void testKeywordPrimaryExpression() { | |
257 | Matches(Node(CTFParser.UNARY_EXPRESSION_STRING, | |
258 | Node(CTFParser.SIGNEDTOK, "signed")), | |
259 | primaryExpression("signed")); | |
260 | Matches(Node(CTFParser.UNARY_EXPRESSION_STRING, | |
261 | Node(CTFParser.ALIGNTOK, "align")), | |
262 | primaryExpression("align")); | |
263 | } | |
264 | ||
265 | /** | |
266 | * Validate parsing of identifiers through a primary expression | |
267 | */ | |
268 | @Test | |
269 | public void testIdentifierPrimaryExpression() { | |
270 | Matches(Node(CTFParser.UNARY_EXPRESSION_STRING, | |
271 | Node(CTFParser.IDENTIFIER, "x")), | |
272 | primaryExpression("x")); | |
273 | Matches(Node(CTFParser.UNARY_EXPRESSION_STRING, | |
274 | Node(CTFParser.IDENTIFIER, "_123")), | |
275 | primaryExpression("_123")); | |
276 | } | |
277 | ||
278 | /** | |
279 | * Validate that parsing of an empty unary expression is invalid. | |
280 | */ | |
281 | @Test | |
282 | public void testUnaryExpression() { | |
283 | CommonTree tree_empty = unaryExpression(""); | |
284 | assertEquals(null, tree_empty); | |
285 | } | |
286 | ||
287 | /** | |
288 | * Validate parsing primary expression through an unary expression | |
289 | */ | |
290 | @Test | |
291 | public void testSimpleUnaryExpression() { | |
292 | Matches(Node(CTFParser.UNARY_EXPRESSION_DEC, | |
293 | Node(CTFParser.DECIMAL_LITERAL, "123")), | |
294 | unaryExpression("123")); | |
295 | ||
296 | Matches(Node(CTFParser.UNARY_EXPRESSION_STRING, | |
297 | Node(CTFParser.IDENTIFIER, "x")), | |
298 | unaryExpression("x")); | |
299 | } | |
300 | ||
301 | /** | |
302 | * Validate parsing array through an unary expression | |
303 | */ | |
304 | @Test | |
305 | public void testArrayUnaryExpression() { | |
306 | Matches(List(Node(CTFParser.UNARY_EXPRESSION_STRING, | |
307 | Node(CTFParser.IDENTIFIER, "x")), | |
308 | Node(CTFParser.OPENBRAC), | |
309 | Node(CTFParser.UNARY_EXPRESSION_DEC, | |
310 | Node(CTFParser.DECIMAL_LITERAL, "1"))), | |
311 | unaryExpression("x[1]")); | |
312 | ||
313 | Matches(List(Node(CTFParser.UNARY_EXPRESSION_STRING, | |
314 | Node(CTFParser.IDENTIFIER, "x")), | |
315 | Node(CTFParser.OPENBRAC), | |
316 | Node(CTFParser.UNARY_EXPRESSION_STRING, | |
317 | Node(CTFParser.IDENTIFIER, "n"))), | |
318 | unaryExpression("x[n]")); | |
319 | ||
320 | Matches(List(Node(CTFParser.UNARY_EXPRESSION_STRING, | |
321 | Node(CTFParser.IDENTIFIER, "x")), | |
322 | Node(CTFParser.OPENBRAC), | |
323 | Node(CTFParser.UNARY_EXPRESSION_STRING, | |
324 | Node(CTFParser.IDENTIFIER, "n")), | |
325 | Node(CTFParser.OPENBRAC), | |
326 | Node(CTFParser.UNARY_EXPRESSION_DEC, | |
327 | Node(CTFParser.DECIMAL_LITERAL, "1"))), | |
328 | unaryExpression("x[n][1]")); | |
329 | ||
330 | Matches(List(Node(CTFParser.UNARY_EXPRESSION_STRING, | |
331 | Node(CTFParser.IDENTIFIER, "x")), | |
332 | Node(CTFParser.OPENBRAC), | |
333 | Node(CTFParser.UNARY_EXPRESSION_STRING, | |
334 | Node(CTFParser.IDENTIFIER, "n")), | |
335 | Node(CTFParser.OPENBRAC), | |
336 | Node(CTFParser.UNARY_EXPRESSION_DEC, | |
337 | Node(CTFParser.DECIMAL_LITERAL, "1"), | |
338 | Node(CTFParser.SIGN, "+"))), | |
339 | unaryExpression("x[n][+1]")); | |
340 | } | |
341 | ||
342 | /** | |
343 | * Validate parsing array with keywords through an unary expression | |
344 | */ | |
345 | @Test | |
346 | public void testSpecialArrayUnaryExpression() { | |
347 | // Added for CTF-v1.8 | |
348 | Matches(List(Node(CTFParser.TRACE), | |
349 | Node(CTFParser.OPENBRAC), | |
350 | Node(CTFParser.UNARY_EXPRESSION_STRING, | |
351 | Node(CTFParser.IDENTIFIER, "n"))), | |
352 | unaryExpression("trace[n]")); | |
353 | ||
354 | Matches(List(Node(CTFParser.CLOCK), | |
355 | Node(CTFParser.OPENBRAC), | |
356 | Node(CTFParser.UNARY_EXPRESSION_STRING, | |
357 | Node(CTFParser.IDENTIFIER, "n")), | |
358 | Node(CTFParser.OPENBRAC), | |
359 | Node(CTFParser.UNARY_EXPRESSION_DEC, | |
360 | Node(CTFParser.DECIMAL_LITERAL, "1"))), | |
361 | unaryExpression("clock[n][1]")); | |
362 | } | |
363 | ||
364 | /** | |
365 | * Validate parsing member expression through an unary expression | |
366 | */ | |
367 | @Test | |
368 | public void testMemberUnaryExpression() { | |
369 | Matches(List(Node(CTFParser.UNARY_EXPRESSION_STRING, | |
370 | Node(CTFParser.IDENTIFIER, "x")), | |
371 | Node(CTFParser.DOT, | |
372 | Node(CTFParser.UNARY_EXPRESSION_STRING, | |
373 | Node(CTFParser.IDENTIFIER, "y")))), | |
374 | unaryExpression("x.y")); | |
375 | ||
376 | Matches(List(Node(CTFParser.UNARY_EXPRESSION_STRING, | |
377 | Node(CTFParser.IDENTIFIER, "x")), | |
378 | Node(CTFParser.DOT, | |
379 | Node(CTFParser.UNARY_EXPRESSION_STRING, | |
380 | Node(CTFParser.IDENTIFIER, "y"))), | |
381 | Node(CTFParser.DOT, | |
382 | Node(CTFParser.UNARY_EXPRESSION_STRING, | |
383 | Node(CTFParser.IDENTIFIER, "z")))), | |
384 | unaryExpression("x.y.z")); | |
385 | } | |
386 | ||
387 | /** | |
388 | * Validate parsing pointer expression through an unary expression | |
389 | */ | |
390 | @Test | |
391 | public void testPointerUnaryExpression() { | |
392 | Matches(List(Node(CTFParser.UNARY_EXPRESSION_STRING, | |
393 | Node(CTFParser.IDENTIFIER, "x")), | |
394 | Node(CTFParser.ARROW, | |
395 | Node(CTFParser.UNARY_EXPRESSION_STRING, | |
396 | Node(CTFParser.IDENTIFIER, "y")))), | |
397 | unaryExpression("x->y")); | |
398 | ||
399 | Matches(List(Node(CTFParser.UNARY_EXPRESSION_STRING, | |
400 | Node(CTFParser.IDENTIFIER, "x")), | |
401 | Node(CTFParser.ARROW, | |
402 | Node(CTFParser.UNARY_EXPRESSION_STRING, | |
403 | Node(CTFParser.IDENTIFIER, "y"))), | |
404 | Node(CTFParser.ARROW, | |
405 | Node(CTFParser.UNARY_EXPRESSION_STRING, | |
406 | Node(CTFParser.IDENTIFIER, "z")))), | |
407 | unaryExpression("x->y->z")); | |
408 | } | |
409 | ||
410 | /** | |
411 | * Validate complex expressions through an unary expression | |
412 | */ | |
413 | @Test | |
414 | public void testMixedUnaryExpression() { | |
415 | Matches(List(Node(CTFParser.UNARY_EXPRESSION_STRING, | |
416 | Node(CTFParser.IDENTIFIER, "x")), | |
417 | Node(CTFParser.OPENBRAC), | |
418 | Node(CTFParser.UNARY_EXPRESSION_DEC, | |
419 | Node(CTFParser.DECIMAL_LITERAL, "2")), | |
420 | Node(CTFParser.ARROW, | |
421 | Node(CTFParser.UNARY_EXPRESSION_STRING, | |
422 | Node(CTFParser.IDENTIFIER, "y"))), | |
423 | Node(CTFParser.DOT, | |
424 | Node(CTFParser.UNARY_EXPRESSION_STRING, | |
425 | Node(CTFParser.IDENTIFIER, "z"))), | |
426 | Node(CTFParser.OPENBRAC), | |
427 | Node(CTFParser.UNARY_EXPRESSION_DEC, | |
428 | Node(CTFParser.DECIMAL_LITERAL, "1"))), | |
429 | unaryExpression("x[2]->y.z[1]")); | |
430 | } | |
431 | ||
432 | /** | |
433 | * Validate that parsing of an empty declaration is invalid. | |
434 | */ | |
435 | @Test | |
436 | public void testDeclaration() { | |
437 | CommonTree tree_empty = declaration(""); | |
438 | assertEquals(null, tree_empty); | |
439 | } | |
440 | ||
441 | /** | |
442 | * Validate parsing of integer declaration | |
443 | */ | |
444 | @Test | |
445 | public void testIntegerTypeAliasDeclaration() { | |
446 | // TODO: replace the "all" match with a better tree matcher. | |
447 | Matches(All(), | |
448 | declaration("typealias integer { } := int;")); | |
449 | Matches(All(), | |
450 | declaration("typealias integer { signed=true; } := int;")); | |
451 | } | |
452 | ||
453 | /** | |
454 | * Validate parsing of floating declaration | |
455 | */ | |
456 | @Test | |
457 | public void testFloatingTypeAliasDeclaration() { | |
458 | // TODO: replace the "all" match with a better tree matcher. | |
459 | Matches(All(), | |
460 | declaration("typealias floating_point { } := float;")); | |
461 | Matches(All(), | |
462 | declaration("typealias floating_point { align = 32; } := float;")); | |
463 | } | |
464 | ||
465 | /** | |
466 | * Validate parsing of typedef declaration | |
467 | */ | |
468 | @Ignore("This need a fix to the grammar to support a dummy initial scope. ") | |
469 | @Test | |
470 | public void testTypedefDeclaration() { | |
471 | // TODO: replace the "all" match with a better tree matcher. | |
472 | Matches(All(), | |
473 | declaration("typedef dummy int;")); | |
474 | Matches(All(), | |
475 | declaration("typedef integer { } int;")); | |
476 | } | |
477 | ||
478 | /** | |
479 | * Validate parsing of an enum declaration | |
480 | */ | |
481 | @Test | |
482 | public void testEnumDeclaration() { | |
483 | Matches(Node(CTFParser.DECLARATION, | |
484 | Node(CTFParser.TYPE_SPECIFIER_LIST, | |
485 | Node(CTFParser.ENUM, | |
486 | Node(CTFParser.ENUM_NAME, | |
487 | Node(CTFParser.IDENTIFIER, "name")), | |
488 | Node(CTFParser.ENUM_BODY, | |
489 | Node(CTFParser.ENUM_ENUMERATOR, | |
490 | Node(CTFParser.UNARY_EXPRESSION_STRING, | |
491 | Node(CTFParser.IDENTIFIER, "A"))))))), | |
492 | declaration("enum name { A };")); | |
493 | ||
494 | Matches(Node(CTFParser.DECLARATION, | |
495 | Node(CTFParser.TYPE_SPECIFIER_LIST, | |
496 | Node(CTFParser.ENUM, | |
497 | Node(CTFParser.ENUM_NAME, All()), | |
498 | Node(CTFParser.ENUM_CONTAINER_TYPE, | |
499 | Node(CTFParser.TYPE_SPECIFIER_LIST, | |
500 | Node(CTFParser.INTTOK))), | |
501 | Node(CTFParser.ENUM_BODY, All())))), | |
502 | declaration("enum name : int { A };")); | |
503 | ||
504 | Matches(Node(CTFParser.DECLARATION, | |
505 | Node(CTFParser.TYPE_SPECIFIER_LIST, | |
506 | Node(CTFParser.ENUM, | |
507 | Node(CTFParser.ENUM_BODY, All())))), | |
508 | declaration("enum { A };")); | |
509 | ||
510 | Matches(Node(CTFParser.DECLARATION, | |
511 | Node(CTFParser.TYPE_SPECIFIER_LIST, | |
512 | Node(CTFParser.ENUM, | |
513 | Node(CTFParser.ENUM_CONTAINER_TYPE, | |
514 | Node(CTFParser.TYPE_SPECIFIER_LIST, | |
515 | Node(CTFParser.INTTOK))), | |
516 | Node(CTFParser.ENUM_BODY, All())))), | |
517 | declaration("enum : int { A };")); | |
518 | } | |
519 | ||
520 | /** | |
521 | * Validate parsing of an enumerator | |
522 | */ | |
523 | @Ignore("The grammar needs to be fixed.") | |
524 | @Test | |
525 | public void testDeclaratorOfEnumDeclaration() { | |
526 | /* TODO: This test crash the parser. */ | |
527 | Matches(All(), | |
528 | declaration("enum { };")); | |
529 | ||
530 | Matches(Node(CTFParser.DECLARATION, | |
531 | Node(CTFParser.TYPE_SPECIFIER_LIST, | |
532 | Node(CTFParser.ENUM, | |
533 | Node(CTFParser.ENUM_BODY, | |
534 | Node(CTFParser.ENUM_ENUMERATOR, | |
535 | Node(CTFParser.UNARY_EXPRESSION_STRING, | |
536 | Node(CTFParser.IDENTIFIER, "A"))), | |
537 | Node(CTFParser.ENUM_ENUMERATOR, | |
538 | Node(CTFParser.UNARY_EXPRESSION_STRING, | |
539 | Node(CTFParser.IDENTIFIER, "B")), | |
540 | Node(CTFParser.ENUM_VALUE, | |
541 | Node(CTFParser.UNARY_EXPRESSION_DEC, | |
542 | Node(CTFParser.DECIMAL_LITERAL, "2")))), | |
543 | Node(CTFParser.ENUM_ENUMERATOR, | |
544 | Node(CTFParser.UNARY_EXPRESSION_STRING, | |
545 | Node(CTFParser.IDENTIFIER, "C")), | |
546 | Node(CTFParser.ENUM_VALUE_RANGE, | |
547 | Node(CTFParser.UNARY_EXPRESSION_DEC, | |
548 | Node(CTFParser.DECIMAL_LITERAL, "3")), | |
549 | Node(CTFParser.UNARY_EXPRESSION_DEC, | |
550 | Node(CTFParser.DECIMAL_LITERAL, "5")))))))), | |
551 | declaration("enum { A, B=2, C=3...5 };")); | |
552 | ||
553 | Matches(Node(CTFParser.DECLARATION, | |
554 | Node(CTFParser.TYPE_SPECIFIER_LIST, | |
555 | Node(CTFParser.ENUM, | |
556 | Node(CTFParser.ENUM_BODY, | |
557 | Node(CTFParser.ENUM_ENUMERATOR, | |
558 | Node(CTFParser.UNARY_EXPRESSION_STRING_QUOTES, | |
559 | Node(CTFParser.STRING_LITERAL, "\"A\""))), | |
560 | Node(CTFParser.ENUM_ENUMERATOR, | |
561 | Node(CTFParser.UNARY_EXPRESSION_STRING_QUOTES, | |
562 | Node(CTFParser.STRING_LITERAL, "\"B\"")), | |
563 | All()))))), | |
564 | declaration("enum { \"A\", \"B\"=2 };")); | |
565 | } | |
566 | ||
567 | /** | |
568 | * Validate parsing of empty declaration | |
569 | */ | |
570 | @Ignore("The grammar need to be fixed to support empty ctf-body.") | |
571 | @Test | |
572 | public void testEmptyDeclaration() { | |
573 | /* TODO: An exception is throw when building an common tree without | |
574 | * assignments in the ctf-body. | |
575 | */ | |
576 | Matches(All(), | |
577 | declaration("env { };")); | |
578 | Matches(All(), | |
579 | declaration("trace { };")); | |
580 | Matches(All(), | |
581 | declaration("stream { };")); | |
582 | Matches(All(), | |
583 | declaration("event { };")); | |
584 | } | |
585 | ||
586 | /** | |
587 | * Validate parsing of an environment declaration | |
588 | */ | |
589 | @Test | |
590 | public void testEnvDeclaration() { | |
591 | Matches(Node(CTFParser.ENV, | |
592 | Node(CTFParser.CTF_EXPRESSION_VAL, | |
593 | Node(CTFParser.CTF_LEFT, | |
594 | Node(CTFParser.UNARY_EXPRESSION_STRING, | |
595 | Node(CTFParser.IDENTIFIER, "pid"))), | |
596 | Node(CTFParser.CTF_RIGHT, | |
597 | Node(CTFParser.UNARY_EXPRESSION_STRING, | |
598 | Node(CTFParser.IDENTIFIER, "value"))))), | |
599 | declaration("env { pid = value; };")); | |
600 | ||
601 | Matches(Node(CTFParser.ENV, | |
602 | Node(CTFParser.CTF_EXPRESSION_VAL, All(), All()), | |
603 | Node(CTFParser.CTF_EXPRESSION_VAL, All(), All()), | |
604 | Node(CTFParser.CTF_EXPRESSION_VAL, All(), All())), | |
605 | declaration("env { pid = value; proc_name = \"name\"; x = y;};")); | |
606 | } | |
607 | ||
608 | /** | |
609 | * Validate parsing of a trace declaration | |
610 | */ | |
611 | @Ignore("The grammar need to be fixed.") | |
612 | @Test | |
613 | public void testTraceDeclaration() { | |
614 | Matches(Node(CTFParser.TRACE, | |
615 | Node(CTFParser.CTF_EXPRESSION_VAL, | |
616 | Node(CTFParser.CTF_LEFT, | |
617 | Node(CTFParser.UNARY_EXPRESSION_STRING, | |
618 | Node(CTFParser.IDENTIFIER, "major"))), | |
619 | Node(CTFParser.CTF_RIGHT, | |
620 | Node(CTFParser.UNARY_EXPRESSION_DEC, | |
621 | Node(CTFParser.DECIMAL_LITERAL, "1"))))), | |
622 | declaration("trace { major = 1; };")); | |
623 | ||
624 | Matches(Node(CTFParser.TRACE, | |
625 | Node(CTFParser.CTF_EXPRESSION_TYPE, | |
626 | Node(CTFParser.CTF_LEFT, | |
627 | Node(CTFParser.UNARY_EXPRESSION_STRING, | |
628 | Node(CTFParser.IDENTIFIER, "packet")), | |
629 | Node(CTFParser.DOT, | |
630 | Node(CTFParser.UNARY_EXPRESSION_STRING, | |
631 | Node(CTFParser.IDENTIFIER, "header")))), | |
632 | Node(CTFParser.CTF_RIGHT, | |
633 | Node(CTFParser.TYPE_SPECIFIER_LIST, | |
634 | Node(CTFParser.STRUCT, | |
635 | Node(CTFParser.STRUCT_NAME, | |
636 | Node(CTFParser.IDENTIFIER, "dummy"))))))), | |
637 | declaration("trace { packet.header := struct dummy; };")); | |
638 | ||
639 | /* TODO: This test crash the parser. */ | |
640 | Matches(Node(CTFParser.TRACE, | |
641 | All()), | |
642 | declaration("trace { typedef x y; };")); | |
643 | ||
644 | Matches(Node(CTFParser.TRACE, | |
645 | Node(CTFParser.CTF_EXPRESSION_VAL, All(), All()), | |
646 | Node(CTFParser.CTF_EXPRESSION_VAL, All(), All()), | |
647 | Node(CTFParser.CTF_EXPRESSION_TYPE, All(), All())), | |
648 | declaration("trace { major = 1; minor = 1;" | |
649 | + "packet.header := struct dummy; };")); | |
650 | } | |
651 | ||
652 | } |