Update modelica for 7.0.0

This commit is contained in:
Clément Fournier
2019-12-19 15:26:40 +01:00
parent ffcacd6edd
commit fc5426ab53
5 changed files with 79 additions and 38 deletions

View File

@ -4,19 +4,21 @@
package net.sourceforge.pmd.lang.modelica;
import net.sourceforge.pmd.lang.AbstractLanguageVersionHandler;
import net.sourceforge.pmd.lang.AbstractPmdLanguageVersionHandler;
import net.sourceforge.pmd.lang.Parser;
import net.sourceforge.pmd.lang.ParserOptions;
import net.sourceforge.pmd.lang.VisitorStarter;
import net.sourceforge.pmd.lang.XPathHandler;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.ast.xpath.DefaultASTXPathHandler;
import net.sourceforge.pmd.lang.modelica.ast.ASTStoredDefinition;
import net.sourceforge.pmd.lang.modelica.resolver.ModelicaSymbolFacade;
import net.sourceforge.pmd.lang.modelica.internal.ModelicaProcessingStage;
import net.sourceforge.pmd.lang.modelica.rule.ModelicaRuleViolationFactory;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
public class ModelicaHandler extends AbstractLanguageVersionHandler {
public class ModelicaHandler extends AbstractPmdLanguageVersionHandler {
public ModelicaHandler() {
super(ModelicaProcessingStage.class);
}
@Override
public XPathHandler getXPathHandler() {
return new DefaultASTXPathHandler();
@ -32,18 +34,4 @@ public class ModelicaHandler extends AbstractLanguageVersionHandler {
return new ModelicaParser(parserOptions);
}
@Override
public VisitorStarter getSymbolFacade() {
return new VisitorStarter() {
@Override
public void start(Node rootNode) {
new ModelicaSymbolFacade().initializeWith((ASTStoredDefinition) rootNode);
}
};
}
@Override
public VisitorStarter getSymbolFacade(ClassLoader classLoader) {
return getSymbolFacade();
}
}

View File

@ -5,8 +5,6 @@
package net.sourceforge.pmd.lang.modelica;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import net.sourceforge.pmd.lang.AbstractParser;
import net.sourceforge.pmd.lang.ParserOptions;
@ -27,19 +25,10 @@ public class ModelicaParser extends AbstractParser {
return new ModelicaTokenManager(source);
}
@Override
public boolean canParse() {
return true;
}
@Override
public Node parse(String fileName, Reader source) throws ParseException {
AbstractTokenManager.setFileName(fileName);
return new net.sourceforge.pmd.lang.modelica.ast.ModelicaParser(new SimpleCharStream(source)).StoredDefinition();
}
@Override
public Map<Integer, String> getSuppressMap() {
return new HashMap<Integer, String>(); // TODO
}
}

View File

@ -0,0 +1,42 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.modelica.internal;
import net.sourceforge.pmd.lang.Language;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.ast.AstAnalysisContext;
import net.sourceforge.pmd.lang.ast.AstProcessingStage;
import net.sourceforge.pmd.lang.ast.RootNode;
import net.sourceforge.pmd.lang.modelica.ModelicaLanguageModule;
import net.sourceforge.pmd.lang.modelica.ast.ASTStoredDefinition;
import net.sourceforge.pmd.lang.modelica.resolver.ModelicaSymbolFacade;
/**
* @author Clément Fournier
*/
public enum ModelicaProcessingStage implements AstProcessingStage<ModelicaProcessingStage> {
SYMBOL_RESOLUTION("Symbol resolution") {
@Override
public void processAST(RootNode rootNode, AstAnalysisContext configuration) {
new ModelicaSymbolFacade().initializeWith((ASTStoredDefinition) rootNode);
}
};
private final String displayName;
ModelicaProcessingStage(String displayName) {
this.displayName = displayName;
}
@Override
public Language getLanguage() {
return LanguageRegistry.getLanguage(ModelicaLanguageModule.NAME);
}
@Override
public String getDisplayName() {
return displayName;
}
}

View File

@ -8,6 +8,7 @@ import java.util.List;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.ast.AstProcessingStage;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.modelica.ModelicaLanguageModule;
import net.sourceforge.pmd.lang.modelica.ast.ASTAddOp;
@ -154,6 +155,7 @@ import net.sourceforge.pmd.lang.modelica.ast.ASTWhileStatement;
import net.sourceforge.pmd.lang.modelica.ast.ASTWithinClause;
import net.sourceforge.pmd.lang.modelica.ast.ModelicaNode;
import net.sourceforge.pmd.lang.modelica.ast.ModelicaParserVisitor;
import net.sourceforge.pmd.lang.modelica.internal.ModelicaProcessingStage;
import net.sourceforge.pmd.lang.rule.AbstractRule;
import net.sourceforge.pmd.lang.rule.ImmutableLanguage;
@ -185,6 +187,14 @@ public abstract class AbstractModelicaRule extends AbstractRule implements Model
return data;
}
@Override
public boolean dependsOn(AstProcessingStage<?> stage) {
if (!(stage instanceof ModelicaProcessingStage)) {
throw new IllegalArgumentException("Processing stage wasn't a Modelica one: " + stage);
}
return true;
}
@Override
public Object visit(ASTNegated node, Object data) {
return visit((ModelicaNode) node, data);

View File

@ -11,10 +11,12 @@ import java.io.Reader;
import org.apache.tools.ant.filters.StringInputStream;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.lang.LanguageVersionHandler;
import net.sourceforge.pmd.lang.Parser;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.ast.AstAnalysisContext;
import net.sourceforge.pmd.lang.modelica.ast.ASTStoredDefinition;
import net.sourceforge.pmd.lang.modelica.internal.ModelicaProcessingStage;
public class ModelicaLoader {
private ModelicaLoader() {
@ -31,12 +33,22 @@ public class ModelicaLoader {
}
public static ASTStoredDefinition parse(final String fileName, Reader source) {
final LanguageVersionHandler modelicaLang = LanguageRegistry.getLanguage(ModelicaLanguageModule.NAME)
.getDefaultVersion()
.getLanguageVersionHandler();
LanguageVersion lversion = LanguageRegistry.getLanguage(ModelicaLanguageModule.NAME).getDefaultVersion();
final LanguageVersionHandler modelicaLang = lversion.getLanguageVersionHandler();
final Parser parser = modelicaLang.getParser(modelicaLang.getDefaultParserOptions());
final Node node = parser.parse(fileName, source);
modelicaLang.getSymbolFacade().start(node);
return (ASTStoredDefinition) node;
final ASTStoredDefinition node = (ASTStoredDefinition) parser.parse(fileName, source);
AstAnalysisContext ctx = new AstAnalysisContext() {
@Override
public ClassLoader getTypeResolutionClassLoader() {
return ModelicaLoader.class.getClassLoader();
}
@Override
public LanguageVersion getLanguageVersion() {
return lversion;
}
};
ModelicaProcessingStage.SYMBOL_RESOLUTION.processAST(node, ctx);
return node;
}
}