aboutsummaryrefslogtreecommitdiff
path: root/antlr-3.4/runtime/Python/tests/t046rewrite.g
blob: e8dc1dcc9e309869a87aa3ee96bc86422ec2eca6 (plain)
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
grammar t046rewrite;
options {
    language=Python;
}

program
@init {
    start = self.input.LT(1)
}
    :   method+
        {
        self.input.insertBefore(start,"public class Wrapper {\n")
        self.input.insertAfter($method.stop, "\n}\n")
        }
    ;

method
    :   m='method' ID '(' ')' body
        {self.input.replace($m, "public void");}
    ; 

body
scope {
    decls
}
@init {
    $body::decls = set()
}
    :   lcurly='{' stat* '}'
        {
        for it in $body::decls:
            self.input.insertAfter($lcurly, "\nint "+it+";")
        }
    ;

stat:   ID '=' expr ';' {$body::decls.add($ID.text);}
    ;

expr:   mul ('+' mul)* 
    ;

mul :   atom ('*' atom)*
    ;

atom:   ID
    |   INT
    ;

ID  :   ('a'..'z'|'A'..'Z')+ ;

INT :   ('0'..'9')+ ;

WS  :   (' '|'\t'|'\n')+ {$channel=HIDDEN;}
    ;