Merge pull request #2945 from adamwojs/escape_target_string_literal_in_php

[PHP] Escape dollar sign in target string literal
This commit is contained in:
Terence Parr 2020-10-22 09:32:48 -07:00 committed by GitHub
commit 67e49703c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 0 deletions

View File

@ -265,3 +265,4 @@ YYYY/MM/DD, github id, Full name, email
2020/09/15, rmcgregor1990, Robert McGregor, rmcgregor1990@gmail.com
2020/09/16, trenki2, Markus Trenkwalder, trenki2[at]gmx[dot]net
2020/10/08, Marti2203, Martin Mirchev, mirchevmartin2203@gmail.com
2020/10/20, adamwojs, Adam Wójs, adam[at]wojs.pl

View File

@ -1057,4 +1057,33 @@ public class LexerExecDescriptors {
return new Pair<>(grammarName, grammar);
}
}
/**
* This is a regression test for antlr/antlr4#2709 "PHP target generates
* invalid output when $ is used as part of the literal in lexer rule"
* https://github.com/antlr/antlr4/issues/2709
*/
public static class EscapeTargetStringLiteral extends BaseLexerTestDescriptor {
/**
[@0,0:-1='<EOF>',<-1>,1:0]
*/
@CommentHasStringValue
public String output;
public String errors = null;
public String startRule = "";
public String grammarName = "L";
/**
lexer grammar L;
ACTION_WITH_DOLLAR: '$ACTION';
*/
@CommentHasStringValue
public String grammar;
@Override
public boolean ignore(String targetName) {
return !targetName.equals("PHP");
}
}
}

View File

@ -102,4 +102,12 @@ public class PHPTarget extends Target {
protected void appendUnicodeEscapedCodePoint(int codePoint, StringBuilder sb) {
UnicodeEscapes.appendPythonStyleEscapedCodePoint(codePoint, sb);
}
@Override
public String getTargetStringLiteralFromANTLRStringLiteral(CodeGenerator generator, String literal, boolean addQuotes) {
String targetStringLiteral = super.getTargetStringLiteralFromANTLRStringLiteral(generator, literal, addQuotes);
targetStringLiteral = targetStringLiteral.replace("$", "\\$");
return targetStringLiteral;
}
}