Store file name in data key

This commit is contained in:
Clément Fournier
2021-08-08 14:29:20 +02:00
parent 54dc756207
commit 064c1d7aef
8 changed files with 31 additions and 15 deletions

View File

@ -17,6 +17,7 @@ import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.benchmark.TimeTracker;
import net.sourceforge.pmd.benchmark.TimedOperation;
import net.sourceforge.pmd.benchmark.TimedOperationCategory;
import net.sourceforge.pmd.lang.AbstractParser;
import net.sourceforge.pmd.lang.Language;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.lang.LanguageVersionHandler;
@ -132,7 +133,7 @@ public class SourceCodeProcessor {
private Node parse(RuleContext ctx, Reader sourceCode, Parser parser) {
try (TimedOperation to = TimeTracker.startOperation(TimedOperationCategory.PARSER)) {
Node rootNode = parser.parse(String.valueOf(ctx.getSourceCodeFile()), sourceCode);
Node rootNode = AbstractParser.doParse(parser, String.valueOf(ctx.getSourceCodeFile()), sourceCode);
ctx.getReport().suppress(parser.getSuppressMap());
return rootNode;
}

View File

@ -29,6 +29,7 @@ import net.sourceforge.pmd.RuleSetNotFoundException;
import net.sourceforge.pmd.RuleSets;
import net.sourceforge.pmd.RulesetsFactoryUtils;
import net.sourceforge.pmd.SourceCodeProcessor;
import net.sourceforge.pmd.lang.AbstractParser;
import net.sourceforge.pmd.lang.Language;
import net.sourceforge.pmd.lang.LanguageFilenameFilter;
import net.sourceforge.pmd.lang.LanguageRegistry;
@ -147,7 +148,7 @@ public final class Benchmarker {
for (DataSource ds : dataSources) {
try (DataSource dataSource = ds; InputStreamReader reader = new InputStreamReader(dataSource.getInputStream())) {
parser.parse(dataSource.getNiceFileName(false, null), reader);
AbstractParser.doParse(parser, dataSource.getNiceFileName(false, null), reader);
}
}

View File

@ -6,6 +6,9 @@ package net.sourceforge.pmd.lang;
import java.io.Reader;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.ast.RootNode;
/**
* This is a generic implementation of the Parser interface.
*
@ -34,4 +37,11 @@ public abstract class AbstractParser implements Parser {
}
protected abstract TokenManager createTokenManager(Reader source);
@Deprecated
public static Node doParse(Parser parser, String fileName, Reader source) {
Node rootNode = parser.parse(fileName, source);
rootNode.getUserMap().set(RootNode.FILE_NAME_KEY, fileName);
return rootNode;
}
}

View File

@ -4,9 +4,16 @@
package net.sourceforge.pmd.lang.ast;
import net.sourceforge.pmd.annotation.Experimental;
import net.sourceforge.pmd.util.DataMap;
import net.sourceforge.pmd.util.DataMap.SimpleDataKey;
/**
* This interface can be used to tag the root node of various ASTs.
*/
public interface RootNode extends Node {
@Experimental
SimpleDataKey<String> FILE_NAME_KEY = DataMap.simpleDataKey("pmd.fileName");
// that's only a marker interface.
}

View File

@ -105,6 +105,7 @@ import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.RuleSet;
import net.sourceforge.pmd.RuleSets;
import net.sourceforge.pmd.SourceCodeProcessor;
import net.sourceforge.pmd.lang.AbstractParser;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.lang.LanguageVersionHandler;
@ -199,7 +200,8 @@ public class Designer implements ClipboardOwner {
static Node getCompilationUnit(LanguageVersionHandler languageVersionHandler, String code) {
Parser parser = languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions());
Node node = parser.parse(null, new StringReader(code));
Node node = AbstractParser.doParse(parser, "no file name", new StringReader(code));
languageVersionHandler.getSymbolFacade().start(node);
languageVersionHandler.getTypeResolutionFacade(Designer.class.getClassLoader()).start(node);
return node;

View File

@ -22,6 +22,7 @@ import org.apache.commons.io.input.CloseShieldInputStream;
import org.apache.commons.lang3.StringEscapeUtils;
import net.sourceforge.pmd.annotation.Experimental;
import net.sourceforge.pmd.lang.AbstractParser;
import net.sourceforge.pmd.lang.Language;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersionHandler;
@ -174,7 +175,7 @@ public class TreeExportCli {
Logger.getLogger(Attribute.class.getName()).setLevel(Level.OFF);
try (Reader reader = source) {
Node root = parser.parse(file, reader);
Node root = AbstractParser.doParse(parser, file, reader);
languageHandler.getQualifiedNameResolutionFacade(this.getClass().getClassLoader()).start(root);
renderer.renderSubtree(root, System.out);

View File

@ -11,6 +11,7 @@ import net.sourceforge.pmd.lang.LanguageRegistry
import net.sourceforge.pmd.lang.ast.Node
import net.sourceforge.pmd.lang.ast.test.matchNode
import net.sourceforge.pmd.lang.ast.test.shouldBe
import net.sourceforge.pmd.lang.modelica.ModelicaParsingHelper
import java.io.StringReader
class ModelicaCoordsTest : FunSpec({
@ -108,12 +109,8 @@ end TestPackage;
}
})
fun String.parseModelica(): ASTStoredDefinition {
val ver = LanguageRegistry.getLanguage("Modelica").defaultVersion.languageVersionHandler
val parser = ver.getParser(ver.defaultParserOptions)
return parser.parse(":dummy:", StringReader(this)) as ASTStoredDefinition
}
fun String.parseModelica(): ASTStoredDefinition =
ModelicaParsingHelper.DEFAULT.parse(this, ":dummy")
fun Node.assertBounds(bline: Int, bcol: Int, eline: Int, ecol: Int) {
this::getBeginLine shouldBe bline

View File

@ -80,12 +80,9 @@ class Foo {
}
})
fun String.parseScala(): ASTSource {
val ver = LanguageRegistry.getLanguage("Scala").defaultVersion.languageVersionHandler
val parser = ver.getParser(ver.defaultParserOptions)
fun String.parseScala(): ASTSource =
ScalaParsingHelper.DEFAULT.parse(this, ":dummy")
return parser.parse(":dummy:", StringReader(this)) as ASTSource
}
fun Node.assertBounds(bline: Int, bcol: Int, eline: Int, ecol: Int) {
this::getBeginLine shouldBe bline