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