Skip to content

Commit

Permalink
fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
hvub committed Oct 17, 2022
1 parent 9bb8599 commit 5d99fb3
Showing 1 changed file with 40 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,37 +67,41 @@ public class AntlrParserDemo

public static void main( String[] args )
{
System.out.println("== With tree listener, grammar from XML ==");
new AntlrParserDemo( true, true, QUERY, INDENT);
System.out.println("== With tree listener, grammar from g4 ==");
new AntlrParserDemo( true, false, QUERY, INDENT);
System.out.println("== Manual tree walking, grammar from XML ==");
new AntlrParserDemo( false, true, QUERY, INDENT);
System.out.println("== Manual tree walking, grammar from g4 ==");
new AntlrParserDemo( false, false, QUERY, INDENT);
System.out.println( "== With tree listener, grammar from XML ==" );
new AntlrParserDemo( true, true, QUERY, INDENT );
System.out.println( "== With tree listener, grammar from g4 ==" );
new AntlrParserDemo( true, false, QUERY, INDENT );
System.out.println( "== Manual tree walking, grammar from XML ==" );
new AntlrParserDemo( false, true, QUERY, INDENT );
System.out.println( "== Manual tree walking, grammar from g4 ==" );
new AntlrParserDemo( false, false, QUERY, INDENT );
}

public AntlrParserDemo( boolean useTreeListener, boolean createGrammarFromXML, String query, String indentStep)
public AntlrParserDemo( boolean useTreeListener, boolean createGrammarFromXML, String query, String indentStep )
{
org.antlr.v4.tool.Grammar grammar;
if(createGrammarFromXML) {
if ( createGrammarFromXML )
{
grammar = createGrammarFromXML( GRAMMAR_XML );
} else {
}
else
{
grammar = readGrammarFromG4( GRAMMAR_G4 );
}
if(grammar != null) {
if ( grammar != null )
{
LexerInterpreter lexer = grammar.createLexerInterpreter( CharStreams.fromString( query ) );
ParserInterpreter parser = grammar.createParserInterpreter( new CommonTokenStream( lexer ) );
lexer.removeErrorListeners();
parser.removeErrorListeners();
lexer.addErrorListener( new FailingErrorListener( query ) );
parser.addErrorListener( new FailingErrorListener( query ) );
if( useTreeListener )
if ( useTreeListener )
{
parser.addParseListener( new MyParseTreeListener(grammar, INDENT) );
parser.addParseListener( new MyParseTreeListener( grammar, INDENT ) );
}
ParseTree tree = parser.parse( grammar.getRule( "oC_Cypher" ).index );
if( !useTreeListener )
if ( !useTreeListener )
{
print( grammar, tree, INDENT, "" );
}
Expand All @@ -107,12 +111,14 @@ public AntlrParserDemo( boolean useTreeListener, boolean createGrammarFromXML, S
/*
* Using a ParseTreeListener to consume the parse tree
*/
private class MyParseTreeListener implements ParseTreeListener {
private class MyParseTreeListener implements ParseTreeListener
{
private String indent = "";
private final String indentStep;
private final org.antlr.v4.tool.Grammar grammar;

MyParseTreeListener(org.antlr.v4.tool.Grammar grammar, String indentStep) {
MyParseTreeListener( org.antlr.v4.tool.Grammar grammar, String indentStep )
{
this.grammar = grammar;
this.indentStep = indentStep;
}
Expand All @@ -134,7 +140,7 @@ public void enterEveryRule( ParserRuleContext ctx )
{
String ruleName = grammar.getRule( ctx.getRuleIndex() ).name;
indent = indent + indentStep;
System.out.println( indent + ruleName);
System.out.println( indent + ruleName );
//System.out.println("Entre:" + ctx.getText());
}

Expand All @@ -148,19 +154,21 @@ public void exitEveryRule( ParserRuleContext ctx )
/*
* Walking the parse tree manually
*/
private void print(org.antlr.v4.tool.Grammar grammar, ParseTree tree, String indentStep, String indent) {
if(tree != null) {
private void print( org.antlr.v4.tool.Grammar grammar, ParseTree tree, String indentStep, String indent )
{
if ( tree != null )
{
Object payload = tree.getPayload();
if( payload instanceof InterpreterRuleContext )
if ( payload instanceof InterpreterRuleContext )
{
String ruleName = grammar.getRule( ((InterpreterRuleContext) payload).getRuleIndex() ).name;
System.out.println( indent + ruleName);
System.out.println( indent + ruleName );
}
else
{
System.out.println( indent + "\"" + tree.getText() + "\"" );
}
for( int i = 0; i <= tree.getChildCount(); i++ )
for ( int i = 0; i <= tree.getChildCount(); i++ )
{
print( grammar, tree.getChild( i ), indentStep, indent + indentStep );
}
Expand All @@ -176,14 +184,14 @@ private Grammar readGrammarFromG4( String filename )
try
{
File g4File = new File( filename );
if(g4File.exists())
if ( g4File.exists() )
{
grammarString = new String( Files.readAllBytes( g4File.toPath() ) );
return parseGrammarFromString( grammarString );
return parseGrammarFromString( grammarString );
}
else
{
System.out.println(filename + " not found.");
System.out.println( filename + " not found." );
return null;
}
}
Expand Down Expand Up @@ -211,10 +219,11 @@ private Grammar createGrammarFromXML( String resource, org.opencypher.grammar.Gr
{
fail( "Unexpected error while writing antlr grammar: " + t.getMessage() );
}
return parseGrammarFromString( grammarString );
return parseGrammarFromString( grammarString );
}

private Grammar parseGrammarFromString( String grammarString ) {
private Grammar parseGrammarFromString( String grammarString )
{
org.antlr.v4.Tool tool = new org.antlr.v4.Tool();
GrammarRootAST ast = tool.parseGrammarFromString( grammarString );
Grammar grammar = tool.createGrammar( ast );
Expand Down Expand Up @@ -271,7 +280,8 @@ public void reportContextSensitivity( Parser parser, DFA dfa, int i, int i1, int
}
}

private void fail(String msg) {
throw new RuntimeException(msg);
private void fail( String msg )
{
throw new RuntimeException( msg );
}
}

0 comments on commit 5d99fb3

Please sign in to comment.