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)");
}
}
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)");
}
}
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;
}
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;
}
}
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;
}
Aggregations