e54c5179aec31f64baf21d26e580c528f2f3eb09
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Etienne Bergeron
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
10 * Etienne Bergeron - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.ctf
.parser
.tests
;
15 import static org
.junit
.Assert
.fail
;
17 import java
.util
.LinkedList
;
18 import java
.util
.List
;
20 import org
.antlr
.runtime
.ANTLRStringStream
;
21 import org
.antlr
.runtime
.CharStream
;
22 import org
.antlr
.runtime
.Token
;
23 import org
.eclipse
.tracecompass
.ctf
.parser
.CTFLexer
;
24 import org
.junit
.Ignore
;
25 import org
.junit
.Test
;
28 * This test validates the CTF-Lexer implementation.
30 * The test splits a string into tokens with the compiled lexer and
31 * validates the sequences of tokens produced by comparing their type
34 * @author Etienne Bergeron
36 public class CtfLexerTest
{
38 // ------------------------------------------------------------------------
40 // ------------------------------------------------------------------------
42 private final List
<Token
> tokens
= new LinkedList
<>();
44 // ------------------------------------------------------------------------
46 // ------------------------------------------------------------------------
48 private void tokenize(String content
) {
49 CharStream cs
= new ANTLRStringStream(content
);
50 CTFLexer lexer
= new CTFLexer(cs
);
54 Token token
= lexer
.nextToken();
55 if (token
== Token
.EOF_TOKEN
) {
62 private void checkToken(int type
, String content
) {
63 Token token
= tokens
.remove(0);
64 if (token
.getType() != type
) {
65 fail("Invalid type [value " + token
.getType()
66 + " but expect " + type
+ "]."
67 + " Fail to tokenize:" + content
);
68 } else if (token
.getText().compareTo(content
) != 0) {
69 fail("Invalid content [value " + token
.getText()
70 + " but expect " + content
+ "].");
74 private void checkSingle(int type
, String content
) {
76 checkToken(type
, content
);
79 // ------------------------------------------------------------------------
81 // ------------------------------------------------------------------------
84 * Validate the parsing of keywords
87 public void testKeywords() {
88 checkSingle(CTFLexer
.ALIGNTOK
, "align");
89 checkSingle(CTFLexer
.CONSTTOK
, "const");
90 checkSingle(CTFLexer
.CHARTOK
, "char");
91 checkSingle(CTFLexer
.DOUBLETOK
, "double");
92 checkSingle(CTFLexer
.ENUMTOK
, "enum");
93 checkSingle(CTFLexer
.EVENTTOK
, "event");
94 checkSingle(CTFLexer
.FLOATINGPOINTTOK
, "floating_point");
95 checkSingle(CTFLexer
.FLOATTOK
, "float");
96 checkSingle(CTFLexer
.INTEGERTOK
, "integer");
97 checkSingle(CTFLexer
.INTTOK
, "int");
98 checkSingle(CTFLexer
.LONGTOK
, "long");
99 checkSingle(CTFLexer
.SHORTTOK
, "short");
100 checkSingle(CTFLexer
.SIGNEDTOK
, "signed");
101 checkSingle(CTFLexer
.STREAMTOK
, "stream");
102 checkSingle(CTFLexer
.STRINGTOK
, "string");
103 checkSingle(CTFLexer
.STRUCTTOK
, "struct");
104 checkSingle(CTFLexer
.TRACETOK
, "trace");
105 checkSingle(CTFLexer
.TYPEALIASTOK
, "typealias");
106 checkSingle(CTFLexer
.TYPEDEFTOK
, "typedef");
107 checkSingle(CTFLexer
.UNSIGNEDTOK
, "unsigned");
108 checkSingle(CTFLexer
.VARIANTTOK
, "variant");
109 checkSingle(CTFLexer
.VOIDTOK
, "void");
110 checkSingle(CTFLexer
.BOOLTOK
, "_Bool");
111 checkSingle(CTFLexer
.COMPLEXTOK
, "_Complex");
112 checkSingle(CTFLexer
.IMAGINARYTOK
, "_Imaginary");
113 checkSingle(CTFLexer
.ENVTOK
, "env");
114 checkSingle(CTFLexer
.CLOCKTOK
, "clock");
115 checkSingle(CTFLexer
.NANNUMBERTOK
, "NaN");
116 checkSingle(CTFLexer
.INFINITYTOK
, "+inf");
117 checkSingle(CTFLexer
.NINFINITYTOK
, "-inf");
121 * Validate the parsing of symbols
124 public void testSymbols() {
125 tokenize(" , : ... ");
126 checkToken(CTFLexer
.WS
, " ");
127 checkToken(CTFLexer
.SEPARATOR
, ",");
128 checkToken(CTFLexer
.WS
, " ");
129 checkToken(CTFLexer
.COLON
, ":");
130 checkToken(CTFLexer
.WS
, " ");
131 checkToken(CTFLexer
.ELIPSES
, "...");
132 checkToken(CTFLexer
.WS
, " ");
134 tokenize(" = := = ");
135 checkToken(CTFLexer
.WS
, " ");
136 checkToken(CTFLexer
.ASSIGNMENT
, "=");
137 checkToken(CTFLexer
.WS
, " ");
138 checkToken(CTFLexer
.TYPE_ASSIGNMENT
, ":=");
139 checkToken(CTFLexer
.WS
, " ");
142 checkToken(CTFLexer
.WS
, " ");
143 checkToken(CTFLexer
.LT
, "<");
144 checkToken(CTFLexer
.LT
, "<");
145 checkToken(CTFLexer
.GT
, ">");
146 checkToken(CTFLexer
.GT
, ">");
147 checkToken(CTFLexer
.WS
, " ");
149 tokenize(" ({[]}) ");
150 checkToken(CTFLexer
.WS
, " ");
151 checkToken(CTFLexer
.LPAREN
, "(");
152 checkToken(CTFLexer
.LCURL
, "{");
153 checkToken(CTFLexer
.OPENBRAC
, "[");
154 checkToken(CTFLexer
.CLOSEBRAC
, "]");
155 checkToken(CTFLexer
.RCURL
, "}");
156 checkToken(CTFLexer
.RPAREN
, ")");
157 checkToken(CTFLexer
.WS
, " ");
160 checkToken(CTFLexer
.TERM
, ";");
161 checkToken(CTFLexer
.TERM
, ";");
164 checkToken(CTFLexer
.WS
, " ");
165 checkToken(CTFLexer
.SIGN
, "+");
166 checkToken(CTFLexer
.SIGN
, "+");
167 checkToken(CTFLexer
.WS
, " ");
168 checkToken(CTFLexer
.SIGN
, "-");
169 checkToken(CTFLexer
.SIGN
, "-");
170 checkToken(CTFLexer
.WS
, " ");
173 checkToken(CTFLexer
.ARROW
, "->");
174 checkToken(CTFLexer
.WS
, " ");
175 checkToken(CTFLexer
.DOT
, ".");
176 checkToken(CTFLexer
.POINTER
, "*");
177 checkToken(CTFLexer
.DOT
, ".");
181 * Validate the parsing of literals
184 public void testLiterals() {
185 tokenize("01 02 010");
186 checkToken(CTFLexer
.OCTAL_LITERAL
, "01");
187 checkToken(CTFLexer
.WS
, " ");
188 checkToken(CTFLexer
.OCTAL_LITERAL
, "02");
189 checkToken(CTFLexer
.WS
, " ");
190 checkToken(CTFLexer
.OCTAL_LITERAL
, "010");
192 tokenize("1 2 10 1024 ");
193 checkToken(CTFLexer
.DECIMAL_LITERAL
, "1");
194 checkToken(CTFLexer
.WS
, " ");
195 checkToken(CTFLexer
.DECIMAL_LITERAL
, "2");
196 checkToken(CTFLexer
.WS
, " ");
197 checkToken(CTFLexer
.DECIMAL_LITERAL
, "10");
198 checkToken(CTFLexer
.WS
, " ");
199 checkToken(CTFLexer
.DECIMAL_LITERAL
, "1024");
200 checkToken(CTFLexer
.WS
, " ");
202 tokenize("0x01 0x02 0x0F0");
203 checkToken(CTFLexer
.HEX_LITERAL
, "0x01");
204 checkToken(CTFLexer
.WS
, " ");
205 checkToken(CTFLexer
.HEX_LITERAL
, "0x02");
206 checkToken(CTFLexer
.WS
, " ");
207 checkToken(CTFLexer
.HEX_LITERAL
, "0x0F0");
211 * Validate the parsing of literals with hexa prefix
214 public void testLiteralPrefixes() {
215 checkSingle(CTFLexer
.HEX_LITERAL
, "0x1");
216 checkSingle(CTFLexer
.HEX_LITERAL
, "0X1");
220 * Validate the parsing of literals with type suffix
223 public void testLiteralSuffixes() {
224 checkSingle(CTFLexer
.DECIMAL_LITERAL
, "0l");
225 checkSingle(CTFLexer
.DECIMAL_LITERAL
, "0L");
226 checkSingle(CTFLexer
.DECIMAL_LITERAL
, "0ll");
227 checkSingle(CTFLexer
.DECIMAL_LITERAL
, "0LL");
228 checkSingle(CTFLexer
.DECIMAL_LITERAL
, "0ul");
229 checkSingle(CTFLexer
.DECIMAL_LITERAL
, "0uL");
230 checkSingle(CTFLexer
.DECIMAL_LITERAL
, "0ull");
231 checkSingle(CTFLexer
.DECIMAL_LITERAL
, "0uLL");
232 checkSingle(CTFLexer
.DECIMAL_LITERAL
, "0Ul");
233 checkSingle(CTFLexer
.DECIMAL_LITERAL
, "0UL");
234 checkSingle(CTFLexer
.DECIMAL_LITERAL
, "0Ull");
235 checkSingle(CTFLexer
.DECIMAL_LITERAL
, "0ULL");
239 * Validate the accepted characters in literals.
242 public void testLiteralDigits() {
243 checkSingle(CTFLexer
.OCTAL_LITERAL
, "001234567");
245 checkSingle(CTFLexer
.DECIMAL_LITERAL
, "123456");
246 checkSingle(CTFLexer
.DECIMAL_LITERAL
, "987654");
248 checkSingle(CTFLexer
.HEX_LITERAL
, "0x012345");
249 checkSingle(CTFLexer
.HEX_LITERAL
, "0x678990");
250 checkSingle(CTFLexer
.HEX_LITERAL
, "0xABCDEF");
251 checkSingle(CTFLexer
.HEX_LITERAL
, "0xabcdef");
255 * Validate zero literal to be the right token.
258 public void testLiteralZero() {
259 checkSingle(CTFLexer
.OCTAL_LITERAL
, "00");
260 checkSingle(CTFLexer
.DECIMAL_LITERAL
, "0");
261 checkSingle(CTFLexer
.HEX_LITERAL
, "0x0");
265 * Validate character literals
268 public void testCharLiteral() {
269 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "'x'");
270 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "'\\''");
271 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "' '");
272 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "L'1'");
276 * Validate escaped character literals
279 public void testEscapeCharLiteral() {
280 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "'\\a'");
281 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "'\\b'");
282 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "'\\f'");
283 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "'\\n'");
284 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "'\\r'");
285 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "'\\t'");
286 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "'\\v'");
287 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "'\\''");
288 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "'\\\"'");
289 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "'\\\\'");
291 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "'\001'");
292 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "'\01'");
293 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "'\1'");
295 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "'\\x1A'");
296 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "'\\x1a'");
297 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "'\\xa'");
298 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "'\\x0'");
300 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "'\uABCD'");
301 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "'\u0123'");
302 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "'\u012345678'");
303 checkSingle(CTFLexer
.CHARACTER_LITERAL
, "'\uFEDCBA987'");
307 * Validate string literals
310 public void testStringLiteral() {
311 checkSingle(CTFLexer
.STRING_LITERAL
, "\"\"");
312 checkSingle(CTFLexer
.STRING_LITERAL
, "\"x\"");
313 checkSingle(CTFLexer
.STRING_LITERAL
, "\"\\\"\"");
314 checkSingle(CTFLexer
.STRING_LITERAL
, "\" \"");
315 checkSingle(CTFLexer
.STRING_LITERAL
, "L\"1\"");
317 checkSingle(CTFLexer
.STRING_LITERAL
, "\"This is \\n a multiline\\r\\n\"");
318 checkSingle(CTFLexer
.STRING_LITERAL
, "L\"This is \\n a multiline\\r\\n\"");
322 * Validate string literals with escape sequence
325 public void testEscapeStringLiteral() {
326 checkSingle(CTFLexer
.STRING_LITERAL
, "\"\\a\"");
327 checkSingle(CTFLexer
.STRING_LITERAL
, "\"\\b\"");
328 checkSingle(CTFLexer
.STRING_LITERAL
, "\"\\f\"");
329 checkSingle(CTFLexer
.STRING_LITERAL
, "\"\\n\"");
330 checkSingle(CTFLexer
.STRING_LITERAL
, "\"\\r\"");
331 checkSingle(CTFLexer
.STRING_LITERAL
, "\"\\t\"");
332 checkSingle(CTFLexer
.STRING_LITERAL
, "\"\\v\"");
333 checkSingle(CTFLexer
.STRING_LITERAL
, "\"\\'\"");
334 checkSingle(CTFLexer
.STRING_LITERAL
, "\"\\\"\"");
335 checkSingle(CTFLexer
.STRING_LITERAL
, "\"\\\\\"");
337 checkSingle(CTFLexer
.STRING_LITERAL
, "\"\001\"");
338 checkSingle(CTFLexer
.STRING_LITERAL
, "\"\01\"");
339 checkSingle(CTFLexer
.STRING_LITERAL
, "\"\1\"");
341 checkSingle(CTFLexer
.STRING_LITERAL
, "\"\\x1A\"");
342 checkSingle(CTFLexer
.STRING_LITERAL
, "\"\\x1a\"");
343 checkSingle(CTFLexer
.STRING_LITERAL
, "\"\\xa\"");
344 checkSingle(CTFLexer
.STRING_LITERAL
, "\"\\x0\"");
346 checkSingle(CTFLexer
.STRING_LITERAL
, "\"\uABCD\"");
347 checkSingle(CTFLexer
.STRING_LITERAL
, "\"\u0123\"");
348 checkSingle(CTFLexer
.STRING_LITERAL
, "\"\u012345678\"");
349 checkSingle(CTFLexer
.STRING_LITERAL
, "\"\uFEDCBA987\"");
353 * Validate spaces parsing
356 public void testWhitespaces() {
357 tokenize(" \r\t\n\u000C ");
358 checkToken(CTFLexer
.WS
, " ");
359 checkToken(CTFLexer
.WS
, " ");
360 checkToken(CTFLexer
.WS
, "\r");
361 checkToken(CTFLexer
.WS
, "\t");
362 checkToken(CTFLexer
.WS
, "\n");
363 checkToken(CTFLexer
.WS
, "\u000C");
364 checkToken(CTFLexer
.WS
, " ");
368 * Validate comments parsing
371 public void testComment() {
372 tokenize(" /* test */ ");
373 checkToken(CTFLexer
.WS
, " ");
374 checkToken(CTFLexer
.COMMENT
, "/* test */");
375 checkToken(CTFLexer
.WS
, " ");
379 * Validate complex nested comments parsing
382 public void testNestedComment() {
383 tokenize(" /* /* */ ");
384 checkToken(CTFLexer
.WS
, " ");
385 checkToken(CTFLexer
.COMMENT
, "/* /* */");
386 checkToken(CTFLexer
.WS
, " ");
388 tokenize(" /* /* * ** / */ ");
389 checkToken(CTFLexer
.WS
, " ");
390 checkToken(CTFLexer
.COMMENT
, "/* /* * ** / */");
391 checkToken(CTFLexer
.WS
, " ");
395 * Validate multi-lines comments
398 public void testMultiLineComment() {
399 tokenize(" /*\ntest\n*/ ");
400 checkToken(CTFLexer
.WS
, " ");
401 checkToken(CTFLexer
.COMMENT
, "/*\ntest\n*/");
402 checkToken(CTFLexer
.WS
, " ");
406 * Validate single line comments
409 public void testLineComment() {
410 tokenize(" // asdad\r\n ");
411 checkToken(CTFLexer
.WS
, " ");
412 checkToken(CTFLexer
.LINE_COMMENT
, "// asdad\r\n");
413 checkToken(CTFLexer
.WS
, " ");
417 * Validate incomplete comments parsing
419 @Ignore("Lexer must be fixed first")
421 public void testLineCommentWithEOF() {
423 checkToken(CTFLexer
.LINE_COMMENT
, "//");
427 * Validate parsing of mixed kind of comments
430 public void testMixedComment() {
431 tokenize(" // /*\n");
432 checkToken(CTFLexer
.WS
, " ");
433 checkToken(CTFLexer
.LINE_COMMENT
, "// /*\n");
435 tokenize(" /*\n//\n*/ ");
436 checkToken(CTFLexer
.WS
, " ");
437 checkToken(CTFLexer
.COMMENT
, "/*\n//\n*/");
438 checkToken(CTFLexer
.WS
, " ");
442 * Validate parsing identifiers
445 public void testIdentifier() {
446 tokenize("_ a a1 B ");
447 checkToken(CTFLexer
.IDENTIFIER
, "_");
448 checkToken(CTFLexer
.WS
, " ");
449 checkToken(CTFLexer
.IDENTIFIER
, "a");
450 checkToken(CTFLexer
.WS
, " ");
451 checkToken(CTFLexer
.IDENTIFIER
, "a1");
452 checkToken(CTFLexer
.WS
, " ");
453 checkToken(CTFLexer
.IDENTIFIER
, "B");
454 checkToken(CTFLexer
.WS
, " ");
458 * Validate accepted characters within an identifier
461 public void testIdentifierLetters() {
462 checkSingle(CTFLexer
.IDENTIFIER
, "ABCDEFGHI");
463 checkSingle(CTFLexer
.IDENTIFIER
, "JKLMNOPQR");
464 checkSingle(CTFLexer
.IDENTIFIER
, "STUVWXYZ");
465 checkSingle(CTFLexer
.IDENTIFIER
, "abcdefghi");
466 checkSingle(CTFLexer
.IDENTIFIER
, "jklmnopqr");
467 checkSingle(CTFLexer
.IDENTIFIER
, "stuvwxyz");
468 checkSingle(CTFLexer
.IDENTIFIER
, "_0123456789");
This page took 0.041518 seconds and 4 git commands to generate.