Search in sources :

Example 26 with EOF

use of org.antlr.v4.runtime.Recognizer.EOF in project cafebabepy by cafebabepy.

the class CafeBabePyLexer method nextToken.

@Override
public Token nextToken() {
    boolean atStartOfInput = getCharPositionInLine() == 0 && getLine() == 1;
    readNewLine();
    if (this.opened == 0 && (this.newLineBuilder.length() > 0 || atStartOfInput)) {
        int la = readSpaces();
        if (atStartOfInput && this.spacesBuilder.length() == 0) {
        // skip
        } else {
            if (la == '\r' || la == '\n' || la == '\f') {
                return nextToken();
            } else if (la == '#') {
                return super.nextToken();
            }
            String spaces = this.spacesBuilder.toString();
            int indent = getIndentCount(spaces);
            int previous = this.indents.isEmpty() ? 0 : this.indents.peekFirst();
            if (indent == previous) {
                newLine(false);
            // skip
            } else if (indent > previous) {
                if (la != EOF) {
                    newLine(false);
                    this.indents.addFirst(indent);
                    emitOnly(createCommonToken(INDENT, spaces, this.spaceCharIndex));
                }
            } else {
                newLine(false);
                while (!this.indents.isEmpty() && this.indents.peekFirst() > indent) {
                    CommonToken dedent = new CommonToken(DEDENT, "<DEDENT>");
                    dedent.setLine(getLine());
                    emitOnly(dedent);
                    this.indents.removeFirst();
                }
            }
        }
    }
    Token next;
    while ((next = super.nextToken()) == null) ;
    int la = this._input.LA(1);
    if (la == EOF) {
        if (!this.eof) {
            for (int i = this.tokens.size() - 1; i >= 0; i--) {
                if (this.tokens.get(i).getType() == EOF) {
                    this.tokens.remove(i);
                }
            }
            if (!this.indents.isEmpty()) {
                newLine(true);
                while (!this.indents.isEmpty()) {
                    CommonToken dedent = new CommonToken(DEDENT, "<DEDENT>");
                    int dedentLine = (this.lastToken != null) ? this.lastToken.getLine() : getLine();
                    dedent.setLine(dedentLine);
                    emitOnly(dedent);
                    this.indents.removeFirst();
                }
                newEof();
            } else {
                newLine(true);
                newEof();
            }
            this.eof = true;
        }
    }
    if (next.getChannel() == Token.DEFAULT_CHANNEL) {
        this.lastToken = next;
    }
    Token token = this.tokens.isEmpty() ? next : this.tokens.poll();
    return token;
}
Also used : CommonToken(org.antlr.v4.runtime.CommonToken) Token(org.antlr.v4.runtime.Token) CommonToken(org.antlr.v4.runtime.CommonToken)

Example 27 with EOF

use of org.antlr.v4.runtime.Recognizer.EOF in project contribution by checkstyle.

the class ExpectedParseTreeGenerator method walk.

public String walk(ParseTree t, String parentObjectName) {
    final String className = t.getClass().getSimpleName();
    String id = null;
    if (t instanceof TerminalNode) {
        final TerminalNodeImpl terminal = (TerminalNodeImpl) t;
        final int type = terminal.symbol.getType();
        String tokenType = "";
        if (type == -1) {
            tokenType = "EOF";
        } else {
            tokenType = JavadocUtils.getTokenName(type);
        }
        String text = terminal.getText();
        if ("\n".equals(text)) {
            text = "\\n";
        } else if ("\t".equals(text)) {
            text = "\\t";
        } else {
            text = text.replace("\"", "\\\"");
        }
        final int number = getVariableCounter(tokenType);
        id = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, tokenType.toLowerCase()) + number;
        System.out.println("    CommonToken " + id + " = new CommonToken(JavadocTokenTypes." + tokenType + ", \"" + text + "\");");
    } else {
        int number = getVariableCounter(className);
        id = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, className) + number++;
        System.out.println("    " + className + " " + id + " = new " + className + "(" + parentObjectName + ", 0);");
        final int n = t.getChildCount();
        for (int i = 0; i < n; i++) {
            final String childId = walk(t.getChild(i), id);
            System.out.println("    " + id + ".addChild(" + childId + ");");
        }
    }
    return id;
}
Also used : TerminalNodeImpl(org.antlr.v4.runtime.tree.TerminalNodeImpl) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode)

Example 28 with EOF

use of org.antlr.v4.runtime.Recognizer.EOF in project titan.EclipsePlug-ins by eclipse.

the class ModuleLevelTokenStreamTracker method getBlock.

private boolean getBlock(final Token first) {
    // return true if EOF hit
    Token t;
    TokenWithIndexAndSubTokens result;
    t = getTokenSource().nextToken();
    if (t instanceof WritableToken) {
        ((WritableToken) t).setTokenIndex(tokens.size());
    }
    List<Token> tokenList = new ArrayList<Token>();
    int nofUnclosedParanthesis = 1;
    while (t != null && t.getType() != Token.EOF) {
        if (t.getType() == Asn1Lexer.BEGINCHAR) {
            nofUnclosedParanthesis++;
        } else if (t.getType() == Asn1Lexer.ENDCHAR) {
            nofUnclosedParanthesis--;
            if (nofUnclosedParanthesis == 0) {
                result = new TokenWithIndexAndSubTokens(Asn1Lexer.BLOCK, tokenList, sourceFile);
                result.setCharPositionInLine(first.getCharPositionInLine());
                result.setLine(first.getLine());
                result.setStartIndex(((TokenWithIndexAndSubTokens) first).getStopIndex());
                result.setStopIndex(((TokenWithIndexAndSubTokens) t).getStopIndex());
                result.setText(makeString(tokenList));
                tokens.add(result);
                return false;
            }
        }
        if (!discardMask.contains(Integer.valueOf(t.getType()))) {
            tokenList.add(new TokenWithIndexAndSubTokens(t));
        }
        t = getTokenSource().nextToken();
    }
    result = new TokenWithIndexAndSubTokens(Asn1Lexer.BLOCK, tokenList, sourceFile);
    result.setCharPositionInLine(first.getCharPositionInLine());
    result.setLine(first.getLine());
    result.setStartIndex(((TokenWithIndexAndSubTokens) first).getStopIndex());
    if (t != null) {
        result.setStopIndex(((TokenWithIndexAndSubTokens) t).getStopIndex());
    }
    tokens.add(result);
    return true;
}
Also used : ArrayList(java.util.ArrayList) Token(org.antlr.v4.runtime.Token) WritableToken(org.antlr.v4.runtime.WritableToken) WritableToken(org.antlr.v4.runtime.WritableToken)

Example 29 with EOF

use of org.antlr.v4.runtime.Recognizer.EOF in project antlr4 by tunnelvisionlabs.

the class TestATNInterpreter method testMustTrackPreviousGoodAlt2WithEOF.

@Test(expected = NoViableAltException.class)
public void testMustTrackPreviousGoodAlt2WithEOF() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n" + "D : 'd' ;\n");
    Grammar g = new Grammar("parser grammar T;\n" + "a : (A | A B | A B C) EOF;");
    checkMatchedAlt(lg, g, "a", 1);
    checkMatchedAlt(lg, g, "ab", 2);
    checkMatchedAlt(lg, g, "abc", 3);
    try {
        checkMatchedAlt(lg, g, "abd", 1);
    } catch (NoViableAltException re) {
        assertEquals(2, re.getOffendingToken().getTokenIndex());
        assertEquals(4, re.getOffendingToken().getType());
        throw re;
    }
}
Also used : NoViableAltException(org.antlr.v4.runtime.NoViableAltException) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 30 with EOF

use of org.antlr.v4.runtime.Recognizer.EOF in project antlr4 by tunnelvisionlabs.

the class TestATNLexerInterpreter method testRecursiveLexerRuleRefWithWildcard.

@Test
public void testRecursiveLexerRuleRefWithWildcard() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "CMT : '/*' (CMT | .)*? '*/' ;\n" + "WS : (' '|'\\n')+ ;");
    String expecting = "CMT, WS, CMT, WS, EOF";
    checkLexerMatches(lg, "/* ick */\n" + "/* /* */\n" + "/* /*nested*/ */\n", expecting);
}
Also used : LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)218 LexerGrammar (org.antlr.v4.tool.LexerGrammar)182 Grammar (org.antlr.v4.tool.Grammar)110 CommonToken (org.antlr.v4.runtime.CommonToken)35 JavadocContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.JavadocContext)31 TextContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.TextContext)29 Token (org.antlr.v4.runtime.Token)19 ArrayList (java.util.ArrayList)18 ATN (org.antlr.v4.runtime.atn.ATN)18 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)18 DescriptionContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.DescriptionContext)15 ParseTree (org.antlr.v4.runtime.tree.ParseTree)13 HtmlElementContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.HtmlElementContext)12 JavadocTagContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.JavadocTagContext)12 JavadocInlineTagContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.JavadocInlineTagContext)10 ReferenceContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.ReferenceContext)10 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)10 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)10 HtmlElementCloseContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.HtmlElementCloseContext)9 HtmlElementOpenContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.HtmlElementOpenContext)9