Skip to content

Commit 9787872

Browse files
committed
add token::LIT_STR_RAW(ident, num of # symbols)
Treat it as a synonym for LIT_STR for now.
1 parent 18099fe commit 9787872

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed

src/libsyntax/ext/base.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,8 @@ pub fn get_single_str_from_tts(cx: @ExtCtxt,
437437
}
438438

439439
match tts[0] {
440-
ast::tt_tok(_, token::LIT_STR(ident)) => cx.str_of(ident),
440+
ast::tt_tok(_, token::LIT_STR(ident))
441+
| ast::tt_tok(_, token::LIT_STR_RAW(ident, _)) => cx.str_of(ident),
441442
_ => cx.span_fatal(sp, format!("{} requires a string.", name)),
442443
}
443444
}

src/libsyntax/ext/quote.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,13 @@ fn mk_token(cx: @ExtCtxt, sp: Span, tok: &token::Token) -> @ast::Expr {
464464
~[mk_ident(cx, sp, ident)]);
465465
}
466466

467+
LIT_STR_RAW(ident, n) => {
468+
return cx.expr_call_ident(sp,
469+
id_ext("LIT_STR_RAW"),
470+
~[mk_ident(cx, sp, ident),
471+
cx.expr_uint(sp, n)]);
472+
}
473+
467474
IDENT(ident, b) => {
468475
return cx.expr_call_ident(sp,
469476
id_ext("IDENT"),

src/libsyntax/parse/lexer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ fn next_token_inner(rdr: @mut StringReader) -> token::Token {
883883
content_start_bpos,
884884
content_end_bpos,
885885
str_to_ident);
886-
return token::LIT_STR(str_content);
886+
return token::LIT_STR_RAW(str_content, hash_count);
887887
}
888888
'-' => {
889889
if nextch(rdr) == '>' {
@@ -1048,7 +1048,7 @@ mod test {
10481048
let TokenAndSpan {tok, sp: _} =
10491049
env.string_reader.next_token();
10501050
let id = token::str_to_ident("\"#a\\b\x00c\"");
1051-
assert_eq!(tok, token::LIT_STR(id));
1051+
assert_eq!(tok, token::LIT_STR_RAW(id, 3));
10521052
}
10531053

10541054
#[test] fn line_doc_comments() {

src/libsyntax/parse/parser.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,7 @@ impl Parser {
12831283
token::LIT_FLOAT_UNSUFFIXED(s) =>
12841284
lit_float_unsuffixed(self.id_to_str(s)),
12851285
token::LIT_STR(s) => lit_str(self.id_to_str(s)),
1286+
token::LIT_STR_RAW(s, _) => lit_str(self.id_to_str(s)),
12861287
token::LPAREN => { self.expect(&token::RPAREN); lit_nil },
12871288
_ => { self.unexpected_last(tok); }
12881289
}
@@ -4345,7 +4346,8 @@ impl Parser {
43454346
// parse a string as an ABI spec on an extern type or module
43464347
fn parse_opt_abis(&self) -> Option<AbiSet> {
43474348
match *self.token {
4348-
token::LIT_STR(s) => {
4349+
token::LIT_STR(s)
4350+
| token::LIT_STR_RAW(s, _) => {
43494351
self.bump();
43504352
let the_string = ident_to_str(&s);
43514353
let mut abis = AbiSet::empty();
@@ -4932,7 +4934,8 @@ impl Parser {
49324934

49334935
pub fn parse_optional_str(&self) -> Option<@str> {
49344936
match *self.token {
4935-
token::LIT_STR(s) => {
4937+
token::LIT_STR(s)
4938+
| token::LIT_STR_RAW(s, _) => {
49364939
self.bump();
49374940
Some(ident_to_str(&s))
49384941
}

src/libsyntax/parse/token.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pub enum Token {
7979
LIT_FLOAT(ast::Ident, ast::float_ty),
8080
LIT_FLOAT_UNSUFFIXED(ast::Ident),
8181
LIT_STR(ast::Ident),
82+
LIT_STR_RAW(ast::Ident, uint), /* raw str delimited by n hash symbols */
8283

8384
/* Name components */
8485
// an identifier contains an "is_mod_name" boolean,
@@ -194,6 +195,10 @@ pub fn to_str(input: @ident_interner, t: &Token) -> ~str {
194195
body
195196
}
196197
LIT_STR(ref s) => { format!("\"{}\"", ident_to_str(s).escape_default()) }
198+
LIT_STR_RAW(ref s, n) => {
199+
format!("r{delim}\"{string}\"{delim}",
200+
delim="#".repeat(n), string=ident_to_str(s))
201+
}
197202

198203
/* Name components */
199204
IDENT(s, _) => input.get(s.name).to_owned(),
@@ -243,6 +248,7 @@ pub fn can_begin_expr(t: &Token) -> bool {
243248
LIT_FLOAT(_, _) => true,
244249
LIT_FLOAT_UNSUFFIXED(_) => true,
245250
LIT_STR(_) => true,
251+
LIT_STR_RAW(_, _) => true,
246252
POUND => true,
247253
AT => true,
248254
NOT => true,
@@ -284,6 +290,7 @@ pub fn is_lit(t: &Token) -> bool {
284290
LIT_FLOAT(_, _) => true,
285291
LIT_FLOAT_UNSUFFIXED(_) => true,
286292
LIT_STR(_) => true,
293+
LIT_STR_RAW(_, _) => true,
287294
_ => false
288295
}
289296
}

0 commit comments

Comments
 (0)