Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cs] CPD: Replace C# tokenizer by an Antlr-based one #2280

Merged
merged 3 commits into from
Feb 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

package net.sourceforge.pmd.cpd.token.internal;

import static net.sourceforge.pmd.internal.util.IteratorUtil.AbstractIterator;

import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.LinkedList;

import net.sourceforge.pmd.cpd.token.TokenFilter;
import net.sourceforge.pmd.lang.TokenManager;
import net.sourceforge.pmd.lang.ast.GenericToken;
Expand All @@ -15,21 +21,32 @@
public abstract class BaseTokenFilter<T extends GenericToken> implements TokenFilter {

private final TokenManager tokenManager;
private final LinkedList<T> unprocessedTokens; // NOPMD - used both as Queue and List
private final Iterable<T> remainingTokens;
private boolean discardingSuppressing;
private T currentToken;

/**
* Creates a new BaseTokenFilter
* @param tokenManager The token manager from which to retrieve tokens to be filtered
*/
public BaseTokenFilter(final TokenManager tokenManager) {
this.tokenManager = tokenManager;
this.unprocessedTokens = new LinkedList<>();
this.remainingTokens = new RemainingTokens();
}

@Override
public final T getNextToken() {
T currentToken = (T) tokenManager.getNextToken();
currentToken = null;
if (!unprocessedTokens.isEmpty()) {
currentToken = unprocessedTokens.poll();
return currentToken;
}
currentToken = (T) tokenManager.getNextToken();
while (!shouldStopProcessing(currentToken)) {
analyzeToken(currentToken);
analyzeTokens(currentToken, remainingTokens);
processCPDSuppression(currentToken);

if (!isDiscarding()) {
Expand Down Expand Up @@ -73,6 +90,18 @@ protected void analyzeToken(final T currentToken) {
// noop
}

/**
* Extension point for subclasses to analyze all tokens (before filtering)
* and update internal status to decide on custom discard rules.
*
* @param currentToken The token to be analyzed
* @param remainingTokens All upcoming tokens
* @see #isLanguageSpecificDiscarding()
*/
protected void analyzeTokens(final T currentToken, final Iterable<T> remainingTokens) {
// noop
}

/**
* Extension point for subclasses to indicate tokens are to be filtered.
*
Expand All @@ -90,4 +119,43 @@ protected boolean isLanguageSpecificDiscarding() {
*/
protected abstract boolean shouldStopProcessing(T currentToken);

private class RemainingTokens implements Iterable<T> {

@Override
public Iterator<T> iterator() {
return new RemainingTokensIterator(currentToken);
}

private class RemainingTokensIterator extends AbstractIterator<T> implements Iterator<T> {

int index = 0; // index of next element
T startToken;

RemainingTokensIterator(final T startToken) {
this.startToken = startToken;
}

@Override
protected void computeNext() {
assert index >= 0;
if (startToken != currentToken) { // NOPMD - intentional check for reference equality
throw new ConcurrentModificationException("Using iterator after next token has been requested.");
}
if (index < unprocessedTokens.size()) {
setNext(unprocessedTokens.get(index++));
} else {
final T nextToken = (T) tokenManager.getNextToken();
if (shouldStopProcessing(nextToken)) {
done();
return;
}
index++;
unprocessedTokens.add(nextToken);
setNext(nextToken);
}
}

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;


/**
Expand Down Expand Up @@ -88,4 +89,59 @@ public void remove() {
}
};
}

public abstract static class AbstractIterator<T> implements Iterator<T> {

private State state = State.NOT_READY;
private T next = null;


@Override
public boolean hasNext() {
switch (state) {
case DONE:
return false;
case READY:
return true;
default:
state = null;
computeNext();
if (state == null) {
throw new IllegalStateException("Should have called done or setNext");
}
return state == State.READY;
}
}

@Override
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
state = State.NOT_READY;
return next;
}

protected final void setNext(T t) {
next = t;
state = State.READY;
}

protected final void done() {
state = State.DONE;
}

protected abstract void computeNext();

enum State {
READY, NOT_READY, DONE
}

@Deprecated
@Override
public final void remove() {
throw new UnsupportedOperationException();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/

package net.sourceforge.pmd.cpd.token.internal;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;

import org.junit.Test;

import net.sourceforge.pmd.lang.TokenManager;
import net.sourceforge.pmd.lang.ast.GenericToken;

public class BaseTokenFilterTest {

class StringToken implements GenericToken {

private final String text;

StringToken(final String text) {
this.text = text;
}

@Override
public GenericToken getNext() {
return null;
}

@Override
public GenericToken getPreviousComment() {
return null;
}

@Override
public String getImage() {
return text;
}

@Override
public int getBeginLine() {
return 0;
}

@Override
public int getEndLine() {
return 0;
}

@Override
public int getBeginColumn() {
return 0;
}

@Override
public int getEndColumn() {
return 0;
}
}

class StringTokenManager implements TokenManager {

Iterator<String> iterator = Collections.unmodifiableList(Arrays.asList("a", "b", "c")).iterator();

@Override
public Object getNextToken() {
if (iterator.hasNext()) {
return new StringToken(iterator.next());
} else {
return null;
}
}

@Override
public void setFileName(final String fileName) {
}
}

class DummyTokenFilter<T extends GenericToken> extends BaseTokenFilter<T> {

Iterable<T> remainingTokens;

DummyTokenFilter(final TokenManager tokenManager) {
super(tokenManager);
}

@Override
protected boolean shouldStopProcessing(final T currentToken) {
return currentToken == null;
}

@Override
protected void analyzeTokens(final T currentToken, final Iterable<T> remainingTokens) {
this.remainingTokens = remainingTokens;
}

public Iterable getRemainingTokens() {
return remainingTokens;
}
}

@Test
public void testRemainingTokensFunctionality1() {
final TokenManager tokenManager = new StringTokenManager();
final DummyTokenFilter tokenFilter = new DummyTokenFilter(tokenManager);
final GenericToken firstToken = tokenFilter.getNextToken();
assertEquals("a", firstToken.getImage());
final Iterable<StringToken> iterable = tokenFilter.getRemainingTokens();
final Iterator it1 = iterable.iterator();
final Iterator it2 = iterable.iterator();
assertTrue(it1.hasNext());
assertTrue(it2.hasNext());
final StringToken firstValFirstIt = (StringToken) it1.next();
final StringToken firstValSecondIt = (StringToken) it2.next();
assertTrue(it1.hasNext());
assertTrue(it2.hasNext());
final StringToken secondValFirstIt = (StringToken) it1.next();
assertFalse(it1.hasNext());
assertTrue(it2.hasNext());
final StringToken secondValSecondIt = (StringToken) it2.next();
assertFalse(it2.hasNext());
assertEquals("b", firstValFirstIt.getImage());
assertEquals("b", firstValSecondIt.getImage());
assertEquals("c", secondValFirstIt.getImage());
assertEquals("c", secondValSecondIt.getImage());
}

@Test
public void testRemainingTokensFunctionality2() {
final TokenManager tokenManager = new StringTokenManager();
final DummyTokenFilter tokenFilter = new DummyTokenFilter(tokenManager);
final GenericToken firstToken = tokenFilter.getNextToken();
assertEquals("a", firstToken.getImage());
final Iterable<StringToken> iterable = tokenFilter.getRemainingTokens();
final Iterator it1 = iterable.iterator();
final Iterator it2 = iterable.iterator();
assertTrue(it1.hasNext());
assertTrue(it2.hasNext());
final StringToken firstValFirstIt = (StringToken) it1.next();
assertTrue(it1.hasNext());
final StringToken secondValFirstIt = (StringToken) it1.next();
assertFalse(it1.hasNext());
assertTrue(it2.hasNext());
final StringToken firstValSecondIt = (StringToken) it2.next();
assertTrue(it2.hasNext());
final StringToken secondValSecondIt = (StringToken) it2.next();
assertFalse(it2.hasNext());
assertEquals("b", firstValFirstIt.getImage());
assertEquals("b", firstValSecondIt.getImage());
assertEquals("c", secondValFirstIt.getImage());
assertEquals("c", secondValSecondIt.getImage());
}

@Test(expected = NoSuchElementException.class)
public void testRemainingTokensFunctionality3() {
final TokenManager tokenManager = new StringTokenManager();
final DummyTokenFilter tokenFilter = new DummyTokenFilter(tokenManager);
final GenericToken firstToken = tokenFilter.getNextToken();
assertEquals("a", firstToken.getImage());
final Iterable<StringToken> iterable = tokenFilter.getRemainingTokens();
final Iterator it1 = iterable.iterator();
final Iterator it2 = iterable.iterator();
it1.next();
it1.next();
it2.next();
it2.next();
it1.next();
}

@Test(expected = ConcurrentModificationException.class)
public void testRemainingTokensFunctionality4() {
final TokenManager tokenManager = new StringTokenManager();
final DummyTokenFilter tokenFilter = new DummyTokenFilter(tokenManager);
final GenericToken firstToken = tokenFilter.getNextToken();
assertEquals("a", firstToken.getImage());
final Iterable<StringToken> iterable = tokenFilter.getRemainingTokens();
final Iterator it1 = iterable.iterator();
final GenericToken secondToken = tokenFilter.getNextToken();
assertEquals("b", secondToken.getImage());
it1.next();
}

}
10 changes: 6 additions & 4 deletions pmd-cs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

<build>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
</plugin>

<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
Expand All @@ -23,6 +28,7 @@
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
Expand All @@ -32,10 +38,6 @@
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
Expand Down
Loading