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.yahoo.validatar.assertion.Assertor.java

License:Apache License

private static void assertOneAssertion(String assertion, Map<String, TypedObject> row, Test test) {
    log.info("Running assertion: {}", assertion);
    try {/*from  w  w w  . j  a v a  2 s  . c  om*/
        ANTLRInputStream in = new ANTLRInputStream(assertion);
        GrammarLexer lexer = new GrammarLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        GrammarParser parser = new GrammarParser(tokens);
        parser.setCurrentRow(row);
        if (!parser.expression().value) {
            test.setFailed();
            test.addMessage(assertion + " was false for these values " + parser.getLookedUpValues());
        }
    } catch (Exception e) {
        test.setFailed();
        test.addMessage(assertion + " : " + e.toString());
        log.error("Assertion failed with exception", e);
    }
}

From source file:com.yahoo.yqlplus.language.parser.ProgramParser.java

private yqlplusParser prepareParser(final String programName, CharStream input) {
    yqlplusLexer lex = new yqlplusLexer(input);
    lex.addErrorListener(new BaseErrorListener() {
        @Override/*from www  .j av a  2s  .c  o m*/
        public void syntaxError(@NotNull Recognizer<?, ?> recognizer, @Nullable Object offendingSymbol,
                int line, int charPositionInLine, @NotNull String msg, @Nullable RecognitionException e) {
            throw new ProgramCompileException(new Location(programName, line, charPositionInLine), msg);
        }

    });
    TokenStream tokens = new CommonTokenStream(lex);
    yqlplusParser parser = new yqlplusParser(tokens);
    parser.addErrorListener(new BaseErrorListener() {
        @Override
        public void syntaxError(@NotNull Recognizer<?, ?> recognizer, @Nullable Object offendingSymbol,
                int line, int charPositionInLine, @NotNull String msg, @Nullable RecognitionException e) {
            throw new ProgramCompileException(new Location(programName, line, charPositionInLine), msg);
        }

    });
    parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    return parser;
}

From source file:compile.compilersource.CompilerHelper.java

public static String compile(String expression, CompilerUI ui, ArrayList<String> watchList,
        ArrayList<Integer> breaklineList) {
    String output = "";
    try {/*  ww  w  .j a v  a  2s.  co m*/
        //TODO: assign classname here

        //lexer splits input into tokens
        ANTLRInputStream input = new ANTLRInputStream(expression);
        CommonTokenStream tokens = new CommonTokenStream(new myGrammarLexer(input));
        //parser generates abstract syntax tree
        myGrammarParser parser = new myGrammarParser(tokens);

        ExceptionErrorStrategy errorStrat = new ExceptionErrorStrategy();
        parser.setErrorHandler(errorStrat);

        ErrorReporter errorReporter = new ErrorReporter(ui);
        EvalVisitor<String> eval = new EvalVisitor<String>(errorReporter, ui, watchList, breaklineList);
        String totalErrorString = "";

        String results = eval.visitParse(parser.parse());
        totalErrorString += errorReporter.GetTotalErrorString() + "\n";

        //use a listener to gather syntax errors
        totalErrorString += CheckSyntaxReturnErrors(expression, ui);

        if (isStringNullOrWhiteSpace(totalErrorString)) {
            output = "";
        } else {
            //ui.getOutputConsole().setText("");
            output = totalErrorString;
        }
        System.out.println("result: " + output);
    } catch (RecognitionException e) {
        throw new IllegalStateException("Recognition exception is never thrown, only declared.");
    }
    return output;
}

From source file:compile.compilersource.CompilerHelper.java

static String CheckSyntaxReturnErrors(String expression, CompilerUI ui) {
    //lexer splits input into tokens
    ANTLRInputStream input = new ANTLRInputStream(expression);
    CommonTokenStream tokens = new CommonTokenStream(new myGrammarLexer(input));

    //parser generates abstract syntax tree
    myGrammarParser parser = new myGrammarParser(tokens);
    parser.removeErrorListeners();/*from  w w w.j a  v  a  2 s.  c  o m*/
    AntlrErrorListener errorListener = new AntlrErrorListener(ui);//BaseErrorListenerExtension errorListener = new BaseErrorListenerExtension();
    //errorListener.SetEditor(inputTextArea);
    parser.addErrorListener(errorListener);

    //parse input text
    ParseTreeWalker walker = new ParseTreeWalker();
    GrammarListener listener = new GrammarListener();
    walker.walk(listener, parser.parse());

    return errorListener.GetAllErrorMessages() + "\n";
}

From source file:construction.Construction.java

License:Open Source License

public void parseIT(String inputFile) {
    try {//from   w ww .  j  ava  2  s  .c  o  m
        System.out.println("Parsing file, " + inputFile);
        FileInputStream fis = new FileInputStream(inputFile);
        ANTLRInputStream input = new ANTLRInputStream(fis);
        testLexer lexer = new testLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        testParser parser = new testParser(tokens);
        QuestionForm q = parser.questionaire().form;

        int typeErrors = TypeChecker(q.getQuestions());
        if (typeErrors == 0) {
            QuestionGUI g = new QuestionGUI(q.getName(), q.getQuestions());
            g.render();
        } else {
            System.out.println("Type Errors were found, fix these before proceeding");
        }

        //        q.render();
    } catch (RecognitionException ex) {
        Logger.getLogger(Construction.class.getName()).log(Level.SEVERE, null, ex);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Construction.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Construction.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:construction.MainWindow.java

License:Open Source License

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    try {//from w  w  w.  j  av a  2 s .  c  o m

        //    System.out.println("Parsing file, " + inputFile);
        //    FileInputStream fis = new FileInputStream(inputFile);
        //    ANTLRInputStream input = new ANTLRInputStream(fis);
        ANTLRInputStream input = new ANTLRInputStream(jTextArea1.getText());
        testLexer lexer = new testLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        testParser parser = new testParser(tokens);
        QuestionForm q = parser.questionaire().form;

        HashMap<String, IType> memory = new HashMap();
        TypeChecker(q.getQuestions(), memory);

        //        q.render();
    } catch (RecognitionException ex) {
        Logger.getLogger(Construction.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:controle.analiseTexto.AnalisePeriodo.java

public void etiquetarPadrao(String sentencaAnalisada) {
    ArrayList<String> palavras = new ArrayList<String>();
    CommonToken ob = null;//from ww w .  j  ava 2s .  c o m

    SrsGrammarLexer lexer = new SrsGrammarLexer(new ANTLRInputStream(sentencaAnalisada));
    tokens = new CommonTokenStream(lexer);
    System.out.println(tokens.getText());
    Iterator it = tokens.getTokens().iterator();

    while (it.hasNext()) {
        ob = (CommonToken) it.next();
        palavras.add(ob.getText().toLowerCase());
    }

    tagger = new Etiquetador(palavras);
    tagger.etiquetar();
}

From source file:controle.analiseTexto.AnalisePeriodo.java

public ArrayList<String> recuperarConceitosComplexos(String frase) throws Exception {

    SrsGrammarLexer lexer = new SrsGrammarLexer(new ANTLRInputStream(frase));
    tokens = new CommonTokenStream(lexer);
    SrsGrammarParser parser = new SrsGrammarParser(tokens);
    System.out.println(tokens.getText());

    tags = etiquetar(tokens);/* w ww . j av  a 2 s.c  o m*/
    parser.conceitos();

    listaConceitosComplexos.addAll(parser.conceitos);
    return parser.conceitos;
}

From source file:controle.analiseTexto.AnalisePeriodo.java

public ArrayList<String> recuperarAtributos(String frase) throws Exception {

    SrsGrammarLexer lexer = new SrsGrammarLexer(new ANTLRInputStream(frase));
    tokens = new CommonTokenStream(lexer);
    SrsGrammarParser parser = new SrsGrammarParser(tokens);
    System.out.println(tokens.getText());

    etiquetar(tokens);//from w  ww . j a v a2  s .co  m
    parser.atributos();

    listaConceitosComplexos.addAll(parser.conceitos);
    return parser.conceitos;
}

From source file:controle.analiseTexto.AnalisePeriodo.java

public ArrayList<String> recuperarOutrosConceitosComplexos(String frase) throws Exception {

    SrsGrammarLexer lexer = new SrsGrammarLexer(new ANTLRInputStream(frase));
    tokens = new CommonTokenStream(lexer);
    SrsGrammarParser parser = new SrsGrammarParser(tokens);
    System.out.println(tokens.getText());
    tags = etiquetar(tokens);/*from   w w w  .  ja v  a 2  s  . c om*/
    parser.conceitos_1();
    listaConceitosComplexos.addAll(parser.conceitos);
    //System.out.println("Conceitos:" + parser.conceitos);
    return parser.conceitos;
}