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

35 files changed

+274
-80
lines changed

Diff for: doc/rust.md

+32-4
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}

Diff for: doc/tutorial.md

+6-1
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

Diff for: src/etc/vim/syntax/rust.vim

+1
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

Diff for: src/librustc/front/test.rs

+1-1
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,

Diff for: src/librustc/metadata/creader.rs

+1-1
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) =>

Diff for: src/librustc/metadata/encoder.rs

+1-1
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());

Diff for: src/librustc/middle/check_const.rs

+2-2
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 \

Diff for: src/librustc/middle/const_eval.rs

+1-1
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),

Diff for: src/librustc/middle/trans/consts.rs

+1-1
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

Diff for: src/librustc/middle/trans/expr.rs

+1-1
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) |

Diff for: src/librustc/middle/trans/tvec.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ pub fn trans_slice_vstore(bcx: @mut Block,
205205

206206
// Handle the &"..." case:
207207
match content_expr.node {
208-
ast::ExprLit(@codemap::Spanned {node: ast::lit_str(s), span: _}) => {
208+
ast::ExprLit(@codemap::Spanned {node: ast::lit_str(s, _), span: _}) => {
209209
return trans_lit_str(bcx, content_expr, s, dest);
210210
}
211211
_ => {}
@@ -296,7 +296,7 @@ pub fn trans_uniq_or_managed_vstore(bcx: @mut Block, heap: heap, vstore_expr: &a
296296
heap_exchange => {
297297
match content_expr.node {
298298
ast::ExprLit(@codemap::Spanned {
299-
node: ast::lit_str(s), span
299+
node: ast::lit_str(s, _), span
300300
}) => {
301301
let llptrval = C_cstr(bcx.ccx(), s);
302302
let llptrval = PointerCast(bcx, llptrval, Type::i8p());
@@ -357,7 +357,7 @@ pub fn write_content(bcx: @mut Block,
357357
let _indenter = indenter();
358358

359359
match content_expr.node {
360-
ast::ExprLit(@codemap::Spanned { node: ast::lit_str(s), _ }) => {
360+
ast::ExprLit(@codemap::Spanned { node: ast::lit_str(s, _), _ }) => {
361361
match dest {
362362
Ignore => {
363363
return bcx;
@@ -490,7 +490,7 @@ pub fn elements_required(bcx: @mut Block, content_expr: &ast::Expr) -> uint {
490490
//! Figure out the number of elements we need to store this content
491491
492492
match content_expr.node {
493-
ast::ExprLit(@codemap::Spanned { node: ast::lit_str(s), _ }) => {
493+
ast::ExprLit(@codemap::Spanned { node: ast::lit_str(s, _), _ }) => {
494494
s.len()
495495
},
496496
ast::ExprVec(ref es, _) => es.len(),

Diff for: src/librustc/middle/ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3266,7 +3266,7 @@ pub fn expr_kind(tcx: ctxt,
32663266
ast::ExprDoBody(*) |
32673267
ast::ExprBlock(*) |
32683268
ast::ExprRepeat(*) |
3269-
ast::ExprLit(@codemap::Spanned {node: lit_str(_), _}) |
3269+
ast::ExprLit(@codemap::Spanned {node: lit_str(*), _}) |
32703270
ast::ExprVstore(_, ast::ExprVstoreSlice) |
32713271
ast::ExprVstore(_, ast::ExprVstoreMutSlice) |
32723272
ast::ExprVec(*) => {

Diff for: src/librustc/middle/typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2259,7 +2259,7 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt,
22592259
match expr.node {
22602260
ast::ExprVstore(ev, vst) => {
22612261
let typ = match ev.node {
2262-
ast::ExprLit(@codemap::Spanned { node: ast::lit_str(_), _ }) => {
2262+
ast::ExprLit(@codemap::Spanned { node: ast::lit_str(*), _ }) => {
22632263
let tt = ast_expr_vstore_to_vstore(fcx, ev, vst);
22642264
ty::mk_estr(tcx, tt)
22652265
}

Diff for: src/librustdoc/clean.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ impl Clean<ViewItemInner> for ast::view_item_ {
10081008
fn clean(&self) -> ViewItemInner {
10091009
match self {
10101010
&ast::view_item_extern_mod(ref i, ref p, ref mi, ref id) =>
1011-
ExternMod(i.clean(), p.map(|x| x.to_owned()), mi.clean(), *id),
1011+
ExternMod(i.clean(), p.map(|&(ref x, _)| x.to_owned()), mi.clean(), *id),
10121012
&ast::view_item_use(ref vp) => Import(vp.clean())
10131013
}
10141014
}
@@ -1114,7 +1114,7 @@ impl ToSource for syntax::codemap::Span {
11141114

11151115
fn lit_to_str(lit: &ast::lit) -> ~str {
11161116
match lit.node {
1117-
ast::lit_str(st) => st.to_owned(),
1117+
ast::lit_str(st, _) => st.to_owned(),
11181118
ast::lit_char(c) => ~"'" + std::char::from_u32(c).unwrap().to_str() + "'",
11191119
ast::lit_int(i, _t) => i.to_str(),
11201120
ast::lit_uint(u, _t) => u.to_str(),

Diff for: src/librustpkg/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ impl<'self> Visitor<()> for ViewItemVisitor<'self> {
406406
// ignore metadata, I guess
407407
ast::view_item_extern_mod(lib_ident, path_opt, _, _) => {
408408
let lib_name = match path_opt {
409-
Some(p) => p,
409+
Some((p, _)) => p,
410410
None => self.sess.str_of(lib_ident)
411411
};
412412
debug2!("Finding and installing... {}", lib_name);
@@ -513,7 +513,7 @@ pub fn find_and_install_dependencies(context: &BuildContext,
513513

514514
pub fn mk_string_lit(s: @str) -> ast::lit {
515515
Spanned {
516-
node: ast::lit_str(s),
516+
node: ast::lit_str(s, ast::CookedStr),
517517
span: dummy_sp()
518518
}
519519
}

Diff for: src/libsyntax/ast.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -680,11 +680,17 @@ pub enum mac_ {
680680
mac_invoc_tt(Path,~[token_tree],SyntaxContext), // new macro-invocation
681681
}
682682

683+
#[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
684+
pub enum StrStyle {
685+
CookedStr,
686+
RawStr(uint)
687+
}
688+
683689
pub type lit = Spanned<lit_>;
684690

685691
#[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
686692
pub enum lit_ {
687-
lit_str(@str),
693+
lit_str(@str, StrStyle),
688694
lit_char(u32),
689695
lit_int(i64, int_ty),
690696
lit_uint(u64, uint_ty),
@@ -862,6 +868,7 @@ pub enum asm_dialect {
862868
#[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
863869
pub struct inline_asm {
864870
asm: @str,
871+
asm_str_style: StrStyle,
865872
clobbers: @str,
866873
inputs: ~[(@str, @Expr)],
867874
outputs: ~[(@str, @Expr)],
@@ -1027,7 +1034,7 @@ pub enum view_item_ {
10271034
// optional @str: if present, this is a location (containing
10281035
// arbitrary characters) from which to fetch the crate sources
10291036
// For example, extern mod whatever = "github.com/mozilla/rust"
1030-
view_item_extern_mod(Ident, Option<@str>, ~[@MetaItem], NodeId),
1037+
view_item_extern_mod(Ident, Option<(@str, StrStyle)>, ~[@MetaItem], NodeId),
10311038
view_item_use(~[@view_path]),
10321039
}
10331040

Diff for: src/libsyntax/attr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl AttrMetaMethods for MetaItem {
6767
match self.node {
6868
MetaNameValue(_, ref v) => {
6969
match v.node {
70-
ast::lit_str(s) => Some(s),
70+
ast::lit_str(s, _) => Some(s),
7171
_ => None,
7272
}
7373
},
@@ -127,7 +127,7 @@ impl AttributeMethods for Attribute {
127127
/* Constructors */
128128

129129
pub fn mk_name_value_item_str(name: @str, value: @str) -> @MetaItem {
130-
let value_lit = dummy_spanned(ast::lit_str(value));
130+
let value_lit = dummy_spanned(ast::lit_str(value, ast::CookedStr));
131131
mk_name_value_item(name, value_lit)
132132
}
133133

@@ -153,7 +153,7 @@ pub fn mk_attr(item: @MetaItem) -> Attribute {
153153

154154
pub fn mk_sugared_doc_attr(text: @str, lo: BytePos, hi: BytePos) -> Attribute {
155155
let style = doc_comment_style(text);
156-
let lit = spanned(lo, hi, ast::lit_str(text));
156+
let lit = spanned(lo, hi, ast::lit_str(text, ast::CookedStr));
157157
let attr = Attribute_ {
158158
style: style,
159159
value: @spanned(lo, hi, MetaNameValue(@"doc", lit)),

Diff for: src/libsyntax/ext/asm.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub fn expand_asm(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
4444
tts.to_owned());
4545

4646
let mut asm = @"";
47+
let mut asm_str_style = None;
4748
let mut outputs = ~[];
4849
let mut inputs = ~[];
4950
let mut cons = ~"";
@@ -58,8 +59,11 @@ pub fn expand_asm(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
5859
while continue_ {
5960
match state {
6061
Asm => {
61-
asm = expr_to_str(cx, p.parse_expr(),
62-
"inline assembly must be a string literal.");
62+
let (s, style) =
63+
expr_to_str(cx, p.parse_expr(),
64+
"inline assembly must be a string literal.");
65+
asm = s;
66+
asm_str_style = Some(style);
6367
}
6468
Outputs => {
6569
while *p.token != token::EOF &&
@@ -70,7 +74,7 @@ pub fn expand_asm(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
7074
p.eat(&token::COMMA);
7175
}
7276

73-
let constraint = p.parse_str();
77+
let (constraint, _str_style) = p.parse_str();
7478
p.expect(&token::LPAREN);
7579
let out = p.parse_expr();
7680
p.expect(&token::RPAREN);
@@ -93,7 +97,7 @@ pub fn expand_asm(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
9397
p.eat(&token::COMMA);
9498
}
9599

96-
let constraint = p.parse_str();
100+
let (constraint, _str_style) = p.parse_str();
97101
p.expect(&token::LPAREN);
98102
let input = p.parse_expr();
99103
p.expect(&token::RPAREN);
@@ -111,14 +115,15 @@ pub fn expand_asm(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
111115
p.eat(&token::COMMA);
112116
}
113117

114-
let clob = format!("~\\{{}\\}", p.parse_str());
118+
let (s, _str_style) = p.parse_str();
119+
let clob = format!("~\\{{}\\}", s);
115120
clobs.push(clob);
116121
}
117122

118123
cons = clobs.connect(",");
119124
}
120125
Options => {
121-
let option = p.parse_str();
126+
let (option, _str_style) = p.parse_str();
122127

123128
if "volatile" == option {
124129
volatile = true;
@@ -175,6 +180,7 @@ pub fn expand_asm(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
175180
id: ast::DUMMY_NODE_ID,
176181
node: ast::ExprInlineAsm(ast::inline_asm {
177182
asm: asm,
183+
asm_str_style: asm_str_style.unwrap(),
178184
clobbers: cons.to_managed(),
179185
inputs: inputs,
180186
outputs: outputs,

0 commit comments

Comments
 (0)