Search in sources :

Example 1 with ParseTreeVisitor

use of org.antlr.v4.runtime.tree.ParseTreeVisitor in project Alpha by alpha-asp.

the class ProgramParserImpl method parse.

public ASPCore2Program parse(CharStream stream, Map<String, PredicateInterpretation> externals) {
    // @formatter:off
    /*
		 * // In order to require less memory: use unbuffered streams and avoid constructing a full parse tree. 
		 * ASPCore2Lexer lexer = new ASPCore2Lexer(new UnbufferedCharStream(is)); 
		 * lexer.setTokenFactory(new CommonTokenFactory(true)); 
		 * final ASPCore2Parser parser = new ASPCore2Parser(new UnbufferedTokenStream<>(lexer)); 
		 * parser.setBuildParseTree(false);
		 */
    // @formatter:on
    CommonTokenStream tokens = new CommonTokenStream(new ASPCore2Lexer(stream));
    final ASPCore2Parser parser = new ASPCore2Parser(tokens);
    // Try SLL parsing mode (faster but may terminate incorrectly).
    parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    parser.removeErrorListeners();
    parser.setErrorHandler(new BailErrorStrategy());
    final CustomErrorListener errorListener = new CustomErrorListener(stream.getSourceName());
    ASPCore2Parser.ProgramContext programContext;
    try {
        // Parse program
        programContext = parser.program();
    } catch (ParseCancellationException e) {
        // retry with LL parser and DefaultErrorStrategy printing errors to console.
        if (e.getCause() instanceof RecognitionException) {
            tokens.seek(0);
            parser.addErrorListener(errorListener);
            parser.setErrorHandler(new DefaultErrorStrategy());
            parser.getInterpreter().setPredictionMode(PredictionMode.LL);
            // Re-run parse.
            programContext = parser.program();
        } else {
            throw e;
        }
    }
    // is attempted) and the user will only see the first error encountered.
    if (errorListener.getRecognitionException() != null) {
        throw errorListener.getRecognitionException();
    }
    // Abort parsing if there were some (recoverable) syntax errors.
    if (parser.getNumberOfSyntaxErrors() != 0) {
        throw new ParseCancellationException();
    }
    // The union of this parser's preloaded externals and the (program-specific) externals passed to the parse method
    Map<String, PredicateInterpretation> knownExternals;
    if (externals != null && !externals.isEmpty()) {
        knownExternals = new HashMap<>(preloadedExternals);
        knownExternals.putAll(externals);
    } else {
        knownExternals = preloadedExternals;
    }
    // Construct internal program representation.
    ParseTreeVisitor visitor = new ParseTreeVisitor(knownExternals);
    return visitor.translate(programContext);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) PredicateInterpretation(at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation) ASPCore2Parser(at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Parser) ASPCore2Lexer(at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Lexer) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) DefaultErrorStrategy(org.antlr.v4.runtime.DefaultErrorStrategy) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 2 with ParseTreeVisitor

use of org.antlr.v4.runtime.tree.ParseTreeVisitor in project bmoth by hhu-stups.

the class FormulaVisitorExceptionsTest method init.

@Before
public void init() {
    visitor = new FormulaVisitor<Object, Object>() {

        @Override
        public Object visitEnumerationSetNode(EnumerationSetNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitDeferredSetNode(DeferredSetNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitEnumeratedSetElementNode(EnumeratedSetElementNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitExprOperatorNode(ExpressionOperatorNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitIdentifierExprNode(IdentifierExprNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitCastPredicateExpressionNode(CastPredicateExpressionNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitNumberNode(NumberNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitQuantifiedExpressionNode(QuantifiedExpressionNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitSetComprehensionNode(SetComprehensionNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitIdentifierPredicateNode(IdentifierPredicateNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitPredicateOperatorNode(PredicateOperatorNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitPredicateOperatorWithExprArgs(PredicateOperatorWithExprArgsNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitQuantifiedPredicateNode(QuantifiedPredicateNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitLTLPrefixOperatorNode(LTLPrefixOperatorNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitLTLKeywordNode(LTLKeywordNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitLTLInfixOperatorNode(LTLInfixOperatorNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitLTLBPredicateNode(LTLBPredicateNode node, Object expected) {
            return null;
        }
    };
    terminalNode = new TerminalNode() {

        @Override
        public Token getSymbol() {
            return null;
        }

        @Override
        public ParseTree getParent() {
            return null;
        }

        @Override
        public ParseTree getChild(int i) {
            return null;
        }

        @Override
        public <T> T accept(ParseTreeVisitor<? extends T> parseTreeVisitor) {
            return null;
        }

        @Override
        public String getText() {
            return null;
        }

        @Override
        public String toStringTree(Parser parser) {
            return null;
        }

        @Override
        public Interval getSourceInterval() {
            return null;
        }

        @Override
        public Object getPayload() {
            return null;
        }

        @Override
        public int getChildCount() {
            return 0;
        }

        @Override
        public String toStringTree() {
            return null;
        }

        @Override
        public void setParent(RuleContext parent) {
        // not needed in test stub
        }
    };
}
Also used : RuleContext(org.antlr.v4.runtime.RuleContext) Token(org.antlr.v4.runtime.Token) LTLPrefixOperatorNode(de.bmoth.parser.ast.nodes.ltl.LTLPrefixOperatorNode) LTLBPredicateNode(de.bmoth.parser.ast.nodes.ltl.LTLBPredicateNode) LTLInfixOperatorNode(de.bmoth.parser.ast.nodes.ltl.LTLInfixOperatorNode) Parser(org.antlr.v4.runtime.Parser) LTLKeywordNode(de.bmoth.parser.ast.nodes.ltl.LTLKeywordNode) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree) Interval(org.antlr.v4.runtime.misc.Interval) Before(org.junit.Before)

Example 3 with ParseTreeVisitor

use of org.antlr.v4.runtime.tree.ParseTreeVisitor in project bmoth by hhu-stups.

the class SubstitutionVisitorExceptionsTest method init.

@Before
public void init() {
    visitor = new SubstitutionVisitor<Object, Object>() {

        @Override
        public Object visitSkipSubstitutionNode(SkipSubstitutionNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitIfSubstitutionNode(IfSubstitutionNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitConditionSubstitutionNode(ConditionSubstitutionNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitAnySubstitution(AnySubstitutionNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitSelectSubstitutionNode(SelectSubstitutionNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitSingleAssignSubstitution(SingleAssignSubstitutionNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitParallelSubstitutionNode(ParallelSubstitutionNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitBecomesElementOfSubstitutionNode(BecomesElementOfSubstitutionNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitBecomesSuchThatSubstitutionNode(BecomesSuchThatSubstitutionNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitLTLPrefixOperatorNode(LTLPrefixOperatorNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitLTLKeywordNode(LTLKeywordNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitLTLInfixOperatorNode(LTLInfixOperatorNode node, Object expected) {
            return null;
        }

        @Override
        public Object visitLTLBPredicateNode(LTLBPredicateNode node, Object expected) {
            return null;
        }
    };
    terminalNode = new TerminalNode() {

        @Override
        public Token getSymbol() {
            return new Token() {

                @Override
                public String getText() {
                    return "adsf";
                }

                @Override
                public int getType() {
                    return 0;
                }

                @Override
                public int getLine() {
                    return 0;
                }

                @Override
                public int getCharPositionInLine() {
                    return 0;
                }

                @Override
                public int getChannel() {
                    return 0;
                }

                @Override
                public int getTokenIndex() {
                    return 0;
                }

                @Override
                public int getStartIndex() {
                    return 0;
                }

                @Override
                public int getStopIndex() {
                    return 0;
                }

                @Override
                public TokenSource getTokenSource() {
                    return null;
                }

                @Override
                public CharStream getInputStream() {
                    return null;
                }
            };
        }

        @Override
        public ParseTree getParent() {
            return null;
        }

        @Override
        public ParseTree getChild(int i) {
            return null;
        }

        @Override
        public <T> T accept(ParseTreeVisitor<? extends T> parseTreeVisitor) {
            return null;
        }

        @Override
        public String getText() {
            return null;
        }

        @Override
        public String toStringTree(Parser parser) {
            return null;
        }

        @Override
        public Interval getSourceInterval() {
            return null;
        }

        @Override
        public Object getPayload() {
            return null;
        }

        @Override
        public int getChildCount() {
            return 0;
        }

        @Override
        public String toStringTree() {
            return null;
        }

        @Override
        public void setParent(RuleContext parent) {
        // not needed in test stub
        }
    };
}
Also used : LTLPrefixOperatorNode(de.bmoth.parser.ast.nodes.ltl.LTLPrefixOperatorNode) LTLBPredicateNode(de.bmoth.parser.ast.nodes.ltl.LTLBPredicateNode) LTLInfixOperatorNode(de.bmoth.parser.ast.nodes.ltl.LTLInfixOperatorNode) BMoThParser(de.bmoth.antlr.BMoThParser) LTLKeywordNode(de.bmoth.parser.ast.nodes.ltl.LTLKeywordNode) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree) Interval(org.antlr.v4.runtime.misc.Interval) Before(org.junit.Before)

Example 4 with ParseTreeVisitor

use of org.antlr.v4.runtime.tree.ParseTreeVisitor in project antlr4 by antlr.

the class TestVisitors method testShouldNotVisitEOF.

/**
 * This test verifies that {@link AbstractParseTreeVisitor#visitChildren} does not call
 * {@link ParseTreeVisitor#visit} after {@link AbstractParseTreeVisitor#shouldVisitNextChild} returns
 * {@code false}.
 */
@Test
public void testShouldNotVisitEOF() {
    String input = "A";
    VisitorBasicLexer lexer = new VisitorBasicLexer(new ANTLRInputStream(input));
    VisitorBasicParser parser = new VisitorBasicParser(new CommonTokenStream(lexer));
    VisitorBasicParser.SContext context = parser.s();
    Assert.assertEquals("(s A <EOF>)", context.toStringTree(parser));
    VisitorBasicVisitor<String> listener = new VisitorBasicBaseVisitor<String>() {

        @Override
        public String visitTerminal(TerminalNode node) {
            return node.getSymbol().toString() + "\n";
        }

        @Override
        protected boolean shouldVisitNextChild(RuleNode node, String currentResult) {
            return currentResult == null || currentResult.isEmpty();
        }
    };
    String result = listener.visit(context);
    String expected = "[@0,0:0='A',<1>,1:0]\n";
    Assert.assertEquals(expected, result);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) RuleNode(org.antlr.v4.runtime.tree.RuleNode) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) Test(org.junit.Test)

Aggregations

TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)3 LTLBPredicateNode (de.bmoth.parser.ast.nodes.ltl.LTLBPredicateNode)2 LTLInfixOperatorNode (de.bmoth.parser.ast.nodes.ltl.LTLInfixOperatorNode)2 LTLKeywordNode (de.bmoth.parser.ast.nodes.ltl.LTLKeywordNode)2 LTLPrefixOperatorNode (de.bmoth.parser.ast.nodes.ltl.LTLPrefixOperatorNode)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 Interval (org.antlr.v4.runtime.misc.Interval)2 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2 Before (org.junit.Before)2 PredicateInterpretation (at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation)1 ASPCore2Lexer (at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Lexer)1 ASPCore2Parser (at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Parser)1 BMoThParser (de.bmoth.antlr.BMoThParser)1 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)1 BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)1 DefaultErrorStrategy (org.antlr.v4.runtime.DefaultErrorStrategy)1 Parser (org.antlr.v4.runtime.Parser)1 RecognitionException (org.antlr.v4.runtime.RecognitionException)1 RuleContext (org.antlr.v4.runtime.RuleContext)1 Token (org.antlr.v4.runtime.Token)1