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

项目: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    文件:SemicolonInjectionHelper.java   
/**
 * Returns true if there was an unexpected EOL.
 */
public static boolean hasDisallowedEOL(Callback callback) {
    TokenStream input = callback.getInput();
    Token lt = input.LT(1);

    // Start on the position before the current token and scan backwards off channel tokens until the previous on
    // channel token.
    for (int ix = lt.getTokenIndex() - 1; ix > 0; ix--) {
        lt = input.get(ix);
        if (lt.getChannel() == Token.DEFAULT_CHANNEL) {
            // On channel token found: stop scanning.
            break;
        } else if (isSemicolonEquivalent(lt)) {
            return true;
        }
    }
    return false;
}
项目:JavaGraph    文件:ParseTree.java   
/** Creates a parser for a given term, generating trees of this kind. */
public <P extends Parser> P createParser(Class<P> parserType, I info, String term) {
    try {
        // find the lexer type
        String parserName = parserType.getName();
        String lexerName = parserName.substring(0, parserName.indexOf("Parser"))
            .concat("Lexer");
        @SuppressWarnings("unchecked") Class<? extends Lexer> lexerType =
            (Class<? extends Lexer>) Class.forName(lexerName);
        Lexer lexer = createLexer(lexerType, info, term);
        // instantiate the parser
        CommonTokenStream tokenStream = new CommonTokenStream(lexer);
        Constructor<P> parserConstructor = parserType.getConstructor(TokenStream.class);
        P result = parserConstructor.newInstance(tokenStream);
        Method adaptorSetter = parserType.getMethod("setTreeAdaptor", TreeAdaptor.class);
        adaptorSetter.invoke(result, new ParseTreeAdaptor<>(this, info, tokenStream));
        callInitialise(result, info);
        return result;
    } catch (Exception e) {
        throw toRuntime(e);
    }
}
项目: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());
}
项目:cassandra-kmean    文件:CqlParserTest.java   
@Test
public void testAddErrorListener() throws Exception
{
    SyntaxErrorCounter firstCounter = new SyntaxErrorCounter();
    SyntaxErrorCounter secondCounter = new SyntaxErrorCounter();

    CharStream stream = new ANTLRStringStream("SELECT * FORM test;");
    CqlLexer lexer = new CqlLexer(stream);

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

    parser.query();

    // ANTLR 3.5 reports 2 errors in the sentence above (missing FROM and missing EOF).
    assertTrue(firstCounter.count > 0);
    assertTrue(secondCounter.count > 0);
}
项目:cassandra-kmean    文件:CqlParserTest.java   
@Test
public void testRemoveErrorListener() throws Exception
{
    SyntaxErrorCounter firstCounter = new SyntaxErrorCounter();
    SyntaxErrorCounter secondCounter = new SyntaxErrorCounter();

    CharStream stream = new ANTLRStringStream("SELECT * FORM test;");
    CqlLexer lexer = new CqlLexer(stream);

    TokenStream tokenStream = new CommonTokenStream(lexer);
    CqlParser parser = new CqlParser(tokenStream);
    parser.addErrorListener(firstCounter);
    parser.addErrorListener(secondCounter);
    parser.removeErrorListener(secondCounter);

    parser.query();

    assertTrue(firstCounter.count > 0);
    assertEquals(0, secondCounter.count);
}
项目:Pinot    文件:PQLCompiler.java   
@Override
public JSONObject compile(String bqlStmt) throws RecognitionException {
  // Lexer splits input into tokens
  ANTLRStringStream input = new ANTLRStringStream(bqlStmt);
  TokenStream tokens = new CommonTokenStream(new PQLLexer(input));

  // Parser generates abstract syntax tree
  PQLParser parser = new PQLParser(tokens, _facetInfoMap);
  _parser.set(parser);
  PQLParser.statement_return ret = parser.statement();

  // Acquire parse result
  CommonTree ast = (CommonTree) ret.tree;

  JSONObject json = (JSONObject) ret.json;
  // XXX To be removed
  // printTree(ast);
  // System.out.println(">>> json = " + json.toString());
  return json;
}
项目:PDFReporter-Studio    文件:InternalJavaJRExpressionParser.java   
public int specialStateTransition(int s, IntStream _input) throws NoViableAltException {
    TokenStream input = (TokenStream)_input;
    int _s = s;
    switch ( s ) {
            case 0 : 
                int LA14_3 = input.LA(1);


                int index14_3 = input.index();
                input.rewind();
                s = -1;
                if ( (synpred9_InternalJavaJRExpression()) ) {s = 19;}

                else if ( (true) ) {s = 4;}


                input.seek(index14_3);
                if ( s>=0 ) return s;
                break;
    }
    if (state.backtracking>0) {state.failed=true; return -1;}
    NoViableAltException nvae =
        new NoViableAltException(getDescription(), 14, _s, input);
    error(nvae);
    throw nvae;
}
项目:PDFReporter-Studio    文件:InternalJavaJRExpressionParser.java   
public int specialStateTransition(int s, IntStream _input) throws NoViableAltException {
    TokenStream input = (TokenStream)_input;
    int _s = s;
    switch ( s ) {
            case 0 : 
                int LA36_1 = input.LA(1);


                int index36_1 = input.index();
                input.rewind();
                s = -1;
                if ( (synpred12_InternalJavaJRExpression()) ) {s = 22;}

                else if ( (true) ) {s = 2;}


                input.seek(index36_1);
                if ( s>=0 ) return s;
                break;
    }
    if (state.backtracking>0) {state.failed=true; return -1;}
    NoViableAltException nvae =
        new NoViableAltException(getDescription(), 36, _s, input);
    error(nvae);
    throw nvae;
}
项目:PDFReporter-Studio    文件:InternalJavaJRExpressionParser.java   
public int specialStateTransition(int s, IntStream _input) throws NoViableAltException {
    TokenStream input = (TokenStream)_input;
    int _s = s;
    switch ( s ) {
            case 0 : 
                int LA6_3 = input.LA(1);


                int index6_3 = input.index();
                input.rewind();
                s = -1;
                if ( (synpred10_InternalJavaJRExpression()) ) {s = 19;}

                else if ( (true) ) {s = 4;}


                input.seek(index6_3);
                if ( s>=0 ) return s;
                break;
    }
    if (state.backtracking>0) {state.failed=true; return -1;}
    NoViableAltException nvae =
        new NoViableAltException(getDescription(), 6, _s, input);
    error(nvae);
    throw nvae;
}
项目:PDFReporter-Studio    文件:InternalJavaJRExpressionParser.java   
public int specialStateTransition(int s, IntStream _input) throws NoViableAltException {
    TokenStream input = (TokenStream)_input;
    int _s = s;
    switch ( s ) {
            case 0 : 
                int LA39_1 = input.LA(1);


                int index39_1 = input.index();
                input.rewind();
                s = -1;
                if ( (synpred63_InternalJavaJRExpression()) ) {s = 22;}

                else if ( (true) ) {s = 2;}


                input.seek(index39_1);
                if ( s>=0 ) return s;
                break;
    }
    if (state.backtracking>0) {state.failed=true; return -1;}
    NoViableAltException nvae =
        new NoViableAltException(getDescription(), 39, _s, input);
    error(nvae);
    throw nvae;
}
项目:scylla-tools-java    文件: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;
}
项目:scylla-tools-java    文件:CqlParserTest.java   
@Test
public void testAddErrorListener() throws Exception
{
    SyntaxErrorCounter firstCounter = new SyntaxErrorCounter();
    SyntaxErrorCounter secondCounter = new SyntaxErrorCounter();

    CharStream stream = new ANTLRStringStream("SELECT * FORM users");
    CqlLexer lexer = new CqlLexer(stream);

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

    parser.query();

    // ANTLR 3.5 reports 2 errors in the sentence above (missing FROM and missing EOF).
    assertTrue(firstCounter.count > 0);
    assertTrue(secondCounter.count > 0);
}
项目:scylla-tools-java    文件:CqlParserTest.java   
@Test
public void testRemoveErrorListener() throws Exception
{
    SyntaxErrorCounter firstCounter = new SyntaxErrorCounter();
    SyntaxErrorCounter secondCounter = new SyntaxErrorCounter();

    CharStream stream = new ANTLRStringStream("SELECT * FORM test;");
    CqlLexer lexer = new CqlLexer(stream);

    TokenStream tokenStream = new CommonTokenStream(lexer);
    CqlParser parser = new CqlParser(tokenStream);
    parser.addErrorListener(firstCounter);
    parser.addErrorListener(secondCounter);
    parser.removeErrorListener(secondCounter);

    parser.query();

    assertTrue(firstCounter.count > 0);
    assertEquals(0, secondCounter.count);
}
项目:GraphTrek    文件:CqlParserTest.java   
@Test
public void testAddErrorListener() throws Exception
{
    SyntaxErrorCounter firstCounter = new SyntaxErrorCounter();
    SyntaxErrorCounter secondCounter = new SyntaxErrorCounter();

    CharStream stream = new ANTLRStringStream("SELECT * FORM users");
    CqlLexer lexer = new CqlLexer(stream);

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

    parser.query();

    assertTrue(firstCounter.count > 0);
    assertTrue(secondCounter.count > 0);
}
项目:GraphTrek    文件:CqlParserTest.java   
@Test
public void testRemoveErrorListener() throws Exception
{
    SyntaxErrorCounter firstCounter = new SyntaxErrorCounter();
    SyntaxErrorCounter secondCounter = new SyntaxErrorCounter();

    CharStream stream = new ANTLRStringStream("SELECT * FORM test;");
    CqlLexer lexer = new CqlLexer(stream);

    TokenStream tokenStream = new CommonTokenStream(lexer);
    CqlParser parser = new CqlParser(tokenStream);
    parser.addErrorListener(firstCounter);
    parser.addErrorListener(secondCounter);
    parser.removeErrorListener(secondCounter);

    parser.query();

    assertTrue(firstCounter.count > 0);
    assertEquals(0, secondCounter.count);
}
项目:goworks    文件:ANTLRErrorProvidingParser.java   
public GrammarASTErrorNode(TokenStream input, Token start, Token stop, RecognitionException e) {
    super(stop);
    //Console.Out.WriteLine( "start: " + start + ", stop: " + stop );
    if ( stop == null ||
         ( stop.getTokenIndex() < start.getTokenIndex() &&
          stop.getType() != Token.EOF) ) {
        // sometimes resync does not consume a token (when LT(1) is
        // in follow set.  So, stop will be 1 to left to start. adjust.
        // Also handle case where start is the first token and no token
        // is consumed during recovery; LT(-1) will return null.
        stop = start;
    }
    this.input = input;
    this.start = start;
    this.stop = stop;
    this.trappedException = e;
}
项目:goworks    文件:ANTLRErrorProvidingParser.java   
@Override
public String getText()
{
    String badText;
    if (start instanceof Token) {
        int i = start.getTokenIndex();
        int j = stop.getTokenIndex();
        if (stop.getType() == Token.EOF) {
            j = ((TokenStream)input).size();
        }
        badText = ((TokenStream)input).toString(i, j);
    } else if (start instanceof Tree) {
        badText = ((TreeNodeStream)input).toString(start, stop);
    } else {
        // people should subclass if they alter the tree type so this
        // next one is for sure correct.
        badText = "<unknown>";
    }
    return badText;
}
项目:stratio-cassandra    文件:CqlParserTest.java   
@Test
public void testAddErrorListener() throws Exception
{
    SyntaxErrorCounter firstCounter = new SyntaxErrorCounter();
    SyntaxErrorCounter secondCounter = new SyntaxErrorCounter();

    CharStream stream = new ANTLRStringStream("SELECT * FORM test;");
    CqlLexer lexer = new CqlLexer(stream);

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

    parser.query();

    // ANTLR 3.5 reports 2 errors in the sentence above (missing FROM and missing EOF).
    assertTrue(firstCounter.count > 0);
    assertTrue(secondCounter.count > 0);
}
项目:stratio-cassandra    文件:CqlParserTest.java   
@Test
public void testRemoveErrorListener() throws Exception
{
    SyntaxErrorCounter firstCounter = new SyntaxErrorCounter();
    SyntaxErrorCounter secondCounter = new SyntaxErrorCounter();

    CharStream stream = new ANTLRStringStream("SELECT * FORM test;");
    CqlLexer lexer = new CqlLexer(stream);

    TokenStream tokenStream = new CommonTokenStream(lexer);
    CqlParser parser = new CqlParser(tokenStream);
    parser.addErrorListener(firstCounter);
    parser.addErrorListener(secondCounter);
    parser.removeErrorListener(secondCounter);

    parser.query();

    assertTrue(firstCounter.count > 0);
    assertEquals(0, secondCounter.count);
}
项目:tesora-dve-pub    文件:Utils.java   
private String extractLine(TokenStream input, RecognitionException e) {
    int line = e.line;
    // select tokens on the same line
    boolean done =false;
    int position = -1;
    StringBuilder buf = new StringBuilder();
    while(!done) {
        position++;
        try {
            Token tok = input.get(position);
            if (tok.getLine() < line)
                continue;
            if (tok.getLine() == line) {
                buf.append(tok.getText());
                buf.append(" ");
            } else if (tok.getLine() > line)
                done = true;
        } catch (NoSuchElementException nsee) {
            done = true;
        }
    }       
    return buf.toString();  
}
项目:antlrworks2    文件:ANTLRErrorProvidingParser.java   
public GrammarASTErrorNode(TokenStream input, Token start, Token stop, RecognitionException e) {
    super(stop);
    //Console.Out.WriteLine( "start: " + start + ", stop: " + stop );
    if ( stop == null ||
         ( stop.getTokenIndex() < start.getTokenIndex() &&
          stop.getType() != Token.EOF) ) {
        // sometimes resync does not consume a token (when LT(1) is
        // in follow set.  So, stop will be 1 to left to start. adjust.
        // Also handle case where start is the first token and no token
        // is consumed during recovery; LT(-1) will return null.
        stop = start;
    }
    this.input = input;
    this.start = start;
    this.stop = stop;
    this.trappedException = e;
}
项目:antlrworks2    文件:ANTLRErrorProvidingParser.java   
@Override
public String getText()
{
    String badText;
    if (start instanceof Token) {
        int i = start.getTokenIndex();
        int j = stop.getTokenIndex();
        if (stop.getType() == Token.EOF) {
            j = ((TokenStream)input).size();
        }
        badText = ((TokenStream)input).toString(i, j);
    } else if (start instanceof Tree) {
        badText = ((TreeNodeStream)input).toString(start, stop);
    } else {
        // people should subclass if they alter the tree type so this
        // next one is for sure correct.
        badText = "<unknown>";
    }
    return badText;
}
项目:sstable-adaptor    文件:ErrorCollector.java   
/**
 * Appends a query snippet to the message to help the user to understand the problem.
 *
 * @param parser the parser used to parse the query
 * @param builder the <code>StringBuilder</code> used to build the error message
 */
private void appendQuerySnippet(Parser parser, StringBuilder builder)
{
    TokenStream tokenStream = parser.getTokenStream();
    int index = tokenStream.index();
    int size = tokenStream.size();

    Token from = tokenStream.get(getSnippetFirstTokenIndex(index));
    Token to = tokenStream.get(getSnippetLastTokenIndex(index, size));
    Token offending = tokenStream.get(getOffendingTokenIndex(index, size));

    appendSnippet(builder, from, to, offending);
}
项目:n4js    文件:InternalHighlightingParser.java   
/**
 * Protected to allow access from subtypes and same package.
 */
protected InternalHighlightingParser(TokenStream input, N4JSGrammarAccess grammarAccess,
        TokenTypeRewriter rewriter) {
    super(input, grammarAccess);
    this.rewriter = rewriter;
    this.recoverySets = computeRecoverySets();
}
项目:n4js    文件:InternalSemicolonInjectingParser.java   
/**
 * @param input
 *            token stream, is to be expected an {@link LazyTokenStream}
 */
InternalSemicolonInjectingParser(TokenStream input, N4JSGrammarAccess grammarAccess) {
    super(input, grammarAccess);
    Field f = null;
    try {
        f = AbstractInternalAntlrParser.class.getDeclaredField("currentError");
        f.setAccessible(true);
    } catch (Exception e) {
        // ignore
    }
    this.reflectCurrentError = f;
    recoverySets = computeRecoverySets();
}
项目:n4js    文件:SemicolonInjectionHelper.java   
/**
 * <p>
 * Promotes EOL which may lead to an automatically inserted semicolon. This is probably the most important method
 * for automatic semicolon insertion, as it is only possible to insert a semicolon in case of line breaks (even if
 * they are hidden in a multi-line comment!).
 * </p>
 */
public static void promoteEOL(Callback callback) {
    RecognizerSharedState state = callback.getState();
    TokenStream input = callback.getInput();
    // Don't promote EOL if there was a syntax error at EOF
    if (state.lastErrorIndex == input.size()) {
        return;
    }
    // Get current token and its type (the possibly offending token).
    Token prev = input.LT(-1);
    Token next = input.LT(1);
    int la = next.getType();

    // Promoting an EOL means switching it from off channel to on channel.
    // A ML_COMMENT gets promoted when it contains an EOL.
    for (int idx = prev == null ? 0 : prev.getTokenIndex() + 1, max = la == Token.EOF ? input.size()
            : next.getTokenIndex(); idx < max; idx++) {
        Token lt = input.get(idx);
        if (lt.getChannel() == Token.DEFAULT_CHANNEL) {
            // On channel token found: stop scanning (previously promoted)
            break;
        } else if (isSemicolonEquivalent(lt)) {
            // We found our EOL: promote the token to on channel, position the input on it and reset the rule
            // start.
            lt.setChannel(Token.DEFAULT_CHANNEL);
            input.seek(idx);
            break;
        }
    }
}
项目:n4js    文件:SemicolonInjectionHelper.java   
/**
 * A "," cannot be followed by an automatically inserted semicolon. This is in particular true in case of variable
 * statements, in which the last declaration is ended with a comma (which might easily happen in case of copying the
 * initializer from a list or object literal (cf. IDEBUG-214).
 */
private static boolean findCommaBeforeEOL(TokenStream casted, int startIndex) {
    for (int ix = startIndex - 1; ix > 0; ix--) {
        Token lt = casted.get(ix);
        if (lt.getType() == InternalN4JSParser.Comma) {
            // System.out.println("Found Comma, EOL is not valid");
            return true;
        }
        if (lt.getChannel() == Token.DEFAULT_CHANNEL) { // any other real char ends this search
            break;
        }
    }
    return false;
}
项目:incubator-netbeans    文件:UnrecognizedOption.java   
public UnrecognizedOption(TokenStream input, Token start, Token stop, RecognitionException e) {
    super(start);        
    this.input = input;
    this.start = start;
    this.stop = stop;
    this.e = e;
    if (start != null) {
        setName(start.getText());
    }
    setValue(new OptionValue.SwitchOnly(true));
}
项目:incubator-netbeans    文件:ExtCss3Parser.java   
public ExtCss3Parser(TokenStream input, NbParseTreeBuilder dbg, String mimeType) {
    super(input, dbg);        
    if(mimeType != null) {
        this.isLessSource = mimeType.equals("text/less");
        this.isScssSource = mimeType.equals("text/scss");
    }
}
项目:JavaGraph    文件:ParseTreeAdaptor.java   
@Override
public T errorNode(TokenStream input, Token start, Token stop,
        RecognitionException e) {
    T result = this.prototype.newNode();
    result.setErrorNode(start, stop, e);
    return result;
}
项目:alfresco-remote-api    文件:WhereCompiler.java   
public static CommonTree compileWhereClause(String expression) throws RecognitionException {
    //lexer splits input into tokens
    ANTLRStringStream input = new ANTLRStringStream(expression);
    TokenStream tokens = new CommonTokenStream(new WhereClauseLexer(input));

    //parser generates abstract syntax tree
    WhereClauseParser parser = new WhereClauseParser(tokens);
    WhereClauseParser.whereclause_return ret = parser.whereclause();

    //acquire parse result
    CommonTree ast = (CommonTree) ret.getTree();
    if (logger.isDebugEnabled()) print(ast, 0);
    return ast;
}