Added ASTNodes for all Scala AST Nodes to do direct translation.
This commit is contained in:
@ -106,9 +106,8 @@ public class ScalaTokenizer implements Tokenizer {
|
||||
boolean skip = false;
|
||||
if (token.text() != null) {
|
||||
// skip any token that is whitespaces
|
||||
skip |= token instanceof Token.Space || token instanceof Token.Tab
|
||||
|| token instanceof Token.CR || token instanceof Token.LF
|
||||
|| token instanceof Token.FF || token instanceof Token.LFLF;
|
||||
skip |= token instanceof Token.Space || token instanceof Token.Tab || token instanceof Token.CR
|
||||
|| token instanceof Token.LF || token instanceof Token.FF || token instanceof Token.LFLF;
|
||||
}
|
||||
return skip;
|
||||
}
|
||||
|
@ -16,12 +16,11 @@ import net.sourceforge.pmd.lang.ParserOptions;
|
||||
import net.sourceforge.pmd.lang.TokenManager;
|
||||
import net.sourceforge.pmd.lang.ast.Node;
|
||||
import net.sourceforge.pmd.lang.ast.ParseException;
|
||||
import net.sourceforge.pmd.lang.scala.ast.ASTSourceNode;
|
||||
import net.sourceforge.pmd.lang.scala.ast.ScalaWrapperNode;
|
||||
import net.sourceforge.pmd.lang.scala.ast.ScalaTreeBuilder;
|
||||
import net.sourceforge.pmd.lang.scala.ast.nodes.ASTSource;
|
||||
|
||||
import scala.meta.Dialect;
|
||||
import scala.meta.Source;
|
||||
import scala.meta.Tree;
|
||||
import scala.meta.inputs.Input;
|
||||
import scala.meta.internal.parsers.ScalametaParser;
|
||||
|
||||
@ -33,8 +32,6 @@ import scala.meta.internal.parsers.ScalametaParser;
|
||||
public class ScalaParser extends AbstractParser {
|
||||
private final Dialect dialect;
|
||||
|
||||
private Map<Tree, ScalaWrapperNode> nodeCache = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Create a parser using the given Scala Dialect and set of parser options.
|
||||
*
|
||||
@ -55,7 +52,6 @@ public class ScalaParser extends AbstractParser {
|
||||
|
||||
@Override
|
||||
public Node parse(String fileName, Reader source) throws ParseException {
|
||||
nodeCache.clear();
|
||||
Input.VirtualFile virtualFile;
|
||||
try {
|
||||
String sourceString = IOUtils.toString(source);
|
||||
@ -64,30 +60,7 @@ public class ScalaParser extends AbstractParser {
|
||||
throw new ParseException(e);
|
||||
}
|
||||
Source src = new ScalametaParser(virtualFile, dialect).parseSource();
|
||||
ASTSourceNode srcNode = new ASTSourceNode(this, src);
|
||||
nodeCache.put(src, srcNode);
|
||||
return srcNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a wrapper around the given node so that we can interact with PMD
|
||||
* systems using the underlying scala node.
|
||||
*
|
||||
* @param scalaNode
|
||||
* a node from Scala's parsing
|
||||
* @return A Java-wrapped version of the given node, using a cache if this
|
||||
* has been previously wrapped, or null, if the given node is null
|
||||
*/
|
||||
public ScalaWrapperNode wrapNode(Tree scalaNode) {
|
||||
if (scalaNode == null) {
|
||||
return null;
|
||||
}
|
||||
ScalaWrapperNode node = nodeCache.get(scalaNode);
|
||||
if (node == null) {
|
||||
node = new ScalaWrapperNode(this, scalaNode);
|
||||
nodeCache.put(scalaNode, node);
|
||||
}
|
||||
return node;
|
||||
return (ASTSource) new ScalaTreeBuilder().build(src);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,28 +0,0 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast;
|
||||
|
||||
import net.sourceforge.pmd.lang.ast.RootNode;
|
||||
import net.sourceforge.pmd.lang.scala.ScalaParser;
|
||||
|
||||
import scala.meta.Source;
|
||||
|
||||
/**
|
||||
* The root node for a Scala AST.
|
||||
*/
|
||||
public class ASTSourceNode extends ScalaWrapperNode implements RootNode {
|
||||
|
||||
/**
|
||||
* Create a new root node wrapper for the Scala root AST node.
|
||||
*
|
||||
* @param scalaParser
|
||||
* the ScalaParser used to generate the node
|
||||
* @param scalaNode
|
||||
* the scalaNode node to wrap
|
||||
*/
|
||||
public ASTSourceNode(ScalaParser scalaParser, Source scalaNode) {
|
||||
super(scalaParser, scalaNode);
|
||||
}
|
||||
}
|
@ -29,7 +29,7 @@ public class DumpFacade extends ScalaParserVisitorAdapter {
|
||||
* @param node
|
||||
* the node to start with. Not necessarily a tree root.
|
||||
*/
|
||||
public void dump(Writer outWriter, String prefix, boolean shouldRecurse, ScalaNode node) {
|
||||
public void dump(Writer outWriter, String prefix, boolean shouldRecurse, ScalaNode<?> node) {
|
||||
this.writer = outWriter instanceof PrintWriter ? (PrintWriter) outWriter : new PrintWriter(outWriter);
|
||||
this.recurse = shouldRecurse;
|
||||
this.visit(node, prefix);
|
||||
@ -37,7 +37,7 @@ public class DumpFacade extends ScalaParserVisitorAdapter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(ScalaNode node, Object data) {
|
||||
public Object visit(ScalaNode<?> node, Object data) {
|
||||
dump(node, (String) data);
|
||||
if (recurse) {
|
||||
return super.visit(node, data + " ");
|
||||
@ -46,7 +46,7 @@ public class DumpFacade extends ScalaParserVisitorAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
private void dump(ScalaNode node, String prefix) {
|
||||
private void dump(ScalaNode<?> node, String prefix) {
|
||||
writer.print(prefix);
|
||||
writer.print(node.getXPathNodeName());
|
||||
|
||||
|
@ -11,8 +11,11 @@ import scala.meta.Tree;
|
||||
/**
|
||||
* A Base interface of a Scala Node. Defines several required methods of all
|
||||
* nodes.
|
||||
*
|
||||
* @param <T>
|
||||
* The Scala node type that extends Scala's Tree trait
|
||||
*/
|
||||
public interface ScalaNode extends Node {
|
||||
public interface ScalaNode<T extends Tree> extends Node {
|
||||
/**
|
||||
* Accept a visitor and traverse this node.
|
||||
*
|
||||
@ -41,5 +44,5 @@ public interface ScalaNode extends Node {
|
||||
*
|
||||
* @return the Scala Node for this node
|
||||
*/
|
||||
Tree getNode();
|
||||
T getNode();
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,155 +0,0 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import net.sourceforge.pmd.lang.ast.AbstractNode;
|
||||
import net.sourceforge.pmd.lang.ast.Node;
|
||||
import net.sourceforge.pmd.lang.scala.ScalaParser;
|
||||
|
||||
import scala.meta.Tree;
|
||||
import scala.meta.inputs.Position;
|
||||
|
||||
/**
|
||||
* The Java-wrapper for Scala Nodes to allow interoperability with PMD.
|
||||
*/
|
||||
public class ScalaWrapperNode extends AbstractNode implements ScalaNode {
|
||||
private Tree node;
|
||||
private ScalaParser parser;
|
||||
|
||||
/**
|
||||
* Create a new Java Wrapper around a Scala node.
|
||||
*
|
||||
* @param scalaParser
|
||||
* the ScalaParser used to generate the node
|
||||
* @param scalaNode
|
||||
* the scalaNode node to wrap
|
||||
*/
|
||||
public ScalaWrapperNode(ScalaParser scalaParser, Tree scalaNode) {
|
||||
super(0);
|
||||
this.parser = scalaParser;
|
||||
this.node = scalaNode;
|
||||
Position pos = node.pos();
|
||||
beginLine = pos.startLine() + 1;
|
||||
endLine = pos.endLine() + 1;
|
||||
beginColumn = pos.startColumn() + 1;
|
||||
endColumn = pos.endColumn() + 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tree getNode() {
|
||||
return node;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object accept(ScalaParserVisitor visitor, Object data) {
|
||||
return visitor.visit(this, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object childrenAccept(ScalaParserVisitor visitor, Object data) {
|
||||
int numChildren = jjtGetNumChildren();
|
||||
for (int i = 0; i < numChildren; ++i) {
|
||||
jjtGetChild(i).accept(visitor, data);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getXPathNodeName() {
|
||||
return node.productPrefix().replace(".", "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jjtClose() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jjtSetParent(Node parent) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScalaWrapperNode jjtGetParent() {
|
||||
if (node.parent().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return parser.wrapNode(node.parent().get());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jjtAddChild(Node child, int index) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jjtSetChildIndex(int index) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int jjtGetChildIndex() {
|
||||
int idx = node.parent().get().children().indexOf(node);
|
||||
if (idx == -1) {
|
||||
throw new IllegalStateException("This node is not a child of its parent: " + node);
|
||||
}
|
||||
return idx;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScalaWrapperNode jjtGetChild(int index) {
|
||||
return parser.wrapNode(node.children().apply(index));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int jjtGetNumChildren() {
|
||||
return node.children().size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int jjtGetId() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getImage() {
|
||||
String image = null;
|
||||
if (node instanceof scala.meta.Lit) {
|
||||
image = String.valueOf(((scala.meta.Lit) node).value());
|
||||
} else if (node instanceof scala.meta.Name) {
|
||||
image = ((scala.meta.Name) node).value().toString();
|
||||
} else if (node instanceof scala.meta.Type.Name) {
|
||||
image = ((scala.meta.Type.Name) node).value();
|
||||
} else if (node instanceof scala.meta.Term.Name) {
|
||||
image = ((scala.meta.Term.Name) node).value();
|
||||
} else if (node instanceof scala.meta.Type.Var.Name) {
|
||||
image = ((scala.meta.Type.Var.Name) node).value();
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setImage(String image) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasImageEqualTo(String image) {
|
||||
return Objects.equals(image, getImage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeChildAtIndex(int childIndex) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast.nodes;
|
||||
|
||||
import scala.meta.Case;
|
||||
|
||||
public class ASTCase extends AbstractScalaNode<Case> {
|
||||
|
||||
public ASTCase(Case scalaNode) {
|
||||
super(scalaNode);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast.nodes;
|
||||
|
||||
import scala.meta.Ctor;
|
||||
|
||||
public class ASTCtorPrimary extends AbstractScalaNode<Ctor.Primary> {
|
||||
|
||||
public ASTCtorPrimary(Ctor.Primary scalaNode) {
|
||||
super(scalaNode);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast.nodes;
|
||||
|
||||
import scala.meta.Ctor;
|
||||
|
||||
public class ASTCtorSecondary extends AbstractScalaNode<Ctor.Secondary> {
|
||||
|
||||
public ASTCtorSecondary(Ctor.Secondary scalaNode) {
|
||||
super(scalaNode);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast.nodes;
|
||||
|
||||
import scala.meta.Decl;
|
||||
|
||||
public class ASTDeclDef extends AbstractScalaNode<Decl.Def> {
|
||||
|
||||
public ASTDeclDef(Decl.Def scalaNode) {
|
||||
super(scalaNode);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast.nodes;
|
||||
|
||||
import scala.meta.Decl;
|
||||
|
||||
public class ASTDeclType extends AbstractScalaNode<Decl.Type> {
|
||||
|
||||
public ASTDeclType(Decl.Type scalaNode) {
|
||||
super(scalaNode);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast.nodes;
|
||||
|
||||
import scala.meta.Decl;
|
||||
|
||||
public class ASTDeclVal extends AbstractScalaNode<Decl.Val> {
|
||||
|
||||
public ASTDeclVal(Decl.Val scalaNode) {
|
||||
super(scalaNode);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast.nodes;
|
||||
|
||||
import scala.meta.Decl;
|
||||
|
||||
public class ASTDeclVar extends AbstractScalaNode<Decl.Var> {
|
||||
|
||||
public ASTDeclVar(Decl.Var scalaNode) {
|
||||
super(scalaNode);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast.nodes;
|
||||
|
||||
import scala.meta.Defn;
|
||||
|
||||
public class ASTDefnClass extends AbstractScalaNode<Defn.Class> {
|
||||
|
||||
public ASTDefnClass(Defn.Class scalaNode) {
|
||||
super(scalaNode);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast.nodes;
|
||||
|
||||
import scala.meta.Defn;
|
||||
|
||||
public class ASTDefnDef extends AbstractScalaNode<Defn.Def> {
|
||||
|
||||
public ASTDefnDef(Defn.Def scalaNode) {
|
||||
super(scalaNode);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast.nodes;
|
||||
|
||||
import scala.meta.Defn;
|
||||
|
||||
public class ASTDefnMacro extends AbstractScalaNode<Defn.Macro> {
|
||||
|
||||
public ASTDefnMacro(Defn.Macro scalaNode) {
|
||||
super(scalaNode);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast.nodes;
|
||||
|
||||
import scala.meta.Defn;
|
||||
|
||||
public class ASTDefnObject extends AbstractScalaNode<Defn.Object> {
|
||||
|
||||
public ASTDefnObject(Defn.Object scalaNode) {
|
||||
super(scalaNode);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast.nodes;
|
||||
|
||||
import scala.meta.Defn;
|
||||
|
||||
public class ASTDefnTrait extends AbstractScalaNode<Defn.Trait> {
|
||||
|
||||
public ASTDefnTrait(Defn.Trait scalaNode) {
|
||||
super(scalaNode);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast.nodes;
|
||||
|
||||
import scala.meta.Defn;
|
||||
|
||||
public class ASTDefnType extends AbstractScalaNode<Defn.Type> {
|
||||
|
||||
public ASTDefnType(Defn.Type scalaNode) {
|
||||
super(scalaNode);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast.nodes;
|
||||
|
||||
import scala.meta.Defn;
|
||||
|
||||
public class ASTDefnVal extends AbstractScalaNode<Defn.Val> {
|
||||
|
||||
public ASTDefnVal(Defn.Val scalaNode) {
|
||||
super(scalaNode);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast.nodes;
|
||||
|
||||
import scala.meta.Defn;
|
||||
|
||||
public class ASTDefnVar extends AbstractScalaNode<Defn.Var> {
|
||||
|
||||
public ASTDefnVar(Defn.Var scalaNode) {
|
||||
super(scalaNode);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast.nodes;
|
||||
|
||||
import scala.meta.Enumerator;
|
||||
|
||||
public class ASTEnumeratorGenerator extends AbstractScalaNode<Enumerator.Generator> {
|
||||
|
||||
public ASTEnumeratorGenerator(Enumerator.Generator scalaNode) {
|
||||
super(scalaNode);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast.nodes;
|
||||
|
||||
import scala.meta.Enumerator;
|
||||
|
||||
public class ASTEnumeratorGuard extends AbstractScalaNode<Enumerator.Guard> {
|
||||
|
||||
public ASTEnumeratorGuard(Enumerator.Guard scalaNode) {
|
||||
super(scalaNode);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast.nodes;
|
||||
|
||||
import scala.meta.Enumerator;
|
||||
|
||||
public class ASTEnumeratorVal extends AbstractScalaNode<Enumerator.Val> {
|
||||
|
||||
public ASTEnumeratorVal(Enumerator.Val scalaNode) {
|
||||
super(scalaNode);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast.nodes;
|
||||
|
||||
import scala.meta.Import;
|
||||
|
||||
public class ASTImport extends AbstractScalaNode<Import> {
|
||||
|
||||
public ASTImport(Import scalaNode) {
|
||||
super(scalaNode);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast.nodes;
|
||||
|
||||
import scala.meta.Importee;
|
||||
|
||||
public class ASTImporteeName extends AbstractScalaNode<Importee.Name> {
|
||||
|
||||
public ASTImporteeName(Importee.Name scalaNode) {
|
||||
super(scalaNode);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast.nodes;
|
||||
|
||||
import scala.meta.Importee;
|
||||
|
||||
public class ASTImporteeRename extends AbstractScalaNode<Importee.Rename> {
|
||||
|
||||
public ASTImporteeRename(Importee.Rename scalaNode) {
|
||||
super(scalaNode);
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user