From 95721effd41106862c1281d291498d5b543741c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sun, 21 Apr 2024 15:46:18 +0200 Subject: [PATCH] Trick javacc into giving string literal a non-literal image --- pmd-plsql/etc/grammar/PLSQL.jjt | 10 ++++++---- .../sourceforge/pmd/lang/plsql/cpd/PLSQLCpdLexer.java | 4 +--- .../pmd/lang/plsql/ast/StringLiteralsTest.java | 5 +++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/pmd-plsql/etc/grammar/PLSQL.jjt b/pmd-plsql/etc/grammar/PLSQL.jjt index 367af46534..02713f61a3 100644 --- a/pmd-plsql/etc/grammar/PLSQL.jjt +++ b/pmd-plsql/etc/grammar/PLSQL.jjt @@ -5289,8 +5289,8 @@ TOKEN : | (["q","Q"]) "'<" (~[">"] | ">" ~["'"] )* ">" | (["q","Q"]) "'(" (~[")"] | ")" ~["'"] )* ")" > - | <(["n","N"])? "'" (<_WHATEVER_CHARACTER_WO_APOSTROPHE> | | "''")*> : IN_STRING_LITERAL_TOKENIZE - | <(["n","N"])? <_ALTERNATIVE_QUOTING_STRING_LITERAL>> : IN_STRING_LITERAL_TOKENIZE + | <(["n","N"])? "'" (<_WHATEVER_CHARACTER_WO_APOSTROPHE> | | "''")*> { input_stream.backup(1); } : IN_STRING_LITERAL_TOKENIZE + | <(["n","N"])? <_ALTERNATIVE_QUOTING_STRING_LITERAL>> { input_stream.backup(1); } : IN_STRING_LITERAL_TOKENIZE // special handling for custom quote delimiters | <(["n","N"])? (["q","Q"]) "'" (~[" ", "\t", "\r", "\n", "[", "{", "<", "("])> : IN_STRING_LITERAL @@ -5305,12 +5305,14 @@ TOKEN : } int beforeQuote = image.charAt(image.length() - 2); if (quoteDelimiter == beforeQuote) { - input_stream.backup(1); + input_stream.backup(2); SwitchTo(IN_STRING_LITERAL_TOKENIZE); } } } - TOKEN : { : DEFAULT } + TOKEN : { + : DEFAULT +} /** diff --git a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/cpd/PLSQLCpdLexer.java b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/cpd/PLSQLCpdLexer.java index 9777e014eb..29db777792 100644 --- a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/cpd/PLSQLCpdLexer.java +++ b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/cpd/PLSQLCpdLexer.java @@ -41,13 +41,11 @@ public class PLSQLCpdLexer extends JavaccCpdLexer { if (ignoreIdentifiers && plsqlToken.kind == PLSQLTokenKinds.IDENTIFIER) { image = ""; - } else if (ignoreLiterals && plsqlToken.kind == PLSQLTokenKinds.STRING_LITERAL) { - // Javacc gives it the wrong name in PLSQLTokenKinds.describe - return ""; } else if (ignoreLiterals && (plsqlToken.kind == PLSQLTokenKinds.UNSIGNED_NUMERIC_LITERAL || plsqlToken.kind == PLSQLTokenKinds.FLOAT_LITERAL || plsqlToken.kind == PLSQLTokenKinds.INTEGER_LITERAL || plsqlToken.kind == PLSQLTokenKinds.CHARACTER_LITERAL + || plsqlToken.kind == PLSQLTokenKinds.STRING_LITERAL || plsqlToken.kind == PLSQLTokenKinds.QUOTED_LITERAL)) { // the token kind is preserved image = PLSQLTokenKinds.describe(plsqlToken.kind); diff --git a/pmd-plsql/src/test/java/net/sourceforge/pmd/lang/plsql/ast/StringLiteralsTest.java b/pmd-plsql/src/test/java/net/sourceforge/pmd/lang/plsql/ast/StringLiteralsTest.java index 2cff6fba06..c753ef2a07 100644 --- a/pmd-plsql/src/test/java/net/sourceforge/pmd/lang/plsql/ast/StringLiteralsTest.java +++ b/pmd-plsql/src/test/java/net/sourceforge/pmd/lang/plsql/ast/StringLiteralsTest.java @@ -4,8 +4,9 @@ package net.sourceforge.pmd.lang.plsql.ast; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.startsWith; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.List; @@ -36,7 +37,7 @@ class StringLiteralsTest extends AbstractPLSQLParserTst { ASTInput input = plsql.parseResource("MultilineVarchar.pls"); List strings = input.descendants(ASTStringLiteral.class).toList(); assertEquals(1, strings.size()); - assertTrue(normalizeEol(strings.get(0).getString()).startsWith("\ncreate or replace and")); + assertThat(normalizeEol(strings.get(0).getString()), startsWith("\ncreate or replace and")); } private static void assertString(String quoted, String plain, int index, List strings) {