Search in sources :

Example 21 with CommonToken

use of org.antlr.v4.runtime.CommonToken in project antlr4 by antlr.

the class TestCommonTokenStream method testSingleEOF.

@Test
public void testSingleEOF() throws Exception {
    TokenSource lexer = new TokenSource() {

        @Override
        public Token nextToken() {
            return new CommonToken(Token.EOF);
        }

        @Override
        public int getLine() {
            return 0;
        }

        @Override
        public int getCharPositionInLine() {
            return 0;
        }

        @Override
        public CharStream getInputStream() {
            return null;
        }

        @Override
        public String getSourceName() {
            return IntStream.UNKNOWN_SOURCE_NAME;
        }

        @Override
        public TokenFactory<?> getTokenFactory() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void setTokenFactory(TokenFactory<?> factory) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    };
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    tokens.fill();
    assertEquals(Token.EOF, tokens.LA(1));
    assertEquals(0, tokens.index());
    assertEquals(1, tokens.size());
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) TokenSource(org.antlr.v4.runtime.TokenSource) TokenFactory(org.antlr.v4.runtime.TokenFactory) CommonToken(org.antlr.v4.runtime.CommonToken) Test(org.junit.Test)

Example 22 with CommonToken

use of org.antlr.v4.runtime.CommonToken in project antlr4 by antlr.

the class TestCommonTokenStream method testCannotConsumeEOF.

@Test(expected = IllegalStateException.class)
public void testCannotConsumeEOF() throws Exception {
    TokenSource lexer = new TokenSource() {

        @Override
        public Token nextToken() {
            return new CommonToken(Token.EOF);
        }

        @Override
        public int getLine() {
            return 0;
        }

        @Override
        public int getCharPositionInLine() {
            return 0;
        }

        @Override
        public CharStream getInputStream() {
            return null;
        }

        @Override
        public String getSourceName() {
            return IntStream.UNKNOWN_SOURCE_NAME;
        }

        @Override
        public TokenFactory<?> getTokenFactory() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void setTokenFactory(TokenFactory<?> factory) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    };
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    tokens.fill();
    assertEquals(Token.EOF, tokens.LA(1));
    assertEquals(0, tokens.index());
    assertEquals(1, tokens.size());
    tokens.consume();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) TokenSource(org.antlr.v4.runtime.TokenSource) TokenFactory(org.antlr.v4.runtime.TokenFactory) CommonToken(org.antlr.v4.runtime.CommonToken) Test(org.junit.Test)

Example 23 with CommonToken

use of org.antlr.v4.runtime.CommonToken in project antlr4 by antlr.

the class GrammarASTAdaptor method create.

@Override
public /** Make sure even imaginary nodes know the input stream */
Object create(int tokenType, String text) {
    GrammarAST t;
    if (tokenType == ANTLRParser.RULE) {
        // needed by TreeWizard to make RULE tree
        t = new RuleAST(new CommonToken(tokenType, text));
    } else if (tokenType == ANTLRParser.STRING_LITERAL) {
        // implicit lexer construction done with wizard; needs this node type
        // whereas grammar ANTLRParser.g can use token option to spec node type
        t = new TerminalAST(new CommonToken(tokenType, text));
    } else {
        t = (GrammarAST) super.create(tokenType, text);
    }
    t.token.setInputStream(input);
    return t;
}
Also used : RuleAST(org.antlr.v4.tool.ast.RuleAST) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) CommonToken(org.antlr.runtime.CommonToken) TerminalAST(org.antlr.v4.tool.ast.TerminalAST)

Example 24 with CommonToken

use of org.antlr.v4.runtime.CommonToken in project antlr4 by antlr.

the class LeftRecursiveRuleAnalyzer method addPrecedenceArgToRules.

public AltAST addPrecedenceArgToRules(AltAST t, int prec) {
    if (t == null)
        return null;
    // get all top-level rule refs from ALT
    List<GrammarAST> outerAltRuleRefs = t.getNodesWithTypePreorderDFS(IntervalSet.of(RULE_REF));
    for (GrammarAST x : outerAltRuleRefs) {
        RuleRefAST rref = (RuleRefAST) x;
        boolean recursive = rref.getText().equals(ruleName);
        boolean rightmost = rref == outerAltRuleRefs.get(outerAltRuleRefs.size() - 1);
        if (recursive && rightmost) {
            GrammarAST dummyValueNode = new GrammarAST(new CommonToken(ANTLRParser.INT, "" + prec));
            rref.setOption(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME, dummyValueNode);
        }
    }
    return t;
}
Also used : RuleRefAST(org.antlr.v4.tool.ast.RuleRefAST) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) CommonToken(org.antlr.runtime.CommonToken)

Example 25 with CommonToken

use of org.antlr.v4.runtime.CommonToken in project sqldelight by square.

the class TokenStreamSubset method setIndexOfLastToken.

public void setIndexOfLastToken(int indexOfLastToken) {
    System.out.println("setIndexOfLastToken(" + indexOfLastToken + ")");
    if (indexOfLastToken < 0) {
        System.out.println("replacing " + saveToken.getTokenIndex() + " with " + saveToken);
        tokens.set(saveToken.getTokenIndex(), saveToken);
        //			this.indexOfLastToken = indexOfLastToken;
        return;
    }
    // we want to keep token at indexOfLastToken
    int i = indexOfLastToken + 1;
    sync(i);
    saveToken = tokens.get(i);
    System.out.println("saving " + saveToken);
    CommonToken stopToken = new CommonToken(saveToken);
    stopToken.setType(STOP_TOKEN_TYPE);
    System.out.println("setting " + i + " to " + stopToken);
    tokens.set(i, stopToken);
//		this.indexOfLastToken = indexOfLastToken;
}
Also used : CommonToken(org.antlr.v4.runtime.CommonToken)

Aggregations

CommonToken (org.antlr.v4.runtime.CommonToken)39 JavadocContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.JavadocContext)31 TextContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.TextContext)29 DescriptionContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.DescriptionContext)15 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 HtmlElementCloseContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.HtmlElementCloseContext)9 HtmlElementOpenContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.HtmlElementOpenContext)9 HtmlTagContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.HtmlTagContext)9 AttributeContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.AttributeContext)5 PTagOpenContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.PTagOpenContext)5 CommonToken (org.antlr.runtime.CommonToken)5 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)5 ParametersContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.ParametersContext)4 TokenFactory (org.antlr.v4.runtime.TokenFactory)4 TokenSource (org.antlr.v4.runtime.TokenSource)4 Test (org.junit.Test)4 PTagCloseContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.PTagCloseContext)3