ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-3.0/src/mdParser/MDParser.g
Revision: 2469
Committed: Fri Dec 2 15:38:03 2005 UTC (18 years, 7 months ago) by tim
File size: 10832 byte(s)
Log Message:
End of the Link --> List
Return of the Oject-Oriented
replace yacc/lex parser with antlr parser

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 ATOM = "atom";
32 BOND = "bond";
33 BEND = "bend";
34 TORSION = "torsion";
35 RIGIDBODY = "rigidBody";
36 CUTOFFGROUP = "cutoffGroup";
37 FRAGMENT = "fragment";
38 MEMBERS = "members";
39 POSITION = "position";
40 ORIENTATION = "orientation";
41 ENDBLOCK;
42 }
43
44 {
45 // Suppport C++-style single-line comments?
46 }
47
48 mdfile : (statement)*
49 ;
50
51 statement : assignment
52 | componentblock
53 | moleculeblock
54 | zconstraintblock
55 ;
56
57 assignment : ID ASSIGNEQUAL^ constant SEMICOLON!
58 ;
59
60 constant : signedNumber
61 | 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 | POSITION^ LPAREN! signedNumberTuple RPAREN! SEMICOLON!
89 | ORIENTATION^ LPAREN! signedNumberTuple RPAREN! SEMICOLON!
90 ;
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 rigidbodyblock : RIGIDBODY^ LBRACKET! intConst RBRACKET! LCURLY!(rigidbodystatement)* RCURLY {#RCURLY->setType(ENDBLOCK);}
115 ;
116
117 rigidbodystatement : assignment
118 | MEMBERS^ LPAREN! inttuple RPAREN! SEMICOLON!
119 ;
120
121 cutoffgroupblock : CUTOFFGROUP^ (LBRACKET! intConst! RBRACKET!)? LCURLY! (cutoffgroupstatement)* RCURLY {#RCURLY->setType(ENDBLOCK);}
122 ;
123
124 cutoffgroupstatement : assignment
125 | MEMBERS^ LPAREN! inttuple RPAREN! SEMICOLON!
126 ;
127
128 fragmentblock : FRAGMENT^ LBRACKET! intConst RBRACKET! LCURLY! (fragmentstatement)* RCURLY {#RCURLY->setType(ENDBLOCK);}
129 ;
130
131 fragmentstatement : assignment
132 ;
133
134
135
136 signedNumberTuple : signedNumber (COMMA! signedNumber)*
137 ;
138
139 inttuple : intConst (COMMA! intConst)*
140 ;
141
142 protected
143 intConst
144 : OCTALINT | DECIMALINT | HEXADECIMALINT
145 ;
146
147 protected
148 signedNumber : (PLUS! | MINUS^)?
149 (intConst | floatConst)
150 ;
151
152 protected
153 floatConst
154 :
155 FLOATONE | FLOATTWO
156 ;
157
158
159
160 class MDLexer extends Lexer;
161
162 options
163 {
164 k = 3;
165 exportVocab = MD;
166 testLiterals = false;
167 }
168
169 tokens {
170 DOT;
171 }
172
173 {
174
175
176 int deferredLineCount;
177 FilenameObserver* observer;
178
179 public:
180 void setObserver(FilenameObserver* osv) {observer = osv;}
181 void initDeferredLineCount() { deferredLineCount = 0;}
182 void deferredNewline() {
183 deferredLineCount++;
184 }
185
186
187 virtual void newline() {
188 for (;deferredLineCount>0;deferredLineCount--) {
189 CharScanner::newline();
190 }
191 CharScanner::newline();
192 }
193
194 }
195
196
197 // Operators:
198
199 ASSIGNEQUAL : '=' ;
200 COLON : ':' ;
201 COMMA : ',' ;
202 QUESTIONMARK : '?' ;
203 SEMICOLON : ';' ;
204
205 LPAREN : '(' ;
206 RPAREN : ')' ;
207 LBRACKET : '[' ;
208 RBRACKET : ']' ;
209 LCURLY : '{' ;
210 RCURLY : '}' ;
211
212 PLUS : '+' ;
213 MINUS : '-' ;
214
215 /*
216 EQUAL : "==" ;
217 NOTEQUAL : "!=" ;
218 LESSTHANOREQUALTO : "<=" ;
219 LESSTHAN : "<" ;
220 GREATERTHANOREQUALTO : ">=" ;
221 GREATERTHAN : ">" ;
222
223 DIVIDE : '/' ;
224 DIVIDEEQUAL : "/=" ;
225 PLUS : '+' ;
226 PLUSEQUAL : "+=" ;
227 PLUSPLUS : "++" ;
228 MINUS : '-' ;
229 MINUSEQUAL : "-=" ;
230 MINUSMINUS : "--" ;
231 STAR : '*' ;
232 TIMESEQUAL : "*=" ;
233 MOD : '%' ;
234 MODEQUAL : "%=" ;
235 SHIFTRIGHT : ">>" ;
236 SHIFTRIGHTEQUAL : ">>=" ;
237 SHIFTLEFT : "<<" ;
238 SHIFTLEFTEQUAL : "<<=" ;
239
240 AND : "&&" ;
241 NOT : '!' ;
242 OR : "||" ;
243
244 AMPERSAND : '&' ;
245 BITWISEANDEQUAL : "&=" ;
246 TILDE : '~' ;
247 BITWISEOR : '|' ;
248 BITWISEOREQUAL : "|=" ;
249 BITWISEXOR : '^' ;
250 BITWISEXOREQUAL : "^=" ;
251 */
252
253
254 Whitespace
255 :
256 ( // whitespace ignored
257 (' ' |'\t' | '\f')
258 | // handle newlines
259 ( '\r' '\n' // MS
260 | '\r' // Mac
261 | '\n' // Unix
262 ) { newline(); }
263 | // handle continuation lines
264 ( '\\' '\r' '\n' // MS
265 | '\\' '\r' // Mac
266 | '\\' '\n' // Unix
267 ) {printf("CPP_parser.g continuation line detected\n");
268 deferredNewline();}
269 )
270 {_ttype = ANTLR_USE_NAMESPACE(antlr)Token::SKIP;}
271 ;
272
273 Comment
274 :
275 "/*"
276 ( {LA(2) != '/'}? '*'
277 | EndOfLine {deferredNewline();}
278 | ~('*'| '\r' | '\n')
279 )*
280 "*/" {_ttype = ANTLR_USE_NAMESPACE(antlr)Token::SKIP;}
281 ;
282
283 CPPComment
284 :
285 "//" (~('\n' | '\r'))* EndOfLine
286 {_ttype = ANTLR_USE_NAMESPACE(antlr)Token::SKIP; newline();}
287 ;
288
289 PREPROC_DIRECTIVE
290 options{paraphrase = "a line directive";}
291 :
292 '#' LineDirective
293 {_ttype = ANTLR_USE_NAMESPACE(antlr)Token::SKIP; newline();}
294 ;
295
296 protected
297 LineDirective
298 :
299 {
300 deferredLineCount = 0;
301 }
302 ("line")? // this would be for if the directive started "#line"
303 (Space)+
304 n:Decimal { setLine(oopse::lexi_cast<int>(n->getText()) - 1); }
305 (Space)+
306 (sl:StringLiteral) {std::string filename = sl->getText().substr(1,sl->getText().length()-2); observer->notify(filename);}
307 ((Space)+ Decimal)* // To support cpp flags (GNU)
308 EndOfLine
309 ;
310
311 protected
312 Space
313 :
314 (' '|'\t'|'\f')
315 ;
316
317
318 // Literals:
319
320 /*
321 * Note that we do NOT handle tri-graphs nor multi-byte sequences.
322 */
323
324 /*
325 * Note that we can't have empty character constants (even though we
326 * can have empty strings :-).
327 */
328 CharLiteral
329 :
330 '\'' (Escape | ~('\'')) '\''
331 ;
332
333 /*
334 * Can't have raw imbedded newlines in string constants. Strict reading of
335 * the standard gives odd dichotomy between newlines & carriage returns.
336 * Go figure.
337 */
338 StringLiteral
339 :
340 '"'
341 ( Escape
342 |
343 ( "\\\r\n" // MS
344 | "\\\r" // MAC
345 | "\\\n" // Unix
346 ) {deferredNewline();}
347 |
348 ~('"'|'\r'|'\n'|'\\')
349 )*
350 '"'
351 ;
352
353 protected
354 EndOfLine
355 :
356 ( options{generateAmbigWarnings = false;}:
357 "\r\n" // MS
358 | '\r' // Mac
359 | '\n' // Unix
360 )
361 ;
362
363 /*
364 * Handle the various escape sequences.
365 *
366 * Note carefully that these numeric escape *sequences* are *not* of the
367 * same form as the C language numeric *constants*.
368 *
369 * There is no such thing as a binary numeric escape sequence.
370 *
371 * Octal escape sequences are either 1, 2, or 3 octal digits exactly.
372 *
373 * There is no such thing as a decimal escape sequence.
374 *
375 * Hexadecimal escape sequences are begun with a leading \x and continue
376 * until a non-hexadecimal character is found.
377 *
378 * No real handling of tri-graph sequences, yet.
379 */
380
381 protected
382 Escape
383 :
384 '\\'
385 ( options{warnWhenFollowAmbig=false;}:
386 'a'
387 | 'b'
388 | 'f'
389 | 'n'
390 | 'r'
391 | 't'
392 | 'v'
393 | '"'
394 | '\''
395 | '\\'
396 | '?'
397 | ('0'..'3') (options{warnWhenFollowAmbig=false;}: Digit (options{warnWhenFollowAmbig=false;}: Digit)? )?
398 | ('4'..'7') (options{warnWhenFollowAmbig=false;}: Digit)?
399 | 'x' (options{warnWhenFollowAmbig=false;}: Digit | 'a'..'f' | 'A'..'F')+
400 )
401 ;
402
403 // Numeric Constants:
404
405 protected
406 Digit
407 :
408 '0'..'9'
409 ;
410
411 protected
412 Decimal
413 :
414 ('0'..'9')+
415 ;
416
417 protected
418 LongSuffix
419 : 'l'
420 | 'L'
421 ;
422
423 protected
424 UnsignedSuffix
425 : 'u'
426 | 'U'
427 ;
428
429 protected
430 FloatSuffix
431 : 'f'
432 | 'F'
433 ;
434
435 protected
436 Exponent
437 :
438 ('e'|'E'|'d'|'D') ('+'|'-')? (Digit)+
439 ;
440
441 protected
442 Vocabulary
443 :
444 '\3'..'\377'
445 ;
446
447 Number
448 :
449 ( (Digit)+ ('.' | 'e' | 'E' | 'd' | 'D' ) )=>
450 (Digit)+
451 ( '.' (Digit)* (Exponent)? {_ttype = FLOATONE;} //Zuo 3/12/01
452 | Exponent {_ttype = FLOATTWO;} //Zuo 3/12/01
453 ) //{_ttype = DoubleDoubleConst;}
454 (FloatSuffix //{_ttype = FloatDoubleConst;}
455 |LongSuffix //{_ttype = LongDoubleConst;}
456 )?
457 |
458 '.' {_ttype = DOT;}
459 ( (Digit)+ (Exponent)? {_ttype = FLOATONE;} //Zuo 3/12/01
460 //{_ttype = DoubleDoubleConst;}
461 (FloatSuffix //{_ttype = FloatDoubleConst;}
462 |LongSuffix //{_ttype = LongDoubleConst;}
463 )?
464 )?
465 |
466 '0' ('0'..'7')* //{_ttype = IntOctalConst;}
467 (LongSuffix //{_ttype = LongOctalConst;}
468 |UnsignedSuffix //{_ttype = UnsignedOctalConst;}
469 )* {_ttype = OCTALINT;}
470 |
471 '1'..'9' (Digit)* //{_ttype = IntIntConst;}
472 (LongSuffix //{_ttype = LongIntConst;}
473 |UnsignedSuffix //{_ttype = UnsignedIntConst;}
474 )* {_ttype = DECIMALINT;}
475 |
476 '0' ('x' | 'X') ('a'..'f' | 'A'..'F' | Digit)+
477 //{_ttype = IntHexConst;}
478 (LongSuffix //{_ttype = LongHexConst;}
479 |UnsignedSuffix //{_ttype = UnsignedHexConst;}
480 )* {_ttype = HEXADECIMALINT;}
481 ;
482
483 ID
484 options {testLiterals = true;}
485 :
486 ('a'..'z'|'A'..'Z'|'_')
487 ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*
488 ;

Properties

Name Value
svn:executable *