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:kr.ac.korea.dbserver.parser.SQLAnalyzer.java

License:Apache License

public Expr parse(String sql) {
    ANTLRInputStream input = new ANTLRInputStream(sql);
    SQLLexer lexer = new SQLLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    this.parser = new SQLParser(tokens);
    parser.setBuildParseTree(true);/*w ww  .  ja v  a2  s .c o  m*/
    parser.removeErrorListeners();

    parser.setErrorHandler(new SQLErrorStrategy());
    parser.addErrorListener(new SQLErrorListener());

    SqlContext context;
    try {
        context = parser.sql();
    } catch (SQLParseError e) {
        throw new SQLSyntaxError(e);
    }
    return visitSql(context);
}

From source file:kr.co.bitnine.octopus.sql.OctopusSql.java

License:Apache License

public static List<OctopusSqlCommand> parse(String query) {
    ANTLRInputStream input = new ANTLRInputStream(query);
    OctopusSqlLexer lexer = new OctopusSqlLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    OctopusSqlParser parser = new OctopusSqlParser(tokens);
    ParserRuleContext tree = parser.ddl();

    ParseTreeWalker walker = new ParseTreeWalker();
    Listener lsnr = new Listener();
    walker.walk(lsnr, tree);/*  w  w  w. j a va  2 s . c om*/

    return lsnr.getSqlCommands();
}

From source file:kr.simula.formula.ide.core.parser.FormulaSourceParser.java

License:Apache License

@Override
public IModuleDeclaration parse(IModuleSource input, IProblemReporter reporter) {
    final String sourceCode = input.getSourceContents();

    CharStream antlrInput = new ANTLRInputStream(sourceCode);
    FormulaScriptLexer lexer = new FormulaScriptLexer(antlrInput);
    TokenStream tokenStream = new CommonTokenStream(lexer);
    FormulaScriptParser parser = new FormulaScriptParser(tokenStream);

    FormulaASTHandler handler = FormulaPlugin.getDefault().getHandlerFactory().newHandler();

    parser.setHandler(handler);/* w w  w .j a va  2  s  . c o m*/

    SyntaxErrorAdapter errorAdapter = new SyntaxErrorAdapter(reporter, input.getFileName());

    parser.addErrorListener(errorAdapter);

    Script script = null;

    try {
        FormulaScriptContext ctx = parser.formulaScript();
        script = ctx.script;

        System.err.println("FormulaSourceParser#parse " + script.getExpression());
        ProblemCollector collector = (ProblemCollector) reporter.getAdapter(ProblemCollector.class);
        for (IProblem p : collector.getProblems()) {
            System.err.println(p);
        }
    } catch (BuildException e) {
        errorAdapter.reportBuildError(e);
    }

    FormulaModuleDeclaration module = new FormulaModuleDeclaration(sourceCode.length());

    return module;
}

From source file:leads.tajo.module.LeadsSQLAnalyzer.java

License:Apache License

public Expr parse(String sql) {
    ANTLRInputStream input = new ANTLRInputStream(sql);
    SQLLexer lexer = new SQLLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    this.parser = new LeadsSQLParser(tokens);
    parser.setBuildParseTree(true);/* www . j av  a 2s .c  om*/
    parser.removeErrorListeners();

    parser.setErrorHandler(new SQLErrorStrategy());
    parser.addErrorListener(new SQLErrorListener());

    SqlContext context;
    try {
        context = parser.sql();
    } catch (SQLParseError e) {
        throw new SQLSyntaxError(e);
    }
    return visitSql(context);
}

From source file:lenguajes.project.GUI.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

    String cypherText = input.getText();
    ANTLRInputStream input = new ANTLRInputStream(cypherText);
    Neo4JLexer lexer = new Neo4JLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    Neo4JParser parser = new Neo4JParser(tokens);
    ParseTree tree = parser.init();/*from   w w  w . ja  va2  s.  c o  m*/
    MyNeo4JVisitor<Object> loader = new MyNeo4JVisitor<>();
    translation = (String) loader.visit(tree);
    System.out.println("translation " + translation);
    output.setText(translation.replaceAll(";", ";\n").replaceAll(",", ",\n"));
    if (cond) {
        openBrowser(cypherText);
    }
    drawComponents();
}

From source file:lenguajes.project.Interpreter.java

public static void main(String[] args) throws Exception {
    ANTLRInputStream input = new ANTLRInputStream(new FileInputStream(new File("inputNeo4J.txt")));
    Neo4JLexer lexer = new Neo4JLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    Neo4JParser parser = new Neo4JParser(tokens);
    ParseTree tree = parser.init();/*from   w w  w.ja  v a  2s .  c o m*/
    MyNeo4JVisitor<Object> loader = new MyNeo4JVisitor<>();
    System.out.println("translation " + loader.visit(tree));
}

From source file:lfa.obj.HTMLObjetos.java

/**
 * @param args the command line arguments
 * @throws java.io.IOException/*from   w  w  w  . j a v a 2  s  .c  o m*/
 */
public static void main(String[] args) throws IOException {
    // Caso nao seja possivel passar os arquivos de testes como parametros

    String s = "inicio\n" + "1000 1000 branco\n" + "quadrado 100 100 vermelho\n" + "circulo 100 100 azul\n"
            + "triangulo 100 100 preto\n" + "trapezio 100 100 roxo\n" + "paralelograma 100 100 verde\n"
            + "retangulo 100 100 rosa\n" + "oval 100 100 amarelo\n" + "estrela 100 100 azul\n" + "fim";

    ANTLRInputStream input = new ANTLRInputStream(args[1]);
    ObjetosLexer lexer = new ObjetosLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ObjetosParser parser = new ObjetosParser(tokens);
    String ctx = parser.init().result;
    if (ctx != null) {
        System.out.printf(">>> %s criado com sucesso.\n", ctx);
        Forma forma = new Forma(ctx);
        forma.eval();
    } else {
        System.out.println("!!! Error");
    }

}

From source file:logic.Extractor.java

License:Apache License

/**
 * Returns a map, containing clazzName and count of methods, for the given
 * file./*www .java  2s  . com*/
 * 
 * Based upon the <a href=
 * "https://theantlrguy.atlassian.net/wiki/display/ANTLR4/Parse+Tree+Listeners">ANTLR-Tutorial</a>
 * 
 * @param importFile
 *            The file to import.
 * @return A map containing the clazzName and count of methods, extracted
 *         from {@code importFile}.
 * @throws Exception
 *             IO- and ANTLR-Exceptions, not catched for simplicity.
 */
public Map<String, Integer> extract(File importFile) throws Exception {

    /** Prepare input for ANTLR. */
    InputStream is = new FileInputStream(importFile.getAbsolutePath());
    ANTLRInputStream input = new ANTLRInputStream(is);
    JavaLexer lexer = new JavaLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    JavaParser parser = new JavaParser(tokens);

    /** Parse. */
    ParseTree tree = parser.compilationUnit();

    /** Create standard walker. */
    ParseTreeWalker walker = new ParseTreeWalker();

    /** Initiate walk of tree with listener. */
    Listener listener = new Listener(parser);
    walker.walk(listener, tree);

    /** Save the results. */
    Map<String, Integer> result = new HashMap<>();
    result.put(listener.getClazzName(), listener.getMethodCount());

    return result;
}

From source file:main.java.eu.maurosabatino.lftlab1314.es2.expr.RunExpr.java

public static void main(String[] args) throws Exception {
    ANTLRInputStream input = new ANTLRInputStream(System.in);
    ExprLexer lexer = new ExprLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ExprParser parser = new ExprParser(tokens);
    parser.start();/*  w w w.  ja  v a 2s  .  com*/
}

From source file:main.MainInputFile.java

public static void main(String... args) {
    //System.out.println(Arrays.toString(args));

    String outputFile = "";
    String inputFile = "";

    if (args.length == 0)
        return;/*from ww w  .jav  a  2  s  .co  m*/

    if (args.length == 1) {
        inputFile = outputFile = args[0];
        outputFile += ".c";
    }

    if (args.length == 2) {
        inputFile = args[0];
        outputFile = args[1];
    }

    FileInputStream fis = null;
    try {
        fis = new FileInputStream(new File(inputFile));
    } catch (FileNotFoundException ex) {
        Logger.getLogger(MainInputFile.class.getName()).log(Level.SEVERE, null, ex);
    }

    if (fis == null)
        return;

    //Stream de texto
    ANTLRInputStream inputStream = null;
    try {
        inputStream = new ANTLRInputStream(fis);
    } catch (IOException ex) {
        Logger.getLogger(MainInputFile.class.getName()).log(Level.SEVERE, null, ex);
    }
    if (inputStream == null)
        return;

    //Lexer para gerar os tokens
    LAGrammar1_CodeGenLexer lexer = new LAGrammar1_CodeGenLexer(inputStream);
    //Estrutura comum de stream de tokens
    CommonTokenStream stream = new CommonTokenStream(lexer);
    //Parser do programa
    LAGrammar1_CodeGenParser parser = new LAGrammar1_CodeGenParser(stream);

    parser.addErrorListener(SyntaticErrorHandlerSaidaPadronizada.INSTANCE);
    lexer.addErrorListener(LexErrorHandlerSaidaPadronizada.INSTANCE);

    //Chama o token inicial
    parser.programa();
    if (SemanticUtil.hasErrors() || parser.getNumberOfSyntaxErrors() > 0) {
        System.err.println("Compilation errors!");
        if (SemanticUtil.hasErrors()) {
            System.err.println("Semantic errors:");
            SemanticUtil.printErrors();
            try {
                Generator.publishLog(outputFile, SemanticUtil.listErrors());
            } catch (IOException ex) {
                Logger.getLogger(MainInputFile.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (parser.getNumberOfSyntaxErrors() > 0) {
            System.err.println(SyntaticErrorHandler.INSTANCE.listSyntaticErrors());
            try {
                if (SErrorList.sErrors.size() > 0) {
                    Generator.publishLog(outputFile, SErrorList.sErrors.get(0) + "\nFim da compilacao\n");
                }
            } catch (IOException ex) {
                Logger.getLogger(MainInputFile.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    } else {
        System.out.println("Compilation successful!");
        try {
            Generator.publishCode(outputFile);
        } catch (IOException ex) {
            Logger.getLogger(MainInputFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    Generator.reset();
    SemanticUtil.reset();
}