1 /*
2 * Sonar Scala Plugin
3 * Copyright (C) 2011 - 2014 All contributors
4 * dev@sonar.codehaus.org
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
19 */
20 package org.sonar.plugins.scala.cpd;
21
22 import java.util.List;
23
24 import net.sourceforge.pmd.cpd.SourceCode;
25 import net.sourceforge.pmd.cpd.TokenEntry;
26 import net.sourceforge.pmd.cpd.Tokenizer;
27 import net.sourceforge.pmd.cpd.Tokens;
28 import net.sourceforge.pmd.lang.ast.TokenMgrError;
29
30 import org.sonar.plugins.scala.compiler.Lexer;
31 import org.sonar.plugins.scala.compiler.Token;
32
33 /**
34 * Scala tokenizer for PMD CPD.
35 *
36 * @since 0.1
37 */
38 public final class ScalaTokenizer implements Tokenizer {
39
40 public void tokenize(SourceCode source, Tokens cpdTokens) {
41 String filename = source.getFileName();
42
43 try {
44 Lexer lexer = new Lexer();
45 List<Token> tokens = lexer.getTokensOfFile(filename);
46 for (Token token : tokens) {
47 String tokenVal =
48 token.tokenVal() != null ? token.tokenVal() : Integer.toString(token.tokenType());
49
50 TokenEntry cpdToken = new TokenEntry(tokenVal, filename, token.line());
51 cpdTokens.add(cpdToken);
52 }
53 cpdTokens.add(TokenEntry.getEOF());
54 } catch (RuntimeException e) {
55 e.printStackTrace();
56 // Wrap exceptions of the Scala tokenizer in a TokenMgrError, so they are correctly handled
57 // when CPD is executed with the '--skipLexicalErrors' command line option
58 throw new TokenMgrError(
59 "Lexical error in file " + filename + ". The scala tokenizer exited with error: " + e.getMessage(),
60 TokenMgrError.LEXICAL_ERROR);
61 }
62 }
63
64 }