diff src/ast_dump.rs @ 91:859d44f143b8

Implement Bytes in the AST.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 25 Jun 2016 02:08:50 +0100
parents 898876834564
children 7977a52c3202
line wrap: on
line diff
--- a/src/ast_dump.rs
+++ b/src/ast_dump.rs
@@ -196,6 +196,28 @@ impl ToStringable for expr {
             }),
             expr::Num(n) => format!("{}", n),
             expr::Str(s) => format!("\"{}\"", s),
+            expr::Bytes(s) => format!("b\"{}\"", {
+                let mut string = String::with_capacity(s.len());
+                for ascii_code in s {
+                    let c = ascii_code as char;
+                    if c >= ' ' && c <= '~' {
+                        if c == '"' || c == '\\' {
+                            string.push('\\');
+                        }
+                        string.push(c);
+                    } else if c == '\t' {
+                        string.push_str("\\t");
+                    } else if c == '\n' {
+                        string.push_str("\\n");
+                    } else if c == '\r' {
+                        string.push_str("\\r");
+                    } else /* if c > '~' */ {
+                        let value = format!("\\x{:02x}", ascii_code);
+                        string.push_str(value.as_str());
+                    }
+                }
+                string
+            }),
             expr::NameConstant(constant) => match constant {
                 name_constant::True => String::from("True"),
                 name_constant::False => String::from("False"),