Skip to content

Commit 4aa1fb2

Browse files
quildtidejohanmon
authored andcommitted
Improve flow of documentation for non-standard strings (JuliaLang#40057)
1 parent df727e0 commit 4aa1fb2

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

doc/src/manual/metaprogramming.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -981,13 +981,13 @@ block:
981981
end
982982
```
983983

984-
## Non-Standard String Literals
984+
## [Non-Standard String Literals](@id meta-non-standard-string-literals)
985985

986986
Recall from [Strings](@ref non-standard-string-literals) that string literals prefixed by an identifier are called non-standard
987987
string literals, and can have different semantics than un-prefixed string literals. For example:
988988

989-
* `r"^\s*(?:#|$)"` produces a regular expression object rather than a string
990-
* `b"DATA\xff\u2200"` is a byte array literal for `[68,65,84,65,255,226,136,128]`.
989+
* `r"^\s*(?:#|$)"` produces a [regular expression object](@ref man-regex-literals) rather than a string
990+
* `b"DATA\xff\u2200"` is a [byte array literal](@ref man-byte-array-literals) for `[68,65,84,65,255,226,136,128]`.
991991

992992
Perhaps surprisingly, these behaviors are not hard-coded into the Julia parser or compiler. Instead,
993993
they are custom behaviors provided by a general mechanism that anyone can use: prefixed string
@@ -1051,20 +1051,9 @@ constructed on each iteration. In the vast majority of use cases, however, regul
10511051
are not constructed based on run-time data. In this majority of cases, the ability to write regular
10521052
expressions as compile-time values is invaluable.
10531053

1054-
Like non-standard string literals, non-standard command literals exist using a prefixed variant
1055-
of the command literal syntax. The command literal ```custom`literal` ``` is parsed as `@custom_cmd "literal"`.
1056-
Julia itself does not contain any non-standard command literals, but packages can make use of
1057-
this syntax. Aside from the different syntax and the `_cmd` suffix instead of the `_str` suffix,
1058-
non-standard command literals behave exactly like non-standard string literals.
1059-
1060-
In the event that two modules provide non-standard string or command literals with the same name,
1061-
it is possible to qualify the string or command literal with a module name. For instance, if both
1062-
`Foo` and `Bar` provide non-standard string literal `@x_str`, then one can write `Foo.x"literal"`
1063-
or `Bar.x"literal"` to disambiguate between the two.
1064-
10651054
The mechanism for user-defined string literals is deeply, profoundly powerful. Not only are Julia's
1066-
non-standard literals implemented using it, but also the command literal syntax (``` `echo "Hello, $person"` ```)
1067-
is implemented with the following innocuous-looking macro:
1055+
non-standard literals implemented using it, but the command literal syntax (``` `echo "Hello, $person"` ```)
1056+
is also implemented using the following innocuous-looking macro:
10681057

10691058
```julia
10701059
macro cmd(str)
@@ -1077,6 +1066,20 @@ but they are just functions, written entirely in Julia. You can read their sourc
10771066
what they do -- and all they do is construct expression objects to be inserted into your program's
10781067
syntax tree.
10791068

1069+
Like string literals, command literals can also be prefixed by an identifier
1070+
to form what are called non-standard command literals. These command literals are parsed
1071+
as calls to specially-named macros. For example, the syntax ```custom`literal` ``` is parsed
1072+
as `@custom_cmd "literal"`.
1073+
Julia itself does not contain any non-standard command literals, but packages can make use of
1074+
this syntax. Aside from the different syntax and the `_cmd` suffix instead of the `_str` suffix,
1075+
non-standard command literals behave exactly like non-standard string literals.
1076+
1077+
In the event that two modules provide non-standard string or command literals with the same name,
1078+
it is possible to qualify the string or command literal with a module name. For instance, if both
1079+
`Foo` and `Bar` provide non-standard string literal `@x_str`, then one can write `Foo.x"literal"`
1080+
or `Bar.x"literal"` to disambiguate between the two.
1081+
1082+
10801083
Another way to define a macro would be like this:
10811084

10821085
```julia

doc/src/manual/strings.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -739,13 +739,16 @@ Some other useful functions include:
739739

740740
There are situations when you want to construct a string or use string semantics, but the behavior
741741
of the standard string construct is not quite what is needed. For these kinds of situations, Julia
742-
provides [non-standard string literals](@ref). A non-standard string literal looks like a regular
743-
double-quoted string literal, but is immediately prefixed by an identifier, and doesn't behave
744-
quite like a normal string literal. Regular expressions, byte array literals and version number
745-
literals, as described below, are some examples of non-standard string literals. Other examples
746-
are given in the [Metaprogramming](@ref) section.
742+
provides non-standard string literals. A non-standard string literal looks like a regular
743+
double-quoted string literal,
744+
but is immediately prefixed by an identifier, and may behave differently from a normal string literal.
747745

748-
## Regular Expressions
746+
[Regular expressions](@ref man-regex-literals), [byte array literals](@ref man-byte-array-literals),
747+
and [version number literals](@ref man-version-number-literals), as described below,
748+
are some examples of non-standard string literals. Users and packages may also define new non-standard string literals.
749+
Further documentation is given in the [Metaprogramming](@ref meta-non-standard-string-literals) section.
750+
751+
## [Regular Expressions](@id man-regex-literals)
749752

750753
Julia has Perl-compatible regular expressions (regexes), as provided by the [PCRE](http://www.pcre.org/)
751754
library (a description of the syntax can be found [here](http://www.pcre.org/current/doc/html/pcre2syntax.html)). Regular expressions are related to strings in two ways: the obvious connection is that

0 commit comments

Comments
 (0)