Search in sources :

Example 1 with ParseTree

use of org.antlr.runtime.tree.ParseTree in project antlrworks by antlr.

the class InterpreterTab method process.

protected void process() {
    progress.setInfo("Interpreting...");
    window.consoleTab.println("Interpreting...");
    CharStream input = new ANTLRStringStream(Utils.convertRawTextWithEOL(textPane.getText(), eolCombo));
    ANTLRGrammarEngine eg = window.getGrammarEngine().getANTLRGrammarEngine();
    try {
        eg.createGrammars();
    } catch (Exception e) {
        window.consoleTab.println(e);
        return;
    }
    Grammar parser = eg.getParserGrammar();
    Grammar lexer = eg.getLexerGrammar();
    if (lexer == null) {
        throw new RuntimeException("Lexer is null. Check the grammar before running the interpreterTab.");
    }
    Interpreter lexEngine = new CustomInterpreter(lexer, input);
    FilteringTokenStream tokens = new FilteringTokenStream(lexEngine);
    StringTokenizer tk = new StringTokenizer(tokensToIgnoreLabel.getText(), " ");
    while (tk.hasMoreTokens()) {
        String tokenName = tk.nextToken();
        tokens.setTokenTypeChannel(lexer.getTokenType(tokenName), Token.HIDDEN_CHANNEL);
    }
    Interpreter parseEngine = new CustomInterpreter(parser, tokens);
    ParseTree t = null;
    try {
        if (ATEToken.isLexerName(startSymbol)) {
            t = lexEngine.parse(startSymbol);
        } else {
            t = parseEngine.parse(startSymbol);
        }
    } catch (Exception e) {
        window.consoleTab.println(e);
    }
    if (parser != null && t != null) {
        SwingUtilities.invokeLater(new Refresh(parser, t));
    }
}
Also used : Interpreter(org.antlr.tool.Interpreter) Grammar(org.antlr.tool.Grammar) ANTLRGrammarEngine(org.antlr.works.grammar.antlr.ANTLRGrammarEngine) StringTokenizer(java.util.StringTokenizer) ParseTree(org.antlr.runtime.tree.ParseTree)

Aggregations

StringTokenizer (java.util.StringTokenizer)1 ParseTree (org.antlr.runtime.tree.ParseTree)1 Grammar (org.antlr.tool.Grammar)1 Interpreter (org.antlr.tool.Interpreter)1 ANTLRGrammarEngine (org.antlr.works.grammar.antlr.ANTLRGrammarEngine)1