/**
   * Test method for {@link
   * org.drools.rule.builder.dialect.java.JavaExprAnalyzer#analyzeBlock(java.lang.String,
   * java.util.Set[])}.
   */
  @Test
  public void testAnalyzeBlock() {
    JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
    String codeBlock =
        "int x;\n"
            + "Cheese cheese = new Cheese();\n"
            + "for( Iterator it = list.iterator(); it.hasNext(); ) {\n"
            + "    int shouldNotBeIncluded = 1;\n"
            + "}\n"
            + "{\n"
            + "    String anotherNonTopLevelVar = \"test\";\n"
            + "}\n"
            + "double thisIsAGoodVar = 0;\n"
            + "method();\n";
    try {

      JavaAnalysisResult analysis =
          analyzer.analyzeBlock(
              codeBlock,
              new BoundIdentifiers(
                  new HashMap<String, Class<?>>(), new HashMap<String, Class<?>>()));
      Set<String> vars = analysis.getLocalVariables();

      assertEquals(3, vars.size());
      assertTrue(vars.contains("x"));
      assertTrue(vars.contains("cheese"));
      assertTrue(vars.contains("thisIsAGoodVar"));

    } catch (RecognitionException e) {
      e.printStackTrace();
      fail("Not supposed to raise exception: " + e.getMessage());
    }
  }
Exemplo n.º 2
0
  /**
   * Translate antlr internal exceptions to sane flume data flow configuration specific messages.
   */
  @Override
  public String getMessage() {
    if (re instanceof NoViableAltException) {
      NoViableAltException nvae = (NoViableAltException) re;
      String c = StringEscapeUtils.escapeJava("" + (char) nvae.c);
      return "Lexer error at char '"
          + c
          + "' at line "
          + nvae.line
          + " char "
          + nvae.charPositionInLine;
    }

    if (re instanceof MismatchedTokenException) {
      MismatchedTokenException mte = (MismatchedTokenException) re;
      String token = (mte.token == null) ? "\"\"" : mte.token.getText();

      return "Parser error: unexpected '"
          + token
          + "' at position "
          + mte.charPositionInLine
          + " line "
          + mte.line
          + ": '"
          + mte.input
          + "'";
    }

    return "Unknown RecognitionException: " + re.getMessage();
  }
Exemplo n.º 3
0
 public FelNode parse(String exp) {
   if (exp == null || "".equals(exp)) {
     return null;
   }
   ByteArrayInputStream is = new ByteArrayInputStream(exp.getBytes());
   ANTLRInputStream input = null;
   try {
     input = new ANTLRInputStream(is);
   } catch (IOException e) {
     throw new ParseException(FelException.getCauseMessage(e), e);
   }
   FelLexer lexer = new FelLexer(input);
   CommonTokenStream tokens = new CommonTokenStream(lexer);
   FelParser parser = new FelParser(tokens);
   parser.setTreeAdaptor(adaptor);
   ParserRuleReturnScope r = null;
   try {
     r = parser.program();
   } catch (RecognitionException e) {
     throw new ParseException(e.getMessage(), e);
   }
   if (r != null) {
     Object tree = r.getTree();
     if (tree instanceof FelNode) {
       initFun((FelNode) tree);
       return (FelNode) tree;
     }
   }
   return null;
 }
Exemplo n.º 4
0
  @Override
  public Object call(ExecutionContext context, Object self, Object... args) {
    // 15.3.2.1
    int numArgs = args.length;
    String body = "";
    if (numArgs > 0) {
      body = Types.toString(context, args[numArgs - 1]);
    }

    StringBuffer formalParams = new StringBuffer();
    boolean first = true;

    Set<String> seenParams = new HashSet<>();
    boolean duplicateFormalParams = false;

    for (int i = 0; i < numArgs - 1; ++i) {
      if (!first) {
        formalParams.append(",");
      }
      String param = Types.toString(context, args[i]);
      if (seenParams.contains(param)) {
        duplicateFormalParams = true;
      }
      seenParams.add(param);
      formalParams.append(param);
      first = false;
    }

    StringBuffer code = new StringBuffer();
    code.append("function(" + formalParams.toString() + "){\n");
    code.append(body);
    code.append("}");

    try {
      FunctionDescriptor descriptor = parseFunction(context, code.toString());
      JSCompiler compiler = context.getGlobalObject().getCompiler();
      JSFunction function =
          compiler.compileFunction(
              context, descriptor.getFormalParameters(), descriptor.getBlock(), false);
      if (function.isStrict() && duplicateFormalParams) {
        throw new ThrowException(
            context,
            context.createSyntaxError("duplicate formal parameters in function definition"));
      }
      function.setPrototype(getPrototype());
      return function;
    } catch (RecognitionException e) {
      throw new ThrowException(context, context.createSyntaxError(e.getMessage()));
    }
  }
Exemplo n.º 5
0
 @Test
 public void testBaseUid() {
   CharStream stream = new ANTLRStringStream("Tellurium");
   UdlLexer lexer = new UdlLexer(stream);
   TokenStream tokenStream = new CommonTokenStream(lexer);
   UdlParser parser = new UdlParser(tokenStream);
   try {
     MetaData data = parser.uid();
     assertNotNull(data);
     assertEquals("Tellurium", data.getId());
   } catch (RecognitionException e) {
     fail(e.getMessage());
   }
 }
Exemplo n.º 6
0
 /** Load template stream into this group */
 public CompiledST loadTemplateFile(String prefix, String fileName, CharStream templateStream) {
   GroupLexer lexer = new GroupLexer(templateStream);
   CommonTokenStream tokens = new CommonTokenStream(lexer);
   GroupParser parser = new GroupParser(tokens);
   parser.group = this;
   lexer.group = this;
   try {
     parser.templateDef(prefix);
   } catch (RecognitionException re) {
     errMgr.groupSyntaxError(ErrorType.SYNTAX_ERROR, fileName, re, re.getMessage());
   }
   String templateName = Misc.getFileNameNoSuffix(fileName);
   if (prefix != null && prefix.length() > 0) templateName = prefix + "/" + templateName;
   return rawGetTemplate(templateName);
 }
Exemplo n.º 7
0
 @Test
 public void testTableHeaderUidNoId() {
   CharStream stream = new ANTLRStringStream("{header: 3}");
   UdlLexer lexer = new UdlLexer(stream);
   TokenStream tokenStream = new CommonTokenStream(lexer);
   UdlParser parser = new UdlParser(tokenStream);
   try {
     MetaData data = parser.uid();
     assertNotNull(data);
     assertTrue(data instanceof TableHeaderMetaData);
     TableHeaderMetaData th = (TableHeaderMetaData) data;
     assertEquals("_3", th.getId());
     assertEquals("3", th.getIndex().getValue());
     assertEquals(IndexType.VAL, th.getIndex().getType());
   } catch (RecognitionException e) {
     fail(e.getMessage());
   }
 }
Exemplo n.º 8
0
 @Test
 public void testListUidNoId() {
   CharStream stream = new ANTLRStringStream("{10}");
   UdlLexer lexer = new UdlLexer(stream);
   TokenStream tokenStream = new CommonTokenStream(lexer);
   UdlParser parser = new UdlParser(tokenStream);
   try {
     MetaData data = parser.uid();
     assertNotNull(data);
     assertEquals("_10", data.getId());
     assertTrue(data instanceof ListMetaData);
     ListMetaData lm = (ListMetaData) data;
     assertEquals("10", lm.getIndex().getValue());
     assertEquals(IndexType.VAL, lm.getIndex().getType());
   } catch (RecognitionException e) {
     fail(e.getMessage());
   }
 }
Exemplo n.º 9
0
 public void generateDependencies(String text, EGTask task) throws Exception {
   try {
     this.task = task;
     DependenciesLexer lex = new DependenciesLexer(new ANTLRStringStream(text));
     CommonTokenStream tokens = new CommonTokenStream(lex);
     DependenciesParser g = new DependenciesParser(tokens);
     try {
       g.setGenerator(this);
       g.prog();
     } catch (RecognitionException ex) {
       logger.error("Unable to load mapping task: " + ex.getMessage());
       throw new ParserException(ex);
     }
   } catch (Exception e) {
     e.printStackTrace();
     logger.error(e.getLocalizedMessage());
     throw new ParserException(e);
   }
 }
Exemplo n.º 10
0
 @Test
 public void testTableBodyMixedUid() {
   try {
     MetaData data = UidParser.parse("{row:3, column -> bad} as Search");
     assertNotNull(data);
     assertEquals("Search", data.getId());
     assertTrue(data instanceof TableBodyMetaData);
     TableBodyMetaData tbmd = (TableBodyMetaData) data;
     assertEquals("1", tbmd.getTbody().getValue());
     assertEquals(IndexType.VAL, tbmd.getTbody().getType());
     assertEquals("3", tbmd.getRow().getValue());
     assertEquals(IndexType.VAL, tbmd.getRow().getType());
     assertEquals("bad", tbmd.getColumn().getValue());
     assertEquals(IndexType.REF, tbmd.getColumn().getType());
   } catch (RecognitionException e) {
     e.printStackTrace();
     fail(e.getMessage());
   }
 }
Exemplo n.º 11
0
 @Test
 public void testTableBodyRefUid() {
   CharStream stream = new ANTLRStringStream("{tbody : 1, row -> good, column -> bad} as Search");
   UdlLexer lexer = new UdlLexer(stream);
   TokenStream tokenStream = new CommonTokenStream(lexer);
   UdlParser parser = new UdlParser(tokenStream);
   try {
     MetaData data = parser.uid();
     assertNotNull(data);
     assertEquals("Search", data.getId());
     assertTrue(data instanceof TableBodyMetaData);
     TableBodyMetaData tbmd = (TableBodyMetaData) data;
     assertEquals("1", tbmd.getTbody().getValue());
     assertEquals(IndexType.VAL, tbmd.getTbody().getType());
     assertEquals("good", tbmd.getRow().getValue());
     assertEquals(IndexType.REF, tbmd.getRow().getType());
     assertEquals("bad", tbmd.getColumn().getValue());
     assertEquals(IndexType.REF, tbmd.getColumn().getType());
   } catch (RecognitionException e) {
     fail(e.getMessage());
   }
 }
Exemplo n.º 12
0
 @Test
 public void testTableBodyValUidNoId() {
   CharStream stream = new ANTLRStringStream("{tbody : 1, row : 2, column : 3}");
   UdlLexer lexer = new UdlLexer(stream);
   TokenStream tokenStream = new CommonTokenStream(lexer);
   UdlParser parser = new UdlParser(tokenStream);
   try {
     MetaData data = parser.uid();
     assertNotNull(data);
     assertEquals("_1_2_3", data.getId());
     assertTrue(data instanceof TableBodyMetaData);
     TableBodyMetaData tb = (TableBodyMetaData) data;
     assertEquals("1", tb.getTbody().getValue());
     assertEquals(IndexType.VAL, tb.getTbody().getType());
     assertEquals("2", tb.getRow().getValue());
     assertEquals(IndexType.VAL, tb.getRow().getType());
     assertEquals("3", tb.getColumn().getValue());
     assertEquals(IndexType.VAL, tb.getColumn().getType());
   } catch (RecognitionException e) {
     fail(e.getMessage());
   }
 }
Exemplo n.º 13
0
 public static ILockingHandler createLock(String config) {
   try {
     LGenParser parser = parseConfig(config);
     switch (parser.getStore().getType()) {
       case LGenLexer.MONGODB:
         boolean useMongo2 =
             Boolean.parseBoolean(
                 MultiValueConfigLoader.getConfig("MONGODB-lock.useVersion2", "false"));
         if (useMongo2) {
           return getLockStore(
               "rapture.lock.mongodb.MongoLockHandler2", parser.getConfig().getConfig());
         } else {
           return getLockStore(
               "rapture.lock.mongodb.MongoLockHandler", parser.getConfig().getConfig());
         }
       case LGenLexer.MEMORY:
         return getLockStore(
             "rapture.lock.memory.MemoryLockingHandler", parser.getConfig().getConfig());
       case LGenLexer.DUMMY:
         return getLockStore(
             "rapture.lock.dummy.DummyLockHandler", parser.getConfig().getConfig());
       case LGenLexer.REDIS:
         return getLockStore(
             "rapture.lock.redis.RedisLockHandler", parser.getConfig().getConfig());
       case LGenLexer.ZOOKEEPER:
         return getLockStore(
             "rapture.lock.zookeeper.ZooKeeperLockHandler", parser.getConfig().getConfig());
       case LGenLexer.ETCD:
         return getLockStore("rapture.lock.etcd.ETCDLockHandler", parser.getConfig().getConfig());
         // TODO case LGenLexer.FILE: ?
     }
   } catch (RecognitionException e) {
     log.error("Error parsing config - " + e.getMessage());
   }
   return null;
 }