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