aboutsummaryrefslogtreecommitdiff
path: root/antlr-3.4/runtime/Python/tests/t046rewrite.g
diff options
context:
space:
mode:
Diffstat (limited to 'antlr-3.4/runtime/Python/tests/t046rewrite.g')
-rw-r--r--antlr-3.4/runtime/Python/tests/t046rewrite.g54
1 files changed, 54 insertions, 0 deletions
diff --git a/antlr-3.4/runtime/Python/tests/t046rewrite.g b/antlr-3.4/runtime/Python/tests/t046rewrite.g
new file mode 100644
index 0000000..e8dc1dc
--- /dev/null
+++ b/antlr-3.4/runtime/Python/tests/t046rewrite.g
@@ -0,0 +1,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;}
+ ;