public static <BaseClass, Element> IFilter<BaseClass, Element> makeFilter(CommonTree tree) {
    String tokenText = getTokenText(tree);
    if (tokenText == null) return null;
    char firstChar = tokenText.charAt(0);
    if (AND.equals(tokenText)) {
      CommonTree child0 = (CommonTree) tree.getChild(0);
      CommonTree child1 = (CommonTree) tree.getChild(1);
      IFilter<BaseClass, Element> filter0 = makeFilter(child0);
      IFilter<BaseClass, Element> filter1 = makeFilter(child1);
      return new AndFilter<BaseClass, Element>(filter0, filter1);
    } else if (OR.equals(tokenText)) {
      CommonTree child0 = (CommonTree) tree.getChild(0);
      CommonTree child1 = (CommonTree) tree.getChild(1);
      IFilter<BaseClass, Element> filter0 = makeFilter(child0);
      IFilter<BaseClass, Element> filter1 = makeFilter(child1);
      return new OrFilter<BaseClass, Element>(filter0, filter1);
    } else if (NOT.equals(tokenText)) {
      CommonTree child0 = (CommonTree) tree.getChild(0);
      IFilter<BaseClass, Element> filter0 = makeFilter(child0);
      return new NotFilter<BaseClass, Element>(filter0);
    } else if (CONSTRAINT.equals(tokenText)) {
      CommonTree child0 = (CommonTree) tree.getChild(0);
      CommonTree child1 = null;
      if (tree.getChildCount() == 1) {
        return makeFilter(getTokenText(child0));
      } else if (tree.getChildCount() == 2) {
        child1 = (CommonTree) tree.getChild(1);
        return makeFilter(getTokenText(child0), parseCronstraintParameterTree(child1));
      }
      return null;
    } else {

    }
    return null;
  }
Ejemplo n.º 2
0
 @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);
   }
 }
Ejemplo n.º 3
0
  public WhileStatement(CommonTree MyTree, Model MyModel) {
    setLine(MyTree.getLine());
    this.MyModel = MyModel;

    Comparison = ExpressionFactory.getExpression((CommonTree) MyTree.getChild(0), MyModel);
    if (MyTree.getChildCount() > 1) {
      for (int i = 1; i < MyTree.getChildCount(); i++)
        Statements.add(StatementFactory.getStatement((CommonTree) MyTree.getChild(i), MyModel));
    }
  }
Ejemplo n.º 4
0
  public SqlJetTriggerDef(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();
    tableName = nameNode.getChildCount() > 0 ? nameNode.getChild(0).getText() : null;
    databaseName = nameNode.getChildCount() > 1 ? nameNode.getChild(1).getText() : null;
  }
Ejemplo n.º 5
0
  private int getElementOffset(CommonTree tree) {
    switch (tree.getType()) {
      case ANTLRParser.MODE:
      case ANTLRParser.ASSIGN:
      case ANTLRParser.RULE:
        if (tree.getChildCount() > 0 && tree.getChild(0) instanceof CommonTree) {
          CommonTree child = (CommonTree) tree.getChild(0);
          if (child.getToken() instanceof CommonToken) {
            CommonToken token = (CommonToken) child.getToken();
            return token.getStartIndex();
          }
        }

        break;

      case ANTLRParser.ID:
        break;

      default:
        throw new UnsupportedOperationException();
    }

    if (tree.getToken() instanceof CommonToken) {
      return ((CommonToken) tree.getToken()).getStartIndex();
    }

    return 0;
  }
Ejemplo n.º 6
0
  // 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.");
    }
  }
Ejemplo n.º 7
0
  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);
  }
Ejemplo n.º 8
0
  @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());
    }
  }
Ejemplo n.º 9
0
 @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);
   }
 }
Ejemplo n.º 10
0
  @Test
  public void character_classTest() throws Exception {

    String[] tests = {"[^\\da-z]", "[^^]", "[^]^]", "[^\\]^]"};

    for (String test : tests) {

      PCREParser parser = getParser(test);
      PCREParser.character_class_return value = parser.character_class();

      assertThat(value, notNullValue());

      CommonTree tree = (CommonTree) value.getTree();

      assertThat(tree.getType(), is(PCRELexer.NEGATED_CHARACTER_CLASS));
    }

    tests = new String[] {"[\\da-z]", "[\\^]", "[]^]", "[\\]^]"};

    for (String test : tests) {

      PCREParser parser = getParser(test);
      PCREParser.character_class_return value = parser.character_class();

      assertThat(value, notNullValue());

      CommonTree tree = (CommonTree) value.getTree();

      assertThat(tree.getType(), is(PCRELexer.CHARACTER_CLASS));
    }

    PCREParser parser = getParser("[]-\\]]"); // valid range!
    PCREParser.character_class_return value = parser.character_class();

    assertThat(value, notNullValue());

    CommonTree tree = (CommonTree) value.getTree();

    assertThat(tree.getChildCount(), is(1));
    assertThat(tree.getType(), is(PCRELexer.CHARACTER_CLASS));

    CommonTree rangeNode = (CommonTree) tree.getChild(0);
    assertThat(rangeNode.getChildCount(), is(2));
    assertThat(rangeNode.getType(), is(PCRELexer.RANGE));
  }
Ejemplo n.º 11
0
 /**
  * @param alterTableDef
  * @return
  */
 private String getAlterTableName(SqlJetAlterTableDef alterTableDef) {
   final ParserRuleReturnScope parsedSql = alterTableDef.getParsedSql();
   final CommonTree ast = (CommonTree) parsedSql.getTree();
   final CommonToken stopToken = (CommonToken) parsedSql.getStop();
   final CommonToken nameToken =
       (CommonToken) ((CommonTree) ast.getChild(ast.getChildCount() - 1)).getToken();
   final CharStream inputStream = nameToken.getInputStream();
   return inputStream.substring(nameToken.getStartIndex(), stopToken.getStopIndex());
 }
Ejemplo n.º 12
0
 @Override
 public ArrayList<V> fromASTNode(CommonTree treeNode) throws ReadingException {
   if (treeNode.token != null && treeNode.token.getType() == headTokenType) {
     checkNode(treeNode);
     if (treeNode.getChildCount() < 1) {
       return null;
     }
     ArrayList<V> objs = new ArrayList<>(treeNode.getChildCount());
     int i = 0;
     for (Object obj : treeNode.getChildren()) {
       objs.add(fromASTChildAt(i++, (CommonTree) obj));
     }
     return objs;
   }
   throw new ReadingException(
       i18n("ERR_TRANSLATE_UNEXPECTED_ELEMENT", i18n(i18n("ERR_TRANSLATE_LIST_OR_SLIST"))),
       treeNode);
 }
  @Override
  public void transform(final CommonTree ast) {
    LOGGER.info("DatatypeInfoMapper.transform..." + ast);
    namespaceStack.clear();

    this.datatypeInfos = new ArrayList<DatatypeInfo>(ast.getChildCount());

    try {
      for (int i = 0; i < ast.getChildCount(); i++) {
        walk(ast.getChild(i), null);
      }
    } catch (RuntimeException e) {
      LOGGER.error(e);
      throw e;
    }

    LOGGER.info("DONE: " + references.size() + " loaded.");
  }
Ejemplo n.º 14
0
  private void executeDelete(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);

    byte[] superColumnName = null;
    byte[] columnName = null;
    boolean isSuper;

    try {
      if (!(getCFMetaData(tableName).containsKey(columnFamily))) {
        css_.out.println("No such column family: " + columnFamily);
        return;
      }

      isSuper =
          getCFMetaData(tableName).get(columnFamily).get("Type").equals("Super") ? true : false;
    } catch (NotFoundException nfe) {
      css_.out.printf("No such keyspace: %s\n", tableName);
      return;
    }

    if ((columnSpecCnt < 0) || (columnSpecCnt > 2)) {
      css_.out.println("Invalid row, super column, or column specification.");
      return;
    }

    if (columnSpecCnt == 1) {
      // table.cf['key']['column']
      if (isSuper) superColumnName = CliCompiler.getColumn(columnFamilySpec, 0).getBytes("UTF-8");
      else columnName = CliCompiler.getColumn(columnFamilySpec, 0).getBytes("UTF-8");
    } else if (columnSpecCnt == 2) {
      // table.cf['key']['column']['column']
      superColumnName = CliCompiler.getColumn(columnFamilySpec, 0).getBytes("UTF-8");
      columnName = CliCompiler.getColumn(columnFamilySpec, 1).getBytes("UTF-8");
    }

    thriftClient_.remove(
        tableName,
        key,
        createColumnPath(columnFamily, superColumnName, columnName),
        timestampMicros(),
        ConsistencyLevel.ONE);
    css_.out.println(String.format("%s removed.", (columnSpecCnt == 0) ? "row" : "column"));
  }
Ejemplo n.º 15
0
 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;
   }
 }
Ejemplo n.º 16
0
  /*
   * def test_variable_end
   *   template = Liquid::Template.parse("  {{funk}}")
   *   assert_equal 2, template.root.nodelist.size
   *   assert_equal String, template.root.nodelist[0].class
   *   assert_equal Variable, template.root.nodelist[1].class
   * end
   */
  @Test
  public void variableEndTest() throws RecognitionException {

    CommonTree root = Template.parse("  {{funk}}").getAST();

    assertThat(root.getChildCount(), is(2));

    assertThat(root.getChild(0).getType(), is(LiquidLexer.PLAIN));
    assertThat(root.getChild(1).getChild(0).getType(), is(LiquidLexer.LOOKUP));
  }
Ejemplo n.º 17
0
  private static String getChildText(CommonTree tree) {

    StringBuilder builder = new StringBuilder();

    for (int i = 0; i < tree.getChildCount(); i++) {
      builder.append(tree.getChild(i).getText());
    }

    return builder.toString();
  }
  private static String getVariableName(CommonTree t, StringBuilder sb) {
    if (t != null) {
      for (int i = 0; i < t.getChildCount(); i++) {
        sb.append(t.getChild(i).toString());
        getVariableName((CommonTree) t.getChild(i), sb);
      }
    }

    return sb.toString();
  }
Ejemplo n.º 19
0
 @Override
 public Map<String, Entry<V, CommonTree>> fromASTNode(CommonTree treeNode)
     throws ReadingException {
   if (treeNode.token != null && treeNode.token.getType() == headTokenType) {
     if (treeNode.getChildCount() < 1) {
       return null;
     }
     HashMap<String, Entry<V, CommonTree>> kVals = new HashMap<>();
     for (Object obj : treeNode.getChildren()) {
       CommonTree node = (CommonTree) obj;
       if (node.token != null
           && node.token.getType() == SpiderDiagramsParser.PAIR
           && node.getChildCount() == 2) {
         String key = IDTranslator.Instance.fromASTNode((CommonTree) node.getChild(0));
         ElementTranslator<? extends V> translator = null;
         if (typedValueTranslators != null) {
           translator = typedValueTranslators.get(key);
         }
         if (translator == null) {
           if (defaultValueTranslator != null) {
             translator = defaultValueTranslator;
           } else {
             throw new ReadingException(
                 i18n(
                     "ERR_TRANSLATE_UNEXPECTED_KEY_VALUE",
                     key,
                     typedValueTranslators == null ? "" : typedValueTranslators.keySet()),
                 (CommonTree) node.getChild(0));
           }
         }
         V value = translator.fromASTNode((CommonTree) node.getChild(1));
         kVals.put(key, new SimpleEntry<>(value, node));
       } else {
         throw new ReadingException(
             i18n("ERR_TRANSLATE_UNEXPECTED_ELEMENT", i18n("TRANSLATE_KEY_VALUE_PAIR")), node);
       }
     }
     return kVals;
   }
   throw new ReadingException(
       i18n("ERR_TRANSLATE_UNEXPECTED_ELEMENT", i18n(i18n("ERR_TRANSLATE_LIST_OR_SLIST"))),
       treeNode);
 }
Ejemplo n.º 20
0
 /**
  * @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;
 }
Ejemplo n.º 21
0
  /*
   * def test_with_block
   *   template = Liquid::Template.parse("  {% comment %} {% endcomment %} ")
   *   assert_equal [String, Comment, String], block_types(template.root.nodelist)
   *   assert_equal 3, template.root.nodelist.size
   * end
   */
  @Test
  public void blockTest() throws RecognitionException {

    CommonTree root = Template.parse("  {% comment %} {% endcomment %} ").getAST();

    assertThat(root.getChildCount(), is(3));

    assertThat(root.getChild(0).getType(), is(LiquidLexer.PLAIN));
    assertThat(root.getChild(1).getType(), is(LiquidLexer.COMMENT));
    assertThat(root.getChild(2).getType(), is(LiquidLexer.PLAIN));
  }
Ejemplo n.º 22
0
  /**
   * 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);
      }
    }
  }
  private static String getOneLevel(CommonTree t) {
    StringBuilder sb = new StringBuilder();

    if (t != null) {
      for (int i = 0; i < t.getChildCount(); i++) {
        sb.append(t.getChild(i).toString());
      }
    }

    return sb.toString();
  }
Ejemplo n.º 24
0
  @Test
  public void lettersTest() throws Exception {

    String source = "abc)";
    PCREParser parser = getParser(source);

    PCREParser.alpha_nums_return value = parser.alpha_nums();
    CommonTree tree = (CommonTree) value.getTree();

    assertThat(tree.getChildCount(), is(3));
  }
Ejemplo n.º 25
0
  @Test
  public void digitsTest() throws Exception {

    String source = "0123456789";
    PCREParser parser = getParser(source);

    PCREParser.digits_return value = parser.digits();
    CommonTree tree = (CommonTree) value.getTree();

    assertThat(tree.getChildCount(), is(10));
  }
Ejemplo n.º 26
0
  @Test
  public void non_close_parensTest() throws Exception {

    String source = "abc)";
    PCREParser parser = getParser(source);

    PCREParser.non_close_parens_return value = parser.non_close_parens();
    CommonTree tree = (CommonTree) value.getTree();

    assertThat(tree.getChildCount(), is(3));
  }
Ejemplo n.º 27
0
  @Test
  public void option_flagsTest() throws Exception {

    PCREParser parser = getParser("iJmsUx???");
    PCREParser.option_flags_return value = parser.option_flags();

    assertThat(value, notNullValue());

    CommonTree tree = (CommonTree) value.getTree();

    assertThat(tree.getChildCount(), is(6));
  }
 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));
 }
Ejemplo n.º 29
0
  @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));
    }
  }
 /*
  * Each of these are individual rules for constructing different types of ASTreeNodes
  */
 private RuleSequenceNode constructRuleSeqNode(CommonTree tree) {
   if (checkNode(tree)) {
     if (tree.getChildCount() == 1) {
       RuleNode rule = constructRuleNode((CommonTree) tree.getChild(0));
       return new RuleSequenceNode(rule);
     }
     CommonTree ruleNameNode = (CommonTree) tree.getChild(0);
     String ruleName = ruleNameNode.getToken().getText();
     RuleNode rule = constructRuleNode((CommonTree) tree.getChild(1));
     return new RuleSequenceNode(ruleName, rule);
   }
   return null;
 }