[ 
https://issues.apache.org/jira/browse/THRIFT-4244?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18114209#comment-18114209
 ] 

Jens Geyer commented on THRIFT-4244:
------------------------------------

Where the string literal syntax is documented:

* {{doc/specs/idl.md}}, which is also what [https://thrift.apache.org/docs/idl] 
shows, only has rule 36. It does not mention escape sequences. Read literally, 
a backslash is an ordinary character, a double quote cannot appear in a 
double-quoted literal and line breaks are allowed. That is the reading in the 
description above, but it is not what the compiler does:
{noformat}
[36] Literal ::= ('"' [^"]* '"') | ("'" [^']* "'")
{noformat}
* Nothing else covers it. [https://thrift.apache.org/docs/types] only says that 
strings are UTF-8, and {{tutorial/tutorial.thrift}} had no example either.

The actual syntax, checked against current master:
{noformat}
- Enclosed in double or single quotes. The other quote character can be used
  as is: "don't", 'say "hi"'
- Escape sequences, in both quote styles:  \"  \'  \\  \n  \r  \t
- Any other character after a backslash is an error, so a literal backslash
  has to be doubled: "Y-m-d\\TH:i:s.uP"
- No numeric escapes such as \x41 or \u00e4; non-ASCII characters are
  written directly
- A literal cannot span lines
- The same rules apply to include paths and annotation values
{noformat}

[PR #3842|https://github.com/apache/thrift/pull/3842] documents this in 
{{tutorial/tutorial.thrift}} (a plain comment, not a doc comment, so generated 
code does not change) and corrects rule 36 in {{doc/specs/idl.md}}, which the 
website picks up on its next build.

Related: several generators (c_glib, cl, dart, delphi, kotlin, lua, rb, rs) do 
not escape string constant values correctly for their language, so a constant 
containing a quote, a backslash or a line break can give invalid code or a 
different value, e.g. for Rust:
{noformat}
pub const X: &str = "say "hi"";
{noformat}
Details in THRIFT-6236.

_Drafted with AI assistance (Claude Opus 5); reviewed and posted by Jens Geyer._


> PHP compiler errors out if escape character is part in string constant
> ----------------------------------------------------------------------
>
>                 Key: THRIFT-4244
>                 URL: https://issues.apache.org/jira/browse/THRIFT-4244
>             Project: Thrift
>          Issue Type: Bug
>          Components: PHP - Compiler
>    Affects Versions: 0.10.0
>         Environment: official thrift docker image 
> https://hub.docker.com/_/thrift/
>            Reporter: Pascal von Rickenbach
>            Assignee: Volodymyr Panivko
>            Priority: Minor
>             Fix For: 0.25.0
>
>          Time Spent: 2h 10m
>  Remaining Estimate: 0h
>
> The compiler throws an error when defining a string constant that contains an 
> escape character. E.g a backslash like in this definition:
> const string ISO8601U = "Y-m-d\TH:i:s.uP";
> According to the Thrift IDL only double quotes are forbidden in the string.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to