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