org.antlr.runtime.tree.Tree Java Examples
The following examples show how to use
org.antlr.runtime.tree.Tree.
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: TarsNamespace.java From TarsJava with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public void addChild(Tree child) { super.addChild(child); if (child instanceof TarsStruct) { structList.add((TarsStruct) child); } else if (child instanceof TarsInterface) { interfaceList.add((TarsInterface) child); } else if (child instanceof TarsEnum) { enumList.add((TarsEnum) child); } else if (child instanceof TarsConst) { constList.add((TarsConst) child); } else if (child instanceof TarsKey) { TarsKey jk = (TarsKey) child; keyMap.put(jk.structName(), jk); } }
Example #2
Source File: Gpr.java From jFuzzyLogic with GNU Lesser General Public License v3.0 | 6 votes |
/** Parse a double number */ public static double parseDouble(Tree tree) { double sign = +1.0; double number = 0; if (tree.getText().equals("-")) { // Negative sign sign = -1.0; number = Gpr.parseDoubleSafe(tree.getChild(0).getText()); } else if (tree.getText().equals("+")) { // Positive sign sign = +1.0; number = Gpr.parseDoubleSafe(tree.getChild(0).getText()); } else // No sign number = Gpr.parseDoubleSafe(tree.getText()); return sign * number; }
Example #3
Source File: MembershipFunctionFuncion.java From jFuzzyLogic with GNU Lesser General Public License v3.0 | 6 votes |
/** * Parse each term (from tree) creating appropriate functions. * * @param ruleBlock Fuzzy Set for this function * @param tree : AST (tree) to parse * @return An array of objects (terms[]) */ private FclObject[] parseTerms(FunctionBlock functionBlock, Tree tree) { if (debug) Gpr.debug("Tree:" + tree.toStringTree()); Tree child = tree.getChild(0); int numberOfChilds = tree.getChildCount(); FclObject terms[] = new FclObject[numberOfChilds]; for (int i = 0; i < numberOfChilds; i++) { child = tree.getChild(i); if (debug) Gpr.debug("\t\tChild:" + child.toStringTree()); terms[i] = createFuncionTree(functionBlock, child); } return terms; }
Example #4
Source File: IgniteQueryTreeRenderer.java From hibernate-ogm-ignite with GNU Lesser General Public License v2.1 | 6 votes |
private void combineChildren(StringBuilder builder, Tree parent, String operator, boolean parentheses) { if ( parentheses ) { builder.append( '(' ); } for ( int i = 0; i < parent.getChildCount(); ++i ) { if ( i > 0 ) { builder.append( operator ); } processSubtree( builder, parent.getChild( i ) ); } if ( parentheses ) { builder.append( ')' ); } }
Example #5
Source File: LeftRecursiveRuleAnalyzer.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
/** * Match (RULE RULE_REF (BLOCK (ALT .*) (ALT RULE_REF[self] .*) (ALT .*))) * Match (RULE RULE_REF (BLOCK (ALT .*) (ALT (ASSIGN ID RULE_REF[self]) .*) (ALT .*))) */ public static boolean hasImmediateRecursiveRuleRefs(GrammarAST t, String ruleName) { if ( t==null ) return false; GrammarAST blk = (GrammarAST)t.getFirstChildWithType(BLOCK); if ( blk==null ) return false; int n = blk.getChildren().size(); for (int i = 0; i < n; i++) { GrammarAST alt = (GrammarAST)blk.getChildren().get(i); Tree first = alt.getChild(0); if ( first==null ) continue; if (first.getType() == ELEMENT_OPTIONS) { first = alt.getChild(1); if (first == null) { continue; } } if ( first.getType()==RULE_REF && first.getText().equals(ruleName) ) return true; Tree rref = first.getChild(1); if ( rref!=null && rref.getType()==RULE_REF && rref.getText().equals(ruleName) ) return true; } return false; }
Example #6
Source File: QueryTreeTransformer.java From cuba with Apache License 2.0 | 6 votes |
public void replaceWithCount(String entityName) { Tree selectedItems = queryTree.getAstSelectedItemsNode(); boolean isDistinct = "distinct".equalsIgnoreCase(selectedItems.getChild(0).getText()); if (!(isDistinct && selectedItems.getChildCount() == 2 || selectedItems.getChildCount() == 1)) throw new IllegalStateException("Cannot replace with count if multiple fields selected"); SelectedItemNode selectedItemNode; if (isDistinct) selectedItems.deleteChild(0); selectedItemNode = (SelectedItemNode) selectedItems.getChild(0); AggregateExpressionNode countNode = createAggregateCount(createWord(entityName), isDistinct); selectedItemNode.deleteChild(0); selectedItemNode.addChild(countNode); Tree orderBy = queryTree.getAstTree().getFirstChildWithType(JPA2Lexer.T_ORDER_BY); if (orderBy != null) { queryTree.getAstTree().deleteChild(orderBy.getChildIndex()); } queryTree.getAstTree().freshenParentAndChildIndexes(); }
Example #7
Source File: TestQueryParser.java From spork with Apache License 2.0 | 6 votes |
@Test public void test() throws IOException, RecognitionException { CharStream input = new QueryParserFileStream( "test/org/apache/pig/parser/TestParser.pig" ); QueryLexer lexer = new QueryLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); QueryParser parser = new QueryParser(tokens); QueryParser.query_return result = parser.query(); Tree ast = (Tree)result.getTree(); System.out.println( ast.toStringTree() ); TreePrinter.printTree( (CommonTree)ast, 0 ); Assert.assertEquals( 0, lexer.getNumberOfSyntaxErrors() ); Assert.assertEquals( 0, parser.getNumberOfSyntaxErrors() ); }
Example #8
Source File: QueryHelper.java From alfresco-remote-api with GNU Lesser General Public License v3.0 | 6 votes |
/** * Gets the children as a List * @param tree Tree * @return either emptyList or the children. */ public static List<Tree> getChildren(Tree tree) { if (tree!=null && tree.getChildCount() > 0) { List<Tree> children = new ArrayList<Tree>(tree.getChildCount()); for (int i = 0; i < tree.getChildCount(); i++) { Tree child = tree.getChild(i); children.add(child); } return children; } //Default return Collections.emptyList(); }
Example #9
Source File: FunctionBlock.java From jFuzzyLogic with GNU Lesser General Public License v3.0 | 6 votes |
/** * Parse a tree for singletons membership function series of points * @param tree : Tree to parse * @param numberOfPoints : Number of points in this function * @return A new membership function */ private MembershipFunction fclTreeFuzzifyTermSingletonsPoints(Tree tree, int numberOfPoints) { if (debug) Gpr.debug("Tree: " + tree.toStringTree()); Value x[] = new Value[numberOfPoints]; Value y[] = new Value[numberOfPoints]; for (int childNum = 0; childNum < tree.getChildCount(); childNum++) { Tree child = tree.getChild(childNum); String leaveName = child.getText(); if (debug) Gpr.debug("Sub-Parsing: " + leaveName); // It's a set of points? => Defines a piece-wise linear membership function if (leaveName.equalsIgnoreCase("(")) { x[childNum] = new Value(child.getChild(0), this); // Parse and add each point y[childNum] = new Value(child.getChild(1), this); if ((y[childNum].getValue() < 0) || (y[childNum].getValue() > 1)) throw new RuntimeException("\n\tError parsing line " + child.getLine() + " character " + child.getCharPositionInLine() + ": Membership function out of range (should be between 0 and 1). Value: '" + y[childNum] + "'\n\tTree: " + child.toStringTree()); if (debug) Gpr.debug("Parsed point " + childNum + " x=" + x[childNum] + ", y=" + y[childNum]); } else throw new RuntimeException("Unknown (or unimplemented) option : " + leaveName); } return new MembershipFunctionGenericSingleton(x, y); }
Example #10
Source File: GrammarAST.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
/** Walk ancestors of this node until we find ALT with * alt!=null or leftRecursiveAltInfo!=null. Then grab label if any. * If not a rule element, just returns null. */ public String getAltLabel() { List<? extends Tree> ancestors = this.getAncestors(); if ( ancestors==null ) return null; for (int i=ancestors.size()-1; i>=0; i--) { GrammarAST p = (GrammarAST)ancestors.get(i); if ( p.getType()== ANTLRParser.ALT ) { AltAST a = (AltAST)p; if ( a.altLabel!=null ) return a.altLabel.getText(); if ( a.leftRecursiveAltInfo!=null ) { return a.leftRecursiveAltInfo.altLabel; } } } return null; }
Example #11
Source File: IgniteQueryTreeRenderer.java From hibernate-ogm-ignite with GNU Lesser General Public License v2.1 | 6 votes |
/** * Flattens a subtree of kind "op[A, op[op[B, C], ...]]" * (or similar) into "op[A, B, C, ...]". Useful for nodes * representing multipart dis/conjunctions and arithmetic * expressions to skip unnecessary parentheses */ private static void flattenSubtree(Tree node) { if ( firstChildOfType( node, node.getType() ) == null ) { return; } Deque<Tree> s = new ArrayDeque<>(); Deque<Tree> m = new ArrayDeque<>(); s.add( node ); Tree n; while ( ( n = s.pollLast() ) != null ) { if ( n.getType() == node.getType() ) { for ( int i = 0; i < n.getChildCount(); ++i ) { s.add( n.getChild( i ) ); } } else { m.add( n ); } } for ( int i = 0; i < node.getChildCount(); ++i ) { node.setChild( i, m.pollLast() ); } while ( ( n = m.pollLast() ) != null ) { node.addChild( n ); } }
Example #12
Source File: FTSQueryParser.java From alfresco-data-model with GNU Lesser General Public License v3.0 | 5 votes |
static private String getText(List<? extends Object> nodes) { StringBuilder builder = new StringBuilder(); for(Object node : nodes) { builder.append(getText((Tree)node, false)); } return builder.toString(); }
Example #13
Source File: CliClient.java From stratio-cassandra with Apache License 2.0 | 5 votes |
/** * Delete a keyspace * @param statement - a token tree representing current statement * @throws TException - exception * @throws InvalidRequestException - exception * @throws NotFoundException - exception * @throws SchemaDisagreementException */ private void executeDelKeySpace(Tree statement) throws TException, InvalidRequestException, NotFoundException, SchemaDisagreementException { if (!CliMain.isConnected()) return; String keyspaceName = CliCompiler.getKeySpace(statement, thriftClient.describe_keyspaces()); String version = thriftClient.system_drop_keyspace(keyspaceName); sessionState.out.println(version); if (keyspaceName.equals(keySpace)) //we just deleted the keyspace we were authenticated too keySpace = null; }
Example #14
Source File: CliClient.java From stratio-cassandra with Apache License 2.0 | 5 votes |
public ByteBuffer getKeyAsBytes(String columnFamily, Tree keyTree) { if (keyTree.getType() == CliParser.FUNCTION_CALL) return convertValueByFunction(keyTree, null, null); String key = CliUtils.unescapeSQLString(keyTree.getText()); return getBytesAccordingToType(key, getKeyComparatorForCF(columnFamily)); }
Example #15
Source File: FIS.java From jFuzzyLogic with GNU Lesser General Public License v3.0 | 5 votes |
/** * Create a "Fuzzy inference system (FIS)" from an FCL definition string * @param lexer : lexer to use * @param verbose : be verbose? * @return A new FIS (or null on error) */ private static FIS createFromLexer(FclLexer lexer, boolean verbose) throws RecognitionException { FIS fis = new FIS(); CommonTokenStream tokens = new CommonTokenStream(lexer); FclParser parser = new FclParser(tokens); // FclParser.fcl_return root = parser.fcl(); FclParser.main_return root; root = parser.main(); Tree parseTree = (Tree) root.getTree(); // Error loading file? if (parseTree == null) { System.err.println("Can't create FIS"); return null; } if (debug) Gpr.debug("Tree: " + parseTree.toStringTree()); // Add every FunctionBlock (there may be more than one in each FCL file) for (int childNum = 0; childNum < parseTree.getChildCount(); childNum++) { Tree child = parseTree.getChild(childNum); if (debug) Gpr.debug("Child " + childNum + ":\t" + child + "\tTree:'" + child.toStringTree() + "'"); // Create a new FunctionBlock FunctionBlock functionBlock = new FunctionBlock(fis); // Generate fuzzyRuleSet based on tree String name = functionBlock.fclTree(child); if (debug) Gpr.debug("FunctionBlock Name: '" + name + "'"); fis.addFunctionBlock(name, functionBlock); } return fis; }
Example #16
Source File: RuleAST.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
public ActionAST getLexerAction() { Tree blk = getFirstChildWithType(ANTLRParser.BLOCK); if ( blk.getChildCount()==1 ) { Tree onlyAlt = blk.getChild(0); Tree lastChild = onlyAlt.getChild(onlyAlt.getChildCount()-1); if ( lastChild.getType()==ANTLRParser.ACTION ) { return (ActionAST)lastChild; } } return null; }
Example #17
Source File: FunctionBlock.java From jFuzzyLogic with GNU Lesser General Public License v3.0 | 5 votes |
/** * Builds rule set based on FCL tree (parsed from an FCL file) * @param tree : Tree to use * @return : RuleSet's name (or "" if no name) */ public String fclTree(Tree tree) { if (debug) Gpr.debug("Tree: " + tree.toStringTree()); Gpr.checkRootNode("FUNCTION_BLOCK", tree); ruleBlocks = new HashMap<String, RuleBlock>(); boolean firstChild = true; int ruleBlockCount = 1; // Add every child for (int childNum = 0; childNum < tree.getChildCount(); childNum++) { Tree child = tree.getChild(childNum); if (debug) Gpr.debug("\t\tChild: " + child.toStringTree()); String leaveName = child.getText(); if (firstChild) name = leaveName; else if (leaveName.equalsIgnoreCase("VAR_INPUT")) fclTreeVariables(child); else if (leaveName.equalsIgnoreCase("VAR_OUTPUT")) fclTreeVariables(child); else if (leaveName.equalsIgnoreCase("FUZZIFY")) fclTreeFuzzify(child); else if (leaveName.equalsIgnoreCase("DEFUZZIFY")) fclTreeDefuzzify(child); else if (leaveName.equalsIgnoreCase("RULEBLOCK")) { // Create and parse RuleBlock RuleBlock ruleBlock = new RuleBlock(this); String rbname = ruleBlock.fclTree(child); if (rbname.equals("")) rbname = "RuleBlock_" + ruleBlockCount; // Create name if none is given ruleBlockCount++; // Add RuleBlock ruleBlocks.put(rbname, ruleBlock); } else throw new RuntimeException("Unknown item '" + leaveName + "':\t" + child.toStringTree()); firstChild = false; } return name; }
Example #18
Source File: TarsStructMember.java From TarsJava with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void addChild(Tree child) { super.addChild(child); if (child instanceof TarsType) { memberType = (TarsType) child; } }
Example #19
Source File: ParserTestingUtils.java From spork with Apache License 2.0 | 5 votes |
public static Tree validateAst(String query) throws RecognitionException, ParsingFailureException, IOException { Tree ast = parse( query ); CommonTreeNodeStream nodes = new CommonTreeNodeStream( ast ); AstValidator walker = new AstValidator( nodes ); AstValidator.query_return newResult = walker.query(); Tree newAst = (Tree)newResult.getTree(); TreePrinter.printTree( (CommonTree)newAst, 0 ); if( 0 < walker.getNumberOfSyntaxErrors() ) throw new ParsingFailureException( AstValidator.class ); return newAst; }
Example #20
Source File: FTSQueryParser.java From alfresco-data-model with GNU Lesser General Public License v3.0 | 5 votes |
static private Float findFuzzy(Tree node) { for (int i = 0, l = node.getChildCount(); i < l; i++) { CommonTree child = (CommonTree) node.getChild(i); if (child.getType() == FTSParser.FUZZY) { String fuzzyString = child.getChild(0).getText(); float fuzzy = Float.parseFloat(fuzzyString); return Float.valueOf(fuzzy); } } return null; }
Example #21
Source File: TreeToQuery.java From cuba with Apache License 2.0 | 5 votes |
private boolean isExtractFromNode(CommonTree node) { if (node.parent != null && "from".equalsIgnoreCase(node.getText())) { //third part of EXTRACT expression if (node.childIndex >= 2) { Tree extractNode = node.parent.getChild(node.childIndex - 2); if ("extract(".equalsIgnoreCase(extractNode.getText())) { return true; } } } return false; }
Example #22
Source File: PigMacro.java From spork with Apache License 2.0 | 5 votes |
private static void traverseMacro(Tree t, List<CommonTree> nodes, String nodeType) { if (t.getText().equals(nodeType)) { nodes.add((CommonTree) t); } int n = t.getChildCount(); for (int i = 0; i < n; i++) { Tree t0 = t.getChild(i); traverseMacro(t0, nodes, nodeType); } }
Example #23
Source File: RecordPathCompiler.java From nifi with Apache License 2.0 | 5 votes |
public static RecordPathSegment compile(final Tree pathTree, final RecordPathSegment root, final boolean absolute) { if (pathTree.getType() == FUNCTION) { return buildPath(pathTree, null, absolute); } RecordPathSegment parent = root; for (int i = 0; i < pathTree.getChildCount(); i++) { final Tree child = pathTree.getChild(i); parent = RecordPathCompiler.buildPath(child, parent, absolute); } return parent; }
Example #24
Source File: CliClient.java From stratio-cassandra with Apache License 2.0 | 5 votes |
private void executeConnect(Tree statement) { Tree idList = statement.getChild(0); int portNumber = Integer.parseInt(statement.getChild(1).getText()); StringBuilder hostName = new StringBuilder(); int idCount = idList.getChildCount(); for (int idx = 0; idx < idCount; idx++) { hostName.append(idList.getChild(idx).getText()); } // disconnect current connection, if any. // This is a no-op, if you aren't currently connected. CliMain.disconnect(); // now, connect to the newly specified host name and port sessionState.hostName = hostName.toString(); sessionState.thriftPort = portNumber; // if we have user name and password if (statement.getChildCount() == 4) { sessionState.username = statement.getChild(2).getText(); sessionState.password = CliUtils.unescapeSQLString(statement.getChild(3).getText()); } CliMain.connect(sessionState.hostName, sessionState.thriftPort); }
Example #25
Source File: StreamDeclarationParser.java From tracecompass with Eclipse Public License 2.0 | 5 votes |
private static DeclarationScope lookupStructName(CommonTree typeSpecifier, DeclarationScope scope) { /* * This needs a struct.struct_name.name to work, luckily, that is 99.99% * of traces we receive. */ final Tree potentialStruct = typeSpecifier.getChild(0); DeclarationScope eventHeaderScope = null; if (potentialStruct.getType() == (CTFParser.STRUCT)) { final Tree potentialStructName = potentialStruct.getChild(0); if (potentialStructName.getType() == (CTFParser.STRUCT_NAME)) { final String name = potentialStructName.getChild(0).getText(); eventHeaderScope = scope.lookupChildRecursive(name); if (eventHeaderScope == null) { eventHeaderScope = lookupScopeRecursiveStruct(name, scope); } } } /* * If that fails, maybe the struct is anonymous */ if (eventHeaderScope == null) { eventHeaderScope = scope.lookupChildRecursive(MetadataStrings.STRUCT); } /* * This can still be null */ return eventHeaderScope; }
Example #26
Source File: QueryParserDriver.java From spork with Apache License 2.0 | 5 votes |
static void traverse(Tree t, List<CommonTree> macroNodes, List<CommonTree> inlineNodes) { if (t.getText().equals(MACRO_DEF)) { macroNodes.add((CommonTree) t.getParent()); } else if (t.getText().equals(MACRO_INLINE)) { inlineNodes.add((CommonTree) t); } int n = t.getChildCount(); for (int i = 0; i < n; i++) { Tree t0 = t.getChild(i); traverse(t0, macroNodes, inlineNodes); } }
Example #27
Source File: Query.java From nifi with Apache License 2.0 | 5 votes |
static String evaluateExpression(final Tree tree, final Evaluator<?> rootEvaluator, final String queryText, final EvaluationContext evaluationContext, final AttributeValueDecorator decorator) throws ProcessException { Query query = new Query(queryText, tree, rootEvaluator); final Object evaluated = query.evaluate(evaluationContext).getValue(); if (evaluated == null) { return null; } final String value = evaluated.toString(); return decorator == null ? value : decorator.decorate(value); }
Example #28
Source File: Value.java From jFuzzyLogic with GNU Lesser General Public License v3.0 | 5 votes |
public Value(Tree tree, FunctionBlock fb) { if (tree.getType() == FclLexer.VALUE_REAL) { type = Type.REAL; valReal = Gpr.parseDouble(tree.getChild(0)); } else if (tree.getType() == FclLexer.VALUE_ID) { type = Type.VAR_REFERENCE; String varName = tree.getChild(0).getText(); varRef = fb.getVariable(varName); if (varRef == null) throw new RuntimeException("Cannot find variable: '" + varName + "'"); } else throw new RuntimeException("Unimplemented 'Value' for node type: " + tree.getType() + "\ttree: " + tree.toStringTree()); }
Example #29
Source File: HL7Query.java From nifi with Apache License 2.0 | 5 votes |
private void processDeclare(final Tree declare) { for (int i = 0; i < declare.getChildCount(); i++) { final Tree declarationTree = declare.getChild(i); final String identifier = declarationTree.getChild(0).getText(); final Tree requiredOrOptionalTree = declarationTree.getChild(1); final boolean required = requiredOrOptionalTree.getType() == REQUIRED; final String segmentName = declarationTree.getChild(2).getText(); final Declaration declaration = new Declaration() { @Override public String getAlias() { return identifier; } @Override public boolean isRequired() { return required; } @Override public Object getDeclaredValue(final HL7Message message) { if (message == null) { return null; } return message.getSegments(segmentName); } }; declarations.add(declaration); } }
Example #30
Source File: HL7Query.java From nifi with Apache License 2.0 | 5 votes |
private BooleanEvaluator buildBooleanEvaluator(final Tree tree) { // TODO: add Date comparisons // LT/GT/GE/GE should allow for dates based on Field's Type // BETWEEN // DATE('2015/01/01') // DATE('2015/01/01 12:00:00') // DATE('24 HOURS AGO') // DATE('YESTERDAY') switch (tree.getType()) { case EQUALS: return new EqualsEvaluator(buildReferenceEvaluator(tree.getChild(0)), buildReferenceEvaluator(tree.getChild(1))); case NOT_EQUALS: return new NotEqualsEvaluator(buildReferenceEvaluator(tree.getChild(0)), buildReferenceEvaluator(tree.getChild(1))); case GT: return new GreaterThanEvaluator(buildReferenceEvaluator(tree.getChild(0)), buildReferenceEvaluator(tree.getChild(1))); case LT: return new LessThanEvaluator(buildReferenceEvaluator(tree.getChild(0)), buildReferenceEvaluator(tree.getChild(1))); case GE: return new GreaterThanOrEqualEvaluator(buildReferenceEvaluator(tree.getChild(0)), buildReferenceEvaluator(tree.getChild(1))); case LE: return new LessThanOrEqualEvaluator(buildReferenceEvaluator(tree.getChild(0)), buildReferenceEvaluator(tree.getChild(1))); case NOT: return new NotEvaluator(buildBooleanEvaluator(tree.getChild(0))); case AND: return new AndEvaluator(buildBooleanEvaluator(tree.getChild(0)), buildBooleanEvaluator(tree.getChild(1))); case OR: return new OrEvaluator(buildBooleanEvaluator(tree.getChild(0)), buildBooleanEvaluator(tree.getChild(1))); case IS_NULL: return new IsNullEvaluator(buildReferenceEvaluator(tree.getChild(0))); case NOT_NULL: return new NotNullEvaluator(buildReferenceEvaluator(tree.getChild(0))); default: throw new HL7QueryParsingException("Cannot build boolean evaluator for '" + tree.getText() + "'"); } }