Cleanup duplicate code paths in escape translators

This commit is contained in:
Clément Fournier
2020-11-18 12:27:55 +01:00
parent f515e34d51
commit 870e13ce83
7 changed files with 49 additions and 47 deletions

View File

@@ -6,7 +6,6 @@ package net.sourceforge.pmd.lang.ast.impl.javacc;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -36,22 +35,11 @@ public final class JavaccTokenDocument extends TokenDocument<JavaccToken> {
public static class TokenDocumentBehavior {
public static final TokenDocumentBehavior DEFAULT = new TokenDocumentBehavior();
public static final TokenDocumentBehavior DEFAULT = new TokenDocumentBehavior(Collections.emptyList());
private final List<String> tokenNames;
private final Function<TextDocument, TextDocument> translator;
private TokenDocumentBehavior() {
this(Collections.emptyList());
}
public TokenDocumentBehavior(List<String> tokenNames) {
this(tokenNames, t -> t);
}
public TokenDocumentBehavior(List<String> tokenNames,
Function<TextDocument, TextDocument> translator) {
this.tokenNames = tokenNames;
this.translator = translator;
}
/**
@@ -73,7 +61,7 @@ public final class JavaccTokenDocument extends TokenDocument<JavaccToken> {
* @see EscapeTranslator
*/
protected TextDocument translate(TextDocument text) throws MalformedSourceException {
return translator.apply(text);
return text;
}

View File

@@ -6,8 +6,6 @@ package net.sourceforge.pmd.lang.ast.impl.javacc.io;
import static java.lang.Integer.min;
import java.util.function.Function;
import net.sourceforge.pmd.internal.util.AssertionUtil;
import net.sourceforge.pmd.util.StringUtil;
import net.sourceforge.pmd.util.document.Chars;
@@ -22,7 +20,7 @@ import net.sourceforge.pmd.util.document.TextDocument;
* perform any escape processing. Subclasses refine this behavior.
*/
@SuppressWarnings("PMD.AssignmentInOperand")
public abstract class EscapeTranslator implements AutoCloseable {
public abstract class EscapeTranslator {
// Note that this can easily be turned into a java.io.Reader with
// efficient block IO, optimized for the common case where there are
// few or no escapes. This is part of the history of this file, but
@@ -43,6 +41,13 @@ public abstract class EscapeTranslator implements AutoCloseable {
private Chars curEscape;
private int offInEscape;
/**
* Create a translator that will read from the given document.
*
* @param original Original document
*
* @throws NullPointerException If the parameter is null
*/
public EscapeTranslator(TextDocument original) {
AssertionUtil.requireParamNotNull("builder", original);
this.input = original.getText();
@@ -52,10 +57,23 @@ public abstract class EscapeTranslator implements AutoCloseable {
/**
* Translate all the input in the buffer.
* Translate all the input in the buffer. This consumes this object.
*
* @return The translated text document. If there is no escape, returns the original text
*
* @throws IllegalStateException If this method is called more than once on the same object
* @throws MalformedSourceException If there are invalid escapes in the source
*/
public TextDocument translateDocument() throws MalformedSourceException {
ensureOpen();
try {
return translateImpl();
} finally {
close();
}
}
private TextDocument translateImpl() {
if (this.bufpos == input.length()) {
return escapes.build();
}
@@ -113,15 +131,18 @@ public abstract class EscapeTranslator implements AutoCloseable {
return startOffsetInclusive;
}
@Override
public void close() {
/**
* Closing a translator does not close the underlying document, it just
* clears the intermediary state.
*/
private void close() {
this.bufpos = -1;
this.input = null;
}
/** Check to make sure that the stream has not been closed */
protected void ensureOpen() {
protected final void ensureOpen() {
if (input == null) {
throw new IllegalStateException("Closed");
}
@@ -142,12 +163,4 @@ public abstract class EscapeTranslator implements AutoCloseable {
}
public static Function<TextDocument, TextDocument> translatorFor(Function<TextDocument, EscapeTranslator> translatorMaker) {
return original -> {
try (EscapeTranslator translator = translatorMaker.apply(original)) {
return translator.translateDocument();
}
};
}
}

View File

@@ -145,7 +145,12 @@ public class CharStreamImplTest {
public CharStream javaCharStream(String abcd) {
return CharStream.create(TextDocument.readOnlyString(abcd, dummyVersion),
new TokenDocumentBehavior(Collections.emptyList(), EscapeTranslator.translatorFor(JavaEscapeTranslator::new)));
new TokenDocumentBehavior(Collections.emptyList()) {
@Override
protected TextDocument translate(TextDocument text) throws MalformedSourceException {
return new JavaEscapeTranslator(text).translateDocument();
}
});
}
@Test

View File

@@ -18,9 +18,7 @@ public class JavaEscapeReaderTest {
public TextDocument readString(String input) {
TextDocument intext = TextDocument.readOnlyString(Chars.wrap(input), LanguageRegistry.getDefaultLanguage().getDefaultVersion());
try (JavaEscapeTranslator translator = new JavaEscapeTranslator(intext)) {
return translator.translateDocument();
}
return new JavaEscapeTranslator(intext).translateDocument();
}

View File

@@ -66,13 +66,9 @@ public class CPPTokenizer extends JavaCCTokenizer {
@Override
protected TextDocument translate(TextDocument text) throws MalformedSourceException {
if (skipBlocks) {
try (CppBlockSkipper translator = new CppBlockSkipper(text, skipBlocksStart, skipBlocksEnd)) {
text = translator.translateDocument();
}
}
try (CppEscapeTranslator translator = new CppEscapeTranslator(text)) {
return translator.translateDocument();
text = new CppBlockSkipper(text, skipBlocksStart, skipBlocksEnd).translateDocument();
}
return new CppEscapeTranslator(text).translateDocument();
}
};
}

View File

@@ -17,7 +17,6 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import net.sourceforge.pmd.lang.ast.impl.javacc.CharStream;
import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken;
import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccTokenDocument;
import net.sourceforge.pmd.lang.ast.impl.javacc.io.EscapeTranslator;
import net.sourceforge.pmd.lang.ast.impl.javacc.io.JavaEscapeTranslator;
import net.sourceforge.pmd.lang.ast.impl.javacc.io.MalformedSourceException;
import net.sourceforge.pmd.util.document.TextDocument;
@@ -30,7 +29,7 @@ final class JavaTokenDocument extends JavaccTokenDocument.TokenDocumentBehavior
static final JavaTokenDocument INSTANCE = new JavaTokenDocument();
private JavaTokenDocument() {
super(JavaTokenKinds.TOKEN_NAMES, EscapeTranslator.translatorFor(JavaEscapeTranslator::new));
super(JavaTokenKinds.TOKEN_NAMES);
}
@@ -51,9 +50,7 @@ final class JavaTokenDocument extends JavaccTokenDocument.TokenDocumentBehavior
@Override
protected TextDocument translate(TextDocument text) throws MalformedSourceException {
try (JavaEscapeTranslator translator = new JavaEscapeTranslator(text)) {
return translator.translateDocument();
}
return new JavaEscapeTranslator(text).translateDocument();
}

View File

@@ -10,9 +10,10 @@ import net.sourceforge.pmd.lang.ast.impl.javacc.CharStream;
import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken;
import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccTokenDocument;
import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccTokenDocument.TokenDocumentBehavior;
import net.sourceforge.pmd.lang.ast.impl.javacc.io.EscapeTranslator;
import net.sourceforge.pmd.lang.ast.impl.javacc.io.JavaEscapeTranslator;
import net.sourceforge.pmd.lang.ast.impl.javacc.io.MalformedSourceException;
import net.sourceforge.pmd.lang.vf.ast.VfTokenKinds;
import net.sourceforge.pmd.util.document.TextDocument;
/**
* @author sergey.gorbaty
@@ -26,8 +27,12 @@ public class VfTokenizer extends JavaCCTokenizer {
@Override
protected TokenDocumentBehavior tokenBehavior() {
return new JavaccTokenDocument.TokenDocumentBehavior(VfTokenKinds.TOKEN_NAMES,
EscapeTranslator.translatorFor(JavaEscapeTranslator::new));
return new JavaccTokenDocument.TokenDocumentBehavior(VfTokenKinds.TOKEN_NAMES) {
@Override
protected TextDocument translate(TextDocument text) throws MalformedSourceException {
return new JavaEscapeTranslator(text).translateDocument();
}
};
}
}