-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40069d8
commit 52822cd
Showing
4 changed files
with
24 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters