private void reportError(String message, CommonTree ast) { // Print an error message relating to the given // (sub)AST. int line = ast.getLine(), column = ast.getCharPositionInLine(); System.err.println("line " + line + ":" + column + " " + message); errorCount++; }
// $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; }
// process a statement of the form: describe table <tablename> private void executeDescribeTable(CommonTree ast) throws TException { if (!CliMain.isConnected()) return; // Get table name int childCount = ast.getChildCount(); assert (childCount == 1); String tableName = ast.getChild(0).getText(); if (tableName == null) { css_.out.println("Keyspace argument required"); return; } // Describe and display Map<String, Map<String, String>> columnFamiliesMap; try { columnFamiliesMap = thriftClient_.describe_keyspace(tableName); for (String columnFamilyName : columnFamiliesMap.keySet()) { Map<String, String> columnMap = columnFamiliesMap.get(columnFamilyName); String desc = columnMap.get("Desc"); String columnFamilyType = columnMap.get("Type"); String sort = columnMap.get("CompareWith"); String flushperiod = columnMap.get("FlushPeriodInMinutes"); css_.out.println(desc); css_.out.println("Column Family Type: " + columnFamilyType); css_.out.println("Column Sorted By: " + sort); css_.out.println("flush period: " + flushperiod + " minutes"); css_.out.println("------"); } } catch (NotFoundException e) { css_.out.println("Keyspace " + tableName + " could not be found."); } }
@SuppressWarnings("unchecked") // tree.getChildren() private void walk(CommonTree tree, StringBuilder buffer, int level) { String indent = LOTSOBLANKS.substring(0, level); Token token = tree.token; int tokenType = token.getType(); String tokenText = token.getText(); int childCount = tree.getChildCount(); int childIndex = tree.getChildIndex(); buffer.append('\n'); buffer.append(indent); buffer.append(tokenText); buffer.append(" class: "); buffer.append(tree.getClass().getName()); buffer.append(" tokenType "); buffer.append(tokenType); buffer.append(" child count "); buffer.append(childCount); buffer.append(" child index "); buffer.append(childIndex); List<CommonTree> children = tree.getChildren(); if (children == null) { return; } for (CommonTree child : children) { walk(child, buffer, level + 2); } }
private void cond_gen(CommonTree tr, int[] label_num) { String cond = null; String code = null; String[] left_res_type = {null, null, null}; String[] right_res_type = {null, null, null}; label_num[0] = label_index++; // System.out.println("TEST cond_gen 1: " + getCh(tr,0) + " "+getCh(tr,1)); op_gen(getCh(tr, 0), left_res_type); op_gen(getCh(tr, 1), right_res_type); if (tr.getType() == MicroParser.GT) cond = "LE "; else if (tr.getType() == MicroParser.LT) cond = "GE "; else if (tr.getType() == MicroParser.EQ) cond = "NE "; else cond = tr.getToken().getText(); code = cond + left_res_type[2] + " " + right_res_type[2] + " label" + Integer.toString(label_num[0]); link(code); // System.out.println( "TEST cond gen 2: " + code ); }
@Test public void cc_atomTest() throws Exception { String[] ranges = {"0-9", "\\]-$", "---", "[-["}; for (String range : ranges) { PCREParser parser = getParser(range); PCREParser.cc_atom_return value = parser.cc_atom(); assertThat(value, notNullValue()); CommonTree tree = (CommonTree) value.getTree(); assertThat(tree.getChildCount(), is(2)); assertThat(tree.getType(), is(PCRELexer.RANGE)); } String[] tests = ("a b c [[:digit:]] \\d * $ ?").split("\\s+"); for (String test : tests) { PCREParser parser = getParser(test); PCREParser.cc_atom_return value = parser.cc_atom(); assertThat(value, notNullValue()); } }
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); } }
@Test public void backtrack_controlTest() throws Exception { Object[][] tests = { {"(*ACCEPT)", PCRELexer.BACKTACK_CONTROL_ACCEPT}, {"(*FAIL)", PCRELexer.BACKTACK_CONTROL_FAIL}, {"(*MARK:NAME)", PCRELexer.BACKTACK_CONTROL_MARK_NAME}, {"(*COMMIT)", PCRELexer.BACKTACK_CONTROL_COMMIT}, {"(*PRUNE)", PCRELexer.BACKTACK_CONTROL_PRUNE}, {"(*PRUNE:NAME)", PCRELexer.BACKTACK_CONTROL_PRUNE_NAME}, {"(*SKIP)", PCRELexer.BACKTACK_CONTROL_SKIP}, {"(*SKIP:NAME)", PCRELexer.BACKTACK_CONTROL_SKIP_NAME}, {"(*THEN)", PCRELexer.BACKTACK_CONTROL_THEN}, {"(*THEN:NAME)", PCRELexer.BACKTACK_CONTROL_THEN_NAME} }; for (Object[] test : tests) { String input = (String) test[0]; Integer expected = (Integer) test[1]; PCREParser parser = getParser(input); PCREParser.backtrack_control_return value = parser.backtrack_control(); assertThat(value, notNullValue()); CommonTree tree = (CommonTree) value.getTree(); assertThat(tree.getType(), is(expected)); } }
@Test public void newline_conventionTest() throws Exception { Object[][] tests = { {"(*CR)", PCRELexer.NEWLINE_CONVENTION_CR}, {"(*LF)", PCRELexer.NEWLINE_CONVENTION_LF}, {"(*CRLF)", PCRELexer.NEWLINE_CONVENTION_CRLF}, {"(*ANYCRLF)", PCRELexer.NEWLINE_CONVENTION_ANYCRLF}, {"(*ANY)", PCRELexer.NEWLINE_CONVENTION_ANY}, {"(*BSR_ANYCRLF)", PCRELexer.NEWLINE_CONVENTION_BSR_ANYCRLF}, {"(*BSR_UNICODE)", PCRELexer.NEWLINE_CONVENTION_BSR_UNICODE} }; for (Object[] test : tests) { String input = (String) test[0]; Integer expected = (Integer) test[1]; PCREParser parser = getParser(input); PCREParser.newline_convention_return value = parser.newline_convention(); assertThat(value, notNullValue()); CommonTree tree = (CommonTree) value.getTree(); assertThat(tree.getType(), is(expected)); } }
@Test public void subroutine_referenceTest() throws Exception { Object[][] tests = { {"(?R)", PCRELexer.NUMBERED_REFERENCE_ABSOLUTE}, {"(?123)", PCRELexer.NUMBERED_REFERENCE_ABSOLUTE}, {"(?+123)", PCRELexer.NUMBERED_REFERENCE_RELATIVE_PLUS}, {"(?-123)", PCRELexer.NUMBERED_REFERENCE_RELATIVE_MINUS}, {"(?&name)", PCRELexer.NAMED_REFERENCE_PERL}, {"(?P>name)", PCRELexer.NAMED_REFERENCE_PYTHON}, {"\\g<name>", PCRELexer.NAMED_REFERENCE_ONIGURUMA}, {"\\g'name'", PCRELexer.NAMED_REFERENCE_ONIGURUMA}, {"\\g<123>", PCRELexer.NUMBERED_REFERENCE_ABSOLUTE_ONIGURUMA}, {"\\g'123'", PCRELexer.NUMBERED_REFERENCE_ABSOLUTE_ONIGURUMA}, {"\\g<+123>", PCRELexer.NUMBERED_REFERENCE_RELATIVE_PLUS}, {"\\g'+123'", PCRELexer.NUMBERED_REFERENCE_RELATIVE_PLUS}, {"\\g<-123>", PCRELexer.NUMBERED_REFERENCE_RELATIVE_MINUS}, {"\\g'-123'", PCRELexer.NUMBERED_REFERENCE_RELATIVE_MINUS} }; for (Object[] test : tests) { String input = (String) test[0]; Integer expected = (Integer) test[1]; PCREParser parser = getParser(input); PCREParser.subroutine_reference_return value = parser.subroutine_reference(); assertThat(value, notNullValue()); CommonTree tree = (CommonTree) value.getTree(); assertThat(tree.getType(), is(expected)); } }
@Test public void conditionalTest() throws Exception { Object[][] tests = { {"(?(123)a)", PCRELexer.REFERENCE_CONDITION_ABSOLUTE}, {"(?(+123)a)", PCRELexer.REFERENCE_CONDITION_RELATIVE_PLUS}, {"(?(-123)a)", PCRELexer.REFERENCE_CONDITION_RELATIVE_MINUS}, {"(?(<name>)a)", PCRELexer.NAMED_REFERENCE_CONDITION_PERL}, {"(?('name')a)", PCRELexer.NAMED_REFERENCE_CONDITION_PERL}, {"(?(name)a)", PCRELexer.NAMED_REFERENCE_CONDITION}, {"(?(R)a)", PCRELexer.OVERALL_RECURSION_CONDITION}, {"(?(R123)a)", PCRELexer.SPECIFIC_GROUP_RECURSION_CONDITION}, {"(?(R&name)a)", PCRELexer.SPECIFIC_RECURSION_CONDITION}, {"(?(DEFINE)a)", PCRELexer.DEFINE}, {"(?(assert)a)", PCRELexer.ASSERT}, }; for (Object[] test : tests) { String input = (String) test[0]; Integer expected = (Integer) test[1]; PCREParser parser = getParser(input); PCREParser.conditional_return value = parser.conditional(); assertThat(value, notNullValue()); CommonTree tree = (CommonTree) value.getTree(); assertThat(tree.getType(), is(expected)); } }
@Test public void look_aroundTest() throws Exception { Object[][] tests = { {"(?=a)", PCRELexer.LOOK_AHEAD}, {"(?!b)", PCRELexer.NEGATIVE_LOOK_AHEAD}, {"(?<=c)", PCRELexer.LOOK_BEHIND}, {"(?<!d)", PCRELexer.NEGATIVE_LOOK_BEHIND} }; for (Object[] test : tests) { String input = (String) test[0]; Integer expected = (Integer) test[1]; PCREParser parser = getParser(input); PCREParser.look_around_return value = parser.look_around(); assertThat(value, notNullValue()); CommonTree tree = (CommonTree) value.getTree(); assertThat(tree.getType(), is(expected)); } }
@Test public void optionTest() throws Exception { Object[][] tests = { {"(*NO_START_OPT)", PCRELexer.OPTIONS_NO_START_OPT}, {"(*UTF8)", PCRELexer.OPTIONS_UTF8}, {"(*UTF16)", PCRELexer.OPTIONS_UTF16}, {"(*UCP)", PCRELexer.OPTIONS_UCP}, {"(?mis)", PCRELexer.OPTIONS}, {"(?-Jx)", PCRELexer.OPTIONS}, {"(?m-isx)", PCRELexer.OPTIONS} }; for (Object[] test : tests) { String input = (String) test[0]; Integer expected = (Integer) test[1]; PCREParser parser = getParser(input); PCREParser.option_return value = parser.option(); assertThat(value, notNullValue()); CommonTree tree = (CommonTree) value.getTree(); assertThat(tree.getType(), is(expected)); } }
private String getCoreSQL(RuleReturnScope parsedSQL) { final CommonTree ast = (CommonTree) parsedSQL.getTree(); final CommonToken nameToken = (CommonToken) ((CommonTree) ast.getChild(1)).getToken(); final CharStream inputStream = nameToken.getInputStream(); final CommonToken stopToken = (CommonToken) parsedSQL.getStop(); return inputStream.substring(nameToken.getStartIndex(), stopToken.getStopIndex()); }
/** * @param sql * @param alterTableDef * @return * @throws SqlJetException */ private String getTableAlteredSql(String sql, SqlJetAlterTableDef alterTableDef) throws SqlJetException { final RuleReturnScope parsedSQL = parseTable(sql); final CommonTree ast = (CommonTree) parsedSQL.getTree(); final CommonToken nameToken = (CommonToken) ((CommonTree) ast.getChild(1)).getToken(); final CharStream inputStream = nameToken.getInputStream(); final CommonToken stopToken = (CommonToken) parsedSQL.getStop(); final StringBuilder b = new StringBuilder(); if (alterTableDef.getNewTableName() != null) { b.append(inputStream.substring(0, nameToken.getStartIndex() - 1)); b.append(getAlterTableName(alterTableDef)); b.append(inputStream.substring(nameToken.getStopIndex() + 1, stopToken.getStopIndex())); } else if (alterTableDef.getNewColumnDef() != null) { b.append(inputStream.substring(0, stopToken.getStartIndex() - 1)); b.append(",").append(getAlterTableName(alterTableDef)); b.append(inputStream.substring(stopToken.getStartIndex(), stopToken.getStopIndex())); } else { throw new SqlJetException("Wrong ALTER TABLE statement"); } return b.toString(); }
/* * Adding one to stopIndex from Tokens. ANTLR defines the char position as * being the array index of the actual characters. Most tools these days * define document offsets as the positions between the characters. If you * imagine drawing little boxes around each character and think of the * numbers as pointing to either the left or right side of a character's * box, then 0 is before the first character - and in a Document of 10 * characters, position 10 is after the last character. */ public int getCharStopIndex() { if (charStopIndex == -1 && node.getToken() != null) { return ((CommonToken) node.getToken()).getStopIndex() + 1; } return charStopIndex; }
private void executeCount(CommonTree ast) throws TException, InvalidRequestException, UnavailableException, TimedOutException, UnsupportedEncodingException { if (!CliMain.isConnected()) return; int childCount = ast.getChildCount(); assert (childCount == 1); CommonTree columnFamilySpec = (CommonTree) ast.getChild(0); if (!(columnFamilySpec.getType() == CliParser.NODE_COLUMN_ACCESS)) return; String tableName = CliCompiler.getTableName(columnFamilySpec); String key = CliCompiler.getKey(columnFamilySpec); String columnFamily = CliCompiler.getColumnFamily(columnFamilySpec); int columnSpecCnt = CliCompiler.numColumnSpecifiers(columnFamilySpec); ColumnParent colParent; if (columnSpecCnt == 0) { colParent = createColumnParent(columnFamily, null); } else { assert (columnSpecCnt == 1); colParent = createColumnParent( columnFamily, CliCompiler.getColumn(columnFamilySpec, 0).getBytes("UTF-8")); } int count = thriftClient_.get_count(tableName, key, colParent, ConsistencyLevel.ONE); css_.out.printf("%d columns\n", count); }
private VOpNode constructVBOpNode(VOperator vOp, CommonTree tree) { if (checkNode(tree)) { ExprNode expr = constructExprNode((CommonTree) tree.getChild(0)); return new VOpNode(vOp, expr, tree.getLine()); } return null; }
private VBOpNode constructVBOpNode(VBoolOperator vBOp, CommonTree tree) { if (!checkNode(tree)) { return null; } BExprNode bExpr = constructBExprNode((CommonTree) tree.getChild(0)); return new VBOpNode(vBOp, bExpr, tree.getLine()); }
/** * Walks through the CommonTree generating an exact copy but in ASTree form * * @return the root node of the new ASTree * @throws TreeWalkerException only occurs if the parser has let the incorrect input through */ public ConstructedASTree constructASTree() { RuleSequenceNode constructedTree = null; // needs to be removed if ASTrees are not constructed from a base if (!checkNode(antlrTree)) { return new ConstructedASTree(null, exceptions); } List<?> childRules = antlrTree.getChildren(); RuleSequenceNode currentSeq = null; for (Object c : childRules) { CommonTree child = (CommonTree) c; if (checkNode(child)) { Token childToken = child.getToken(); if (childToken.getType() != BACONParser.RULE) { // TODO - Sort out line/char pos exceptions.add(new TreeWalkerException("Expected a rule token", 999, 999)); } RuleSequenceNode ruleSeq = constructRuleSeqNode(child); if (currentSeq != null) { currentSeq.setRuleSequence(ruleSeq); currentSeq = ruleSeq; } else { currentSeq = ruleSeq; constructedTree = ruleSeq; } } } return new ConstructedASTree(constructedTree, exceptions); }
@Override protected void checkNode(CommonTree treeNode) throws ReadingException { if (treeNode.getChildCount() != valueTranslators.size()) { throw new ReadingException( i18n("ERR_TRANSLATE_ELEMENTS_COUNT", valueTranslators.size(), treeNode.getChildCount()), treeNode); } }
private BinaryPrimitiveNode constructBinPrimNode(BinaryPrimitive prim, CommonTree tree) { if (checkNode(tree)) { ExprNode lExpr = constructExprNode((CommonTree) tree.getChild(0)); ExprNode rExpr = constructExprNode((CommonTree) tree.getChild(1)); return new BinaryPrimitiveNode(prim, lExpr, rExpr, tree.getLine()); } return null; }
private BinOpNode constructBinOpNode(MathematicalOperator op, CommonTree tree) { if (checkNode(tree)) { ExprNode lExpr = constructExprNode((CommonTree) tree.getChild(0)); ExprNode rExpr = constructExprNode((CommonTree) tree.getChild(1)); return new BinOpNode(op, lExpr, rExpr, tree.getLine()); } return null; }
private BooleanComparitorNode constructBooleanCompNode(ComparisonOperator comp, CommonTree tree) { if (!checkNode(tree)) { return null; } ExprNode lExpr = constructExprNode((CommonTree) tree.getChild(0)); ExprNode rExpr = constructExprNode((CommonTree) tree.getChild(1)); return new BooleanComparitorNode(comp, lExpr, rExpr, tree.getLine()); }
private BooleanBinOpNode constructBooleanBinOpNode(BooleanBinOperator binOp, CommonTree tree) { if (!checkNode(tree)) { return null; } BExprNode lBExpr = constructBExprNode((CommonTree) tree.getChild(0)); BExprNode rBExpr = constructBExprNode((CommonTree) tree.getChild(1)); return new BooleanBinOpNode(binOp, lBExpr, rBExpr, tree.getLine()); }
private BinaryFunctionNode constructBinFuncNode(BinaryFunction binFunc, CommonTree tree) { if (!checkNode(tree)) { return null; } IdNode id = constructIdNode((CommonTree) tree.getChild(0)); ExprNode expr = constructExprNode((CommonTree) tree.getChild(1)); return new BinaryFunctionNode(binFunc, id, expr, tree.getLine()); }
private AssignNode constructAssignNode(CommonTree tree) { if (!checkNode(tree)) { return null; } IdNode id = constructIdNode((CommonTree) tree.getChild(0)); ExprNode expr = constructExprNode((CommonTree) tree.getChild(1)); return new AssignNode(id, expr, tree.getLine()); }
@Test public void testDoubleLevelTree() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("(A (B C) (B D) E)"); String found = t.toStringTree(); String expecting = "(A (B C) (B D) E)"; assertEquals(expecting, found); }
@Test public void testListTree() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("(nil A B C)"); String found = t.toStringTree(); String expecting = "A B C"; assertEquals(expecting, found); }
@Test public void testSingleNodeWithArg() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("ID[foo]"); String found = t.toStringTree(); String expecting = "foo"; assertEquals(expecting, found); }