Search in sources :

Example 11 with RuleContext

use of org.antlr.v4.runtime.RuleContext in project kalang by kasonyang.

the class CompilantUnitTest method test.

@Test
public void test() {
    KalangSource source = new KalangSource("Test", "class{" + "void main(){" + "}" + "}", "Test.kl");
    CompilationUnit cu = new CompilationUnit(source, new DefaultCompileContext());
    cu.compile(CompilePhase.PHASE_PARSING);
    AstBuilder astBuilder = cu.getAstBuilder();
    ParseTreeNavigator treeNav = new ParseTreeNavigator(astBuilder.getParseTree());
    ParseTree tree = treeNav.getParseTree(0);
    assertNotNull(tree);
    ParseTree treeMd = treeNav.getParseTree(2);
    ParseTree treeMdEnd = treeNav.getParseTree(7);
    // System.out.println(treeMd);
    // System.out.println(treeMdEnd);
    assertNotNull(treeMd);
    assertNotNull(treeMdEnd);
// assertEquals(treeMd, treeMdEnd.getParent().getParent());
// AstNode ast = sp.getAstNode(tree);
// assertNotNull(ast);
// System.out.println("ast:"+ast);
// RuleContext treeOfAst = sp.getParseTree(ast);
// assertNotNull(treeOfAst);
}
Also used : CompilationUnit(kalang.compiler.CompilationUnit) AstBuilder(kalang.compiler.AstBuilder) DefaultCompileContext(kalang.compiler.DefaultCompileContext) KalangSource(kalang.compiler.KalangSource) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeNavigator(kalang.util.ParseTreeNavigator) Test(org.junit.Test)

Example 12 with RuleContext

use of org.antlr.v4.runtime.RuleContext 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 13 with RuleContext

use of org.antlr.v4.runtime.RuleContext 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 14 with RuleContext

use of org.antlr.v4.runtime.RuleContext in project antlr4 by tunnelvisionlabs.

the class ParseTreeWalker method enterRule.

/**
 * The discovery of a rule node, involves sending two events: the generic
 * {@link ParseTreeListener#enterEveryRule} and a
 * {@link RuleContext}-specific event. First we trigger the generic and then
 * the rule specific. We to them in reverse order upon finishing the node.
 */
protected void enterRule(ParseTreeListener listener, RuleNode r) {
    ParserRuleContext ctx = (ParserRuleContext) r.getRuleContext();
    listener.enterEveryRule(ctx);
    ctx.enterRule(listener);
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext)

Example 15 with RuleContext

use of org.antlr.v4.runtime.RuleContext in project presto by prestodb.

the class ErrorHandler method syntaxError.

@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String message, RecognitionException e) {
    try {
        Parser parser = (Parser) recognizer;
        ATN atn = parser.getATN();
        ATNState currentState;
        Token currentToken;
        RuleContext context;
        if (e != null) {
            currentState = atn.states.get(e.getOffendingState());
            currentToken = e.getOffendingToken();
            context = e.getCtx();
            if (e instanceof NoViableAltException) {
                currentToken = ((NoViableAltException) e).getStartToken();
            }
        } else {
            currentState = atn.states.get(parser.getState());
            currentToken = parser.getCurrentToken();
            context = parser.getContext();
        }
        Analyzer analyzer = new Analyzer(atn, parser.getVocabulary(), specialRules, specialTokens, ignoredRules, parser.getTokenStream());
        Multimap<Integer, String> candidates = analyzer.process(currentState, currentToken.getTokenIndex(), context);
        // pick the candidate tokens associated largest token index processed (i.e., the path that consumed the most input)
        String expected = candidates.asMap().entrySet().stream().max(Comparator.comparing(Map.Entry::getKey)).get().getValue().stream().sorted().collect(Collectors.joining(", "));
        message = String.format("mismatched input '%s'. Expecting: %s", ((Token) offendingSymbol).getText(), expected);
    } catch (Exception exception) {
        LOG.error(exception, "Unexpected failure when handling parsing error. This is likely a bug in the implementation");
    }
    throw new ParsingException(message, e, line, charPositionInLine);
}
Also used : RuleContext(org.antlr.v4.runtime.RuleContext) Token(org.antlr.v4.runtime.Token) NoViableAltException(org.antlr.v4.runtime.NoViableAltException) RecognitionException(org.antlr.v4.runtime.RecognitionException) Parser(org.antlr.v4.runtime.Parser) ATNState(org.antlr.v4.runtime.atn.ATNState) NoViableAltException(org.antlr.v4.runtime.NoViableAltException) ATN(org.antlr.v4.runtime.atn.ATN)

Aggregations

RuleContext (org.antlr.v4.runtime.RuleContext)10 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)7 Parser (org.antlr.v4.runtime.Parser)6 Token (org.antlr.v4.runtime.Token)6 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)6 ParseTree (org.antlr.v4.runtime.tree.ParseTree)6 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)4 RecognitionException (org.antlr.v4.runtime.RecognitionException)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 ArrayList (java.util.ArrayList)2 KalangParser (kalang.antlr.KalangParser)2 AstBuilder (kalang.compiler.AstBuilder)2 DispatchMethod (org.antlr.v4.codegen.model.DispatchMethod)2 ListenerDispatchMethod (org.antlr.v4.codegen.model.ListenerDispatchMethod)2 VisitorDispatchMethod (org.antlr.v4.codegen.model.VisitorDispatchMethod)2 Lexer (org.antlr.v4.runtime.Lexer)2 ATN (org.antlr.v4.runtime.atn.ATN)2