Repository navigation
Expand file tree
/
Copy pathTextLexer.g
More file actions
304 lines (263 loc) · 8.53 KB
/
Copy pathTextLexer.g
File metadata and controls
304 lines (263 loc) · 8.53 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// SPDX-License-Identifier: GPL-3.0-only
/*!
* @file TextLexer.g
*
* @copyright Copyright (C) 2002-2024 srcML, LLC. (www.srcML.org)
*
* This file is part of the srcML translator.
*
* An antlr Lexer that passes tokens consisting of either
* whitespace or non-whitespace. Non-whitespace is output
* in a TEXT token. Contiguous word characters are combined
* in one token.
*
* Base lexer for srcML Lexer.
* Identifies keywords in the stream. Non-keywords and other text,
* including whitespace, is passed through unused.
*/
header {
#ifndef _MSC_VER
#else
#pragma warning(disable : 4242) // 'argument': conversion from 'int' to 'char'
#endif
}
options {
language="Cpp";
namespaceAntlr="antlr";
namespaceStd="std";
}
class TextLexer extends Lexer;
options {
k = 1;
testLiterals = false;
charVocabulary = '\000'..'\377';
importVocab = CommentTextLexer;
}
tokens {
BLOCK_COMMENT_START;
JAVADOC_COMMENT_START;
DOXYGEN_COMMENT_START;
LINE_DOXYGEN_COMMENT_START;
DQUOTE_DOCSTRING_START;
CHAR_START;
SQUOTE_DOCSTRING_START;
MACRO_NAME;
COMPLEX_NUMBER;
HASHBANG_COMMENT_START;
HASHTAG_COMMENT_START;
WS_EOL;
}
{
public:
bool onpreprocline;
std::string delimiter;
}
STRING_START :
{ startline = false; }
// double quoted string
// strings are allowed to span multiple lines
// special case is when it is one a preprocessor line, e.g.,
// #define a "abc
// note that the "abc does not end at the end of this line,
// but the #define must end, so EOL is not a valid string character
'"' {
// handle a potential triple-quoted string in Python
if (inLanguage(LANGUAGE_PYTHON) && LA(1) == '"')
changetotextlexer(PY_DQUOTE_STRING_START);
// handle a string that starts/ends with one double-quote in Python
else if (inLanguage(LANGUAGE_PYTHON))
changetotextlexer(PY_SIMPLE_DQUOTE_STRING_END);
else
changetotextlexer(STRING_END);
atstring = false;
}
;
protected
RAW_STRING_START :
{ startline = false; }
// double quoted string
// strings are allowed to span multiple lines
// special case is when it is one a preprocessor line, e.g.,
// #define a "abc
// note that the "abc does not end at the end of this line,
// but the #define must end, so EOL is not a valid string character
'"' RSTRING_DELIMITER ('(')?
{
changetotextlexer(RAW_STRING_END, delimiter);
atstring = false;
}
;
protected
RSTRING_DELIMITER:
{ delimiter = ""; }
(options { greedy = true; } : { delimiter += static_cast<char>(LA(1)); } ~('(' | ')' | '\\' | '\n' | ' ' | '\t' ))*
;
CHAR_START :
{ startline = false; }
// character literal or single quoted string
'\'' {
// handle a potential triple-quoted string in Python
if (inLanguage(LANGUAGE_PYTHON) && LA(1) == '\'')
changetotextlexer(PY_SQUOTE_STRING_START);
// handle a string that starts/ends with one single-quote in Python
else if (inLanguage(LANGUAGE_PYTHON))
changetotextlexer(PY_SIMPLE_SQUOTE_STRING_END);
else {
$setType(CHAR_START); changetotextlexer(CHAR_END);
}
}
;
CONSTANTS :
{ startline = false; }
('0'..'9') (options { greedy = true; } : '0'..'9' | '_')*
(options { greedy = true; } : '.' | '0'..'9')*
(options { greedy = true; } : ('e' | 'E') ('+' | '-')* ('0'..'9')*)?
(options { greedy = true; } :
'i' { $setType(COMPLEX_NUMBER); } |
{ inLanguage(LANGUAGE_PYTHON) }?
('j' | 'J') { $setType(COMPLEX_NUMBER); }
)*
(options { greedy = true; } : NAME)*
{
//firstpreprocline = false;
if (onpreprocline && isline) {
line_number = atoi(text.substr(_begin, text.length()-_begin).data());
}
}
;
NAME options { testLiterals = true; } :
{ startline = false; }
('a'..'z' | 'A'..'Z' | '_' | '\200'..'\377' | '$')
((options { greedy = true; } : '0'..'9' | 'a'..'z' | 'A'..'Z' | '_' | '\200'..'\377' | '$')*)
(
{ text == "L"sv || text == "U"sv || text == "u"sv || text == "u8"sv }?
{ $setType(STRING_START); } STRING_START |
{ (inLanguage(LANGUAGE_C) || inLanguage(LANGUAGE_CXX))
&& (text == "L"sv || text == "U"sv || text == "u"sv || text == "u8"sv) }?
{ $setType(CHAR_START); } CHAR_START |
{ inLanguage(LANGUAGE_CXX) && (text == "R"sv || text == "u8R"sv || text == "LR"sv || text == "UR"sv || text == "uR"sv) }?
{ $setType(STRING_START); } RAW_STRING_START |
{
inLanguage(LANGUAGE_PYTHON)
&& (text == "b"sv || text == "f"sv || text == "r"sv || text == "u"sv
|| text == "B"sv || text == "F"sv || text == "R"sv || text == "U"sv
|| text == "rf"sv || text == "rb"sv || text == "Rf"sv || text == "Rb"sv
|| text == "rF"sv || text == "rB"sv || text == "RF"sv || text == "RB"sv
|| text == "fr"sv || text == "fR"sv || text == "Fr"sv || text == "FR"sv
|| text == "ur"sv || text == "Ur"sv || text == "uR"sv || text == "UR"sv)
}?
(
{ LA(1) == '"' }?
{ $setType(STRING_START); } STRING_START |
{ LA(1) == '\'' }?
{ $setType(CHAR_START); } CHAR_START
)
)?
;
// Single-line comments (no EOL)
LINE_COMMENT_START options { testLiterals = true; } { int mode = 0; } : '/'
('/'
{
// '//' is an operator in Python
if (inLanguage(LANGUAGE_PYTHON)) {
$setType(OPERATORS);
mode = 0;
}
else
mode = LINE_COMMENT_END;
}
(
('/' | '!') { $setType(LINE_DOXYGEN_COMMENT_START); mode = LINE_DOXYGEN_COMMENT_END; } |
// '//=' is an operator in Python
{ inLanguage(LANGUAGE_PYTHON) }? ('=')
)? |
'*'
{
$setType(BLOCK_COMMENT_START);
mode = BLOCK_COMMENT_END;
}
(
{ inLanguage(LANGUAGE_JAVA) }? '*'
{
$setType(JAVADOC_COMMENT_START);
mode = JAVADOC_COMMENT_END;
} ('/' { $setType(WHOLE_COMMENT); mode = 0; })? |
{ inLanguage(LANGUAGE_CXX) || inLanguage(LANGUAGE_C) || inLanguage(LANGUAGE_CSHARP) }? ('*' | '!')
{
$setType(DOXYGEN_COMMENT_START);
mode = DOXYGEN_COMMENT_END;
} ('/' { $setType(WHOLE_COMMENT); mode = 0; })?
)? |
// /= is an operator
'=' )?
{
if (mode != 0) {
changetotextlexer(mode);
// when we return, we may have eaten the EOL, so we will turn back on startline
startline = true;
onpreprocline = false;
}
}
;
// whitespace (except for newline)
WS { int lastColumn = 0; } : (
// single space
' ' |
// horizontal tab
{ lastColumn = getColumn(); } '\t' {
// expand tab if option says to
if (isoption(options, SRCML_PARSER_OPTION_EXPAND_TABS)) {
// remove the tab character
text.pop_back();
// append the spaces
static const std::string_view spaces = " ";
text.append(spaces.substr(0, getColumn() - lastColumn));
}
}
)+
(
{ inLanguage(LANGUAGE_PYTHON) && LA(1) == '\n' }?
'\n'
{
$setType(WS_EOL);
onpreprocline = false;
startline = true;
newline();
if (isoption(options, SRCML_OPTION_LINE))
setLine(getLine() + (1 << 16));
if (isline && line_number > -1)
setLine((int)(line_number << 16 | (getLine() & 0xFFFF)));
isline = false;
line_number = -1;
}
)? ;
// end of line
EOL : '\n' {
// onpreprocline is turned on when on a preprocessor line
// to prevent mostly string ending problems.
// it has to be turned back on when the EOL is reached
onpreprocline = false;
// mark that we are starting a new line, so preproc
// can be detected
startline = true;
// record to new lines for optional positions
newline();
if (isoption(options, SRCML_OPTION_LINE))
setLine(getLine() + (1 << 16));
if (isline && line_number > -1)
setLine((int)(line_number << 16 | (getLine() & 0xFFFF)));
isline = false;
line_number = -1;
} ;
/*
Encode the control character in the text, so that is can be
issued in an escape character.
*/
CONTROL_CHAR :
{ startline = true; }
(
'\000'..'\010' |
'\013'..'\014' |
'\016'..'\037'
)
;