Search in sources :

Example 1 with PRIVATE

use of org.apache.groovy.parser.antlr4.GroovyParser.PRIVATE in project groovy by apache.

the class AstBuilder method transformRecordHeaderToProperties.

private void transformRecordHeaderToProperties(ClassDeclarationContext ctx, ClassNode classNode) {
    Parameter[] parameters = this.visitFormalParameters(ctx.formalParameters());
    classNode.putNodeMetaData(RECORD_HEADER, parameters);
    final int n = parameters.length;
    for (int i = 0; i < n; i += 1) {
        Parameter parameter = parameters[i];
        FormalParameterContext parameterCtx = parameter.getNodeMetaData(PARAMETER_CONTEXT);
        ModifierManager parameterModifierManager = parameter.getNodeMetaData(PARAMETER_MODIFIER_MANAGER);
        ClassNode originType = parameter.getOriginType();
        PropertyNode propertyNode = declareProperty(parameterCtx, parameterModifierManager, originType, classNode, i, parameter, parameter.getName(), parameter.getModifiers(), parameter.getInitialExpression());
        propertyNode.getField().putNodeMetaData(IS_RECORD_GENERATED, Boolean.TRUE);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) PropertyNode(org.codehaus.groovy.ast.PropertyNode) FormalParameterContext(org.apache.groovy.parser.antlr4.GroovyParser.FormalParameterContext) ThisFormalParameterContext(org.apache.groovy.parser.antlr4.GroovyParser.ThisFormalParameterContext) Parameter(org.codehaus.groovy.ast.Parameter)

Example 2 with PRIVATE

use of org.apache.groovy.parser.antlr4.GroovyParser.PRIVATE in project groovy by apache.

the class SmartDocumentFilter method findTokensToRender.

private List<Token> findTokensToRender(List<Token> tokenList) {
    final Tuple2<Integer, Integer> renderRange = getRenderRange();
    if (null != renderRange) {
        long startLine = lineNumber(renderRange.getV1());
        long stopLine = lineNumber(renderRange.getV2());
        // should never happen
        if (startLine < 0 || stopLine < 0)
            return tokenList;
        return tokenList.stream().filter(e -> e.getLine() >= startLine && PositionConfigureUtils.endPosition(e).getV1() <= stopLine).collect(Collectors.toList());
    }
    List<Token> tmpLatestTokenList = filterNewlines(this.latestTokenList);
    int latestTokenListSize = tmpLatestTokenList.size();
    if (0 == latestTokenListSize) {
        return tokenList;
    }
    List<Token> tmpTokenList = filterNewlines(tokenList);
    int tmpTokenListSize = tmpTokenList.size();
    if (0 == tmpTokenListSize) {
        return tokenList;
    }
    int startTokenIndex = 0;
    int minSize = Math.min(tmpTokenListSize, latestTokenListSize);
    for (int i = 0; i < minSize; i++) {
        Token token = tmpTokenList.get(i);
        Token latestToken = tmpLatestTokenList.get(i);
        if (token.getType() == latestToken.getType() && token.getStartIndex() == latestToken.getStartIndex() && token.getStopIndex() == latestToken.getStopIndex()) {
            continue;
        }
        startTokenIndex = i;
        break;
    }
    List<Token> newTokenList = new ReversedList<>(tmpTokenList);
    List<Token> newLatestTokenList = new ReversedList<>(tmpLatestTokenList);
    int stopTokenIndex = tmpTokenListSize;
    Token lastToken = newTokenList.get(0);
    Token lastLatestToken = newLatestTokenList.get(0);
    for (int i = 0; i < minSize; i++) {
        Token token = newTokenList.get(i);
        Token latestToken = newLatestTokenList.get(i);
        if ((token.getType() == latestToken.getType()) && (token.getStartIndex() - lastToken.getStartIndex()) == (latestToken.getStartIndex() - lastLatestToken.getStartIndex()) && ((token.getStopIndex() - lastToken.getStopIndex()) == (latestToken.getStopIndex() - lastLatestToken.getStopIndex()))) {
            continue;
        }
        stopTokenIndex = tmpTokenListSize - i;
        break;
    }
    if (startTokenIndex == stopTokenIndex) {
        return tmpTokenListSize != latestTokenListSize ? tokenList : Collections.emptyList();
    } else if (startTokenIndex < stopTokenIndex) {
        return tmpTokenList.subList(startTokenIndex, stopTokenIndex);
    }
    // should never reach here. If unexpected error occurred, it's better to render all tokens
    return tokenList;
}
Also used : Color(java.awt.Color) THROWS(org.apache.groovy.parser.antlr4.GroovyLexer.THROWS) Arrays(java.util.Arrays) FloatingPointLiteral(org.apache.groovy.parser.antlr4.GroovyLexer.FloatingPointLiteral) BooleanLiteral(org.apache.groovy.parser.antlr4.GroovyLexer.BooleanLiteral) ABSTRACT(org.apache.groovy.parser.antlr4.GroovyLexer.ABSTRACT) STRICTFP(org.apache.groovy.parser.antlr4.GroovyLexer.STRICTFP) Token(org.antlr.v4.runtime.Token) CONST(org.apache.groovy.parser.antlr4.GroovyLexer.CONST) DEF(org.apache.groovy.parser.antlr4.GroovyLexer.DEF) RETURN(org.apache.groovy.parser.antlr4.GroovyLexer.RETURN) DEFAULT(org.apache.groovy.parser.antlr4.GroovyLexer.DEFAULT) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) EOF(org.apache.groovy.parser.antlr4.GroovyLexer.EOF) IMPLEMENTS(org.apache.groovy.parser.antlr4.GroovyLexer.IMPLEMENTS) CharStreams(org.antlr.v4.runtime.CharStreams) THROW(org.apache.groovy.parser.antlr4.GroovyLexer.THROW) CASE(org.apache.groovy.parser.antlr4.GroovyLexer.CASE) GStringEnd(org.apache.groovy.parser.antlr4.GroovyLexer.GStringEnd) NATIVE(org.apache.groovy.parser.antlr4.GroovyLexer.NATIVE) ConsoleErrorListener(org.antlr.v4.runtime.ConsoleErrorListener) INSTANCEOF(org.apache.groovy.parser.antlr4.GroovyLexer.INSTANCEOF) SEALED(org.apache.groovy.parser.antlr4.GroovyLexer.SEALED) COMMA(org.apache.groovy.parser.antlr4.GroovyLexer.COMMA) AttributeSet(javax.swing.text.AttributeSet) BuiltInPrimitiveType(org.apache.groovy.parser.antlr4.GroovyLexer.BuiltInPrimitiveType) ELSE(org.apache.groovy.parser.antlr4.GroovyLexer.ELSE) NL(org.apache.groovy.parser.antlr4.GroovyLexer.NL) RECORD(org.apache.groovy.parser.antlr4.GroovyLexer.RECORD) PositionConfigureUtils(org.apache.groovy.parser.antlr4.util.PositionConfigureUtils) CONTINUE(org.apache.groovy.parser.antlr4.GroovyLexer.CONTINUE) FINAL(org.apache.groovy.parser.antlr4.GroovyLexer.FINAL) StringLiteral(org.apache.groovy.parser.antlr4.GroovyLexer.StringLiteral) BadLocationException(javax.swing.text.BadLocationException) Collectors(java.util.stream.Collectors) GOTO(org.apache.groovy.parser.antlr4.GroovyLexer.GOTO) GStringBegin(org.apache.groovy.parser.antlr4.GroovyLexer.GStringBegin) VAR(org.apache.groovy.parser.antlr4.GroovyLexer.VAR) WHILE(org.apache.groovy.parser.antlr4.GroovyLexer.WHILE) List(java.util.List) AS(org.apache.groovy.parser.antlr4.GroovyLexer.AS) NEW(org.apache.groovy.parser.antlr4.GroovyLexer.NEW) YIELD(org.apache.groovy.parser.antlr4.GroovyLexer.YIELD) CATCH(org.apache.groovy.parser.antlr4.GroovyLexer.CATCH) SWITCH(org.apache.groovy.parser.antlr4.GroovyLexer.SWITCH) PUBLIC(org.apache.groovy.parser.antlr4.GroovyLexer.PUBLIC) PACKAGE(org.apache.groovy.parser.antlr4.GroovyLexer.PACKAGE) DefaultStyledDocument(javax.swing.text.DefaultStyledDocument) ENUM(org.apache.groovy.parser.antlr4.GroovyLexer.ENUM) FINALLY(org.apache.groovy.parser.antlr4.GroovyLexer.FINALLY) NOT_IN(org.apache.groovy.parser.antlr4.GroovyLexer.NOT_IN) IN(org.apache.groovy.parser.antlr4.GroovyLexer.IN) StyleConstants(javax.swing.text.StyleConstants) GroovyLangLexer(org.apache.groovy.parser.antlr4.GroovyLangLexer) PROTECTED(org.apache.groovy.parser.antlr4.GroovyLexer.PROTECTED) STATIC(org.apache.groovy.parser.antlr4.GroovyLexer.STATIC) FOR(org.apache.groovy.parser.antlr4.GroovyLexer.FOR) VOLATILE(org.apache.groovy.parser.antlr4.GroovyLexer.VOLATILE) IntegerLiteral(org.apache.groovy.parser.antlr4.GroovyLexer.IntegerLiteral) UNEXPECTED_CHAR(org.apache.groovy.parser.antlr4.GroovyLexer.UNEXPECTED_CHAR) NON_SEALED(org.apache.groovy.parser.antlr4.GroovyLexer.NON_SEALED) Tuple2(groovy.lang.Tuple2) StyleContext(javax.swing.text.StyleContext) TRY(org.apache.groovy.parser.antlr4.GroovyLexer.TRY) CharStream(org.antlr.v4.runtime.CharStream) GroovySyntaxError(org.apache.groovy.parser.antlr4.GroovySyntaxError) SUPER(org.apache.groovy.parser.antlr4.GroovyLexer.SUPER) ASSERT(org.apache.groovy.parser.antlr4.GroovyLexer.ASSERT) NullLiteral(org.apache.groovy.parser.antlr4.GroovyLexer.NullLiteral) StringUtils(org.apache.groovy.parser.antlr4.util.StringUtils) GStringPart(org.apache.groovy.parser.antlr4.GroovyLexer.GStringPart) SEMI(org.apache.groovy.parser.antlr4.GroovyLexer.SEMI) SYNCHRONIZED(org.apache.groovy.parser.antlr4.GroovyLexer.SYNCHRONIZED) IF(org.apache.groovy.parser.antlr4.GroovyLexer.IF) PRIVATE(org.apache.groovy.parser.antlr4.GroovyLexer.PRIVATE) BREAK(org.apache.groovy.parser.antlr4.GroovyLexer.BREAK) TRANSIENT(org.apache.groovy.parser.antlr4.GroovyLexer.TRANSIENT) THREADSAFE(org.apache.groovy.parser.antlr4.GroovyLexer.THREADSAFE) LexerNoViableAltException(org.antlr.v4.runtime.LexerNoViableAltException) IMPORT(org.apache.groovy.parser.antlr4.GroovyLexer.IMPORT) IOException(java.io.IOException) DO(org.apache.groovy.parser.antlr4.GroovyLexer.DO) ReversedList(org.apache.groovy.util.ReversedList) THIS(org.apache.groovy.parser.antlr4.GroovyLexer.THIS) DocumentFilter(javax.swing.text.DocumentFilter) StringReader(java.io.StringReader) Style(javax.swing.text.Style) INTERFACE(org.apache.groovy.parser.antlr4.GroovyLexer.INTERFACE) TRAIT(org.apache.groovy.parser.antlr4.GroovyLexer.TRAIT) VOID(org.apache.groovy.parser.antlr4.GroovyLexer.VOID) EXTENDS(org.apache.groovy.parser.antlr4.GroovyLexer.EXTENDS) PERMITS(org.apache.groovy.parser.antlr4.GroovyLexer.PERMITS) Collections(java.util.Collections) CLASS(org.apache.groovy.parser.antlr4.GroovyLexer.CLASS) NOT_INSTANCEOF(org.apache.groovy.parser.antlr4.GroovyLexer.NOT_INSTANCEOF) Token(org.antlr.v4.runtime.Token) ReversedList(org.apache.groovy.util.ReversedList)

Example 3 with PRIVATE

use of org.apache.groovy.parser.antlr4.GroovyParser.PRIVATE 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 4 with PRIVATE

use of org.apache.groovy.parser.antlr4.GroovyParser.PRIVATE 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 5 with PRIVATE

use of org.apache.groovy.parser.antlr4.GroovyParser.PRIVATE 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)4 Token (org.antlr.v4.runtime.Token)3 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 BadLocationException (javax.swing.text.BadLocationException)2 CharStream (org.antlr.v4.runtime.CharStream)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 LexerNoViableAltException (org.antlr.v4.runtime.LexerNoViableAltException)2 Tuple2 (groovy.lang.Tuple2)1 Color (java.awt.Color)1 Insets (java.awt.Insets)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 ButtonGroup (javax.swing.ButtonGroup)1 JToggleButton (javax.swing.JToggleButton)1 AttributeSet (javax.swing.text.AttributeSet)1 DefaultStyledDocument (javax.swing.text.DefaultStyledDocument)1 DocumentFilter (javax.swing.text.DocumentFilter)1