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