Java TokenStream类使用实例

发布时间:2025-12-09 12:02:54 浏览次数:2

实例1: parseAnyUnhandled

import org.antlr.runtime.TokenStream; //导入依赖的package包/类/** * 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;} 

实例2: hasDisallowedEOL

import org.antlr.runtime.TokenStream; //导入依赖的package包/类/** * 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;} 

实例3: createParser

import org.antlr.runtime.TokenStream; //导入依赖的package包/类/** 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);    }} 

实例4: test1

import org.antlr.runtime.TokenStream; //导入依赖的package包/类@Testpublic 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());} 
tokenstream
需要做网站?需要网络推广?欢迎咨询客户经理 13272073477