ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/mdParser/MDParser.g
Revision: 1465
Committed: Fri Jul 9 23:08:25 2010 UTC (14 years, 10 months ago) by chuckv
File size: 10750 byte(s)
Log Message:
Creating busticated version of OpenMD

File Contents

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

Properties

Name Value
svn:executable *
svn:keywords Author Id Revision Date