Search in sources :

Example 1 with TokenMgrError

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

the class ScalaTokenizer method tokenize.

public void tokenize(SourceCode source, Tokens cpdTokens) {
    String filename = source.getFileName();
    try {
        Lexer lexer = new Lexer();
        List<Token> tokens = lexer.getTokensOfFile(filename);
        for (Token token : tokens) {
            String tokenVal = token.tokenVal() != null ? token.tokenVal() : Integer.toString(token.tokenType());
            TokenEntry cpdToken = new TokenEntry(tokenVal, filename, token.line());
            cpdTokens.add(cpdToken);
        }
        cpdTokens.add(TokenEntry.getEOF());
    } catch (RuntimeException e) {
        e.printStackTrace();
        // option
        throw new TokenMgrError("Lexical error in file " + filename + ". The scala tokenizer exited with error: " + e.getMessage(), TokenMgrError.LEXICAL_ERROR);
    }
}
Also used : Lexer(org.sonar.plugins.scala.compiler.Lexer) TokenEntry(net.sourceforge.pmd.cpd.TokenEntry) Token(org.sonar.plugins.scala.compiler.Token) TokenMgrError(net.sourceforge.pmd.lang.ast.TokenMgrError)

Example 2 with TokenMgrError

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

the class ObjectiveCTokenizer method tokenize.

@Override
public void tokenize(SourceCode sourceCode, Tokens tokenEntries) {
    StringBuilder buffer = sourceCode.getCodeBuffer();
    Reader reader = null;
    try {
        LanguageVersionHandler languageVersionHandler = LanguageRegistry.getLanguage(ObjectiveCLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler();
        reader = new StringReader(buffer.toString());
        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.objectivec.ast.Token) TokenMgrError(net.sourceforge.pmd.lang.ast.TokenMgrError) LanguageVersionHandler(net.sourceforge.pmd.lang.LanguageVersionHandler) TokenManager(net.sourceforge.pmd.lang.TokenManager)

Example 3 with TokenMgrError

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

the class CPPTokenizer method tokenize.

@Override
public void tokenize(SourceCode sourceCode, Tokens tokenEntries) {
    StringBuilder buffer = sourceCode.getCodeBuffer();
    Reader reader = null;
    try {
        LanguageVersionHandler languageVersionHandler = LanguageRegistry.getLanguage(CppLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler();
        reader = new StringReader(maybeSkipBlocks(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());
    } catch (IOException e) {
        e.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) Reader(java.io.Reader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) Token(net.sourceforge.pmd.lang.cpp.ast.Token) TokenMgrError(net.sourceforge.pmd.lang.ast.TokenMgrError) IOException(java.io.IOException) LanguageVersionHandler(net.sourceforge.pmd.lang.LanguageVersionHandler) TokenManager(net.sourceforge.pmd.lang.TokenManager)

Example 4 with TokenMgrError

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

the class GroovyTokenizer method tokenize.

@Override
public void tokenize(SourceCode sourceCode, Tokens tokenEntries) {
    StringBuilder buffer = sourceCode.getCodeBuffer();
    GroovyLexer lexer = new GroovyLexer(IOUtils.toInputStream(buffer.toString()));
    TokenStream tokenStream = lexer.plumb();
    try {
        Token token = tokenStream.nextToken();
        while (token.getType() != Token.EOF_TYPE) {
            TokenEntry tokenEntry = new TokenEntry(token.getText(), sourceCode.getFileName(), token.getLine());
            tokenEntries.add(tokenEntry);
            token = tokenStream.nextToken();
        }
    } catch (TokenStreamException err) {
        // option
        throw new TokenMgrError("Lexical error in file " + sourceCode.getFileName() + " at line " + lexer.getLine() + ", column " + lexer.getColumn() + ".  Encountered: " + err.getMessage(), TokenMgrError.LEXICAL_ERROR);
    } finally {
        tokenEntries.add(TokenEntry.getEOF());
    }
}
Also used : TokenStreamException(groovyjarjarantlr.TokenStreamException) TokenStream(groovyjarjarantlr.TokenStream) GroovyLexer(org.codehaus.groovy.antlr.parser.GroovyLexer) Token(groovyjarjarantlr.Token) TokenMgrError(net.sourceforge.pmd.lang.ast.TokenMgrError)

Example 5 with TokenMgrError

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

the class EcmascriptTokenizer method tokenize.

@Override
public void tokenize(SourceCode sourceCode, Tokens tokenEntries) {
    StringBuilder buffer = sourceCode.getCodeBuffer();
    Reader reader = null;
    try {
        LanguageVersionHandler languageVersionHandler = LanguageRegistry.getLanguage(EcmascriptLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler();
        reader = new StringReader(buffer.toString());
        TokenManager tokenManager = languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).getTokenManager(sourceCode.getFileName(), reader);
        Token currentToken = (Token) tokenManager.getNextToken();
        while (currentToken.image.length() > 0) {
            tokenEntries.add(new TokenEntry(getTokenImage(currentToken), 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.ecmascript5.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