private void checkInvocations(CommonTree node) { if (exceptionClass != "" && node.getType() == 4 && !forward && node.getText() != exceptionClass) { exceptionVariable = node.getText(); } if (exceptionVariable != "" && node.getType() == FORWARDCURLYBRACKET) { this.forward = true; } if ((forward) && (node.getType() == IDENTIFIER) && (node.getText()).equals(exceptionVariable)) { foundExceptionVariable = true; } if (foundExceptionVariable && node.getType() == DOT) { foundDot = true; } if (foundExceptionVariable && foundDot && node.getType() == IDENTIFIER) { foundDot = false; foundExceptionVariable = false; CSharpData data = new CSharpData(); data.setClassName(belongsToClass); data.setLineNumber(node.getLine()); data.setInvocationTo(exceptionClass); data.setInvocationName(node.getText()); allInvocations.add(data); } }
// $ANTLR start "unit" // SequenceWalker.g:20:1: unit returns [Unit result] : ^( UNIT WORD TERMINATOR ) ; public final Unit unit() throws RecognitionException { Unit result = null; CommonTree WORD1 = null; CommonTree TERMINATOR2 = null; try { // SequenceWalker.g:21:2: ( ^( UNIT WORD TERMINATOR ) ) // SequenceWalker.g:21:4: ^( UNIT WORD TERMINATOR ) { match(input, UNIT, FOLLOW_UNIT_in_unit75); match(input, Token.DOWN, null); WORD1 = (CommonTree) match(input, WORD, FOLLOW_WORD_in_unit77); TERMINATOR2 = (CommonTree) match(input, TERMINATOR, FOLLOW_TERMINATOR_in_unit79); match(input, Token.UP, null); result = new Unit( (WORD1 != null ? WORD1.getText() : null), (TERMINATOR2 != null ? TERMINATOR2.getText() : null)); } } catch (RecognitionException re) { reportError(re); recover(input, re); } finally { // do for sure before leaving } return result; }
public SqlJetBindParameter(CommonTree ast) { if ("bind_name".equalsIgnoreCase(ast.getText())) { position = null; name = ast.getChild(0).getText(); } else { assert "bind".equalsIgnoreCase(ast.getText()); position = (ast.getChildCount() > 0) ? Integer.valueOf(ast.getChild(0).getText()) : null; name = null; } }
/** * @see org.coode.parsers.ErrorListener#incompatibleSymbols(org.antlr.runtime.tree.CommonTree, * org.antlr.runtime.tree.CommonTree[]) */ public void incompatibleSymbols(CommonTree parentExpression, CommonTree... trees) { List<String> symbols = new ArrayList<String>(trees.length); for (CommonTree commonTree : trees) { symbols.add(commonTree.getText()); } throw new IncompatibleSymbolsParsingException( parentExpression.getText(), parentExpression.getLine(), parentExpression.getCharPositionInLine(), symbols.toArray(new String[symbols.size()])); }
@Test public void backreference_or_octalTest() throws Exception { Object[][] tests = { {"\\41", "!"}, {"\\041", "!"} }; for (Object[] test : tests) { String input = (String) test[0]; String expected = (String) test[1]; PCREParser parser = getParser(input); PCREParser.backreference_or_octal_return value = parser.backreference_or_octal(); assertThat(value, notNullValue()); CommonTree tree = (CommonTree) value.getTree(); assertThat(tree.getType(), is(PCRELexer.LITERAL)); assertThat(tree.getText(), is(expected)); } tests = new Object[][] { {"\\1", "1"}, {"\\9", "9"} }; for (Object[] test : tests) { String input = (String) test[0]; String expected = (String) test[1]; PCREParser parser = getParser(input); PCREParser.backreference_or_octal_return value = parser.backreference_or_octal(); assertThat(value, notNullValue()); CommonTree tree = (CommonTree) value.getTree(); assertThat(tree.getType(), is(PCRELexer.NUMBERED_BACKREFERENCE)); assertThat(tree.getChildCount(), is(1)); CommonTree numberNode = (CommonTree) tree.getChild(0); assertThat(numberNode.getType(), is(PCRELexer.NUMBER)); assertThat(numberNode.getText(), is(expected)); } }
private static UIQueryASTPredicateRelation parseRelation(CommonTree rel) { String relText = rel.getText().toUpperCase(); boolean caseSensitive = true; final String CASE_INSENSITIVE_SPEC = "[C]"; if (relText.endsWith(CASE_INSENSITIVE_SPEC)) { caseSensitive = false; relText = relText.substring(0, relText.length() - CASE_INSENSITIVE_SPEC.length()); } if ("BEGINSWITH".equals(relText)) { return new BeginsWithRelation(caseSensitive); } else if ("ENDSWITH".equals(relText)) { return new EndsWithRelation(caseSensitive); } else if ("CONTAINS".equals(relText)) { return new ContainsRelation(caseSensitive); } else if ("LIKE".equals(relText)) { return new LikeRelation(caseSensitive); } else if ("<".equals(relText)) { return ComparisonOperator.LESSTHAN; } else if ("<=".equals(relText)) { return ComparisonOperator.LESSTHANOREQUAL; } else if ("=".equals(relText)) { return ComparisonOperator.EQUAL; } else if (">".equals(relText)) { return ComparisonOperator.GREATERTHAN; } else if (">=".equals(relText)) { return ComparisonOperator.GREATERTHANOREQUAL; } else { throw new IllegalStateException("Unsupported Relation: " + relText); } }
private String getLocatorName(CommonTree tree) { String text = tree.getText(); if (text.endsWith("://")) { return text.substring(0, text.length() - 3); } else { return null; } }
private Number getNumber(CommonTree tree) { String text = tree.getText(); if (text.contains(".")) { return Double.parseDouble(text); } else { return Integer.parseInt(text); } }
// $ANTLR start "formal" // FunChecker.g:207:1: formal returns [Type type] : ( ^( FORMAL t= type ID ) | NOFORMAL ); public final Type formal() throws RecognitionException { Type type = null; CommonTree ID6 = null; CommonTree FORMAL7 = null; Type t = null; try { // FunChecker.g:208:2: ( ^( FORMAL t= type ID ) | NOFORMAL ) int alt6 = 2; int LA6_0 = input.LA(1); if ((LA6_0 == FORMAL)) { alt6 = 1; } else if ((LA6_0 == NOFORMAL)) { alt6 = 2; } else { NoViableAltException nvae = new NoViableAltException("", 6, 0, input); throw nvae; } switch (alt6) { case 1: // FunChecker.g:208:4: ^( FORMAL t= type ID ) { FORMAL7 = (CommonTree) match(input, FORMAL, FOLLOW_FORMAL_in_formal234); match(input, Token.DOWN, null); pushFollow(FOLLOW_type_in_formal238); t = type(); state._fsp--; ID6 = (CommonTree) match(input, ID, FOLLOW_ID_in_formal240); match(input, Token.UP, null); define((ID6 != null ? ID6.getText() : null), t, FORMAL7); type = t; } break; case 2: // FunChecker.g:212:4: NOFORMAL { match(input, NOFORMAL, FOLLOW_NOFORMAL_in_formal252); type = Type.VOID; } break; } } catch (RecognitionException re) { reportError(re); recover(input, re); } finally { // do for sure before leaving } return type; }
/** * @param ast * @return */ private boolean isCreateVirtualTable(CommonTree ast) { final CommonTree optionsNode = (CommonTree) ast.getChild(0); for (int i = 0; i < optionsNode.getChildCount(); i++) { CommonTree optionNode = (CommonTree) optionsNode.getChild(i); if ("virtual".equalsIgnoreCase(optionNode.getText())) { return true; } } return false; }
public SqlJetIndexedColumn(CommonTree ast) { name = ast.getText(); String collation = null; SqlJetSortingOrder sortingOrder = null; for (int i = 0; i < ast.getChildCount(); i++) { CommonTree child = (CommonTree) ast.getChild(i); if ("collate".equalsIgnoreCase(child.getText())) { collation = child.getChild(0).getText(); } else if ("asc".equalsIgnoreCase(child.getText())) { sortingOrder = SqlJetSortingOrder.ASC; } else if ("desc".equalsIgnoreCase(child.getText())) { sortingOrder = SqlJetSortingOrder.DESC; } else { assert false; } } this.collation = collation; this.sortingOrder = sortingOrder; }
private String checkForExceptionClass(CommonTree node) { if (exceptionClass == null) { exceptionClass = ""; } if (node.getType() == IDENTIFIER && exceptionClass.isEmpty()) { exceptionClass = node.getText(); } return exceptionClass; }
private void analyzeIncludes(Tree includeNode, UnitInterface activeUnit) { if (activeUnit == null || includeNode == null) { return; } includeIndex = 0; CommonTree node; while ((node = getNextUnitIncludeNode(includeNode)) != null) { activeUnit.addIncludes(node.getText()); } }
/** * This method prints several strings representing an AST. * * @param ct * @param level */ public static void printTree(CommonTree ct, int level) { System.out.println("son: " + ct.getText() + ": parent :" + ct.getParent() + ": level:" + level); if (ct.getChildCount() > 0) { ListIterator li = ct.getChildren().listIterator(); while (li.hasNext()) { printTree((CommonTree) li.next(), level + 1); } } }
public SqlJetViewDef(String sql, CommonTree ast) { CommonTree optionsNode = (CommonTree) ast.getChild(0); sqlStatement = sql; temporary = SqlJetTableDef.hasOption(optionsNode, "temporary"); ifNotExists = SqlJetTableDef.hasOption(optionsNode, "exists"); CommonTree nameNode = (CommonTree) ast.getChild(1); name = nameNode.getText(); databaseName = nameNode.getChildCount() > 0 ? nameNode.getChild(0).getText() : null; }
public static UIQueryASTPredicate newPredicateFromAST(CommonTree step) { // TODO Auto-generated method stub if (step.getChildCount() != 3) { throw new IllegalStateException( "Bad Predicate query: " + step + ". Expected form {getter RELATION value}."); } CommonTree prop = (CommonTree) step.getChild(0); CommonTree rel = (CommonTree) step.getChild(1); CommonTree val = (CommonTree) step.getChild(2); return new UIQueryASTPredicate( prop.getText(), UIQueryASTPredicate.parseRelation(rel), UIQueryUtils.parseValue(val)); }
/** {@inheritDoc} */ @Override public FilterCriterion parse(String dql) { try { CommonTree tree = AntlrUtils.parseDqlToAbstractSytntaxTree(dql); List<?> children = tree.getChildren(); CommonTree field = (CommonTree) children.get(0); String dqlField = field.getText(); CommonTree operator = (CommonTree) children.get(1); String dqlOperator = operator.getText(); CommonTree values = (CommonTree) children.get(2); FilterCriterion criterion; final List<CommonTree> setValues = values.getChildren(); if (setValues == null) { String dqlValue = values.getText(); criterion = DqlCriterionFactory.newCriterion(dqlField, dqlOperator, fromDqlString(dqlValue)); } else { Collection<String> dqlValues = new ArrayList<String>(); for (CommonTree element : setValues) { dqlValues.add(element.getText()); } criterion = DqlCriterionFactory.newCriterion(dqlField, dqlOperator, dqlValues); } return criterion; } catch (RecognitionException e) { throw new DqlParseException(e); } catch (IllegalArgumentException e) { // BNO - added to provide improved stack tracing where the user enters an invalid argument for // the extension_mismatch (Boolean) filter condition. throw new DqlParseException(e); } }
@Test public void numberTest() throws Exception { String source = "4567"; PCREParser parser = getParser(source + " ... "); PCREParser.number_return value = parser.number(); CommonTree tree = (CommonTree) value.getTree(); assertThat(tree.getChildCount(), is(0)); assertThat(tree.getType(), is(PCRELexer.NUMBER)); assertThat(tree.getText(), is(source)); }
@Test public void nameTest() throws Exception { String source = "justAname"; PCREParser parser = getParser(source + " ... "); PCREParser.name_return value = parser.name(); CommonTree tree = (CommonTree) value.getTree(); assertThat(tree.getChildCount(), is(0)); assertThat(tree.getType(), is(PCRELexer.NAME)); assertThat(tree.getText(), is(source)); }
@Test public void commentTest() throws Exception { String comment = "just a [a-z]+ comment"; PCREParser parser = getParser(String.format("(?#%s)regex", comment)); PCREParser.comment_return value = parser.comment(); assertThat(value, notNullValue()); CommonTree tree = (CommonTree) value.getTree(); assertThat(tree.getType(), is(PCRELexer.COMMENT)); assertThat(tree.getText(), is(comment)); }
public static Object parseValue(CommonTree val) { switch (val.getType()) { case UIQueryParser.STRING: { String textWithPings = val.getText(); String text = textWithPings.substring(1, textWithPings.length() - 1); text = text.replaceAll("\\\\'", "'"); return text; } case UIQueryParser.INT: return Integer.parseInt(val.getText(), 10); case UIQueryParser.BOOL: { String text = val.getText(); return Boolean.parseBoolean(text); } case UIQueryParser.NIL: return null; default: throw new IllegalArgumentException( "Unable to parse value type:" + val.getType() + " text " + val.getText()); } }
@Test public void octal_digitTest() throws Exception { String[] tests = ("0 1 2 3 4 5 6 7").split("\\s+"); for (String test : tests) { PCREParser parser = getParser(test); PCREParser.octal_digit_return value = parser.octal_digit(); assertThat(value, notNullValue()); CommonTree tree = (CommonTree) value.getTree(); assertThat(tree.getChildCount(), is(0)); assertThat(tree.getText(), is(test)); } }
@Test public void captureTest() throws Exception { // named captures final String name = "Just_A_Name"; Object[][] tests = { {String.format("(?<%s>regex)", name), PCRELexer.NAMED_CAPTURING_GROUP_PERL}, {String.format("(?'%s'regex)", name), PCRELexer.NAMED_CAPTURING_GROUP_PERL}, {String.format("(?P<%s>regex)", name), PCRELexer.NAMED_CAPTURING_GROUP_PYTHON} }; for (Object[] test : tests) { String input = (String) test[0]; Integer expected = (Integer) test[1]; PCREParser parser = getParser(input); PCREParser.capture_return value = parser.capture(); assertThat(value, notNullValue()); CommonTree tree = (CommonTree) value.getTree(); assertThat(tree.getChildCount(), is(2)); assertThat(tree.getType(), is(expected)); CommonTree nameChild = (CommonTree) tree.getChild(0); assertThat(nameChild.getText(), is(name)); } // numbered captures PCREParser parser = getParser("(regex)"); PCREParser.capture_return value = parser.capture(); assertThat(value, notNullValue()); CommonTree tree = (CommonTree) value.getTree(); assertThat(tree.getType(), is(PCRELexer.CAPTURING_GROUP)); }
@Override public Boolean call(FelNode... node) { FelNode n = node[0]; if (n == null) { return false; } boolean isFun = n instanceof FunNode; if (isFun) { if (n instanceof CommonTree) { CommonTree treeNode = (CommonTree) n; CommonTree p = treeNode.parent; if (p != null) { if (Dot.DOT.equals(p.getText())) { // 点运算符后的函数节点不是真正意义上的变量节点。 isFun = p.getChildren().get(0) == n; } } } } return isFun; }
// $ANTLR start "var_decl" // FunChecker.g:216:1: var_decl : ^( VAR t1= type ID t2= expr ) ; public final void var_decl() throws RecognitionException { CommonTree ID8 = null; CommonTree VAR9 = null; Type t1 = null; Type t2 = null; try { // FunChecker.g:217:2: ( ^( VAR t1= type ID t2= expr ) ) // FunChecker.g:217:4: ^( VAR t1= type ID t2= expr ) { VAR9 = (CommonTree) match(input, VAR, FOLLOW_VAR_in_var_decl270); match(input, Token.DOWN, null); pushFollow(FOLLOW_type_in_var_decl274); t1 = type(); state._fsp--; ID8 = (CommonTree) match(input, ID, FOLLOW_ID_in_var_decl276); pushFollow(FOLLOW_expr_in_var_decl280); t2 = expr(); state._fsp--; match(input, Token.UP, null); define((ID8 != null ? ID8.getText() : null), t1, VAR9); checkType(t1, t2, VAR9); } } catch (RecognitionException re) { reportError(re); recover(input, re); } finally { // do for sure before leaving } return; }
@Test public void octalTest() throws Exception { Object[][] tests = { {"\\41", "!"}, {"\\041", "!"} }; for (Object[] test : tests) { String input = (String) test[0]; String expected = (String) test[1]; PCREParser parser = getParser(input); PCREParser.octal_char_return value = parser.octal_char(); assertThat(value, notNullValue()); CommonTree tree = (CommonTree) value.getTree(); assertThat(tree.getType(), is(PCRELexer.LITERAL)); assertThat(tree.getText(), is(expected)); } }
private void processParseResult( DocumentSnapshot snapshot, CompiledFileModelV4 result, GrammarNode.GrammarNodeDescription parserRulesRootDescription, GrammarNode.GrammarNodeDescription lexerRulesRootDescription) { GrammarRootAST parseResult = result.getResult(); if (parseResult == null) { return; } GrammarRootAST lexerParseResult = null; if (result.getGrammar() != null && result.getGrammar().getImplicitLexer() != null) { lexerParseResult = result.getGrammar().getImplicitLexer().ast; } FileObject fileObject = result.getFileObject(); Set<GrammarAST> topLevelRules = new HashSet<>(parseResult.getNodesWithType(ANTLRParser.RULE)); Set<GrammarAST> modes = new HashSet<>(parseResult.getNodesWithType(ANTLRParser.MODE)); if (lexerParseResult != null) { topLevelRules.addAll(lexerParseResult.getNodesWithType(ANTLRParser.RULE)); modes.addAll(lexerParseResult.getNodesWithType(ANTLRParser.MODE)); } Map<GrammarAST, Set<GrammarAST>> modeRules = new HashMap<>(); for (GrammarAST mode : modes) { Set<GrammarAST> rules = new HashSet<>(mode.getNodesWithType(ANTLRParser.RULE)); modeRules.put(mode, rules); topLevelRules.removeAll(rules); } processRules( snapshot, result, topLevelRules, parserRulesRootDescription.getChildren(), lexerRulesRootDescription.getChildren()); for (Map.Entry<GrammarAST, Set<GrammarAST>> entry : modeRules.entrySet()) { String modeName = getModeName(entry.getKey()); GrammarNode.GrammarNodeDescription modeDescription = new GrammarNode.GrammarNodeDescription(DeclarationKind.MODE, "_" + modeName); modeDescription.setHtmlHeader("mode " + modeName); modeDescription.setOffset(snapshot, result.getFileObject(), getElementOffset(entry.getKey())); modeDescription.setSpan(getSpan(snapshot, result, entry.getKey())); modeDescription.setInherited( snapshot == null); // for now, go on the fact that snapshots aren't available for imported // files lexerRulesRootDescription.getChildren().add(modeDescription); processRules( snapshot, result, entry.getValue(), modeDescription.getChildren(), modeDescription.getChildren()); } Set<GrammarAST> tokensSpecs = new HashSet<>(parseResult.getNodesWithType(ANTLRParser.TOKENS_SPEC)); if (lexerParseResult != null) { tokensSpecs.addAll(lexerParseResult.getNodesWithType(ANTLRParser.TOKENS_SPEC)); } for (GrammarAST tokensSpec : tokensSpecs) { for (Object childObject : tokensSpec.getChildren()) { if (!(childObject instanceof CommonTree)) { continue; } CommonTree child = (CommonTree) childObject; if (child.getType() == ANTLRParser.ASSIGN && child.getChildCount() == 2) { String ruleName = child.getChild(0).getText(); if (ruleName == null || ruleName.length() == 0) { continue; } GrammarNode.GrammarNodeDescription ruleDescription = new GrammarNode.GrammarNodeDescription(DeclarationKind.TOKEN, ruleName); ruleDescription.setOffset(snapshot, fileObject, getElementOffset(child)); ruleDescription.setSpan(getSpan(snapshot, result, child)); ruleDescription.setInherited( snapshot == null); // for now, go on the fact that snapshots aren't available for imported // files if (Grammar.isTokenName(ruleName)) { lexerRulesRootDescription.getChildren().add(ruleDescription); } else { parserRulesRootDescription.getChildren().add(ruleDescription); } } else if (child.getChildCount() == 0 && child.getToken() != null) { String ruleName = child.getText(); if (ruleName == null || ruleName.length() == 0) { continue; } GrammarNode.GrammarNodeDescription ruleDescription = new GrammarNode.GrammarNodeDescription(DeclarationKind.TOKEN, ruleName); ruleDescription.setOffset(snapshot, fileObject, getElementOffset(child)); ruleDescription.setSpan(getSpan(snapshot, result, child)); ruleDescription.setInherited( snapshot == null); // for now, go on the fact that snapshots aren't available for imported // files if (Grammar.isTokenName(ruleName)) { lexerRulesRootDescription.getChildren().add(ruleDescription); } else { parserRulesRootDescription.getChildren().add(ruleDescription); } } } } }
private String getString(CommonTree tree) { return tree.getText().substring(1, tree.getText().length() - 1); }
// $ANTLR start "propertyExpr" // nu/cotentin/parsing/cotentinel/CotentinELTree.g:67:1: propertyExpr returns [ExprNode result] : // (id= ID (acc= accessor )? (prop= propertyExpr )? ) ; public final ExprNode propertyExpr() throws RecognitionException { ExprNode result = null; CommonTree id = null; ExprNode acc = null; ExprNode prop = null; try { // nu/cotentin/parsing/cotentinel/CotentinELTree.g:67:39: ( (id= ID (acc= accessor )? (prop= // propertyExpr )? ) ) // nu/cotentin/parsing/cotentinel/CotentinELTree.g:68:2: (id= ID (acc= accessor )? (prop= // propertyExpr )? ) { // nu/cotentin/parsing/cotentinel/CotentinELTree.g:68:2: (id= ID (acc= accessor )? (prop= // propertyExpr )? ) // nu/cotentin/parsing/cotentinel/CotentinELTree.g:68:2: id= ID (acc= accessor )? (prop= // propertyExpr )? { id = (CommonTree) match(input, ID, FOLLOW_ID_in_propertyExpr246); if (state.failed) return result; // nu/cotentin/parsing/cotentinel/CotentinELTree.g:68:11: (acc= accessor )? int alt7 = 2; switch (input.LA(1)) { case NUMBER: case STRING: { alt7 = 1; } break; } switch (alt7) { case 1: // nu/cotentin/parsing/cotentinel/CotentinELTree.g:68:11: acc= accessor { pushFollow(FOLLOW_accessor_in_propertyExpr250); acc = accessor(); state._fsp--; if (state.failed) return result; } break; } // nu/cotentin/parsing/cotentinel/CotentinELTree.g:68:26: (prop= propertyExpr )? int alt8 = 2; switch (input.LA(1)) { case ID: { alt8 = 1; } break; } switch (alt8) { case 1: // nu/cotentin/parsing/cotentinel/CotentinELTree.g:68:26: prop= propertyExpr { pushFollow(FOLLOW_propertyExpr_in_propertyExpr255); prop = propertyExpr(); state._fsp--; if (state.failed) return result; } break; } } if (state.backtracking == 0) { result = new IdentifierNode(id.getText()); ExprNode cur = result; if (acc != null) { result.getChildren().add(acc); cur = acc; } if (prop != null) { cur.getChildren().add(prop); } } } } catch (RecognitionException re) { reportError(re); recover(input, re); } finally { // do for sure before leaving } return result; }
/** * @see org.coode.parsers.ErrorListener#illegalToken(org.antlr.runtime.tree.CommonTree, * java.lang.String) */ public void illegalToken(CommonTree t, String message) { throw new IllegalTokenParsingException( t.getText(), t.getLine(), t.getCharPositionInLine(), message); }