Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement raw strings (r#"foo"#) #9674

Merged
merged 7 commits into from
Oct 8, 2013
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add token::LIT_STR_RAW(ident, num of # symbols)
Treat it as a synonym for LIT_STR for now.
  • Loading branch information
ben0x539 committed Oct 7, 2013
commit 97878725532c4d1dd1af07e88175462178d78cdb
3 changes: 2 additions & 1 deletion src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
@@ -437,7 +437,8 @@ pub fn get_single_str_from_tts(cx: @ExtCtxt,
}

match tts[0] {
ast::tt_tok(_, token::LIT_STR(ident)) => cx.str_of(ident),
ast::tt_tok(_, token::LIT_STR(ident))
| ast::tt_tok(_, token::LIT_STR_RAW(ident, _)) => cx.str_of(ident),
_ => cx.span_fatal(sp, format!("{} requires a string.", name)),
}
}
7 changes: 7 additions & 0 deletions src/libsyntax/ext/quote.rs
Original file line number Diff line number Diff line change
@@ -464,6 +464,13 @@ fn mk_token(cx: @ExtCtxt, sp: Span, tok: &token::Token) -> @ast::Expr {
~[mk_ident(cx, sp, ident)]);
}

LIT_STR_RAW(ident, n) => {
return cx.expr_call_ident(sp,
id_ext("LIT_STR_RAW"),
~[mk_ident(cx, sp, ident),
cx.expr_uint(sp, n)]);
}

IDENT(ident, b) => {
return cx.expr_call_ident(sp,
id_ext("IDENT"),
4 changes: 2 additions & 2 deletions src/libsyntax/parse/lexer.rs
Original file line number Diff line number Diff line change
@@ -883,7 +883,7 @@ fn next_token_inner(rdr: @mut StringReader) -> token::Token {
content_start_bpos,
content_end_bpos,
str_to_ident);
return token::LIT_STR(str_content);
return token::LIT_STR_RAW(str_content, hash_count);
}
'-' => {
if nextch(rdr) == '>' {
@@ -1048,7 +1048,7 @@ mod test {
let TokenAndSpan {tok, sp: _} =
env.string_reader.next_token();
let id = token::str_to_ident("\"#a\\b\x00c\"");
assert_eq!(tok, token::LIT_STR(id));
assert_eq!(tok, token::LIT_STR_RAW(id, 3));
}

#[test] fn line_doc_comments() {
7 changes: 5 additions & 2 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
@@ -1283,6 +1283,7 @@ impl Parser {
token::LIT_FLOAT_UNSUFFIXED(s) =>
lit_float_unsuffixed(self.id_to_str(s)),
token::LIT_STR(s) => lit_str(self.id_to_str(s)),
token::LIT_STR_RAW(s, _) => lit_str(self.id_to_str(s)),
token::LPAREN => { self.expect(&token::RPAREN); lit_nil },
_ => { self.unexpected_last(tok); }
}
@@ -4345,7 +4346,8 @@ impl Parser {
// parse a string as an ABI spec on an extern type or module
fn parse_opt_abis(&self) -> Option<AbiSet> {
match *self.token {
token::LIT_STR(s) => {
token::LIT_STR(s)
| token::LIT_STR_RAW(s, _) => {
self.bump();
let the_string = ident_to_str(&s);
let mut abis = AbiSet::empty();
@@ -4932,7 +4934,8 @@ impl Parser {

pub fn parse_optional_str(&self) -> Option<@str> {
match *self.token {
token::LIT_STR(s) => {
token::LIT_STR(s)
| token::LIT_STR_RAW(s, _) => {
self.bump();
Some(ident_to_str(&s))
}
7 changes: 7 additions & 0 deletions src/libsyntax/parse/token.rs
Original file line number Diff line number Diff line change
@@ -79,6 +79,7 @@ pub enum Token {
LIT_FLOAT(ast::Ident, ast::float_ty),
LIT_FLOAT_UNSUFFIXED(ast::Ident),
LIT_STR(ast::Ident),
LIT_STR_RAW(ast::Ident, uint), /* raw str delimited by n hash symbols */

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

/* Name components */
IDENT(s, _) => input.get(s.name).to_owned(),
@@ -243,6 +248,7 @@ pub fn can_begin_expr(t: &Token) -> bool {
LIT_FLOAT(_, _) => true,
LIT_FLOAT_UNSUFFIXED(_) => true,
LIT_STR(_) => true,
LIT_STR_RAW(_, _) => true,
POUND => true,
AT => true,
NOT => true,
@@ -284,6 +290,7 @@ pub fn is_lit(t: &Token) -> bool {
LIT_FLOAT(_, _) => true,
LIT_FLOAT_UNSUFFIXED(_) => true,
LIT_STR(_) => true,
LIT_STR_RAW(_, _) => true,
_ => false
}
}