ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/mdParser/MDParser.g
Revision: 1275
Committed: Fri Jul 4 20:54:29 2008 UTC (16 years, 10 months ago) by cli2
Original Path: trunk/src/mdParser/MDParser.g
File size: 10549 byte(s)
Log Message:
Changes required for Inversions and Base Atom types.  This will
break OOPSE badly for a few days or so...

File Contents

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

Properties

Name Value
svn:executable *