From 9ae5049d44f3019a30ad35c65522671b7fd244a4 Mon Sep 17 00:00:00 2001 From: parrt Date: Sat, 21 Jan 2012 15:15:01 -0800 Subject: [PATCH] play with tests [git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9879] --- tool/playground/E.g | 35 +++++-------------------- tool/playground/TestE.java | 52 +++++--------------------------------- 2 files changed, 14 insertions(+), 73 deletions(-) diff --git a/tool/playground/E.g b/tool/playground/E.g index 7a0663faa..12eb5a68f 100644 --- a/tool/playground/E.g +++ b/tool/playground/E.g @@ -1,29 +1,8 @@ -grammar E; - -prog: classDef+ ; // match one or more class definitions - -classDef - : 'class' ID '{' member+ '}' // a class has one or more members - {System.out.println("class "+$ID.text);} +lexer grammar E; +DUH : 'eee' {int y=1;} + | 'fff' {int z=3;} ; - -member - : 'int' ID ';' // field definition - {System.out.println("var "+$ID.text);} - | 'int' f=ID '(' ID ')' '{' stat '}' // method definition - {System.out.println("method: "+$f.text);} - ; - -stat: expr ';' - {System.out.println("found expr: "+$stat.text);} - | ID '=' expr ';' - {System.out.println("found assign: "+$stat.text);} - ; - -expr: INT - | ID '(' INT ')' - ; - -ID : ('a'..'z'|'A'..'Z')+ ; -INT : '0'..'9'+ ; -WS : (' '|'\t'|'\n'|'\r')+ {$channel=HIDDEN;} ; +I : '0'..'9'+ {System.out.println("I");} + | 'z' {int x = 2;} + ; +WS : (' '|'\n') -> type(WS) ; diff --git a/tool/playground/TestE.java b/tool/playground/TestE.java index cf278656d..02791e0c7 100644 --- a/tool/playground/TestE.java +++ b/tool/playground/TestE.java @@ -1,49 +1,11 @@ -/* - [The "BSD license"] - Copyright (c) 2011 Terence Parr - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. The name of the author may not be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - import org.antlr.v4.runtime.*; public class TestE { - public static void main(String[] args) throws Exception { -// START:rig - CharStream input = new ANTLRFileStream(args[0]); - ELexer lexer = new ELexer(input); - CommonTokenStream tokens = new CommonTokenStream(lexer); - EParser parser = new EParser(tokens); - parser.setBuildParseTree(true); - ParserRuleContext ctx = parser.prog(); - System.out.println(ctx.toStringTree(parser)); - // Either view in dialog box: - //ctx.inspect(parser); - // Or in postscript - ctx.save(parser, "/tmp/t.ps"); -// System.out.println(Trees.getPS(ctx, parser, "Helvetica", 10)); -// END:rig - } + public static void main(String[] args) throws Exception { + CharStream input = new ANTLRFileStream(args[0]); + E lex = new E(input); + CommonTokenStream tokens = new CommonTokenStream(lex); + tokens.fill(); + for (Object t : tokens.getTokens()) System.out.println(t); + } }