Search in sources :

Example 1 with SwiftLexer

use of net.sourceforge.pmd.lang.swift.antlr4.SwiftLexer in project pmd by pmd.

the class SwiftTokenizer method tokenize.

@Override
public void tokenize(SourceCode sourceCode, Tokens tokenEntries) {
    StringBuilder buffer = sourceCode.getCodeBuffer();
    try {
        ANTLRInputStream ais = new ANTLRInputStream(buffer.toString());
        SwiftLexer lexer = new SwiftLexer(ais);
        lexer.removeErrorListeners();
        lexer.addErrorListener(new ErrorHandler());
        Token token = lexer.nextToken();
        while (token.getType() != Token.EOF) {
            if (token.getChannel() != Lexer.HIDDEN) {
                TokenEntry tokenEntry = new TokenEntry(token.getText(), sourceCode.getFileName(), token.getLine());
                tokenEntries.add(tokenEntry);
            }
            token = lexer.nextToken();
        }
    } catch (ANTLRSyntaxError err) {
        // option
        throw new TokenMgrError("Lexical error in file " + sourceCode.getFileName() + " at line " + err.getLine() + ", column " + err.getColumn() + ".  Encountered: " + err.getMessage(), TokenMgrError.LEXICAL_ERROR);
    } finally {
        tokenEntries.add(TokenEntry.getEOF());
    }
}
Also used : Token(org.antlr.v4.runtime.Token) TokenMgrError(net.sourceforge.pmd.lang.ast.TokenMgrError) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) SwiftLexer(net.sourceforge.pmd.lang.swift.antlr4.SwiftLexer)

Aggregations

TokenMgrError (net.sourceforge.pmd.lang.ast.TokenMgrError)1 SwiftLexer (net.sourceforge.pmd.lang.swift.antlr4.SwiftLexer)1 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)1 Token (org.antlr.v4.runtime.Token)1