about summary refs log tree commit diff
path: root/src/libsyntax/ext
diff options
context:
space:
mode:
authorBenjamin Herr <ben@0x539.de>2013-10-08 02:49:10 +0200
committerBenjamin Herr <ben@0x539.de>2013-10-08 03:43:28 +0200
commit9d7b13004192b8eef1f68501035d05c85dee8c47 (patch)
treec96d00a2f583c825b91836764219440f5b9cb7b6 /src/libsyntax/ext
parent97878725532c4d1dd1af07e88175462178d78cdb (diff)
downloadrust-9d7b13004192b8eef1f68501035d05c85dee8c47.tar.gz
rust-9d7b13004192b8eef1f68501035d05c85dee8c47.zip
add new enum ast::StrStyle as field to ast::lit_str
For the benefit of the pretty printer we want to keep track of how
string literals in the ast were originally represented in the source
code.

This commit changes parser functions so they don't extract strings from
the token stream without at least also returning what style of string
literal it was. This is stored in the resulting ast node for string
literals, obviously, for the package id in `extern mod = r"package id"`
view items, for the inline asm in `asm!()` invocations.

For `asm!()`'s other arguments or for `extern "Rust" fn()` items, I just
the style of string, because it seemed disproportionally cumbersome to
thread that information through the string processing that happens with
those string literals, given the limited advantage raw string literals
would provide in these positions.

The other syntax extensions don't seem to store passed string literals
in the ast, so they also discard the style of strings they parse.
Diffstat (limited to 'src/libsyntax/ext')
-rw-r--r--src/libsyntax/ext/asm.rs18
-rw-r--r--src/libsyntax/ext/base.rs4
-rw-r--r--src/libsyntax/ext/build.rs2
-rw-r--r--src/libsyntax/ext/bytes.rs2
-rw-r--r--src/libsyntax/ext/deriving/generic.rs2
-rw-r--r--src/libsyntax/ext/env.rs7
-rw-r--r--src/libsyntax/ext/fmt.rs2
-rw-r--r--src/libsyntax/ext/format.rs4
-rw-r--r--src/libsyntax/ext/quote.rs2
9 files changed, 26 insertions, 17 deletions
diff --git a/src/libsyntax/ext/asm.rs b/src/libsyntax/ext/asm.rs
index 9241e8c4fbc..e836367555a 100644
--- a/src/libsyntax/ext/asm.rs
+++ b/src/libsyntax/ext/asm.rs
@@ -44,6 +44,7 @@ pub fn expand_asm(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
                                        tts.to_owned());
 
     let mut asm = @"";
+    let mut asm_str_style = None;
     let mut outputs = ~[];
     let mut inputs = ~[];
     let mut cons = ~"";
@@ -58,8 +59,11 @@ pub fn expand_asm(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
     while continue_ {
         match state {
             Asm => {
-                asm = expr_to_str(cx, p.parse_expr(),
-                                  "inline assembly must be a string literal.");
+                let (s, style) =
+                    expr_to_str(cx, p.parse_expr(),
+                                "inline assembly must be a string literal.");
+                asm = s;
+                asm_str_style = Some(style);
             }
             Outputs => {
                 while *p.token != token::EOF &&
@@ -70,7 +74,7 @@ pub fn expand_asm(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
                         p.eat(&token::COMMA);
                     }
 
-                    let constraint = p.parse_str();
+                    let (constraint, _str_style) = p.parse_str();
                     p.expect(&token::LPAREN);
                     let out = p.parse_expr();
                     p.expect(&token::RPAREN);
@@ -93,7 +97,7 @@ pub fn expand_asm(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
                         p.eat(&token::COMMA);
                     }
 
-                    let constraint = p.parse_str();
+                    let (constraint, _str_style) = p.parse_str();
                     p.expect(&token::LPAREN);
                     let input = p.parse_expr();
                     p.expect(&token::RPAREN);
@@ -111,14 +115,15 @@ pub fn expand_asm(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
                         p.eat(&token::COMMA);
                     }
 
-                    let clob = format!("~\\{{}\\}", p.parse_str());
+                    let (s, _str_style) = p.parse_str();
+                    let clob = format!("~\\{{}\\}", s);
                     clobs.push(clob);
                 }
 
                 cons = clobs.connect(",");
             }
             Options => {
-                let option = p.parse_str();
+                let (option, _str_style) = p.parse_str();
 
                 if "volatile" == option {
                     volatile = true;
@@ -175,6 +180,7 @@ pub fn expand_asm(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
         id: ast::DUMMY_NODE_ID,
         node: ast::ExprInlineAsm(ast::inline_asm {
             asm: asm,
+            asm_str_style: asm_str_style.unwrap(),
             clobbers: cons.to_managed(),
             inputs: inputs,
             outputs: outputs,
diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs
index 5a3c3a86fed..7f89271927c 100644
--- a/src/libsyntax/ext/base.rs
+++ b/src/libsyntax/ext/base.rs
@@ -410,10 +410,10 @@ impl ExtCtxt {
     }
 }
 
-pub fn expr_to_str(cx: @ExtCtxt, expr: @ast::Expr, err_msg: &str) -> @str {
+pub fn expr_to_str(cx: @ExtCtxt, expr: @ast::Expr, err_msg: &str) -> (@str, ast::StrStyle) {
     match expr.node {
       ast::ExprLit(l) => match l.node {
-        ast::lit_str(s) => s,
+        ast::lit_str(s, style) => (s, style),
         _ => cx.span_fatal(l.span, err_msg)
       },
       _ => cx.span_fatal(expr.span, err_msg)
diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs
index 65a6572fa5e..a5336187200 100644
--- a/src/libsyntax/ext/build.rs
+++ b/src/libsyntax/ext/build.rs
@@ -562,7 +562,7 @@ impl AstBuilder for @ExtCtxt {
         self.expr_vstore(sp, self.expr_vec(sp, exprs), ast::ExprVstoreSlice)
     }
     fn expr_str(&self, sp: Span, s: @str) -> @ast::Expr {
-        self.expr_lit(sp, ast::lit_str(s))
+        self.expr_lit(sp, ast::lit_str(s, ast::CookedStr))
     }
     fn expr_str_uniq(&self, sp: Span, s: @str) -> @ast::Expr {
         self.expr_vstore(sp, self.expr_str(sp, s), ast::ExprVstoreUniq)
diff --git a/src/libsyntax/ext/bytes.rs b/src/libsyntax/ext/bytes.rs
index b27fcb6c9b9..5ebaea2ce44 100644
--- a/src/libsyntax/ext/bytes.rs
+++ b/src/libsyntax/ext/bytes.rs
@@ -28,7 +28,7 @@ pub fn expand_syntax_ext(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> bas
             // expression is a literal
             ast::ExprLit(lit) => match lit.node {
                 // string literal, push each byte to vector expression
-                ast::lit_str(s) => {
+                ast::lit_str(s, _) => {
                     for byte in s.byte_iter() {
                         bytes.push(cx.expr_u8(expr.span, byte));
                     }
diff --git a/src/libsyntax/ext/deriving/generic.rs b/src/libsyntax/ext/deriving/generic.rs
index b3fd4f920d8..c31c609d4e7 100644
--- a/src/libsyntax/ext/deriving/generic.rs
+++ b/src/libsyntax/ext/deriving/generic.rs
@@ -361,7 +361,7 @@ impl<'self> TraitDef<'self> {
             span,
             cx.meta_name_value(span,
                                @"doc",
-                               ast::lit_str(@"Automatically derived.")));
+                               ast::lit_str(@"Automatically derived.", ast::CookedStr)));
         cx.item(
             span,
             ::parse::token::special_idents::clownshoes_extensions,
diff --git a/src/libsyntax/ext/env.rs b/src/libsyntax/ext/env.rs
index 63a45b06e16..15630e37ead 100644
--- a/src/libsyntax/ext/env.rs
+++ b/src/libsyntax/ext/env.rs
@@ -41,10 +41,13 @@ pub fn expand_env(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
         cx.span_fatal(sp, "env! takes 1 or 2 arguments");
     }
 
-    let var = expr_to_str(cx, exprs[0], "expected string literal");
+    let (var, _var_str_style) = expr_to_str(cx, exprs[0], "expected string literal");
     let msg = match exprs.len() {
         1 => format!("Environment variable {} not defined", var).to_managed(),
-        2 => expr_to_str(cx, exprs[1], "expected string literal"),
+        2 => {
+            let (s, _style) = expr_to_str(cx, exprs[1], "expected string literal");
+            s
+        }
         _ => cx.span_fatal(sp, "env! takes 1 or 2 arguments")
     };
 
diff --git a/src/libsyntax/ext/fmt.rs b/src/libsyntax/ext/fmt.rs
index cd364e7ad64..8258048a04d 100644
--- a/src/libsyntax/ext/fmt.rs
+++ b/src/libsyntax/ext/fmt.rs
@@ -30,7 +30,7 @@ pub fn expand_syntax_ext(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
     if args.len() == 0 {
         cx.span_fatal(sp, "fmt! takes at least 1 argument.");
     }
-    let fmt =
+    let (fmt, _fmt_str_style) =
         expr_to_str(cx, args[0],
                     "first argument to fmt! must be a string literal.");
     let fmtspan = args[0].span;
diff --git a/src/libsyntax/ext/format.rs b/src/libsyntax/ext/format.rs
index 8d327de6d61..171748e9b2e 100644
--- a/src/libsyntax/ext/format.rs
+++ b/src/libsyntax/ext/format.rs
@@ -722,8 +722,8 @@ pub fn expand_args(ecx: @ExtCtxt, sp: Span,
         (_, None) => { return MRExpr(ecx.expr_uint(sp, 2)); }
     };
     cx.fmtsp = efmt.span;
-    let fmt = expr_to_str(ecx, efmt,
-                          "format argument must be a string literal.");
+    let (fmt, _fmt_str_style) = expr_to_str(ecx, efmt,
+                                            "format argument must be a string literal.");
 
     let mut err = false;
     do parse::parse_error::cond.trap(|m| {
diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs
index 59d55933fa3..4bef9601855 100644
--- a/src/libsyntax/ext/quote.rs
+++ b/src/libsyntax/ext/quote.rs
@@ -118,7 +118,7 @@ pub mod rt {
 
     impl<'self> ToSource for &'self str {
         fn to_source(&self) -> @str {
-            let lit = dummy_spanned(ast::lit_str(self.to_managed()));
+            let lit = dummy_spanned(ast::lit_str(self.to_managed(), ast::CookedStr));
             pprust::lit_to_str(@lit).to_managed()
         }
     }