Add cleanup phase for antlr CPD languages

This removes the parser and other things we don't need in the API

Currently only pmd-dart has been adapted
This commit is contained in:
Clément Fournier 2020-08-30 18:50:01 +02:00
parent ae26c29542
commit 40443326d3
5 changed files with 41 additions and 9 deletions

View File

@ -34,7 +34,18 @@
<property name="node-itf-name" value="${lang-name}Node"/>
<property name="base-class-name" value="Abstract${lang-name}Node"/>
<target name="adapt-antlr-sources" description="Adapt antlr sources to the PMD codebase">
<target name="cpd-language" description="Adapt Antlr sources for CPD-only languages">
<!-- We only need the Lexer file. -->
<delete file="${parser-file}"/>
<delete>
<fileset dir="${target-package-dir}">
<include name="*"/>
<exclude name="*Lexer.java"/>
</fileset>
</delete>
</target>
<target name="pmd-language" description="Adapt Antlr sources for PMD languages">
<!-- Adapt parser. -->
<replace file="${parser-file}">

View File

@ -17,6 +17,27 @@
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>antlr-cleanup</id>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<ant antfile="${antlr4.ant.wrapper}" target="cpd-language">
<property name="lang-name" value="Dart" />
<property name="lang-terse-name" value="dart" />
</ant>
</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>

View File

@ -26,7 +26,7 @@
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
grammar Dart2;
grammar Dart;
compilationUnit: libraryDefinition | partDeclaration;

View File

@ -10,7 +10,7 @@ import net.sourceforge.pmd.cpd.internal.AntlrTokenizer;
import net.sourceforge.pmd.cpd.token.AntlrTokenFilter;
import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrToken;
import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrTokenManager;
import net.sourceforge.pmd.lang.dart.antlr4.Dart2Lexer;
import net.sourceforge.pmd.lang.dart.ast.DartLexer;
/**
* The Dart Tokenizer
@ -20,7 +20,7 @@ public class DartTokenizer extends AntlrTokenizer {
@Override
protected AntlrTokenManager getLexerForSource(SourceCode sourceCode) {
CharStream charStream = AntlrTokenizer.getCharStreamFromSourceCode(sourceCode);
return new AntlrTokenManager(new Dart2Lexer(charStream), sourceCode.getFileName());
return new AntlrTokenManager(new DartLexer(charStream), sourceCode.getFileName());
}
@Override
@ -54,19 +54,19 @@ public class DartTokenizer extends AntlrTokenizer {
private void skipLibraryAndImport(final AntlrToken currentToken) {
final int type = currentToken.getKind();
if (type == Dart2Lexer.LIBRARY || type == Dart2Lexer.IMPORT) {
if (type == DartLexer.LIBRARY || type == DartLexer.IMPORT) {
discardingLibraryAndImport = true;
} else if (discardingLibraryAndImport && (type == Dart2Lexer.SEMICOLON || type == Dart2Lexer.NEWLINE)) {
} else if (discardingLibraryAndImport && (type == DartLexer.SEMICOLON || type == DartLexer.NEWLINE)) {
discardingLibraryAndImport = false;
}
}
private void skipNewLines(final AntlrToken currentToken) {
discardingNL = currentToken.getKind() == Dart2Lexer.NEWLINE;
discardingNL = currentToken.getKind() == DartLexer.NEWLINE;
}
private void skipSemicolons(final AntlrToken currentToken) {
discardingSemicolon = currentToken.getKind() == Dart2Lexer.SEMICOLON;
discardingSemicolon = currentToken.getKind() == DartLexer.SEMICOLON;
}
@Override

View File

@ -35,7 +35,7 @@
</goals>
<configuration>
<target>
<ant antfile="${antlr4.ant.wrapper}">
<ant antfile="${antlr4.ant.wrapper}" target="pmd-language">
<property name="lang-name" value="Swift" />
<property name="lang-terse-name" value="swift" />
<property name="root-node-name" value="TopLevel" />