Skip to content

Commit

Permalink
Merge pull request #2945 from adamwojs/escape_target_string_literal_i…
Browse files Browse the repository at this point in the history
…n_php

[PHP] Escape dollar sign in target string literal
  • Loading branch information
parrt committed Oct 22, 2020
2 parents 0a7eaed + 5d3452e commit 67e4970
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions contributors.txt
Expand Up @@ -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
Expand Up @@ -1057,4 +1057,33 @@ public Pair<String, String> getGrammar() {
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");
}
}
}
8 changes: 8 additions & 0 deletions tool/src/org/antlr/v4/codegen/target/PHPTarget.java
Expand Up @@ -102,4 +102,12 @@ public boolean supportsOverloadedMethods() {
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;
}
}

0 comments on commit 67e4970

Please sign in to comment.