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 |
protected |
199 |
vectorConst |
200 |
: |
201 |
LPAREN^ doubleNumber COMMA doubleNumber COMMA doubleNumber RPAREN |
202 |
; |
203 |
|
204 |
class MDLexer extends Lexer; |
205 |
|
206 |
options |
207 |
{ |
208 |
k = 3; |
209 |
exportVocab = MD; |
210 |
testLiterals = false; |
211 |
} |
212 |
|
213 |
tokens { |
214 |
DOT; |
215 |
} |
216 |
|
217 |
{ |
218 |
|
219 |
|
220 |
int deferredLineCount; |
221 |
FilenameObserver* observer; |
222 |
|
223 |
public: |
224 |
void setObserver(FilenameObserver* osv) {observer = osv;} |
225 |
void initDeferredLineCount() { deferredLineCount = 0;} |
226 |
void deferredNewline() { |
227 |
deferredLineCount++; |
228 |
} |
229 |
|
230 |
|
231 |
virtual void newline() { |
232 |
for (;deferredLineCount>0;deferredLineCount--) { |
233 |
CharScanner::newline(); |
234 |
} |
235 |
CharScanner::newline(); |
236 |
} |
237 |
|
238 |
} |
239 |
|
240 |
|
241 |
// Operators: |
242 |
|
243 |
ASSIGNEQUAL : '=' ; |
244 |
COLON : ':' ; |
245 |
COMMA : ',' ; |
246 |
QUESTIONMARK : '?' ; |
247 |
SEMICOLON : ';' ; |
248 |
|
249 |
LPAREN : '(' ; |
250 |
RPAREN : ')' ; |
251 |
LBRACKET : '[' ; |
252 |
RBRACKET : ']' ; |
253 |
LCURLY : '{' ; |
254 |
RCURLY : '}' ; |
255 |
|
256 |
Whitespace |
257 |
: |
258 |
( // whitespace ignored |
259 |
(' ' |'\t' | '\f') |
260 |
| // handle newlines |
261 |
( '\r' '\n' // MS |
262 |
| '\r' // Mac |
263 |
| '\n' // Unix |
264 |
) { newline(); } |
265 |
| // handle continuation lines |
266 |
( '\\' '\r' '\n' // MS |
267 |
| '\\' '\r' // Mac |
268 |
| '\\' '\n' // Unix |
269 |
) {printf("CPP_parser.g continuation line detected\n"); |
270 |
deferredNewline();} |
271 |
) |
272 |
{_ttype = ANTLR_USE_NAMESPACE(antlr)Token::SKIP;} |
273 |
; |
274 |
|
275 |
Comment |
276 |
: |
277 |
"/*" |
278 |
( {LA(2) != '/'}? '*' |
279 |
| EndOfLine {deferredNewline();} |
280 |
| ~('*'| '\r' | '\n') |
281 |
)* |
282 |
"*/" {_ttype = ANTLR_USE_NAMESPACE(antlr)Token::SKIP;} |
283 |
; |
284 |
|
285 |
CPPComment |
286 |
: |
287 |
"//" (~('\n' | '\r'))* EndOfLine |
288 |
{_ttype = ANTLR_USE_NAMESPACE(antlr)Token::SKIP; newline();} |
289 |
; |
290 |
|
291 |
PREPROC_DIRECTIVE |
292 |
options{paraphrase = "a line directive";} |
293 |
: |
294 |
'#' LineDirective |
295 |
{_ttype = ANTLR_USE_NAMESPACE(antlr)Token::SKIP; newline();} |
296 |
; |
297 |
|
298 |
protected |
299 |
LineDirective |
300 |
: |
301 |
{ |
302 |
deferredLineCount = 0; |
303 |
} |
304 |
("line")? // this would be for if the directive started "#line" |
305 |
(Space)+ |
306 |
n:Decimal { setLine(OpenMD::lexi_cast<int>(n->getText()) - 1); } |
307 |
(Space)+ |
308 |
(sl:StringLiteral) {std::string filename = sl->getText().substr(1,sl->getText().length()-2); observer->notify(filename);} |
309 |
((Space)+ Decimal)* // To support cpp flags (GNU) |
310 |
EndOfLine |
311 |
; |
312 |
|
313 |
protected |
314 |
Space |
315 |
: |
316 |
(' '|'\t'|'\f') |
317 |
; |
318 |
|
319 |
|
320 |
// Literals: |
321 |
|
322 |
/* |
323 |
* Note that we do NOT handle tri-graphs nor multi-byte sequences. |
324 |
*/ |
325 |
|
326 |
/* |
327 |
* Note that we can't have empty character constants (even though we |
328 |
* can have empty strings :-). |
329 |
*/ |
330 |
CharLiteral |
331 |
: |
332 |
'\'' (Escape | ~('\'')) '\'' |
333 |
; |
334 |
|
335 |
/* |
336 |
* Can't have raw imbedded newlines in string constants. Strict reading of |
337 |
* the standard gives odd dichotomy between newlines & carriage returns. |
338 |
* Go figure. |
339 |
*/ |
340 |
StringLiteral |
341 |
: |
342 |
'"' |
343 |
( Escape |
344 |
| |
345 |
( "\\\r\n" // MS |
346 |
| "\\\r" // MAC |
347 |
| "\\\n" // Unix |
348 |
) {deferredNewline();} |
349 |
| |
350 |
~('"'|'\r'|'\n'|'\\') |
351 |
)* |
352 |
'"' |
353 |
; |
354 |
|
355 |
protected |
356 |
EndOfLine |
357 |
: |
358 |
( options{generateAmbigWarnings = false;}: |
359 |
"\r\n" // MS |
360 |
| '\r' // Mac |
361 |
| '\n' // Unix |
362 |
) |
363 |
; |
364 |
|
365 |
/* |
366 |
* Handle the various escape sequences. |
367 |
* |
368 |
* Note carefully that these numeric escape *sequences* are *not* of the |
369 |
* same form as the C language numeric *constants*. |
370 |
* |
371 |
* There is no such thing as a binary numeric escape sequence. |
372 |
* |
373 |
* Octal escape sequences are either 1, 2, or 3 octal digits exactly. |
374 |
* |
375 |
* There is no such thing as a decimal escape sequence. |
376 |
* |
377 |
* Hexadecimal escape sequences are begun with a leading \x and continue |
378 |
* until a non-hexadecimal character is found. |
379 |
* |
380 |
* No real handling of tri-graph sequences, yet. |
381 |
*/ |
382 |
|
383 |
protected |
384 |
Escape |
385 |
: |
386 |
'\\' |
387 |
( options{warnWhenFollowAmbig=false;}: |
388 |
'a' |
389 |
| 'b' |
390 |
| 'f' |
391 |
| 'n' |
392 |
| 'r' |
393 |
| 't' |
394 |
| 'v' |
395 |
| '"' |
396 |
| '\'' |
397 |
| '\\' |
398 |
| '?' |
399 |
| ('0'..'3') (options{warnWhenFollowAmbig=false;}: Digit (options{warnWhenFollowAmbig=false;}: Digit)? )? |
400 |
| ('4'..'7') (options{warnWhenFollowAmbig=false;}: Digit)? |
401 |
| 'x' (options{warnWhenFollowAmbig=false;}: Digit | 'a'..'f' | 'A'..'F')+ |
402 |
) |
403 |
; |
404 |
|
405 |
|
406 |
protected |
407 |
Vocabulary |
408 |
: |
409 |
'\3'..'\377' |
410 |
; |
411 |
|
412 |
|
413 |
ID |
414 |
options {testLiterals = true;} |
415 |
: |
416 |
('a'..'z'|'A'..'Z'|'_') |
417 |
('a'..'z'|'A'..'Z'|'_'|'0'..'9')* |
418 |
; |
419 |
|
420 |
|
421 |
protected |
422 |
Digit |
423 |
: |
424 |
'0'..'9' |
425 |
; |
426 |
|
427 |
protected |
428 |
Decimal |
429 |
: |
430 |
('0'..'9')+ |
431 |
; |
432 |
|
433 |
// hexadecimal digit (again, note it's protected!) |
434 |
protected |
435 |
HEX_DIGIT |
436 |
: ('0'..'9'|'A'..'F'|'a'..'f') |
437 |
; |
438 |
|
439 |
|
440 |
// a numeric literal |
441 |
NUM_INT |
442 |
{ |
443 |
bool isDecimal = false; |
444 |
ANTLR_USE_NAMESPACE(antlr)RefToken t = ANTLR_USE_NAMESPACE(antlr)nullToken; |
445 |
} |
446 |
: ('+' | '-')? |
447 |
( |
448 |
'.' {_ttype = DOT;} |
449 |
( ('0'..'9')+ (EXPONENT)? (f1:FLOAT_SUFFIX {t=f1;})? |
450 |
{ |
451 |
if ( t && |
452 |
(t->getText().find('f') != ANTLR_USE_NAMESPACE(std)string::npos || |
453 |
t->getText().find('F') != ANTLR_USE_NAMESPACE(std)string::npos ) ) { |
454 |
_ttype = NUM_FLOAT; |
455 |
} |
456 |
else { |
457 |
_ttype = NUM_DOUBLE; // assume double |
458 |
} |
459 |
} |
460 |
)? |
461 |
|
462 |
| ( '0' {isDecimal = true;} // special case for just '0' |
463 |
( ('x'|'X') |
464 |
( // hex |
465 |
// the 'e'|'E' and float suffix stuff look |
466 |
// like hex digits, hence the (...)+ doesn't |
467 |
// know when to stop: ambig. ANTLR resolves |
468 |
// it correctly by matching immediately. It |
469 |
// is therefor ok to hush warning. |
470 |
options { |
471 |
warnWhenFollowAmbig=false; |
472 |
} |
473 |
: HEX_DIGIT |
474 |
)+ |
475 |
| //float or double with leading zero |
476 |
(('0'..'9')+ ('.'|EXPONENT|FLOAT_SUFFIX)) => ('0'..'9')+ |
477 |
| ('0'..'7')+ // octal |
478 |
)? |
479 |
| ('1'..'9') ('0'..'9')* {isDecimal=true;} // non-zero decimal |
480 |
) |
481 |
( ('l'|'L') { _ttype = NUM_LONG; } |
482 |
|
483 |
// only check to see if it's a float if looks like decimal so far |
484 |
| {isDecimal}? |
485 |
( '.' ('0'..'9')* (EXPONENT)? (f2:FLOAT_SUFFIX {t=f2;})? |
486 |
| EXPONENT (f3:FLOAT_SUFFIX {t=f3;})? |
487 |
| f4:FLOAT_SUFFIX {t=f4;} |
488 |
) |
489 |
{ |
490 |
if ( t && |
491 |
(t->getText().find('f') != ANTLR_USE_NAMESPACE(std)string::npos || |
492 |
t->getText().find('F') != ANTLR_USE_NAMESPACE(std)string::npos ) ) { |
493 |
_ttype = NUM_FLOAT; |
494 |
} |
495 |
else { |
496 |
_ttype = NUM_DOUBLE; // assume double |
497 |
} |
498 |
} |
499 |
)? |
500 |
) |
501 |
; |
502 |
|
503 |
// a couple protected methods to assist in matching floating point numbers |
504 |
protected |
505 |
EXPONENT |
506 |
: ('e'|'E'|'d'|'D') ('+'|'-')? ('0'..'9')+ |
507 |
; |
508 |
|
509 |
protected |
510 |
FLOAT_SUFFIX |
511 |
: 'f'|'F'|'d'|'D' |
512 |
; |