List of usage examples for org.antlr.v4.runtime ParserRuleContext getChildCount
@Override public int getChildCount()
From source file:com.satisfyingstructures.J2S.J2SGrammarUtils.java
License:Open Source License
public static ParserRuleContext descendToSignificantExpression(ParserRuleContext ctx) { // These expressions are chained by precedence in the grammar. We descend into the subrulecontexts until we // find a rule context that is significant, i.e. its not just a context with one child that is another // expression. // First validate parameter switch (ctx.getRuleIndex()) { case Java8Parser.RULE_constantExpression: case Java8Parser.RULE_expression: case Java8Parser.RULE_assignmentExpression: case Java8Parser.RULE_conditionalExpression: case Java8Parser.RULE_conditionalOrExpression: case Java8Parser.RULE_conditionalAndExpression: case Java8Parser.RULE_inclusiveOrExpression: case Java8Parser.RULE_exclusiveOrExpression: case Java8Parser.RULE_andExpression: case Java8Parser.RULE_equalityExpression: case Java8Parser.RULE_relationalExpression: case Java8Parser.RULE_shiftExpression: case Java8Parser.RULE_additiveExpression: case Java8Parser.RULE_multiplicativeExpression: case Java8Parser.RULE_unaryExpression: case Java8Parser.RULE_unaryExpressionNotPlusMinus: case Java8Parser.RULE_postfixExpression: case Java8Parser.RULE_primary: case Java8Parser.RULE_primaryNoNewArray_lfno_primary: break;//from w ww. j av a 2s .co m default: return ctx; // not an acceptable parameter type } descent: while (ctx.getChildCount() == 1) { ParserRuleContext childCtx = ctx.getChild(ParserRuleContext.class, 0); if (null == childCtx) break; switch (ctx.getRuleIndex()) { case Java8Parser.RULE_unaryExpression: if (childCtx.getRuleIndex() != Java8Parser.RULE_unaryExpressionNotPlusMinus) break descent; case Java8Parser.RULE_unaryExpressionNotPlusMinus: if (childCtx.getRuleIndex() != Java8Parser.RULE_postfixExpression) break descent; case Java8Parser.RULE_postfixExpression: if (childCtx.getRuleIndex() != Java8Parser.RULE_primary) break descent; } ctx = childCtx; } return ctx; }
From source file:com.yahoo.yqlplus.language.parser.ProgramParser.java
private String assignAlias(String alias, ParserRuleContext node, Scope scope) { if (alias == null) { alias = "source"; }/*from w ww .j a v a 2 s. com*/ 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; } }
From source file:com.yahoo.yqlplus.language.parser.ProgramParser.java
private OperatorNode<SequenceOperator> convertSource(ParserRuleContext sourceSpecNode, Scope scope) { // DataSources String alias;/*from w ww.j a v a 2 s. c o m*/ OperatorNode<SequenceOperator> result; ParserRuleContext dataSourceNode = sourceSpecNode; ParserRuleContext aliasContext = null; //data_source //: call_source //| LPAREN source_statement RPAREN //| sequence_source //; if (sourceSpecNode instanceof Source_specContext) { dataSourceNode = (ParserRuleContext) sourceSpecNode.getChild(0); if (sourceSpecNode.getChildCount() == 2) { aliasContext = (ParserRuleContext) sourceSpecNode.getChild(1); } if (dataSourceNode.getChild(0) instanceof Call_sourceContext || dataSourceNode.getChild(0) instanceof Sequence_sourceContext) { dataSourceNode = (ParserRuleContext) dataSourceNode.getChild(0); } //TODO double check whether comment out this is correct // else { //source_statement // dataSourceNode = (ParserRuleContext)dataSourceNode.getChild(1); // } } switch (getParseTreeIndex(dataSourceNode)) { case yqlplusParser.RULE_write_data_source: case yqlplusParser.RULE_call_source: { List<String> names = readName( (Namespaced_nameContext) dataSourceNode.getChild(Namespaced_nameContext.class, 0)); alias = assignAlias(names.get(names.size() - 1), aliasContext, scope); List<OperatorNode<ExpressionOperator>> arguments = ImmutableList.of(); ArgumentsContext argumentsContext = dataSourceNode.getRuleContext(ArgumentsContext.class, 0); if (argumentsContext != null) { List<ArgumentContext> argumentContexts = argumentsContext.argument(); arguments = Lists.newArrayListWithExpectedSize(argumentContexts.size()); for (ArgumentContext argumentContext : argumentContexts) { arguments.add(convertExpr(argumentContext, scope)); } } if (names.size() == 1 && scope.isVariable(names.get(0))) { String ident = names.get(0); if (arguments.size() > 0) { throw new ProgramCompileException(toLocation(scope, argumentsContext), "Invalid call-with-arguments on local source '%s'", ident); } result = OperatorNode.create(toLocation(scope, dataSourceNode), SequenceOperator.EVALUATE, OperatorNode.create(toLocation(scope, dataSourceNode), ExpressionOperator.VARREF, ident)); } else { result = OperatorNode.create(toLocation(scope, dataSourceNode), SequenceOperator.SCAN, scope.resolvePath(names), arguments); } break; } case yqlplusParser.RULE_sequence_source: { IdentContext identContext = dataSourceNode.getRuleContext(IdentContext.class, 0); String ident = identContext.getText(); if (!scope.isVariable(ident)) { throw new ProgramCompileException(toLocation(scope, identContext), "Unknown variable reference '%s'", ident); } alias = assignAlias(ident, aliasContext, scope); result = OperatorNode.create(toLocation(scope, dataSourceNode), SequenceOperator.EVALUATE, OperatorNode.create(toLocation(scope, dataSourceNode), ExpressionOperator.VARREF, ident)); break; } case yqlplusParser.RULE_source_statement: { alias = assignAlias(null, dataSourceNode, scope); result = convertQuery(dataSourceNode, scope); break; } case yqlplusParser.RULE_data_source: { alias = assignAlias("source", aliasContext, scope); result = convertQuery(dataSourceNode.getChild(1), scope); break; } default: throw new IllegalArgumentException( "Unexpected argument type to convertSource: " + dataSourceNode.getText()); } result.putAnnotation("alias", alias); return result; }
From source file:com.yahoo.yqlplus.language.parser.ProgramParser.java
private OperatorNode<ExpressionOperator> readValues(ParserRuleContext node, Scope scope) { List<String> fieldNames; List<OperatorNode<ExpressionOperator>> fieldValues; if (node.getRuleIndex() == yqlplusParser.RULE_field_def) { Field_defContext fieldDefContext = (Field_defContext) node; //TODO double check fieldNames = Lists.newArrayListWithExpectedSize(node.getChildCount()); fieldValues = Lists.newArrayListWithExpectedSize(node.getChildCount()); for (int i = 0; i < node.getChildCount(); i++) { fieldNames/* w w w . j av a2 s . c o m*/ .add((String) convertExpr(node.getChild(i).getChild(0).getChild(0), scope).getArgument(1)); fieldValues.add(convertExpr(node.getChild(i).getChild(0).getChild(1), scope)); } } else { assert node.getChildCount() % 2 == 0; int numPairs = node.getChildCount() / 2; fieldNames = Lists.newArrayListWithExpectedSize(numPairs); fieldValues = Lists.newArrayListWithExpectedSize(numPairs); for (int i = 0; i < numPairs; i++) { fieldNames.add((String) convertExpr(node.getChild(i).getChild(0), scope).getArgument(1)); fieldValues.add(convertExpr(node.getChild(numPairs + i), scope)); } } return OperatorNode.create(ExpressionOperator.MAP, fieldNames, fieldValues); }
From source file:de.uni.bremen.monty.moco.util.ParseTreePrinter.java
License:Open Source License
@Override public void exitEveryRule(ParserRuleContext ctx) { if (ctx.getChildCount() > 0) { intend--; } }
From source file:org.eclipse.titan.common.parsers.cfg.CfgParseTreePrinter.java
License:Open Source License
/** * RECURSIVE/*ww w . j a v a2 s . co m*/ * Builds parse tree text including hidden tokens * @param aParseTree parse tree * @param aTokens token list from the lexer (all, hidden and not hidden also) * @param aPrintHiddenBefore true to print hidden tokens before the parse tree * (NOTE: hidden tokens in the parse tree will be printed) * @param aResolveMode mode of resolving * @param aFile the parse tree of this file to print * needed only if aResolveMode != NO_RESOLVING, in case of [ORDERED_INCLUDE] */ private void print(final ParseTree aParseTree, final List<Token> aTokens, final boolean aPrintHiddenBefore, final ResolveMode aResolveMode, final Path aFile) { if (aParseTree == null) { ErrorReporter.logWarning("ConfigTreeNodeUtilities.print(): aParseTree == null"); return; } if (aParseTree instanceof ParserRuleContext) { final ParserRuleContext rule = (ParserRuleContext) aParseTree; if (mDisallowedNodes != null && mDisallowedNodes.contains(rule.start.getType())) { return; } if (aPrintHiddenBefore && rule.getChildCount() > 0 && rule.getChild(0) instanceof AddedParseTree) { // special case: if AddedParseTree is the 1st in the rule, it has no information // about the hidden tokens, as it has no position in the token list, but the rule may have printHiddenTokensBefore(rule, aTokens); } } else if (aParseTree instanceof TerminalNodeImpl) { final TerminalNodeImpl tn = (TerminalNodeImpl) aParseTree; final Token token = tn.getSymbol(); if (mDisallowedNodes == null || !mDisallowedNodes.contains(token.getType())) { printToken(token, aTokens, aPrintHiddenBefore, aResolveMode, aFile); } } else if (aParseTree instanceof AddedParseTree) { final AddedParseTree t = (AddedParseTree) aParseTree; mSb.append(t.getText()); } else { ErrorReporter.INTERNAL_ERROR("ConfigTreeNodeUtilities.print(): unexpected ParseTree type"); } for (int i = 0; i < aParseTree.getChildCount(); i++) { ParseTree child = aParseTree.getChild(i); if (child == aParseTree) { ErrorReporter.INTERNAL_ERROR("ConfigTreeNodeUtilities.print(): child == aParseTree"); } else { print(child, aTokens, aPrintHiddenBefore || i > 0, aResolveMode, aFile); } } }
From source file:org.eclipse.titan.common.parsers.ParserLogger.java
License:Open Source License
/** * Logs a parse tree./*from w w w . jav a 2s . c o m*/ * Internal version. * RECURSIVE * @param aRoot parse tree * @param aParser parser to get rule name * @param aTokens token list to get tokens by index (for getting tokens of a rule) * @param aTokenNameResolver resolver to get token name * @param aLevel indentation level * @param aParentOneChild parent has 1 child */ private static void log(final ParseTree aRoot, final Parser aParser, final List<Token> aTokens, final TokenNameResolver aTokenNameResolver, final int aLevel, final boolean aParentOneChild) { if (aRoot == null) { println("ERROR: ParseTree root is null"); return; } if (!aParser.getBuildParseTree()) { println("ERROR: ParseTree is not build. Call Parser.setBuildParseTree( true ); BEFORE parsing. Or do NOT call Parser.setBuildParseTree( false );"); return; } if (aRoot instanceof ParserRuleContext) { final ParserRuleContext rule = (ParserRuleContext) aRoot; final String ruleInfo = getRuleInfo(rule, aParser, aTokenNameResolver); if (aParentOneChild) { printArrow(ruleInfo); } else { printIndent(ruleInfo, aLevel); } final int count = rule.getChildCount(); final boolean oneChild = count == 1 && rule.exception == null; if (!oneChild) { print(": '" + getEscapedRuleText(rule, aTokens) + "'"); println(); } for (int i = 0; i < count; i++) { final ParseTree child = rule.getChild(i); log(child, aParser, aTokens, aTokenNameResolver, aLevel + (aParentOneChild ? 0 : 1), oneChild); } } else if (aRoot instanceof TerminalNodeImpl) { final TerminalNodeImpl tn = (TerminalNodeImpl) aRoot; if (aParentOneChild) { print(": '" + getEscapedTokenText(tn.getSymbol()) + "'"); println(); } printIndent(getTokenInfo(tn.getSymbol(), aTokenNameResolver), aLevel); if (tn.parent == null) { print(", parent == null <-------------------------------------------------------------- ERROR"); } println(); } else if (aRoot instanceof AddedParseTree) { final AddedParseTree apt = (AddedParseTree) aRoot; if (aParentOneChild) { print(": '" + getEscapedText(apt.getText()) + "'"); println(); } printIndent("AddedParseString: " + getEscapedText(apt.getText()), aLevel); if (apt.getParent() == null) { print(", parent == null <-------------------------------------------------------------- ERROR"); } println(); } else { println("ERROR: INVALID ParseTree type"); } }
From source file:org.napile.asm.io.text.in.type.TypeNodeWorker.java
License:Apache License
protected void acceptChild(@NotNull ParserRuleContext<Token> parserRuleContext) { for (int i = 0; i < parserRuleContext.getChildCount(); i++) { ParseTree parserTree = parserRuleContext.getChild(i); if (parserTree instanceof ParserRuleContext) ((ParserRuleContext) parserTree).enterRule(this); else if (parserTree instanceof ParseTree.TerminalNode) visitTerminal((ParseTree.TerminalNode) parserTree); }// w ww . j a v a 2 s . c om }
From source file:org.tinygroup.template.interpret.TemplateFromContext.java
License:GNU General Public License
/** * Trim ?Set ? ?//from w ww.java 2 s. com * @param text * @param nowTerminalNode * @param trimWhileSpace ??? * @return */ private String trimCommentsDirectiveWhileSpaceNewLine(String text, TerminalNode nowTerminalNode, boolean trimWhileSpace) { if (text == null || text.length() == 0) { return ""; } if (nowTerminalNode.getParent() instanceof TinyTemplateParser.TextContext) { // ??TextContext TinyTemplateParser.TextContext parseTree = (TinyTemplateParser.TextContext) nowTerminalNode.getParent(); // ?Text Context?Block ParserRuleContext parentParserRuleContext = getParseTrreeParentButBlock(parseTree); if (isDirectiveNeedTrim(parentParserRuleContext)) {// ??Trim\r\n text = trimTextLeft(text); if (trimWhileSpace) { text = TemplateUtil.trimStart(text, stripWithSpaceChars); text = TemplateUtil.trimEnd(text, stripWithSpaceChars); } } else if (parentParserRuleContext instanceof TinyTemplateParser.BlockContext) { // ?Text ContextBlockContext? int parentChildrenIndex = getParentChildrenIndex(parseTree, parentParserRuleContext); // ? if (parentChildrenIndex > 0) { // ? ParseTree previousParseContext = parentParserRuleContext.getChild(parentChildrenIndex - 1); // text?? if (previousParseContext instanceof TinyTemplateParser.CommentContext || (previousParseContext instanceof TinyTemplateParser.DirectiveContext && (previousParseContext .getChild(0) instanceof TinyTemplateParser.Set_directiveContext || previousParseContext.getChild( 0) instanceof TinyTemplateParser.Blank_directiveContext || previousParseContext.getChild( 0) instanceof TinyTemplateParser.Call_macro_directiveContext || previousParseContext.getChild( 0) instanceof TinyTemplateParser.Call_macro_block_directiveContext || previousParseContext.getChild( 0) instanceof TinyTemplateParser.Import_directiveContext || previousParseContext.getChild( 0) instanceof TinyTemplateParser.If_directiveContext))) { text = trimTextLeft(text); if (trimWhileSpace) { text = TemplateUtil.trimStart(text, stripWithSpaceChars); } } } if (trimWhileSpace) { // ??? if (parentChildrenIndex < parentParserRuleContext.getChildCount()) { // ? ParseTree previousParseContext = parentParserRuleContext.getChild(parentChildrenIndex + 1); // text?? if (previousParseContext instanceof TinyTemplateParser.CommentContext || (previousParseContext instanceof TinyTemplateParser.DirectiveContext && (previousParseContext .getChild(0) instanceof TinyTemplateParser.Set_directiveContext || previousParseContext.getChild( 0) instanceof TinyTemplateParser.Blank_directiveContext || previousParseContext.getChild( 0) instanceof TinyTemplateParser.Call_macro_directiveContext || previousParseContext.getChild( 0) instanceof TinyTemplateParser.Call_macro_block_directiveContext || previousParseContext.getChild( 0) instanceof TinyTemplateParser.Import_directiveContext || previousParseContext.getChild( 0) instanceof TinyTemplateParser.If_directiveContext))) { text = TemplateUtil.trimEnd(text, stripWithSpaceChars); } } } } } return text; }
From source file:org.tinygroup.template.interpret.TemplateFromContext.java
License:GNU General Public License
/** * ??TextContext(?TextContextBlockContext)?List? * @param parseTree ?//from w w w .j a v a 2 s . c o m * @param parserRuleContext * @return */ private int getParentChildrenIndex(ParserRuleContext parseTree, ParserRuleContext parserRuleContext) { int parentChildrenIndex = 0; for (int i = 0; i < parserRuleContext.getChildCount(); i++) { ParserRuleContext childrenTree = (ParserRuleContext) parserRuleContext.getChild(i); if (childrenTree == parseTree) { parentChildrenIndex = i; break; } } return parentChildrenIndex; }