1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
package org.anarres.cpp;
import java.util.Arrays;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.anarres.cpp.PreprocessorTest.assertType;
import static org.anarres.cpp.Token.*;
import static org.junit.Assert.*;
public class LexerSourceTest {
private static final Logger LOG = LoggerFactory.getLogger(LexerSourceTest.class);
public static void testLexerSource(String in, boolean textmatch, int... out)
throws Exception {
LOG.info("Testing '" + in + "' => "
+ Arrays.toString(out));
StringLexerSource s = new StringLexerSource(in);
StringBuilder buf = new StringBuilder();
for (int i = 0; i < out.length; i++) {
Token tok = s.token();
LOG.info("Token is " + tok);
assertType(out[i], tok);
// assertEquals(col, tok.getColumn());
buf.append(tok.getText());
}
Token tok = s.token();
LOG.info("Token is " + tok);
assertType(EOF, tok);
if (textmatch)
assertEquals(in, buf.toString());
}
@Test
public void testLexerSource()
throws Exception {
testLexerSource("int a = 5;", true,
IDENTIFIER, WHITESPACE, IDENTIFIER, WHITESPACE,
'=', WHITESPACE, NUMBER, ';'
);
// \n is WHITESPACE because ppvalid = false
testLexerSource("# # \r\n\n\r \rfoo", true,
HASH, WHITESPACE, '#', WHITESPACE, IDENTIFIER
);
// No match - trigraphs
testLexerSource("%:%:", false, PASTE);
testLexerSource("%:?", false, '#', '?');
testLexerSource("%:%=", false, '#', MOD_EQ);
testLexerSource("0x1234ffdUL 0765I", true,
NUMBER, WHITESPACE, NUMBER);
testLexerSource("+= -= *= /= %= <= >= >>= <<= &= |= ^= x", true,
PLUS_EQ, WHITESPACE,
SUB_EQ, WHITESPACE,
MULT_EQ, WHITESPACE,
DIV_EQ, WHITESPACE,
MOD_EQ, WHITESPACE,
LE, WHITESPACE,
GE, WHITESPACE,
RSH_EQ, WHITESPACE,
LSH_EQ, WHITESPACE,
AND_EQ, WHITESPACE,
OR_EQ, WHITESPACE,
XOR_EQ, WHITESPACE,
IDENTIFIER);
testLexerSource("/**/", true, CCOMMENT);
testLexerSource("/* /**/ */", true, CCOMMENT, WHITESPACE, '*', '/');
testLexerSource("/** ** **/", true, CCOMMENT);
testLexerSource("//* ** **/", true, CPPCOMMENT);
testLexerSource("'\\r' '\\xf' '\\xff' 'x' 'aa' ''", true,
CHARACTER, WHITESPACE,
CHARACTER, WHITESPACE,
CHARACTER, WHITESPACE,
CHARACTER, WHITESPACE,
SQSTRING, WHITESPACE,
SQSTRING);
if (false) // Actually, I think this is illegal.
testLexerSource("1i1I1l1L1ui1ul", true,
NUMBER, NUMBER,
NUMBER, NUMBER,
NUMBER, NUMBER);
testLexerSource("'' 'x' 'xx'", true,
SQSTRING, WHITESPACE, CHARACTER, WHITESPACE, SQSTRING);
}
@Test
public void testNumbers() throws Exception {
testLexerSource("0", true, NUMBER);
testLexerSource("045", true, NUMBER);
testLexerSource("45", true, NUMBER);
testLexerSource("0.45", true, NUMBER);
testLexerSource("1.45", true, NUMBER);
testLexerSource("1e6", true, NUMBER);
testLexerSource("1.45e6", true, NUMBER);
testLexerSource(".45e6", true, NUMBER);
testLexerSource("-6", true, '-', NUMBER);
}
@Test
public void testNumbersSuffix() throws Exception {
testLexerSource("6f", true, NUMBER);
testLexerSource("6d", true, NUMBER);
testLexerSource("6l", true, NUMBER);
testLexerSource("6ll", true, NUMBER);
testLexerSource("6ul", true, NUMBER);
testLexerSource("6ull", true, NUMBER);
testLexerSource("6e3f", true, NUMBER);
testLexerSource("6e3d", true, NUMBER);
testLexerSource("6e3l", true, NUMBER);
testLexerSource("6e3ll", true, NUMBER);
testLexerSource("6e3ul", true, NUMBER);
testLexerSource("6e3ull", true, NUMBER);
}
@Test
public void testNumbersInvalid() throws Exception {
// testLexerSource("0x foo", true, INVALID, WHITESPACE, IDENTIFIER); // FAIL
testLexerSource("6x foo", true, INVALID, WHITESPACE, IDENTIFIER);
testLexerSource("6g foo", true, INVALID, WHITESPACE, IDENTIFIER);
testLexerSource("6xsd foo", true, INVALID, WHITESPACE, IDENTIFIER);
testLexerSource("6gsd foo", true, INVALID, WHITESPACE, IDENTIFIER);
}
@Test
public void testUnterminatedComment() throws Exception {
testLexerSource("5 /*", false, NUMBER, WHITESPACE, INVALID); // Bug #15
testLexerSource("5 //", false, NUMBER, WHITESPACE, CPPCOMMENT);
}
}
|