aboutsummaryrefslogtreecommitdiff
path: root/antlr-3.4/runtime/JavaScript/tests/functional/t046rewrite.g
blob: 71aab82a279139f793dcccf3caafdb9038f9f332 (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
55
56
grammar t046rewrite;
options {
    language=JavaScript;
}

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

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

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

stat:   ID '=' expr ';' {$body::decls[$ID.text] = true;}
    ;

expr:   mul ('+' mul)* 
    ;

mul :   atom ('*' atom)*
    ;

atom:   ID
    |   INT
    ;

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

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

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