forked from phoedos/pmd
Rename nodes so they don't have the 'context' suffix
This commit is contained in:
@ -7,6 +7,7 @@
|
||||
<!-- Input properties:
|
||||
- lang-name: matches the grammar name (eg "Swift")
|
||||
- lang-terse-name: uncapitalized package name (eg "swift")
|
||||
- node-prefix: prefix for generated AST nodes (eg "Sw")
|
||||
- root-node-name: name of the root node (eg "TopLevel"), will be made to implement RootNode
|
||||
|
||||
See AntlrGeneratedParserBase
|
||||
@ -88,30 +89,16 @@
|
||||
|
||||
</replace>
|
||||
|
||||
<replace file="${visitor-file}">
|
||||
|
||||
<replacefilter token="Visitor<T> extends ParseTreeVisitor<T> {"
|
||||
value="Visitor<P, R> extends net.sourceforge.pmd.lang.ast.AstVisitor<P, R> {
|
||||
|
||||
/**
|
||||
* The default visit method for ${lang-name} nodes. Unless overridden,
|
||||
* the default implementations of the methods of this interface delegate
|
||||
* to this method. The default calls {@link #visitNode(Node, Object)}.
|
||||
*
|
||||
* @param node Node to visit
|
||||
* @param data Parameter of the visit
|
||||
* @return Result of the visit
|
||||
*/
|
||||
default R visit${node-itf-name}(${node-itf-name} node, P data) { return visitNode(node, data); }
|
||||
|
||||
"/>
|
||||
|
||||
<replacefilter token="T visit" value="default R visit"/>
|
||||
<replacefilter token="ctx);" value="node, P data) { return visit${node-itf-name}(node, data); }"/>
|
||||
|
||||
</replace>
|
||||
|
||||
<!-- This is in the main sources (not much to do). -->
|
||||
<delete file="${base-visitor-file}" />
|
||||
|
||||
|
||||
<replaceregexp flags="g">
|
||||
<fileset dir="${target-package-dir}" />
|
||||
<regexp pattern="\b([A-Z]\w*)Context\b(?<!\b(Parser)?RuleContext\b)"/>
|
||||
<substitution expression="${node-prefix}\1"/>
|
||||
</replaceregexp>
|
||||
|
||||
|
||||
</target>
|
||||
</project>
|
||||
|
@ -39,6 +39,7 @@
|
||||
<property name="lang-name" value="Swift" />
|
||||
<property name="lang-terse-name" value="swift" />
|
||||
<property name="root-node-name" value="TopLevel" />
|
||||
<property name="node-prefix" value="Sw" />
|
||||
</ant>
|
||||
</target>
|
||||
</configuration>
|
||||
|
@ -6,7 +6,9 @@ package net.sourceforge.pmd.lang.swift;
|
||||
|
||||
import net.sourceforge.pmd.RuleContext;
|
||||
import net.sourceforge.pmd.lang.ast.AstVisitor;
|
||||
import net.sourceforge.pmd.lang.ast.Node;
|
||||
import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrBaseRule;
|
||||
import net.sourceforge.pmd.lang.swift.ast.SwiftInnerNode;
|
||||
|
||||
public abstract class AbstractSwiftRule extends AntlrBaseRule {
|
||||
|
||||
@ -14,6 +16,16 @@ public abstract class AbstractSwiftRule extends AntlrBaseRule {
|
||||
// inheritance constructor
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addRuleChainVisit(Class<? extends Node> nodeClass) {
|
||||
// note that this is made unnecessary by #2490
|
||||
if (SwiftInnerNode.class.isAssignableFrom(nodeClass)) {
|
||||
addRuleChainVisit(nodeClass.getSimpleName().substring("Sw".length()));
|
||||
return;
|
||||
}
|
||||
super.addRuleChainVisit(nodeClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract AstVisitor<RuleContext, ?> buildVisitor();
|
||||
}
|
||||
|
@ -10,19 +10,19 @@ import org.antlr.v4.runtime.Lexer;
|
||||
|
||||
import net.sourceforge.pmd.lang.ParserOptions;
|
||||
import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrBaseParser;
|
||||
import net.sourceforge.pmd.lang.swift.ast.SwiftParser.TopLevelContext;
|
||||
import net.sourceforge.pmd.lang.swift.ast.SwiftParser.SwTopLevel;
|
||||
|
||||
/**
|
||||
* Adapter for the SwiftParser.
|
||||
*/
|
||||
public final class PmdSwiftParser extends AntlrBaseParser<SwiftNode, TopLevelContext> {
|
||||
public final class PmdSwiftParser extends AntlrBaseParser<SwiftNode, SwTopLevel> {
|
||||
|
||||
public PmdSwiftParser(final ParserOptions parserOptions) {
|
||||
super(parserOptions);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected TopLevelContext parse(final Lexer lexer) {
|
||||
protected SwTopLevel parse(final Lexer lexer) {
|
||||
SwiftParser parser = new SwiftParser(new CommonTokenStream(lexer));
|
||||
return parser.topLevel();
|
||||
}
|
||||
|
@ -9,9 +9,12 @@ import java.util.List;
|
||||
import net.sourceforge.pmd.RuleContext;
|
||||
import net.sourceforge.pmd.lang.ast.AstVisitor;
|
||||
import net.sourceforge.pmd.lang.swift.AbstractSwiftRule;
|
||||
import net.sourceforge.pmd.lang.swift.ast.SwiftParser;
|
||||
import net.sourceforge.pmd.lang.swift.ast.SwiftParser.FunctionDeclarationContext;
|
||||
import net.sourceforge.pmd.lang.swift.ast.SwiftParser.InitializerDeclarationContext;
|
||||
import net.sourceforge.pmd.lang.swift.ast.SwiftParser.SwAttribute;
|
||||
import net.sourceforge.pmd.lang.swift.ast.SwiftParser.SwAttributes;
|
||||
import net.sourceforge.pmd.lang.swift.ast.SwiftParser.SwCodeBlock;
|
||||
import net.sourceforge.pmd.lang.swift.ast.SwiftParser.SwFunctionDeclaration;
|
||||
import net.sourceforge.pmd.lang.swift.ast.SwiftParser.SwInitializerDeclaration;
|
||||
import net.sourceforge.pmd.lang.swift.ast.SwiftParser.SwStatement;
|
||||
import net.sourceforge.pmd.lang.swift.ast.SwiftVisitorBase;
|
||||
|
||||
public class UnavailableFunctionRule extends AbstractSwiftRule {
|
||||
@ -21,8 +24,8 @@ public class UnavailableFunctionRule extends AbstractSwiftRule {
|
||||
|
||||
public UnavailableFunctionRule() {
|
||||
super();
|
||||
addRuleChainVisit(FunctionDeclarationContext.class);
|
||||
addRuleChainVisit(InitializerDeclarationContext.class);
|
||||
addRuleChainVisit(SwFunctionDeclaration.class);
|
||||
addRuleChainVisit(SwInitializerDeclaration.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -30,13 +33,13 @@ public class UnavailableFunctionRule extends AbstractSwiftRule {
|
||||
return new SwiftVisitorBase<RuleContext, Void>() {
|
||||
|
||||
@Override
|
||||
public Void visitFunctionDeclaration(final FunctionDeclarationContext ctx, RuleContext ruleCtx) {
|
||||
public Void visitFunctionDeclaration(final SwFunctionDeclaration ctx, RuleContext ruleCtx) {
|
||||
if (ctx == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (shouldIncludeUnavailableModifier(ctx.functionBody().codeBlock())) {
|
||||
final SwiftParser.AttributesContext attributes = ctx.functionHead().attributes();
|
||||
final SwAttributes attributes = ctx.functionHead().attributes();
|
||||
if (attributes == null || !hasUnavailableModifier(attributes.attribute())) {
|
||||
addViolation(ruleCtx, ctx);
|
||||
}
|
||||
@ -46,13 +49,13 @@ public class UnavailableFunctionRule extends AbstractSwiftRule {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visitInitializerDeclaration(final InitializerDeclarationContext ctx, RuleContext ruleCtx) {
|
||||
public Void visitInitializerDeclaration(final SwInitializerDeclaration ctx, RuleContext ruleCtx) {
|
||||
if (ctx == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (shouldIncludeUnavailableModifier(ctx.initializerBody().codeBlock())) {
|
||||
final SwiftParser.AttributesContext attributes = ctx.initializerHead().attributes();
|
||||
final SwAttributes attributes = ctx.initializerHead().attributes();
|
||||
if (attributes == null || !hasUnavailableModifier(attributes.attribute())) {
|
||||
addViolation(ruleCtx, ctx);
|
||||
}
|
||||
@ -61,17 +64,17 @@ public class UnavailableFunctionRule extends AbstractSwiftRule {
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean shouldIncludeUnavailableModifier(final SwiftParser.CodeBlockContext ctx) {
|
||||
private boolean shouldIncludeUnavailableModifier(final SwCodeBlock ctx) {
|
||||
if (ctx == null || ctx.statements() == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final List<SwiftParser.StatementContext> statements = ctx.statements().statement();
|
||||
final List<SwStatement> statements = ctx.statements().statement();
|
||||
|
||||
return statements.size() == 1 && FATAL_ERROR.equals(statements.get(0).getFirstAntlrToken().getText());
|
||||
}
|
||||
|
||||
private boolean hasUnavailableModifier(final List<SwiftParser.AttributeContext> attributes) {
|
||||
private boolean hasUnavailableModifier(final List<SwAttribute> attributes) {
|
||||
return attributes.stream().anyMatch(atr -> AVAILABLE_UNAVAILABLE.equals(atr.joinTokenText()));
|
||||
}
|
||||
};
|
||||
|
@ -8,18 +8,18 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import net.sourceforge.pmd.lang.ast.test.BaseParsingHelper;
|
||||
import net.sourceforge.pmd.lang.swift.SwiftLanguageModule;
|
||||
import net.sourceforge.pmd.lang.swift.ast.SwiftParser.TopLevelContext;
|
||||
import net.sourceforge.pmd.lang.swift.ast.SwiftParser.SwTopLevel;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class SwiftParsingHelper extends BaseParsingHelper<SwiftParsingHelper, TopLevelContext> {
|
||||
public class SwiftParsingHelper extends BaseParsingHelper<SwiftParsingHelper, SwTopLevel> {
|
||||
|
||||
public static final SwiftParsingHelper DEFAULT = new SwiftParsingHelper(Params.getDefaultNoProcess());
|
||||
|
||||
|
||||
public SwiftParsingHelper(@NotNull Params params) {
|
||||
super(SwiftLanguageModule.NAME, TopLevelContext.class, params);
|
||||
super(SwiftLanguageModule.NAME, SwTopLevel.class, params);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
Reference in New Issue
Block a user