Java 类org.antlr.runtime.ANTLRStringStream 实例源码

项目:sstable-adaptor    文件:CQLFragmentParser.java   
/**
 * Just call a parser method in {@link CqlParser} - does not do any error handling.
 */
public static <R> R parseAnyUnhandled(CQLParserFunction<R> parserFunction, String input) throws RecognitionException
{
    // Lexer and parser
    ErrorCollector errorCollector = new ErrorCollector(input);
    CharStream stream = new ANTLRStringStream(input);
    CqlLexer lexer = new CqlLexer(stream);
    lexer.addErrorListener(errorCollector);

    TokenStream tokenStream = new CommonTokenStream(lexer);
    CqlParser parser = new CqlParser(tokenStream);
    parser.addErrorListener(errorCollector);

    // Parse the query string to a statement instance
    R r = parserFunction.parse(parser);

    // The errorCollector has queue up any errors that the lexer and parser may have encountered
    // along the way, if necessary, we turn the last error into exceptions here.
    errorCollector.throwFirstSyntaxError();

    return r;
}
项目:n4js    文件:ContentAssistContextFactory.java   
@Override
protected void createContextsForLastCompleteNode(EObject previousModel, boolean strict) {
    String currentNodePrefix = getPrefix(currentNode);
    if (!Strings.isEmpty(currentNodePrefix) && !currentNode.getText().equals(currentNodePrefix)) {
        lexer.setCharStream(new ANTLRStringStream(currentNodePrefix));
        Token token = lexer.nextToken();
        if (token == Token.EOF_TOKEN) { // error case - nothing could be parsed
            return;
        }
        while (token != Token.EOF_TOKEN) {
            if (isErrorToken(token))
                return;
            token = lexer.nextToken();
        }
    }
    String prefix = "";
    Collection<FollowElement> followElements = getParser().getFollowElements(rootNode, 0, completionOffset, strict);
    doCreateContexts(lastCompleteNode, currentNode, prefix, previousModel, followElements);
}
项目:QDrill    文件:TestEvaluationVisitor.java   
private LogicalExpression getExpr(String expr) throws Exception{
    ExprLexer lexer = new ExprLexer(new ANTLRStringStream(expr));
    CommonTokenStream tokens = new CommonTokenStream(lexer);

//    tokens.fill();
//    for(Token t : (List<Token>) tokens.getTokens()){
//      System.out.println(t + "" + t.getType());
//    }
//    tokens.rewind();

    ExprParser parser = new ExprParser(tokens);
    parse_return ret = parser.parse();

    return ret.e;

  }
项目:QDrill    文件:TreeTest.java   
private LogicalExpression parseExpression(String expr) throws RecognitionException, IOException{

    ExprLexer lexer = new ExprLexer(new ANTLRStringStream(expr));
    CommonTokenStream tokens = new CommonTokenStream(lexer);

//    tokens.fill();
//    for(Token t : (List<Token>) tokens.getTokens()){
//      System.out.println(t + "" + t.getType());
//    }
//    tokens.rewind();

    ExprParser parser = new ExprParser(tokens);
    parse_return ret = parser.parse();

    return ret.e;

  }
项目:dremio-oss    文件:TreeTest.java   
private LogicalExpression parseExpression(String expr) throws RecognitionException, IOException{

    ExprLexer lexer = new ExprLexer(new ANTLRStringStream(expr));
    CommonTokenStream tokens = new CommonTokenStream(lexer);

//    tokens.fill();
//    for(Token t : (List<Token>) tokens.getTokens()){
//      System.out.println(t + "" + t.getType());
//    }
//    tokens.rewind();

    ExprParser parser = new ExprParser(tokens);
    parse_return ret = parser.parse();

    return ret.e;

  }
项目:rb-bi    文件:QueryTestCase.java   
@Test
    public void Test1() throws RecognitionException {
        SiddhiQLGrammarLexer lexer = new SiddhiQLGrammarLexer();

        lexer.setCharStream(new ANTLRStringStream(
                "define stream `define` ( `string` string, price int, volume float );"));
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        SiddhiQLGrammarParser parser = new SiddhiQLGrammarParser(tokens);

        parser.executionPlan();
//        System.out.println("OK");
//        double float1 = 2d;
//        System.out.println(float1);
        // CommonTree t = (CommonTree) r.getTree();
        //
        // CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
        // nodes.setTokenStream(tokens);
        // SiddhiGrammarWalker walker = new SiddhiGrammarWalker(nodes);
        // return walker.siddhiGrammar(existingStreams);

    }
项目:rb-bi    文件:SiddhiCompiler.java   
public static List<ExecutionPlan> parse(String source) throws SiddhiParserException {
    try {
        SiddhiQLGrammarLexer lexer = new SiddhiQLGrammarLexer();
        lexer.setCharStream(new ANTLRStringStream(source));
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        SiddhiQLGrammarParser parser = new SiddhiQLGrammarParser(tokens);

        SiddhiQLGrammarParser.executionPlan_return r = parser.executionPlan();
        CommonTree t = (CommonTree) r.getTree();

        CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
        nodes.setTokenStream(tokens);
        SiddhiQLGrammarWalker walker = new SiddhiQLGrammarWalker(nodes);
        return walker.executionPlan();
    } catch (Throwable e) {
        throw new SiddhiParserException(e.getMessage(), e);
    }
}
项目:rb-bi    文件:SiddhiCompiler.java   
public static StreamDefinition parseStreamDefinition(String source) throws SiddhiParserException {
    try {
        SiddhiQLGrammarLexer lexer = new SiddhiQLGrammarLexer();
        lexer.setCharStream(new ANTLRStringStream(source));
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        SiddhiQLGrammarParser parser = new SiddhiQLGrammarParser(tokens);

        SiddhiQLGrammarParser.definitionStreamFinal_return r = parser.definitionStreamFinal();
        CommonTree t = (CommonTree) r.getTree();

        CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
        nodes.setTokenStream(tokens);
        SiddhiQLGrammarWalker walker = new SiddhiQLGrammarWalker(nodes);
        return walker.definitionStreamFinal();
    } catch (Throwable e) {
        throw new SiddhiParserException(e.getMessage(), e);
    }
}
项目:rb-bi    文件:SiddhiCompiler.java   
public static TableDefinition parseTableDefinition(String source) throws SiddhiParserException {
    try {
        SiddhiQLGrammarLexer lexer = new SiddhiQLGrammarLexer();
        lexer.setCharStream(new ANTLRStringStream(source));
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        SiddhiQLGrammarParser parser = new SiddhiQLGrammarParser(tokens);

        SiddhiQLGrammarParser.definitionTableFinal_return r = parser.definitionTableFinal();
        CommonTree t = (CommonTree) r.getTree();

        CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
        nodes.setTokenStream(tokens);
        SiddhiQLGrammarWalker walker = new SiddhiQLGrammarWalker(nodes);
        return walker.definitionTableFinal();
    } catch (Throwable e) {
        throw new SiddhiParserException(e.getMessage(), e);
    }
}
项目:rb-bi    文件:SiddhiCompiler.java   
public static PartitionDefinition parsePartitionDefinition(String source) throws SiddhiParserException {
    try {
        SiddhiQLGrammarLexer lexer = new SiddhiQLGrammarLexer();
        lexer.setCharStream(new ANTLRStringStream(source));
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        SiddhiQLGrammarParser parser = new SiddhiQLGrammarParser(tokens);

        SiddhiQLGrammarParser.definitionPartitionFinal_return r = parser.definitionPartitionFinal();
        CommonTree t = (CommonTree) r.getTree();

        CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
        nodes.setTokenStream(tokens);
        SiddhiQLGrammarWalker walker = new SiddhiQLGrammarWalker(nodes);
        return walker.definitionPartitionFinal();
    } catch (Throwable e) {
        throw new SiddhiParserException(e.getMessage(), e);
    }
}
项目:rb-bi    文件:SiddhiCompiler.java   
public static Query parseQuery(String source) throws SiddhiParserException {
    try {
        SiddhiQLGrammarLexer lexer = new SiddhiQLGrammarLexer();
        lexer.setCharStream(new ANTLRStringStream(source));
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        SiddhiQLGrammarParser parser = new SiddhiQLGrammarParser(tokens);

        SiddhiQLGrammarParser.queryFinal_return r = parser.queryFinal();
        CommonTree t = (CommonTree) r.getTree();

        CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
        nodes.setTokenStream(tokens);
        SiddhiQLGrammarWalker walker = new SiddhiQLGrammarWalker(nodes);
        return walker.queryFinal();
    } catch (Throwable e) {
        throw new SiddhiParserException(e.getMessage(), e);
    }
}
项目:rb-bi    文件:QueryTestCase.java   
@Test
    public void Test1() throws RecognitionException {
        SiddhiQLGrammarLexer lexer = new SiddhiQLGrammarLexer();

        lexer.setCharStream(new ANTLRStringStream(
                "define stream `define` ( `string` string, price int, volume float );"));
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        SiddhiQLGrammarParser parser = new SiddhiQLGrammarParser(tokens);

        parser.executionPlan();
//        System.out.println("OK");
//        double float1 = 2d;
//        System.out.println(float1);
        // CommonTree t = (CommonTree) r.getTree();
        //
        // CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
        // nodes.setTokenStream(tokens);
        // SiddhiGrammarWalker walker = new SiddhiGrammarWalker(nodes);
        // return walker.siddhiGrammar(existingStreams);

    }
项目:Rapture    文件:ApiChecks.java   
private void runTestFor(String script) throws RecognitionException {
    ReflexLexer lexer = new ReflexLexer();
    lexer.setCharStream(new ANTLRStringStream(script));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ReflexParser parser = new ReflexParser(tokens);
    CommonTree tree = (CommonTree) parser.parse().getTree();

    /*
     * DOTTreeGenerator gen = new DOTTreeGenerator(); StringTemplate st =
     * gen.toDOT(tree); System.out.println(st);
     */

    CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
    ReflexTreeWalker walker = new ReflexTreeWalker(nodes, parser.languageRegistry);
    HttpLoginApi loginApi = new HttpLoginApi("http://localhost:8665/rapture", new SimpleCredentialsProvider("rapture", "rapture"));
    loginApi.login();
    HttpUserApi userApi = new HttpUserApi(loginApi);
    ApiVersion version = userApi.getServerApiVersion();
    System.out.println("version is " + version.getMajor() + ":" + version.getMinor());

    walker.getReflexHandler().setApi(new ScriptClient(loginApi));

    ReflexNode returned = walker.walk();
    System.out.println(returned == null ? "null" : returned.evaluateWithoutScope(new ReflexPrintingDebugger()));
}
项目:Rapture    文件:ResourceBasedTest.java   
protected void runSuspendTestFor(String fileName) throws RecognitionException {
    ReflexLexer lexer = new ReflexLexer();
    lexer.setCharStream(new ANTLRStringStream(getResourceAsString(this, fileName)));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ReflexParser parser = new ReflexParser(tokens);
    CommonTree tree = (CommonTree) parser.parse().getTree();

    /*
     * DOTTreeGenerator gen = new DOTTreeGenerator(); StringTemplate st =
     * gen.toDOT(tree); System.out.println(st);
     */

    CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
    ReflexTreeWalker walker = new ReflexTreeWalker(nodes, parser.languageRegistry);
    ReflexNode returned = walker.walk();
    ReflexValue value = returned.evaluateWithoutScope(new NullDebugger());
    while (value.getValue() == ReflexValue.Internal.SUSPEND) {
        System.out.println("Oooh it suspended...");
        System.out.println("Resuming script");
        value = returned.evaluateWithResume(new NullDebugger(), returned.getScope());
    }
    System.out.println(value);
}
项目:Rapture    文件:ResourceBasedTest.java   
protected String runTestForWithApi(String fileName, ScriptingApi api, IReflexDataHandler dataHandler, Map<String, Object> injectedVars)
        throws RecognitionException {
    ReflexLexer lexer = new ReflexLexer();
    lexer.setCharStream(new ANTLRStringStream(getResourceAsString(this, fileName)));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ReflexParser parser = new ReflexParser(tokens);
    CommonTree tree = (CommonTree) parser.parse().getTree();

    CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
    ReflexTreeWalker walker = new ReflexTreeWalker(nodes, parser.languageRegistry);

    walker.getReflexHandler().setApi(api);
    walker.getReflexHandler().setDataHandler(dataHandler);
    if (injectedVars != null && !injectedVars.isEmpty()) {
        for (Map.Entry<String, Object> kv : injectedVars.entrySet()) {
            walker.currentScope.assign(kv.getKey(), kv.getValue() == null ? new ReflexNullValue() : new ReflexValue(kv.getValue()));
        }
    }

    ReflexNode returned = walker.walk();
    return returned.evaluateWithoutScope(new ReflexPrintingDebugger()).asString();
}
项目:Rapture    文件:JavaWalkerTest.java   
@Test
public void test1() throws RecognitionException {
    CharStream input = new ANTLRStringStream("version(1.1.0)\n" + "minVer(1.1.0)\n"
            + "[ The Admin API is used to manipulate and access the low level entities in Rapture. Typically the methods in this API\n"
            + "are only used during significant setup events in a Rapture environment.]\n" + "api(Admin) {\n"
            + "    [This method restores a user that has been deleted]\n" + "    @entitle=/admin/main\n"
            + "    @public Boolean restoreUser(String userName);\n" + "}\n" + "[A return value from a native query]\n"
            + "type RaptureQueryResult(@package=rapture.common) {\n" + "    List(JsonContent) rows;\n" + "}\n");
    TLexer lexer = new TLexer(input);
    TokenStream tokenInputStream = new CommonTokenStream(lexer);
    TParser parser = new TParser(tokenInputStream);
    hmxdef_return returnVal = parser.hmxdef();
    System.out.println("Done " + returnVal.getTree().toStringTree());

    TreeNodeStream treeInput = new CommonTreeNodeStream(returnVal.getTree());
    TTree walker = new TTree(treeInput);
    walker.setTemplateLib(TemplateRepo.getApiTemplates("Java"));
    com.incapture.rapgen.TTree.hmxdef_return walkerResult = walker.hmxdef();
    System.out.println("Done, result=" + walkerResult.toString());
}
项目:Rapture    文件:JavaWalkerTest.java   
@Test
public void test2() throws RecognitionException {
    CharStream input = new ANTLRStringStream("version(1.1.0)\n" + "minVer(1.1.0)\n"
            + "[ The Admin API is used to manipulate and access the low level entities in Rapture. Typically the methods in this API\n"
            + "are only used during significant setup events in a Rapture environment.]\n" + "api(Admin) {\n"
            + "    [This method restores a user that has been deleted]\n" + "    @entitle=/admin/main\n"
            + "    @public Boolean restoreUser(String userName);\n" + "}\n" + "[A return value from a native query]\n"
            + "type RaptureQueryResult(@package=rapture.common) {\n" + "    List(JsonContent) rows;\n" + "}\n");
    TLexer lexer = new TLexer(input);
    TokenStream tokenInputStream = new CommonTokenStream(lexer);
    TParser parser = new TParser(tokenInputStream);
    hmxdef_return returnVal = parser.hmxdef();
    System.out.println("Done " + returnVal.getTree().toStringTree());

    TreeNodeStream treeInput = new CommonTreeNodeStream(returnVal.getTree());
    TTree walker = new TTree(treeInput);
    walker.setTemplateLib(TemplateRepo.getApiTemplates("Java"));
    apiGen_return walkerResult = walker.apiGen();
    System.out.println("Done, result=" + walkerResult.toString());
}
项目:Rapture    文件:JavaWalkerTest.java   
@Test
public void test3() throws RecognitionException {
    CharStream input = new ANTLRStringStream("version(1.1.0)\n" + "minVer(1.1.0)\n"
            + "[ The Admin API is used to manipulate and access the low level entities in Rapture. Typically the methods in this API\n"
            + "are only used during significant setup events in a Rapture environment.]\n" + "api(Admin) {\n"
            + "    [This method restores a user that has been deleted]\n" + "    @entitle=/admin/main\n"
            + "    @public Boolean restoreUser(String userName);\n" + "}\n" + "[A return value from a native query]\n"
            + "type RaptureQueryResult(@package=rapture.common) {\n" + "    List(JsonContent) rows;\n" + "}\n");
    TLexer lexer = new TLexer(input);
    TokenStream tokenInputStream = new CommonTokenStream(lexer);
    TParser parser = new TParser(tokenInputStream);
    hmxdef_return returnVal = parser.hmxdef();
    System.out.println("Done " + returnVal.getTree().toStringTree());

    TreeNodeStream treeInput = new CommonTreeNodeStream(returnVal.getTree());
    TTree walker = new TTree(treeInput);
    walker.setTemplateLib(TemplateRepo.getSdkTemplates("Java"));
    try {
        walker.sdkGen();
        fail("This should have failed, sdk name is required but not present in code");
    } catch (IllegalArgumentException e) {
        assertTrue(e.getMessage().contains("SDK Name"));
    }
}
项目:Rapture    文件:JavaWalkerTest.java   
@Test
public void test4() throws RecognitionException {
    CharStream input = new ANTLRStringStream("sdk(alan)\n" + "version(0.0.1)\n" + "minVer(0.0.1)\n"
            + "[ The Admin API is used to manipulate and access the low level entities in Rapture. Typically the methods in this API\n"
            + "are only used during significant setup events in a Rapture environment.]\n" + "api(Admin) {\n"
            + "    [This method restores a user that has been deleted]\n" + "    @entitle=/admin/main\n"
            + "    @public Boolean restoreUser(String userName);\n" + "}\n" + "[A return value from a native query]\n"
            + "type RaptureQueryResult(@package=rapture.common) {\n" + "    List(JsonContent) rows;\n" + "}\n");
    TLexer lexer = new TLexer(input);
    TokenStream tokenInputStream = new CommonTokenStream(lexer);
    TParser parser = new TParser(tokenInputStream);
    hmxdef_return returnVal = parser.hmxdef();
    System.out.println("Done " + returnVal.getTree().toStringTree());

    TreeNodeStream treeInput = new CommonTreeNodeStream(returnVal.getTree());
    TTree walker = new TTree(treeInput);
    walker.setTemplateLib(TemplateRepo.getSdkTemplates("Java"));
    walker.sdkGen();
}
项目:Rapture    文件:JavaWalkerTest.java   
@Test
public void test5() throws RecognitionException {
    CharStream input = new ANTLRStringStream("sdk(alan)\n" + "version(0.0.1)\n" + "minVer(0.0.1)\n"
            + "[ The Admin API is used to manipulate and access the low level entities in Rapture. Typically the methods in this API\n"
            + "are only used during significant setup events in a Rapture environment.]\n" + "api(Admin) {\n"
            + "    [This method restores a user that has been deleted]\n" + "    @entitle=/admin/main\n"
            + "    @public Boolean restoreUser(String userName);\n" + "}\n" + "[A return value from a native query]\n"
            + "type RaptureQueryResult(@package=rapture.common) {\n" + "    List(JsonContent) rows;\n" + "}\n");
    TLexer lexer = new TLexer(input);
    TokenStream tokenInputStream = new CommonTokenStream(lexer);
    TParser parser = new TParser(tokenInputStream);
    hmxdef_return returnVal = parser.hmxdef();
    System.out.println("Done " + returnVal.getTree().toStringTree());

    TreeNodeStream treeInput = new CommonTreeNodeStream(returnVal.getTree());
    TTree walker = new TTree(treeInput);
    walker.setTemplateLib(TemplateRepo.getApiTemplates("Java"));
    try {
        walker.apiGen();
        fail("This should have failed, sdk name is not expected but found");
    } catch (IllegalArgumentException e) {
        assertTrue(e.getMessage().contains("SDK Name"));
    }
}
项目:Rapture    文件:JavaWalkerTest.java   
@Test
public void test7() throws RecognitionException {
    CharStream input = new ANTLRStringStream("version(1.1.0)\n" + "minVer(1.1.0)\n"
            + "[ The Admin API is used to manipulate and access the low level entities in Rapture. Typically the methods in this API\n"
            + "are only used during significant setup events in a Rapture environment.]\n" + "api(Admin) {\n"
            + "    [This method restores a user that has been deleted]\n" + "    @entitle=/admin/main\n"
            + "    @public Boolean restoreUser(String userName);\n" + "}\n" + "[A return value from a native query]\n"
            + "@Storable(storagePath : {\"sys\", name}, separator=\".\")\n" +
            "type RepoConfig (@package=rapture.common.model) {\n" +
            "   String name = \"test\";\n" +
            "   String config;\n" +
            "}\n" +
            "");
    TLexer lexer = new TLexer(input);
    TokenStream tokenInputStream = new CommonTokenStream(lexer);
    TParser parser = new TParser(tokenInputStream);
    hmxdef_return returnVal = parser.hmxdef();
    System.out.println("Done " + returnVal.getTree().toStringTree());

    TreeNodeStream treeInput = new CommonTreeNodeStream(returnVal.getTree());
    TTree walker = new TTree(treeInput);
    walker.setTemplateLib(TemplateRepo.getApiTemplates("Java"));
    apiGen_return walkerResult = walker.apiGen();
    System.out.println("Done, result=" + walkerResult.toString());
}
项目:Rapture    文件:JavaWalkerTest.java   
@Test
public void testBean() throws RecognitionException {
    CharStream input = new ANTLRStringStream("version(1.1.0)\n" + "minVer(1.1.0)\n"
            + "[ The Admin API is used to manipulate and access the low level entities in Rapture. Typically the methods in this API\n"
            + "are only used during significant setup events in a Rapture environment.]\n" + "api(Admin) {\n"
            + "    [This method restores a user that has been deleted]\n" + "    @entitle=/admin/main\n"
            + "    @public Boolean restoreUser(String userName);\n" + "}\n" +
            "[A Graph node]\n" +
            "@Bean\n" +
            "type Node(@package=rapture.common.dp) {\n" +
            "    String nodeId; //this is not a URI, just a String id\n" +
            "    List<XFer> xferValues;\n" +
            "}\n" +
            "");
    TLexer lexer = new TLexer(input);
    TokenStream tokenInputStream = new CommonTokenStream(lexer);
    TParser parser = new TParser(tokenInputStream);
    hmxdef_return returnVal = parser.hmxdef();
    System.out.println("Done " + returnVal.getTree().toStringTree());

    TreeNodeStream treeInput = new CommonTreeNodeStream(returnVal.getTree());
    TTree walker = new TTree(treeInput);
    walker.setTemplateLib(TemplateRepo.getApiTemplates("Java"));
    apiGen_return walkerResult = walker.apiGen();
    System.out.println("Done, result=" + walkerResult.toString());
}
项目:Rapture    文件:AnnotationsTest.java   
@Test
public void test6() throws RecognitionException {
    System.out.println("6");
    CharStream input = new ANTLRStringStream("[ This is a mailbox message, usually posted by an external user]\n" + "@Addressable(scheme = MAILBOX)\n"
            + "@Storable(storagePath : {authority , documentPath , id} )\n" + "type RaptureMailMessage(@package=rapture.common.model) {\n"
            + "   String id;\n" + "   String authority;\n" + "   String category;\n" + "   String content;\n" + "   Date when;\n" + "   String who;\n"
            + "}\n" + "");
    TLexer lexer = new TLexer(input);
    TokenStream tokenInputStream = new CommonTokenStream(lexer);
    TParser parser = new TParser(tokenInputStream);
    typeExpr_return returnVal = parser.typeExpr();
    System.out.println("Done " + returnVal.getTree().toStringTree());

    TreeNodeStream treeInput = new CommonTreeNodeStream(returnVal.getTree());
    TTree walker = new TTree(treeInput);
    walker.setTemplateLib(TemplateRepo.getApiTemplates("Java"));
    com.incapture.rapgen.TTree.typeExpr_return walkerResult = walker.typeExpr();
    System.out.println("Done, result=" + walkerResult.toString());
}
项目:Rapture    文件:JSWalkerTest.java   
@Test
public void test1() throws RecognitionException, IOException {
    CharStream input = new ANTLRStringStream("version(1.1.0)\n" + "minVer(1.1.0)\n"
            + "[ The Admin API is used to manipulate and access the low level entities in Rapture. Typically the methods in this API\n"
            + "are only used during significant setup events in a Rapture environment.]\n" + "api(Admin) {\n"
            + "    [This method restores a user that has been deleted]\n" + "    @entitle=/admin/main\n"
            + "    @public Boolean restoreUser(String userName);\n" + "}\n" + "[A return value from a native query]\n"
            + "type RaptureQueryResult(@package=rapture.common) {\n" + "    List(JsonContent) rows;\n" + "}\n");
    TLexer lexer = new TLexer(input);
    TokenStream tokenInputStream = new CommonTokenStream(lexer);
    TParser parser = new TParser(tokenInputStream);
    hmxdef_return returnVal = parser.hmxdef();
    System.out.println("Done " + returnVal.getTree().toStringTree());

    TreeNodeStream treeInput = new CommonTreeNodeStream(returnVal.getTree());
    TTreeJS walker = new TTreeJS(treeInput);
    walker.setTemplateLib(getJsTemplate());
}
项目:Rapture    文件:StatStoreFactory.java   
public static IRaptureStatApi getStatImpl(String config) {
    // Given a config that is formatted in the idgen command
    // string, create a RaptureIdGen
    // and assign the appropriate features and implementation to it.
    //
    try {
        SGenLexer lexer = new SGenLexer();
        log.info("Creating store from config - " + config);
        lexer.setCharStream(new ANTLRStringStream(config));
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        SGenParser parser = new SGenParser(tokens);
        parser.sinfo();

        StandardStatImpl ret = new StandardStatImpl();
        ret.setStatStore(createStatStore(parser.getStore().getType(), parser.getConfig().getConfig()));
        return ret;
    } catch (RecognitionException e) {
        log.error("Error parsing config - " + e.getMessage());
    }
    return null;
}
项目:Rapture    文件:ParseEntitlementPath.java   
public static String getEntPath(String path, IEntitlementsContext entCtx) {
    EntLexer lexer = new EntLexer();
    lexer.setCharStream(new ANTLRStringStream(path));
    TokenRewriteStream tokens = new TokenRewriteStream(lexer);
    EntParser parser = new EntParser(tokens);
    parser.setEctx(entCtx);

    try {
        parser.entpath();
        return parser.getPath();
    } catch (RecognitionException e) {
        // If we don't recognize it, ignore it and return the path
        log.error("Could not parse path " + path + ", error was " + e.getMessage());
    }
    return path;
}
项目:Rapture    文件:ReflexRaptureScript.java   
private ReflexTreeWalker getStandardWalker(CallingContext ctx, String script) throws RecognitionException {
    ReflexLexer lexer = new ReflexLexer();
    lexer.dataHandler = new ReflexIncludeHelper(ctx);
    lexer.setCharStream(new ANTLRStringStream(script));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ReflexParser parser = new ReflexParser(tokens);
    CommonTree tree;
    tree = (CommonTree) parser.parse().getTree();
    CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
    ReflexTreeWalker walker = new ReflexTreeWalker(nodes, parser.languageRegistry);
    walker.setReflexHandler(new AddingOutputReflexHandler());
    walker.getReflexHandler().setOutputHandler(new SimpleCollectingOutputHandler());
    walker.getReflexHandler().setOutputHandler(new DummyReflexOutputHandler());
    walker.getReflexHandler().setDataHandler(new ReflexDataHelper(ctx));
    walker.getReflexHandler().setIOHandler(new BlobOnlyIOHandler());
    walker.setScriptInfo(parser.scriptInfo);
    return walker;
}
项目:Rapture    文件:ResourceBasedTest.java   
protected void runSuspendTestFor(String fileName) throws RecognitionException {
    ReflexLexer lexer = new ReflexLexer();
    lexer.setCharStream(new ANTLRStringStream(getResourceAsString(this, fileName)));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ReflexParser parser = new ReflexParser(tokens);
    CommonTree tree = (CommonTree) parser.parse().getTree();

    /*
     * DOTTreeGenerator gen = new DOTTreeGenerator(); StringTemplate st =
     * gen.toDOT(tree); System.out.println(st);
     */

    CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
    ReflexTreeWalker walker = new ReflexTreeWalker(nodes, parser.languageRegistry);
    ReflexNode returned = walker.walk();
    ReflexValue value = returned.evaluateWithoutScope(new NullDebugger());
    while (value.getValue() == ReflexValue.Internal.SUSPEND) {
        System.out.println("Oooh it suspended...");
        System.out.println("Resuming script");
        value = returned.evaluateWithResume(new NullDebugger(), returned.getScope());
    }
    System.out.println(value);
}
项目:Rapture    文件:ResourceBasedTest.java   
protected void runTestForWithApi(String fileName, ScriptingApi api, IReflexDataHandler dataHandler) throws RecognitionException {
    ReflexLexer lexer = new ReflexLexer();
    lexer.setCharStream(new ANTLRStringStream(getResourceAsString(this, fileName)));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ReflexParser parser = new ReflexParser(tokens);
    CommonTree tree = (CommonTree) parser.parse().getTree();

    CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
    ReflexTreeWalker walker = new ReflexTreeWalker(nodes, parser.languageRegistry);

    walker.getReflexHandler().setApi(api);
    walker.getReflexHandler().setDataHandler(dataHandler);

    ReflexNode returned = walker.walk();
    System.out.println(returned == null ? "null" : returned.evaluateWithoutScope(new ReflexPrintingDebugger()));
}
项目:codebuff    文件:ActionTranslator.java   
public static List<ActionChunk> translateActionChunk(OutputModelFactory factory,
                                                     RuleFunction rf,
                                                     String action,
                                                     ActionAST node)
{
    Token tokenWithinAction = node.token;
    ActionTranslator translator = new ActionTranslator(factory, node);
    translator.rf = rf;
       factory.getGrammar().tool.log("action-translator", "translate " + action);
    String altLabel = node.getAltLabel();
    if ( rf!=null ) {
        translator.nodeContext = rf.ruleCtx;
        if ( altLabel!=null ) translator.nodeContext = rf.altLabelCtxs.get(altLabel);
    }
    ANTLRStringStream in = new ANTLRStringStream(action);
    in.setLine(tokenWithinAction.getLine());
    in.setCharPositionInLine(tokenWithinAction.getCharPositionInLine());
    ActionSplitter trigger = new ActionSplitter(in, translator);
    // forces eval, triggers listener methods
    trigger.getActionTokens();
    return translator.chunks;
}
项目:coj-web    文件:Utils.java   
public static List<Token> getTokens(String sourceCode, String language)
        throws Exception {
    Lexer lexer = null;
    if (language.compareTo("C++") == 0) {
        lexer = new PlagiCOJCppLexer(new ANTLRStringStream(sourceCode));
    } else if (language.compareTo("Java") == 0) {
        lexer = new PlagiCOJJavaLexer(new ANTLRStringStream(sourceCode));
    } else if (language.compareTo("C#") == 0) {
        lexer = new PlagiCOJCSharpLexer(new ANTLRStringStream(sourceCode));
    } else if (language.compareTo("C") == 0) {
        lexer = new PlagiCOJCLexer(new ANTLRStringStream(sourceCode));
    } else if (language.compareTo("Python") == 0) {
        lexer = new PlagiCOJPythonLexer(new ANTLRStringStream(sourceCode));
    } else if (language.compareTo("Pascal") == 0) {
        lexer = new PlagiCOJPascalLexer(new ANTLRStringStream(sourceCode));
    } else {
        throw new PlagiCOJUnsupportedLanguageException();
    }
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    return tokens.getTokens();
}
项目:xtext-core    文件:PartialContentAssistContextFactory.java   
@Override
protected void createContextsForLastCompleteNode(EObject previousModel, boolean strict) {
    String currentNodePrefix = getPrefix(currentNode);
    if (!Strings.isEmpty(currentNodePrefix) && !currentNode.getText().equals(currentNodePrefix)) {
        lexer.setCharStream(new ANTLRStringStream(currentNodePrefix));
        Token token = lexer.nextToken();
        if (token == Token.EOF_TOKEN) {
            return;
        }
        while (token != Token.EOF_TOKEN) {
            if (isErrorToken(token)) {
                return;
            }
            token = lexer.nextToken();
        }
    }
    String prefix = "";
    Collection<FollowElement> followElements = parseFollowElements(completionOffset, strict);
    doCreateContexts(lastCompleteNode, currentNode, prefix, previousModel, followElements);
}
项目:xtext-core    文件:ContentAssistContextFactory.java   
protected void createContextsForLastCompleteNode(EObject previousModel, boolean strict) {
    String currentNodePrefix = getPrefix(currentNode);
    if (!Strings.isEmpty(currentNodePrefix) && !currentNode.getText().equals(currentNodePrefix)) {
        lexer.setCharStream(new ANTLRStringStream(currentNodePrefix));
        Token token = lexer.nextToken();
        if (token == Token.EOF_TOKEN) { // error case - nothing could be parsed
            return;
        }
        while(token != Token.EOF_TOKEN) {
            if (isErrorToken(token))
                return;
            token = lexer.nextToken();
        }
    }
    String prefix = "";
    String completeInput = getInputToParse(document, completionOffset);
    Collection<FollowElement> followElements = parser.getFollowElements(completeInput, strict);
    doCreateContexts(lastCompleteNode, currentNode, prefix, previousModel, followElements);
}
项目:Reer    文件:GrammarTest.java   
@Test
public void canUseGeneratedGrammar() throws Exception {
    ANTLRStringStream in = new ANTLRStringStream("1+2");
    CalculatorLexer lexer = new CalculatorLexer(in);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    CalculatorParser parser = new CalculatorParser(tokens);
    parser.add();
}
项目:n4js    文件:NodeModelTokenSource.java   
/**
 * Produce an Antlr token for the prefix of the given leaf that overlaps the requested region
 *
 * @see #endOffset
 */
private Token toPrefixToken(ILeafNode leaf) {
    Lexer lexer = new InternalN4JSLexer();
    String text = leaf.getText();
    String prefix = text.substring(0, endOffset - leaf.getTotalOffset());
    ANTLRStringStream stream = new ANTLRStringStream(prefix);
    lexer.setCharStream(stream);
    Token nextToken = lexer.nextToken();
    // copy to get rid of the reference to the stream again
    return new CommonToken(nextToken.getType(), nextToken.getText());
}
项目:incubator-netbeans    文件:VMOptionsTableModel.java   
public void fill(String str) throws Exception {
    rows.clear();
    final CommandLineLexer lexer = new CommandLineLexer(new ANTLRStringStream(str));
    final CommonTokenStream cts = new CommonTokenStream(lexer);
    final CommandLineParser parser = new CommandLineParser(cts);
    parser.setTreeAdaptor(new VMOptionTreeAdaptor());
    rows.addAll(parser.parse());
    Collections.sort(rows);
    fireTableDataChanged();
}
项目:incubator-netbeans    文件:VmOptionsGrammarTest.java   
private void verifyOption(JavaVMOption<?> option) throws Exception {
    option = setOptionValue(option);
    StringBuilder sb = new StringBuilder();
    sb = option.print(sb);
    final CommandLineLexer lineLexer = new CommandLineLexer(new ANTLRStringStream(sb.toString()));
    CommandLineParser parser = new CommandLineParser(new CommonTokenStream(lineLexer));
    parser.setTreeAdaptor(new VMOptionTreeAdaptor());
    final CommandLineParser.vmOptions_return options_return = parser.vmOptions();
    final JavaVMOption<?> parsedOption = extractOption(options_return);
    if (!option.equals(parsedOption)) {
        throw new Exception("The parsed and origin is not same");
    }
}
项目:alfresco-repository    文件:OpenCmisQueryTest.java   
private CommonTree parse(String query) throws RecognitionException
{
    CharStream cs = new ANTLRStringStream(query);
    CMISLexer lexer = new CMISLexer(cs);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    CMISParser parser = new CMISParser(tokens);
    CommonTree queryNode = (CommonTree) parser.query().getTree();
    return queryNode;
}
项目:JavaGraph    文件:CtrlLoader.java   
private TokenRewriteStream getRewriter(CtrlTree tree) {
    CtrlLexer lexer = new CtrlLexer(null);
    lexer.setCharStream(new ANTLRStringStream(tree.toInputString()));
    TokenRewriteStream rewriter = new TokenRewriteStream(lexer);
    rewriter.fill();
    return rewriter;
}
项目:JavaGraph    文件:ParseTree.java   
/** Factory method for a lexer generating this kind of tree. */
public Lexer createLexer(Class<? extends Lexer> lexerType, I info, String term) {
    try {
        // instantiate the lexer
        ANTLRStringStream input = new ANTLRStringStream(term);
        Constructor<? extends Lexer> lexerConstructor =
            lexerType.getConstructor(CharStream.class);
        Lexer result = lexerConstructor.newInstance(input);
        callInitialise(result, info);
        return result;
    } catch (Exception e) {
        throw toRuntime(e);
    }
}