diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 8abb75f161..9b57e5c1e4 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -19,11 +19,23 @@ This is a {{ site.pmd.release_type }} release. ### New and noteworthy +#### Updated PMD Designer + +This PMD release ships a new version of the pmd-designer. +For the changes, see [PMD Designer Changelog](https://github.com/pmd/pmd-designer/releases/tag/6.21.0). + ### Fixed Issues +* java-errorprone + * [#2250](https://github.com/pmd/pmd/issues/2250): \[java] InvalidLogMessageFormat flags logging calls using a slf4j-Marker + ### API Changes ### External Contributions +* [#2251](https://github.com/pmd/pmd/pull/2251): \[java] FP for InvalidLogMessageFormat when using slf4j-Markers - [Kris Scheibe](https://github.com/kris-scheibe) +* [#2253](https://github.com/pmd/pmd/pull/2253): \[modelica] Remove duplicated dependencies - [Piotrek Żygieło](https://github.com/pzygielo) +* [#2256](https://github.com/pmd/pmd/pull/2256): \[doc] Corrected XML attributes in release notes - [Maikel Steneker](https://github.com/maikelsteneker) + {% endtocmaker %} diff --git a/docs/pages/release_notes_old.md b/docs/pages/release_notes_old.md index f7f5c558cd..51b9e8416e 100644 --- a/docs/pages/release_notes_old.md +++ b/docs/pages/release_notes_old.md @@ -66,9 +66,9 @@ If a CPD language doesn't provide these exact information, then these additional Each `` element in the XML format now has 3 new attributes: -* attribute `endLine` -* attribute `beginColumn` (if there is column information available) -* attribute `endColumn` (if there is column information available) +* attribute `endline` +* attribute `column` (if there is column information available) +* attribute `endcolumn` (if there is column information available) #### Modified Rules diff --git a/javacc-wrapper.xml b/javacc-wrapper.xml new file mode 100644 index 0000000000..a1e842b7d8 --- /dev/null +++ b/javacc-wrapper.xml @@ -0,0 +1,478 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + /** Literal token values. */ + + /** Returns a string describing the given token kind. + * Returns null if the kind is unknown. + * + * @param kind Kind of token + * + * @return A string describing the given kind + */ + public static @org.checkerframework.checker.nullness.qual.Nullable + String describe(int kind) { + return kind < 0 || kind >= tokenImage.length + ? null + : tokenImage[kind]; + } + + private static final + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pmd-core/src/main/ant/alljavacc.xml b/pmd-core/src/main/ant/alljavacc.xml index 70e6be4959..49b8b00e7d 100644 --- a/pmd-core/src/main/ant/alljavacc.xml +++ b/pmd-core/src/main/ant/alljavacc.xml @@ -9,8 +9,8 @@ - - + + - - - - - - - - - - - - - - - - - - - - - - - "Lexical error in file " + net.sourceforge.pmd.lang.ast.AbstractTokenManager.getFileName() + " at line " - - - - - - - - - - + + + + + + + + + + + + + + + + + diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/internal/AntlrTokenizer.java b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/internal/AntlrTokenizer.java index 4dc054e38a..a41a7e0ed2 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/internal/AntlrTokenizer.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/internal/AntlrTokenizer.java @@ -40,9 +40,7 @@ public abstract class AntlrTokenizer implements Tokenizer { } catch (final AntlrTokenManager.ANTLRSyntaxError err) { // Wrap exceptions of the ANTLR tokenizer in a TokenMgrError, so they are correctly handled // when CPD is executed with the '--skipLexicalErrors' command line option - throw new TokenMgrError("Lexical error in file " + tokenManager.getFileName() + " at line " - + err.getLine() + ", column " + err.getColumn() + ". Encountered: " + err.getMessage(), - TokenMgrError.LEXICAL_ERROR); + throw new TokenMgrError(err.getLine(), err.getColumn(), tokenManager.getFileName(), err.getMessage(), null); } finally { tokenEntries.add(TokenEntry.getEOF()); } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/internal/util/IteratorUtil.java b/pmd-core/src/main/java/net/sourceforge/pmd/internal/util/IteratorUtil.java index 451f1c792d..c177a9fc90 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/internal/util/IteratorUtil.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/internal/util/IteratorUtil.java @@ -55,7 +55,7 @@ public final class IteratorUtil { return tmp.iterator(); } - public static Iterator flatMap(Iterator iter, Function> f) { + public static Iterator flatMap(Iterator iter, Function> f) { return new AbstractIterator() { private Iterator current = null; @@ -158,7 +158,7 @@ public final class IteratorUtil { return filter(iter, seen::add); } - public static List toList(Iterator it) { + public static List toList(Iterator it) { List list = new ArrayList<>(); while (it.hasNext()) { list.add(it.next()); @@ -166,6 +166,17 @@ public final class IteratorUtil { return list; } + public static List<@NonNull T> toNonNullList(Iterator it) { + List<@NonNull T> list = new ArrayList<>(); + while (it.hasNext()) { + T next = it.next(); + if (next != null) { + list.add(next); + } + } + return list; + } + public static Iterable toIterable(final Iterator it) { return () -> it; } @@ -180,7 +191,7 @@ public final class IteratorUtil { return count; } - public static @Nullable T last(Iterator iterator) { + public static @Nullable T last(Iterator iterator) { T next = null; while (iterator.hasNext()) { next = iterator.next(); @@ -194,7 +205,7 @@ public final class IteratorUtil { * * @throws IllegalArgumentException If n is negative */ - public static @Nullable T getNth(Iterator iterator, int n) { + public static @Nullable T getNth(Iterator iterator, int n) { advance(iterator, n); return iterator.hasNext() ? iterator.next() : null; } @@ -210,7 +221,7 @@ public final class IteratorUtil { } /** Limit the number of elements yielded by this iterator to the given number. */ - public static Iterator take(Iterator iterator, final int n) { + public static Iterator take(Iterator iterator, final int n) { AssertionUtil.requireNonNegative("n", n); if (n == 0) { return Collections.emptyIterator(); @@ -232,10 +243,10 @@ public final class IteratorUtil { } /** Produce an iterator whose first element is the nth element of the given source. */ - public static Iterator drop(Iterator source, final int n) { + public static Iterator drop(Iterator source, final int n) { AssertionUtil.requireNonNegative("n", n); if (n == 0) { - return source; + return (Iterator) source; } return new AbstractIterator() { diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/Parser.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/Parser.java index bcbb57241c..3546ce01b5 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/Parser.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/Parser.java @@ -10,7 +10,15 @@ import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.ast.ParseException; /** - * Common interface for calling tree-building parsers or source files. + * Produces an AST from a source file. Instances of this interface must + * be stateless (which makes them trivially threadsafe). + * + * TODO + * - Ideally ParserOptions would be an argument to ::parse + * - ::parse would also take some more parameters, eg an error collector + * - The reader + filename would be a TextDocument + * - Remove TokenManager from here. Only JavaCC implementations support that, + * and it's barely used. * * @author Pieter_Van_Raemdonck - Application Engineers NV/SA - www.ae.be */ diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/CharStream.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/CharStream.java new file mode 100644 index 0000000000..5cf6044ae7 --- /dev/null +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/CharStream.java @@ -0,0 +1,120 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.ast; + + +import java.io.IOException; + +import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccTokenDocument; + +/** + * PMD flavour of character streams used by JavaCC parsers. + * + * TODO for when all JavaCC languages are aligned: + * * rename methods to match decent naming conventions + * * move to impl.javacc package + */ +public interface CharStream { + + /** + * Returns the next character from the input. After a {@link #backup(int)}, + * some of the already read chars must be spit out again. + * + * @return The next character + * + * @throws IOException If the underlying char stream throws + */ + char readChar() throws IOException; + + + /** + * Calls {@link #readChar()} and returns its value, marking its position + * as the beginning of the next token. All characters must remain in + * the buffer between two successive calls to this method to implement + * backup correctly. + */ + char BeginToken() throws IOException; // SUPPRESS CHECKSTYLE we'll rename it later + + + /** + * Returns a string made up of characters from the token mark up to + * to the current buffer position. + */ + String GetImage(); // SUPPRESS CHECKSTYLE we'll rename it later + + + /** + * Returns an array of characters that make up the suffix of length 'len' for + * the current token. This is used to build up the matched string + * for use in actions in the case of MORE. A simple and inefficient + * implementation of this is as follows : + * + *
{@code
+     * String t = tokenImage();
+     * return t.substring(t.length() - len, t.length()).toCharArray();
+     * }
+ * + * @param len Length of the returned array + * + * @return The suffix + * + * @throws IndexOutOfBoundsException If len is greater than the length of the + * current token + */ + char[] GetSuffix(int len); // SUPPRESS CHECKSTYLE we'll rename it later + + + /** + * Pushes a given number of already read chars into the buffer. + * Subsequent calls to {@link #readChar()} will read those characters + * before proceeding to read the underlying char stream. + * + *

A lexer calls this method if it has already read some characters, + * but cannot use them to match a (longer) token. So, they will + * be used again as the prefix of the next token. + * + * @throws AssertionError If the requested amount is greater than the + * number of read chars + */ + void backup(int amount); + + @Deprecated + int getBeginColumn(); + + @Deprecated + int getBeginLine(); + + + /** Returns the column number of the last character for the current token. */ + int getEndColumn(); + + + /** Returns the line number of the last character for current token. */ + int getEndLine(); + + // These methods are added by PMD + + + /** + * Returns the token document for the tokens being built. Having it + * here is the most convenient place for the time being. + */ + default JavaccTokenDocument getTokenDocument() { + return null; // for VelocityCharStream + } + + + /** Returns the start offset of the current token (in the original source), inclusive. */ + default int getStartOffset() { + return -1; + } + + + /** Returns the end offset of the current token (in the original source), exclusive. */ + default int getEndOffset() { + return -1; + } + +} diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/GenericToken.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/GenericToken.java index a771750191..c98472a1b1 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/GenericToken.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/GenericToken.java @@ -5,12 +5,15 @@ package net.sourceforge.pmd.lang.ast; /** - * Represents a language-independent token such as constants, values language reserved keywords, or comments. + * Represents a language-independent token such as constants, values language reserved keywords, or comments. + * + * TODO make generic */ public interface GenericToken { /** * Obtain the next generic token according to the input stream which generated the instance of this token. + * * @return the next generic token if it exists; null if it does not exist */ GenericToken getNext(); @@ -18,6 +21,7 @@ public interface GenericToken { /** * Obtain a comment-type token which, according to the input stream which generated the instance of this token, * precedes this instance token and succeeds the previous generic token (if there is any). + * * @return the comment-type token if it exists; null if it does not exist */ GenericToken getPreviousComment(); @@ -27,6 +31,7 @@ public interface GenericToken { */ String getImage(); + // TODO these default implementations are here for compatibility because // the functionality is only used in pmd-java for now, though it could // be ported. I prefer doing this as changing all the GenericToken in @@ -47,25 +52,41 @@ public interface GenericToken { /** * Gets the line where the token's region begins + * * @return a non-negative integer containing the begin line */ int getBeginLine(); + /** * Gets the line where the token's region ends + * * @return a non-negative integer containing the end line */ int getEndLine(); + /** * Gets the column offset from the start of the begin line where the token's region begins + * * @return a non-negative integer containing the begin column */ int getBeginColumn(); + /** * Gets the column offset from the start of the end line where the token's region ends + * * @return a non-negative integer containing the begin column */ int getEndColumn(); + + /** + * Returns true if this token is implicit, ie was inserted artificially + * and has a zero-length image. + */ + default boolean isImplicit() { + return false; + } + } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/Node.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/Node.java index 1d69e35541..ca5f5f9422 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/Node.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/Node.java @@ -4,7 +4,6 @@ package net.sourceforge.pmd.lang.ast; -import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.xml.parsers.DocumentBuilder; @@ -18,8 +17,8 @@ import org.jaxen.JaxenException; import org.w3c.dom.Document; import net.sourceforge.pmd.annotation.InternalApi; +import net.sourceforge.pmd.lang.ast.NodeStream.DescendantNodeStream; import net.sourceforge.pmd.lang.ast.internal.StreamImpl; -import net.sourceforge.pmd.lang.ast.internal.TraversalUtils; import net.sourceforge.pmd.lang.ast.xpath.Attribute; import net.sourceforge.pmd.lang.ast.xpath.AttributeAxisIterator; import net.sourceforge.pmd.lang.ast.xpath.DocumentNavigator; @@ -233,13 +232,20 @@ public interface Node { } + /** - * Returns true if this node is considered a boundary by traversal methods. Traversal methods such as {@link - * #getFirstDescendantOfType(Class)} don't look past such boundaries by default, which is usually the expected thing - * to do. For example, in Java, lambdas and nested classes are considered find boundaries. + * Returns true if this node is considered a boundary by traversal + * methods. Traversal methods such as {@link #descendants()} + * don't look past such boundaries by default, which is usually the + * expected thing to do. For example, in Java, lambdas and nested + * classes are considered find boundaries. * *

Note: This attribute is deprecated for XPath queries. It is not useful * for XPath queries and will be removed with PMD 7.0.0. + * + * @return True if this node is a find boundary + * + * @see DescendantNodeStream#crossFindBoundaries(boolean) */ @DeprecatedAttribute default boolean isFindBoundary() { @@ -337,7 +343,7 @@ public interface Node { */ @Deprecated default void findDescendantsOfType(Class targetType, List results, boolean crossFindBoundaries) { - TraversalUtils.findDescendantsOfType(this, targetType, results, crossFindBoundaries); + descendants(targetType).crossFindBoundaries(crossFindBoundaries).forEach(results::add); } /** @@ -352,9 +358,7 @@ public interface Node { * @return List of all matching descendants */ default List findDescendantsOfType(Class targetType, boolean crossFindBoundaries) { - List results = new ArrayList<>(); - TraversalUtils.findDescendantsOfType(this, targetType, results, crossFindBoundaries); - return results; + return descendants(targetType).crossFindBoundaries(crossFindBoundaries).toList(); } /** @@ -365,7 +369,7 @@ public interface Node { * @see #getFirstDescendantOfType(Class) if traversal of the entire tree is needed. */ default T getFirstChildOfType(Class childType) { - return children().first(childType); + return children(childType).first(); } @@ -571,26 +575,26 @@ public interface Node { /** * Returns a node stream containing all the descendants - * of this node, in depth-first order. + * of this node. See {@link DescendantNodeStream} for details. * * @return A node stream of the descendants of this node * * @see NodeStream#descendants() */ - default NodeStream descendants() { + default DescendantNodeStream descendants() { return StreamImpl.descendants(this); } /** * Returns a node stream containing this node, then all its - * descendants in depth-first order. + * descendants. See {@link DescendantNodeStream} for details. * * @return A node stream of the whole subtree topped by this node * * @see NodeStream#descendantsOrSelf() */ - default NodeStream descendantsOrSelf() { + default DescendantNodeStream descendantsOrSelf() { return StreamImpl.descendantsOrSelf(this); } @@ -641,7 +645,8 @@ public interface Node { /** * Returns a {@linkplain NodeStream node stream} of the {@linkplain #descendants() descendants} - * of this node that are of the given type. + * of this node that are of the given type. See {@link DescendantNodeStream} + * for details. * * @param rClass Type of node the returned stream should contain * @param Type of node the returned stream should contain @@ -650,7 +655,7 @@ public interface Node { * * @see NodeStream#descendants(Class) */ - default NodeStream descendants(Class rClass) { + default DescendantNodeStream descendants(Class rClass) { return StreamImpl.descendants(this, rClass); } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/NodeStream.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/NodeStream.java index 9441de7294..3a73dbe9aa 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/NodeStream.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/NodeStream.java @@ -24,35 +24,40 @@ import net.sourceforge.pmd.lang.ast.internal.StreamImpl; /** - * Lazy stream of AST nodes. Conceptually identical to a {@link java.util.stream.Stream}, but exposes - * a specialized API to navigate abstract syntax trees. This API replaces the defunct {@link Node#findChildNodesWithXPath(String)}. + * A sequence of AST nodes. Conceptually similar to a {@link Stream}, + * and exposes a specialized API to navigate abstract syntax trees. + * This API replaces the defunct {@link Node#findChildNodesWithXPath(String)}. * *

API usage

* - *

The {@link Node} interface exposes methods like {@link Node#children()} or {@link Node#asStream()} - * to obtain new NodeStreams. Null-safe construction methods are available here, see {@link #of(Node)}, - * {@link #of(Node[])}, {@link #fromIterable(Iterable)}. + *

The {@link Node} interface exposes methods like {@link Node#children()} + * or {@link Node#asStream()} to obtain new NodeStreams. Null-safe construction + * methods are available here, see {@link #of(Node)}, {@link #of(Node[])}, + * {@link #fromIterable(Iterable)}. * - *

Most functions have an equivalent in the {@link Stream} interface and their behaviour is equivalent. - * Some additional functions are provided to iterate the axes of the tree: {@link #children()}, {@link #descendants()}, - * {@link #descendantsOrSelf()}, {@link #parents()}, {@link #ancestors()}, {@link #ancestorsOrSelf()}, - * {@link #precedingSiblings()}, {@link #followingSiblings()}. Filtering and mapping - * nodes by type is possible through {@link #filterIs(Class)}, and the specialized {@link #children(Class)}, - * {@link #descendants(Class)}, and {@link #ancestors(Class)}. + *

Most functions have an equivalent in the {@link Stream} interface + * and their behaviour is similar. One important departure from the + * {@link Stream} contract is the absence of requirement on the laziness + * of pipeline operations. More on that in the details section below. * - *

Many complex predicates about nodes can be expressed by testing the emptiness of a node stream. E.g. the - * following tests if the node is a variable declarator id initialized to the value {@code 0}: + *

Some additional functions are provided to iterate the axes of the + * tree: {@link #children()}, {@link #descendants()}, {@link #descendantsOrSelf()}, + * {@link #parents()}, {@link #ancestors()}, {@link #ancestorsOrSelf()}, + * {@link #precedingSiblings()}, {@link #followingSiblings()}. + * Filtering and mapping nodes by type is possible through {@link #filterIs(Class)}, + * and the specialized {@link #children(Class)}, {@link #descendants(Class)}, + * and {@link #ancestors(Class)}. + * + *

Many complex predicates about nodes can be expressed by testing + * the emptiness of a node stream. E.g. the following tests if the node + * is a variable declarator id initialized to the value {@code 0}: *

  *     {@linkplain #of(Node) NodeStream.of}(someNode)                           // the stream here is empty if the node is null
  *               {@linkplain #filterIs(Class) .filterIs}(ASTVariableDeclaratorId.class)// the stream here is empty if the node was not a variable declarator id
  *               {@linkplain #followingSiblings() .followingSiblings}()                    // the stream here contains only the siblings, not the original node
- *               {@linkplain #filterIs(Class) .filterIs}(ASTVariableInitializer.class)
- *               {@linkplain #children(Class) .children}(ASTExpression.class)
- *               .children(ASTPrimaryExpression.class)
- *               .children(ASTPrimaryPrefix.class)
- *               .children(ASTLiteral.class)
- *               {@linkplain #filterMatching(Function, Object) .filterMatching}(Node::getImage, "0")
- *               {@linkplain #filterNot(Predicate) .filterNot}(ASTLiteral::isStringLiteral)
+ *               {@linkplain #take(int) .take}(1)                                // the stream here contains only the first sibling, if it exists
+ *               {@linkplain #filterIs(Class) .filterIs}(ASTNumericLiteral.class)
+ *               {@linkplain #filter(Predicate) .filter}(it -> !it.isFloatingPoint() && it.getValueAsInt() == 0)
  *               {@linkplain #nonEmpty() .nonEmpty}(); // If the stream is non empty here, then all the pipeline matched
  * 
* @@ -84,13 +89,19 @@ import net.sourceforge.pmd.lang.ast.internal.StreamImpl; * *

NodeStreams are not necessarily implemented with {@link Stream}, but * when a method has an equivalent in the {@link Stream} API, their - * contract is identical. The same guidelines about statefulness and - * side-effects apply. + * contract is similar. The only difference, is that node streams are not + * necessarily lazy, ie, a pipeline operation may be evaluated eagerly + * to improve performance. For this reason, relying on side-effects + * produced in the middle of the pipeline is a bad idea. {@link Stream} + * gives the same guideline about statefulness, but not for the same reason. + * Their justification is parallelism and operation reordering, once + * the pipeline is fully known. * - *

Node streams are meant to be sequential streams, so there is no equivalent to {@link Stream#findAny()}. - * The method {@link #first()} is an equivalent to {@link Stream#findFirst()}. - * There is however a {@link #last()} method, which may be implemented - * efficiently on some streams (eg {@link #children()}). + *

Node streams are meant to be sequential streams, so there is no + * equivalent to {@link Stream#findAny()}. The method {@link #first()} + * is an equivalent to {@link Stream#findFirst()}. There is however a + * {@link #last()} method, which may be implemented efficiently on some + * streams (eg {@link #children()}). TODO maybe implement reverse * *

Node streams are most of the time ordered in document order (w.r.t. the XPath specification), * a.k.a. prefix order. Some operations which explicitly manipulate the order of nodes, like @@ -114,21 +125,12 @@ import net.sourceforge.pmd.lang.ast.internal.StreamImpl; */ public interface NodeStream extends Iterable<@NonNull T> { - - /** - * Returns a new stream of Ts having the pipeline of operations - * defined by this node stream. This can be called multiple times. - * - * @return A stream containing the same elements as this node stream - */ - Stream<@NonNull T> toStream(); - + // TODO measure performance of eager child stream /** * Returns a node stream consisting of the results of replacing each - * node of this stream with the contents of a mapped stream - * produced by applying the given mapping function to each - * node. If a mapped stream is null an empty stream is used, instead. + * node of this stream with the contents of a stream produced by the + * given mapping function. If a mapped stream is null, it is discarded. * *

If you want to flatMap this node stream to a {@link Stream} with * arbitrary elements (ie not nodes), use {@link #toStream()} then @@ -196,36 +198,6 @@ public interface NodeStream extends Iterable<@NonNull T> { NodeStream peek(Consumer action); - /** - * Applies the given mapping functions to this node stream in order and merges the - * results into a new node stream. This allows exploring several paths at once on the - * same stream. The method is lazy and won't evaluate the upstream pipeline several times. - * - * @param fst First mapper - * @param snd Second mapper - * @param rest Rest of the mappers - * @param Common supertype for the element type of the streams returned by the mapping functions - * - * @return A merged node stream - */ - default NodeStream forkJoin(Function> fst, - Function> snd, - Function>... rest) { - Objects.requireNonNull(fst); - Objects.requireNonNull(snd); - - List>> mappers = new ArrayList<>(rest.length + 2); - mappers.add(fst); - mappers.add(snd); - mappers.addAll(Arrays.asList(rest)); - - Function> aggregate = - t -> NodeStream.union(mappers.stream().map(f -> f.apply(t)).collect(Collectors.toList())); - - // with forkJoin we know that the stream will be iterated more than twice so we cache the values - return cached().flatMap(aggregate); - } - /** * Returns a new node stream that contains all the elements of this stream, then @@ -252,9 +224,8 @@ public interface NodeStream extends Iterable<@NonNull T> { /** * Returns a node stream containing all the elements of this node stream, * but which will evaluate the upstream pipeline only once. The returned - * stream is also lazy, which means the elements of this stream are not - * eagerly evaluated when calling this method, but only on the first - * terminal operation called on the downstream of the returned stream. + * stream is not necessarily lazy, which means it may evaluate the upstream + * pipeline as soon as the call to this method is made. * *

This is useful e.g. if you want to call several terminal operations * without executing the pipeline several times. For example, @@ -277,6 +248,7 @@ public interface NodeStream extends Iterable<@NonNull T> { */ NodeStream cached(); + /** * Returns a stream consisting of the elements of this stream, * truncated to be no longer than maxSize in length. @@ -315,13 +287,14 @@ public interface NodeStream extends Iterable<@NonNull T> { * @param predicate The predicate used to test elements. * * @return the longest prefix of this stream whose elements all satisfy - * the predicate `p`. + * the predicate. */ NodeStream takeWhile(Predicate predicate); /** - * Returns a stream consisting of the distinct elements (w.r.t {@link Object#equals(Object)}) of this stream. + * Returns a stream consisting of the distinct elements (w.r.t + * {@link Object#equals(Object)}) of this stream. * * @return a stream consisting of the distinct elements of this stream */ @@ -397,10 +370,10 @@ public interface NodeStream extends Iterable<@NonNull T> { /** * Returns a node stream containing all the strict descendants of the nodes - * contained in this stream. The nodes of the returned stream are yielded - * in a depth-first fashion. + * contained in this stream. See {@link DescendantNodeStream} for details. * - *

This is equivalent to {@code flatMap(Node::descendants)}. + *

This is equivalent to {@code flatMap(Node::descendants)}, except + * the returned stream is a {@link DescendantNodeStream}. * * @return A stream of descendants * @@ -408,25 +381,22 @@ public interface NodeStream extends Iterable<@NonNull T> { * @see #descendants(Class) * @see #descendantsOrSelf() */ - default NodeStream descendants() { - return flatMap(Node::descendants); - } + DescendantNodeStream descendants(); /** * Returns a node stream containing the nodes contained in this stream and their descendants. - * The nodes of the returned stream are yielded in a depth-first fashion. + * See {@link DescendantNodeStream} for details. * - *

This is equivalent to {@code flatMap(Node::descendantsOrSelf)}. + *

This is equivalent to {@code flatMap(Node::descendantsOrSelf)}, except + * the returned stream is a {@link DescendantNodeStream}. * * @return A stream of descendants * * @see Node#descendantsOrSelf() * @see #descendants() */ - default NodeStream descendantsOrSelf() { - return flatMap(Node::descendantsOrSelf); - } + DescendantNodeStream descendantsOrSelf(); /** @@ -469,12 +439,15 @@ public interface NodeStream extends Iterable<@NonNull T> { return flatMap(it -> it.children(rClass)); } + // todo maybe having a firstChild(rClass) -> flatMap(it -> it.children(rClass).take(1)) would be nice /** * Returns the {@linkplain #descendants() descendant stream} of each node - * in this stream, filtered by the given node type. + * in this stream, filtered by the given node type. See {@link DescendantNodeStream} + * for details. * - *

This is equivalent to {@code descendants().filterIs(rClass)}. + *

This is equivalent to {@code descendants().filterIs(rClass)}, except + * the returned stream is a {@link DescendantNodeStream}. * * @param rClass Type of node the returned stream should contain * @param Type of node the returned stream should contain @@ -484,9 +457,7 @@ public interface NodeStream extends Iterable<@NonNull T> { * @see #filterIs(Class) * @see Node#descendants(Class) */ - default NodeStream descendants(Class rClass) { - return flatMap(it -> it.descendants(rClass)); - } + DescendantNodeStream descendants(Class rClass); /** @@ -584,7 +555,6 @@ public interface NodeStream extends Iterable<@NonNull T> { } - // "terminal" operations @@ -601,7 +571,6 @@ public interface NodeStream extends Iterable<@NonNull T> { int count(); - /** * Returns 'true' if the stream has at least one element. * @@ -821,7 +790,19 @@ public interface NodeStream extends Iterable<@NonNull T> { /** - * Collects the elements of this node stream into a list. + * Returns a new stream of Ts having the pipeline of operations + * defined by this node stream. This can be called multiple times. + * + * @return A stream containing the same elements as this node stream + */ + Stream<@NonNull T> toStream(); + + + /** + * Collects the elements of this node stream into a list. Just like + * for {@link Collectors#toList()}, there are no guarantees on the + * type, mutability, serializability, or thread-safety of the returned + * list. * *

This is equivalent to {@code collect(Collectors.toList())}. * @@ -853,6 +834,7 @@ public interface NodeStream extends Iterable<@NonNull T> { return collect(Collectors.mapping(mapper, Collectors.toList())); } + /** * Returns a node stream containing zero or one node, * depending on whether the argument is null or not. @@ -966,4 +948,125 @@ public interface NodeStream extends Iterable<@NonNull T> { static NodeStream empty() { return StreamImpl.empty(); } + + + /** + * Applies the given mapping functions to the given upstream in order and merges the + * results into a new node stream. This allows exploring several paths at once on the + * same stream. The method is lazy and won't evaluate the upstream pipeline several times. + * + * @param upstream Source of the stream + * @param fst First mapper + * @param snd Second mapper + * @param rest Rest of the mappers + * @param Common supertype for the element type of the streams returned by the mapping functions + * + * @return A merged node stream + */ + @SafeVarargs // this method is static because of the generic varargs + static NodeStream forkJoin(NodeStream upstream, + Function> fst, + Function> snd, + Function>... rest) { + Objects.requireNonNull(fst); + Objects.requireNonNull(snd); + + List>> mappers = new ArrayList<>(rest.length + 2); + mappers.add(fst); + mappers.add(snd); + mappers.addAll(Arrays.asList(rest)); + + Function> aggregate = + t -> NodeStream.union(mappers.stream().map(f -> f.apply(t)).collect(Collectors.toList())); + + // with forkJoin we know that the stream will be iterated more than twice so we cache the values + return upstream.cached().flatMap(aggregate); + } + + + + /** + * A specialization of {@link NodeStream} that allows configuring + * tree traversal behaviour when traversing the descendants of a node. + * Such a stream is returned by methods such as {@link Node#descendants()}. + * When those methods are called on a stream containing more than one + * element (eg {@link NodeStream#descendants()}), the configuration + * applies to each individual traversal. + * + *

By default, traversal is performed depth-first (prefix order). Eg + *

{@code
+     * A
+     * + B
+     *   + C
+     *   + D
+     * + E
+     *   + F
+     * }
+ * is traversed in the order {@code A, B, C, D, E, F}. + * + *

By default, traversal also does not cross {@linkplain #crossFindBoundaries(boolean) find boundaries}. + * + * @param Type of node this stream contains + */ + interface DescendantNodeStream extends NodeStream { + + // TODO stop recursion on an arbitrary boundary + // TODO breadth-first traversal + + + /** + * Returns a node stream that will not stop the tree traversal + * when encountering a find boundary. Find boundaries are node + * that by default stop tree traversals, like class declarations. + * They are identified via {@link Node#isFindBoundary()}. + * + *

For example, supposing you have the AST node for the following + * method: + *

{@code
+         *  void method() {
+         *    String outer = "before";
+         *
+         *    class Local {
+         *      void localMethod() {
+         *        String local = "local";
+         *      }
+         *    }
+         *
+         *    String after = "after";
+         *  }
+         * }
+ * Then the stream {@code method.descendants(ASTStringLiteral.class)} + * will only yield the literals {@code "before"} and {@code "after"}, + * because the traversal doesn't go below the local class. + * + *

Note that traversal is stopped only for the subtree of the + * find boundary, but continues on the siblings. This is why + * {@code "after"} is yielded. This is also why {@link #takeWhile(Predicate)} + * is not a substitute for this method: {@code method.descendants(ASTStringLiteral.class).takeWhile(it -> !it.isFindBoundary)} + * would yield only {@code "before"}. + * + *

This behaviour can be opted out of with this method. In the + * example, the stream {@code method.descendants(ASTStringLiteral.class).crossFindBoundaries()} + * will yield {@code "before"}, {@code "local"} and {@code "after"} + * literals. + * + * @param cross If true, boundaries will be crossed. + * + * @return A new node stream + */ + DescendantNodeStream crossFindBoundaries(boolean cross); + + + /** + * An alias for {@link #crossFindBoundaries(boolean) crossFindBoundaries(true)}. + * + * @return A new node stream + */ + default DescendantNodeStream crossFindBoundaries() { + return crossFindBoundaries(true); + } + + } + + } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/ParseException.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/ParseException.java index 0a3e9e67b9..cef18319c6 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/ParseException.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/ParseException.java @@ -4,21 +4,137 @@ package net.sourceforge.pmd.lang.ast; +import java.util.LinkedHashSet; +import java.util.Set; +import java.util.stream.Collectors; + +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken; +import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccTokenDocument; +import net.sourceforge.pmd.util.StringUtil; + public class ParseException extends RuntimeException { + /** + * This is the last token that has been consumed successfully. If + * this object has been created due to a parse error, the token + * followng this token will (therefore) be the first error token. + */ + public final @Nullable GenericToken currentToken; + public ParseException() { super(); + this.currentToken = null; } public ParseException(String message) { super(message); + this.currentToken = null; } public ParseException(Throwable cause) { super(cause); + this.currentToken = null; } public ParseException(String message, Throwable cause) { super(message, cause); + this.currentToken = null; } + + public ParseException(JavaccToken token, String message) { + super(message); + this.currentToken = token; + } + + /** + * This constructor is called by Javacc. + */ + public ParseException(@NonNull JavaccToken currentTokenVal, + int[][] expectedTokenSequencesVal) { + super(makeMessage(currentTokenVal, expectedTokenSequencesVal)); + currentToken = currentTokenVal; + } + + /** + * It uses "currentToken" and "expectedTokenSequences" to generate a parse + * error message and returns it. If this object has been created + * due to a parse error, and you do not catch it (it gets thrown + * from the parser) the correct error message + * gets displayed. + */ + private static String makeMessage(@NonNull JavaccToken currentToken, + int[][] expectedTokenSequences) { + + JavaccTokenDocument document = currentToken.getDocument(); + String eol = System.lineSeparator(); + Set expectedBranches = new LinkedHashSet<>(); + int maxSize = 0; + for (int[] expectedTokenSequence : expectedTokenSequences) { + StringBuilder expected = new StringBuilder(); + if (maxSize < expectedTokenSequence.length) { + maxSize = expectedTokenSequence.length; + } + for (int i : expectedTokenSequence) { + expected.append(document.describeKind(i)).append(' '); + } + if (expectedTokenSequence[expectedTokenSequence.length - 1] != 0) { + expected.append("..."); + } + expectedBranches.add(expected.toString()); + } + + String expected = expectedBranches.stream().collect(Collectors.joining(System.lineSeparator() + " ")); + + StringBuilder retval = new StringBuilder("Encountered "); + if (maxSize > 1) { + retval.append('['); + } + JavaccToken tok = currentToken.next; + for (int i = 0; i < maxSize; i++) { + if (i != 0) { + retval.append(' '); + } + if (tok.kind == 0) { + retval.append(document.describeKind(0)); + break; + } + + String kindStr = document.describeKind(tok.kind); + + String image = StringUtil.escapeJava(tok.getImage()); + + retval.append(kindStr); + + if (!isEnquotedVersion(kindStr, image)) { + // then it's an angle-braced name + retval.deleteCharAt(retval.length() - 1); // remove '>' + retval.append(": \""); + retval.append(image); + retval.append("\">"); + } + + tok = tok.next; + } + + if (maxSize > 1) { + retval.append(']'); + } + retval.append(" at line ").append(currentToken.next.getBeginLine()).append(", column ").append(currentToken.next.getBeginColumn()); + retval.append('.').append(eol); + if (expectedTokenSequences.length == 1) { + retval.append("Was expecting:").append(eol).append(" "); + } else { + retval.append("Was expecting one of:").append(eol).append(" "); + } + retval.append(expected); + return retval.toString(); + } + + private static boolean isEnquotedVersion(String kindStr, String image) { + return kindStr.equals('"' + image + '"'); + } + } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/TokenMgrError.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/TokenMgrError.java new file mode 100644 index 0000000000..114dac2a3a --- /dev/null +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/TokenMgrError.java @@ -0,0 +1,120 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.ast; + +import org.checkerframework.checker.nullness.qual.Nullable; + +import net.sourceforge.pmd.annotation.InternalApi; +import net.sourceforge.pmd.util.StringUtil; + +/** + * An error thrown during lexical analysis of a file. + */ +public final class TokenMgrError extends RuntimeException { + + + /** + * @deprecated Will be removed when all modules are ported + */ + @Deprecated + public static final int LEXICAL_ERROR = 0; + + /** + * @deprecated Will be removed when all modules are ported, + * see {@link #TokenMgrError(String, int)} + */ + @Deprecated + public static final int INVALID_LEXICAL_STATE = 1; + + private final int line; + private final int column; + private final String filename; + + public TokenMgrError(int line, int column, @Nullable String filename, String message, @Nullable Throwable cause) { + super(message, cause); + this.line = line; + this.column = column; + this.filename = filename; + } + + public TokenMgrError(int line, int column, String message, @Nullable Throwable cause) { + this(line, column, null, message, cause); + } + + /** + * @deprecated This is used by javacc but those usages are being replaced with an IllegalArgumentException + */ + @Deprecated + @SuppressWarnings("PMD.UnusedFormalParameter") + public TokenMgrError(String message, int errorCode) { + this(-1, -1, null, message, null); + } + + /** + * Constructor called by JavaCC. + */ + @InternalApi + @SuppressWarnings("PMD.UnusedFormalParameter") + public TokenMgrError(boolean eofSeen, String lexStateName, int errorLine, int errorColumn, String errorAfter, char curChar) { + super(makeReason(eofSeen, lexStateName, errorAfter, curChar)); + line = errorLine; + column = errorColumn; + filename = AbstractTokenManager.getFileName(); + } + + /** + * Constructor called by JavaCC. + * + * @deprecated The error code is useless, ported modules use the other constructor + */ + @Deprecated + @SuppressWarnings("PMD.UnusedFormalParameter") + public TokenMgrError(boolean eofSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar, int errorCode) { + super(makeReason(eofSeen, String.valueOf(lexState), errorAfter, curChar)); + line = errorLine; + column = errorColumn; + filename = AbstractTokenManager.getFileName(); + } + + public int getLine() { + return line; + } + + public int getColumn() { + return column; + } + + public @Nullable String getFilename() { + return filename; + } + + + @Override + public String getMessage() { + String leader = filename != null ? "Lexical error in file " + filename : "Lexical error"; + return leader + " at line " + line + ", column " + column + ". Encountered: " + super.getMessage(); + } + + /** + * Replace the file name of this error. + * + * @param filename New filename + */ + public TokenMgrError withFileName(String filename) { + return new TokenMgrError(this.line, this.column, filename, this.getMessage(), this.getCause()); + } + + private static String makeReason(boolean eofseen, String lexStateName, String errorAfter, char curChar) { + String message; + if (eofseen) { + message = " "; + } else { + message = "\"" + StringUtil.escapeJava(String.valueOf(curChar)) + "\"" + " (" + (int) curChar + "), "; + } + message += "after : \"" + StringUtil.escapeJava(errorAfter) + "\" (in lexical state " + lexStateName + ")"; + + return message; + } +} diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/TokenDocument.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/TokenDocument.java index b31a3fd856..4159867391 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/TokenDocument.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/TokenDocument.java @@ -5,15 +5,15 @@ package net.sourceforge.pmd.lang.ast.impl; import net.sourceforge.pmd.annotation.Experimental; +import net.sourceforge.pmd.lang.ast.GenericToken; import net.sourceforge.pmd.lang.ast.SourceCodePositioner; import net.sourceforge.pmd.util.StringUtil; /** - * Maybe this can be used to eg double link tokens, provide an identity - * for them, idk. + * Token layer of a parsed file. */ @Experimental -public class TokenDocument { +public abstract class TokenDocument { private final String fullText; private final SourceCodePositioner positioner; @@ -28,7 +28,6 @@ public class TokenDocument { return fullText; } - public int lineNumberFromOffset(int offset) { return positioner.lineNumberFromOffset(offset); } @@ -37,4 +36,12 @@ public class TokenDocument { return StringUtil.columnNumberAt(fullText, offsetInclusive); } + /** + * Returns the first token of the token chain. + * + * @throws IllegalStateException If the document has not been parsed yet + */ + public abstract T getFirstToken(); + + } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/AbstractJjtreeNode.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/AbstractJjtreeNode.java index 76e1b0889e..1b1e54baf6 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/AbstractJjtreeNode.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/AbstractJjtreeNode.java @@ -6,8 +6,10 @@ package net.sourceforge.pmd.lang.ast.impl.javacc; import net.sourceforge.pmd.annotation.Experimental; import net.sourceforge.pmd.lang.ast.AbstractNode; +import net.sourceforge.pmd.lang.ast.GenericToken; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.ast.NodeStream; +import net.sourceforge.pmd.lang.ast.TextAvailableNode; /** * Base class for node produced by JJTree. JJTree specific functionality @@ -18,16 +20,40 @@ import net.sourceforge.pmd.lang.ast.NodeStream; * unforeseeable ways. Don't use it directly, use the node interfaces. */ @Experimental -public abstract class AbstractJjtreeNode extends AbstractNode { +public abstract class AbstractJjtreeNode extends AbstractNode implements TextAvailableNode { + public AbstractJjtreeNode(int id) { super(id); } - public AbstractJjtreeNode(int id, int theBeginLine, int theEndLine, int theBeginColumn, int theEndColumn) { - super(id, theBeginLine, theEndLine, theBeginColumn, theEndColumn); + @Override + public CharSequence getText() { + String fullText = jjtGetFirstToken().document.getFullText(); + return fullText.substring(getStartOffset(), getEndOffset()); } + @Override + public JavaccToken jjtGetFirstToken() { + return (JavaccToken) super.jjtGetFirstToken(); + } + + @Override + public JavaccToken jjtGetLastToken() { + return (JavaccToken) super.jjtGetLastToken(); + } + + // the super methods query line & column, which we want to avoid + + @Override + public void jjtSetLastToken(GenericToken token) { + this.lastToken = token; + } + + @Override + public void jjtSetFirstToken(GenericToken token) { + this.firstToken = token; + } @Override @SuppressWarnings("unchecked") @@ -41,10 +67,46 @@ public abstract class AbstractJjtreeNode extends AbstractNode { return (N) super.getParent(); } - @Override @SuppressWarnings("unchecked") public NodeStream children() { return (NodeStream) super.children(); } + + @Override + public int getBeginLine() { + return firstToken.getBeginLine(); + } + + @Override + public int getBeginColumn() { + return firstToken.getBeginColumn(); + } + + @Override + public int getEndLine() { + return lastToken.getEndLine(); + } + + @Override + public int getEndColumn() { + return lastToken.getEndColumn(); + } + + /** + * This toString implementation is only meant for debugging purposes. + */ + @Override + public String toString() { + return "[" + getXPathNodeName() + ":" + getBeginLine() + ":" + getBeginColumn() + "]" + getText(); + } + + private int getStartOffset() { + return this.jjtGetFirstToken().getStartInDocument(); + } + + + private int getEndOffset() { + return this.jjtGetLastToken().getEndInDocument(); + } } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/CharStreamFactory.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/CharStreamFactory.java index 0445df6e69..f579c8a0a2 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/CharStreamFactory.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/CharStreamFactory.java @@ -11,11 +11,7 @@ import java.util.function.Function; import org.apache.commons.io.IOUtils; import net.sourceforge.pmd.lang.ast.CharStream; -import net.sourceforge.pmd.lang.ast.JavaCharStream; -import net.sourceforge.pmd.lang.ast.SimpleCharStream; -import net.sourceforge.pmd.lang.ast.impl.TokenDocument; -@SuppressWarnings("PMD.UnusedFormalParameter") // for later public final class CharStreamFactory { private CharStreamFactory() { @@ -26,29 +22,32 @@ public final class CharStreamFactory { * A char stream that doesn't perform any escape translation. */ public static CharStream simpleCharStream(Reader input) { - return simpleCharStream(input, TokenDocument::new); + return simpleCharStream(input, JavaccTokenDocument::new); } /** * A char stream that doesn't perform any escape translation. */ - public static CharStream simpleCharStream(Reader input, Function documentMaker) { - return new SimpleCharStream(input); + public static CharStream simpleCharStream(Reader input, Function documentMaker) { + String source = toString(input); + JavaccTokenDocument document = documentMaker.apply(source); + return new SimpleCharStream(document); } /** * A char stream that translates java unicode sequences. */ public static CharStream javaCharStream(Reader input) { - return javaCharStream(input, TokenDocument::new); + return javaCharStream(input, JavaccTokenDocument::new); } /** * A char stream that translates java unicode sequences. */ - public static CharStream javaCharStream(Reader input, Function documentMaker) { + public static CharStream javaCharStream(Reader input, Function documentMaker) { String source = toString(input); - return new JavaCharStream(source); + JavaccTokenDocument tokens = documentMaker.apply(source); + return new JavaCharStream(tokens); } /** diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/JavaCharStream.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JavaCharStream.java similarity index 65% rename from pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/JavaCharStream.java rename to pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JavaCharStream.java index 18b441876c..31c2833340 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/JavaCharStream.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JavaCharStream.java @@ -2,48 +2,37 @@ * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ -package net.sourceforge.pmd.lang.ast; +package net.sourceforge.pmd.lang.ast.impl.javacc; +import java.io.EOFException; import java.io.IOException; -import java.io.Reader; import java.io.StringReader; -import org.apache.commons.io.IOUtils; - -import net.sourceforge.pmd.lang.ast.impl.TokenDocument; -import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken; - /** * This stream buffers the whole file in memory before parsing, * and track start/end offsets of tokens. This allows building {@link JavaccToken}. * The buffer is assumed to be composed of only ASCII characters, * and the stream unescapes Unicode escapes. The {@link #getTokenDocument() token document} * stores the original file with escapes and all. - * - * TODO this is to be moved into the impl.javacc subpackage */ public class JavaCharStream extends JavaCharStreamBase { // full text with nothing escaped and all private final String fullText; - private final TokenDocument document; + private final JavaccTokenDocument document; private int[] startOffsets; - public JavaCharStream(String fulltext) { - super(new StringReader(fulltext)); - this.fullText = fulltext; - this.document = new TokenDocument(fullText); + public JavaCharStream(JavaccTokenDocument document) { + super(new StringReader(document.getFullText())); + this.fullText = document.getFullText(); + this.document = document; this.startOffsets = new int[bufsize]; maxNextCharInd = fullText.length(); nextCharBuf = null; } - public JavaCharStream(Reader toDump) { - this(toString(toDump)); - } - @Override protected void ExpandBuff(boolean wrapAround) { int[] newStartOffsets = new int[bufsize + 2048]; @@ -61,25 +50,27 @@ public class JavaCharStream extends JavaCharStreamBase { } @Override - protected void beforeReadChar() { - if (bufpos + 1 < available) { - startOffsets[bufpos + 1] = nextCharInd + 1; - } + protected void UpdateLineColumn(char c) { + startOffsets[bufpos] = nextCharInd; + super.UpdateLineColumn(c); } + @Override public int getStartOffset() { return startOffsets[tokenBegin]; } + @Override public int getEndOffset() { - if (bufpos >= startOffsets.length) { + if (isAtEof()) { return fullText.length(); } else { return startOffsets[bufpos] + 1; // + 1 for exclusive } } - public TokenDocument getTokenDocument() { + @Override + public JavaccTokenDocument getTokenDocument() { return document; } @@ -87,31 +78,31 @@ public class JavaCharStream extends JavaCharStreamBase { protected char ReadByte() throws IOException { ++nextCharInd; - if (nextCharInd >= fullText.length()) { + if (isAtEof()) { if (bufpos != 0) { --bufpos; - backup(0); + if (bufpos < 0) { + bufpos += bufsize; + } } else { bufline[bufpos] = line; bufcolumn[bufpos] = column; + startOffsets[bufpos] = fullText.length(); } - throw new IOException(); + throw new EOFException(); } return fullText.charAt(nextCharInd); } + private boolean isAtEof() { + return nextCharInd >= fullText.length(); + } + @Override protected void FillBuff() { throw new IllegalStateException("Buffer shouldn't be refilled"); } - private static String toString(Reader dstream) { - try (Reader r = dstream) { - return IOUtils.toString(r); - } catch (IOException e) { - throw new RuntimeException(e); - } - } } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JavaccToken.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JavaccToken.java index c1f13f6614..cac4d4ab17 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JavaccToken.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JavaccToken.java @@ -4,12 +4,13 @@ package net.sourceforge.pmd.lang.ast.impl.javacc; +import java.util.Comparator; + +import net.sourceforge.pmd.lang.ast.CharStream; import net.sourceforge.pmd.lang.ast.GenericToken; -import net.sourceforge.pmd.lang.ast.impl.TokenDocument; /** - * A generic token implementation for JavaCC parsers. Will probably help - * remove those duplicated implementations that all have the same name. + * A generic token implementation for JavaCC parsers. * *

Largely has the same interface as the default generated token class. * The main difference is that the position of the token is encoded as @@ -21,19 +22,27 @@ import net.sourceforge.pmd.lang.ast.impl.TokenDocument; * and column bounds can be derived as well - though this should not be * done systematically because it's costlier. *

  • It's a bit lighter. Token instances are one of the most numerous - * class in a typical PMD run and this may reduce GC pressur. + * class in a typical PMD run and this may reduce GC pressure. * * *

    TODO replace duplicates over PMD. */ -public class JavaccToken implements GenericToken, java.io.Serializable { +public class JavaccToken implements GenericToken, Comparable { /** - * The version identifier for this Serializable class. - * Increment only if the serialized form of the - * class changes. + * Kind for EOF tokens. */ - private static final long serialVersionUID = 4L; + public static final int EOF = 0; + /** + * Kind for implicit tokens. Negative because JavaCC only picks + * positive numbers for token kinds. + */ + public static final int IMPLICIT_TOKEN = -1; + + private static final Comparator COMPARATOR = + Comparator.comparingInt(JavaccToken::getStartInDocument) + .thenComparing(JavaccToken::getEndInDocument); + /** * An integer that describes the kind of this token. This numbering @@ -41,6 +50,12 @@ public class JavaccToken implements GenericToken, java.io.Serializable { * stored in the file ...Constants.java. */ public final int kind; + + protected final JavaccTokenDocument document; + private final CharSequence image; + private final int startInclusive; + private final int endExclusive; + /** * A reference to the next regular (non-special) token from the input * stream. If this is the last token from the input stream, or if the @@ -50,6 +65,7 @@ public class JavaccToken implements GenericToken, java.io.Serializable { * this field. */ public JavaccToken next; + /** * This field is used to access special tokens that occur prior to this * token, but after the immediately preceding regular (non-special) token. @@ -64,28 +80,33 @@ public class JavaccToken implements GenericToken, java.io.Serializable { */ public JavaccToken specialToken; - private final CharSequence image; - private final int startInclusive; - private final int endExclusive; - protected final TokenDocument document; - - /** {@link #undefined()} */ - private JavaccToken() { - this(null); - } + /** + * @deprecated This is used by a few deprecated tests about comments, + * will be removed when they're updated. + */ + @Deprecated public JavaccToken(String image) { this(-1, image, -1, -1, null); } /** - * Constructs a new token for the specified Image and Kind. + * Builds a new token of the specified kind. + * + * @param kind Kind of token + * @param image Image of the token (after translating escapes if any) + * @param startInclusive Start character of the token in the text file (before translating escapes) + * @param endExclusive End of the token in the text file (before translating escapes) + * @param document Document owning the token */ public JavaccToken(int kind, CharSequence image, int startInclusive, int endExclusive, - TokenDocument document) { + JavaccTokenDocument document) { + assert startInclusive <= endExclusive + : "Offsets should be correctly ordered: " + startInclusive + " <= " + endExclusive; + this.kind = kind; this.image = image; this.startInclusive = startInclusive; @@ -93,14 +114,21 @@ public class JavaccToken implements GenericToken, java.io.Serializable { this.document = document; } + /** + * Returns the document owning this token. + */ + public JavaccTokenDocument getDocument() { + return document; + } + @Override - public GenericToken getNext() { + public JavaccToken getNext() { return next; } @Override - public GenericToken getPreviousComment() { + public JavaccToken getPreviousComment() { return specialToken; } @@ -126,7 +154,7 @@ public class JavaccToken implements GenericToken, java.io.Serializable { @Override public int getEndLine() { - return document == null ? -1 : document.lineNumberFromOffset(endExclusive - 1); + return document == null ? -1 : document.lineNumberFromOffset(endExclusive); } @Override @@ -136,19 +164,105 @@ public class JavaccToken implements GenericToken, java.io.Serializable { @Override public int getEndColumn() { - return document == null ? -1 : document.columnFromOffset(endExclusive - 1); + return document == null ? -1 : document.columnFromOffset(endExclusive); + } + + + @Override + public boolean isImplicit() { + return kind == IMPLICIT_TOKEN; + } + + @Override + public String toString() { + return document.describeKind(kind) + ": " + getImage(); } /** - * Returns the image. + * Returns a new token with the same kind as this one, whose image + * is replaced by the one marked on the char stream. + * + * @param charStream Char stream from which to start + * + * @return A new token */ - @Override - public String toString() { - return image.toString(); + public JavaccToken replaceImage(CharStream charStream) { + return new JavaccToken( + this.kind, + charStream.GetImage(), + this.startInclusive, + charStream.getEndOffset(), + this.document + ); } - public static JavaccToken undefined() { - return new JavaccToken(); + /** + * Returns a new token with the given kind, and all other parameters + * identical to this one. + * + * @param newKind Char stream from which to start + * + * @return A new token + */ + public JavaccToken withKind(int newKind) { + JavaccToken tok = new JavaccToken( + newKind, + this.image, + this.startInclusive, + this.endExclusive, + this.document + ); + tok.specialToken = this.specialToken; + tok.next = this.next; + return tok; + } + + @Override + public int compareTo(JavaccToken o) { + return COMPARATOR.compare(this, o); + } + + + /** + * Creates an implicit token, with zero length, that is linked to + * the given token as its special predecessor. + * + * @param next Token before which to insert the new token + * + * @return A new token + */ + public static JavaccToken implicitBefore(JavaccToken next) { + + JavaccToken implicit = newImplicit(next.getStartInDocument(), next.document); + + // insert it right before the next token + // as a special token + implicit.next = next; + + if (next.specialToken != null) { + next.specialToken.next = implicit; + implicit.specialToken = next.specialToken; + } + + next.specialToken = implicit; + + return implicit; + } + + /** + * Returns a new implicit token, positioned at the given offset. + * + * @param offset Offset of the token + * @param document Document owning the token + * + * @return A new token + */ + public static JavaccToken newImplicit(int offset, JavaccTokenDocument document) { + return new JavaccToken(IMPLICIT_TOKEN, + "", + offset, + offset, + document); } } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JavaccTokenDocument.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JavaccTokenDocument.java new file mode 100644 index 0000000000..956effd204 --- /dev/null +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JavaccTokenDocument.java @@ -0,0 +1,112 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.ast.impl.javacc; + +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import net.sourceforge.pmd.lang.ast.CharStream; +import net.sourceforge.pmd.lang.ast.impl.TokenDocument; + +/** + * Token document for Javacc implementations. This is a helper object + * for generated token managers. + */ +public class JavaccTokenDocument extends TokenDocument { + + private JavaccToken first; + + public JavaccTokenDocument(String fullText) { + super(fullText); + } + + /** + * Open the document. This is only meant to be used by a Javacc-generated + * parser. + * + * @return The token for the document start. This token is implicit and + * will never end up in the final token chain. + * + * @throws IllegalStateException If the document has already been opened + */ + public JavaccToken open() { + synchronized (this) { + if (first != null) { + throw new RuntimeException("Document is already opened"); + } + first = JavaccToken.newImplicit(0, this); + } + return first; + } + + + @Override + public JavaccToken getFirstToken() { + if (first == null || first.next == null) { + throw new IllegalStateException("Document has not been opened"); + } + return first.next; + } + + /** + * Returns a string that describes the token kind. + * + * @param kind Kind of token + * + * @return A descriptive string + */ + public final @NonNull String describeKind(int kind) { + if (kind == JavaccToken.IMPLICIT_TOKEN) { + return ""; + } + String impl = describeKindImpl(kind); + if (impl != null) { + return impl; + } + return ""; + } + + /** + * Describe the given kind. If this returns a non-null value, then + * that's what {@link #describeKind(int)} will use. Otherwise a default + * implementation is used. + * + *

    An implementation typically uses the JavaCC-generated array + * named {@code Constants.tokenImage}. Remember to + * check the bounds of the array. + * + * @param kind Kind of token + * + * @return A descriptive string, or null to use default + */ + protected @Nullable String describeKindImpl(int kind) { + return null; + } + + + /** + * Creates a new token with the given kind. This is called back to + * by JavaCC-generated token managers (jjFillToken). Note that a + * created token is not guaranteed to end up in the final token chain. + * + * @param kind Kind of the token + * @param cs Char stream of the file. This can be used to get text + * coordinates and the image + * @param image Shared instance of the image token. If this is non-null, + * then no call to {@link CharStream#GetImage()} should be + * issued. + * + * @return A new token + */ + public JavaccToken createToken(int kind, CharStream cs, @Nullable String image) { + return new JavaccToken( + kind, + image == null ? cs.GetImage() : image, + cs.getStartOffset(), + cs.getEndOffset(), + this + ); + } +} diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JjtreeBuilder.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JjtreeBuilder.java new file mode 100644 index 0000000000..9b6e3fc502 --- /dev/null +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JjtreeBuilder.java @@ -0,0 +1,217 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.ast.impl.javacc; + +import java.util.ArrayList; +import java.util.List; + +/** + * Shared implementation of the tree builder generated by JJTree. + * + * @param Type of node this takes + */ +public final class JjtreeBuilder> { + + private final List nodes = new ArrayList<>(); + private final List marks = new ArrayList<>(); + + private int sp = 0; // number of nodes on stack + private int mk = 0; // current mark + private boolean nodeCreated; + + /*** If non-zero, then the top "n" nodes of the stack will be injected as the first children of the next + * node to be opened. This is not very flexible, but it's enough. The grammar needs to take + * care of the order in which nodes are opened in a few places, in most cases this just means using + * eg A() B() #N(2) instead of (A() B()) #N, so as not to open N before A. + */ + private int numPendingInjection; + + + /** + * Determines whether the current node was actually closed and + * pushed. This should only be called in the final user action of a + * node scope. + */ + public boolean nodeCreated() { + return nodeCreated; + } + + /** + * Call this to reinitialize the node stack. It is called + * automatically by the parser's ReInit() method. + */ + public void reset() { + nodes.clear(); + marks.clear(); + sp = 0; + mk = 0; + } + + /** + * Returns the root node of the AST. It only makes sense to call + * this after a successful parse. + */ + public N rootNode() { + return nodes.get(0); + } + + /*** + * Extend the number of children of the current node of one to the left. + * If the node is closed, one additional node from the stack will be popped + * and added to its children. This allows mimicking "left-recursive" nodes, + * while keeping the parsing iterative. + * + *

    Note that when the total number of children is definitely known, you + * can use "definite nodes", ie write the expected number of children (including + * the ones to the left) in the JJTree annotation (eg {@code #AdditiveExpression(2)}). + * So this is only useful when the number of children of the current node is not certain. + * + *

    This method does not affect the stack unless the current jjtThis is + * closed in the future. + */ + public void extendLeft() { + mk--; + } + + /*** + * Peek the nth node from the top of the stack. + * peekNode(0) == peekNode() + */ + public N peekNode(int n) { + return nodes.get(nodes.size() - n - 1); + } + + public boolean isInjectionPending() { + return numPendingInjection > 0; + } + + public void injectRight(int n) { + numPendingInjection = n; + } + + /** Pushes a node on to the stack. */ + + public void pushNode(N n) { + nodes.add(n); + ++sp; + } + + /** + * Returns the node on the top of the stack, and remove it from the + * stack. + */ + public N popNode() { + --sp; + if (sp < mk) { + mk = marks.remove(marks.size() - 1); + } + return nodes.remove(nodes.size() - 1); + } + + /** Returns the node currently on the top of the stack. */ + public N peekNode() { + return nodes.get(nodes.size() - 1); + } + + /** + * Returns the number of children on the stack in the current node + * scope. + */ + public int nodeArity() { + return sp - mk; + } + + + public void clearNodeScope(N n) { + while (sp > mk) { + popNode(); + } + mk = marks.remove(marks.size() - 1); + } + + + public void openNodeScope(N n, JavaccToken firstToken) { + marks.add(mk); + mk = sp; + + if (isInjectionPending()) { + mk -= numPendingInjection; + numPendingInjection = 0; + } + + n.jjtSetFirstToken(firstToken); + n.jjtOpen(); + } + + + /** + * Close the node scope and adds the given number of children to the + * node. A definite node is constructed from a specified number of + * children. That number of nodes are popped from the stack and + * made the children of the definite node. Then the definite node + * is pushed on to the stack. + */ + public void closeNodeScope(N n, final int num, JavaccToken lastToken) { + int a = nodeArity(); + mk = marks.remove(marks.size() - 1); + N child = null; + int i = num; + while (i-- > 0) { + child = popNode(); + n.jjtAddChild(child, i); + } + + if (child != null && num > a) { + // this node has more children that what was in its node scope + // (ie first token is wrong) + n.jjtSetFirstToken(child.jjtGetFirstToken()); + } + + closeImpl(n, lastToken); + } + + + /** + * Close the node scope if the condition is true. + * All the nodes that have been pushed since the node was opened are + * made children of the conditional node, which is then pushed on to + * the stack. If the condition is false the node is not constructed + * and they are left on the stack. + * + * @param n Node to close + * @param condition Whether to close the node or not + * @param lastToken Last token that was consumed while the node scope was open + */ + public void closeNodeScope(N n, boolean condition, JavaccToken lastToken) { + if (condition) { + int a = nodeArity(); + mk = marks.remove(marks.size() - 1); + while (a-- > 0) { + n.jjtAddChild(popNode(), a); + } + closeImpl(n, lastToken); + } else { + mk = marks.remove(marks.size() - 1); + nodeCreated = false; + } + } + + private void closeImpl(N n, JavaccToken lastToken) { + if (lastToken.getNext() == n.jjtGetFirstToken()) { + // this means, that the node has zero length. + // create an implicit token to represent this case. + JavaccToken implicit = JavaccToken.implicitBefore(lastToken.getNext()); + + n.jjtSetFirstToken(implicit); + n.jjtSetLastToken(implicit); + } else { + n.jjtSetLastToken(lastToken); + } + // note that the last token has been set before jjtClose + n.jjtClose(); + pushNode(n); + nodeCreated = true; + } +} diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JjtreeParserAdapter.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JjtreeParserAdapter.java new file mode 100644 index 0000000000..e5f9d9fc08 --- /dev/null +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JjtreeParserAdapter.java @@ -0,0 +1,73 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.ast.impl.javacc; + +import java.io.Reader; + +import net.sourceforge.pmd.lang.Parser; +import net.sourceforge.pmd.lang.ParserOptions; +import net.sourceforge.pmd.lang.TokenManager; +import net.sourceforge.pmd.lang.ast.CharStream; +import net.sourceforge.pmd.lang.ast.ParseException; +import net.sourceforge.pmd.lang.ast.RootNode; +import net.sourceforge.pmd.lang.ast.TokenMgrError; + +/** + * Base implementation of the {@link Parser} interface for JavaCC language + * implementations. This wraps a parser generated by JavaCC, it's not meant + * as a base class for the generated parser. + * + * @param Type of the root node of this language + */ +public abstract class JjtreeParserAdapter implements Parser { + + protected final ParserOptions parserOptions; + + protected JjtreeParserAdapter(ParserOptions parserOptions) { + this.parserOptions = parserOptions; + } + + @Override + public ParserOptions getParserOptions() { + return parserOptions; + } + + @Override + public TokenManager getTokenManager(String fileName, Reader source) { + TokenManager tokenManager = createTokenManager(source); + tokenManager.setFileName(fileName); + return tokenManager; + } + + protected abstract TokenManager createTokenManager(Reader source); + + + protected abstract JavaccTokenDocument newDocument(String fullText); + + protected CharStream newCharStream(JavaccTokenDocument tokenDocument) { + return new SimpleCharStream(tokenDocument); + } + + @Override + public R parse(String fileName, Reader source) throws ParseException { + String text = CharStreamFactory.toString(source); + JavaccTokenDocument doc = newDocument(text); + CharStream charStream = newCharStream(doc); + + try { + return parseImpl(charStream, getParserOptions()); + } catch (TokenMgrError tme) { + throw tme.withFileName(fileName); + } + } + + protected abstract R parseImpl(CharStream cs, ParserOptions options) throws ParseException; + + + @Override + public String toString() { + return getClass().getSimpleName(); + } +} diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/SimpleCharStream.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/SimpleCharStream.java new file mode 100644 index 0000000000..d453b34797 --- /dev/null +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/SimpleCharStream.java @@ -0,0 +1,20 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.ast.impl.javacc; + +/** + * A char stream that does not perform unicode escaping. + */ +public class SimpleCharStream extends JavaCharStream { + + public SimpleCharStream(JavaccTokenDocument document) { + super(document); + } + + @Override + protected boolean doEscape() { + return false; + } +} diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/package-info.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/package-info.java new file mode 100644 index 0000000000..2f453c2812 --- /dev/null +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/package-info.java @@ -0,0 +1,8 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +/** + * Support classes for language implementations based on JavaCC. + */ +package net.sourceforge.pmd.lang.ast.impl.javacc; diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/AxisStream.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/AxisStream.java index e70892251f..0d852f11d4 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/AxisStream.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/AxisStream.java @@ -19,6 +19,7 @@ import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import net.sourceforge.pmd.internal.util.AssertionUtil; +import net.sourceforge.pmd.internal.util.IteratorUtil; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.ast.NodeStream; @@ -52,8 +53,8 @@ abstract class AxisStream extends IteratorBasedNStream { } @Override - public NodeStream filter(Predicate predicate) { - return copyWithFilter(filter.thenApply(Filtermap.filter(predicate))); + public NodeStream filter(Predicate predicate) { + return copyWithFilter(filter.thenFilter(predicate)); } @Override @@ -61,6 +62,61 @@ abstract class AxisStream extends IteratorBasedNStream { return copyWithFilter(filter.thenCast(r1Class)); } + /* + * Override one of these three to implement all the overloads of first/last/toList + */ + + protected @Nullable O firstImpl(Filtermap filter) { + Iterator iter = filter.filterMap(baseIterator()); + return iter.hasNext() ? iter.next() : null; + } + + protected @Nullable O lastImpl(Filtermap filter) { + Iterator iter = filter.filterMap(baseIterator()); + return IteratorUtil.last(iter); + } + + protected List toListImpl(Filtermap filter) { + Iterator iter = filter.filterMap(baseIterator()); + return IteratorUtil.toList(iter); + } + + @Override + public @Nullable T first() { + return firstImpl(filter); + } + + @Override + public @Nullable R first(Class r1Class) { + return firstImpl(filter.thenCast(r1Class)); + } + + @Override + public @Nullable T first(Predicate predicate) { + return firstImpl(filter.thenFilter(predicate)); + } + + @Nullable + @Override + public T last() { + return lastImpl(filter); + } + + @Override + public List toList() { + return toListImpl(this.filter); + } + + @Override + public List toList(Function mapper) { + return toListImpl(this.filter.thenApply(mapper)); + } + + @Override + public @Nullable R last(Class rClass) { + return lastImpl(filter.thenCast(rClass)); + } + @Override public String toString() { return getClass().getSimpleName() + "[" + node + "] -> " + toList(); @@ -95,8 +151,7 @@ abstract class AxisStream extends IteratorBasedNStream { default: // eg for NodeStream.of(a,b,c).drop(2) Node nth = get(n); // get(2) == c - return nth == null ? NodeStream.empty() - : StreamImpl.ancestorsOrSelf(nth, filter); // c.ancestorsOrSelf() == [c] + return StreamImpl.ancestorsOrSelf(nth, filter); // c.ancestorsOrSelf() == [c] } } @@ -106,7 +161,7 @@ abstract class AxisStream extends IteratorBasedNStream { } @Override - public @Nullable T first() { + protected @Nullable O firstImpl(Filtermap filter) { return TraversalUtils.getFirstParentOrSelfMatching(node, filter); } } @@ -138,86 +193,123 @@ abstract class AxisStream extends IteratorBasedNStream { } } - static class FilteredDescendantStream extends AxisStream { + abstract static class DescendantStreamBase extends AxisStream implements DescendantNodeStream { - FilteredDescendantStream(Node node, Filtermap target) { - super(node, target); + final TreeWalker walker; + + DescendantStreamBase(@NonNull Node root, + TreeWalker walker, + Filtermap filter) { + super(root, filter); + this.walker = walker; } + protected abstract DescendantNodeStream copyWithWalker(Filtermap filterMap, TreeWalker walker); + @Override - protected Iterator baseIterator() { - DescendantOrSelfIterator iter = new DescendantOrSelfIterator(node); - iter.next(); // skip self - return iter; + public DescendantNodeStream crossFindBoundaries(boolean cross) { + return walker.isCrossFindBoundaries() == cross + ? this + : copyWithWalker(this.filter, walker.crossFindBoundaries(cross)); } @Override protected NodeStream copyWithFilter(Filtermap filterMap) { - return new FilteredDescendantStream<>(node, filterMap); + return copyWithWalker(filterMap, walker); + } + } + + static class FilteredDescendantStream extends DescendantStreamBase { + + FilteredDescendantStream(Node node, + TreeWalker walker, + Filtermap target) { + super(node, walker, target); } @Override - public @Nullable T first() { - return TraversalUtils.getFirstDescendantOfType(node, filter); + protected Iterator baseIterator() { + return walker.descendantIterator(node); + } + + @Override + protected DescendantNodeStream copyWithWalker(Filtermap filterMap, TreeWalker walker) { + return new FilteredDescendantStream<>(node, walker, filterMap); + } + + @Override + protected @Nullable O firstImpl(Filtermap filter) { + return walker.getFirstDescendantOfType(node, filter); } @Override public boolean nonEmpty() { - return TraversalUtils.getFirstDescendantOfType(node, filter) != null; + return walker.getFirstDescendantOfType(node, filter) != null; } @Override - public List toList() { - List result = new ArrayList<>(); - TraversalUtils.findDescendantsOfType(node, filter, result, false); - return result; + protected List toListImpl(Filtermap filter) { + return walker.findDescendantsMatching(node, filter); } } static class DescendantStream extends FilteredDescendantStream { - DescendantStream(Node node) { - super(node, Filtermap.NODE_IDENTITY); + DescendantStream(Node node, TreeWalker walker) { + super(node, walker, Filtermap.NODE_IDENTITY); } + @Override + public DescendantNodeStream crossFindBoundaries(boolean cross) { + return new DescendantStream(node, walker.crossFindBoundaries(cross)); + } + + @Override public boolean nonEmpty() { return node.getNumChildren() > 0; } } - static class FilteredDescendantOrSelfStream extends AxisStream { + static class FilteredDescendantOrSelfStream extends DescendantStreamBase { - FilteredDescendantOrSelfStream(Node node, Filtermap filtermap) { - super(node, filtermap); + FilteredDescendantOrSelfStream(Node node, + TreeWalker walker, + Filtermap filtermap) { + super(node, walker, filtermap); } @Override public Iterator baseIterator() { - return new DescendantOrSelfIterator(node); + return walker.descendantOrSelfIterator(node); } @Override - protected NodeStream copyWithFilter(Filtermap filterMap) { - return new FilteredDescendantOrSelfStream<>(node, filterMap); + protected DescendantNodeStream copyWithWalker(Filtermap filterMap, TreeWalker walker) { + return new FilteredDescendantOrSelfStream<>(node, walker, filterMap); } @Override - public List toList() { - List result = new ArrayList<>(); - T top = filter.apply(node); + protected List toListImpl(Filtermap filter) { + List result = new ArrayList<>(); + O top = filter.apply(node); if (top != null) { result.add(top); } - TraversalUtils.findDescendantsOfType(node, filter, result, false); + walker.findDescendantsMatching(node, filter, result); return result; } } static final class DescendantOrSelfStream extends FilteredDescendantOrSelfStream { - DescendantOrSelfStream(Node node) { - super(node, Filtermap.NODE_IDENTITY); + DescendantOrSelfStream(Node node, TreeWalker walker) { + super(node, walker, Filtermap.NODE_IDENTITY); + } + + @Override + public DescendantNodeStream crossFindBoundaries(boolean cross) { + return new DescendantOrSelfStream(node, walker.crossFindBoundaries(cross)); } @Nullable @@ -247,8 +339,13 @@ abstract class AxisStream extends IteratorBasedNStream { this.len = len; } - FilteredChildrenStream(Node root, Filtermap filtermap) { - this(root, filtermap, 0, root.getNumChildren()); + + @Override + public NodeStream flatMap(Function> mapper) { + // all operations like #children, #followingSiblings, etc + // operate on an eagerly evaluated stream. May be empty or + // singleton + return StreamImpl.fromNonNullList(toList()).flatMap(mapper); } @Override @@ -266,28 +363,16 @@ abstract class AxisStream extends IteratorBasedNStream { return TraversalUtils.childrenIterator(node, low, low + len); } - @Override - public @Nullable T first() { + protected @Nullable O firstImpl(Filtermap filter) { return TraversalUtils.getFirstChildMatching(node, filter, low, len); } @Override - public @Nullable T last() { + protected @Nullable O lastImpl(Filtermap filter) { return TraversalUtils.getLastChildMatching(node, filter, low, len); } - - @Override - public @Nullable R first(Class rClass) { - return TraversalUtils.getFirstChildMatching(node, filter.thenCast(rClass), low, len); - } - - @Override - public @Nullable R last(Class rClass) { - return TraversalUtils.getLastChildMatching(node, filter.thenCast(rClass), low, len); - } - @Override public int count() { return TraversalUtils.countChildrenMatching(node, filter, low, len); @@ -299,11 +384,10 @@ abstract class AxisStream extends IteratorBasedNStream { } @Override - public List toList() { + protected List toListImpl(Filtermap filter) { return TraversalUtils.findChildrenMatching(node, filter, low, len); } - @Override public NodeStream take(int maxSize) { AssertionUtil.requireNonNegative("maxSize", maxSize); @@ -313,10 +397,12 @@ abstract class AxisStream extends IteratorBasedNStream { @Override public NodeStream drop(int n) { AssertionUtil.requireNonNegative("n", n); + if (n == 0) { + return this; + } int newLow = min(low + n, node.getNumChildren()); int newLen = max(len - n, 0); - - return n == 0 ? this : StreamImpl.sliceChildren(node, filter, newLow, newLen); + return StreamImpl.sliceChildren(node, filter, newLow, newLen); } @Override @@ -333,10 +419,6 @@ abstract class AxisStream extends IteratorBasedNStream { super(root, Filtermap.NODE_IDENTITY, low, len); } - ChildrenStream(@NonNull Node root) { - super(root, Filtermap.NODE_IDENTITY); - } - @Nullable @Override public Node first() { @@ -349,6 +431,13 @@ abstract class AxisStream extends IteratorBasedNStream { return len > 0 ? node.getChild(low + len - 1) : null; } + @Nullable + @Override + public Node get(int n) { + AssertionUtil.requireNonNegative("n", n); + return len > 0 && n < len ? node.getChild(low + n) : null; + } + @Override public boolean nonEmpty() { return len > 0; diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/DescendantOrSelfIterator.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/DescendantOrSelfIterator.java deleted file mode 100644 index 4aa87e61c3..0000000000 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/DescendantOrSelfIterator.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * BSD-style license; for more info see http://pmd.sourceforge.net/license.html - */ - -package net.sourceforge.pmd.lang.ast.internal; - -import java.util.ArrayDeque; -import java.util.Deque; -import java.util.Iterator; - -import org.checkerframework.checker.nullness.qual.NonNull; - -import net.sourceforge.pmd.lang.ast.Node; - -/** Iterates over a node and its descendants. */ -class DescendantOrSelfIterator implements Iterator<@NonNull Node> { - - private final Deque queue = new ArrayDeque<>(); - - /** Always {@link #hasNext()} after exiting the constructor. */ - DescendantOrSelfIterator(Node top) { - queue.addFirst(top); - } - - @Override - public boolean hasNext() { - return !queue.isEmpty(); - } - - - @Override - public @NonNull Node next() { - Node node = queue.removeFirst(); - enqueueChildren(node); - return node; - } - - - private void enqueueChildren(Node n) { - for (int i = n.getNumChildren() - 1; i >= 0; i--) { - queue.addFirst(n.getChild(i)); - } - } -} diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/Filtermap.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/Filtermap.java index e4500cbda9..ba3165504e 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/Filtermap.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/Filtermap.java @@ -23,24 +23,24 @@ import net.sourceforge.pmd.lang.ast.Node; interface Filtermap extends Function<@NonNull I, @Nullable O>, Predicate<@NonNull I> { - Filtermap NODE_IDENTITY = emptyFilter(); + Filtermap NODE_IDENTITY = identityFilter(); /** * Returns a null value if the filter accepts the value. Otherwise - * returns the transformed value. + * returns the transformed value. MUST return null for null parameter. */ @Override - @Nullable O apply(@NonNull I i); + @Nullable O apply(@Nullable I i); @Override - default boolean test(@NonNull I i) { + default boolean test(@Nullable I i) { return apply(i) != null; } /** Filter an iterator. */ - default Iterator filterMap(Iterator iter) { + default Iterator filterMap(Iterator iter) { return IteratorUtil.mapNotNull(iter, this); } @@ -49,6 +49,9 @@ interface Filtermap extends Function<@NonNull I, @Nullable O>, Predicate<@ default Filtermap thenApply(Function<@NonNull ? super O, @Nullable ? extends R> then) { Objects.requireNonNull(then); return i -> { + if (i == null) { + return null; + } O o = this.apply(i); return o == null ? null : then.apply(o); }; @@ -60,34 +63,56 @@ interface Filtermap extends Function<@NonNull I, @Nullable O>, Predicate<@ } - static Filtermap emptyFilter() { + default Filtermap thenFilter(Predicate rClass) { + return thenApply(filter(rClass)); + } + + + static Filtermap identityFilter() { return new Filtermap() { @Override - public I apply(@NonNull I i) { + public I apply(@Nullable I i) { return i; } @Override @SuppressWarnings("unchecked") public Filtermap thenApply(Function<@NonNull ? super I, @Nullable ? extends R> then) { - return then instanceof Filtermap ? (Filtermap) then : then::apply; + return then instanceof Filtermap ? (Filtermap) then : Filtermap.super.thenApply(then); } @Override - public Iterator filterMap(Iterator iter) { - return iter; + @SuppressWarnings("unchecked") + public Iterator filterMap(Iterator iter) { + return (Iterator) iter; + } + + @Override + public String toString() { + return "IdentityFilter"; } }; } - static Filtermap filter(Predicate pred) { - return i -> pred.test(i) ? i : null; + static Filtermap filter(Predicate pred) { + return i -> i != null && pred.test(i) ? i : null; } static Filtermap isInstance(Class oClass) { - return i -> oClass.isInstance(i) ? oClass.cast(i) : null; + return new Filtermap() { + @Override + @SuppressWarnings("unchecked") + public @Nullable O apply(@Nullable I i) { + return oClass.isInstance(i) ? (O) i : null; + } + + @Override + public String toString() { + return "IsInstance[" + oClass + "]"; + } + }; } } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/GreedyNStream.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/GreedyNStream.java new file mode 100644 index 0000000000..0f5452a2a1 --- /dev/null +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/GreedyNStream.java @@ -0,0 +1,77 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.ast.internal; + +import java.util.Iterator; +import java.util.List; +import java.util.Spliterator; +import java.util.Spliterators; +import java.util.function.Function; + +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import net.sourceforge.pmd.internal.util.AssertionUtil; +import net.sourceforge.pmd.internal.util.IteratorUtil; +import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.ast.NodeStream; + +/** + * A greedy stream evaluates all axis operations, except for descendants, + * greedily. + */ +abstract class GreedyNStream extends IteratorBasedNStream { + + @Override + protected NodeStream mapIter(Function, Iterator> fun) { + return StreamImpl.fromNonNullList(IteratorUtil.toNonNullList(fun.apply(iterator()))); + } + + @Override + public T first() { + return toList().get(0); + } + + @Override + public @Nullable T get(int n) { + AssertionUtil.requireNonNegative("n", n); + List tList = toList(); + return n < tList.size() ? tList.get(n) : null; + } + + @Override + public Iterator iterator() { + return toList().iterator(); + } + + @Override + public abstract List toList(); + + @Override + public Spliterator spliterator() { + Spliterator spliter = toList().spliterator(); + return Spliterators.spliterator(iterator(), spliter.estimateSize(), + spliter.characteristics() | Spliterator.NONNULL); + } + + @Override + public NodeStream cached() { + return this; + } + + static class GreedyKnownNStream extends GreedyNStream { + + private final List<@NonNull T> coll; + + GreedyKnownNStream(List<@NonNull T> coll) { + this.coll = coll; + } + + @Override + public List toList() { + return coll; + } + } +} diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/IteratorBasedNStream.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/IteratorBasedNStream.java index 9e37ce98d5..0df1d9c8a9 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/IteratorBasedNStream.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/IteratorBasedNStream.java @@ -62,10 +62,37 @@ abstract class IteratorBasedNStream implements NodeStream { } @Override - public NodeStream filter(Predicate predicate) { + public NodeStream filter(Predicate predicate) { return mapIter(it -> IteratorUtil.mapNotNull(it, Filtermap.filter(predicate))); } + @Override + public NodeStream filterIs(Class rClass) { + return mapIter(it -> IteratorUtil.mapNotNull(it, Filtermap.isInstance(rClass))); + } + + @Override + public DescendantNodeStream descendants() { + return flatMapDescendants(Node::descendants); + } + + @Override + public DescendantNodeStream descendantsOrSelf() { + return flatMapDescendants(Node::descendantsOrSelf); + } + + @Override + public DescendantNodeStream descendants(Class rClass) { + return flatMapDescendants(node -> node.descendants(rClass)); + } + + + @NonNull + protected DescendantNodeStream flatMapDescendants(Function> mapper) { + return new DescendantMapping<>(this, mapper); + } + + @Override public void forEach(Consumer action) { iterator().forEachRemaining(action); @@ -190,37 +217,19 @@ abstract class IteratorBasedNStream implements NodeStream { @Override public NodeStream cached() { - return new IteratorBasedNStream() { - - private List cache; - - @Override - public Iterator iterator() { - return toList().iterator(); - } - - @Override - public int count() { - return toList().size(); - } - - @Override - public List toList() { - if (cache == null) { - cache = IteratorBasedNStream.this.toList(); - } - return cache; - } - - @Override - public String toString() { - return "CachedStream[" + IteratorBasedNStream.this + "]"; - } - }; + return StreamImpl.fromNonNullList(toList()); } - private IteratorMapping mapIter(Function, Iterator> fun) { - return new IteratorMapping(fun); + protected NodeStream mapIter(Function, Iterator> fun) { + return new IteratorMapping<>(fun); + } + + + @Override + public String toString() { + return getClass().getSimpleName() + " [" + + toStream().map(Objects::toString).collect(Collectors.joining(", ")) + + "]"; } private class IteratorMapping extends IteratorBasedNStream { @@ -239,8 +248,35 @@ abstract class IteratorBasedNStream implements NodeStream { } } - @Override - public String toString() { - return getClass().getSimpleName() + " [" + toStream().map(Objects::toString).collect(Collectors.joining(", ")) + "]"; + + private static class DescendantMapping extends IteratorBasedNStream implements DescendantNodeStream { + + private final Function> fun; + private final TreeWalker walker; + private final IteratorBasedNStream upstream; + + + private DescendantMapping(IteratorBasedNStream upstream, Function> fun, TreeWalker walker) { + this.fun = fun; + this.walker = walker; + this.upstream = upstream; + } + + DescendantMapping(IteratorBasedNStream upstream, Function> fun) { + this(upstream, fun, TreeWalker.DEFAULT); + } + + @Override + public Iterator iterator() { + return IteratorUtil.flatMap(upstream.iterator(), + fun.andThen(walker::apply).andThen(NodeStream::iterator)); + } + + @Override + public DescendantNodeStream crossFindBoundaries(boolean cross) { + return walker.isCrossFindBoundaries() == cross + ? this + : new DescendantMapping<>(upstream, fun, walker.crossFindBoundaries(cross)); + } } } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/SingletonNodeStream.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/SingletonNodeStream.java index bc0075dd1b..14c97d6cc8 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/SingletonNodeStream.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/SingletonNodeStream.java @@ -179,17 +179,17 @@ final class SingletonNodeStream extends IteratorBasedNStream } @Override - public NodeStream descendants() { + public DescendantNodeStream descendants() { return StreamImpl.descendants(node); } @Override - public NodeStream descendants(Class rClass) { + public DescendantNodeStream descendants(Class rClass) { return StreamImpl.descendants(node, rClass); } @Override - public NodeStream descendantsOrSelf() { + public DescendantNodeStream descendantsOrSelf() { return StreamImpl.descendantsOrSelf(node); } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/StreamImpl.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/StreamImpl.java index 62a25d0c2d..cbc2e36462 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/StreamImpl.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/StreamImpl.java @@ -5,6 +5,7 @@ package net.sourceforge.pmd.lang.ast.internal; +import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -18,6 +19,7 @@ import org.checkerframework.checker.nullness.qual.Nullable; import net.sourceforge.pmd.internal.util.IteratorUtil; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.ast.NodeStream; +import net.sourceforge.pmd.lang.ast.NodeStream.DescendantNodeStream; import net.sourceforge.pmd.lang.ast.internal.AxisStream.AncestorOrSelfStream; import net.sourceforge.pmd.lang.ast.internal.AxisStream.ChildrenStream; import net.sourceforge.pmd.lang.ast.internal.AxisStream.DescendantOrSelfStream; @@ -25,31 +27,12 @@ import net.sourceforge.pmd.lang.ast.internal.AxisStream.DescendantStream; import net.sourceforge.pmd.lang.ast.internal.AxisStream.FilteredAncestorOrSelfStream; import net.sourceforge.pmd.lang.ast.internal.AxisStream.FilteredChildrenStream; import net.sourceforge.pmd.lang.ast.internal.AxisStream.FilteredDescendantStream; +import net.sourceforge.pmd.lang.ast.internal.GreedyNStream.GreedyKnownNStream; public final class StreamImpl { @SuppressWarnings("rawtypes") - private static final NodeStream EMPTY = new IteratorBasedNStream() { - @Override - public Iterator iterator() { - return Collections.emptyIterator(); - } - - @Override - public List toList() { - return Collections.emptyList(); - } - - @Override - public List toList(Function mapper) { - return Collections.emptyList(); - } - - @Override - public String toString() { - return "EmptyStream"; - } - }; + private static final DescendantNodeStream EMPTY = new EmptyNodeStream(); private StreamImpl() { // utility class @@ -59,22 +42,17 @@ public final class StreamImpl { return new SingletonNodeStream<>(node); } - public static NodeStream fromIterable(Iterable iterable) { - return new IteratorBasedNStream() { - @Override - public Iterator iterator() { - return IteratorUtil.filterNotNull(iterable.iterator()); + public static NodeStream fromIterable(Iterable<@Nullable T> iterable) { + if (iterable instanceof Collection) { + Collection<@Nullable T> coll = (Collection) iterable; + if (coll.isEmpty()) { + return empty(); + } else if (coll.size() == 1) { + return NodeStream.of(coll.iterator().next()); } + } - @Override - public Spliterator spliterator() { - Spliterator spliter = iterable.spliterator(); - return Spliterators.spliterator(iterator(), spliter.estimateSize(), - (spliter.characteristics() | Spliterator.NONNULL) - & ~Spliterator.SIZED - & ~Spliterator.SUBSIZED); - } - }; + return fromNonNullList(IteratorUtil.toNonNullList(iterable.iterator())); } public static NodeStream union(Iterable> streams) { @@ -88,28 +66,29 @@ public final class StreamImpl { @SuppressWarnings("unchecked") - public static NodeStream empty() { + public static DescendantNodeStream empty() { return EMPTY; } public static NodeStream children(@NonNull Node node, Class target) { - return node.getNumChildren() == 0 ? empty() : new FilteredChildrenStream<>(node, Filtermap.isInstance(target)); + return sliceChildren(node, Filtermap.isInstance(target), 0, node.getNumChildren()); } public static NodeStream children(@NonNull Node node) { - return node.getNumChildren() == 0 ? empty() : new ChildrenStream(node); + return sliceChildren(node, Filtermap.NODE_IDENTITY, 0, node.getNumChildren()); } - public static NodeStream descendants(@NonNull Node node) { - return node.getNumChildren() == 0 ? empty() : new DescendantStream(node); + public static DescendantNodeStream descendants(@NonNull Node node) { + return node.getNumChildren() == 0 ? empty() : new DescendantStream(node, TreeWalker.DEFAULT); } - public static NodeStream descendants(@NonNull Node node, Class rClass) { - return node.getNumChildren() == 0 ? empty() : new FilteredDescendantStream<>(node, Filtermap.isInstance(rClass)); + public static DescendantNodeStream descendants(@NonNull Node node, Class rClass) { + return node.getNumChildren() == 0 ? empty() + : new FilteredDescendantStream<>(node, TreeWalker.DEFAULT, Filtermap.isInstance(rClass)); } - public static NodeStream descendantsOrSelf(@NonNull Node node) { - return node.getNumChildren() == 0 ? empty() : new DescendantOrSelfStream(node); + public static DescendantNodeStream descendantsOrSelf(@NonNull Node node) { + return node.getNumChildren() == 0 ? empty() : new DescendantOrSelfStream(node, TreeWalker.DEFAULT); } public static NodeStream followingSiblings(@NonNull Node node) { @@ -146,7 +125,12 @@ public final class StreamImpl { : (NodeStream) new ChildrenStream(parent, from, length); return res; } else { - return new FilteredChildrenStream<>(parent, filtermap, from, length); + if (length == 1) { + // eager evaluation, empty or singleton + return NodeStream.of(filtermap.apply(parent.getChild(from))); + } else { + return new FilteredChildrenStream<>(parent, filtermap, from, length); + } } } @@ -178,5 +162,63 @@ public final class StreamImpl { return ancestorsOrSelf(node.getParent(), Filtermap.isInstance(target)); } + static NodeStream fromNonNullList(List<@NonNull T> coll) { + if (coll.isEmpty()) { + return empty(); + } else if (coll.size() == 1) { + return singleton(coll.get(0)); + } + + return new GreedyKnownNStream<>(coll); + } + + + private static class EmptyNodeStream extends IteratorBasedNStream implements DescendantNodeStream { + + @Override + protected NodeStream mapIter(Function, Iterator> fun) { + return StreamImpl.empty(); + } + + @Override + protected @NonNull DescendantNodeStream flatMapDescendants(Function> mapper) { + return StreamImpl.empty(); + } + + @Override + public DescendantNodeStream crossFindBoundaries(boolean cross) { + return StreamImpl.empty(); + } + + @Override + public Iterator iterator() { + return Collections.emptyIterator(); + } + + @Override + public List toList() { + return Collections.emptyList(); + } + + @Override + public List toList(Function mapper) { + return Collections.emptyList(); + } + + @Override + public Spliterator spliterator() { + return Spliterators.emptySpliterator(); + } + + @Override + public NodeStream cached() { + return this; + } + + @Override + public String toString() { + return "EmptyStream"; + } + } } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/TraversalUtils.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/TraversalUtils.java index 2573614afe..b75e4a6cdc 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/TraversalUtils.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/TraversalUtils.java @@ -13,7 +13,7 @@ import org.checkerframework.checker.nullness.qual.NonNull; import net.sourceforge.pmd.lang.ast.Node; -public final class TraversalUtils { +final class TraversalUtils { /* Note that the methods of this class must not use node streams @@ -25,45 +25,7 @@ public final class TraversalUtils { } - public static void findDescendantsOfType(final Node node, Class type, final List results, - final boolean crossFindBoundaries) { - findDescendantsOfType(node, Filtermap.isInstance(type), results, crossFindBoundaries); - } - - static void findDescendantsOfType(final Node node, final Filtermap filtermap, final List results, - final boolean crossFindBoundaries) { - - for (int i = 0; i < node.getNumChildren(); i++) { - final Node child = node.getChild(i); - final T mapped = filtermap.apply(child); - if (mapped != null) { - results.add(mapped); - } - - if (crossFindBoundaries || !child.isFindBoundary()) { - findDescendantsOfType(child, filtermap, results, crossFindBoundaries); - } - } - } - - static T getFirstDescendantOfType(final Node node, final Filtermap filtermap) { - final int n = node.getNumChildren(); - for (int i = 0; i < n; i++) { - Node child = node.getChild(i); - final T t = filtermap.apply(child); - if (t != null) { - return t; - } else if (!child.isFindBoundary()) { - final T n2 = getFirstDescendantOfType(child, filtermap); - if (n2 != null) { - return n2; - } - } - } - return null; - } - - static T getFirstParentOrSelfMatching(final Node node, final Filtermap filter) { + static T getFirstParentOrSelfMatching(final Node node, final Filtermap filter) { Node n = node; while (n != null) { T t = filter.apply(n); @@ -75,7 +37,7 @@ public final class TraversalUtils { return null; } - static T getFirstChildMatching(final Node node, final Filtermap filter, int from, int len) { + static T getFirstChildMatching(final Node node, final Filtermap filter, int from, int len) { for (int i = from, last = from + len; i < last; i++) { Node c = node.getChild(i); T t = filter.apply(c); @@ -86,7 +48,7 @@ public final class TraversalUtils { return null; } - static T getLastChildMatching(final Node node, final Filtermap filter, int from, int len) { + static T getLastChildMatching(final Node node, final Filtermap filter, int from, int len) { for (int i = from + len - 1; i >= from; i--) { Node c = node.getChild(i); T t = filter.apply(c); @@ -97,7 +59,7 @@ public final class TraversalUtils { return null; } - static List findChildrenMatching(final Node node, final Filtermap filter, int from, int len) { + static List findChildrenMatching(final Node node, final Filtermap filter, int from, int len) { List list = new ArrayList<>(); for (int i = from, last = from + len; i < last; i++) { Node c = node.getChild(i); @@ -110,9 +72,6 @@ public final class TraversalUtils { } static int countChildrenMatching(final Node node, final Filtermap filter, int from, int len) { - if (filter == Filtermap.NODE_IDENTITY && from == 0 && len == node.getNumChildren()) { - return node.getNumChildren(); - } int sum = 0; for (int i = from, last = from + len; i < last; i++) { Node c = node.getChild(i); @@ -125,18 +84,13 @@ public final class TraversalUtils { } - static Iterator childrenIterator(Node parent) { - assert parent != null : "parent should not be null"; - return childrenIterator(parent, 0, parent.getNumChildren()); - } - static Iterator childrenIterator(Node parent, final int from, final int to) { assert parent != null : "parent should not be null"; assert from >= 0 && from <= parent.getNumChildren() : "'from' should be a valid index"; assert to >= 0 && to <= parent.getNumChildren() : "'to' should be a valid index"; assert from <= to : "'from' should be lower than 'to'"; - if (parent.getNumChildren() == 0 || to == from) { + if (to == from) { return Collections.emptyIterator(); } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/TreeWalker.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/TreeWalker.java new file mode 100644 index 0000000000..8b038885f1 --- /dev/null +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/TreeWalker.java @@ -0,0 +1,145 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.ast.internal; + + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Deque; +import java.util.Iterator; +import java.util.List; + +import org.checkerframework.checker.nullness.qual.NonNull; + +import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.ast.NodeStream.DescendantNodeStream; + +/** + * Object performing tree traversals. Configuration options can be + * extended later on. + * + * @see DescendantNodeStream + */ +final class TreeWalker { + + private static final TreeWalker CROSS = new TreeWalker(true); + private static final TreeWalker DONT_CROSS = new TreeWalker(false); + + /** + * Default traversal config used by methods like {@link Node#descendants()} + */ + static final TreeWalker DEFAULT = DONT_CROSS; + + private final boolean crossFindBoundaries; + + private TreeWalker(boolean crossFindBoundaries) { + this.crossFindBoundaries = crossFindBoundaries; + } + + public boolean isCrossFindBoundaries() { + return crossFindBoundaries; + } + + /** + * Returns a new walker with the given behaviour for find boundaries. + */ + TreeWalker crossFindBoundaries(boolean cross) { + return cross ? CROSS : DONT_CROSS; + } + + /** + * Configure the given node stream to use this walker. + */ + DescendantNodeStream apply(DescendantNodeStream stream) { + return stream.crossFindBoundaries(crossFindBoundaries); + } + + void findDescendantsMatching(final Node node, + final Filtermap filtermap, + final List results) { + + for (int i = 0; i < node.getNumChildren(); i++) { + final Node child = node.getChild(i); + final T mapped = filtermap.apply(child); + if (mapped != null) { + results.add(mapped); + } + + if (isCrossFindBoundaries() || !child.isFindBoundary()) { + this.findDescendantsMatching(child, filtermap, results); + } + } + } + + T getFirstDescendantOfType(final Node node, final Filtermap filtermap) { + final int n = node.getNumChildren(); + for (int i = 0; i < n; i++) { + Node child = node.getChild(i); + final T t = filtermap.apply(child); + if (t != null) { + return t; + } else if (isCrossFindBoundaries() || !child.isFindBoundary()) { + final T n2 = this.getFirstDescendantOfType(child, filtermap); + if (n2 != null) { + return n2; + } + } + } + return null; + } + + List findDescendantsMatching(final Node node, final Filtermap filtermap) { + List results = new ArrayList<>(); + findDescendantsMatching(node, filtermap, results); + return results; + } + + + Iterator descendantOrSelfIterator(Node top) { + return new DescendantOrSelfIterator(top, this); + } + + Iterator descendantIterator(Node top) { + DescendantOrSelfIterator iter = new DescendantOrSelfIterator(top, this); + iter.next(); // skip self + return iter; + } + + /** Iterates over a node and its descendants. */ + private static class DescendantOrSelfIterator implements Iterator<@NonNull Node> { + + private final Deque queue = new ArrayDeque<>(); + private final TreeWalker config; + + /** Always {@link #hasNext()} after exiting the constructor. */ + DescendantOrSelfIterator(Node top, TreeWalker walker) { + this.config = walker; + queue.addFirst(top); + } + + @Override + public boolean hasNext() { + return !queue.isEmpty(); + } + + + @Override + public @NonNull Node next() { + Node node = queue.removeFirst(); + enqueueChildren(node); + return node; + } + + + private void enqueueChildren(Node n) { + if (config.isCrossFindBoundaries() || !n.isFindBoundary()) { + for (int i = n.getNumChildren() - 1; i >= 0; i--) { + queue.addFirst(n.getChild(i)); + } + } + } + } + +} diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/StringUtil.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/StringUtil.java index 3515251b8e..f1afd60351 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/StringUtil.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/StringUtil.java @@ -30,6 +30,65 @@ public final class StringUtil { private StringUtil() { } + + /** + * Returns the (1-based) line number of the character at the given index. + * Line terminators (\r, \n) are assumed to be on the line they *end* + * and not on the following line. The method also accepts that the given + * offset be the length of the string (in which case there's no targeted character), + * to get the line number of a character that would be inserted at + * the end of the string. + * + *

    +     *
    +     *     lineNumberAt("a\nb", 0)  = 1
    +     *     lineNumberAt("a\nb", 1)  = 1
    +     *     lineNumberAt("a\nb", 2)  = 2
    +     *     lineNumberAt("a\nb", 3)  = 2  // charAt(3) doesn't exist though
    +     *     lineNumberAt("a\nb", 4)  = -1
    +     *
    +     *     lineNumberAt("", 0) = 1
    +     *     lineNumberAt("", _) = -1
    +     *
    +     * 
    + * + * @param charSeq Char sequence + * @param offsetInclusive Offset in the sequence of the targeted character. + * May be the length of the sequence. + * @return -1 if the offset is not in {@code [0, length]}, otherwise + * the line number + */ + public static int lineNumberAt(CharSequence charSeq, int offsetInclusive) { + int len = charSeq.length(); + + if (offsetInclusive > len || offsetInclusive < 0) { + return -1; + } + + int l = 1; + for (int curOffset = 0; curOffset < offsetInclusive; curOffset++) { + // if we end up outside the string, then the line is undefined + if (curOffset >= len) { + return -1; + } + + char c = charSeq.charAt(curOffset); + if (c == '\n') { + l++; + } else if (c == '\r') { + if (curOffset + 1 < len && charSeq.charAt(curOffset + 1) == '\n') { + if (curOffset == offsetInclusive - 1) { + // the CR is assumed to be on the same line as the LF + return l; + } + curOffset++; // SUPPRESS CHECKSTYLE jump to after the \n + } + l++; + } + } + return l; + } + /** * Returns the (1-based) column number of the character at the given index. * Line terminators are by convention taken to be part of the line they end, @@ -699,4 +758,51 @@ public final class StringUtil { return sb.toString(); } + /** + * Replaces unprintable characters by their escaped (or unicode escaped) + * equivalents in the given string + */ + public static String escapeJava(String str) { + StringBuilder retval = new StringBuilder(); + for (int i = 0; i < str.length(); i++) { + final char ch = str.charAt(i); + switch (ch) { + case 0: + break; + case '\b': + retval.append("\\b"); + break; + case '\t': + retval.append("\\t"); + break; + case '\n': + retval.append("\\n"); + break; + case '\f': + retval.append("\\f"); + break; + case '\r': + retval.append("\\r"); + break; + case '\"': + retval.append("\\\""); + break; + case '\'': + retval.append("\\'"); + break; + case '\\': + retval.append("\\\\"); + break; + default: + if (ch < 0x20 || ch > 0x7e) { + String s = "0000" + Integer.toString(ch, 16); + retval.append("\\u").append(s.substring(s.length() - 4)); + } else { + retval.append(ch); + } + break; + } + } + return retval.toString(); + } } diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/AbstractNodeTransversalTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/BoundaryTraversalTest.java similarity index 84% rename from pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/AbstractNodeTransversalTest.java rename to pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/BoundaryTraversalTest.java index 50dbf99da1..f140e3ba32 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/AbstractNodeTransversalTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/BoundaryTraversalTest.java @@ -14,9 +14,9 @@ import org.junit.Before; import org.junit.Test; /** - * Unit test for {@link AbstractNode} tree transversal methods + * Unit test for {@link AbstractNode} tree traversal methods */ -public class AbstractNodeTransversalTest { +public class BoundaryTraversalTest { private int id; private Node rootNode; @@ -44,7 +44,7 @@ public class AbstractNodeTransversalTest { public void testBoundaryIsHonored() { addChild(rootNode, addChild(newDummyNode(true), newDummyNode(false))); - List descendantsOfType = rootNode.findDescendantsOfType(DummyNode.class); + List descendantsOfType = rootNode.descendants(DummyNode.class).toList(); assertEquals(1, descendantsOfType.size()); assertTrue(descendantsOfType.get(0).isFindBoundary()); } @@ -53,7 +53,7 @@ public class AbstractNodeTransversalTest { public void testSearchFromBoundary() { addChild(rootNode, addChild(newDummyNode(true), newDummyNode(false))); - List descendantsOfType = rootNode.findDescendantsOfType(DummyNode.class).get(0).findDescendantsOfType(DummyNode.class); + List descendantsOfType = rootNode.descendants(DummyNode.class).first().descendants(DummyNode.class).toList(); assertEquals(1, descendantsOfType.size()); assertFalse(descendantsOfType.get(0).isFindBoundary()); } diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/internal/NodeStreamBlanketTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/internal/NodeStreamBlanketTest.java index bcef611273..557cfdec71 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/internal/NodeStreamBlanketTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/internal/NodeStreamBlanketTest.java @@ -5,6 +5,7 @@ package net.sourceforge.pmd.lang.ast.internal; import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertSame; import static net.sourceforge.pmd.lang.ast.DummyTreeUtil.node; import static net.sourceforge.pmd.lang.ast.DummyTreeUtil.tree; @@ -18,7 +19,9 @@ import java.util.stream.Stream; import org.junit.Assert; import org.junit.Assume; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @@ -60,6 +63,10 @@ public class NodeStreamBlanketTest { ) ) ); + + @Rule + public ExpectedException expect = ExpectedException.none(); + private final NodeStream stream; public NodeStreamBlanketTest(NodeStream stream) { @@ -136,6 +143,31 @@ public class NodeStreamBlanketTest { ); } + @Test + public void testGet() { + for (int i = 0; i < 100; i++) { + assertSame("stream.get(i) == stream.drop(i).first()", stream.get(i), stream.drop(i).first()); + } + } + + @Test + public void testGetNegative() { + expect.expect(IllegalArgumentException.class); + stream.get(-1); + } + + @Test + public void testDropNegative() { + expect.expect(IllegalArgumentException.class); + stream.drop(-1); + } + + @Test + public void testTakeNegative() { + expect.expect(IllegalArgumentException.class); + stream.take(-1); + } + @Test public void testEmpty() { assertEquivalence( diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/internal/NodeStreamTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/internal/NodeStreamTest.java index 27824c59c8..f3f2b19d49 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/internal/NodeStreamTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/internal/NodeStreamTest.java @@ -91,6 +91,16 @@ public class NodeStreamTest { } + @Test + public void testChildrenEagerEvaluation() { + NodeStream children = tree1.children(); + assertEquals(AxisStream.ChildrenStream.class, children.getClass()); + NodeStream children1 = children.children(); + assertEquals(GreedyNStream.GreedyKnownNStream.class, children1.getClass()); + assertEquals(SingletonNodeStream.class, children1.filter(it -> it.getImage().endsWith("1")).getClass()); + } + + @Test public void testDescendantStream() { assertThat(pathsOf(tree1.descendants()), contains("0", "00", "01", "010", "011", "012", "013", "1")); @@ -217,13 +227,14 @@ public class NodeStreamTest { MutableInt numEvals = new MutableInt(); NodeStream stream = - hook(numEvals::increment, tree1.descendants()) + NodeStream .forkJoin( + hook(numEvals::increment, tree1.descendants()), n -> NodeStream.of(n).filter(m -> m.hasImageEqualTo("0")), n -> NodeStream.of(n).filter(m -> m.hasImageEqualTo("1")) ); - assertThat(numEvals.getValue(), equalTo(0)); // not evaluated yet + // assertThat(numEvals.getValue(), equalTo(0)); // not evaluated yet assertThat(stream.count(), equalTo(2)); @@ -250,7 +261,7 @@ public class NodeStreamTest { .filter(n -> true) .peek(n -> downstreamEvals.increment()); - assertThat(upstreamEvals.getValue(), equalTo(0)); // not evaluated yet + // assertThat(upstreamEvals.getValue(), equalTo(0)); // not evaluated yet assertThat(stream.count(), equalTo(4)); @@ -260,7 +271,7 @@ public class NodeStreamTest { assertThat(stream.count(), equalTo(4)); assertThat(upstreamEvals.getValue(), equalTo(4)); // upstream was not reevaluated - assertThat(downstreamEvals.getValue(), equalTo(8)); // downstream has been reevaluated + assertThat(downstreamEvals.getValue(), equalTo(4)); // downstream was not reevaluated } @@ -311,7 +322,7 @@ public class NodeStreamTest { unionStream.prepend(tree2.descendantsOrSelf()); unionStream.flatMap(Node::descendantsOrSelf); unionStream.iterator(); - unionStream.cached(); + // unionStream.cached(); unionStream.descendants(); unionStream.ancestors(); unionStream.followingSiblings(); diff --git a/pmd-cpp/src/main/java/net/sourceforge/pmd/lang/cpp/CppCharStream.java b/pmd-cpp/src/main/java/net/sourceforge/pmd/lang/cpp/CppCharStream.java index de9332cc12..4718198058 100644 --- a/pmd-cpp/src/main/java/net/sourceforge/pmd/lang/cpp/CppCharStream.java +++ b/pmd-cpp/src/main/java/net/sourceforge/pmd/lang/cpp/CppCharStream.java @@ -8,8 +8,12 @@ import java.io.IOException; import java.io.Reader; import java.util.regex.Pattern; -import net.sourceforge.pmd.annotation.InternalApi; -import net.sourceforge.pmd.lang.ast.SimpleCharStream; +import org.checkerframework.checker.nullness.qual.Nullable; + +import net.sourceforge.pmd.lang.ast.impl.javacc.CharStreamFactory; +import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccTokenDocument; +import net.sourceforge.pmd.lang.ast.impl.javacc.SimpleCharStream; +import net.sourceforge.pmd.lang.cpp.ast.CppParserConstants; /** * A SimpleCharStream, that supports the continuation of lines via backslash+newline, @@ -17,19 +21,18 @@ import net.sourceforge.pmd.lang.ast.SimpleCharStream; * * @author Andreas Dangel */ -@Deprecated -@InternalApi -public class CppCharStream extends SimpleCharStream { +class CppCharStream extends SimpleCharStream { private static final Pattern CONTINUATION = Pattern.compile("\\\\\\n|\\\\\\r\\n"); private static final char BACKSLASH = '\\'; private static final char NEWLINE = '\n'; private static final char CARRIAGE_RETURN = '\r'; - public CppCharStream(Reader dstream) { - super(dstream); + CppCharStream(JavaccTokenDocument document) { + super(document); } + @Override public char readChar() throws IOException { char c = super.readChar(); @@ -62,4 +65,17 @@ public class CppCharStream extends SimpleCharStream { String image = super.GetImage(); return CONTINUATION.matcher(image).replaceAll(""); } + + public static CppCharStream newCppCharStream(Reader dstream) { + String source = CharStreamFactory.toString(dstream); + JavaccTokenDocument document = new JavaccTokenDocument(source) { + @Override + protected @Nullable String describeKindImpl(int kind) { + return 0 <= kind && kind < CppParserConstants.tokenImage.length + ? CppParserConstants.tokenImage[kind] + : null; + } + }; + return new CppCharStream(document); + } } diff --git a/pmd-cpp/src/main/java/net/sourceforge/pmd/lang/cpp/CppTokenManager.java b/pmd-cpp/src/main/java/net/sourceforge/pmd/lang/cpp/CppTokenManager.java index c60c6e1d96..e1e6440573 100644 --- a/pmd-cpp/src/main/java/net/sourceforge/pmd/lang/cpp/CppTokenManager.java +++ b/pmd-cpp/src/main/java/net/sourceforge/pmd/lang/cpp/CppTokenManager.java @@ -22,7 +22,7 @@ public class CppTokenManager implements TokenManager { * the source code */ public CppTokenManager(Reader source) { - tokenManager = new CppParserTokenManager(new CppCharStream(source)); + tokenManager = new CppParserTokenManager(CppCharStream.newCppCharStream(source)); } @Override diff --git a/pmd-cpp/src/test/java/net/sourceforge/pmd/lang/cpp/CppCharStreamTest.java b/pmd-cpp/src/test/java/net/sourceforge/pmd/lang/cpp/CppCharStreamTest.java index dcfc1c04f2..82fffbc93a 100644 --- a/pmd-cpp/src/test/java/net/sourceforge/pmd/lang/cpp/CppCharStreamTest.java +++ b/pmd-cpp/src/test/java/net/sourceforge/pmd/lang/cpp/CppCharStreamTest.java @@ -15,19 +15,19 @@ public class CppCharStreamTest { @Test public void testContinuationUnix() throws IOException { - CppCharStream stream = new CppCharStream(new StringReader("a\\\nb")); + CppCharStream stream = CppCharStream.newCppCharStream(new StringReader("a\\\nb")); assertStream(stream, "ab"); } @Test public void testContinuationWindows() throws IOException { - CppCharStream stream = new CppCharStream(new StringReader("a\\\r\nb")); + CppCharStream stream = CppCharStream.newCppCharStream(new StringReader("a\\\r\nb")); assertStream(stream, "ab"); } @Test public void testBackup() throws IOException { - CppCharStream stream = new CppCharStream(new StringReader("a\\b\\\rc")); + CppCharStream stream = CppCharStream.newCppCharStream(new StringReader("a\\b\\\rc")); assertStream(stream, "a\\b\\\rc"); } diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index e0a41c03be..520e435232 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -227,17 +227,12 @@ */ options { - JAVA_UNICODE_ESCAPE = true; + UNICODE_INPUT=true; CACHE_TOKENS = true; STATIC = false; - USER_CHAR_STREAM = true; - JDK_VERSION = "1.5"; - TOKEN_FACTORY = "JavaTokenFactory"; MULTI = true; VISITOR = true; - NODE_USES_PARSER = true; - TRACK_TOKENS = true; NODE_PACKAGE="net.sourceforge.pmd.lang.java.ast"; //DEBUG_PARSER = true; @@ -245,7 +240,7 @@ options { //DEBUG_TOKEN_MANAGER = true; } -PARSER_BEGIN(JavaParser) +PARSER_BEGIN(JavaParserImpl) package net.sourceforge.pmd.lang.java.ast; import java.util.ArrayList; import java.util.List; @@ -256,7 +251,7 @@ import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken; import net.sourceforge.pmd.lang.ast.TokenMgrError; import net.sourceforge.pmd.lang.ast.Node; -class JavaParser { +class JavaParserImpl { private int jdkVersion = 0; private boolean preview = false; @@ -344,22 +339,22 @@ class JavaParser { } private void forceExprContext() { - Node top = jjtree.peekNode(); + AbstractJavaNode top = jjtree.peekNode(); if (top instanceof ASTAmbiguousName) { // see doc on the method - Node replacement = ((ASTAmbiguousName) top).forceExprContext(); + AbstractJavaNode replacement = (AbstractJavaNode) ((ASTAmbiguousName) top).forceExprContext(); jjtree.popNode(); jjtree.pushNode(replacement); } } private void forceTypeContext() { - Node top = jjtree.peekNode(); + AbstractJavaNode top = jjtree.peekNode(); if (top instanceof ASTAmbiguousName) { // see doc on the method - Node replacement = ((ASTAmbiguousName) top).forceTypeContext(); + AbstractJavaNode replacement = ((ASTAmbiguousName) top).forceTypeContext(); jjtree.popNode(); jjtree.pushNode(replacement); } @@ -383,7 +378,7 @@ class JavaParser { private boolean inExplicitConstructorInvoc = false; } -PARSER_END(JavaParser) +PARSER_END(JavaParserImpl) TOKEN_MGR_DECLS : { @@ -810,7 +805,6 @@ ASTCompilationUnit CompilationUnit() : { jjtThis.setComments(token_source.comments); - jjtThis.setTokenDocument(((net.sourceforge.pmd.lang.ast.JavaCharStream) token_source.input_stream).getTokenDocument()); return jjtThis; } } @@ -2275,14 +2269,14 @@ void AssertStatement() : void RUNSIGNEDSHIFT() #void: {} { - LOOKAHEAD({ JavaTokenFactory.getRealKind(getToken(1)) == RUNSIGNEDSHIFT}) + LOOKAHEAD({ JavaTokenDocument.getRealKind(getToken(1)) == RUNSIGNEDSHIFT}) ">" ">" ">" } void RSIGNEDSHIFT() #void: {} { - LOOKAHEAD({ JavaTokenFactory.getRealKind(getToken(1)) == RSIGNEDSHIFT}) + LOOKAHEAD({ JavaTokenDocument.getRealKind(getToken(1)) == RSIGNEDSHIFT}) ">" ">" } diff --git a/pmd-java/pom.xml b/pmd-java/pom.xml index c5cb04f6c5..708e6c874a 100644 --- a/pmd-java/pom.xml +++ b/pmd-java/pom.xml @@ -58,17 +58,18 @@ generate-sources generate-sources - - - - - - - - run + + + + + + + + + diff --git a/pmd-java/src/main/ant/alljavacc.xml b/pmd-java/src/main/ant/alljavacc.xml deleted file mode 100644 index 0c1970670f..0000000000 --- a/pmd-java/src/main/ant/alljavacc.xml +++ /dev/null @@ -1,249 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Note that when the total number of children is definitely known, you - * can use "definite nodes", ie write the expected number of children (including - * the ones to the left) in the JJTree annotation (eg {@code #AdditiveExpression(2)}). - * So this is only useful when the number of children of the current node is not certain. - * - *

    This method does not affect the stack unless the current jjtThis is - * closed in the future. - */ - public void extendLeft() { - mk--; - } - - /** - * Peek the nth node from the top of the stack. - * peekNode(0) == peekNode() - */ - public Node peekNode(int n) { - return nodes.get(nodes.size() - n - 1); - } - - public boolean isInjectionPending() { - return numPendingInjection > 0; - } - - /** If non-zero, then the top "n" nodes of the stack will be injected as the first children of the next - * node to be opened. This is not very flexible, but it's enough. The grammar needs to take - * care of the order in which nodes are opened in a few places, in most cases this just means using - * eg A() B() #N(2) instead of (A() B()) #N, so as not to open N before A. - */ - private int numPendingInjection; - - public void injectRight(int n) { - numPendingInjection = n; - } - - /* Pushes a node on to the stack. */]]> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - public class - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/cpd/JavaTokenizer.java b/pmd-java/src/main/java/net/sourceforge/pmd/cpd/JavaTokenizer.java index c8a60a86fa..7361a5d4cc 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/cpd/JavaTokenizer.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/cpd/JavaTokenizer.java @@ -16,8 +16,8 @@ import net.sourceforge.pmd.cpd.token.TokenFilter; import net.sourceforge.pmd.lang.TokenManager; import net.sourceforge.pmd.lang.ast.GenericToken; import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken; -import net.sourceforge.pmd.lang.java.JavaTokenManager; -import net.sourceforge.pmd.lang.java.ast.JavaParserConstants; +import net.sourceforge.pmd.lang.java.ast.JavaTokenKinds; +import net.sourceforge.pmd.lang.java.ast.JavaTokenManager; public class JavaTokenizer extends JavaCCTokenizer { @@ -60,13 +60,13 @@ public class JavaTokenizer extends JavaCCTokenizer { constructorDetector.restoreConstructorToken(tokenEntries, javaToken); - if (ignoreLiterals && (javaToken.kind == JavaParserConstants.STRING_LITERAL - || javaToken.kind == JavaParserConstants.CHARACTER_LITERAL - || javaToken.kind == JavaParserConstants.DECIMAL_LITERAL - || javaToken.kind == JavaParserConstants.FLOATING_POINT_LITERAL)) { + if (ignoreLiterals && (javaToken.kind == JavaTokenKinds.STRING_LITERAL + || javaToken.kind == JavaTokenKinds.CHARACTER_LITERAL + || javaToken.kind == JavaTokenKinds.DECIMAL_LITERAL + || javaToken.kind == JavaTokenKinds.FLOATING_POINT_LITERAL)) { image = String.valueOf(javaToken.kind); } - if (ignoreIdentifiers && javaToken.kind == JavaParserConstants.IDENTIFIER) { + if (ignoreIdentifiers && javaToken.kind == JavaTokenKinds.IDENTIFIER) { image = String.valueOf(javaToken.kind); } @@ -125,15 +125,15 @@ public class JavaTokenizer extends JavaCCTokenizer { } private void skipPackageAndImport(final JavaccToken currentToken) { - if (currentToken.kind == JavaParserConstants.PACKAGE || currentToken.kind == JavaParserConstants.IMPORT) { + if (currentToken.kind == JavaTokenKinds.PACKAGE || currentToken.kind == JavaTokenKinds.IMPORT) { discardingKeywords = true; - } else if (discardingKeywords && currentToken.kind == JavaParserConstants.SEMICOLON) { + } else if (discardingKeywords && currentToken.kind == JavaTokenKinds.SEMICOLON) { discardingKeywords = false; } } private void skipSemicolon(final JavaccToken currentToken) { - if (currentToken.kind == JavaParserConstants.SEMICOLON) { + if (currentToken.kind == JavaTokenKinds.SEMICOLON) { discardingSemicolon = true; } else if (discardingSemicolon) { discardingSemicolon = false; @@ -143,10 +143,10 @@ public class JavaTokenizer extends JavaCCTokenizer { private void skipAnnotationSuppression(final JavaccToken currentToken) { // if processing an annotation, look for a CPD-START or CPD-END if (isAnnotation) { - if (!discardingSuppressing && currentToken.kind == JavaParserConstants.STRING_LITERAL + if (!discardingSuppressing && currentToken.kind == JavaTokenKinds.STRING_LITERAL && CPD_START.equals(currentToken.getImage())) { discardingSuppressing = true; - } else if (discardingSuppressing && currentToken.kind == JavaParserConstants.STRING_LITERAL + } else if (discardingSuppressing && currentToken.kind == JavaTokenKinds.STRING_LITERAL && CPD_END.equals(currentToken.getImage())) { discardingSuppressing = false; } @@ -173,19 +173,19 @@ public class JavaTokenizer extends JavaCCTokenizer { nextTokenEndsAnnotation = false; } if (isAnnotation) { - if (currentToken.kind == JavaParserConstants.LPAREN) { + if (currentToken.kind == JavaTokenKinds.LPAREN) { annotationStack++; - } else if (currentToken.kind == JavaParserConstants.RPAREN) { + } else if (currentToken.kind == JavaTokenKinds.RPAREN) { annotationStack--; if (annotationStack == 0) { nextTokenEndsAnnotation = true; } - } else if (annotationStack == 0 && currentToken.kind != JavaParserConstants.IDENTIFIER - && currentToken.kind != JavaParserConstants.LPAREN) { + } else if (annotationStack == 0 && currentToken.kind != JavaTokenKinds.IDENTIFIER + && currentToken.kind != JavaTokenKinds.LPAREN) { isAnnotation = false; } } - if (currentToken.kind == JavaParserConstants.AT) { + if (currentToken.kind == JavaTokenKinds.AT) { isAnnotation = true; } } @@ -218,7 +218,7 @@ public class JavaTokenizer extends JavaCCTokenizer { } switch (currentToken.kind) { - case JavaParserConstants.IDENTIFIER: + case JavaTokenKinds.IDENTIFIER: if ("enum".equals(currentToken.getImage())) { // If declaring an enum, add a new block nesting level at // which constructors may exist @@ -232,17 +232,17 @@ public class JavaTokenizer extends JavaCCTokenizer { prevIdentifier = currentToken.getImage(); break; - case JavaParserConstants.CLASS: + case JavaTokenKinds.CLASS: // If declaring a class, add a new block nesting level at which // constructors may exist pushTypeDeclaration(); break; - case JavaParserConstants.LBRACE: + case JavaTokenKinds.LBRACE: currentNestingLevel++; break; - case JavaParserConstants.RBRACE: + case JavaTokenKinds.RBRACE: // Discard completed blocks if (!classMembersIndentations.isEmpty() && classMembersIndentations.peek().indentationLevel == currentNestingLevel) { @@ -277,7 +277,7 @@ public class JavaTokenizer extends JavaCCTokenizer { return; } - if (currentToken.kind == JavaParserConstants.LPAREN) { + if (currentToken.kind == JavaTokenKinds.LPAREN) { // was the previous token a constructor? If so, restore the // identifier if (!classMembersIndentations.isEmpty() diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAdditiveExpression.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAdditiveExpression.java index 24eed907d5..3ca70962b9 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAdditiveExpression.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAdditiveExpression.java @@ -41,10 +41,6 @@ public final class ASTAdditiveExpression extends AbstractJavaExpr { super(id); } - ASTAdditiveExpression(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAllocationExpression.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAllocationExpression.java index 17f985ec35..e0d38fda0e 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAllocationExpression.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAllocationExpression.java @@ -19,10 +19,6 @@ public class ASTAllocationExpression extends AbstractJavaTypeNode implements Jav super(id); } - ASTAllocationExpression(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAmbiguousName.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAmbiguousName.java index a875c6c0d9..89da391c7b 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAmbiguousName.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAmbiguousName.java @@ -9,8 +9,6 @@ import java.util.List; import java.util.function.BiFunction; import java.util.function.Function; - - /** * An ambiguous name occurring in any context. Without a disambiguation * pass that taking care of obscuring rules and the current declarations @@ -54,8 +52,8 @@ import java.util.function.Function; * *

    Another mechanism is {@link #forceExprContext()} and {@link #forceTypeContext()}, * which are called by the parser to promote an ambiguous name to an - * expression or a type when exiting from the {@link JavaParser#PrimaryExpression()} - * production or {@link JavaParser#ClassOrInterfaceType()}. + * expression or a type when exiting from the {@link JavaParserImpl#PrimaryExpression()} + * production or {@link JavaParserImpl#ClassOrInterfaceType()}. * *

    Those two mechanisms perform the first classification step, the * one that only depends on the syntactic context and not on semantic @@ -70,16 +68,11 @@ public final class ASTAmbiguousName extends AbstractJavaExpr implements ASTRefer ASTAmbiguousName(String id) { - super(JavaParserTreeConstants.JJTAMBIGUOUSNAME); + super(JavaParserImplTreeConstants.JJTAMBIGUOUSNAME); setImage(id); } - ASTAmbiguousName(JavaParser p, int id) { - super(p, id); - } - - public String getName() { return getImage(); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAndExpression.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAndExpression.java index f6817c076b..910a811b13 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAndExpression.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAndExpression.java @@ -28,10 +28,6 @@ public final class ASTAndExpression extends AbstractJavaExpr implements ASTExpre super(id); } - ASTAndExpression(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationMethodDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationMethodDeclaration.java index 663e64c570..acc578eb82 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationMethodDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationMethodDeclaration.java @@ -18,11 +18,6 @@ public final class ASTAnnotationMethodDeclaration extends AbstractJavaAccessNode super(id); } - ASTAnnotationMethodDeclaration(JavaParser p, int id) { - super(p, id); - } - - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationTypeBody.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationTypeBody.java index ff5020c153..71f3e2942b 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationTypeBody.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationTypeBody.java @@ -10,10 +10,6 @@ public final class ASTAnnotationTypeBody extends AbstractJavaNode implements AST super(id); } - ASTAnnotationTypeBody(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationTypeDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationTypeDeclaration.java index a6825079cb..c04eedb6ce 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationTypeDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationTypeDeclaration.java @@ -35,10 +35,6 @@ public final class ASTAnnotationTypeDeclaration extends AbstractAnyTypeDeclarati super(id); } - ASTAnnotationTypeDeclaration(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationTypeMemberDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationTypeMemberDeclaration.java index 84ff11c27a..91c3ed00e0 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationTypeMemberDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationTypeMemberDeclaration.java @@ -10,10 +10,6 @@ public final class ASTAnnotationTypeMemberDeclaration extends AbstractTypeBodyDe super(id); } - ASTAnnotationTypeMemberDeclaration(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnonymousClassDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnonymousClassDeclaration.java index 7adc698138..6bd2867cac 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnonymousClassDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnonymousClassDeclaration.java @@ -28,11 +28,6 @@ public final class ASTAnonymousClassDeclaration extends AbstractJavaTypeNode imp } - ASTAnonymousClassDeclaration(JavaParser p, int id) { - super(p, id); - } - - @Override public boolean isFindBoundary() { return true; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeDeclaration.java index e320b6e899..92748c0528 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeDeclaration.java @@ -87,7 +87,18 @@ public interface ASTAnyTypeDeclaration extends TypeNode, JavaQualifiableNode, Ac /** * Returns true if this type declaration is nested inside an interface, class or annotation. */ - boolean isNested(); + default boolean isNested() { + return getParent() instanceof ASTClassOrInterfaceBodyDeclaration + || getParent() instanceof ASTAnnotationTypeMemberDeclaration; + } + + + /** + * Returns true if this is a local class declaration. + */ + default boolean isLocal() { + return getParent() instanceof ASTLocalClassStatement; + } /** diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArgumentList.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArgumentList.java index 0f764d78e1..963e000c7b 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArgumentList.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArgumentList.java @@ -23,10 +23,6 @@ public final class ASTArgumentList extends AbstractJavaNode implements Iterable< super(id); } - ASTArgumentList(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArguments.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArguments.java index ffccb42cac..aff97d15f7 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArguments.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArguments.java @@ -14,10 +14,6 @@ public class ASTArguments extends AbstractJavaNode { super(id); } - ASTArguments(JavaParser p, int id) { - super(p, id); - } - public int getArgumentCount() { if (this.getNumChildren() == 0) { return 0; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayAccess.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayAccess.java index fefe216f3e..745ced0194 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayAccess.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayAccess.java @@ -23,11 +23,6 @@ public final class ASTArrayAccess extends AbstractJavaExpr implements ASTAssigna } - ASTArrayAccess(JavaParser p, int id) { - super(p, id); - } - - /** * Returns the expression to the left of the "[". * This can never be a {@linkplain ASTTypeExpression type}, diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayAllocation.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayAllocation.java index 5ad735a03f..deb09bdd64 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayAllocation.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayAllocation.java @@ -25,11 +25,6 @@ public final class ASTArrayAllocation extends AbstractJavaExpr implements ASTPri } - ASTArrayAllocation(JavaParser p, int id) { - super(p, id); - } - - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayDimExpr.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayDimExpr.java index 15c22bff23..9e053e1223 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayDimExpr.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayDimExpr.java @@ -23,10 +23,6 @@ public final class ASTArrayDimExpr extends ASTArrayTypeDim implements Annotatabl super(id); } - ASTArrayDimExpr(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayDimensions.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayDimensions.java index 26d2a8326c..a7b73e913a 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayDimensions.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayDimensions.java @@ -39,11 +39,6 @@ public final class ASTArrayDimensions extends AbstractJavaTypeNode implements It } - ASTArrayDimensions(JavaParser p, int id) { - super(p, id); - } - - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayDimsAndInits.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayDimsAndInits.java index 021d84f338..3ebc2f85c7 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayDimsAndInits.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayDimsAndInits.java @@ -16,10 +16,6 @@ public class ASTArrayDimsAndInits extends AbstractJavaNode { super(id); } - ASTArrayDimsAndInits(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayInitializer.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayInitializer.java index b79996f73a..55ed8c63ed 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayInitializer.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayInitializer.java @@ -28,10 +28,6 @@ public final class ASTArrayInitializer extends AbstractJavaExpr implements ASTEx super(id); } - public ASTArrayInitializer(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayType.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayType.java index db7d06976f..dc3ec04016 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayType.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayType.java @@ -22,10 +22,6 @@ public final class ASTArrayType extends AbstractJavaTypeNode implements ASTRefer } - ASTArrayType(JavaParser p, int id) { - super(p, id); - } - @Override public List getDeclaredAnnotations() { // an array type's annotations are on its dimensions diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayTypeDim.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayTypeDim.java index b2221d8f06..9260ad038a 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayTypeDim.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayTypeDim.java @@ -25,10 +25,6 @@ public class ASTArrayTypeDim extends AbstractJavaNode implements Annotatable { super(id); } - ASTArrayTypeDim(JavaParser p, int id) { - super(p, id); - } - /** * Returns true if this is a varargs dimension. Varargs parameters * are represented as an array type whose last dimension has this diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAssertStatement.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAssertStatement.java index 8b5632317a..32cdcbc669 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAssertStatement.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAssertStatement.java @@ -19,10 +19,6 @@ public final class ASTAssertStatement extends AbstractStatement { super(id); } - ASTAssertStatement(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAssignmentExpression.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAssignmentExpression.java index db7b31a0e5..9d39bd98eb 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAssignmentExpression.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAssignmentExpression.java @@ -23,11 +23,6 @@ public final class ASTAssignmentExpression extends AbstractJavaExpr implements I } - ASTAssignmentExpression(JavaParser p, int id) { - super(p, id); - } - - void setOp(AssignmentOp op) { this.operator = op; } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAssignmentOperator.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAssignmentOperator.java index 4d3be8cbfb..9031c3b5bf 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAssignmentOperator.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAssignmentOperator.java @@ -23,10 +23,6 @@ public class ASTAssignmentOperator extends AbstractJavaNode { super(id); } - ASTAssignmentOperator(JavaParser p, int id) { - super(p, id); - } - public boolean isCompound() { return this.isCompound; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTBlock.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTBlock.java index 54f29bc001..20106d123a 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTBlock.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTBlock.java @@ -24,10 +24,6 @@ public final class ASTBlock extends AbstractStatement implements Iterable getComments() { return comments; } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConditionalAndExpression.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConditionalAndExpression.java index eff539433b..0cd69e82aa 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConditionalAndExpression.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConditionalAndExpression.java @@ -27,11 +27,6 @@ public final class ASTConditionalAndExpression extends AbstractJavaExpr implemen } - ASTConditionalAndExpression(JavaParser p, int id) { - super(p, id); - } - - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConditionalExpression.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConditionalExpression.java index ec9cf50134..3aaf5581e3 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConditionalExpression.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConditionalExpression.java @@ -20,10 +20,6 @@ public final class ASTConditionalExpression extends AbstractJavaExpr implements super(id); } - ASTConditionalExpression(JavaParser p, int id) { - super(p, id); - } - /** * Returns the node that represents the guard of this conditional. diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConditionalOrExpression.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConditionalOrExpression.java index 7bbcf8504f..27b934e026 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConditionalOrExpression.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConditionalOrExpression.java @@ -26,10 +26,6 @@ public final class ASTConditionalOrExpression extends AbstractJavaExpr implement super(id); } - ASTConditionalOrExpression(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConstructorCall.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConstructorCall.java index 87e7dfca1d..246d31fcce 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConstructorCall.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConstructorCall.java @@ -30,11 +30,6 @@ public final class ASTConstructorCall extends AbstractJavaExpr implements ASTPri } - ASTConstructorCall(JavaParser p, int id) { - super(p, id); - } - - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConstructorDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConstructorDeclaration.java index e5f6a9bc7b..52ae5a797b 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConstructorDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConstructorDeclaration.java @@ -32,10 +32,6 @@ public final class ASTConstructorDeclaration extends AbstractMethodOrConstructor super(id); } - ASTConstructorDeclaration(JavaParser p, int id) { - super(p, id); - } - @Override public String getName() { return getImage(); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTContinueStatement.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTContinueStatement.java index 6b7605ba25..ae087d8289 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTContinueStatement.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTContinueStatement.java @@ -20,10 +20,6 @@ public final class ASTContinueStatement extends AbstractStatement { super(id); } - ASTContinueStatement(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTDefaultValue.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTDefaultValue.java index 47a8c7d65c..87508bc2c5 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTDefaultValue.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTDefaultValue.java @@ -19,10 +19,6 @@ public final class ASTDefaultValue extends AbstractJavaNode { super(id); } - ASTDefaultValue(JavaParser p, int id) { - super(p, id); - } - /** * Returns the constant value nested in this node. */ diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTDoStatement.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTDoStatement.java index 1ddb4b8476..3317f45bb4 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTDoStatement.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTDoStatement.java @@ -20,10 +20,6 @@ public final class ASTDoStatement extends AbstractStatement { super(id); } - ASTDoStatement(JavaParser p, int id) { - super(p, id); - } - /** * Returns the node that represents the guard of this loop. diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEmptyDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEmptyDeclaration.java index 53e04cbd48..d719d32f86 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEmptyDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEmptyDeclaration.java @@ -20,10 +20,6 @@ public final class ASTEmptyDeclaration extends AbstractJavaNode { super(id); } - ASTEmptyDeclaration(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEmptyStatement.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEmptyStatement.java index fff2741ccf..2324289ee0 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEmptyStatement.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEmptyStatement.java @@ -19,10 +19,6 @@ public final class ASTEmptyStatement extends AbstractStatement { super(id); } - ASTEmptyStatement(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumBody.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumBody.java index 2df01145d5..a7e41788c8 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumBody.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumBody.java @@ -25,10 +25,6 @@ public final class ASTEnumBody extends AbstractJavaNode implements ASTTypeBody { super(id); } - ASTEnumBody(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumConstant.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumConstant.java index 6a304c131b..36c3d63fd6 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumConstant.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumConstant.java @@ -24,10 +24,6 @@ public final class ASTEnumConstant extends AbstractJavaNode super(id); } - ASTEnumConstant(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumDeclaration.java index e7cfd5074e..47ebc9fa61 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumDeclaration.java @@ -39,10 +39,6 @@ public final class ASTEnumDeclaration extends AbstractAnyTypeDeclaration { super(id); } - ASTEnumDeclaration(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEqualityExpression.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEqualityExpression.java index d5d20e0094..3443e7d23f 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEqualityExpression.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEqualityExpression.java @@ -38,10 +38,6 @@ public final class ASTEqualityExpression extends AbstractJavaExpr implements AST super(id); } - ASTEqualityExpression(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExclusiveOrExpression.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExclusiveOrExpression.java index 07b961709a..093c443756 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExclusiveOrExpression.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExclusiveOrExpression.java @@ -27,10 +27,6 @@ public final class ASTExclusiveOrExpression extends AbstractJavaExpr implements super(id); } - ASTExclusiveOrExpression(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExplicitConstructorInvocation.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExplicitConstructorInvocation.java index e89605d297..c70fb18df7 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExplicitConstructorInvocation.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExplicitConstructorInvocation.java @@ -30,10 +30,6 @@ public final class ASTExplicitConstructorInvocation extends AbstractStatement im super(id); } - ASTExplicitConstructorInvocation(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExpressionStatement.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExpressionStatement.java index 078c8e6d0a..aa32e21b02 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExpressionStatement.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExpressionStatement.java @@ -22,10 +22,6 @@ public final class ASTExpressionStatement extends AbstractStatement { super(id); } - ASTExpressionStatement(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExtendsList.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExtendsList.java index 7280374e97..e8cd9ea025 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExtendsList.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExtendsList.java @@ -23,10 +23,6 @@ public final class ASTExtendsList extends AbstractJavaNode implements Iterable iterator() { return children(ASTMemberValuePair.class).iterator(); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTNullLiteral.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTNullLiteral.java index e6c8b8a438..63243339ad 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTNullLiteral.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTNullLiteral.java @@ -19,10 +19,6 @@ public final class ASTNullLiteral extends AbstractLiteral implements ASTLiteral } - ASTNullLiteral(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTNumericLiteral.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTNumericLiteral.java index e7d426afa7..baada51ea7 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTNumericLiteral.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTNumericLiteral.java @@ -29,11 +29,6 @@ public final class ASTNumericLiteral extends AbstractLiteral implements ASTLiter } - ASTNumericLiteral(JavaParser p, int id) { - super(p, id); - } - - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPackageDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPackageDeclaration.java index 3b0c0ceed7..2571e99116 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPackageDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPackageDeclaration.java @@ -23,10 +23,6 @@ public final class ASTPackageDeclaration extends AbstractJavaNode implements Ann super(id); } - ASTPackageDeclaration(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPrimaryPrefix.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPrimaryPrefix.java index ed934b68c0..75e501870c 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPrimaryPrefix.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPrimaryPrefix.java @@ -14,10 +14,6 @@ public class ASTPrimaryPrefix extends AbstractJavaTypeNode { super(id); } - ASTPrimaryPrefix(JavaParser p, int id) { - super(p, id); - } - public boolean usesThisModifier() { return this.usesThisModifier; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPrimarySuffix.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPrimarySuffix.java index 8548abbaaf..ce7f3fc85a 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPrimarySuffix.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPrimarySuffix.java @@ -14,10 +14,6 @@ public class ASTPrimarySuffix extends AbstractJavaTypeNode { super(id); } - ASTPrimarySuffix(JavaParser p, int id) { - super(p, id); - } - public boolean isArrayDereference() { return isArrayDereference; } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPrimitiveType.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPrimitiveType.java index bc6ada9ced..24e2ae49b6 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPrimitiveType.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPrimitiveType.java @@ -35,7 +35,7 @@ public final class ASTPrimitiveType extends AbstractJavaTypeNode implements ASTT @Deprecated @InternalApi public ASTPrimitiveType(PrimitiveType type) { - super(JavaParserTreeConstants.JJTPRIMITIVETYPE); + super(JavaParserImplTreeConstants.JJTPRIMITIVETYPE); setImage(type.getToken()); } @@ -45,11 +45,6 @@ public final class ASTPrimitiveType extends AbstractJavaTypeNode implements ASTT } - ASTPrimitiveType(JavaParser p, int id) { - super(p, id); - } - - public boolean isBoolean() { return "boolean".equals(getImage()); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRSIGNEDSHIFT.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRSIGNEDSHIFT.java index 9ae9308a5f..03e2c3175e 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRSIGNEDSHIFT.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRSIGNEDSHIFT.java @@ -14,10 +14,6 @@ public final class ASTRSIGNEDSHIFT extends AbstractJavaNode { super(id); } - ASTRSIGNEDSHIFT(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRUNSIGNEDSHIFT.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRUNSIGNEDSHIFT.java index fb44ddd37f..4da0ff983c 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRUNSIGNEDSHIFT.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRUNSIGNEDSHIFT.java @@ -14,10 +14,6 @@ public final class ASTRUNSIGNEDSHIFT extends AbstractJavaNode { super(id); } - ASTRUNSIGNEDSHIFT(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTReceiverParameter.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTReceiverParameter.java index fc2539aac5..d991ad2f47 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTReceiverParameter.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTReceiverParameter.java @@ -42,10 +42,6 @@ public final class ASTReceiverParameter extends AbstractJavaNode { super(id); } - ASTReceiverParameter(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRelationalExpression.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRelationalExpression.java index 5c32010a7b..2e1190cbed 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRelationalExpression.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRelationalExpression.java @@ -27,10 +27,6 @@ public final class ASTRelationalExpression extends AbstractJavaExpr implements A super(id); } - ASTRelationalExpression(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTResource.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTResource.java index b9944546bb..a2749b6060 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTResource.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTResource.java @@ -29,10 +29,6 @@ public final class ASTResource extends AbstractJavaNode { super(id); } - ASTResource(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTResourceList.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTResourceList.java index 1072399d1c..630ae9e866 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTResourceList.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTResourceList.java @@ -26,10 +26,6 @@ public final class ASTResourceList extends AbstractJavaNode super(id); } - ASTResourceList(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTResources.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTResources.java index 2d1905e2e8..82e3eb1bcd 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTResources.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTResources.java @@ -11,10 +11,6 @@ public final class ASTResources extends AbstractJavaNode { super(id); } - ASTResources(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTResultType.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTResultType.java index 37c834402b..9067351909 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTResultType.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTResultType.java @@ -21,10 +21,6 @@ public final class ASTResultType extends AbstractJavaNode { super(id); } - ASTResultType(JavaParser p, int id) { - super(p, id); - } - public boolean returnsArray() { return !isVoid() && ((ASTType) getChild(0)).isArrayType(); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTReturnStatement.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTReturnStatement.java index 2e170d52c0..1b345f8530 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTReturnStatement.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTReturnStatement.java @@ -22,10 +22,6 @@ public final class ASTReturnStatement extends AbstractStatement { super(id); } - ASTReturnStatement(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTShiftExpression.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTShiftExpression.java index d6f5ef3746..c954e95186 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTShiftExpression.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTShiftExpression.java @@ -37,10 +37,6 @@ public final class ASTShiftExpression extends AbstractJavaExpr implements ASTExp super(id); } - ASTShiftExpression(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSingleMemberAnnotation.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSingleMemberAnnotation.java index 81e366f231..058a0bef0b 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSingleMemberAnnotation.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSingleMemberAnnotation.java @@ -21,10 +21,6 @@ public final class ASTSingleMemberAnnotation extends AbstractJavaTypeNode implem super(id); } - ASTSingleMemberAnnotation(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTStatementExpression.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTStatementExpression.java index fc60f78af9..cd2b23f261 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTStatementExpression.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTStatementExpression.java @@ -15,10 +15,6 @@ public final class ASTStatementExpression extends AbstractJavaTypeNode { super(id); } - ASTStatementExpression(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTStatementExpressionList.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTStatementExpressionList.java index d8b53d56e5..b00c806eb8 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTStatementExpressionList.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTStatementExpressionList.java @@ -26,10 +26,6 @@ public final class ASTStatementExpressionList extends AbstractStatement implemen super(id); } - ASTStatementExpressionList(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTStringLiteral.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTStringLiteral.java index 8fa6b80f75..ca6e43d4ae 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTStringLiteral.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTStringLiteral.java @@ -20,11 +20,6 @@ public final class ASTStringLiteral extends AbstractLiteral implements ASTLitera } - ASTStringLiteral(JavaParser p, int id) { - super(p, id); - } - - @Override public String getImage() { return getText().toString(); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSuperExpression.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSuperExpression.java index fa10fc7382..b7a3efada6 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSuperExpression.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSuperExpression.java @@ -23,10 +23,6 @@ public final class ASTSuperExpression extends AbstractJavaExpr implements ASTPri } - ASTSuperExpression(JavaParser p, int id) { - super(p, id); - } - @Nullable public ASTClassOrInterfaceType getQualifier() { return getNumChildren() > 0 ? (ASTClassOrInterfaceType) getChild(0) : null; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSwitchArrowBranch.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSwitchArrowBranch.java index 4f32b43223..1c32d23f5a 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSwitchArrowBranch.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSwitchArrowBranch.java @@ -20,10 +20,6 @@ public final class ASTSwitchArrowBranch extends AbstractJavaNode implements Left super(id); } - ASTSwitchArrowBranch(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSwitchExpression.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSwitchExpression.java index 03ab367c49..ad8290bc28 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSwitchExpression.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSwitchExpression.java @@ -24,10 +24,6 @@ public final class ASTSwitchExpression extends AbstractJavaExpr super(id); } - ASTSwitchExpression(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSwitchFallthroughBranch.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSwitchFallthroughBranch.java index bbaf2ebff8..b39f4cc084 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSwitchFallthroughBranch.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSwitchFallthroughBranch.java @@ -40,10 +40,6 @@ public final class ASTSwitchFallthroughBranch extends AbstractJavaNode super(id); } - ASTSwitchFallthroughBranch(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSwitchLabel.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSwitchLabel.java index e70bcd0e90..92af2d1048 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSwitchLabel.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSwitchLabel.java @@ -30,10 +30,6 @@ public final class ASTSwitchLabel extends AbstractJavaNode implements Iterable 0 ? (ASTClassOrInterfaceType) getChild(0) : null; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTThrowStatement.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTThrowStatement.java index 8043987d02..0f71d7342e 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTThrowStatement.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTThrowStatement.java @@ -19,10 +19,6 @@ public final class ASTThrowStatement extends AbstractStatement implements ASTSwi super(id); } - ASTThrowStatement(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTThrowsList.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTThrowsList.java index 5666ae8b16..c4acb5d110 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTThrowsList.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTThrowsList.java @@ -21,10 +21,6 @@ public final class ASTThrowsList extends AbstractJavaNode implements Iterable}. diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTWildcardType.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTWildcardType.java index f6b8d3a141..8d269e6544 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTWildcardType.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTWildcardType.java @@ -25,11 +25,6 @@ public final class ASTWildcardType extends AbstractJavaTypeNode implements ASTRe } - ASTWildcardType(JavaParser p, int id) { - super(p, id); - } - - @Override public String getTypeImage() { return "?"; // TODO? diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTYieldStatement.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTYieldStatement.java index 8df0b40caa..be6a680bb4 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTYieldStatement.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTYieldStatement.java @@ -19,10 +19,6 @@ public class ASTYieldStatement extends AbstractStatement { super(id); } - ASTYieldStatement(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractAnyTypeDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractAnyTypeDeclaration.java index 738f29ac4a..7d8c346ddc 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractAnyTypeDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractAnyTypeDeclaration.java @@ -21,18 +21,6 @@ abstract class AbstractAnyTypeDeclaration extends AbstractJavaAccessTypeNode imp super(i); } - - AbstractAnyTypeDeclaration(JavaParser parser, int i) { - super(parser, i); - } - - - @Override - public final boolean isNested() { - return getParent() instanceof ASTClassOrInterfaceBodyDeclaration - || getParent() instanceof ASTAnnotationTypeMemberDeclaration; - } - @Override @Deprecated public String getImage() { @@ -44,6 +32,16 @@ abstract class AbstractAnyTypeDeclaration extends AbstractJavaAccessTypeNode imp return getQualifiedName().getBinaryName(); } + @Override + public String getSimpleName() { + return getImage(); + } + + @Override + public boolean isFindBoundary() { + return isNested() || isLocal(); + } + /** * Returns true if the enclosing type of this type declaration * is any of the given kinds. If this declaration is a top-level diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaAccessNode.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaAccessNode.java index e9c3c62b01..d0ea4d54bd 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaAccessNode.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaAccessNode.java @@ -12,10 +12,6 @@ abstract class AbstractJavaAccessNode extends AbstractJavaNode implements Access super(i); } - AbstractJavaAccessNode(JavaParser parser, int i) { - super(parser, i); - } - @Override public int getModifiers() { return this.modifiers; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaAccessTypeNode.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaAccessTypeNode.java index c125a3519d..1a95e96b9c 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaAccessTypeNode.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaAccessTypeNode.java @@ -17,10 +17,6 @@ abstract class AbstractJavaAccessTypeNode extends AbstractJavaAccessNode impleme super(i); } - AbstractJavaAccessTypeNode(JavaParser parser, int i) { - super(parser, i); - } - @Override public Class getType() { if (typeDefinition != null) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaExpr.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaExpr.java index ac2a1e4d25..ad0c94a233 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaExpr.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaExpr.java @@ -12,10 +12,6 @@ abstract class AbstractJavaExpr extends AbstractJavaTypeNode implements ASTExpre super(i); } - AbstractJavaExpr(JavaParser p, int i) { - super(p, i); - } - void bumpParenDepth() { parenDepth++; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaNode.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaNode.java index 203c1c13c6..c808e803fa 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaNode.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaNode.java @@ -15,41 +15,15 @@ import net.sourceforge.pmd.lang.symboltable.Scope; abstract class AbstractJavaNode extends AbstractJjtreeNode implements JavaNode { - protected JavaParser parser; private Scope scope; private JSymbolTable symbolTable; private Comment comment; private ASTCompilationUnit root; - private CharSequence text; AbstractJavaNode(int id) { super(id); } - AbstractJavaNode(JavaParser parser, int id) { - super(id); - this.parser = parser; - } - - @Override - public int getBeginLine() { - return jjtGetFirstToken().getBeginLine(); - } - - @Override - public int getBeginColumn() { - return jjtGetFirstToken().getBeginColumn(); - } - - @Override - public int getEndLine() { - return jjtGetLastToken().getEndLine(); - } - - @Override - public int getEndColumn() { - return jjtGetLastToken().getEndColumn(); - } @Override public void jjtClose() { @@ -95,14 +69,6 @@ abstract class AbstractJavaNode extends AbstractJjtreeNode implements return scope; } - @Override - public CharSequence getText() { - if (text == null) { - text = getRoot().getText().subSequence(getStartOffset(), getEndOffset()); - } - return text; - } - void setScope(Scope scope) { this.scope = scope; } @@ -285,25 +251,6 @@ abstract class AbstractJavaNode extends AbstractJjtreeNode implements @Override public final String getXPathNodeName() { - return JavaParserTreeConstants.jjtNodeName[id]; - } - - - /** - * The toString of Java nodes is only meant for debugging purposes - * as it's pretty expensive. - */ - @Override - public String toString() { - return "|" + getXPathNodeName() + "|" + getStartOffset() + "," + getEndOffset() + "|" + getText(); - } - - private int getStartOffset() { - return this.jjtGetFirstToken().getStartInDocument(); - } - - - private int getEndOffset() { - return this.jjtGetLastToken().getEndInDocument(); + return JavaParserImplTreeConstants.jjtNodeName[id]; } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaTypeNode.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaTypeNode.java index d61d045217..7ad7f7e368 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaTypeNode.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaTypeNode.java @@ -22,10 +22,6 @@ abstract class AbstractJavaTypeNode extends AbstractJavaNode implements TypeNode super(i); } - AbstractJavaTypeNode(JavaParser p, int i) { - super(p, i); - } - @Override @Nullable public JavaTypeDefinition getTypeDefinition() { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractLiteral.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractLiteral.java index 42d3947d50..4a16734e5a 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractLiteral.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractLiteral.java @@ -13,7 +13,4 @@ abstract class AbstractLiteral extends AbstractJavaExpr implements ASTLiteral { super(i); } - AbstractLiteral(JavaParser p, int i) { - super(p, i); - } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractMethodLikeNode.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractMethodLikeNode.java index 53eba08ebd..2294ad9b0f 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractMethodLikeNode.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractMethodLikeNode.java @@ -16,11 +16,6 @@ abstract class AbstractMethodLikeNode extends AbstractJavaAccessNode implements } - AbstractMethodLikeNode(JavaParser parser, int i) { - super(parser, i); - } - - void setQualifiedName(JavaOperationQualifiedName qualifiedName) { this.qualifiedName = qualifiedName; } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractMethodOrConstructorDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractMethodOrConstructorDeclaration.java index 5f9f98ea5b..ce529d683f 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractMethodOrConstructorDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractMethodOrConstructorDeclaration.java @@ -17,11 +17,6 @@ abstract class AbstractMethodOrConstructorDeclaration extends AbstractMethodLike } - AbstractMethodOrConstructorDeclaration(JavaParser parser, int i) { - super(parser, i); - } - - @Override public JavaOperationSignature getSignature() { if (signature == null) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractStatement.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractStatement.java index 190e0d8298..79a64d8a06 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractStatement.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractStatement.java @@ -10,7 +10,4 @@ abstract class AbstractStatement extends AbstractJavaNode implements ASTStatemen super(id); } - AbstractStatement(JavaParser parser, int id) { - super(parser, id); - } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractTypeBodyDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractTypeBodyDeclaration.java index 885ef43157..5414cc0903 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractTypeBodyDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractTypeBodyDeclaration.java @@ -29,11 +29,6 @@ abstract class AbstractTypeBodyDeclaration extends AbstractJavaNode implements A } - AbstractTypeBodyDeclaration(JavaParser p, int id) { - super(p, id); - } - - @Override public JavaNode getDeclarationNode() { if (getNumChildren() == 0) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/DummyJavaNode.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/DummyJavaNode.java index 6eb44ab97b..96aaf0892e 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/DummyJavaNode.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/DummyJavaNode.java @@ -20,12 +20,6 @@ public class DummyJavaNode extends AbstractJavaNode { super(id); } - @InternalApi - @Deprecated - public DummyJavaNode(JavaParser parser, int id) { - super(parser, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return data; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/InternalApiBridge.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/InternalApiBridge.java index 1524dcdb48..f0e6538b28 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/InternalApiBridge.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/InternalApiBridge.java @@ -4,13 +4,7 @@ package net.sourceforge.pmd.lang.java.ast; -import java.io.Reader; - import net.sourceforge.pmd.annotation.InternalApi; -import net.sourceforge.pmd.lang.ParserOptions; -import net.sourceforge.pmd.lang.ast.AbstractTokenManager; -import net.sourceforge.pmd.lang.ast.impl.javacc.CharStreamFactory; -import net.sourceforge.pmd.lang.java.ast.internal.LanguageLevelChecker; import net.sourceforge.pmd.lang.java.qname.JavaOperationQualifiedName; import net.sourceforge.pmd.lang.java.qname.JavaTypeQualifiedName; import net.sourceforge.pmd.lang.java.symbols.table.JSymbolTable; @@ -68,20 +62,4 @@ public final class InternalApiBridge { } } - public static ASTCompilationUnit parseInternal(String fileName, Reader source, LanguageLevelChecker checker, ParserOptions options) { - JavaParser parser = new JavaParser(CharStreamFactory.javaCharStream(source)); - String suppressMarker = options.getSuppressMarker(); - if (suppressMarker != null) { - parser.setSuppressMarker(suppressMarker); - } - parser.setJdkVersion(checker.getJdkVersion()); - parser.setPreview(checker.isPreviewEnabled()); - - AbstractTokenManager.setFileName(fileName); - ASTCompilationUnit acu = parser.CompilationUnit(); - acu.setNoPmdComments(parser.getSuppressMap()); - checker.check(acu); - return acu; - } - } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaParser.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaParser.java new file mode 100644 index 0000000000..a87490e60e --- /dev/null +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaParser.java @@ -0,0 +1,64 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.java.ast; + +import java.io.Reader; + +import net.sourceforge.pmd.lang.ParserOptions; +import net.sourceforge.pmd.lang.TokenManager; +import net.sourceforge.pmd.lang.ast.CharStream; +import net.sourceforge.pmd.lang.ast.ParseException; +import net.sourceforge.pmd.lang.ast.impl.javacc.JavaCharStream; +import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccTokenDocument; +import net.sourceforge.pmd.lang.ast.impl.javacc.JjtreeParserAdapter; +import net.sourceforge.pmd.lang.java.ast.internal.LanguageLevelChecker; + +/** + * Adapter for the JavaParser, using the specified grammar version. + * + * @author Pieter_Van_Raemdonck - Application Engineers NV/SA - www.ae.be + * @author Andreas Dangel + */ +public class JavaParser extends JjtreeParserAdapter { + + private final LanguageLevelChecker checker; + + public JavaParser(LanguageLevelChecker checker, ParserOptions parserOptions) { + super(parserOptions); + this.checker = checker; + } + + @Override + public TokenManager createTokenManager(Reader source) { + return new JavaTokenManager(source); + } + + + @Override + protected JavaccTokenDocument newDocument(String fullText) { + return new JavaTokenDocument(fullText); + } + + @Override + protected CharStream newCharStream(JavaccTokenDocument tokenDocument) { + return new JavaCharStream(tokenDocument); + } + + @Override + protected ASTCompilationUnit parseImpl(CharStream cs, ParserOptions options) throws ParseException { + JavaParserImpl parser = new JavaParserImpl(cs); + String suppressMarker = options.getSuppressMarker(); + if (suppressMarker != null) { + parser.setSuppressMarker(suppressMarker); + } + parser.setJdkVersion(checker.getJdkVersion()); + parser.setPreview(checker.isPreviewEnabled()); + + ASTCompilationUnit acu = parser.CompilationUnit(); + acu.setNoPmdComments(parser.getSuppressMap()); + checker.check(acu); + return acu; + } +} diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaTokenFactory.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaTokenDocument.java similarity index 57% rename from pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaTokenFactory.java rename to pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaTokenDocument.java index 0947a00739..2f8c1cf369 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaTokenFactory.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaTokenDocument.java @@ -4,30 +4,39 @@ package net.sourceforge.pmd.lang.java.ast; +import static net.sourceforge.pmd.lang.java.ast.JavaTokenKinds.GT; +import static net.sourceforge.pmd.lang.java.ast.JavaTokenKinds.RSIGNEDSHIFT; +import static net.sourceforge.pmd.lang.java.ast.JavaTokenKinds.RUNSIGNEDSHIFT; +import static net.sourceforge.pmd.lang.java.ast.JavaTokenKinds.WHITESPACE; + +import org.checkerframework.checker.nullness.qual.Nullable; + import net.sourceforge.pmd.lang.ast.CharStream; -import net.sourceforge.pmd.lang.ast.JavaCharStream; -import net.sourceforge.pmd.lang.ast.impl.TokenDocument; import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken; +import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccTokenDocument; /** - * Support methods for the token manager. The call to {@link #newToken(int, CharStream)} - * is hacked in via search/replace on {@link JavaParserTokenManager}. + * {@link JavaccTokenDocument} for Java. */ -final class JavaTokenFactory { - - private JavaTokenFactory() { +final class JavaTokenDocument extends JavaccTokenDocument { + JavaTokenDocument(String fullText) { + super(fullText); } - static JavaccToken newToken(int kind, CharStream charStream) { - JavaCharStream jcs = (JavaCharStream) charStream; + @Override + protected @Nullable String describeKindImpl(int kind) { + return JavaTokenKinds.describe(kind); + } + @Override + public JavaccToken createToken(int kind, CharStream jcs, @Nullable String image) { switch (kind) { - case JavaParserConstants.RUNSIGNEDSHIFT: - case JavaParserConstants.RSIGNEDSHIFT: - case JavaParserConstants.GT: + case RUNSIGNEDSHIFT: + case RSIGNEDSHIFT: + case GT: return new GTToken( - JavaParserConstants.GT, + GT, kind, ">", jcs.getStartOffset(), @@ -35,7 +44,7 @@ final class JavaTokenFactory { jcs.getTokenDocument() ); - case JavaParserConstants.WHITESPACE: + case WHITESPACE: // We don't create a new string for the image of whitespace tokens eagerly // It's unlikely that anybody cares about that, and since @@ -48,18 +57,7 @@ final class JavaTokenFactory { ); default: - // Most tokens have an entry in there, it's used to share the - // image string for keywords & punctuation. Those represent ~40% - // of token instances - String image = JavaParserTokenManager.jjstrLiteralImages[kind]; - - return new JavaccToken( - kind, - image == null ? charStream.GetImage() : image, - jcs.getStartOffset(), - jcs.getEndOffset(), - jcs.getTokenDocument() - ); + return super.createToken(kind, jcs, image); } } @@ -69,7 +67,7 @@ final class JavaTokenFactory { private static final class LazyImageToken extends JavaccToken { - LazyImageToken(int kind, int startInclusive, int endExclusive, TokenDocument document) { + LazyImageToken(int kind, int startInclusive, int endExclusive, JavaccTokenDocument document) { super(kind, null, startInclusive, endExclusive, document); } @@ -83,7 +81,7 @@ final class JavaTokenFactory { final int realKind; - GTToken(int kind, int realKind, CharSequence image, int startOffset, int endOffset, TokenDocument doc) { + GTToken(int kind, int realKind, CharSequence image, int startOffset, int endOffset, JavaccTokenDocument doc) { super(kind, image, startOffset, endOffset, doc); this.realKind = realKind; } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/JavaTokenManager.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaTokenManager.java similarity index 69% rename from pmd-java/src/main/java/net/sourceforge/pmd/lang/java/JavaTokenManager.java rename to pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaTokenManager.java index 4371e11903..b7ea671a71 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/JavaTokenManager.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaTokenManager.java @@ -1,23 +1,22 @@ -/** +/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ -package net.sourceforge.pmd.lang.java; +package net.sourceforge.pmd.lang.java.ast; import java.io.Reader; import net.sourceforge.pmd.lang.TokenManager; import net.sourceforge.pmd.lang.ast.impl.javacc.CharStreamFactory; -import net.sourceforge.pmd.lang.java.ast.JavaParserTokenManager; /** * Java Token Manager implementation. */ public class JavaTokenManager implements TokenManager { - private final JavaParserTokenManager tokenManager; + private final JavaParserImplTokenManager tokenManager; public JavaTokenManager(Reader source) { - tokenManager = new JavaParserTokenManager(CharStreamFactory.javaCharStream(source)); + tokenManager = new JavaParserImplTokenManager(CharStreamFactory.javaCharStream(source)); } @Override diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/ReportingStrategy.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/ReportingStrategy.java index 0871b803c8..5f7156fa63 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/ReportingStrategy.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/ReportingStrategy.java @@ -5,7 +5,7 @@ package net.sourceforge.pmd.lang.java.ast.internal; import net.sourceforge.pmd.lang.ast.Node; -import net.sourceforge.pmd.lang.java.ast.ParseException; +import net.sourceforge.pmd.lang.ast.ParseException; /** * Strategy for reporting language-feature violations, for use by a diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/internal/JavaLanguageHandler.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/internal/JavaLanguageHandler.java index 03234fdce6..81cf4d2541 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/internal/JavaLanguageHandler.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/internal/JavaLanguageHandler.java @@ -17,6 +17,7 @@ import net.sourceforge.pmd.lang.dfa.DFAGraphRule; import net.sourceforge.pmd.lang.java.JavaLanguageModule; import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit; +import net.sourceforge.pmd.lang.java.ast.JavaParser; import net.sourceforge.pmd.lang.java.ast.MethodLikeNode; import net.sourceforge.pmd.lang.java.ast.internal.LanguageLevelChecker; import net.sourceforge.pmd.lang.java.ast.internal.ReportingStrategy; @@ -56,7 +57,7 @@ public class JavaLanguageHandler extends AbstractPmdLanguageVersionHandler { @Override public Parser getParser(ParserOptions parserOptions) { - return new JavaLanguageParser(levelChecker, parserOptions); + return new JavaParser(levelChecker, parserOptions); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/internal/JavaLanguageParser.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/internal/JavaLanguageParser.java deleted file mode 100644 index 55817f8b3b..0000000000 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/internal/JavaLanguageParser.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * BSD-style license; for more info see http://pmd.sourceforge.net/license.html - */ - -package net.sourceforge.pmd.lang.java.internal; - -import java.io.Reader; - -import net.sourceforge.pmd.lang.AbstractParser; -import net.sourceforge.pmd.lang.ParserOptions; -import net.sourceforge.pmd.lang.TokenManager; -import net.sourceforge.pmd.lang.ast.Node; -import net.sourceforge.pmd.lang.java.JavaTokenManager; -import net.sourceforge.pmd.lang.java.ast.InternalApiBridge; -import net.sourceforge.pmd.lang.java.ast.ParseException; -import net.sourceforge.pmd.lang.java.ast.internal.LanguageLevelChecker; - -/** - * Adapter for the JavaParser, using the specified grammar version. - * - * @author Pieter_Van_Raemdonck - Application Engineers NV/SA - www.ae.be - * @author Andreas Dangel - */ -public class JavaLanguageParser extends AbstractParser { - - private final LanguageLevelChecker checker; - - JavaLanguageParser(LanguageLevelChecker checker, ParserOptions parserOptions) { - super(parserOptions); - this.checker = checker; - } - - @Override - public TokenManager createTokenManager(Reader source) { - return new JavaTokenManager(source); - } - - - @Override - public Node parse(String fileName, Reader source) throws ParseException { - return InternalApiBridge.parseInternal(fileName, source, checker, getParserOptions()); - } -} diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/ForLoopCanBeForeachRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/ForLoopCanBeForeachRule.java index aa042e74dd..addffd5c26 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/ForLoopCanBeForeachRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/ForLoopCanBeForeachRule.java @@ -4,6 +4,8 @@ package net.sourceforge.pmd.lang.java.rule.bestpractices; +import static net.sourceforge.pmd.lang.ast.NodeStream.forkJoin; + import java.util.Iterator; import java.util.List; import java.util.Map; @@ -196,29 +198,29 @@ public class ForLoopCanBeForeachRule extends AbstractJavaRule { return Optional.empty(); } - return guardCondition.children(ASTRelationalExpression.class) - // TODO - fix ForLoopCanBeForeachRule - // .forkJoin( - // rel -> NodeStream.of(rel).filterMatching(Node::getImage, "<"), - // rel -> NodeStream.of(rel) - // .filterMatching(Node::getImage, "<=") - // .children(ASTAdditiveExpression.class) - // .filter(expr -> - // expr.getNumChildren() == 2 - // && expr.getOperator().equals("-") - // && expr.children(ASTPrimaryExpression.class) - // .children(ASTPrimaryPrefix.class) - // .children(ASTLiteral.class) - // .filterMatching(Node::getImage, "1") - // .nonEmpty() - // ) - // ) - .children(ASTPrimaryExpression.class) - .children(ASTPrimaryPrefix.class) - .children(ASTName.class) - .filter(n -> n.getImage().matches("\\w+\\.(size|length)")) - .firstOpt() - .map(astName -> astName.getImage().split("\\.")[0]); + return forkJoin( + guardCondition.children(ASTRelationalExpression.class), + rel -> NodeStream.of(rel).filterMatching(Node::getImage, "<"), + rel -> NodeStream.of(rel) + // TODO fix + // .filterMatching(Node::getImage, "<=") + // .children(ASTAdditiveExpression.class) + // .filter(expr -> + // expr.getNumChildren() == 2 + // && expr.getOperator().equals("-") + // && expr.children(ASTPrimaryExpression.class) + // .children(ASTPrimaryPrefix.class) + // .children(ASTLiteral.class) + // .filterMatching(Node::getImage, "1") + // .nonEmpty() + // ) + ) + .children(ASTPrimaryExpression.class) + .children(ASTPrimaryPrefix.class) + .children(ASTName.class) + .filter(n -> n.getImage().matches("\\w+\\.(size|length)")) + .firstOpt() + .map(astName -> astName.getImage().split("\\.")[0]); } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/InvalidLogMessageFormatRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/InvalidLogMessageFormatRule.java index aefdc095f5..05dd22c874 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/InvalidLogMessageFormatRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/InvalidLogMessageFormatRule.java @@ -85,8 +85,13 @@ public class InvalidLogMessageFormatRule extends AbstractJavaRule { } // find the arguments - final List argumentList = parentNode.getFirstChildOfType(ASTPrimarySuffix.class) - .getFirstDescendantOfType(ASTArgumentList.class).findChildrenOfType(ASTExpression.class); + final List argumentList = new ArrayList<>(parentNode.getFirstChildOfType(ASTPrimarySuffix.class) + .getFirstDescendantOfType(ASTArgumentList.class).findChildrenOfType(ASTExpression.class)); + + // ignore the first argument if it is a known non-string value, e.g. a slf4j-Marker + if (argumentList.get(0).getType() != null && !argumentList.get(0).getType().equals(String.class)) { + argumentList.remove(0); + } // remove the message parameter final ASTExpression messageParam = argumentList.remove(0); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/ClassScope.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/ClassScope.java index bc2f36915f..459e065925 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/ClassScope.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/ClassScope.java @@ -35,7 +35,6 @@ import net.sourceforge.pmd.lang.java.ast.ASTTypeParameters; import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId; import net.sourceforge.pmd.lang.java.ast.AccessNode; import net.sourceforge.pmd.lang.java.ast.InternalApiBridge; -import net.sourceforge.pmd.lang.java.ast.JavaParserTreeConstants; import net.sourceforge.pmd.lang.symboltable.Applier; import net.sourceforge.pmd.lang.symboltable.ImageFinderFunction; import net.sourceforge.pmd.lang.symboltable.NameDeclaration; @@ -296,13 +295,13 @@ public class ClassScope extends AbstractJavaScope { */ private MethodNameDeclaration createBuiltInMethodDeclaration(final String methodName, final String... parameterTypes) { - ASTMethodDeclaration methodDeclaration = new ASTMethodDeclaration(JavaParserTreeConstants.JJTMETHODDECLARATION); + ASTMethodDeclaration methodDeclaration = new ASTMethodDeclaration(0); InternalApiBridge.setModifier(methodDeclaration, AccessNode.PUBLIC); InternalApiBridge.setScope(methodDeclaration, this); methodDeclaration.setImage(methodName); - ASTFormalParameters formalParameters = new ASTFormalParameters(JavaParserTreeConstants.JJTFORMALPARAMETERS); + ASTFormalParameters formalParameters = new ASTFormalParameters(0); methodDeclaration.jjtAddChild(formalParameters, 0); formalParameters.jjtSetParent(methodDeclaration); @@ -312,12 +311,11 @@ public class ClassScope extends AbstractJavaScope { * Going backwards makes sure the first time it gets the right size avoiding copies. */ for (int i = parameterTypes.length - 1; i >= 0; i--) { - ASTFormalParameter formalParameter = new ASTFormalParameter(JavaParserTreeConstants.JJTFORMALPARAMETER); + ASTFormalParameter formalParameter = new ASTFormalParameter(0); formalParameters.jjtAddChild(formalParameter, i); formalParameter.jjtSetParent(formalParameters); - ASTVariableDeclaratorId variableDeclaratorId = new ASTVariableDeclaratorId( - JavaParserTreeConstants.JJTVARIABLEDECLARATORID); + ASTVariableDeclaratorId variableDeclaratorId = new ASTVariableDeclaratorId(0); variableDeclaratorId.setImage("arg" + i); formalParameter.jjtAddChild(variableDeclaratorId, 1); variableDeclaratorId.jjtSetParent(formalParameter); diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/cpd/JavaTokensTokenizerTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/cpd/JavaTokensTokenizerTest.java index 74cb844c65..255535afe2 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/cpd/JavaTokensTokenizerTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/cpd/JavaTokensTokenizerTest.java @@ -12,7 +12,7 @@ import java.util.List; import org.junit.Test; import net.sourceforge.pmd.PMD; -import net.sourceforge.pmd.lang.java.ast.JavaParserConstants; +import net.sourceforge.pmd.lang.java.ast.JavaTokenKinds; public class JavaTokensTokenizerTest { @@ -213,7 +213,7 @@ public class JavaTokensTokenizerTest { List tokenList = tokens.getTokens(); // Member variable of type Foo - assertEquals(String.valueOf(JavaParserConstants.IDENTIFIER), tokenList.get(7).toString()); + assertEquals(String.valueOf(JavaTokenKinds.IDENTIFIER), tokenList.get(7).toString()); // Public constructor assertEquals("Foo", tokenList.get(10).toString()); // Private constructor @@ -241,8 +241,8 @@ public class JavaTokensTokenizerTest { List tokenList = tokens.getTokens(); // Enum member - assertEquals(String.valueOf(JavaParserConstants.IDENTIFIER), tokenList.get(4).toString()); - assertEquals(String.valueOf(JavaParserConstants.IDENTIFIER), tokenList.get(9).toString()); + assertEquals(String.valueOf(JavaTokenKinds.IDENTIFIER), tokenList.get(4).toString()); + assertEquals(String.valueOf(JavaTokenKinds.IDENTIFIER), tokenList.get(9).toString()); // Enum constructor assertEquals("Foo", tokenList.get(13).toString()); } @@ -265,6 +265,6 @@ public class JavaTokensTokenizerTest { // Class constructor assertEquals("Foo", tokenList.get(4).toString()); - assertEquals(String.valueOf(JavaParserConstants.IDENTIFIER), tokenList.get(11).toString()); + assertEquals(String.valueOf(JavaTokenKinds.IDENTIFIER), tokenList.get(11).toString()); } } diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ASTImportDeclarationTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ASTImportDeclarationTest.java index ef2a96075f..7547f00363 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ASTImportDeclarationTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ASTImportDeclarationTest.java @@ -11,6 +11,8 @@ import java.util.List; import org.junit.Test; +import net.sourceforge.pmd.lang.ast.ParseException; + public class ASTImportDeclarationTest extends BaseParserTest { @Test diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/AccessNodeTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/AccessNodeTest.java index e430036fbf..63aae9f2cd 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/AccessNodeTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/AccessNodeTest.java @@ -22,10 +22,6 @@ public class AccessNodeTest extends BaseParserTest { super(i); } - public MyAccessNode(JavaParser parser, int i) { - super(parser, i); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/JDKVersionTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/JDKVersionTest.java index 97fda69133..a68f0c776d 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/JDKVersionTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/JDKVersionTest.java @@ -13,6 +13,7 @@ import java.util.List; import org.junit.Test; +import net.sourceforge.pmd.lang.ast.ParseException; import net.sourceforge.pmd.lang.java.JavaParsingHelper; public class JDKVersionTest { diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/SimpleNodeTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/SimpleNodeTest.java index 26eca8e138..497938c003 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/SimpleNodeTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/SimpleNodeTest.java @@ -27,19 +27,19 @@ public class SimpleNodeTest extends BaseParserTest { @Test public void testMethodDiffLines() { List methods = java.getNodes(ASTMethodDeclaration.class, METHOD_DIFF_LINES); - verifyNode(methods.iterator().next(), 2, 9, 4, 2); + verifyNode(methods.iterator().next(), 2, 9, 4, 3); } @Test public void testMethodSameLine() { List methods = java.getNodes(ASTMethodDeclaration.class, METHOD_SAME_LINE); - verifyNode(methods.iterator().next(), 2, 9, 2, 21); + verifyNode(methods.iterator().next(), 2, 9, 2, 22); } @Test public void testNoLookahead() { List uCD = java.getNodes(ASTClassOrInterfaceDeclaration.class, NO_LOOKAHEAD); - verifyNode(uCD.iterator().next(), 1, 8, 1, 20); + verifyNode(uCD.iterator().next(), 1, 8, 1, 21); } @Test @@ -70,7 +70,7 @@ public class SimpleNodeTest extends BaseParserTest { public void testColumnsOnQualifiedName() { for (Node node : java.getNodes(ASTName.class, QUALIFIED_NAME)) { if (node.getImage().equals("java.io.File")) { - verifyNode(node, 1, 8, 1, 19); + verifyNode(node, 1, 8, 1, 20); } } } @@ -79,10 +79,10 @@ public class SimpleNodeTest extends BaseParserTest { public void testLineNumbersForNameSplitOverTwoLines() { for (Node node : java.getNodes(ASTName.class, BROKEN_LINE_IN_NAME)) { if (node.getImage().equals("java.io.File")) { - verifyNode(node, 1, 8, 2, 4); + verifyNode(node, 1, 8, 2, 5); } if (node.getImage().equals("Foo")) { - verifyNode(node, 2, 15, 2, 18); + verifyNode(node, 2, 15, 2, 19); } } } diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/KotlinTestingDsl.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/KotlinTestingDsl.kt index 94961b8cd8..624760b031 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/KotlinTestingDsl.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/KotlinTestingDsl.kt @@ -7,6 +7,7 @@ import io.kotlintest.Result import io.kotlintest.matchers.string.shouldContain import io.kotlintest.shouldThrow import net.sourceforge.pmd.lang.ast.Node +import net.sourceforge.pmd.lang.ast.ParseException import net.sourceforge.pmd.lang.ast.TokenMgrError import net.sourceforge.pmd.lang.ast.test.* import net.sourceforge.pmd.lang.java.JavaParsingHelper diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ParserTestSpec.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ParserTestSpec.kt index 3182bd4591..a317eedc63 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ParserTestSpec.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ParserTestSpec.kt @@ -6,7 +6,10 @@ import io.kotlintest.TestContext import io.kotlintest.TestType import io.kotlintest.specs.IntelliMarker import net.sourceforge.pmd.lang.ast.Node -import net.sourceforge.pmd.lang.ast.test.* +import net.sourceforge.pmd.lang.ast.ParseException +import net.sourceforge.pmd.lang.ast.test.Assertions +import net.sourceforge.pmd.lang.ast.test.ValuedNodeSpec +import net.sourceforge.pmd.lang.ast.test.matchNode import io.kotlintest.should as kotlintestShould /** diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/InvalidLogMessageFormat.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/InvalidLogMessageFormat.xml index 5b44373acc..8f0db82778 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/InvalidLogMessageFormat.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/InvalidLogMessageFormat.xml @@ -780,6 +780,33 @@ public class InvalidLogMessageFormatTest { logger.warn("foo {}", "flibble", "moo", "blah", "blah"); // PMD flags this logger.warn("foo", "flibble", "moo", "blah", "blah"); // PMD doesn't flag this } +} + ]]> + + + + ignore slf4j-Markers when detecting the number of arguments + 2 + 11,17 + diff --git a/pmd-modelica/etc/grammar/Modelica.jjt b/pmd-modelica/etc/grammar/Modelica.jjt index 33a786817d..67039e9d1b 100644 --- a/pmd-modelica/etc/grammar/Modelica.jjt +++ b/pmd-modelica/etc/grammar/Modelica.jjt @@ -39,23 +39,24 @@ options { MULTI = true; VISITOR = true; NODE_USES_PARSER = true; - TRACK_TOKENS = true; + TRACK_TOKENS = false; + SUPPORT_CLASS_VISIBILITY_PUBLIC=false; NODE_PACKAGE="net.sourceforge.pmd.lang.modelica.ast"; UNICODE_INPUT = true; } -PARSER_BEGIN(ModelicaParser) +PARSER_BEGIN(ModelicaParserImpl) package net.sourceforge.pmd.lang.modelica.ast; import net.sourceforge.pmd.lang.ast.CharStream; import net.sourceforge.pmd.lang.ast.TokenMgrError; -public class ModelicaParser { +class ModelicaParserImpl { } -PARSER_END(ModelicaParser) +PARSER_END(ModelicaParserImpl) TOKEN: { @@ -334,7 +335,7 @@ void LanguageSpecification(): } { t = - { jjtThis.setImage(t.image); } + { jjtThis.setImage(t.getImage()); } } void ExternalFunctionCall(): {} @@ -837,7 +838,7 @@ void NumberLiteral(): } { t = - { jjtThis.setImage(t.image); } + { jjtThis.setImage(t.getImage()); } } void StringLiteral(): @@ -846,7 +847,7 @@ void StringLiteral(): } { t = - { jjtThis.setImage(t.image); } + { jjtThis.setImage(t.getImage()); } } void Name(): {} @@ -865,7 +866,7 @@ void SimpleName(): } { t = - { jjtThis.setImage(t.image); } + { jjtThis.setImage(t.getImage()); } } void SubscriptedName(): {} diff --git a/pmd-modelica/pom.xml b/pmd-modelica/pom.xml index 31c8498ea8..bd01139cb4 100644 --- a/pmd-modelica/pom.xml +++ b/pmd-modelica/pom.xml @@ -39,9 +39,10 @@ generate-sources - - - + + + + @@ -76,7 +77,11 @@ net.sourceforge.pmd pmd-core - + + org.junit.vintage + junit-vintage-engine + test + net.sourceforge.pmd pmd-lang-test @@ -87,10 +92,5 @@ pmd-test test - - org.junit.vintage - junit-vintage-engine - test - diff --git a/pmd-modelica/src/main/ant/alljavacc.xml b/pmd-modelica/src/main/ant/alljavacc.xml deleted file mode 100644 index 2f1674b358..0000000000 --- a/pmd-modelica/src/main/ant/alljavacc.xml +++ /dev/null @@ -1,174 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - public class - - - - - - - - - - public class Token implements java.io.Serializable - - - - - - public Token specialToken; - - - - - - - SimpleNode - AbstractModelicaNode - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/cpd/ModelicaTokenizer.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/cpd/ModelicaTokenizer.java index e688b3dcfa..fc77f0ba1e 100644 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/cpd/ModelicaTokenizer.java +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/cpd/ModelicaTokenizer.java @@ -10,9 +10,9 @@ import net.sourceforge.pmd.cpd.internal.JavaCCTokenizer; import net.sourceforge.pmd.cpd.token.JavaCCTokenFilter; import net.sourceforge.pmd.lang.TokenManager; import net.sourceforge.pmd.lang.ast.GenericToken; -import net.sourceforge.pmd.lang.modelica.ModelicaTokenManager; -import net.sourceforge.pmd.lang.modelica.ast.ModelicaParser; -import net.sourceforge.pmd.lang.modelica.ast.Token; +import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken; +import net.sourceforge.pmd.lang.modelica.ast.ModelicaTokenKinds; +import net.sourceforge.pmd.lang.modelica.ast.ModelicaTokenManager; public class ModelicaTokenizer extends JavaCCTokenizer { @@ -35,28 +35,28 @@ public class ModelicaTokenizer extends JavaCCTokenizer { super(tokenManager); } - private void skipWithinAndImport(Token currentToken) { + private void skipWithinAndImport(JavaccToken currentToken) { final int type = currentToken.kind; - if (type == ModelicaParser.IMPORT || type == ModelicaParser.WITHIN) { + if (type == ModelicaTokenKinds.IMPORT || type == ModelicaTokenKinds.WITHIN) { discardingWithinAndImport = true; - } else if (discardingWithinAndImport && type == ModelicaParser.SC) { + } else if (discardingWithinAndImport && type == ModelicaTokenKinds.SC) { discardingWithinAndImport = false; } } - private void skipAnnotation(Token currentToken) { + private void skipAnnotation(JavaccToken currentToken) { final int type = currentToken.kind; - if (type == ModelicaParser.ANNOTATION) { + if (type == ModelicaTokenKinds.ANNOTATION) { discardingAnnotation = true; - } else if (discardingAnnotation && type == ModelicaParser.SC) { + } else if (discardingAnnotation && type == ModelicaTokenKinds.SC) { discardingAnnotation = false; } } @Override protected void analyzeToken(GenericToken currentToken) { - skipWithinAndImport((Token) currentToken); - skipAnnotation((Token) currentToken); + skipWithinAndImport((JavaccToken) currentToken); + skipAnnotation((JavaccToken) currentToken); } @Override diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ModelicaHandler.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ModelicaHandler.java index d7b17b69c1..b0f207f597 100644 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ModelicaHandler.java +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ModelicaHandler.java @@ -9,6 +9,7 @@ import net.sourceforge.pmd.lang.Parser; import net.sourceforge.pmd.lang.ParserOptions; import net.sourceforge.pmd.lang.XPathHandler; import net.sourceforge.pmd.lang.ast.xpath.DefaultASTXPathHandler; +import net.sourceforge.pmd.lang.modelica.ast.ModelicaParser; import net.sourceforge.pmd.lang.modelica.internal.ModelicaProcessingStage; import net.sourceforge.pmd.lang.modelica.rule.ModelicaRuleViolationFactory; import net.sourceforge.pmd.lang.rule.RuleViolationFactory; diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ModelicaParser.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ModelicaParser.java deleted file mode 100644 index 5e58431b39..0000000000 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ModelicaParser.java +++ /dev/null @@ -1,34 +0,0 @@ -/** - * BSD-style license; for more info see http://pmd.sourceforge.net/license.html - */ - -package net.sourceforge.pmd.lang.modelica; - -import java.io.Reader; - -import net.sourceforge.pmd.lang.AbstractParser; -import net.sourceforge.pmd.lang.ParserOptions; -import net.sourceforge.pmd.lang.TokenManager; -import net.sourceforge.pmd.lang.ast.AbstractTokenManager; -import net.sourceforge.pmd.lang.ast.Node; -import net.sourceforge.pmd.lang.ast.ParseException; -import net.sourceforge.pmd.lang.ast.impl.javacc.CharStreamFactory; - - -public class ModelicaParser extends AbstractParser { - public ModelicaParser(final ParserOptions parserOptions) { - super(parserOptions); - } - - @Override - protected TokenManager createTokenManager(Reader source) { - return new ModelicaTokenManager(source); - } - - @Override - public Node parse(String fileName, Reader source) throws ParseException { - AbstractTokenManager.setFileName(fileName); - return new net.sourceforge.pmd.lang.modelica.ast.ModelicaParser(CharStreamFactory.simpleCharStream(source)).StoredDefinition(); - } - -} diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTClassDefinition.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTClassDefinition.java index 08e3aecd56..7735242eec 100644 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTClassDefinition.java +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTClassDefinition.java @@ -15,10 +15,6 @@ public class ASTClassDefinition extends AbstractModelicaNode { super(id); } - ASTClassDefinition(ModelicaParser p, int id) { - super(p, id); - } - public boolean isPartial() { return prefixes.getFirstChildOfType(ASTPartialClause.class) != null; } diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTComponentReference.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTComponentReference.java index 7a86ca8b54..958ab1975b 100644 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTComponentReference.java +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTComponentReference.java @@ -18,10 +18,6 @@ public final class ASTComponentReference extends AbstractModelicaNode implements super(id); } - ASTComponentReference(ModelicaParser p, int id) { - super(p, id); - } - void markAbsolute() { absolute = true; } diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTDerClassSpecifier.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTDerClassSpecifier.java index e3b460909a..902ce473be 100644 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTDerClassSpecifier.java +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTDerClassSpecifier.java @@ -9,10 +9,6 @@ public final class ASTDerClassSpecifier extends AbstractModelicaClassSpecifierNo super(id); } - ASTDerClassSpecifier(ModelicaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTElementList.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTElementList.java index b25bb493c6..315dca7380 100644 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTElementList.java +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTElementList.java @@ -11,10 +11,6 @@ public class ASTElementList extends AbstractModelicaNode { super(id); } - ASTElementList(ModelicaParser p, int id) { - super(p, id); - } - void setVisibility(Visibility visibility) { this.visibility = visibility; } diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTEnumerationShortClassSpecifier.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTEnumerationShortClassSpecifier.java index 85e9477c15..935de9b3ae 100644 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTEnumerationShortClassSpecifier.java +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTEnumerationShortClassSpecifier.java @@ -9,10 +9,6 @@ public final class ASTEnumerationShortClassSpecifier extends AbstractModelicaCla super(id); } - ASTEnumerationShortClassSpecifier(ModelicaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTExtendingLongClassSpecifier.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTExtendingLongClassSpecifier.java index 3b6f0d1ffd..d4e468d718 100644 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTExtendingLongClassSpecifier.java +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTExtendingLongClassSpecifier.java @@ -11,10 +11,6 @@ public final class ASTExtendingLongClassSpecifier extends AbstractModelicaClassS super(id); } - ASTExtendingLongClassSpecifier(ModelicaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTMultipleDefinitionImportClause.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTMultipleDefinitionImportClause.java index 94d332ad5f..2c00b81e89 100644 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTMultipleDefinitionImportClause.java +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTMultipleDefinitionImportClause.java @@ -24,10 +24,6 @@ public final class ASTMultipleDefinitionImportClause extends AbstractModelicaImp super(id); } - ASTMultipleDefinitionImportClause(ModelicaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTName.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTName.java index 816c71d912..2032c2f3a7 100644 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTName.java +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTName.java @@ -18,10 +18,6 @@ public final class ASTName extends AbstractModelicaNode implements ResolvableMod super(id); } - ASTName(ModelicaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTRenamingImportClause.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTRenamingImportClause.java index fcb52bfe83..c3cc002560 100644 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTRenamingImportClause.java +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTRenamingImportClause.java @@ -18,10 +18,6 @@ public final class ASTRenamingImportClause extends AbstractModelicaImportClause super(id); } - ASTRenamingImportClause(ModelicaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTSimpleLongClassSpecifier.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTSimpleLongClassSpecifier.java index f33f0d75f1..8906bd30d4 100644 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTSimpleLongClassSpecifier.java +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTSimpleLongClassSpecifier.java @@ -11,10 +11,6 @@ public final class ASTSimpleLongClassSpecifier extends AbstractModelicaClassSpec super(id); } - ASTSimpleLongClassSpecifier(ModelicaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTSimpleShortClassSpecifier.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTSimpleShortClassSpecifier.java index 74d472a05b..c3d0a47d50 100644 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTSimpleShortClassSpecifier.java +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTSimpleShortClassSpecifier.java @@ -12,10 +12,6 @@ public final class ASTSimpleShortClassSpecifier extends AbstractModelicaClassSpe super(id); } - ASTSimpleShortClassSpecifier(ModelicaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTSingleDefinitionImportClause.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTSingleDefinitionImportClause.java index c38b73de9e..0c7325b7fd 100644 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTSingleDefinitionImportClause.java +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTSingleDefinitionImportClause.java @@ -18,10 +18,6 @@ public class ASTSingleDefinitionImportClause extends AbstractModelicaImportClaus super(id); } - ASTSingleDefinitionImportClause(ModelicaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTStoredDefinition.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTStoredDefinition.java index e2a42a69a7..0bf29bffc5 100644 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTStoredDefinition.java +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTStoredDefinition.java @@ -17,10 +17,6 @@ public class ASTStoredDefinition extends AbstractModelicaNode implements RootNod super(id); } - ASTStoredDefinition(ModelicaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTUnqualifiedImportClause.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTUnqualifiedImportClause.java index 15b3c53b5a..a7ddaf740f 100644 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTUnqualifiedImportClause.java +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTUnqualifiedImportClause.java @@ -20,10 +20,6 @@ public final class ASTUnqualifiedImportClause extends AbstractModelicaImportClau super(id); } - ASTUnqualifiedImportClause(ModelicaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTWithinClause.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTWithinClause.java index 827398a1a9..6f84d1eaf7 100644 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTWithinClause.java +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ASTWithinClause.java @@ -9,10 +9,6 @@ public final class ASTWithinClause extends AbstractModelicaNode { super(id); } - ASTWithinClause(ModelicaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { return visitor.visit(this, data); diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/AbstractModelicaClassSpecifierNode.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/AbstractModelicaClassSpecifierNode.java index 9d9b6b46b3..781f5ac6ce 100644 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/AbstractModelicaClassSpecifierNode.java +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/AbstractModelicaClassSpecifierNode.java @@ -15,10 +15,6 @@ abstract class AbstractModelicaClassSpecifierNode extends AbstractModelicaNode i super(id); } - AbstractModelicaClassSpecifierNode(ModelicaParser parser, int id) { - super(parser, id); - } - @Override public void jjtClose() { super.jjtClose(); diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/AbstractModelicaImportClause.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/AbstractModelicaImportClause.java index beb2a15373..98e6c79999 100644 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/AbstractModelicaImportClause.java +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/AbstractModelicaImportClause.java @@ -21,10 +21,6 @@ abstract class AbstractModelicaImportClause extends AbstractModelicaNode impleme super(id); } - AbstractModelicaImportClause(ModelicaParser parser, int id) { - super(parser, id); - } - /** * Some import clauses are considered "qualified", some "unqualified", the former being processed first * while looking up some name. See "5.3.1 Simple Name Lookup" from MLS 3.4. diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/AbstractModelicaNode.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/AbstractModelicaNode.java index b66edeb876..fab9f05dfb 100644 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/AbstractModelicaNode.java +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/AbstractModelicaNode.java @@ -4,7 +4,6 @@ package net.sourceforge.pmd.lang.modelica.ast; -import net.sourceforge.pmd.lang.ast.GenericToken; import net.sourceforge.pmd.lang.ast.impl.javacc.AbstractJjtreeNode; import net.sourceforge.pmd.lang.modelica.resolver.ModelicaScope; @@ -19,24 +18,12 @@ import net.sourceforge.pmd.lang.modelica.resolver.ModelicaScope; */ abstract class AbstractModelicaNode extends AbstractJjtreeNode implements ModelicaNode { - /** - * Kind for implicit tokens. Negative because JavaCC only picks - * positive numbers for token kinds. - */ - private static final int IMPLICIT_TOKEN = -1; - - private ModelicaParser parser; private ModelicaScope ownScope; AbstractModelicaNode(int id) { super(id); } - AbstractModelicaNode(ModelicaParser parser, int id) { - super(id); - this.parser = parser; - } - @Override public abstract Object jjtAccept(ModelicaParserVisitor visitor, Object data); @@ -45,70 +32,6 @@ abstract class AbstractModelicaNode extends AbstractJjtreeNode imp return getClass().getSimpleName().substring(3); } - @Override - public int getBeginLine() { - return jjtGetFirstToken().getBeginLine(); - } - - @Override - public int getBeginColumn() { - return jjtGetFirstToken().getBeginColumn(); - } - - @Override - public int getEndLine() { - return jjtGetLastToken().getEndLine(); - } - - @Override - public int getEndColumn() { - return jjtGetLastToken().getEndColumn(); - } - - @Override - public void jjtClose() { - - // in jjtClose, jjtSetLastToken has not been called yet, so we use parser.token.next - if (parser.token.next == jjtGetFirstToken()) { - // Reversed, this node consumed no token. - // Forge a token with the correct coordinates, and zero length - - Token next = parser.token.next; - - Token implicit = new Token(IMPLICIT_TOKEN, ""); - implicit.beginColumn = next.beginColumn; - implicit.endColumn = next.beginColumn - 1; // because of inclusive columns.. - implicit.beginLine = next.beginLine; - implicit.endLine = next.beginLine; - - // insert it right before the next token - // as a special token - implicit.next = next; - - if (next.specialToken != null) { - next.specialToken.next = implicit; - implicit.specialToken = next.specialToken; - } - - next.specialToken = implicit; - - - // set it as both first and last - // beware, JJTree calls jjtSetLastToken after this routine.. - // hence the override below - jjtSetFirstToken(implicit); - jjtSetLastToken(implicit); - } - } - - @Override - public void jjtSetLastToken(GenericToken token) { - // don't let jjtree override tokens we've chosen - if (lastToken == null) { - super.jjtSetLastToken(token); - } - } - @Override public ModelicaScope getContainingScope() { return ((AbstractModelicaNode) parent).getMostSpecificScope(); diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ModelicaParser.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ModelicaParser.java new file mode 100644 index 0000000000..ae551e5c2e --- /dev/null +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ModelicaParser.java @@ -0,0 +1,38 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.modelica.ast; + +import java.io.Reader; + +import net.sourceforge.pmd.lang.ParserOptions; +import net.sourceforge.pmd.lang.TokenManager; +import net.sourceforge.pmd.lang.ast.CharStream; +import net.sourceforge.pmd.lang.ast.ParseException; +import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccTokenDocument; +import net.sourceforge.pmd.lang.ast.impl.javacc.JjtreeParserAdapter; + + +public class ModelicaParser extends JjtreeParserAdapter { + + public ModelicaParser(final ParserOptions parserOptions) { + super(parserOptions); + } + + @Override + protected TokenManager createTokenManager(Reader source) { + return new ModelicaTokenManager(source); + } + + @Override + protected JavaccTokenDocument newDocument(String fullText) { + return new ModelicaTokenDocument(fullText); + } + + @Override + protected ASTStoredDefinition parseImpl(CharStream cs, ParserOptions options) throws ParseException { + return new ModelicaParserImpl(cs).StoredDefinition(); + } + +} diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ModelicaParserVisitorAdapter.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ModelicaParserVisitorAdapter.java index 11afe5d8d7..e4ca918534 100644 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ModelicaParserVisitorAdapter.java +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ModelicaParserVisitorAdapter.java @@ -5,722 +5,4 @@ package net.sourceforge.pmd.lang.modelica.ast; public class ModelicaParserVisitorAdapter implements ModelicaParserVisitor { - - @Override - public Object visit(ModelicaNode node, Object data) { - for (ModelicaNode child : node.children()) { - child.jjtAccept(this, data); - } - return data; - } - - @Override - public Object visit(ASTNegated node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTStoredDefinition node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTWithinClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTClassDefinition node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTEncapsulatedClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTClassSpecifier node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTClassPrefixes node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTPartialClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTClassClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTModelClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTRecordClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTOperatorRecordClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTBlockClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTConnectorClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTExpandableConnectorClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTTypeClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTPackageClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTPureClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTImpureClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTOperatorClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTFunctionClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTOperator node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTSimpleLongClassSpecifier node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTExtendingLongClassSpecifier node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTSimpleShortClassSpecifier node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTEnumerationShortClassSpecifier node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTDerClassSpecifier node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTDerClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTBasePrefix node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTEnumList node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTEnumerationLiteral node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTComposition node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTExternalClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTLanguageSpecification node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTExternalFunctionCall node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTElementList node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTRedeclareClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTFinalClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTInnerClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTOuterClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTReplaceableClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTRegularElement node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTImportClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTRenamingImportClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTUnqualifiedImportClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTMultipleDefinitionImportClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTSingleDefinitionImportClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTImportList node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTExtendsClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTConstrainingClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTComponentClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTTypePrefix node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTFlowClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTStreamClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTDiscreteClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTParameterClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTConstantClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTInputClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTOutputClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTTypeSpecifier node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTComponentList node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTComponentDeclaration node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTConditionAttribute node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTDeclaration node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTLongModification node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTShortModification node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTAssignmentModification node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTClassModification node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTArgumentList node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTArgument node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTElementModificationOrReplaceable node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTEachClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTElementModification node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTElementRedeclaration node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTElementReplaceable node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTComponentClause1 node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTComponentDeclaration1 node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTShortClassDefinition node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTEquationSection node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTInitialClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTAlgorithmSection node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTEquation node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTRegularEquation node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTFunctionCallEquation node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTStatement node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTAssignmentStatement node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTFunctionCallStatement node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTAssignmentFromMultiResultFunctionCall node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTBreakStatement node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTReturnStatement node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTIfEquation node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTIfClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTThenClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTElseIfClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTElseClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTIfStatement node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTForEquation node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTEquationList node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTStatementList node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTForStatement node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTForIndices node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTForIndex node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTWhileStatement node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTWhenEquation node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTWhenClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTElseWhenClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTWhenStatement node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTConnectClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTIfExpression node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTSimpleExpression node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTLogicalExpression node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTLogicalTerm node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTRelation node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTRelOp node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTArithmeticExpression node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTAddOp node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTTerm node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTMulOp node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTFactor node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTFalseLiteral node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTTrueLiteral node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTFunctionInvocation node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTListOfExpressionLists node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTNumberLiteral node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTStringLiteral node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTName node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTSimpleName node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTSubscriptedName node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTComponentReference node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTFunctionCallArgs node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTFunctionArguments node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTNamedArguments node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTNamedArgument node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTFunctionArgument node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTOutputExpressionList node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTExpressionList node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTArraySubscripts node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTSubscript node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTColonSubsript node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTComment node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTStringComment node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTAnnotation node, Object data) { - return visit((ModelicaNode) node, data); - } } diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ModelicaTokenDocument.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ModelicaTokenDocument.java new file mode 100644 index 0000000000..a6485a17a7 --- /dev/null +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ModelicaTokenDocument.java @@ -0,0 +1,22 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.modelica.ast; + +import org.checkerframework.checker.nullness.qual.Nullable; + +import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccTokenDocument; + + +public class ModelicaTokenDocument extends JavaccTokenDocument { + + public ModelicaTokenDocument(String fullText) { + super(fullText); + } + + @Override + protected @Nullable String describeKindImpl(int kind) { + return ModelicaTokenKinds.describe(kind); + } +} diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ModelicaTokenManager.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ModelicaTokenManager.java similarity index 67% rename from pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ModelicaTokenManager.java rename to pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ModelicaTokenManager.java index 4d4b75ccd9..fa4becd9d5 100644 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ModelicaTokenManager.java +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/ast/ModelicaTokenManager.java @@ -1,22 +1,21 @@ -/** +/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ -package net.sourceforge.pmd.lang.modelica; +package net.sourceforge.pmd.lang.modelica.ast; import java.io.Reader; import net.sourceforge.pmd.lang.TokenManager; import net.sourceforge.pmd.lang.ast.AbstractTokenManager; import net.sourceforge.pmd.lang.ast.impl.javacc.CharStreamFactory; -import net.sourceforge.pmd.lang.modelica.ast.ModelicaParserTokenManager; public class ModelicaTokenManager implements TokenManager { - private final ModelicaParserTokenManager modelicaParserTokenManager; + private final ModelicaParserImplTokenManager modelicaParserTokenManager; public ModelicaTokenManager(final Reader source) { - modelicaParserTokenManager = new ModelicaParserTokenManager(CharStreamFactory.simpleCharStream(source)); + modelicaParserTokenManager = new ModelicaParserImplTokenManager(CharStreamFactory.simpleCharStream(source)); } @Override diff --git a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/rule/AbstractModelicaRule.java b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/rule/AbstractModelicaRule.java index 965c318e59..f8e4f830ad 100644 --- a/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/rule/AbstractModelicaRule.java +++ b/pmd-modelica/src/main/java/net/sourceforge/pmd/lang/modelica/rule/AbstractModelicaRule.java @@ -11,148 +11,7 @@ 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; -import net.sourceforge.pmd.lang.modelica.ast.ASTAlgorithmSection; -import net.sourceforge.pmd.lang.modelica.ast.ASTAnnotation; -import net.sourceforge.pmd.lang.modelica.ast.ASTArgument; -import net.sourceforge.pmd.lang.modelica.ast.ASTArgumentList; -import net.sourceforge.pmd.lang.modelica.ast.ASTArithmeticExpression; -import net.sourceforge.pmd.lang.modelica.ast.ASTArraySubscripts; -import net.sourceforge.pmd.lang.modelica.ast.ASTAssignmentFromMultiResultFunctionCall; -import net.sourceforge.pmd.lang.modelica.ast.ASTAssignmentModification; -import net.sourceforge.pmd.lang.modelica.ast.ASTAssignmentStatement; -import net.sourceforge.pmd.lang.modelica.ast.ASTBasePrefix; -import net.sourceforge.pmd.lang.modelica.ast.ASTBlockClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTBreakStatement; -import net.sourceforge.pmd.lang.modelica.ast.ASTClassClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTClassDefinition; -import net.sourceforge.pmd.lang.modelica.ast.ASTClassModification; -import net.sourceforge.pmd.lang.modelica.ast.ASTClassPrefixes; -import net.sourceforge.pmd.lang.modelica.ast.ASTClassSpecifier; -import net.sourceforge.pmd.lang.modelica.ast.ASTColonSubsript; -import net.sourceforge.pmd.lang.modelica.ast.ASTComment; -import net.sourceforge.pmd.lang.modelica.ast.ASTComponentClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTComponentClause1; -import net.sourceforge.pmd.lang.modelica.ast.ASTComponentDeclaration; -import net.sourceforge.pmd.lang.modelica.ast.ASTComponentDeclaration1; -import net.sourceforge.pmd.lang.modelica.ast.ASTComponentList; -import net.sourceforge.pmd.lang.modelica.ast.ASTComponentReference; -import net.sourceforge.pmd.lang.modelica.ast.ASTComposition; -import net.sourceforge.pmd.lang.modelica.ast.ASTConditionAttribute; -import net.sourceforge.pmd.lang.modelica.ast.ASTConnectClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTConnectorClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTConstantClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTConstrainingClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTDeclaration; -import net.sourceforge.pmd.lang.modelica.ast.ASTDerClassSpecifier; -import net.sourceforge.pmd.lang.modelica.ast.ASTDerClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTDiscreteClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTEachClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTElementList; -import net.sourceforge.pmd.lang.modelica.ast.ASTElementModification; -import net.sourceforge.pmd.lang.modelica.ast.ASTElementModificationOrReplaceable; -import net.sourceforge.pmd.lang.modelica.ast.ASTElementRedeclaration; -import net.sourceforge.pmd.lang.modelica.ast.ASTElementReplaceable; -import net.sourceforge.pmd.lang.modelica.ast.ASTElseClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTElseIfClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTElseWhenClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTEncapsulatedClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTEnumList; -import net.sourceforge.pmd.lang.modelica.ast.ASTEnumerationLiteral; -import net.sourceforge.pmd.lang.modelica.ast.ASTEnumerationShortClassSpecifier; -import net.sourceforge.pmd.lang.modelica.ast.ASTEquation; -import net.sourceforge.pmd.lang.modelica.ast.ASTEquationList; -import net.sourceforge.pmd.lang.modelica.ast.ASTEquationSection; -import net.sourceforge.pmd.lang.modelica.ast.ASTExpandableConnectorClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTExpressionList; -import net.sourceforge.pmd.lang.modelica.ast.ASTExtendingLongClassSpecifier; -import net.sourceforge.pmd.lang.modelica.ast.ASTExtendsClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTExternalClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTExternalFunctionCall; -import net.sourceforge.pmd.lang.modelica.ast.ASTFactor; -import net.sourceforge.pmd.lang.modelica.ast.ASTFalseLiteral; -import net.sourceforge.pmd.lang.modelica.ast.ASTFinalClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTFlowClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTForEquation; -import net.sourceforge.pmd.lang.modelica.ast.ASTForIndex; -import net.sourceforge.pmd.lang.modelica.ast.ASTForIndices; -import net.sourceforge.pmd.lang.modelica.ast.ASTForStatement; -import net.sourceforge.pmd.lang.modelica.ast.ASTFunctionArgument; -import net.sourceforge.pmd.lang.modelica.ast.ASTFunctionArguments; -import net.sourceforge.pmd.lang.modelica.ast.ASTFunctionCallArgs; -import net.sourceforge.pmd.lang.modelica.ast.ASTFunctionCallEquation; -import net.sourceforge.pmd.lang.modelica.ast.ASTFunctionCallStatement; -import net.sourceforge.pmd.lang.modelica.ast.ASTFunctionClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTFunctionInvocation; -import net.sourceforge.pmd.lang.modelica.ast.ASTIfClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTIfEquation; -import net.sourceforge.pmd.lang.modelica.ast.ASTIfExpression; -import net.sourceforge.pmd.lang.modelica.ast.ASTIfStatement; -import net.sourceforge.pmd.lang.modelica.ast.ASTImportClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTImportList; -import net.sourceforge.pmd.lang.modelica.ast.ASTImpureClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTInitialClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTInnerClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTInputClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTLanguageSpecification; -import net.sourceforge.pmd.lang.modelica.ast.ASTListOfExpressionLists; -import net.sourceforge.pmd.lang.modelica.ast.ASTLogicalExpression; -import net.sourceforge.pmd.lang.modelica.ast.ASTLogicalTerm; -import net.sourceforge.pmd.lang.modelica.ast.ASTLongModification; -import net.sourceforge.pmd.lang.modelica.ast.ASTModelClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTMulOp; -import net.sourceforge.pmd.lang.modelica.ast.ASTMultipleDefinitionImportClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTName; -import net.sourceforge.pmd.lang.modelica.ast.ASTNamedArgument; -import net.sourceforge.pmd.lang.modelica.ast.ASTNamedArguments; -import net.sourceforge.pmd.lang.modelica.ast.ASTNegated; -import net.sourceforge.pmd.lang.modelica.ast.ASTNumberLiteral; -import net.sourceforge.pmd.lang.modelica.ast.ASTOperator; -import net.sourceforge.pmd.lang.modelica.ast.ASTOperatorClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTOperatorRecordClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTOuterClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTOutputClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTOutputExpressionList; -import net.sourceforge.pmd.lang.modelica.ast.ASTPackageClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTParameterClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTPartialClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTPureClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTRecordClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTRedeclareClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTRegularElement; -import net.sourceforge.pmd.lang.modelica.ast.ASTRegularEquation; -import net.sourceforge.pmd.lang.modelica.ast.ASTRelOp; -import net.sourceforge.pmd.lang.modelica.ast.ASTRelation; -import net.sourceforge.pmd.lang.modelica.ast.ASTRenamingImportClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTReplaceableClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTReturnStatement; -import net.sourceforge.pmd.lang.modelica.ast.ASTShortClassDefinition; -import net.sourceforge.pmd.lang.modelica.ast.ASTShortModification; -import net.sourceforge.pmd.lang.modelica.ast.ASTSimpleExpression; -import net.sourceforge.pmd.lang.modelica.ast.ASTSimpleLongClassSpecifier; -import net.sourceforge.pmd.lang.modelica.ast.ASTSimpleName; -import net.sourceforge.pmd.lang.modelica.ast.ASTSimpleShortClassSpecifier; -import net.sourceforge.pmd.lang.modelica.ast.ASTSingleDefinitionImportClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTStatement; -import net.sourceforge.pmd.lang.modelica.ast.ASTStatementList; import net.sourceforge.pmd.lang.modelica.ast.ASTStoredDefinition; -import net.sourceforge.pmd.lang.modelica.ast.ASTStreamClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTStringComment; -import net.sourceforge.pmd.lang.modelica.ast.ASTStringLiteral; -import net.sourceforge.pmd.lang.modelica.ast.ASTSubscript; -import net.sourceforge.pmd.lang.modelica.ast.ASTSubscriptedName; -import net.sourceforge.pmd.lang.modelica.ast.ASTTerm; -import net.sourceforge.pmd.lang.modelica.ast.ASTThenClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTTrueLiteral; -import net.sourceforge.pmd.lang.modelica.ast.ASTTypeClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTTypePrefix; -import net.sourceforge.pmd.lang.modelica.ast.ASTTypeSpecifier; -import net.sourceforge.pmd.lang.modelica.ast.ASTUnqualifiedImportClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTWhenClause; -import net.sourceforge.pmd.lang.modelica.ast.ASTWhenEquation; -import net.sourceforge.pmd.lang.modelica.ast.ASTWhenStatement; -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; @@ -195,713 +54,4 @@ public abstract class AbstractModelicaRule extends AbstractRule implements Model return true; } - @Override - public Object visit(ASTNegated node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTStoredDefinition node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTWithinClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTClassDefinition node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTEncapsulatedClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTClassSpecifier node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTClassPrefixes node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTPartialClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTClassClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTModelClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTRecordClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTOperatorRecordClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTBlockClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTConnectorClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTExpandableConnectorClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTTypeClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTPackageClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTPureClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTImpureClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTOperatorClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTFunctionClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTOperator node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTSimpleLongClassSpecifier node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTExtendingLongClassSpecifier node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTSimpleShortClassSpecifier node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTEnumerationShortClassSpecifier node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTDerClassSpecifier node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTDerClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTBasePrefix node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTEnumList node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTEnumerationLiteral node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTComposition node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTExternalClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTLanguageSpecification node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTExternalFunctionCall node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTElementList node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTRedeclareClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTFinalClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTInnerClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTOuterClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTReplaceableClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTRegularElement node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTImportClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTRenamingImportClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTUnqualifiedImportClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTMultipleDefinitionImportClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTSingleDefinitionImportClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTImportList node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTExtendsClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTConstrainingClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTComponentClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTTypePrefix node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTFlowClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTStreamClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTDiscreteClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTParameterClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTConstantClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTInputClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTOutputClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTTypeSpecifier node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTComponentList node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTComponentDeclaration node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTConditionAttribute node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTDeclaration node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTLongModification node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTShortModification node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTAssignmentModification node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTClassModification node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTArgumentList node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTArgument node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTElementModificationOrReplaceable node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTEachClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTElementModification node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTElementRedeclaration node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTElementReplaceable node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTComponentClause1 node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTComponentDeclaration1 node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTShortClassDefinition node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTEquationSection node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTInitialClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTAlgorithmSection node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTEquation node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTRegularEquation node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTFunctionCallEquation node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTStatement node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTAssignmentStatement node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTFunctionCallStatement node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTAssignmentFromMultiResultFunctionCall node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTBreakStatement node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTReturnStatement node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTIfEquation node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTIfClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTThenClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTElseIfClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTElseClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTIfStatement node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTForEquation node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTEquationList node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTStatementList node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTForStatement node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTForIndices node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTForIndex node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTWhileStatement node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTWhenEquation node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTWhenClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTElseWhenClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTWhenStatement node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTConnectClause node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTIfExpression node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTSimpleExpression node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTLogicalExpression node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTLogicalTerm node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTRelation node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTRelOp node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTArithmeticExpression node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTAddOp node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTTerm node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTMulOp node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTFactor node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTFalseLiteral node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTTrueLiteral node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTFunctionInvocation node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTListOfExpressionLists node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTNumberLiteral node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTStringLiteral node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTName node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTSimpleName node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTSubscriptedName node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTComponentReference node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTFunctionCallArgs node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTFunctionArguments node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTNamedArguments node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTNamedArgument node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTFunctionArgument node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTOutputExpressionList node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTExpressionList node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTArraySubscripts node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTSubscript node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTColonSubsript node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTComment node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTStringComment node, Object data) { - return visit((ModelicaNode) node, data); - } - - @Override - public Object visit(ASTAnnotation node, Object data) { - return visit((ModelicaNode) node, data); - } } diff --git a/pmd-modelica/src/test/kotlin/net/sourceforge/pmd/lang/modelica/ast/ModelicaCoordsTest.kt b/pmd-modelica/src/test/kotlin/net/sourceforge/pmd/lang/modelica/ast/ModelicaCoordsTest.kt index 798e24cec7..4b8ee9f7c0 100644 --- a/pmd-modelica/src/test/kotlin/net/sourceforge/pmd/lang/modelica/ast/ModelicaCoordsTest.kt +++ b/pmd-modelica/src/test/kotlin/net/sourceforge/pmd/lang/modelica/ast/ModelicaCoordsTest.kt @@ -8,6 +8,8 @@ import io.kotlintest.should import io.kotlintest.shouldBe import io.kotlintest.specs.FunSpec import net.sourceforge.pmd.lang.ast.Node +import net.sourceforge.pmd.lang.ast.test.firstToken +import net.sourceforge.pmd.lang.ast.test.lastToken import net.sourceforge.pmd.lang.ast.test.matchNode import net.sourceforge.pmd.lang.ast.test.shouldBe import net.sourceforge.pmd.lang.modelica.ModelicaParsingHelper @@ -24,72 +26,113 @@ package TestPackage end TestPackage; """.trim().parseModelica() should matchNode { - it.assertBounds(1, 1, 4, 16) + it::getText shouldBe """package TestPackage + package EmptyPackage + end EmptyPackage; +end TestPackage;""" + + it.assertBounds(1, 1, 4, 17) child { - it.assertBounds(1, 1, 4, 15) + it::getText shouldBe """package TestPackage + package EmptyPackage + end EmptyPackage; +end TestPackage""" + it.assertBounds(1, 1, 4, 16) child { - it.assertBounds(1, 1, 1, 7) + it::getText shouldBe "package" + it.assertBounds(1, 1, 1, 8) child { - it.assertBounds(1, 1, 1, 7) + it::getText shouldBe "package" + it.assertBounds(1, 1, 1, 8) } } child { - it.assertBounds(1, 9, 4, 15) + it::getText shouldBe """TestPackage + package EmptyPackage + end EmptyPackage; +end TestPackage""" + it.assertBounds(1, 9, 4, 16) child { - it.assertBounds(1, 9, 4, 15) + it::getText shouldBe """TestPackage + package EmptyPackage + end EmptyPackage; +end TestPackage""" + + it.assertBounds(1, 9, 4, 16) child { - it.assertBounds(1, 9, 1, 19) + it::getText shouldBe "TestPackage" + it.assertBounds(1, 9, 1, 20) } child { - it.assertBounds(2, 3, 3, 19) + it::getText shouldBe """package EmptyPackage + end EmptyPackage;""" + it.assertBounds(2, 3, 3, 20) child { - it.assertBounds(2, 3, 3, 19) + it::getText shouldBe """package EmptyPackage + end EmptyPackage;""" + it.assertBounds(2, 3, 3, 20) child { - it.assertBounds(2, 3, 3, 18) + it::getText shouldBe """package EmptyPackage + end EmptyPackage""" + it.assertBounds(2, 3, 3, 19) child { - it.assertBounds(2, 3, 3, 18) + it::getText shouldBe """package EmptyPackage + end EmptyPackage""" + it.assertBounds(2, 3, 3, 19) it.isPartial shouldBe false child { - it.assertBounds(2, 3, 2, 9) + it::getText shouldBe "package" + it.assertBounds(2, 3, 2, 10) child { - it.assertBounds(2, 3, 2, 9) + it::getText shouldBe "package" + it.assertBounds(2, 3, 2, 10) } } child { - it.assertBounds(2, 11, 3, 18) + it::getText shouldBe """EmptyPackage + end EmptyPackage""" + it.assertBounds(2, 11, 3, 19) child { - it.assertBounds(2, 11, 3, 18) + it::getText shouldBe """EmptyPackage + end EmptyPackage""" + it.assertBounds(2, 11, 3, 19) it.simpleClassName shouldBe "EmptyPackage" child { - it.assertBounds(2, 11, 2, 22) + it::getText shouldBe "EmptyPackage" + it.assertBounds(2, 11, 2, 23) } child { + it::getText shouldBe "" + it.firstToken::isImplicit shouldBe true + it.lastToken shouldBe it.firstToken - it.assertBounds(3, 3, 3, 2) + it.assertBounds(3, 3, 3, 3) child { - /* - This ElementList is empty and has no explicit token. - */ + it::getText shouldBe "" + it.firstToken::isImplicit shouldBe true + it.lastToken shouldBe it.firstToken - it.assertBounds(3, 3, 3, 2) + it.assertBounds(3, 3, 3, 3) } } child { - it.assertBounds(3, 7, 3, 18) + it::getText shouldBe "EmptyPackage" + it::getImage shouldBe "EmptyPackage" + it.assertBounds(3, 7, 3, 19) } } } @@ -98,7 +141,8 @@ end TestPackage; } } child { - it.assertBounds(4, 5, 4, 15) + it::getText shouldBe "TestPackage" + it.assertBounds(4, 5, 4, 16) } } } diff --git a/pmd-plsql/etc/grammar/PldocAST.jjt b/pmd-plsql/etc/grammar/PldocAST.jjt index bf99e5a152..dd1a27c92f 100644 --- a/pmd-plsql/etc/grammar/PldocAST.jjt +++ b/pmd-plsql/etc/grammar/PldocAST.jjt @@ -121,14 +121,14 @@ options { SANITY_CHECK = true; FORCE_LA_CHECK = false; - CACHE_TOKENS = true; + TRACK_TOKENS = true; + CACHE_TOKENS = true; - - MULTI = true; - VISITOR = true; - NODE_USES_PARSER = true; - NODE_PACKAGE="net.sourceforge.pmd.lang.plsql.ast"; - NODE_CLASS = "net.sourceforge.pmd.lang.plsql.ast.AbstractPLSQLNode"; + MULTI = true; + VISITOR = true; + NODE_USES_PARSER = true; + NODE_PACKAGE="net.sourceforge.pmd.lang.plsql.ast"; + NODE_CLASS = "net.sourceforge.pmd.lang.plsql.ast.AbstractPLSQLNode"; } PARSER_BEGIN(PLSQLParser) diff --git a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/ast/AbstractPLSQLNode.java b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/ast/AbstractPLSQLNode.java index def82d1a5c..9a7e8aa671 100644 --- a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/ast/AbstractPLSQLNode.java +++ b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/ast/AbstractPLSQLNode.java @@ -87,6 +87,11 @@ public abstract class AbstractPLSQLNode extends AbstractJjtreeNode im return prefix + toString(); } + @Override + public String toString() { + return getXPathNodeName(); + } + /* * Override this method if you want to customize how the node dumps out its * children. diff --git a/pmd-plsql/src/test/java/net/sourceforge/pmd/lang/plsql/dfa/StatementAndBraceFinderTest.java b/pmd-plsql/src/test/java/net/sourceforge/pmd/lang/plsql/dfa/StatementAndBraceFinderTest.java index d75ce4985b..ab3c11c597 100644 --- a/pmd-plsql/src/test/java/net/sourceforge/pmd/lang/plsql/dfa/StatementAndBraceFinderTest.java +++ b/pmd-plsql/src/test/java/net/sourceforge/pmd/lang/plsql/dfa/StatementAndBraceFinderTest.java @@ -12,17 +12,14 @@ import java.util.List; import org.junit.Test; import net.sourceforge.pmd.PMD; -import net.sourceforge.pmd.lang.ast.AbstractNode; import net.sourceforge.pmd.lang.dfa.DataFlowNode; import net.sourceforge.pmd.lang.dfa.NodeType; import net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode; import net.sourceforge.pmd.lang.plsql.AbstractPLSQLParserTst; import net.sourceforge.pmd.lang.plsql.PlsqlParsingHelper; import net.sourceforge.pmd.lang.plsql.ast.ASTExpression; -import net.sourceforge.pmd.lang.plsql.ast.ASTMethodDeclaration; import net.sourceforge.pmd.lang.plsql.ast.ASTProgramUnit; import net.sourceforge.pmd.lang.plsql.ast.ASTVariableOrConstantDeclarator; -import net.sourceforge.pmd.lang.plsql.ast.PLSQLNode; public class StatementAndBraceFinderTest extends AbstractPLSQLParserTst { @@ -190,17 +187,6 @@ public class StatementAndBraceFinderTest extends AbstractPLSQLParserTst { assertTrue(dfn.isType(NodeType.LABEL_STATEMENT)); } - @Test - public void testOnlyWorksForMethodsAndConstructors() { - StatementAndBraceFinder sbf = new StatementAndBraceFinder(plsql.getDefaultHandler().getDataFlowHandler()); - PLSQLNode node = new ASTMethodDeclaration(1); - ((AbstractNode) node).testingOnlySetBeginColumn(1); - sbf.buildDataFlowFor(node); - // sbf.buildDataFlowFor(new ASTConstructorDeclaration(1)); - node = new ASTProgramUnit(1); - ((AbstractNode) node).testingOnlySetBeginColumn(1); - sbf.buildDataFlowFor(node); - } private static final String TEST1 = "CREATE OR REPLACE PACKAGE BODY Foo AS" + PMD.EOL + " PROCEDURE bar IS BEGIN" + PMD.EOL + " x := 2;" + PMD.EOL + " END bar;" + PMD.EOL + "END foo;"; diff --git a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/AbstractVmNode.java b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/AbstractVmNode.java index 328ead1059..a3b9729fee 100644 --- a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/AbstractVmNode.java +++ b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/AbstractVmNode.java @@ -25,7 +25,6 @@ import java.io.Writer; import org.apache.commons.lang3.text.StrBuilder; -import net.sourceforge.pmd.annotation.InternalApi; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.ast.impl.javacc.AbstractJjtreeNode; @@ -48,12 +47,6 @@ public class AbstractVmNode extends AbstractJjtreeNode implements VmNode /** */ protected boolean invalid = false; - /** */ - protected Token first; - - /** */ - protected Token last; - protected String templateName; /** @@ -75,7 +68,7 @@ public class AbstractVmNode extends AbstractJjtreeNode implements VmNode @Override public void jjtOpen() { - first = parser.getToken(1); // added + firstToken = parser.getToken(1); // added if (beginLine == -1 && parser.token.next != null) { beginLine = parser.token.next.beginLine; beginColumn = parser.token.next.beginColumn; @@ -84,7 +77,7 @@ public class AbstractVmNode extends AbstractJjtreeNode implements VmNode @Override public void jjtClose() { - last = parser.getToken(0); // added + lastToken = parser.getToken(0); // added if (beginLine == -1 && (children == null || children.length == 0)) { beginColumn = parser.token.beginColumn; } @@ -95,18 +88,12 @@ public class AbstractVmNode extends AbstractJjtreeNode implements VmNode endColumn = parser.token.endColumn; } - @InternalApi - @Deprecated - public void setFirstToken(final Token t) { - this.first = t; - } - public Token getFirstToken() { - return first; + return (Token) firstToken; } public Token getLastToken() { - return last; + return (Token) lastToken; } @Override @@ -175,13 +162,13 @@ public class AbstractVmNode extends AbstractJjtreeNode implements VmNode public String literal() { // if we have only one string, just return it and avoid // buffer allocation. VELOCITY-606 - if (first != null && first.equals(last)) { - return NodeUtils.tokenLiteral(first); + if (getFirstToken() != null && firstToken.equals(getLastToken())) { + return NodeUtils.tokenLiteral(getFirstToken()); } - Token t = first; + Token t = getFirstToken(); final StrBuilder sb = new StrBuilder(NodeUtils.tokenLiteral(t)); - while (t != null && !t.equals(last)) { + while (t != null && !t.equals(getLastToken())) { t = t.next; sb.append(NodeUtils.tokenLiteral(t)); } @@ -227,14 +214,14 @@ public class AbstractVmNode extends AbstractJjtreeNode implements VmNode * see org.apache.velocity.runtime.parser.node.Node#getLine() */ public int getLine() { - return first.beginLine; + return getBeginLine(); } /* * see org.apache.velocity.runtime.parser.node.Node#getColumn() */ public int getColumn() { - return first.beginColumn; + return getBeginColumn(); } public String getTemplateName() { diff --git a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/rule/bestpractices/AvoidReassigningParametersRule.java b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/rule/bestpractices/AvoidReassigningParametersRule.java index ee71e7b287..f7ce42fedf 100644 --- a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/rule/bestpractices/AvoidReassigningParametersRule.java +++ b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/rule/bestpractices/AvoidReassigningParametersRule.java @@ -21,13 +21,13 @@ public class AvoidReassigningParametersRule extends AbstractVmRule { final Set paramNames = new HashSet<>(); final List params = node.findChildrenOfType(ASTReference.class); for (final ASTReference param : params) { - paramNames.add(param.getFirstToken().toString()); + paramNames.add(param.getFirstToken().getImage()); } final List assignments = node.findDescendantsOfType(ASTSetDirective.class); for (final ASTSetDirective assignment : assignments) { final ASTReference ref = assignment.getFirstChildOfType(ASTReference.class); - if (ref != null && paramNames.contains(ref.getFirstToken().toString())) { - addViolation(data, node, ref.getFirstToken().toString()); + if (ref != null && paramNames.contains(ref.getFirstToken().getImage())) { + addViolation(data, node, ref.getFirstToken().getImage()); } } } diff --git a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/rule/design/CollapsibleIfStatementsRule.java b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/rule/design/CollapsibleIfStatementsRule.java index 38a9e3ba85..156e3c61f7 100644 --- a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/rule/design/CollapsibleIfStatementsRule.java +++ b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/rule/design/CollapsibleIfStatementsRule.java @@ -41,7 +41,7 @@ public class CollapsibleIfStatementsRule extends AbstractVmRule { for (int i = 0; i < ifBlock.getNumChildren(); i++) { final Node blockChild = ifBlock.getChild(i); if (blockChild instanceof ASTText) { - if (StringUtils.isNotBlank(((ASTText) blockChild).getFirstToken().toString())) { + if (StringUtils.isNotBlank(((ASTText) blockChild).getFirstToken().getImage())) { violationFound = false; break; } diff --git a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/rule/errorprone/EmptyForeachStmtRule.java b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/rule/errorprone/EmptyForeachStmtRule.java index 00186d6d21..90b0398c5f 100644 --- a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/rule/errorprone/EmptyForeachStmtRule.java +++ b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/rule/errorprone/EmptyForeachStmtRule.java @@ -20,7 +20,7 @@ public class EmptyForeachStmtRule extends AbstractVmRule { if (block.getNumChildren() == 0) { addViolation(data, node); } else if (block.getNumChildren() == 1 && block.getChild(0) instanceof ASTText - && StringUtils.isBlank(((AbstractVmNode) block.getChild(0)).getFirstToken().toString())) { + && StringUtils.isBlank(((AbstractVmNode) block.getChild(0)).getFirstToken().getImage())) { addViolation(data, node); } return super.visit(node, data); diff --git a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/rule/errorprone/EmptyIfStmtRule.java b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/rule/errorprone/EmptyIfStmtRule.java index 2d0a45d402..d806481180 100644 --- a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/rule/errorprone/EmptyIfStmtRule.java +++ b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/rule/errorprone/EmptyIfStmtRule.java @@ -38,7 +38,7 @@ public class EmptyIfStmtRule extends AbstractVmRule { if (block.getNumChildren() == 0) { addViolation(data, node); } else if (block.getNumChildren() == 1 && block.getChild(0) instanceof ASTText - && StringUtils.isBlank(((AbstractVmNode) block.getChild(0)).getFirstToken().toString())) { + && StringUtils.isBlank(((AbstractVmNode) block.getChild(0)).getFirstToken().getImage())) { addViolation(data, node); } } diff --git a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/util/VelocityCharStream.java b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/util/VelocityCharStream.java index ed4e79ec23..00178f8d51 100644 --- a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/util/VelocityCharStream.java +++ b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/util/VelocityCharStream.java @@ -268,24 +268,6 @@ public final class VelocityCharStream implements CharStream { return c; } - /** - * @deprecated - */ - @Deprecated - @Override - public int getColumn() { - return bufcolumn[bufpos]; - } - - /** - * @deprecated - */ - @Deprecated - @Override - public int getLine() { - return bufline[bufpos]; - } - @Override public int getEndColumn() { return bufcolumn[bufpos]; @@ -394,12 +376,6 @@ public final class VelocityCharStream implements CharStream { return ret; } - @Override - public void Done() { - buffer = null; - bufline = null; - bufcolumn = null; - } /** * Method to adjust line and column numbers for the start of a token.
    diff --git a/pom.xml b/pom.xml index a626e35c32..bdf249fc06 100644 --- a/pom.xml +++ b/pom.xml @@ -108,6 +108,8 @@ 6.21.0 ${settings.localRepository}/net/java/dev/javacc/javacc/${javacc.version}/javacc-${javacc.version}.jar + ${project.build.directory}/generated-sources/javacc + ${project.basedir}/../javacc-wrapper.xml @@ -366,12 +368,12 @@ net.sourceforge.pmd pmd-core - 6.19.0 + 6.21.0 net.sourceforge.pmd pmd-java - 6.19.0 + 6.21.0