1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
// This file is part of PyANTLR. See LICENSE.txt for license
// details..........Copyright (C) Wolfgang Haefelinger, 2004.
//
// $Id$
header {
import keepws
}
options {
language=Python;
}
class keepws_p extends Parser;
options {
buildAST = true;
k=2;
}
tokens {
CALL; // define imaginary token CALL
}
slist
: ( stat )+
;
stat: LBRACE^ (stat)+ RBRACE
| "if"^ expr "then" stat ("else" stat)?
| ID ASSIGN^ expr SEMI
| call
;
expr
: mexpr (PLUS^ mexpr)*
;
mexpr
: atom (STAR^ atom)*
;
atom: INT
| ID
;
call: ID LPAREN (expr)? RPAREN SEMI
{#call = #(#[CALL,"CALL"], #call);}
;
class keepws_l extends Lexer;
options {
charVocabulary = '\3'..'\377';
}
WS : (' '
| '\t'
| ('\n'|'\r'('\n')?) {$newline;}
)+
;
// Single-line comments
SL_COMMENT
: "//"
(~('\n'|'\r'))* ('\n'|'\r'('\n')?)
{$newline;}
;
LBRACE: '{'
;
RBRACE: '}'
;
LPAREN: '('
;
RPAREN: ')'
;
STAR: '*'
;
PLUS: '+'
;
SEMI: ';'
;
ASSIGN
: '='
;
protected
DIGIT
: '0'..'9'
;
INT : (DIGIT)+
;
ID : ('a'..'z')+
;
class keepws_w extends TreeParser;
slist
: {keepws.dumpHidden(keepws.getstream().getInitialHiddenToken());}
(stat)+
;
stat: #(LBRACE {keepws.pr(#LBRACE);} (stat)+ RBRACE {keepws.pr(#RBRACE);})
| #(i:"if" {keepws.pr(i);} expr t:"then" {keepws.pr(t);} stat (e:"else" {keepws.pr(e);} stat)?)
| #(ASSIGN ID {keepws.pr(#ID); keepws.pr(#ASSIGN);} expr SEMI {keepws.pr(#SEMI);} )
| call
;
expr
: #(PLUS expr {keepws.pr(#PLUS);} expr)
| #(STAR expr {keepws.pr(#STAR);} expr)
| INT {keepws.pr(#INT);}
| ID {keepws.pr(#ID);}
;
call: {
self.callDumpInstrumentation(#call);
}
#(CALL ID {keepws.pr(#ID);}
LPAREN {keepws.pr(#LPAREN);} (expr)? RPAREN {keepws.pr(#RPAREN);}
SEMI
{
keepws.write(#SEMI.getText())
keepws.write("}")
keepws.dumpHidden(#SEMI.getHiddenAfter())
}
)
;
/** Dump instrumentation for a call statement.
* The reference to rule expr prints out the arg
* and then at the end of this rule, we close the
* generated called to dbg.invoke().
*/
callDumpInstrumentation
: #(CALL id:ID
{keepws.write("{dbg.invoke(\""+id.getText()+"\", \"");}
LPAREN (e:expr)? RPAREN SEMI
{keepws.write("\"); ");}
)
;
|