Trick javacc into giving string literal a non-literal image

This commit is contained in:
Clément Fournier 2024-04-21 15:46:18 +02:00
parent b931c2f1e0
commit 95721effd4
No known key found for this signature in database
GPG Key ID: 91A544D7F05BBCBB
3 changed files with 10 additions and 9 deletions

View File

@ -5289,8 +5289,8 @@ TOKEN :
| (["q","Q"]) "'<" (~[">"] | ">" ~["'"] )* ">"
| (["q","Q"]) "'(" (~[")"] | ")" ~["'"] )* ")"
>
| <(["n","N"])? "'" (<_WHATEVER_CHARACTER_WO_APOSTROPHE> | <SPECIAL_CHARACTERS> | "''")*> : IN_STRING_LITERAL_TOKENIZE
| <(["n","N"])? <_ALTERNATIVE_QUOTING_STRING_LITERAL>> : IN_STRING_LITERAL_TOKENIZE
| <(["n","N"])? "'" (<_WHATEVER_CHARACTER_WO_APOSTROPHE> | <SPECIAL_CHARACTERS> | "''")*> { 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);
}
}
}
<IN_STRING_LITERAL_TOKENIZE> TOKEN : { <STRING_LITERAL: "'"> : DEFAULT }
<IN_STRING_LITERAL_TOKENIZE> TOKEN : {
<STRING_LITERAL: ~[] "'"> : DEFAULT
}
/**

View File

@ -41,13 +41,11 @@ public class PLSQLCpdLexer extends JavaccCpdLexer {
if (ignoreIdentifiers && plsqlToken.kind == PLSQLTokenKinds.IDENTIFIER) {
image = "<identifier>";
} else if (ignoreLiterals && plsqlToken.kind == PLSQLTokenKinds.STRING_LITERAL) {
// Javacc gives it the wrong name in PLSQLTokenKinds.describe
return "<STRING_LITERAL>";
} 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);

View File

@ -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<ASTStringLiteral> 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<ASTStringLiteral> strings) {