Added support for Matlab to CPD.

This commit is contained in:
Jan van Nunen
2015-01-07 10:46:03 +01:00
parent 779ea00647
commit 16fabee7c3
17 changed files with 1656 additions and 0 deletions

View File

@ -0,0 +1,168 @@
/**
* This Matlab grammar is derived from MParser ANTLR grammar. (http://www.mit.edu/~wingated/code/mparser_0.1.tar.gz)
*
* The ' character is ambiguous, because it is the tranpose operator but can also denote the start of a string.
* (see https://www.mathworks.com/matlabcentral/newsreader/view_thread/25108)
*
* Rule of the thumb:
*
* A single quotation character is a tranpose operator if it directly follows a right bracket ("]"), right parenthesis (")"),
* right brace ("}"), letter, digit, underline ("_"), punctuation mark ("."), or another single quote character ("'"). *
*
* To implement this an extra lexical state 'TRANSPOSE' was introduced. In this state the single quote character "'" will always be parsed as the TRANSPOSE operator.
*/
options {
BUILD_PARSER=false;
CACHE_TOKENS=true;
UNICODE_INPUT = true;
}
PARSER_BEGIN(MatlabParser)
package net.sourceforge.pmd.lang.matlab.ast;
import net.sourceforge.pmd.lang.ast.CharStream;
import net.sourceforge.pmd.lang.ast.TokenMgrError;
public class MatlabParser {
}
PARSER_END(MatlabParser)
<DEFAULT, TRANSPOSE> SKIP :
{
" " : DEFAULT
|
"\t" : DEFAULT
|
"\r\n" : DEFAULT
|
"\n" : DEFAULT
|
"%{" : IN_COMMENT
|
"%" : IN_LINE_COMMENT
}
<IN_COMMENT> SKIP:
{
"%}" : DEFAULT
}
<IN_LINE_COMMENT> SKIP:
{
"\n" : DEFAULT
}
<IN_LINE_COMMENT, IN_COMMENT> MORE:
{
< ~[] >
}
<DEFAULT, TRANSPOSE> TOKEN : /* SEPARATORS AND OTHER USEFULL LANGUAGE CONSTRUCTS*/
{
< SEMI: ";" > : DEFAULT
| < LPAREN: "(" > : DEFAULT
| < RPAREN: ")" > : TRANSPOSE
| < LBRACE: "{" > : DEFAULT
| < RBRACE: "}" > : TRANSPOSE
| < LSBRACE: "[" > : DEFAULT
| < RSBRACE: "]" > : TRANSPOSE
| < AT: "@" > : DEFAULT
| < DOT: "." > : TRANSPOSE
| < COMMA: "," > : DEFAULT
}
<DEFAULT, TRANSPOSE> TOKEN : /* OPERATORS AND ASSIGNMENTS */
{
< DOUBLE_EQ: "==" > : DEFAULT
| < LOG_OR: "||" > : DEFAULT
| < LOG_AND: "&&" > : DEFAULT
| < LSTE: "<=" > : DEFAULT
| < GRTE: ">=" > : DEFAULT
| < NEQ: "~=" > : DEFAULT
| < EL_TIMES: ".*" > : DEFAULT
| < EL_LEFTDIV: "./" > : DEFAULT
| < EL_RIGHTDIV: ".\\" > : DEFAULT
| < EL_EXP: ".^" > : DEFAULT
| < EL_CCT: ".'" > : DEFAULT
| < EQ: "=" > : DEFAULT
| < BIN_OR: "|" > : DEFAULT
| < BIN_AND: "&" > : DEFAULT
| < LST: "<" > : DEFAULT
| < GRT: ">" > : DEFAULT
| < COLON: ":" > : DEFAULT
| < PLUS: "+" > : DEFAULT
| < MINUS: "-" > : DEFAULT
| < NEG: "~" > : DEFAULT
| < TIMES: "*" > : DEFAULT
| < LEFTDIV: "/" > : DEFAULT
| < RIGHTDIV: "\\" > : DEFAULT
| < EXP: "^" > : DEFAULT
}
<DEFAULT, TRANSPOSE> TOKEN : /* KEYWORDS */
{
< BREAK: "break" > : DEFAULT
| < CASE: "case" > : DEFAULT
| < CATCH: "catch" > : DEFAULT
| < CONTINUE: "continue" > : DEFAULT
| < ELSE: "else" > : DEFAULT
| < ELSEIF: "elseif" > : DEFAULT
| < END: "end" > : DEFAULT
| < FOR: "for" > : DEFAULT
| < FUNCTION: "function" > : DEFAULT
| < GLOBAL: "global" > : DEFAULT
| < IF: "if" > : DEFAULT
| < OTHERWISE: "otherwise" > : DEFAULT
| < PERSISTENT: "persistent" > : DEFAULT
| < RETURN: "return" > : DEFAULT
| < SWITCH: "switch" > : DEFAULT
| < TRY: "try" > : DEFAULT
| < VARARGIN: "varargin" > : DEFAULT
| < WHILE: "while" > : DEFAULT
| < CLEAR: "clear" > : DEFAULT
}
<DEFAULT, TRANSPOSE> TOKEN : /* Matlab identifiers */
{
< ID: <LETTER> ( <LETTER> | <DIGIT> | "_" )* > : TRANSPOSE
| < #LETTER: ["a"-"z", "A"-"Z"] >
}
<DEFAULT, TRANSPOSE> TOKEN :
{
< INT: ( <DIGIT> )+ > : DEFAULT
| < FLOAT:
<INT> "." ( <INT> )* ( <EXPONENT> )?
| "." ( <INT> )+ ( <EXPONENT> )?
| ( <INT> )+ <EXPONENT>
> : DEFAULT
| < #EXPONENT: ( "e" | "E" ) ( "+" | "-" )? <INT> >
| < #DIGIT: ["0"-"9"] >
}
<DEFAULT> TOKEN :
{
< STRING: "'" ( <ESC_SEQ> | "'" "'" | ~["\\","'","\n"] )* "'" >
| < #ESC_SEQ:
"\\" ( "b" | "t" | "n" | "f" | "r" | "\"" | "'" | "\\" )
| <UNICODE_ESC>
| <OCTAL_ESC>
>
| < #UNICODE_ESC: "\\" "u" <HEX_DIGIT> <HEX_DIGIT> <HEX_DIGIT> <HEX_DIGIT> >
| < #OCTAL_ESC:
"\\" ["0" - "3"] <OCTAL_DIGIT> <OCTAL_DIGIT>
| "\\" <OCTAL_DIGIT> <OCTAL_DIGIT>
| "\\" <OCTAL_DIGIT>
>
| < #HEX_DIGIT: ["0"-"9", "a"-"f", "A"-"F"] >
| < #OCTAL_DIGIT: ["0"-"7"] >
}
<DEFAULT, TRANSPOSE> TOKEN :
{
< TR : "'" > : TRANSPOSE
}

96
pmd-matlab/pom.xml Normal file
View File

@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>pmd-matlab</artifactId>
<name>PMD Matlab</name>
<parent>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd</artifactId>
<version>5.2.4-SNAPSHOT</version>
</parent>
<properties>
<config.basedir>${basedir}/../pmd-core</config.basedir>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<useDefaultDelimiters>false</useDefaultDelimiters>
<delimiters>
<delimiter>${*}</delimiter>
</delimiters>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<inherited>true</inherited>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<target>
<ant antfile="src/main/ant/alljavacc.xml">
<property name="target" value="${project.build.directory}/generated-sources/javacc" />
<property name="javacc.jar" value="${settings.localRepository}/net/java/dev/javacc/javacc/${javacc.version}/javacc-${javacc.version}.jar" />
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-javacc-generated-sources</id>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/javacc</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<configuration>
<xdocDirectory>${project.build.directory}/generated-xdocs</xdocDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,45 @@
<project name="pmd" default="alljavacc" basedir="../../">
<property name="javacc-home.path" value="target/lib" />
<target name="alljavacc"
description="Generates all JavaCC aspects within PMD"
depends="checkUpToDate,init,matlabjavacc,cleanup" />
<target name="checkUpToDate">
<uptodate property="javaccBuildNotRequired" targetfile="${target}/last-generated-timestamp">
<srcfiles dir="etc/grammar" includes="*.jj*"/>
</uptodate>
<echo message="up to date check: javaccBuildNotRequired=${javaccBuildNotRequired}"/>
</target>
<target name="init" unless="javaccBuildNotRequired">
<mkdir dir="${javacc-home.path}" />
<copy file="${javacc.jar}" tofile="${javacc-home.path}/javacc.jar" />
<mkdir dir="${target}"/>
<touch file="${target}/last-generated-timestamp"/>
</target>
<target name="cleanup">
<delete dir="${javacc-home.path}" />
</target>
<target name="matlabjavacc" description="Generates the Matlab grammar" unless="javaccBuildNotRequired">
<delete dir="${target}/net/sourceforge/pmd/lang/matlab/ast" />
<mkdir dir="${target}/net/sourceforge/pmd/lang/matlab/ast" />
<!-- Ensure generated using CharStream interface -->
<javacc static="false"
usercharstream="true"
target="etc/grammar/matlab.jj"
outputdirectory="${target}/net/sourceforge/pmd/lang/matlab/ast"
javacchome="${javacc-home.path}" />
<replace file="${target}/net/sourceforge/pmd/lang/matlab/ast/MatlabParserTokenManager.java"
token="class MatlabParserTokenManager"
value="class MatlabParserTokenManager extends net.sourceforge.pmd.lang.ast.AbstractTokenManager" />
<delete file="${target}/net/sourceforge/pmd/lang/matlab/ast/CharStream.java" />
<delete file="${target}/net/sourceforge/pmd/lang/matlab/ast/ParseException.java" />
<delete file="${target}/net/sourceforge/pmd/lang/matlab/ast/TokenMgrError.java" />
</target>
</project>

View File

@ -0,0 +1,18 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cpd;
/**
* Defines the Language module for Matlab
*/
public class MatlabLanguage extends AbstractLanguage {
/**
* Creates a new instance of {@link MatlabLanguage} with the default extensions for matlab files.
*/
public MatlabLanguage() {
super("Matlab", "matlab", new MatlabTokenizer(), ".m");
}
}

View File

@ -0,0 +1,46 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cpd;
import java.io.Reader;
import java.io.StringReader;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersionHandler;
import net.sourceforge.pmd.lang.TokenManager;
import net.sourceforge.pmd.lang.ast.TokenMgrError;
import net.sourceforge.pmd.lang.matlab.MatlabLanguageModule;
import net.sourceforge.pmd.lang.matlab.ast.Token;
import net.sourceforge.pmd.util.IOUtil;
import org.apache.commons.io.IOUtils;
public class MatlabTokenizer implements Tokenizer {
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);
}
}
}

View File

@ -0,0 +1,25 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.matlab;
import net.sourceforge.pmd.lang.AbstractLanguageVersionHandler;
import net.sourceforge.pmd.lang.Parser;
import net.sourceforge.pmd.lang.ParserOptions;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
/**
* Implementation of LanguageVersionHandler for the Matlab Language.
*/
public class MatlabHandler extends AbstractLanguageVersionHandler {
@Override
public RuleViolationFactory getRuleViolationFactory() {
throw new UnsupportedOperationException("getRuleViolationFactory() is not supported for Matlab");
}
@Override
public Parser getParser(ParserOptions parserOptions) {
return new MatlabParser(parserOptions);
}
}

View File

@ -0,0 +1,25 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.matlab;
import net.sourceforge.pmd.lang.BaseLanguageModule;
/**
* Implementation of the Matlab Language Module.
*/
public class MatlabLanguageModule extends BaseLanguageModule {
/** The name, that can be used to display the language in UI. */
public static final String NAME = "Matlab";
/** The internal name. */
public static final String TERSE_NAME = "matlab";
/**
* Creates a new instance of {@link MatlabLanguageModule} with the default file extensions for Matlab.
*/
public MatlabLanguageModule() {
super(NAME, null, TERSE_NAME, null, "m");
addVersion("", new MatlabHandler(), true);
}
}

View File

@ -0,0 +1,49 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.matlab;
import java.io.Reader;
import java.util.Map;
import net.sourceforge.pmd.lang.AbstractParser;
import net.sourceforge.pmd.lang.ParserOptions;
import net.sourceforge.pmd.lang.TokenManager;
import net.sourceforge.pmd.lang.ast.AbstractTokenManager;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.ast.ParseException;
/**
* Adapter for the Matlab Parser.
*/
public class MatlabParser extends AbstractParser {
/**
* Creates a new Matlab Parser.
* @param parserOptions the options
*/
public MatlabParser(ParserOptions parserOptions) {
super(parserOptions);
}
@Override
public TokenManager createTokenManager(Reader source) {
return new MatlabTokenManager(source);
}
@Override
public boolean canParse() {
return false;
}
@Override
public Node parse(String fileName, Reader source) throws ParseException {
AbstractTokenManager.setFileName(fileName);
throw new UnsupportedOperationException("parse(Reader) is not supported for Matlab");
}
@Override
public Map<Integer, String> getSuppressMap() {
throw new UnsupportedOperationException("getSuppressMap() is not supported for Matlab");
}
}

View File

@ -0,0 +1,34 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.matlab;
import java.io.Reader;
import net.sourceforge.pmd.lang.TokenManager;
import net.sourceforge.pmd.lang.ast.SimpleCharStream;
import net.sourceforge.pmd.lang.matlab.ast.MatlabParserTokenManager;
/**
* Matlab Token Manager implementation.
*/
public class MatlabTokenManager implements TokenManager {
private final MatlabParserTokenManager tokenManager;
/**
* Creates a new Matlab Token Manager from the given source code.
* @param source the source code
*/
public MatlabTokenManager(Reader source) {
tokenManager = new MatlabParserTokenManager(new SimpleCharStream(source));
}
public Object getNextToken() {
return tokenManager.getNextToken();
}
@Override
public void setFileName(String fileName) {
MatlabParserTokenManager.setFileName(fileName);
}
}

View File

@ -0,0 +1 @@
net.sourceforge.pmd.cpd.MatlabLanguage

View File

@ -0,0 +1 @@
net.sourceforge.pmd.lang.matlab.MatlabLanguageModule

View File

@ -0,0 +1,3 @@
# PMD Matlab
Only CPD is supported. There are no PMD rules for Matlab.

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/DECORATION/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/DECORATION/1.1.0 http://maven.apache.org/xsd/decoration-1.1.0.xsd"
name="PMD Matlab">
<body>
<menu ref="parent"/>
<menu ref="reports"/>
</body>
</project>

View File

@ -0,0 +1,27 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd;
import java.util.Arrays;
import java.util.Collection;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.lang.matlab.MatlabLanguageModule;
import org.junit.runners.Parameterized.Parameters;
public class LanguageVersionTest extends AbstractLanguageVersionTest {
public LanguageVersionTest(String name, String terseName, String version, LanguageVersion expected) {
super(name, terseName, version, expected);
}
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ MatlabLanguageModule.NAME, MatlabLanguageModule.TERSE_NAME, "", LanguageRegistry.getLanguage(MatlabLanguageModule.NAME).getDefaultVersion() }
});
}
}

View File

@ -0,0 +1,40 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cpd;
import java.io.IOException;
import net.sourceforge.pmd.testframework.AbstractTokenizerTest;
import net.sourceforge.pmd.testframework.StreamUtil;
import org.junit.Before;
import org.junit.Test;
public class MatlabTokenizerTest extends AbstractTokenizerTest {
private static final String FILENAME = "sample-matlab.m";
@Before
@Override
public void buildTokenizer() {
this.tokenizer = new MatlabTokenizer();
this.sourceCode = new SourceCode(new SourceCode.StringCodeLoader(this.getSampleCode(), FILENAME));
}
@Override
public String getSampleCode() {
return StreamUtil.toString(MatlabTokenizer.class.getResourceAsStream(FILENAME));
}
@Test
public void tokenizeTest() throws IOException {
this.expectedTokenCount = 3925;
super.tokenizeTest();
}
public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter(MatlabTokenizerTest.class);
}
}

File diff suppressed because it is too large Load Diff