Search in sources :

Example 6 with TokenMgrError

use of net.sourceforge.pmd.lang.ast.TokenMgrError in project pmd by pmd.

the class ApexTokenizer method tokenize.

@Override
public void tokenize(SourceCode sourceCode, Tokens tokenEntries) {
    StringBuilder code = sourceCode.getCodeBuffer();
    ANTLRStringStream ass = new ANTLRStringStream(code.toString());
    ApexLexer lexer = new ApexLexer(ass) {

        public void emitErrorMessage(String msg) {
            throw new TokenMgrError(msg, TokenMgrError.LEXICAL_ERROR);
        }
    };
    try {
        Token token = lexer.nextToken();
        while (token.getType() != Token.EOF) {
            if (token.getChannel() != Lexer.HIDDEN) {
                String tokenText = token.getText();
                if (!caseSensitive) {
                    tokenText = tokenText.toLowerCase(Locale.ROOT);
                }
                TokenEntry tokenEntry = new TokenEntry(tokenText, sourceCode.getFileName(), token.getLine());
                tokenEntries.add(tokenEntry);
            }
            token = lexer.nextToken();
        }
    } finally {
        tokenEntries.add(TokenEntry.getEOF());
    }
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) ApexLexer(apex.jorje.parser.impl.ApexLexer) TokenMgrError(net.sourceforge.pmd.lang.ast.TokenMgrError) Token(org.antlr.runtime.Token)

Example 7 with TokenMgrError

use of net.sourceforge.pmd.lang.ast.TokenMgrError 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)

Example 8 with TokenMgrError

use of net.sourceforge.pmd.lang.ast.TokenMgrError in project pmd by pmd.

the class PythonTokenizer method tokenize.

@Override
public void tokenize(SourceCode sourceCode, Tokens tokenEntries) {
    StringBuilder buffer = sourceCode.getCodeBuffer();
    Reader reader = null;
    try {
        LanguageVersionHandler languageVersionHandler = LanguageRegistry.getLanguage(PythonLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler();
        reader = new StringReader(buffer.toString());
        reader = IOUtil.skipBOM(reader);
        TokenManager tokenManager = languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).getTokenManager(sourceCode.getFileName(), reader);
        Token currentToken = (Token) tokenManager.getNextToken();
        while (currentToken.image.length() > 0) {
            tokenEntries.add(new TokenEntry(currentToken.image, sourceCode.getFileName(), currentToken.beginLine));
            currentToken = (Token) tokenManager.getNextToken();
        }
        tokenEntries.add(TokenEntry.getEOF());
        System.err.println("Added " + sourceCode);
    } catch (TokenMgrError err) {
        err.printStackTrace();
        System.err.println("Skipping " + sourceCode + " due to parse error");
        tokenEntries.add(TokenEntry.getEOF());
    } finally {
        IOUtils.closeQuietly(reader);
    }
}
Also used : StringReader(java.io.StringReader) StringReader(java.io.StringReader) Reader(java.io.Reader) Token(net.sourceforge.pmd.lang.python.ast.Token) TokenMgrError(net.sourceforge.pmd.lang.ast.TokenMgrError) LanguageVersionHandler(net.sourceforge.pmd.lang.LanguageVersionHandler) TokenManager(net.sourceforge.pmd.lang.TokenManager)

Example 9 with TokenMgrError

use of net.sourceforge.pmd.lang.ast.TokenMgrError in project pmd by pmd.

the class MatlabTokenizer method tokenize.

@Override
public void tokenize(SourceCode sourceCode, Tokens tokenEntries) {
    StringBuilder buffer = sourceCode.getCodeBuffer();
    Reader reader = null;
    try {
        LanguageVersionHandler languageVersionHandler = LanguageRegistry.getLanguage(MatlabLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler();
        reader = new StringReader(buffer.toString());
        reader = IOUtil.skipBOM(reader);
        TokenManager tokenManager = languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).getTokenManager(sourceCode.getFileName(), reader);
        Token currentToken = (Token) tokenManager.getNextToken();
        while (currentToken.image.length() > 0) {
            tokenEntries.add(new TokenEntry(currentToken.image, sourceCode.getFileName(), currentToken.beginLine));
            currentToken = (Token) tokenManager.getNextToken();
        }
        tokenEntries.add(TokenEntry.getEOF());
        System.err.println("Added " + sourceCode.getFileName());
    } catch (TokenMgrError err) {
        err.printStackTrace();
        System.err.println("Skipping " + sourceCode.getFileName() + " due to parse error");
        tokenEntries.add(TokenEntry.getEOF());
    } finally {
        IOUtils.closeQuietly(reader);
    }
}
Also used : StringReader(java.io.StringReader) StringReader(java.io.StringReader) Reader(java.io.Reader) Token(net.sourceforge.pmd.lang.matlab.ast.Token) TokenMgrError(net.sourceforge.pmd.lang.ast.TokenMgrError) LanguageVersionHandler(net.sourceforge.pmd.lang.LanguageVersionHandler) TokenManager(net.sourceforge.pmd.lang.TokenManager)

Aggregations

TokenMgrError (net.sourceforge.pmd.lang.ast.TokenMgrError)9 Reader (java.io.Reader)5 StringReader (java.io.StringReader)5 LanguageVersionHandler (net.sourceforge.pmd.lang.LanguageVersionHandler)5 TokenManager (net.sourceforge.pmd.lang.TokenManager)5 ApexLexer (apex.jorje.parser.impl.ApexLexer)1 Token (groovyjarjarantlr.Token)1 TokenStream (groovyjarjarantlr.TokenStream)1 TokenStreamException (groovyjarjarantlr.TokenStreamException)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 TokenEntry (net.sourceforge.pmd.cpd.TokenEntry)1 Token (net.sourceforge.pmd.lang.cpp.ast.Token)1 Token (net.sourceforge.pmd.lang.ecmascript5.ast.Token)1 Token (net.sourceforge.pmd.lang.matlab.ast.Token)1 Token (net.sourceforge.pmd.lang.objectivec.ast.Token)1 Token (net.sourceforge.pmd.lang.python.ast.Token)1 SwiftLexer (net.sourceforge.pmd.lang.swift.antlr4.SwiftLexer)1 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)1 Token (org.antlr.runtime.Token)1