Example usage for org.antlr.v4.runtime CommonTokenStream CommonTokenStream

List of usage examples for org.antlr.v4.runtime CommonTokenStream CommonTokenStream

Introduction

In this page you can find the example usage for org.antlr.v4.runtime CommonTokenStream CommonTokenStream.

Prototype

public CommonTokenStream(TokenSource tokenSource) 

Source Link

Document

Constructs a new CommonTokenStream using the specified token source and the default token channel ( Token#DEFAULT_CHANNEL ).

Usage

From source file:com.qosdev.QPL.Main.java

License:Open Source License

@Override
public String visitIncludeFile(QPLParser.IncludeFileContext ctx) {
    String filePath = ctx.path.getText().trim();
    filePath = filePath.substring(1, filePath.length() - 1);
    if (filePath.charAt(0) != '/') {
        filePath = Paths.get(__FILE_PATH__, filePath).toString();
    }//from   w w  w .j  av a 2 s. com
    try {
        ANTLRInputStream ais = new ANTLRFileStream(filePath);
        QPLLexer lex = new QPLLexer(ais);
        TokenStream toks = new CommonTokenStream(lex);
        QPLParser parse = new QPLParser(toks);
        ParseTree tree = parse.prog();
        return new ImpVisitor(filePath).visit(tree);
    } catch (IOException ex) {
        System.err.println(filePath + " cannot be found! Ignoring");
        return "";
    }
}

From source file:com.rocana.configuration.ConfigurationParser.java

License:Apache License

public <T> T parse(Reader reader, Class<T> targetType) throws IOException {
    logger.debug("Parsing configuration for type:{}", targetType);

    ANTLRErrorListener listener = new BaseErrorListener() {

        @Override//from  ww w.j  av a 2  s . c  om
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
                int charPositionInLine, String msg, RecognitionException e) {
            throw new ConfigurationException(String.format(
                    "Unable to parse configuration: %s (at line:%s pos:%s)", msg, line, charPositionInLine), e);
        }

    };

    TypeDescriptor typeDescriptor = TypeMapping.ofType(targetType);

    ANTLRInputStream inputStream = new ANTLRInputStream(reader);
    ConfigurationLexer lexer = new ConfigurationLexer(inputStream);
    lexer.removeErrorListeners();
    lexer.addErrorListener(listener);

    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    com.rocana.configuration.antlr.ConfigurationParser parser = new com.rocana.configuration.antlr.ConfigurationParser(
            tokenStream);

    parser.removeErrorListeners();
    parser.addErrorListener(listener);

    ParseTree parseTree = parser.config();

    logger.debug("Parsed configuration:{}", parseTree.toStringTree(parser));

    Visitor<T> visitor = new Visitor<>(typeDescriptor);

    visitor.visit(parseTree);

    logger.debug("Configured object:{}", visitor.getResult());

    return visitor.getResult();
}

From source file:com.satisfyingstructures.J2S.J2S.java

License:Open Source License

private static void convert(InputStream is, PrintStream ps) throws Exception {
    ANTLRInputStream input = new ANTLRInputStream(is);

    Java8Lexer lexer = new Java8Lexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    Java8Parser parser = new Java8Parser(tokens);
    ParseTree tree = parser.compilationUnit();
    /*//from w  w  w  . j  a v  a 2 s  . com
        // Fix for stubborn *.java that crash the parser
        // From http://stackoverflow.com/a/32918434/618653
        // But requires antlr 4.5.3-opt - we don't have it yet
        try
        {
    // First Attempt: High-speed parsing for correct documents
            
    parser.setErrorHandler(new BailErrorStrategy());
    parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    parser.getInterpreter().tail_call_preserves_sll = false;
    tree = parser.compilationUnit();
        }
        catch (ParseCancellationException e)
        {
    // Second Attempt: High-accuracy fallback parsing for complex and/or erroneous documents
            
    // T O D O : reset your input stream
    parser.setErrorHandler(new DefaultErrorStrategy());
    parser.getInterpreter().setPredictionMode(PredictionMode.LL);
    parser.getInterpreter().tail_call_preserves_sll = false;
    parser.getInterpreter().enable_global_context_dfa = true;
    tree = parser.compilationUnit();
        }
    */
    J2SRewriter rewriter = new J2SRewriter(tree, tokens);
    J2SConverter listener = new J2SConverter(rewriter, env, typeMappings);
    ParseTreeWalker.DEFAULT.walk(listener, tree);

    ps.println(rewriter.getText());
}

From source file:com.shelloid.script.Compiler.java

public ScriptBin compile(InputStream is, ScriptSource src, HashMap<String, Object> globals)
        throws IOException, CompilerException {
    errorMsgs.clear();//from   w w w .  j  a va2  s.  co  m
    ShelloidLexer lexer = new ShelloidLexer(new ANTLRInputStream(is));
    ShelloidParser parser = new ShelloidParser(new CommonTokenStream(lexer));
    parser.removeErrorListeners();
    parser.addErrorListener(new CustomErrorListener());
    ScriptContext script = parser.script();
    ScriptBin bin = new ScriptBin();
    if (errorMsgs.isEmpty()) {
        CompileCtx ctx = new CompileCtx(src, bin, false, globals, null);
        CompiledScript cscript = translateScript(script, ctx);
        cscript.setSrc(src);
        bin.setScript(cscript);
    }

    if (errorMsgs.isEmpty()) {
        return bin;
    } else {
        throw new CompilerException(errorMsgs);
    }
}

From source file:com.sm.query.ObjectQueryVisitorImpl.java

License:Apache License

private void init() {
    try {/*from   ww w .  j  a va2  s.c  o  m*/
        ObjectQueryLexer lexer = new ObjectQueryLexer(new ANTLRInputStream(new StringReader(queryStr)));
        CommonTokenStream token = new CommonTokenStream(lexer);
        ObjectQueryParser parser = new ObjectQueryParser(token);
        parser.setBuildParseTree(true);
        tree = parser.script();
    } catch (Exception ex) {
        logger.error(ex.getMessage());
        throw new QueryException(ex.getMessage(), ex);
    }
}

From source file:com.sm.query.PredicateEstimator.java

License:Apache License

public void walkTree() {
    try {//from   w w  w.  ja va  2  s  .c o  m
        init();
        PredicateLexer lexer = new PredicateLexer(new ANTLRInputStream(new StringReader(queryStr)));
        CommonTokenStream token = new CommonTokenStream(lexer);
        PredicateParser parser = new PredicateParser(token);
        parser.setBuildParseTree(true);
        PredicateParser.ScriptContext tree = parser.script(); // parse
        ParseTreeWalker parseTreeWalker = new ParseTreeWalker();
        parseTreeWalker.walk(this, tree);
    } catch (Exception ex) {
        logger.error(ex.getMessage());
        throw new QueryException(ex.getMessage(), ex);
    }
}

From source file:com.sm.query.PredicateVisitorImpl.java

License:Apache License

private void init() {
    try {/* w w  w . j  a  va2  s. c o m*/
        if (queryStr == null || queryStr.length() == 0)
            return;
        PredicateLexer lexer = new PredicateLexer(new ANTLRInputStream(new StringReader(queryStr)));
        CommonTokenStream token = new CommonTokenStream(lexer);
        PredicateParser parser = new PredicateParser(token);
        parser.setBuildParseTree(true);
        tree = parser.script();
    } catch (Exception ex) {
        logger.error(ex.getMessage());
        throw new QueryException(ex.getMessage(), ex);
    }
}

From source file:com.sm.query.QueryListenerImpl.java

License:Apache License

public void walkTree() {
    try {//from ww w  . ja v a  2s .  c  o  m
        init();
        QueryLexer lexer = new QueryLexer(new ANTLRInputStream(new StringReader(queryStr)));
        CommonTokenStream token = new CommonTokenStream(lexer);
        QueryParser parser = new QueryParser(token);
        parser.setBuildParseTree(true);
        QueryParser.ScriptContext tree = parser.script(); // parse
        ParseTreeWalker parseTreeWalker = new ParseTreeWalker();
        parseTreeWalker.walk(this, tree);
        //check for key# if size = 1
        checkPredicateStack();
    } catch (Exception ex) {
        logger.error(ex.getMessage());
        throw new QueryException(ex.getMessage(), ex);
    }
}

From source file:com.sm.query.QueryVisitorImpl.java

License:Apache License

private void init() {
    try {//from  ww w . java 2  s . co  m
        QueryLexer lexer = new QueryLexer(new ANTLRInputStream(new StringReader(queryStr)));
        CommonTokenStream token = new CommonTokenStream(lexer);
        QueryParser parser = new QueryParser(token);
        parser.setBuildParseTree(true);
        tree = parser.script();
    } catch (Exception ex) {
        logger.error(ex.getMessage());
        throw new QueryException(ex.getMessage(), ex);
    }
}

From source file:com.spotify.heroic.grammar.CoreQueryParser.java

License:Apache License

private QueryListener parse(Function<HeroicQueryParser, ParserRuleContext> op, String input) {
    final HeroicQueryLexer lexer = new HeroicQueryLexer(new ANTLRInputStream(input));

    final CommonTokenStream tokens = new CommonTokenStream(lexer);
    final HeroicQueryParser parser = new HeroicQueryParser(tokens);

    parser.removeErrorListeners();//from  w  w  w  .j  ava2  s . c o m
    parser.setErrorHandler(new BailErrorStrategy());

    final ParserRuleContext context;

    try {
        context = op.apply(parser);
    } catch (final ParseCancellationException e) {
        if (!(e.getCause() instanceof RecognitionException)) {
            throw e;
        }

        throw toParseException((RecognitionException) e.getCause());
    }

    final QueryListener listener = new QueryListener();

    ParseTreeWalker.DEFAULT.walk(listener, context);

    final Token last = lexer.getToken();

    if (last.getType() != Token.EOF) {
        throw new ParseException(String.format("garbage at end of string: '%s'", last.getText()), null,
                last.getLine(), last.getCharPositionInLine());
    }

    return listener;
}