about summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-11-05 03:31:33 +0000
committerbors <bors@rust-lang.org>2014-11-05 03:31:33 +0000
commit4375b32dabf8096a1137a68c1070fc9a9292cb06 (patch)
tree99a0794fa4839b48edcf0a3212392bb775f4982c /src/libsyntax
parentceeac26de859bd217d8ff576ff291dbd38ff3951 (diff)
parente8d6031c71a5ab42648f26a253671ba17584407a (diff)
downloadrust-4375b32dabf8096a1137a68c1070fc9a9292cb06.tar.gz
rust-4375b32dabf8096a1137a68c1070fc9a9292cb06.zip
auto merge of #18504 : pcwalton/rust/small-escapes, r=pcwalton
Use `\u0080`-`\u00ff` instead. ASCII/byte literals are unaffected.

This PR introduces a new function, `escape_default`, into the ASCII
module. This was necessary for the pretty printer to continue to
function.

RFC #326.

Closes #18062.

[breaking-change]

r? @aturon
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/parse/lexer/mod.rs23
-rw-r--r--src/libsyntax/print/pprust.rs11
2 files changed, 27 insertions, 7 deletions
diff --git a/src/libsyntax/parse/lexer/mod.rs b/src/libsyntax/parse/lexer/mod.rs
index 1bc1d42d888..de5004fe2f8 100644
--- a/src/libsyntax/parse/lexer/mod.rs
+++ b/src/libsyntax/parse/lexer/mod.rs
@@ -720,7 +720,11 @@ impl<'a> StringReader<'a> {
 
     /// Scan over `n_digits` hex digits, stopping at `delim`, reporting an
     /// error if too many or too few digits are encountered.
-    fn scan_hex_digits(&mut self, n_digits: uint, delim: char) -> bool {
+    fn scan_hex_digits(&mut self,
+                       n_digits: uint,
+                       delim: char,
+                       below_0x7f_only: bool)
+                       -> bool {
         debug!("scanning {} digits until {}", n_digits, delim);
         let start_bpos = self.last_pos;
         let mut accum_int = 0;
@@ -745,6 +749,13 @@ impl<'a> StringReader<'a> {
             self.bump();
         }
 
+        if below_0x7f_only && accum_int >= 0x80 {
+            self.err_span_(start_bpos,
+                           self.last_pos,
+                           "this form of character escape may only be used \
+                            with characters in the range [\\x00-\\x7f]");
+        }
+
         match char::from_u32(accum_int) {
             Some(_) => true,
             None => {
@@ -773,9 +784,13 @@ impl<'a> StringReader<'a> {
                     Some(e) => {
                         return match e {
                             'n' | 'r' | 't' | '\\' | '\'' | '"' | '0' => true,
-                            'x' => self.scan_hex_digits(2u, delim),
-                            'u' if !ascii_only => self.scan_hex_digits(4u, delim),
-                            'U' if !ascii_only => self.scan_hex_digits(8u, delim),
+                            'x' => self.scan_hex_digits(2u, delim, !ascii_only),
+                            'u' if !ascii_only => {
+                                self.scan_hex_digits(4u, delim, false)
+                            }
+                            'U' if !ascii_only => {
+                                self.scan_hex_digits(8u, delim, false)
+                            }
                             '\n' if delim == '"' => {
                                 self.consume_whitespace();
                                 true
diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs
index 4cae3691f2a..106e3f1faae 100644
--- a/src/libsyntax/print/pprust.rs
+++ b/src/libsyntax/print/pprust.rs
@@ -30,6 +30,7 @@ use print::pp::{Breaks, Consistent, Inconsistent, eof};
 use print::pp;
 use ptr::P;
 
+use std::ascii;
 use std::io::{IoResult, MemWriter};
 use std::io;
 use std::mem;
@@ -2776,7 +2777,7 @@ impl<'a> State<'a> {
             ast::LitStr(ref st, style) => self.print_string(st.get(), style),
             ast::LitByte(byte) => {
                 let mut res = String::from_str("b'");
-                (byte as char).escape_default(|c| res.push(c));
+                ascii::escape_default(byte, |c| res.push(c as char));
                 res.push('\'');
                 word(&mut self.s, res.as_slice())
             }
@@ -2821,8 +2822,12 @@ impl<'a> State<'a> {
                 if val { word(&mut self.s, "true") } else { word(&mut self.s, "false") }
             }
             ast::LitBinary(ref v) => {
-                let escaped: String = v.iter().map(|&b| b as char).collect();
-                word(&mut self.s, format!("b\"{}\"", escaped.escape_default()).as_slice())
+                let mut escaped: String = String::new();
+                for &ch in v.iter() {
+                    ascii::escape_default(ch as u8,
+                                          |ch| escaped.push(ch as char));
+                }
+                word(&mut self.s, format!("b\"{}\"", escaped).as_slice())
             }
         }
     }