Skip to content

Commit

Permalink
manual: move quoted string to the main chapters
Browse files Browse the repository at this point in the history
  • Loading branch information
Octachron committed May 20, 2018
1 parent 876d53a commit 3af01f5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 48 deletions.
2 changes: 1 addition & 1 deletion manual/manual/refman/const.etex
Expand Up @@ -24,7 +24,7 @@ constant:
\end{syntax}
See also the following language extensions:
\hyperref[s:ext-integer]{integer literals for types \texttt{int32}, \texttt{int64}
and \texttt{nativeint}}, \hyperref[s:quoted-strings]{quoted strings}
and \texttt{nativeint}},
and \hyperref[s:extension-literals]{extension literals}.

The syntactic class of constants comprises literals from the four
Expand Down
59 changes: 13 additions & 46 deletions manual/manual/refman/exten.etex
Expand Up @@ -1792,6 +1792,19 @@ the attributes are considered to apply to the payload:
fun%foo[@bar] x -> x + 1 === [%foo (fun x -> x + 1)[@bar ] ];
\end{verbatim}

Quoted strings "{|...|}" are particularly interesting for payloads
that embed foreign syntax fragments. Those fragments can be interpreted
by a preprocessor and turned into OCaml code without requiring escaping
quotes. For instance, you can use "[%sql {|...|}]" to
represent arbitrary SQL statements -- assuming you have a ppx-rewriter
that recognizes the "%sql" extension.

Note that the non-extension form, for example "{sql|...|sql}", should
not be used for this purpose. Indeed, the user cannot see in the code that
this string literal has a different semantics than they expect. Moreover,
giving a semantics to a specific delimiter limits the freedom to
change the delimiter to avoid escaping issues.

\subsection{Built-in extension nodes}

(Introduced in OCaml 4.03)
Expand All @@ -1815,52 +1828,6 @@ let y = [%extension_constructor Y]
x <> y;;
\end{caml_example}

\section{Quoted strings}\label{s:quoted-strings}

(Introduced in OCaml 4.02)

Quoted strings "{foo|...|foo}" provide a different lexical syntax to
write string literals in OCaml code. They are useful to represent
strings of arbitrary content without escaping -- as long as the
delimiter you chose (here "|foo}") does not occur in the string
itself.

\begin{syntax}
string-literal: ...
| '{' quoted-string-id '|' ........ '|' quoted-string-id '}'
;
quoted-string-id:
{ 'a'...'z' || '_' }
;
\end{syntax}

The opening delimiter has the form "{id|" where "id" is a (possibly
empty) sequence of lowercase letters and underscores. The
corresponding closing delimiter is "|id}" (with the same
identifier). Unlike regular OCaml string literals, quoted
strings do not interpret any character in a special way.

Example:

\begin{verbatim}
String.length {|\"|} (* returns 2 *)
String.length {foo|\"|foo} (* returns 2 *)
\end{verbatim}

Quoted strings are interesting in particular in conjunction to
extension nodes "[%foo ...]" (see \ref{s:extension-nodes}) to embed
foreign syntax fragments to be interpreted by a preprocessor and
turned into OCaml code: you can use "[%sql {|...|}]" for example to
represent arbitrary SQL statements -- assuming you have a ppx-rewriter
that recognizes the "%sql" extension -- without requiring escaping
quotes.

Note that the non-extension form, for example "{sql|...|sql}", should
not be used for this purpose, as the user cannot see in the code that
this string literal has a different semantics than they expect, and
giving a semantics to a specific delimiter limits the freedom to
change the delimiter to avoid escaping issues.

\section{Exception cases in pattern matching}\label{s:exception-match}

(Introduced in OCaml 4.02)
Expand Down
16 changes: 16 additions & 0 deletions manual/manual/refman/lex.etex
Expand Up @@ -147,6 +147,11 @@ The two single quotes enclose either one character different from
\begin{syntax}
string-literal:
'"' { string-character } '"'
| '{' quoted-string-id '|' { any-char } '|' quoted-string-id '}'
;
quoted-string-id:
{ 'a'...'z' || '_' }
;
;
string-character:
regular-string-char
Expand All @@ -171,6 +176,17 @@ To allow splitting long string literals across lines, the sequence
followed by any number of spaces and horizontal tabulations at the
beginning of the next line) is ignored inside string literals.

Quoted string literals provide an alternative lexical syntax for
string literals. They are useful to represent strings of arbitrary content
without escaping. Quoted strings are delimited by a matching pair
of @'{' quoted-string-id '|'@ and @'|' quoted-string-id '}'@ with
the same @quoted-string-id@ on both sides. Quoted strings do not interpret
any character in a special way but requires that the
sequence @'|' quoted-string-id '}'@ does not occur in the string itself.
The identifier @quoted-string-id@ is a (possibly empty) sequence of
lowercase letters and underscores that can be freely chosen to avoid
such issue.

The current implementation places practically no restrictions on the
length of string literals.

Expand Down
3 changes: 2 additions & 1 deletion manual/manual/tutorials/coreexamples.etex
Expand Up @@ -58,7 +58,8 @@ usual basic data types: booleans, characters, and immutable character strings.
\begin{caml_example}{toplevel}
(1 < 2) = false;;
'a';;
"Hello world";;
"Hello" ^ " " ^ "world";;
{|This is a quoted string where \n is not a newline|};;
\end{caml_example}

Predefined data structures include tuples, arrays, and lists. There are also
Expand Down

0 comments on commit 3af01f5

Please sign in to comment.