From 52822cdb2964cafce1b6ce7882e72319424bdf0d Mon Sep 17 00:00:00 2001 From: Federico Tomassetti Date: Sun, 21 Feb 2016 17:03:54 +0100 Subject: [PATCH] skil wrapper nodes --- build.gradle | 5 +--- examples/simple.py | 4 +++ .../me/tomassetti/pythonast/AstPrinter.java | 26 ++++++++++++------- .../java/me/tomassetti/pythonast/Example.java | 3 ++- 4 files changed, 24 insertions(+), 14 deletions(-) create mode 100644 examples/simple.py diff --git a/build.gradle b/build.gradle index 00c7430..7d9cbd9 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ buildscript { } dependencies { - classpath 'me.champeau.gradle:antlr4-gradle-plugin:0.1' + classpath 'me.champeau.gradle:antlr4-gradle-plugin:0.1.1-SNAPSHOT' } } @@ -26,13 +26,10 @@ antlr4 { extraArgs = ['-package', 'me.tomassetti.pythonast.parser'] } -// make the Java compile task depend on the antlr4 task compileJava.dependsOn antlr4 -// add the generated source files to the list of java sources sourceSets.main.java.srcDirs += antlr4.output -// add antlr4 to classpath configurations { compile.extendsFrom antlr4 } diff --git a/examples/simple.py b/examples/simple.py new file mode 100644 index 0000000..688d2df --- /dev/null +++ b/examples/simple.py @@ -0,0 +1,4 @@ +def sum(a, b): + return a + b + +print("The sum of %i and %i is %i" % (5, 3, sum(5, 3))) diff --git a/src/main/java/me/tomassetti/pythonast/AstPrinter.java b/src/main/java/me/tomassetti/pythonast/AstPrinter.java index 7c79e01..dbd431c 100644 --- a/src/main/java/me/tomassetti/pythonast/AstPrinter.java +++ b/src/main/java/me/tomassetti/pythonast/AstPrinter.java @@ -1,31 +1,39 @@ package me.tomassetti.pythonast; -import me.tomassetti.pythonast.parser.Python3Lexer; import me.tomassetti.pythonast.parser.Python3Parser; -import org.antlr.v4.runtime.ANTLRInputStream; -import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.RuleContext; import org.antlr.v4.runtime.tree.ParseTree; public class AstPrinter { + private boolean ignoringWrappers = true; + + public void setIgnoringWrappers(boolean ignoringWrappers) { + this.ignoringWrappers = ignoringWrappers; + } + public void print(RuleContext ctx) { explore(ctx, 0); } private void explore(RuleContext ctx, int indentation) { - String ruleName = Python3Parser.ruleNames[ctx.getRuleIndex()]; - for (int i=0;i