Merge branch 'analysis-listener' into text-utils-simple

This commit is contained in:
Clément Fournier
2020-12-13 05:35:13 +01:00
452 changed files with 9061 additions and 7710 deletions

View File

@ -21,17 +21,21 @@ import net.sourceforge.pmd.lang.cs.ast.CSharpLexer;
public class CsTokenizer extends AntlrTokenizer {
private boolean ignoreUsings = false;
private boolean ignoreLiteralSequences = false;
public void setProperties(Properties properties) {
if (properties.containsKey(IGNORE_USINGS)) {
ignoreUsings = Boolean.parseBoolean(properties.getProperty(IGNORE_USINGS, "false"));
}
ignoreUsings = Boolean.parseBoolean(properties.getProperty(IGNORE_USINGS, "false"));
ignoreLiteralSequences = Boolean.parseBoolean(properties.getProperty(OPTION_IGNORE_LITERAL_SEQUENCES, "false"));
}
public void setIgnoreUsings(boolean ignoreUsings) {
this.ignoreUsings = ignoreUsings;
}
public void setIgnoreLiteralSequences(boolean ignoreLiteralSequences) {
this.ignoreLiteralSequences = ignoreLiteralSequences;
}
@Override
protected Lexer getLexerForSource(final CharStream charStream) {
return new CSharpLexer(charStream);
@ -39,7 +43,7 @@ public class CsTokenizer extends AntlrTokenizer {
@Override
protected AntlrTokenFilter getTokenFilter(final AntlrTokenManager tokenManager) {
return new CsTokenFilter(tokenManager, ignoreUsings);
return new CsTokenFilter(tokenManager, ignoreUsings, ignoreLiteralSequences);
}
/**
@ -57,13 +61,16 @@ public class CsTokenizer extends AntlrTokenizer {
}
private final boolean ignoreUsings;
private final boolean ignoreLiteralSequences;
private boolean discardingUsings = false;
private boolean discardingNL = false;
private boolean discardingLiterals = false;
private boolean discardCurrent = false;
CsTokenFilter(final AntlrTokenManager tokenManager, boolean ignoreUsings) {
CsTokenFilter(final AntlrTokenManager tokenManager, boolean ignoreUsings, boolean ignoreLiteralSequences) {
super(tokenManager);
this.ignoreUsings = ignoreUsings;
this.ignoreLiteralSequences = ignoreLiteralSequences;
}
@Override
@ -75,6 +82,7 @@ public class CsTokenizer extends AntlrTokenizer {
protected void analyzeTokens(final AntlrToken currentToken, final Iterable<AntlrToken> remainingTokens) {
discardCurrent = false;
skipUsingDirectives(currentToken, remainingTokens);
skipLiteralSequences(currentToken, remainingTokens);
}
private void skipUsingDirectives(final AntlrToken currentToken, final Iterable<AntlrToken> remainingTokens) {
@ -151,9 +159,44 @@ public class CsTokenizer extends AntlrTokenizer {
discardingNL = currentToken.getKind() == CSharpLexer.NL;
}
private void skipLiteralSequences(final AntlrToken currentToken, final Iterable<AntlrToken> remainingTokens) {
if (ignoreLiteralSequences) {
final int type = currentToken.getKind();
if (type == CSharpLexer.OPEN_BRACE && isSequenceOfLiterals(remainingTokens)) {
discardingLiterals = true;
} else if (type == CSharpLexer.CLOSE_BRACE && discardingLiterals) {
discardingLiterals = false;
discardCurrent = true;
}
}
}
private boolean isSequenceOfLiterals(final Iterable<AntlrToken> remainingTokens) {
boolean seenLiteral = false;
for (final AntlrToken token : remainingTokens) {
switch (token.getKind()) {
case CSharpLexer.CHARACTER_LITERAL:
case CSharpLexer.HEX_INTEGER_LITERAL:
case CSharpLexer.INTEGER_LITERAL:
case CSharpLexer.REAL_LITERAL:
seenLiteral = true;
break; // can be skipped; continue to the next token
case CSharpLexer.COMMA:
break; // can be skipped; continue to the next token
case CSharpLexer.CLOSE_BRACE:
// end of the list; skip all contents
return seenLiteral;
default:
// some other token than the expected ones; this is not a sequence of literals
return false;
}
}
return false;
}
@Override
protected boolean isLanguageSpecificDiscarding() {
return discardingUsings || discardingNL || discardCurrent;
return discardingUsings || discardingNL || discardingLiterals || discardCurrent;
}
}
}

View File

@ -90,13 +90,28 @@ public class CsTokenizerTest extends CpdTextComparisonTest {
doTest("tabWidth");
}
private Properties ignoreUsings() {
return properties(true);
@Test
public void testLongListsOfNumbersAreNotIgnored() {
doTest("listOfNumbers");
}
private Properties properties(boolean ignoreUsings) {
@Test
public void testLongListsOfNumbersAreIgnored() {
doTest("listOfNumbers", "_ignored", skipLiteralSequences());
}
private Properties ignoreUsings() {
return properties(true, false);
}
private Properties skipLiteralSequences() {
return properties(false, true);
}
private Properties properties(boolean ignoreUsings, boolean ignoreLiteralSequences) {
Properties properties = new Properties();
properties.setProperty(Tokenizer.IGNORE_USINGS, Boolean.toString(ignoreUsings));
properties.setProperty(Tokenizer.OPTION_IGNORE_LITERAL_SEQUENCES, Boolean.toString(ignoreLiteralSequences));
return properties;
}
}

View File

@ -0,0 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
public class LongLists {
List<byte> l = new List<byte> {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
};
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,41 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[using] 1 6
[System] 7 13
[;] 13 14
L2
[using] 1 6
[System] 7 13
[.] 13 14
[Collections] 14 25
[;] 25 26
L3
[using] 1 6
[System] 7 13
[.] 13 14
[Collections] 14 25
[.] 25 26
[Generic] 26 33
[;] 33 34
L4
[public] 1 7
[class] 8 13
[LongLists] 14 23
[{] 24 25
L5
[List] 5 9
[<] 9 10
[byte] 10 14
[>] 14 15
[l] 16 17
[=] 18 19
[new] 20 23
[List] 24 28
[<] 28 29
[byte] 29 33
[>] 33 34
L7
[;] 6 7
L8
[}] 1 2
EOF