net.sourceforge.pmd.lang.PlainTextLanguage Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pmd-core Show documentation
Show all versions of pmd-core Show documentation
PMD is an extensible multilanguage static code analyzer. It finds common programming flaws like unused variables,
empty catch blocks, unnecessary object creation, and so forth. It's mainly concerned with Java and
Apex, but supports 16 other languages. It comes with 400+ built-in rules. It can be
extended with custom rules. It uses JavaCC and Antlr to parse source files into abstract syntax trees
(AST) and runs rules against them to find violations. Rules can be written in Java or using a XPath query.
Currently, PMD supports Java, JavaScript, Salesforce.com Apex and Visualforce,
Kotlin, Swift, Modelica, PLSQL, Apache Velocity, JSP, WSDL, Maven POM, HTML, XML and XSL.
Scala is supported, but there are currently no Scala rules available.
Additionally, it includes CPD, the copy-paste-detector. CPD finds duplicated code in
Coco, C/C++, C#, Dart, Fortran, Gherkin, Go, Groovy, HTML, Java, JavaScript, JSP, Julia, Kotlin,
Lua, Matlab, Modelica, Objective-C, Perl, PHP, PLSQL, Python, Ruby, Salesforce.com Apex and
Visualforce, Scala, Swift, T-SQL, Typescript, Apache Velocity, WSDL, XML and XSL.
The newest version!
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang;
import net.sourceforge.pmd.cpd.AnyCpdLexer;
import net.sourceforge.pmd.cpd.CpdCapableLanguage;
import net.sourceforge.pmd.cpd.CpdLexer;
import net.sourceforge.pmd.lang.ast.AstInfo;
import net.sourceforge.pmd.lang.ast.Parser;
import net.sourceforge.pmd.lang.ast.Parser.ParserTask;
import net.sourceforge.pmd.lang.ast.RootNode;
import net.sourceforge.pmd.lang.ast.impl.AbstractNode;
import net.sourceforge.pmd.lang.document.TextRegion;
import net.sourceforge.pmd.lang.impl.SimpleLanguageModuleBase;
/**
* A dummy language implementation whose parser produces a single node.
* This is provided for cases where a non-null language is required, but
* the parser is not useful. This is useful eg to mock rules when no other
* language is on the classpath. This language is not exposed by {@link LanguageRegistry}
* and can only be used explicitly with {@link #getInstance()}.
*
* @author Clément Fournier
* @since 6.48.0
*/
public final class PlainTextLanguage extends SimpleLanguageModuleBase implements CpdCapableLanguage {
private static final String ID = "text";
private static final PlainTextLanguage INSTANCE = new PlainTextLanguage();
private PlainTextLanguage() {
super(LanguageMetadata.withId(ID).name("Plain text")
.extensions("plain-text-file-goo-extension")
.addDefaultVersion("default"),
new TextLvh());
}
/**
* Returns the singleton instance of this language.
*/
public static PlainTextLanguage getInstance() {
return INSTANCE; // note: this language is _not_ exposed via LanguageRegistry (no entry in META-INF/services)
}
@Override
public CpdLexer createCpdLexer(LanguagePropertyBundle bundle) {
return new AnyCpdLexer();
}
private static final class TextLvh implements LanguageVersionHandler {
@Override
public Parser getParser() {
return PlainTextFile::new;
}
}
/**
* The only node produced by the parser of {@link PlainTextLanguage}.
*/
public static class PlainTextFile extends AbstractNode implements RootNode {
private final AstInfo astInfo;
PlainTextFile(ParserTask task) {
this.astInfo = new AstInfo<>(task, this);
}
@Override
public TextRegion getTextRegion() {
return getTextDocument().getEntireRegion();
}
@Override
public String getXPathNodeName() {
return "TextFile";
}
@Override
public String getImage() {
return null;
}
@Override
public String toString() {
return "Plain text file (" + getEndLine() + " lines)";
}
@Override
public AstInfo extends RootNode> getAstInfo() {
return astInfo;
}
}
}