1 |
header |
2 |
{ |
3 |
|
4 |
#include "antlr/CharScanner.hpp" |
5 |
#include "utils/StringUtils.hpp" |
6 |
#include "mdParser/FilenameObserver.hpp" |
7 |
} |
8 |
|
9 |
options |
10 |
{ |
11 |
language = "Cpp"; |
12 |
} |
13 |
|
14 |
class MDParser extends Parser; |
15 |
|
16 |
options |
17 |
{ |
18 |
k = 3; |
19 |
exportVocab = MD; |
20 |
buildAST = true; |
21 |
codeGenMakeSwitchThreshold = 2; |
22 |
codeGenBitsetTestThreshold = 3; |
23 |
|
24 |
} |
25 |
|
26 |
tokens |
27 |
{ |
28 |
COMPONENT = "component"; |
29 |
MOLECULE = "molecule"; |
30 |
ZCONSTRAINT = "zconstraint"; |
31 |
RESTRAINT = "restraint"; |
32 |
ATOM = "atom"; |
33 |
BOND = "bond"; |
34 |
BEND = "bend"; |
35 |
TORSION = "torsion"; |
36 |
INVERSION = "inversion"; |
37 |
RIGIDBODY = "rigidBody"; |
38 |
CUTOFFGROUP = "cutoffGroup"; |
39 |
FRAGMENT = "fragment"; |
40 |
MEMBERS = "members"; |
41 |
CENTER = "center"; |
42 |
POSITION = "position"; |
43 |
ORIENTATION = "orientation"; |
44 |
FLUCQ = "flucQ"; |
45 |
RNEMD = "RNEMD"; |
46 |
ENDBLOCK; |
47 |
} |
48 |
|
49 |
|
50 |
mdfile : (statement)* |
51 |
; |
52 |
|
53 |
statement : assignment |
54 |
| componentblock |
55 |
| moleculeblock |
56 |
| zconstraintblock |
57 |
| restraintblock |
58 |
| flucqblock |
59 |
| rnemdblock |
60 |
; |
61 |
|
62 |
assignment : ID ASSIGNEQUAL^ constant SEMICOLON! |
63 |
; |
64 |
|
65 |
constant : intConst |
66 |
| floatConst |
67 |
| ID |
68 |
| StringLiteral |
69 |
; |
70 |
|
71 |
componentblock : COMPONENT^ LCURLY! (assignment)* RCURLY {#RCURLY->setType(ENDBLOCK);} |
72 |
; |
73 |
|
74 |
zconstraintblock : ZCONSTRAINT^ LCURLY! (assignment)* RCURLY {#RCURLY->setType(ENDBLOCK);} |
75 |
; |
76 |
|
77 |
restraintblock : RESTRAINT^ LCURLY! (assignment)* RCURLY {#RCURLY->setType(ENDBLOCK);} |
78 |
; |
79 |
|
80 |
flucqblock : FLUCQ^ LCURLY! (assignment)* RCURLY {#RCURLY->setType(ENDBLOCK);} |
81 |
; |
82 |
|
83 |
rnemdblock : RNEMD^ LCURLY! (assignment)* RCURLY {#RCURLY->setType(ENDBLOCK);} |
84 |
; |
85 |
|
86 |
moleculeblock : MOLECULE^ LCURLY! (moleculestatement)* RCURLY {#RCURLY->setType(ENDBLOCK);} |
87 |
; |
88 |
|
89 |
moleculestatement : assignment |
90 |
| atomblock |
91 |
| bondblock |
92 |
| bendblock |
93 |
| torsionblock |
94 |
| inversionblock |
95 |
| rigidbodyblock |
96 |
| cutoffgroupblock |
97 |
| fragmentblock |
98 |
; |
99 |
|
100 |
atomblock : ATOM^ LBRACKET! intConst RBRACKET! LCURLY! (atomstatement)* RCURLY {#RCURLY->setType(ENDBLOCK);} |
101 |
; |
102 |
|
103 |
atomstatement : assignment |
104 |
| POSITION^ LPAREN! doubleNumberTuple RPAREN! SEMICOLON! |
105 |
| ORIENTATION^ LPAREN! doubleNumberTuple RPAREN! SEMICOLON! |
106 |
; |
107 |
|
108 |
|
109 |
bondblock : BOND^ (LBRACKET! intConst! RBRACKET!)? LCURLY!(bondstatement)* RCURLY {#RCURLY->setType(ENDBLOCK);} |
110 |
; |
111 |
|
112 |
bondstatement : assignment |
113 |
| MEMBERS^ LPAREN! inttuple RPAREN! SEMICOLON! |
114 |
; |
115 |
|
116 |
bendblock : BEND^ (LBRACKET! intConst! RBRACKET!)? LCURLY! (bendstatement)* RCURLY {#RCURLY->setType(ENDBLOCK);} |
117 |
; |
118 |
|
119 |
bendstatement : assignment |
120 |
| MEMBERS^ LPAREN! inttuple RPAREN! SEMICOLON! |
121 |
; |
122 |
|
123 |
torsionblock : TORSION^ (LBRACKET! intConst! RBRACKET!)? LCURLY!(torsionstatement)* RCURLY {#RCURLY->setType(ENDBLOCK);} |
124 |
; |
125 |
|
126 |
torsionstatement : assignment |
127 |
| MEMBERS^ LPAREN! inttuple RPAREN! SEMICOLON! |
128 |
; |
129 |
|
130 |
inversionblock : INVERSION^ (LBRACKET! intConst! RBRACKET!)? LCURLY!(inversionstatement)* RCURLY {#RCURLY->setType(ENDBLOCK);} |
131 |
; |
132 |
|
133 |
inversionstatement : assignment |
134 |
| CENTER^ LPAREN! intConst RPAREN! SEMICOLON! |
135 |
; |
136 |
|
137 |
rigidbodyblock : RIGIDBODY^ LBRACKET! intConst RBRACKET! LCURLY!(rigidbodystatement)* RCURLY {#RCURLY->setType(ENDBLOCK);} |
138 |
; |
139 |
|
140 |
rigidbodystatement : assignment |
141 |
| MEMBERS^ LPAREN! inttuple RPAREN! SEMICOLON! |
142 |
; |
143 |
|
144 |
cutoffgroupblock : CUTOFFGROUP^ (LBRACKET! intConst! RBRACKET!)? LCURLY! (cutoffgroupstatement)* RCURLY {#RCURLY->setType(ENDBLOCK);} |
145 |
; |
146 |
|
147 |
cutoffgroupstatement : assignment |
148 |
| MEMBERS^ LPAREN! inttuple RPAREN! SEMICOLON! |
149 |
; |
150 |
|
151 |
fragmentblock : FRAGMENT^ LBRACKET! intConst RBRACKET! LCURLY! (fragmentstatement)* RCURLY {#RCURLY->setType(ENDBLOCK);} |
152 |
; |
153 |
|
154 |
fragmentstatement : assignment |
155 |
; |
156 |
|
157 |
|
158 |
|
159 |
doubleNumberTuple : doubleNumber (COMMA! doubleNumber)* |
160 |
; |
161 |
|
162 |
inttuple : intConst (COMMA! intConst)* |
163 |
; |
164 |
|
165 |
protected |
166 |
intConst |
167 |
: NUM_INT | NUM_LONG |
168 |
; |
169 |
|
170 |
protected |
171 |
doubleNumber : |
172 |
(intConst | floatConst) |
173 |
; |
174 |
|
175 |
protected |
176 |
floatConst |
177 |
: |
178 |
NUM_FLOAT | NUM_DOUBLE |
179 |
; |
180 |
|
181 |
|
182 |
|
183 |
class MDLexer extends Lexer; |
184 |
|
185 |
options |
186 |
{ |
187 |
k = 3; |
188 |
exportVocab = MD; |
189 |
testLiterals = false; |
190 |
} |
191 |
|
192 |
tokens { |
193 |
DOT; |
194 |
} |
195 |
|
196 |
{ |
197 |
|
198 |
|
199 |
int deferredLineCount; |
200 |
FilenameObserver* observer; |
201 |
|
202 |
public: |
203 |
void setObserver(FilenameObserver* osv) {observer = osv;} |
204 |
void initDeferredLineCount() { deferredLineCount = 0;} |
205 |
void deferredNewline() { |
206 |
deferredLineCount++; |
207 |
} |
208 |
|
209 |
|
210 |
virtual void newline() { |
211 |
for (;deferredLineCount>0;deferredLineCount--) { |
212 |
CharScanner::newline(); |
213 |
} |
214 |
CharScanner::newline(); |
215 |
} |
216 |
|
217 |
} |
218 |
|
219 |
|
220 |
// Operators: |
221 |
|
222 |
ASSIGNEQUAL : '=' ; |
223 |
COLON : ':' ; |
224 |
COMMA : ',' ; |
225 |
QUESTIONMARK : '?' ; |
226 |
SEMICOLON : ';' ; |
227 |
|
228 |
LPAREN : '(' ; |
229 |
RPAREN : ')' ; |
230 |
LBRACKET : '[' ; |
231 |
RBRACKET : ']' ; |
232 |
LCURLY : '{' ; |
233 |
RCURLY : '}' ; |
234 |
|
235 |
Whitespace |
236 |
: |
237 |
( // whitespace ignored |
238 |
(' ' |'\t' | '\f') |
239 |
| // handle newlines |
240 |
( '\r' '\n' // MS |
241 |
| '\r' // Mac |
242 |
| '\n' // Unix |
243 |
) { newline(); } |
244 |
| // handle continuation lines |
245 |
( '\\' '\r' '\n' // MS |
246 |
| '\\' '\r' // Mac |
247 |
| '\\' '\n' // Unix |
248 |
) {printf("CPP_parser.g continuation line detected\n"); |
249 |
deferredNewline();} |
250 |
) |
251 |
{_ttype = ANTLR_USE_NAMESPACE(antlr)Token::SKIP;} |
252 |
; |
253 |
|
254 |
Comment |
255 |
: |
256 |
"/*" |
257 |
( {LA(2) != '/'}? '*' |
258 |
| EndOfLine {deferredNewline();} |
259 |
| ~('*'| '\r' | '\n') |
260 |
)* |
261 |
"*/" {_ttype = ANTLR_USE_NAMESPACE(antlr)Token::SKIP;} |
262 |
; |
263 |
|
264 |
CPPComment |
265 |
: |
266 |
"//" (~('\n' | '\r'))* EndOfLine |
267 |
{_ttype = ANTLR_USE_NAMESPACE(antlr)Token::SKIP; newline();} |
268 |
; |
269 |
|
270 |
PREPROC_DIRECTIVE |
271 |
options{paraphrase = "a line directive";} |
272 |
: |
273 |
'#' LineDirective |
274 |
{_ttype = ANTLR_USE_NAMESPACE(antlr)Token::SKIP; newline();} |
275 |
; |
276 |
|
277 |
protected |
278 |
LineDirective |
279 |
: |
280 |
{ |
281 |
deferredLineCount = 0; |
282 |
} |
283 |
("line")? // this would be for if the directive started "#line" |
284 |
(Space)+ |
285 |
n:Decimal { setLine(OpenMD::lexi_cast<int>(n->getText()) - 1); } |
286 |
(Space)+ |
287 |
(sl:StringLiteral) {std::string filename = sl->getText().substr(1,sl->getText().length()-2); observer->notify(filename);} |
288 |
((Space)+ Decimal)* // To support cpp flags (GNU) |
289 |
EndOfLine |
290 |
; |
291 |
|
292 |
protected |
293 |
Space |
294 |
: |
295 |
(' '|'\t'|'\f') |
296 |
; |
297 |
|
298 |
|
299 |
// Literals: |
300 |
|
301 |
/* |
302 |
* Note that we do NOT handle tri-graphs nor multi-byte sequences. |
303 |
*/ |
304 |
|
305 |
/* |
306 |
* Note that we can't have empty character constants (even though we |
307 |
* can have empty strings :-). |
308 |
*/ |
309 |
CharLiteral |
310 |
: |
311 |
'\'' (Escape | ~('\'')) '\'' |
312 |
; |
313 |
|
314 |
/* |
315 |
* Can't have raw imbedded newlines in string constants. Strict reading of |
316 |
* the standard gives odd dichotomy between newlines & carriage returns. |
317 |
* Go figure. |
318 |
*/ |
319 |
StringLiteral |
320 |
: |
321 |
'"' |
322 |
( Escape |
323 |
| |
324 |
( "\\\r\n" // MS |
325 |
| "\\\r" // MAC |
326 |
| "\\\n" // Unix |
327 |
) {deferredNewline();} |
328 |
| |
329 |
~('"'|'\r'|'\n'|'\\') |
330 |
)* |
331 |
'"' |
332 |
; |
333 |
|
334 |
protected |
335 |
EndOfLine |
336 |
: |
337 |
( options{generateAmbigWarnings = false;}: |
338 |
"\r\n" // MS |
339 |
| '\r' // Mac |
340 |
| '\n' // Unix |
341 |
) |
342 |
; |
343 |
|
344 |
/* |
345 |
* Handle the various escape sequences. |
346 |
* |
347 |
* Note carefully that these numeric escape *sequences* are *not* of the |
348 |
* same form as the C language numeric *constants*. |
349 |
* |
350 |
* There is no such thing as a binary numeric escape sequence. |
351 |
* |
352 |
* Octal escape sequences are either 1, 2, or 3 octal digits exactly. |
353 |
* |
354 |
* There is no such thing as a decimal escape sequence. |
355 |
* |
356 |
* Hexadecimal escape sequences are begun with a leading \x and continue |
357 |
* until a non-hexadecimal character is found. |
358 |
* |
359 |
* No real handling of tri-graph sequences, yet. |
360 |
*/ |
361 |
|
362 |
protected |
363 |
Escape |
364 |
: |
365 |
'\\' |
366 |
( options{warnWhenFollowAmbig=false;}: |
367 |
'a' |
368 |
| 'b' |
369 |
| 'f' |
370 |
| 'n' |
371 |
| 'r' |
372 |
| 't' |
373 |
| 'v' |
374 |
| '"' |
375 |
| '\'' |
376 |
| '\\' |
377 |
| '?' |
378 |
| ('0'..'3') (options{warnWhenFollowAmbig=false;}: Digit (options{warnWhenFollowAmbig=false;}: Digit)? )? |
379 |
| ('4'..'7') (options{warnWhenFollowAmbig=false;}: Digit)? |
380 |
| 'x' (options{warnWhenFollowAmbig=false;}: Digit | 'a'..'f' | 'A'..'F')+ |
381 |
) |
382 |
; |
383 |
|
384 |
|
385 |
protected |
386 |
Vocabulary |
387 |
: |
388 |
'\3'..'\377' |
389 |
; |
390 |
|
391 |
|
392 |
ID |
393 |
options {testLiterals = true;} |
394 |
: |
395 |
('a'..'z'|'A'..'Z'|'_') |
396 |
('a'..'z'|'A'..'Z'|'_'|'0'..'9')* |
397 |
; |
398 |
|
399 |
|
400 |
protected |
401 |
Digit |
402 |
: |
403 |
'0'..'9' |
404 |
; |
405 |
|
406 |
protected |
407 |
Decimal |
408 |
: |
409 |
('0'..'9')+ |
410 |
; |
411 |
|
412 |
// hexadecimal digit (again, note it's protected!) |
413 |
protected |
414 |
HEX_DIGIT |
415 |
: ('0'..'9'|'A'..'F'|'a'..'f') |
416 |
; |
417 |
|
418 |
|
419 |
// a numeric literal |
420 |
NUM_INT |
421 |
{ |
422 |
bool isDecimal = false; |
423 |
ANTLR_USE_NAMESPACE(antlr)RefToken t = ANTLR_USE_NAMESPACE(antlr)nullToken; |
424 |
} |
425 |
: ('+' | '-')? |
426 |
( |
427 |
'.' {_ttype = DOT;} |
428 |
( ('0'..'9')+ (EXPONENT)? (f1:FLOAT_SUFFIX {t=f1;})? |
429 |
{ |
430 |
if ( t && |
431 |
(t->getText().find('f') != ANTLR_USE_NAMESPACE(std)string::npos || |
432 |
t->getText().find('F') != ANTLR_USE_NAMESPACE(std)string::npos ) ) { |
433 |
_ttype = NUM_FLOAT; |
434 |
} |
435 |
else { |
436 |
_ttype = NUM_DOUBLE; // assume double |
437 |
} |
438 |
} |
439 |
)? |
440 |
|
441 |
| ( '0' {isDecimal = true;} // special case for just '0' |
442 |
( ('x'|'X') |
443 |
( // hex |
444 |
// the 'e'|'E' and float suffix stuff look |
445 |
// like hex digits, hence the (...)+ doesn't |
446 |
// know when to stop: ambig. ANTLR resolves |
447 |
// it correctly by matching immediately. It |
448 |
// is therefor ok to hush warning. |
449 |
options { |
450 |
warnWhenFollowAmbig=false; |
451 |
} |
452 |
: HEX_DIGIT |
453 |
)+ |
454 |
| //float or double with leading zero |
455 |
(('0'..'9')+ ('.'|EXPONENT|FLOAT_SUFFIX)) => ('0'..'9')+ |
456 |
| ('0'..'7')+ // octal |
457 |
)? |
458 |
| ('1'..'9') ('0'..'9')* {isDecimal=true;} // non-zero decimal |
459 |
) |
460 |
( ('l'|'L') { _ttype = NUM_LONG; } |
461 |
|
462 |
// only check to see if it's a float if looks like decimal so far |
463 |
| {isDecimal}? |
464 |
( '.' ('0'..'9')* (EXPONENT)? (f2:FLOAT_SUFFIX {t=f2;})? |
465 |
| EXPONENT (f3:FLOAT_SUFFIX {t=f3;})? |
466 |
| f4:FLOAT_SUFFIX {t=f4;} |
467 |
) |
468 |
{ |
469 |
if ( t && |
470 |
(t->getText().find('f') != ANTLR_USE_NAMESPACE(std)string::npos || |
471 |
t->getText().find('F') != ANTLR_USE_NAMESPACE(std)string::npos ) ) { |
472 |
_ttype = NUM_FLOAT; |
473 |
} |
474 |
else { |
475 |
_ttype = NUM_DOUBLE; // assume double |
476 |
} |
477 |
} |
478 |
)? |
479 |
) |
480 |
; |
481 |
|
482 |
// a couple protected methods to assist in matching floating point numbers |
483 |
protected |
484 |
EXPONENT |
485 |
: ('e'|'E'|'d'|'D') ('+'|'-')? ('0'..'9')+ |
486 |
; |
487 |
|
488 |
protected |
489 |
FLOAT_SUFFIX |
490 |
: 'f'|'F'|'d'|'D' |
491 |
; |