From c088a6add5fe1b8ee609df410d6f6ac8252b2adc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sat, 18 Apr 2020 13:02:54 +0200 Subject: [PATCH] Rename charStream methods --- javacc-wrapper.xml | 21 +++++++++++++- .../pmd/lang/ast/impl/javacc/CharStream.java | 29 +++++-------------- .../pmd/lang/ast/impl/javacc/JavaccToken.java | 2 +- .../ast/impl/javacc/JavaccTokenDocument.java | 4 +-- .../ast/impl/javacc/CharStreamImplTest.java | 18 ++++++------ .../pmd/cpd/CppCharStreamTest.java | 8 +++-- .../sourceforge/pmd/lang/vm/ast/VmParser.java | 2 +- 7 files changed, 45 insertions(+), 39 deletions(-) diff --git a/javacc-wrapper.xml b/javacc-wrapper.xml index 5181f10667..5392693a56 100644 --- a/javacc-wrapper.xml +++ b/javacc-wrapper.xml @@ -60,7 +60,7 @@ - + @@ -276,6 +276,25 @@ + + + + + + + + + + + + + + + + + + + diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/CharStream.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/CharStream.java index a81d2af44a..d9be04d0a5 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/CharStream.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/CharStream.java @@ -57,7 +57,7 @@ public final class CharStream { * the buffer between two successive calls to this method to implement * backup correctly. */ - public char BeginToken() throws EOFException { + public char markTokenStart() throws EOFException { cursor.mark(); return cursor.next(); } @@ -67,7 +67,7 @@ public final class CharStream { * Returns a string made up of characters from the token mark up to * to the current buffer position. */ - public String GetImage() { + public String getTokenImage() { StringBuilder sb = new StringBuilder(); cursor.markToString(sb); return sb.toString(); @@ -75,31 +75,16 @@ public final class CharStream { /** - * 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).toCharArray();
-     * }
+ * Appends the suffix of length 'len' of the current token to the given + * string builder. This is used to build up the matched string + * for use in actions in the case of MORE. * * @param len Length of the returned array * - * @return The suffix - * - * @throws IndexOutOfBoundsException If len is greater than the length of the - * current token + * @throws IndexOutOfBoundsException If len is greater than the length of the current token */ - public char[] GetSuffix(int len) { - String t = GetImage(); - return t.substring(t.length() - len).toCharArray(); - } - - public void appendSuffix(StringBuilder sb, int len) { - String t = GetImage(); + String t = getTokenImage(); sb.append(t, t.length() - len, t.length()); } 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 d2e0c02045..fb5195f4d6 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 @@ -178,7 +178,7 @@ public class JavaccToken implements GenericToken { public JavaccToken replaceImage(CharStream charStream) { return new JavaccToken( this.kind, - charStream.GetImage(), + charStream.getTokenImage(), this.startOffset, charStream.getEndOffset(), this.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 index 896193d356..27cb3bc609 100644 --- 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 @@ -102,7 +102,7 @@ public class JavaccTokenDocument extends TokenDocument { * @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 + * then no call to {@link CharStream#getTokenImage()} should be * issued. * * @return A new token @@ -110,7 +110,7 @@ public class JavaccTokenDocument extends TokenDocument { public JavaccToken createToken(int kind, CharStream cs, @Nullable String image) { return new JavaccToken( kind, - image == null ? cs.GetImage() : image, + image == null ? cs.getTokenImage() : image, cs.getStartOffset(), cs.getEndOffset(), this diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/impl/javacc/CharStreamImplTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/impl/javacc/CharStreamImplTest.java index 816d184e42..2d57cae381 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/impl/javacc/CharStreamImplTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/impl/javacc/CharStreamImplTest.java @@ -89,12 +89,12 @@ public class CharStreamImplTest { CharStream stream = simpleCharStream("abcd"); - assertEquals('a', stream.BeginToken()); + assertEquals('a', stream.markTokenStart()); assertEquals('b', stream.readChar()); assertEquals('c', stream.readChar()); assertEquals('d', stream.readChar()); - assertEquals("abcd", stream.GetImage()); + assertEquals("abcd", stream.getTokenImage()); stream.backup(2); assertEquals('c', stream.readChar()); @@ -110,25 +110,25 @@ public class CharStreamImplTest { CharStream stream = javaCharStream("__\\u00a0_\\u00a0_"); - assertEquals('_', stream.BeginToken()); + assertEquals('_', stream.markTokenStart()); assertEquals('_', stream.readChar()); assertEquals('\u00a0', stream.readChar()); assertEquals('_', stream.readChar()); - assertEquals("__\u00a0_", stream.GetImage()); + assertEquals("__\u00a0_", stream.getTokenImage()); stream.backup(2); assertEquals('\u00a0', stream.readChar()); assertEquals('_', stream.readChar()); assertEquals('\u00a0', stream.readChar()); - assertEquals("__\u00a0_\u00a0", stream.GetImage()); + assertEquals("__\u00a0_\u00a0", stream.getTokenImage()); assertEquals('_', stream.readChar()); stream.backup(2); - assertEquals('\u00a0', stream.BeginToken()); + assertEquals('\u00a0', stream.markTokenStart()); assertEquals('_', stream.readChar()); - assertEquals("\u00a0_", stream.GetImage()); + assertEquals("\u00a0_", stream.getTokenImage()); expect.expect(EOFException.class); stream.readChar(); @@ -154,7 +154,7 @@ public class CharStreamImplTest { assertEquals('a', stream.readChar()); assertEquals('b', stream.readChar()); - assertEquals('c', stream.BeginToken()); + assertEquals('c', stream.markTokenStart()); assertEquals('d', stream.readChar()); expect.expect(IllegalArgumentException.class); @@ -166,7 +166,7 @@ public class CharStreamImplTest { CharStream stream = simpleCharStream("abcd"); - assertEquals('a', stream.BeginToken()); + assertEquals('a', stream.markTokenStart()); assertEquals('b', stream.readChar()); assertEquals('c', stream.readChar()); assertEquals('d', stream.readChar()); diff --git a/pmd-cpp/src/test/java/net/sourceforge/pmd/cpd/CppCharStreamTest.java b/pmd-cpp/src/test/java/net/sourceforge/pmd/cpd/CppCharStreamTest.java index fab00aab81..55c4aef643 100644 --- a/pmd-cpp/src/test/java/net/sourceforge/pmd/cpd/CppCharStreamTest.java +++ b/pmd-cpp/src/test/java/net/sourceforge/pmd/cpd/CppCharStreamTest.java @@ -42,13 +42,15 @@ public class CppCharStreamTest { } private void assertStream(CharStream stream, String token) throws IOException { - char c = stream.BeginToken(); + char c = stream.markTokenStart(); assertEquals(token.charAt(0), c); for (int i = 1; i < token.length(); i++) { c = stream.readChar(); 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()))); + assertEquals(token, stream.getTokenImage()); + StringBuilder sb = new StringBuilder(); + stream.appendSuffix(sb, token.length()); + assertEquals(token, sb.toString()); } } diff --git a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/VmParser.java b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/VmParser.java index a2995685d2..845653d06c 100644 --- a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/VmParser.java +++ b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/VmParser.java @@ -42,7 +42,7 @@ public class VmParser extends JjtreeParserAdapter { @Override public JavaccToken createToken(int kind, CharStream cs, @Nullable String image) { - String realImage = image == null ? cs.GetImage() : image; + String realImage = image == null ? cs.getTokenImage() : image; if (kind == VmTokenKinds.ESCAPE_DIRECTIVE) { realImage = escapedDirective(realImage); }