Java Code Examples for org.antlr.v4.runtime.tree.ParseTree#getText()
The following examples show how to use
org.antlr.v4.runtime.tree.ParseTree#getText() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SQLListener.java From flair-query-language with Apache License 2.0 | 6 votes |
protected String onFlairCastFunction(FQLParser.Func_call_exprContext func_call_expr) { String dataType = func_call_expr.getChild(2).getChild(0).getText(); ParseTree fieldName = func_call_expr.getChild(2).getChild(2); String finalFieldName = property.get(fieldName) != null ? property.get(fieldName) : fieldName.getText(); FlairCastData flairCastData = new FlairCastData(); flairCastData.setDataType(dataType); flairCastData.setFieldName(finalFieldName); Function<FlairCastData, CharSequence> func = CAST_MAP.getOrDefault( dataType.toLowerCase(), CAST_MAP.get("__other") ); return func.apply(flairCastData).toString(); }
Example 2
Source File: JetTemplateCodeVisitor.java From jetbrick-template-1x with Apache License 2.0 | 6 votes |
private String assert_java_identifier(ParseTree node, boolean isDefining) { String name = node.getText(); if ("for".equals(name)) { if (isDefining) { throw reportError("Syntax error on token \"" + name + "\" is not a valid identifier.", node); } return name; } if (Code.CONTEXT_NAME.equals(name)) { if (isDefining) { throw reportError("Duplicate local variable \"" + name + "\" is a reserved identifier.", node); } return name; } if (SourceVersion.isKeyword(name)) { throw reportError("Syntax error on token \"" + name + "\", It is not a valid identifier in Java.", node); } if (name.startsWith("$")) { throw reportError("Local variable \"" + name + "\" can't start with '$', it is a reserved identifier.", node); } return name; }
Example 3
Source File: Translator.java From bookish with MIT License | 6 votes |
/** Find all x={...} attributes and translate those from bookish to appropriate * target output format. Replace existing attribute value with translation. * * Side effect: alters xml attribute map annotation of attrs rule nodes. */ public void translateXMLAttributes(DocInfo docInfo) { Collection<ParseTree> attrsNodes = XPath.findAll(docInfo.tree, "//attrs", docInfo.parser); for (ParseTree attrsNode : attrsNodes) { for (int i = 0; i<attrsNode.getChildCount(); i++) { ParseTree assignment = attrsNode.getChild(i); ParseTree key = assignment.getChild(0); ParseTree value = assignment.getChild(2); if ( key!=null && value!=null ) { String v = value.getText(); if ( v.charAt(0)=='{' ) { v = stripQuotes(v); BookishParser.AttrsContext a = (BookishParser.AttrsContext) attrsNode; String location = docInfo.getSourceName()+" "+a.start.getLine()+":"+a.start.getCharPositionInLine(); v = tool.translateString(docInfo, v, location); a.attributes.put(key.getText(), v); // System.out.println("ALTER "+key.getText()+" from "+value.getText()+" ->" + v); } } } } }
Example 4
Source File: JetTemplateCodeVisitor.java From jetbrick-template-1x with Apache License 2.0 | 6 votes |
@Override public Code visitExpr_math_binary_shift(Expr_math_binary_shiftContext ctx) { SegmentCode lhs = (SegmentCode) ctx.expression(0).accept(this); SegmentCode rhs = (SegmentCode) ctx.expression(1).accept(this); // Combined '>' '>' => '>>' String op = ""; for (int i = 1; i < ctx.getChildCount() - 1; i++) { ParseTree node = ctx.getChild(i); if (node instanceof TerminalNode) { op = op + node.getText(); } } // 类型检查 Class<?> resultKlass = PromotionUtils.get_binary_shift(lhs.getKlass(), rhs.getKlass(), op); if (resultKlass == null) { throw reportError("The BinaryOperator \"" + op + "\" is not applicable for the operands " + lhs.getKlassName() + " and " + rhs.getKlassName(), ctx.getChild(1)); } String source = "(" + lhs.toString() + op + rhs.toString() + ")"; return new SegmentCode(resultKlass, source, ctx); }
Example 5
Source File: RefactorUtils.java From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License | 6 votes |
public static TerminalNode getRuleDefNameNode(Parser parser, ParseTree tree, String ruleName) { Collection<ParseTree> ruleDefRuleNodes; if ( Grammar.isTokenName(ruleName) ) { ruleDefRuleNodes = XPath.findAll(tree, "//lexerRule/TOKEN_REF", parser); } else { ruleDefRuleNodes = XPath.findAll(tree, "//parserRuleSpec/RULE_REF", parser); } for (ParseTree node : ruleDefRuleNodes) { String r = node.getText(); // always a TerminalNode; just get rule name of this def if ( r.equals(ruleName) ) { return (TerminalNode)node; } } return null; }
Example 6
Source File: KuduSQLParseTreeListener.java From attic-apex-malhar with Apache License 2.0 | 6 votes |
private String extractColumnNameFromContext(KuduSQLExpressionParser.IdorcolumnnameContext ctxForIDOrColumnName) { if (ctxForIDOrColumnName.ID() != null) { // we are dealing with a non-reserved keyword as a columnname. return ctxForIDOrColumnName.ID().getSymbol().getText(); } else { for (int i = 0; i < ctxForIDOrColumnName.getChildCount(); i++) { // iterate all the terminal node children to see wihich keyword is used. Note that there are spaces possible ParseTree terminalNodeTree = ctxForIDOrColumnName.getChild(i); String childNodeText = terminalNodeTree.getText(); if ( (!childNodeText.equalsIgnoreCase(" ")) && (!childNodeText.equalsIgnoreCase("'")) ) { return childNodeText; // Anything other thant a quote or whitespace is the column name ( reserved ) } } // end for loop for terminal node children } // conditional for non-ID column names return null; }
Example 7
Source File: CustomRulesVisitor.java From sonar-tsql-plugin with GNU General Public License v3.0 | 6 votes |
@Override public void apply(final ParseTree tree) { final ParsedNode parsedNode = new org.sonar.plugins.tsql.antlr.nodes.ParsedNode(tree); for (final CandidateRule rule : this.rules) { final RuleImplementation ruleImplemention = rule.getRuleImplementation(); if (matcher.match(ruleImplemention, parsedNode)) { final CandidateNode node = new CandidateNode(rule, parsedNode); if (ruleImplemention.getRuleMode() == RuleMode.GROUP) { final String name = tree.getText(); groupedNodes.putIfAbsent(name, node); } else { singleNodes.add(node); } } } }
Example 8
Source File: XpathProcessor.java From JsoupXpath with Apache License 2.0 | 6 votes |
@Override public XValue visitAdditiveExpr(XpathParser.AdditiveExprContext ctx) { List<XpathParser.MultiplicativeExprContext> multiplicativeExprContexts = ctx.multiplicativeExpr(); if (multiplicativeExprContexts.size() == 1){ return visit(multiplicativeExprContexts.get(0)); }else { Double res = visit(multiplicativeExprContexts.get(0)).asDouble(); String op = null; for (int i=1;i<ctx.getChildCount();i++){ ParseTree chiCtx = ctx.getChild(i); if (chiCtx instanceof XpathParser.MultiplicativeExprContext){ XValue next = visit(chiCtx); if ("+".equals(op)){ res+=next.asDouble(); }else if ("-".equals(op)){ res-=next.asDouble(); }else { throw new XpathParserException("syntax error, "+ctx.getText()); } }else { op = chiCtx.getText(); } } return XValue.create(res); } }
Example 9
Source File: SQLListener.java From datacollector with Apache License 2.0 | 5 votes |
@Override public void enterEquality_expression(plsqlParser.Equality_expressionContext ctx) { if (insideStatement) { // This is pretty horrible, but after some experimentation, I figured that the // third level of the tree contained the actual data. I am assuming it is because // top level is actually empty root, 2nd level contains the actual node, and 3rd level // has its individual tokens -> 0 is key, 1 is = and 2 is the value. String key = null; String val = null; ParseTree level0 = ctx.getChild(0); if (level0 != null) { ParseTree level1 = level0.getChild(0); if (level1 != null) { ParseTree keyNode = level1.getChild(0); if (keyNode != null) { key = formatName(keyNode.getText()); } ParseTree valNode = level1.getChild(2); if (valNode != null) { val = valNode.getText(); } } } // Why check the table's column names? Because stuff like TO_DATE(<something>) will also come in here // with each token as a key with null value. if (key != null && (val != null || (allowNulls && columnsExpected != null && columnsExpected.contains(key))) && !columns.containsKey(key)) { columns.put(key, formatValue(val)); } } }
Example 10
Source File: SQLListener.java From flair-query-language with Apache License 2.0 | 5 votes |
protected String onFlairTruncFunction(FQLParser.Func_call_exprContext func_call_expr) { String dataType = func_call_expr.getChild(2).getChild(2).getText(); ParseTree fieldName = func_call_expr.getChild(2).getChild(0); String finalFieldName = property.get(fieldName) != null ? property.get(fieldName) : fieldName.getText(); if (!DATE_TIME_DATA_TYPES.contains(dataType.toLowerCase())) { return finalFieldName; } String truncated = onDateTruncate(finalFieldName); return truncated == null ? finalFieldName : truncated; }
Example 11
Source File: CypherCstToAstVisitor.java From vertexium with Apache License 2.0 | 5 votes |
private CypherSetItem.Op getSetItemOp(CypherParser.OC_SetItemContext ctx) { for (ParseTree child : ctx.children) { if (child instanceof TerminalNode) { String text = child.getText(); if (text.equals("+=")) { return CypherSetItem.Op.PLUS_EQUAL; } else if (text.equals("=")) { return CypherSetItem.Op.EQUAL; } } } throw new VertexiumException("Could not find set item op: " + ctx.getText()); }
Example 12
Source File: BNFListener.java From openCypher with Apache License 2.0 | 5 votes |
private void pullUpItem(ParseTree ctx) { if (ctx.getChildCount() != 1) { throw new IllegalStateException("There should be only one child at " + ctx.getText() + " but there are " + ctx.getChildCount()); } GrammarItem movedItem = getItem(ctx.getChild(0)); if (movedItem != null) { items.put(ctx, movedItem); } else { throw new IllegalStateException("No item to be moved from " + ctx.getChild(0).getClass().getSimpleName() + " up to " + ctx.getClass().getSimpleName()); } }
Example 13
Source File: ParserBase.java From vespa with Apache License 2.0 | 5 votes |
public boolean isArrayParameter(ParseTree nameNode) { String name = nameNode.getText(); if (name.startsWith("@")) { name = name.substring(1); } return name != null && arrayParameters.contains(name); }
Example 14
Source File: ProgramParser.java From vespa with Apache License 2.0 | 5 votes |
private OperatorNode<TypeOperator> decodeType(Scope scope, TypenameContext type) { TypeOperator op; ParseTree typeNode = type.getChild(0); switch (getParseTreeIndex(typeNode)) { case yqlplusParser.TYPE_BOOLEAN: op = TypeOperator.BOOLEAN; break; case yqlplusParser.TYPE_BYTE: op = TypeOperator.BYTE; break; case yqlplusParser.TYPE_DOUBLE: op = TypeOperator.DOUBLE; break; case yqlplusParser.TYPE_INT16: op = TypeOperator.INT16; break; case yqlplusParser.TYPE_INT32: op = TypeOperator.INT32; break; case yqlplusParser.TYPE_INT64: op = TypeOperator.INT64; break; case yqlplusParser.TYPE_STRING: op = TypeOperator.STRING; break; case yqlplusParser.TYPE_TIMESTAMP: op = TypeOperator.TIMESTAMP; break; case yqlplusParser.RULE_arrayType: return OperatorNode.create(toLocation(scope, typeNode), TypeOperator.ARRAY, decodeType(scope, ((ArrayTypeContext)typeNode).getChild(TypenameContext.class, 0))); case yqlplusParser.RULE_mapType: return OperatorNode.create(toLocation(scope, typeNode), TypeOperator.MAP, decodeType(scope, ((MapTypeContext)typeNode).getChild(TypenameContext.class, 0))); default: throw new ProgramCompileException("Unknown type " + typeNode.getText()); } return OperatorNode.create(toLocation(scope, typeNode), op); }
Example 15
Source File: IndexRQL.java From indexr with Apache License 2.0 | 5 votes |
private static <T> T checkNode(ParseTree node, Class<T> clazz) { if (node instanceof ErrorNode) { throw new IndexRQLParseError(node.getText()); } else if (node instanceof TerminalNode) { return null; } else if (clazz.isInstance(node)) { return (T) node; } return null; }
Example 16
Source File: ProgramParser.java From yql-plus with Apache License 2.0 | 5 votes |
private OperatorNode<TypeOperator> decodeType(Scope scope, TypenameContext type) { TypeOperator op; ParseTree typeNode = type.getChild(0); switch (getParseTreeIndex(typeNode)) { case yqlplusParser.TYPE_BOOLEAN: op = TypeOperator.BOOLEAN; break; case yqlplusParser.TYPE_BYTE: op = TypeOperator.BYTE; break; case yqlplusParser.TYPE_DOUBLE: op = TypeOperator.DOUBLE; break; case yqlplusParser.TYPE_INT16: op = TypeOperator.INT16; break; case yqlplusParser.TYPE_INT32: op = TypeOperator.INT32; break; case yqlplusParser.TYPE_INT64: op = TypeOperator.INT64; break; case yqlplusParser.TYPE_STRING: op = TypeOperator.STRING; break; case yqlplusParser.TYPE_TIMESTAMP: op = TypeOperator.TIMESTAMP; break; case yqlplusParser.RULE_arrayType: return OperatorNode.create(toLocation(scope, typeNode), TypeOperator.ARRAY, decodeType(scope, ((ArrayTypeContext) typeNode).getChild(TypenameContext.class, 0))); case yqlplusParser.RULE_mapType: return OperatorNode.create(toLocation(scope, typeNode), TypeOperator.MAP, decodeType(scope, ((MapTypeContext) typeNode).getChild(TypenameContext.class, 0))); default: throw new ProgramCompileException("Unknown type " + typeNode.getText()); } return OperatorNode.create(toLocation(scope, typeNode), op); }
Example 17
Source File: ProgramParser.java From yql-plus with Apache License 2.0 | 5 votes |
private String assignAlias(String alias, ParserRuleContext node, Scope scope) { if (alias == null) { alias = "source"; } if (node != null && node instanceof yqlplusParser.Alias_defContext) { //alias_def : (AS? ID); ParseTree idChild = node; if (node.getChildCount() > 1) { idChild = node.getChild(1); } alias = idChild.getText(); if (scope.isCursor(alias)) { throw new ProgramCompileException(toLocation(scope, idChild), "Source alias '%s' is already used", alias); } scope.defineDataSource(toLocation(scope, idChild), alias, true); return alias; } else { String candidate = alias; int c = 0; while (scope.isCursor(candidate)) { candidate = alias + (++c); } scope.defineDataSource(null, candidate); return alias; } }
Example 18
Source File: AstBuilder.java From kalang with MIT License | 5 votes |
@Override public Object visitInterpolationExpr(KalangParser.InterpolationExprContext ctx) { List<ParseTree> children = ctx.children; ExprNode[] exprs = new ExprNode[children.size()]; for(int i=0;i<exprs.length;i++){ ParseTree c = children.get(i); if(c instanceof TerminalNode){ Token token = ((TerminalNode) c).getSymbol(); int t = token.getType(); String rawText = c.getText(); String text; switch(t){ case KalangLexer.InterpolationPreffixString: text = rawText.substring(1,rawText.length()-2); break; case KalangLexer.INTERPOLATION_STRING: text = rawText; break; case KalangLexer.RBRACE: case KalangLexer.INTERPOLATION_END: case KalangLexer.INTERPOLATION_INTERUPT: //TODO optimize empty string text = ""; break; default : throw Exceptions.unexpectedValue(t); } exprs[i]=new ConstExpr(StringLiteralUtil.parse(text)); }else if(c instanceof ExpressionContext){ exprs[i] = this.visitExpression((ExpressionContext) c); }else{ throw Exceptions.unexpectedValue(c); } } return this.concatExpressionsToStringExpr(exprs); }
Example 19
Source File: DataType.java From djl with Apache License 2.0 | 5 votes |
private static void parseTypeSpec(DataType dataType, ParseTree tree) { if (tree == null) { return; } if (tree instanceof CParser.StructOrUnionContext) { return; } if (tree instanceof CParser.TypedefNameContext) { if (dataType.getType().isEmpty()) { dataType.appendTypeName(tree.getText()); } return; } if (tree instanceof TerminalNode) { String value = tree.getText(); if ("const".equals(value)) { dataType.setConst(); } else if ("*".equals(value)) { dataType.increasePointerCount(); } else { dataType.appendTypeName(value); } return; } for (int i = 0; i < tree.getChildCount(); i++) { parseTypeSpec(dataType, tree.getChild(i)); } }
Example 20
Source File: Help.java From BigDataScript with Apache License 2.0 | 4 votes |
@Override protected void parse(ParseTree tree) { helpString = tree.getText(); if (helpString.length() > HELP_KEYWORD_LEN) helpString = helpString.substring(HELP_KEYWORD_LEN); else helpString = ""; }