Skip to content

Commit c919629

Browse files
committed
auto merge of #9674 : ben0x539/rust/raw-str, r=alexcrichton
This branch parses raw string literals as in #9411.
2 parents 6ddd011 + d7dfe0a commit c919629

File tree

35 files changed

+274
-80
lines changed

35 files changed

+274
-80
lines changed

doc/rust.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,14 @@ literal : string_lit | char_lit | num_lit ;
239239

240240
~~~~~~~~ {.ebnf .gram}
241241
char_lit : '\x27' char_body '\x27' ;
242-
string_lit : '"' string_body * '"' ;
242+
string_lit : '"' string_body * '"' | 'r' raw_string ;
243243
244244
char_body : non_single_quote
245245
| '\x5c' [ '\x27' | common_escape ] ;
246246
247247
string_body : non_double_quote
248248
| '\x5c' [ '\x22' | common_escape ] ;
249+
raw_string : '"' raw_string_body '"' | '#' raw_string '#' ;
249250
250251
common_escape : '\x5c'
251252
| 'n' | 'r' | 't' | '0'
@@ -267,9 +268,10 @@ which must be _escaped_ by a preceding U+005C character (`\`).
267268

268269
A _string literal_ is a sequence of any Unicode characters enclosed within
269270
two `U+0022` (double-quote) characters, with the exception of `U+0022`
270-
itself, which must be _escaped_ by a preceding `U+005C` character (`\`).
271+
itself, which must be _escaped_ by a preceding `U+005C` character (`\`),
272+
or a _raw string literal_.
271273

272-
Some additional _escapes_ are available in either character or string
274+
Some additional _escapes_ are available in either character or non-raw string
273275
literals. An escape starts with a `U+005C` (`\`) and continues with one of
274276
the following forms:
275277

@@ -285,9 +287,35 @@ the following forms:
285287
* A _whitespace escape_ is one of the characters `U+006E` (`n`), `U+0072`
286288
(`r`), or `U+0074` (`t`), denoting the unicode values `U+000A` (LF),
287289
`U+000D` (CR) or `U+0009` (HT) respectively.
288-
* The _backslash escape_ is the character U+005C (`\`) which must be
290+
* The _backslash escape_ is the character `U+005C` (`\`) which must be
289291
escaped in order to denote *itself*.
290292

293+
Raw string literals do not process any escapes. They start with the character
294+
`U+0072` (`r`), followed zero or more of the character `U+0023` (`#`) and a
295+
`U+0022` (double-quote) character. The _raw string body_ is not defined in the
296+
EBNF grammar above: it can contain any sequence of Unicode characters and is
297+
terminated only by another `U+0022` (double-quote) character, followed by the
298+
same number of `U+0023` (`#`) characters that preceeded the opening `U+0022`
299+
(double-quote) character.
300+
301+
All Unicode characters contained in the raw string body represent themselves,
302+
the characters `U+0022` (double-quote) (except when followed by at least as
303+
many `U+0023` (`#`) characters as were used to start the raw string literal) or
304+
`U+005C` (`\`) do not have any special meaning.
305+
306+
Examples for string literals:
307+
308+
~~~
309+
"foo"; r"foo"; // foo
310+
"\"foo\""; r#""foo""#; // "foo"
311+
312+
"foo #\"# bar";
313+
r##"foo #"# bar"##; // foo #"# bar
314+
315+
"\x52"; "R"; r"R"; // R
316+
"\\x52"; r"\x52"; // \x52
317+
~~~
318+
291319
#### Number literals
292320

293321
~~~~~~~~ {.ebnf .gram}

doc/tutorial.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,12 @@ whose literals are written between single quotes, as in `'x'`.
353353
Just like C, Rust understands a number of character escapes, using the backslash
354354
character, such as `\n`, `\r`, and `\t`. String literals,
355355
written between double quotes, allow the same escape sequences.
356-
More on strings [later](#vectors-and-strings).
356+
357+
On the other hand, raw string literals do not process any escape sequences.
358+
They are written as `r##"blah"##`, with a matching number of zero or more `#`
359+
before the opening and after the closing quote, and can contain any sequence of
360+
characters except their closing delimiter. More on strings
361+
[later](#vectors-and-strings).
357362

358363
The nil type, written `()`, has a single value, also written `()`.
359364

src/etc/vim/syntax/rust.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ syn match rustFormat display "%%" contained
148148
syn match rustSpecial display contained /\\\([nrt\\'"]\|x\x\{2}\|u\x\{4}\|U\x\{8}\)/
149149
syn match rustStringContinuation display contained /\\\n\s*/
150150
syn region rustString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=rustTodo,rustFormat,rustSpecial,rustStringContinuation
151+
syn region rustString start='r\z(#*\)"' end='"\z1'
151152

152153
syn region rustAttribute start="#\[" end="\]" contains=rustString,rustDeriving
153154
syn region rustDeriving start="deriving(" end=")" contained contains=rustTrait

src/librustc/front/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> @ast::Expr {
407407
debug2!("encoding {}", ast_util::path_name_i(path));
408408

409409
let name_lit: ast::lit =
410-
nospan(ast::lit_str(ast_util::path_name_i(path).to_managed()));
410+
nospan(ast::lit_str(ast_util::path_name_i(path).to_managed(), ast::CookedStr));
411411

412412
let name_expr = @ast::Expr {
413413
id: ast::DUMMY_NODE_ID,

src/librustc/metadata/creader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ fn visit_view_item(e: @mut Env, i: &ast::view_item) {
142142
let ident = token::ident_to_str(&ident);
143143
let meta_items = match path_opt {
144144
None => meta_items.clone(),
145-
Some(p) => {
145+
Some((p, _path_str_style)) => {
146146
let p_path = Path(p);
147147
match p_path.filestem() {
148148
Some(s) =>

src/librustc/metadata/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,7 @@ fn encode_meta_item(ebml_w: &mut writer::Encoder, mi: @MetaItem) {
14401440
}
14411441
MetaNameValue(name, value) => {
14421442
match value.node {
1443-
lit_str(value) => {
1443+
lit_str(value, _) => {
14441444
ebml_w.start_tag(tag_meta_item_name_value);
14451445
ebml_w.start_tag(tag_meta_item_name);
14461446
ebml_w.writer.write(name.as_bytes());

src/librustc/middle/check_const.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub fn check_pat(v: &mut CheckCrateVisitor, p: @Pat, _is_const: bool) {
8686
match e.node {
8787
ExprVstore(
8888
@Expr { node: ExprLit(@codemap::Spanned {
89-
node: lit_str(_),
89+
node: lit_str(*),
9090
_}),
9191
_ },
9292
ExprVstoreUniq
@@ -120,7 +120,7 @@ pub fn check_expr(v: &mut CheckCrateVisitor,
120120
"disallowed operator in constant expression");
121121
return;
122122
}
123-
ExprLit(@codemap::Spanned {node: lit_str(_), _}) => { }
123+
ExprLit(@codemap::Spanned {node: lit_str(*), _}) => { }
124124
ExprBinary(*) | ExprUnary(*) => {
125125
if method_map.contains_key(&e.id) {
126126
sess.span_err(e.span, "user-defined operators are not \

src/librustc/middle/const_eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ pub fn eval_const_expr_partial<T: ty::ExprTyProvider>(tcx: &T, e: &Expr)
475475

476476
pub fn lit_to_const(lit: &lit) -> const_val {
477477
match lit.node {
478-
lit_str(s) => const_str(s),
478+
lit_str(s, _) => const_str(s),
479479
lit_char(n) => const_uint(n as u64),
480480
lit_int(n, _) => const_int(n),
481481
lit_uint(n, _) => const_uint(n),

src/librustc/middle/trans/consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub fn const_lit(cx: &mut CrateContext, e: &ast::Expr, lit: ast::lit)
7171
}
7272
ast::lit_bool(b) => C_bool(b),
7373
ast::lit_nil => C_nil(),
74-
ast::lit_str(s) => C_estr_slice(cx, s)
74+
ast::lit_str(s, _) => C_estr_slice(cx, s)
7575
}
7676
}
7777

src/librustc/middle/trans/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ fn trans_rvalue_dps_unadjusted(bcx: @mut Block, expr: &ast::Expr,
705705
args.iter().enumerate().map(|(i, arg)| (i, *arg)).collect();
706706
return trans_adt(bcx, repr, 0, numbered_fields, None, dest);
707707
}
708-
ast::ExprLit(@codemap::Spanned {node: ast::lit_str(s), _}) => {
708+
ast::ExprLit(@codemap::Spanned {node: ast::lit_str(s, _), _}) => {
709709
return tvec::trans_lit_str(bcx, expr, s, dest);
710710
}
711711
ast::ExprVstore(contents, ast::ExprVstoreSlice) |

0 commit comments

Comments
 (0)