Fix cpd compat

This commit is contained in:
Clément Fournier
2020-08-31 02:57:53 +02:00
parent f4d6f68cab
commit 52862997f6
4 changed files with 16 additions and 7 deletions

View File

@ -12,6 +12,7 @@ import org.apache.commons.io.IOUtils;
import net.sourceforge.pmd.lang.ast.CharStream;
import net.sourceforge.pmd.util.document.TextDocument;
import net.sourceforge.pmd.util.document.io.PmdFiles;
public final class CharStreamFactory {
@ -31,7 +32,7 @@ public final class CharStreamFactory {
*/
public static CharStream simpleCharStream(Reader input, Function<? super TextDocument, ? extends JavaccTokenDocument> documentMaker) {
String source = toString(input);
JavaccTokenDocument document = documentMaker.apply(TextDocument.readOnlyString(source, null));
JavaccTokenDocument document = documentMaker.apply(TextDocument.readOnlyString(source, PmdFiles.dummyCpdVersion()));
return new SimpleCharStream(document);
}
@ -47,7 +48,7 @@ public final class CharStreamFactory {
*/
public static CharStream javaCharStream(Reader input, Function<? super TextDocument, ? extends JavaccTokenDocument> documentMaker) {
String source = toString(input);
JavaccTokenDocument tokens = documentMaker.apply(TextDocument.readOnlyString(source, null));
JavaccTokenDocument tokens = documentMaker.apply(TextDocument.readOnlyString(source, PmdFiles.dummyCpdVersion()));
return new JavaCharStream(tokens);
}

View File

@ -184,6 +184,11 @@ public final class PmdFiles {
};
@Deprecated
public static LanguageVersion dummyCpdVersion() {
return DUMMY_CPD_LANG.getDefaultVersion();
}
/**
* Bridges {@link SourceCode} with {@link TextFile}. This allows
* javacc tokenizers to work on text documents.
@ -195,7 +200,7 @@ public final class PmdFiles {
return new StringTextFile(
sourceCode.getCodeBuffer().toString(),
sourceCode.getFileName(),
DUMMY_CPD_LANG.getDefaultVersion()
dummyCpdVersion()
);
}
}

View File

@ -14,6 +14,7 @@ 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.util.document.TextDocument;
import net.sourceforge.pmd.util.document.io.PmdFiles;
/**
* A SimpleCharStream, that supports the continuation of lines via backslash+newline,
@ -68,7 +69,7 @@ public class CppCharStream extends SimpleCharStream {
public static CppCharStream newCppCharStream(Reader dstream) {
String source = CharStreamFactory.toString(dstream);
JavaccTokenDocument document = new JavaccTokenDocument(TextDocument.readOnlyString(source, null)) {
JavaccTokenDocument document = new JavaccTokenDocument(TextDocument.readOnlyString(source, PmdFiles.dummyCpdVersion())) {
@Override
protected @Nullable String describeKindImpl(int kind) {
return CppTokenKinds.describe(kind);

View File

@ -21,14 +21,16 @@ public class CppCharStreamTest {
@Test
public void testContinuationWindows() throws IOException {
// note that the \r is normalized to a \n by the TextFile
CppCharStream stream = CppCharStream.newCppCharStream(new StringReader("a\\\r\nb"));
assertStream(stream, "ab");
}
@Test
public void testBackup() throws IOException {
CppCharStream stream = CppCharStream.newCppCharStream(new StringReader("a\\b\\\rc"));
assertStream(stream, "a\\b\\\rc");
// note that the \r is normalized to a \n by the TextFile
CppCharStream stream = CppCharStream.newCppCharStream(new StringReader("a\\b\\qc"));
assertStream(stream, "a\\b\\qc");
}
private void assertStream(CppCharStream stream, String token) throws IOException {
@ -36,7 +38,7 @@ public class CppCharStreamTest {
assertEquals(token.charAt(0), c);
for (int i = 1; i < token.length(); i++) {
c = stream.readChar();
assertEquals(token.charAt(i), c);
assertEquals(token + " char at " + i + ": " + token.charAt(i) + " != " + c, token.charAt(i), c);
}
assertEquals(token, stream.GetImage());
assertEquals(token, new String(stream.GetSuffix(token.length())));