Search in sources :

Example 21 with ParserRuleContext

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

the class SqlParser method invokeParser.

private Node invokeParser(String name, String sql, Function<SqlBaseParser, ParserRuleContext> parseFunction) {
    try {
        SqlBaseLexer lexer = new SqlBaseLexer(new CaseInsensitiveStream(new ANTLRInputStream(sql)));
        CommonTokenStream tokenStream = new CommonTokenStream(lexer);
        SqlBaseParser parser = new SqlBaseParser(tokenStream);
        parser.addParseListener(new PostProcessor());
        lexer.removeErrorListeners();
        lexer.addErrorListener(ERROR_LISTENER);
        parser.removeErrorListeners();
        parser.addErrorListener(ERROR_LISTENER);
        ParserRuleContext tree;
        try {
            // first, try parsing with potentially faster SLL mode
            parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
            tree = parseFunction.apply(parser);
        } catch (ParseCancellationException ex) {
            // if we fail, parse with LL mode
            // rewind input stream
            tokenStream.reset();
            parser.reset();
            parser.getInterpreter().setPredictionMode(PredictionMode.LL);
            tree = parseFunction.apply(parser);
        }
        return new AstBuilder().visit(tree);
    } catch (StackOverflowError e) {
        throw new ParsingException(name + " is too large (stack overflow while parsing)");
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 22 with ParserRuleContext

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

the class TypeCalculation method calculateLiteralValue.

public static Long calculateLiteralValue(String calculation, Map<String, Long> inputs) {
    try {
        ParserRuleContext tree = parseTypeCalculation(calculation);
        CalculateTypeVisitor visitor = new CalculateTypeVisitor(inputs);
        BigInteger result = visitor.visit(tree);
        return result.longValueExact();
    } catch (StackOverflowError e) {
        throw new ParsingException("Type calculation is too large (stack overflow while parsing)");
    }
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) ParsingException(com.facebook.presto.sql.parser.ParsingException) BigInteger(java.math.BigInteger)

Example 23 with ParserRuleContext

use of org.antlr.v4.runtime.ParserRuleContext in project dex2jar by pxb1988.

the class AntlrSmaliUtil method findTotalRegisters.

private static int findTotalRegisters(SmaliParser.SMethodContext ctx, int ins) {
    int totalRegisters = -1;
    List<SmaliParser.SInstructionContext> instructionContexts = ctx.sInstruction();
    for (SmaliParser.SInstructionContext instructionContext : instructionContexts) {
        ParserRuleContext parserRuleContext = (ParserRuleContext) instructionContext.getChild(0);
        if (parserRuleContext != null) {
            int ruleIndex = parserRuleContext.getRuleIndex();
            if (ruleIndex == SmaliParser.RULE_fregisters) {
                totalRegisters = parseInt(((SmaliParser.FregistersContext) parserRuleContext).xregisters.getText());
                break;
            } else if (ruleIndex == SmaliParser.RULE_flocals) {
                totalRegisters = ins + parseInt(((SmaliParser.FlocalsContext) parserRuleContext).xlocals.getText());
                break;
            }
        }
    }
    return totalRegisters;
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) SmaliParser(com.googlecode.d2j.smali.antlr4.SmaliParser)

Example 24 with ParserRuleContext

use of org.antlr.v4.runtime.ParserRuleContext in project dex2jar by pxb1988.

the class AntlrSmaliUtil method acceptAnnotation.

private static void acceptAnnotation(DexAnnotationVisitor dexAnnotationVisitor, String name, SmaliParser.SAnnotationValueContext ctx) {
    ParserRuleContext t = (ParserRuleContext) ctx.getChild(0);
    switch(t.getRuleIndex()) {
        case SmaliParser.RULE_sSubannotation:
            {
                SmaliParser.SSubannotationContext subannotationContext = (SmaliParser.SSubannotationContext) t;
                DexAnnotationVisitor annotationVisitor = dexAnnotationVisitor.visitAnnotation(name, Utils.unEscapeId(subannotationContext.type.getText()));
                if (annotationVisitor != null) {
                    List<SmaliParser.SAnnotationKeyNameContext> keys = subannotationContext.sAnnotationKeyName();
                    if (keys.size() > 0) {
                        List<SmaliParser.SAnnotationValueContext> values = subannotationContext.sAnnotationValue();
                        for (int i = 0; i < keys.size(); i++) {
                            acceptAnnotation(annotationVisitor, Utils.unEscapeId(keys.get(i).getText()), values.get(i));
                        }
                    }
                    annotationVisitor.visitEnd();
                }
                break;
            }
        case SmaliParser.RULE_sArrayValue:
            {
                SmaliParser.SArrayValueContext arrayValueContext = (SmaliParser.SArrayValueContext) t;
                DexAnnotationVisitor annotationVisitor = dexAnnotationVisitor.visitArray(name);
                if (annotationVisitor != null) {
                    for (SmaliParser.SAnnotationValueContext annotationValueContext : arrayValueContext.sAnnotationValue()) {
                        acceptAnnotation(annotationVisitor, null, annotationValueContext);
                    }
                    annotationVisitor.visitEnd();
                }
                break;
            }
        case SmaliParser.RULE_sBaseValue:
            SmaliParser.SBaseValueContext baseValueContext = (SmaliParser.SBaseValueContext) t;
            Object value = parseBaseValue(baseValueContext);
            dexAnnotationVisitor.visit(name, value);
            break;
    }
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) SmaliParser(com.googlecode.d2j.smali.antlr4.SmaliParser) List(java.util.List)

Example 25 with ParserRuleContext

use of org.antlr.v4.runtime.ParserRuleContext in project jetbrick-template-1x by subchen.

the class JetTemplateCodeVisitor method visitBlock.

@Override
public Code visitBlock(BlockContext ctx) {
    int size = ctx.getChildCount();
    BlockCode code = scopeCode.createBlockCode(size);
    if (size == 0)
        return code;
    for (int i = 0; i < size; i++) {
        ParseTree node = ctx.children.get(i);
        Code c = node.accept(this);
        if (node instanceof TextContext) {
            // 文本节点
            TextCode textCode = (TextCode) c;
            if (trimDirectiveLine || trimDirectiveComments) {
                ParseTree prev = (i > 0) ? ctx.children.get(i - 1) : null;
                ParseTree next = (i < size - 1) ? ctx.children.get(i + 1) : null;
                boolean trimLeft;
                boolean keepLeftNewLine = false;
                if (prev == null) {
                    trimLeft = !(ctx.getParent() instanceof TemplateContext);
                } else {
                    trimLeft = prev instanceof DirectiveContext;
                    if (trimLeft) {
                        // inline directive, 对于一个内联的 #if, #for 指令,后面有要求保留一个 NewLine
                        // @see https://github.com/subchen/jetbrick-template/issues/25
                        ParserRuleContext directive = (ParserRuleContext) ((DirectiveContext) prev).getChild(0);
                        if (directive instanceof If_directiveContext || directive instanceof For_directiveContext) {
                            if (directive.getStart().getLine() == directive.getStop().getLine()) {
                                // 保留一个 NewLine
                                keepLeftNewLine = true;
                            }
                        }
                    }
                }
                boolean trimRight;
                if (next == null) {
                    trimRight = !(ctx.getParent() instanceof TemplateContext);
                } else {
                    trimRight = (next instanceof DirectiveContext);
                }
                // trim 指令两边的注释
                if (trimDirectiveComments) {
                    textCode.trimComments(trimLeft, trimRight, commentsPrefix, commentsSuffix);
                }
                // trim 指令两边的空白内容
                if (trimDirectiveLine) {
                    textCode.trimEmptyLine(trimLeft, trimRight, keepLeftNewLine);
                }
                // trim 掉 #tag 和 #macro 指令最后一个多余的 '\n'
                if (next == null) {
                    if (ctx.getParent() instanceof Tag_directiveContext || ctx.getParent() instanceof Macro_directiveContext) {
                        textCode.trimLastNewLine();
                    }
                }
            }
            if (!textCode.isEmpty()) {
                // 如果有相同内容的Text,则从缓存中读取
                TextCode old = textCache.get(textCode.getText());
                if (old == null) {
                    old = textCode;
                    textCache.put(textCode.getText(), textCode);
                    // add text into field
                    tcc.addField(textCode.getId(), textCode.getText());
                }
                code.addLine(old.toString());
            }
        } else {
            code.addChild(c);
        }
    }
    return code;
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) For_directiveContext(jetbrick.template.parser.grammer.JetTemplateParser.For_directiveContext) TemplateContext(jetbrick.template.parser.grammer.JetTemplateParser.TemplateContext) ScopeCode(jetbrick.template.parser.code.ScopeCode) BlockCode(jetbrick.template.parser.code.BlockCode) SegmentListCode(jetbrick.template.parser.code.SegmentListCode) TemplateClassCode(jetbrick.template.parser.code.TemplateClassCode) TextCode(jetbrick.template.parser.code.TextCode) ForExpressionCode(jetbrick.template.parser.code.ForExpressionCode) SegmentCode(jetbrick.template.parser.code.SegmentCode) MacroCode(jetbrick.template.parser.code.MacroCode) Code(jetbrick.template.parser.code.Code) DefineExpressionCode(jetbrick.template.parser.code.DefineExpressionCode) TagCode(jetbrick.template.parser.code.TagCode) BlockCode(jetbrick.template.parser.code.BlockCode) If_directiveContext(jetbrick.template.parser.grammer.JetTemplateParser.If_directiveContext) TextContext(jetbrick.template.parser.grammer.JetTemplateParser.TextContext) Macro_directiveContext(jetbrick.template.parser.grammer.JetTemplateParser.Macro_directiveContext) DirectiveContext(jetbrick.template.parser.grammer.JetTemplateParser.DirectiveContext) TextCode(jetbrick.template.parser.code.TextCode) Tag_directiveContext(jetbrick.template.parser.grammer.JetTemplateParser.Tag_directiveContext) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Aggregations

ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)24 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)6 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)5 ParseTree (org.antlr.v4.runtime.tree.ParseTree)5 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)4 SmaliParser (com.googlecode.d2j.smali.antlr4.SmaliParser)3 BitSet (java.util.BitSet)3 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)3 NoViableAltException (org.antlr.v4.runtime.NoViableAltException)3 ParserInterpreter (org.antlr.v4.runtime.ParserInterpreter)3 Token (org.antlr.v4.runtime.Token)3 ATN (org.antlr.v4.runtime.atn.ATN)3 DFAState (org.antlr.v4.runtime.dfa.DFAState)3 Interval (org.antlr.v4.runtime.misc.Interval)3 ErrorNode (org.antlr.v4.runtime.tree.ErrorNode)3 ArrayList (java.util.ArrayList)2 CommonToken (org.antlr.v4.runtime.CommonToken)2 ATNState (org.antlr.v4.runtime.atn.ATNState)2 DecisionInfo (org.antlr.v4.runtime.atn.DecisionInfo)2 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)2