Skip to content

Commit

Permalink
skil wrapper nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Feb 21, 2016
1 parent 40069d8 commit 52822cd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
5 changes: 1 addition & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}

Expand All @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions examples/simple.py
Original file line number Diff line number Diff line change
@@ -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)))
26 changes: 17 additions & 9 deletions src/main/java/me/tomassetti/pythonast/AstPrinter.java
Original file line number Diff line number Diff line change
@@ -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<indentation;i++) {
System.out.print(" ");
boolean toBeIgnored = ignoringWrappers
&& ctx.getChildCount() == 1
&& ctx.getChild(0) instanceof ParserRuleContext;
if (!toBeIgnored) {
String ruleName = Python3Parser.ruleNames[ctx.getRuleIndex()];
for (int i = 0; i < indentation; i++) {
System.out.print(" ");
}
System.out.println(ruleName);
}
System.out.println(ruleName);
for (int i=0;i<ctx.getChildCount();i++) {
ParseTree element = ctx.getChild(i);
if (element instanceof RuleContext) {
explore((RuleContext)element, indentation + 1);
explore((RuleContext)element, indentation + (toBeIgnored ? 0 : 1));
}
}
}


}
3 changes: 2 additions & 1 deletion src/main/java/me/tomassetti/pythonast/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class Example {

public static void main(String[] args) throws IOException {
ParserFacade parserFacade = new ParserFacade();
new AstPrinter().print(parserFacade.parse(new File("examples/render.py")));
AstPrinter astPrinter = new AstPrinter();
astPrinter.print(parserFacade.parse(new File("examples/simple.py")));
}
}

0 comments on commit 52822cd

Please sign in to comment.