From: Javier Miranda <mira...@adacore.com> gcc/ada/
* doc/gnat_rm/implementation_defined_pragmas.rst (Extensions_Allowed): Document string interpolation. * gnat_rm.texi: Regenerate. * gnat_ugn.texi: Regenerate. Tested on x86_64-pc-linux-gnu, committed on master. --- .../implementation_defined_pragmas.rst | 63 +++++++ gcc/ada/gnat_rm.texi | 160 +++++++++++++++++- gcc/ada/gnat_ugn.texi | 2 +- 3 files changed, 223 insertions(+), 2 deletions(-) diff --git a/gcc/ada/doc/gnat_rm/implementation_defined_pragmas.rst b/gcc/ada/doc/gnat_rm/implementation_defined_pragmas.rst index ed42d087a25..725d5241845 100644 --- a/gcc/ada/doc/gnat_rm/implementation_defined_pragmas.rst +++ b/gcc/ada/doc/gnat_rm/implementation_defined_pragmas.rst @@ -2425,6 +2425,69 @@ are identified below. -- ... end Stacks; +* String Interpolation + + The syntax for string literals is extended to support string interpolation. + + Within an interpolated string literal, an arbitrary expression, when + enclosed in { ... }, is expanded at run time into the result of calling + 'Image on the result of evaluating the expression enclosed by the brace + characters, unless it is already a string or a single character. + + Here is an example of this feature where the expressions "Name" and + "X + Y" will be evaluated and included in the string. + + .. code-block:: ada + + Put_Line (f"The name is {Name} and the sum is {X + Y}."); + + In addition, an escape character (\'\\\') is provided for inserting certain + standard control characters (such as \'\\t\' for tabulation or \'\\n\' for + newline) or to escape characters with special significance to the + interpolated string syntax, namely \'"\', \'{\', \'}\',and \'\\\' itself. + +================= ================= +escaped_character meaning +----------------- ----------------- +'\\a' ALERT +'\\b' BACKSPACE +'\\f' FORM FEED +'\\n' LINE FEED +'\\r' CARRIAGE RETURN +'\\t' CHARACTER TABULATION +'\\v' LINE TABULATION +'\\0' NUL +----------------- ----------------- +'\\\\' '\\' +'\\"' '"' +'\\{' '{' +'\\}' '}' +================= ================= + + Note that, unlike normal string literals, doubled characters have no + special significance. So to include a double-quote or a brace character + in an interpolated string, they must be preceded by a \'\\\'. + For example: + + .. code-block:: ada + + Put_Line + (f"X = {X} and Y = {Y} and X+Y = {X+Y};\n" & + f" a double quote is \" and" & + f" an open brace is \{"); + + Finally, a syntax is provided for creating multi-line string literals, + without having to explicitly use an escape sequence such as \'\\n\'. For + example: + + .. code-block:: ada + + Put_Line + (f"This is a multi-line" + "string literal" + "There is no ambiguity about how many" + "spaces are included in each line"); + .. _Pragma-Extensions_Visible: Pragma Extensions_Visible diff --git a/gcc/ada/gnat_rm.texi b/gcc/ada/gnat_rm.texi index 212ed3df9d8..81b36b87288 100644 --- a/gcc/ada/gnat_rm.texi +++ b/gcc/ada/gnat_rm.texi @@ -19,7 +19,7 @@ @copying @quotation -GNAT Reference Manual , Dec 01, 2022 +GNAT Reference Manual , May 09, 2023 AdaCore @@ -3880,8 +3880,166 @@ private -- ... end Stacks; @end example + +@item +String Interpolation + +The syntax for string literals is extended to support string interpolation. + +Within an interpolated string literal, an arbitrary expression, when +enclosed in @{ … @}, is expanded at run time into the result of calling +‘Image on the result of evaluating the expression enclosed by the brace +characters, unless it is already a string or a single character. + +Here is an example of this feature where the expressions “Name” and +“X + Y” will be evaluated and included in the string. + +@example +Put_Line (f"The name is @{Name@} and the sum is @{X + Y@}."); +@end example + +In addition, an escape character ('\') is provided for inserting certain +standard control characters (such as '\t' for tabulation or '\n' for +newline) or to escape characters with special significance to the +interpolated string syntax, namely '”', '@{', '@}',and '\' itself. @end itemize + +@multitable {xxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxx} +@item + +escaped_character + +@tab + +meaning + +@item + +‘\a’ + +@tab + +ALERT + +@item + +‘\b’ + +@tab + +BACKSPACE + +@item + +‘\f’ + +@tab + +FORM FEED + +@item + +‘\n’ + +@tab + +LINE FEED + +@item + +‘\r’ + +@tab + +CARRIAGE RETURN + +@item + +‘\t’ + +@tab + +CHARACTER TABULATION + +@item + +‘\v’ + +@tab + +LINE TABULATION + +@item + +‘\0’ + +@tab + +NUL + +@item + +‘\\’ + +@tab + +‘\’ + +@item + +‘\”’ + +@tab + +‘”’ + +@item + +‘\@{’ + +@tab + +‘@{’ + +@item + +‘\@}’ + +@tab + +‘@}’ + +@end multitable + + +@quotation + +Note that, unlike normal string literals, doubled characters have no +special significance. So to include a double-quote or a brace character +in an interpolated string, they must be preceded by a '\'. +For example: + +@example +Put_Line + (f"X = @{X@} and Y = @{Y@} and X+Y = @{X+Y@};\n" & + f" a double quote is \" and" & + f" an open brace is \@{"); +@end example + +Finally, a syntax is provided for creating multi-line string literals, +without having to explicitly use an escape sequence such as '\n'. For +example: + +@example +Put_Line + (f"This is a multi-line" + "string literal" + "There is no ambiguity about how many" + "spaces are included in each line"); +@end example +@end quotation + @node Pragma Extensions_Visible,Pragma External,Pragma Extensions_Allowed,Implementation Defined Pragmas @anchor{gnat_rm/implementation_defined_pragmas id12}@anchor{65}@anchor{gnat_rm/implementation_defined_pragmas pragma-extensions-visible}@anchor{66} @section Pragma Extensions_Visible diff --git a/gcc/ada/gnat_ugn.texi b/gcc/ada/gnat_ugn.texi index e8512cb0471..134a42e3df9 100644 --- a/gcc/ada/gnat_ugn.texi +++ b/gcc/ada/gnat_ugn.texi @@ -29433,8 +29433,8 @@ to permit their use in free software. @printindex ge -@anchor{gnat_ugn/gnat_utility_programs switches-related-to-project-files}@w{ } @anchor{cf}@w{ } +@anchor{gnat_ugn/gnat_utility_programs switches-related-to-project-files}@w{ } @c %**end of body @bye -- 2.40.0