Search in sources :

Example 1 with GroovyLangLexer

use of org.apache.groovy.parser.antlr4.GroovyLangLexer in project groovy by apache.

the class SmartDocumentFilter method createLexer.

private GroovyLangLexer createLexer(String text) throws IOException {
    CharStream charStream = CharStreams.fromReader(new StringReader(text));
    GroovyLangLexer lexer = new GroovyLangLexer(charStream);
    lexer.setErrorIgnored(true);
    lexer.removeErrorListener(ConsoleErrorListener.INSTANCE);
    return lexer;
}
Also used : GroovyLangLexer(org.apache.groovy.parser.antlr4.GroovyLangLexer) StringReader(java.io.StringReader) CharStream(org.antlr.v4.runtime.CharStream)

Example 2 with GroovyLangLexer

use of org.apache.groovy.parser.antlr4.GroovyLangLexer in project groovy by apache.

the class SmartDocumentFilter method parseDocument.

private void parseDocument() throws BadLocationException {
    GroovyLangLexer lexer;
    try {
        lexer = createLexer(styledDocument.getText(0, styledDocument.getLength()));
    } catch (IOException e) {
        e.printStackTrace();
        this.latest = false;
        return;
    }
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    try {
        tokenStream.fill();
    } catch (LexerNoViableAltException | GroovySyntaxError e) {
        // ignore
        this.latest = false;
        return;
    } catch (Exception e) {
        e.printStackTrace();
        this.latest = false;
        return;
    }
    List<Token> tokenList = tokenStream.getTokens();
    List<Token> tokenListToRender;
    try {
        tokenListToRender = findTokensToRender(tokenList);
    } finally {
        this.setRenderRange(null);
    }
    for (Token token : tokenListToRender) {
        int tokenType = token.getType();
        if (EOF == tokenType) {
            continue;
        }
        int tokenStartIndex = token.getStartIndex();
        int tokenStopIndex = token.getStopIndex();
        int tokenLength = tokenStopIndex - tokenStartIndex + 1;
        styledDocument.setCharacterAttributes(tokenStartIndex, tokenLength, findStyleByTokenType(tokenType), true);
        if (GStringBegin == tokenType || GStringPart == tokenType) {
            styledDocument.setCharacterAttributes(tokenStartIndex + tokenLength - 1, 1, defaultStyle, true);
        }
    }
    this.latestTokenList = tokenList;
    this.latest = true;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) GroovyLangLexer(org.apache.groovy.parser.antlr4.GroovyLangLexer) LexerNoViableAltException(org.antlr.v4.runtime.LexerNoViableAltException) Token(org.antlr.v4.runtime.Token) IOException(java.io.IOException) GroovySyntaxError(org.apache.groovy.parser.antlr4.GroovySyntaxError) BadLocationException(javax.swing.text.BadLocationException) LexerNoViableAltException(org.antlr.v4.runtime.LexerNoViableAltException) IOException(java.io.IOException)

Example 3 with GroovyLangLexer

use of org.apache.groovy.parser.antlr4.GroovyLangLexer in project groovy by apache.

the class LexerFrame method scanScript.

private void scanScript(final StringReader reader) throws Exception {
    scriptPane.read(reader, null);
    reader.reset();
    // create lexer
    final GroovyLangLexer lexer = new GroovyLangLexer(reader);
    tokenPane.setEditable(true);
    tokenPane.setText("");
    int line = 1;
    final ButtonGroup bg = new ButtonGroup();
    Token token;
    int tokenCnt = 0;
    while (true) {
        token = lexer.nextToken();
        JToggleButton tokenButton = new JToggleButton(tokens.get(token.getType()));
        tokenButton.setFont(MONOSPACED_FONT);
        bg.add(tokenButton);
        tokenButton.addActionListener(this);
        tokenButton.setToolTipText(token.getText());
        tokenButton.putClientProperty("token", token);
        tokenButton.setMargin(new Insets(0, 1, 0, 1));
        tokenButton.setFocusPainted(false);
        if (token.getLine() > line) {
            tokenPane.getDocument().insertString(tokenPane.getDocument().getLength(), "\n", null);
            line = token.getLine();
        }
        insertComponent(tokenButton);
        tokenCnt++;
        if (eof().equals(token.getType())) {
            break;
        }
    }
    tokenStreamLabel.setText(" Token Stream(" + tokenCnt + "):");
    tokenPane.setEditable(false);
    tokenPane.setCaretPosition(0);
    reader.close();
}
Also used : Insets(java.awt.Insets) JToggleButton(javax.swing.JToggleButton) ButtonGroup(javax.swing.ButtonGroup) GroovyLangLexer(org.apache.groovy.parser.antlr4.GroovyLangLexer) Token(org.antlr.v4.runtime.Token)

Aggregations

GroovyLangLexer (org.apache.groovy.parser.antlr4.GroovyLangLexer)3 Token (org.antlr.v4.runtime.Token)2 Insets (java.awt.Insets)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ButtonGroup (javax.swing.ButtonGroup)1 JToggleButton (javax.swing.JToggleButton)1 BadLocationException (javax.swing.text.BadLocationException)1 CharStream (org.antlr.v4.runtime.CharStream)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 LexerNoViableAltException (org.antlr.v4.runtime.LexerNoViableAltException)1 GroovySyntaxError (org.apache.groovy.parser.antlr4.GroovySyntaxError)1