List of usage examples for org.antlr.v4.runtime.tree TerminalNodeImpl getSymbol
@Override
public Token getSymbol()
From source file:info.fulloo.trygve.parser.Pass1Listener.java
License:Open Source License
private void processIdentifierListRecursive(final Identifier_listContext identifier_list, final Type type, final int lineNumber, final AccessQualifier accessQualifier) { final List<ParseTree> children = identifier_list.children; Token tok;/*from w w w . java2 s . c o m*/ ObjectDeclaration objDecl = null; for (ParseTree pt : children) { if (pt instanceof TerminalNodeImpl) { final TerminalNodeImpl tnpt = (TerminalNodeImpl) pt; if (tnpt.getChildCount() == 0) { tok = tnpt.getSymbol(); final String tokAsText = tok.getText(); if (tokAsText.equals("=") == true) { ; // we pick it up under the ExprContext check below } else if (tokAsText.equals(",") == true) { ; // skip it; it separates elements } else { this.nameCheck(tokAsText, lineNumber); objDecl = this.pass1InitialDeclarationCheck(tokAsText, lineNumber); if (null == objDecl) { objDecl = new ObjectDeclaration(tokAsText, type, lineNumber); declareObjectSuitableToPass(currentScope_, objDecl); objDecl.setAccess(accessQualifier, currentScope_, lineNumber); } else { // Doesn't hurt to update type objDecl.updateType(type); } } } else { for (int i = 0; i < tnpt.getChildCount(); i++) { final ParseTree pt2 = tnpt.getChild(i); assert pt2 instanceof TerminalNodeImpl; final TerminalNodeImpl tnpt2 = (TerminalNodeImpl) pt2; tok = tnpt2.getSymbol(); final String tokAsText = tok.getText(); this.nameCheck(tokAsText, lineNumber); if (tokAsText.equals("=") == true) { ; // we get it with the ExprContext catch below } else if (tokAsText.equals(",") == true) { ; // skip it; it separates elements } else { objDecl = this.pass1InitialDeclarationCheck(tokAsText, lineNumber); if (null == objDecl) { objDecl = new ObjectDeclaration(tokAsText, type, lineNumber); declareObjectSuitableToPass(currentScope_, objDecl); objDecl.setAccess(accessQualifier, currentScope_, lineNumber); } else { // Doesn't hurt to update type objDecl.updateType(type); } } } } } else if (pt instanceof Identifier_listContext) { this.processIdentifierListRecursive((Identifier_listContext) pt, type, lineNumber, accessQualifier); // System.err.print("Alert: "); // System.err.println(pt.getText()); } else if (pt instanceof ExprContext) { if (parsingData_.currentExpressionExists()) { final Expression initializationExpr = parsingData_.popExpression(); assert initializationExpr != null; assert objDecl != null; updateInitializationLists(initializationExpr, objDecl); } } else { assert false; } } }
From source file:org.eclipse.titan.common.parsers.cfg.CfgParseTreePrinter.java
License:Open Source License
/** * RECURSIVE/* ww w. j a v a2 s. com*/ * 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 . ja v 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.tinygroup.template.parser.TinyTemplateCodeVisitor.java
License:Open Source License
public CodeBlock visitMacro_directive(@NotNull TinyTemplateParser.Macro_directiveContext ctx) { String name = ctx.getChild(0).getText(); name = name.substring(6, name.length() - 1).trim(); //?// w w w .j ava 2 s .c om boolean isReserve = false; for (String word : RESERVED_WORDS) { if (name.equals(word)) { TerminalNodeImpl terminalNode = (TerminalNodeImpl) ctx.getChild(0); throw new SyntaxErrorException("Macro name<" + name + "> is reserved word.", terminalNode.getSymbol().getLine(), terminalNode.getSymbol().getStartIndex()); } } name = ResourceCompilerUtils.getClassNameGetter().getClassName(name).getSimpleClassName(); initCodeBlock.subCode(new CodeLet().lineCode("addMacro(new %s());", name)); CodeBlock macro = new CodeBlock(); TinyTemplateParser.Define_expression_listContext defineExpressionListContext = ctx.define_expression_list(); pushPeekCodeLet(); if (defineExpressionListContext != null) { defineExpressionListContext.accept(this); } macro.header(new CodeLet().lineCode("class %s extends AbstractMacro {", name)); macro.footer(new CodeLet().lineCode("}")); macro.subCode(constructMethod(name)); popCodeLet(); CodeBlock render = getMacroRenderCodeBlock(); pushCodeBlock(render); macro.subCode(render); ctx.block().accept(this); popCodeBlock(); macroCodeBlock.subCode(macro); return null; }
From source file:org.tinygroup.template.parser.TinyTemplateCodeVisitor.java
License:Open Source License
public CodeBlock visitCall_macro_directive(@NotNull TinyTemplateParser.Call_macro_directiveContext ctx) { CodeBlock callMacro = new CodeBlock(); String name = ctx.getChild(0).getText(); name = name.substring(1, name.length()); if (name.endsWith("(")) { name = name.substring(0, name.length() - 1).trim(); }/*from w w w.j ava2 s . c om*/ if (name.equals("macro")) { TerminalNodeImpl terminalNode = (TerminalNodeImpl) ctx.getChild(0); throw new SyntaxErrorException("Missing macro name for #macro directive.", terminalNode.getSymbol().getLine(), terminalNode.getSymbol().getStartIndex()); } processCallMacro(ctx.para_expression_list(), callMacro, "\"" + name + "\""); callMacro.subCode(String.format("$macro.render($template,$context,$newContext,$writer);")); return callMacro; }