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