about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGraydon Hoare <graydon@mozilla.com>2010-08-24 08:56:42 -0700
committerGraydon Hoare <graydon@mozilla.com>2010-08-24 08:56:42 -0700
commitca3ca041f39d34d76f7a5ee6fc8925bd2416e8aa (patch)
tree2e41e31ac0240636ba34e9aa6d48c2bdf17f37af
parentdf3ea680eae217a9677495d690dbf768ad7614e0 (diff)
Add very basic char / str literal handling to rustc lexer.
-rw-r--r--src/comp/fe/lexer.rs56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/comp/fe/lexer.rs b/src/comp/fe/lexer.rs
index 8885e6e0dff..10c16b8df88 100644
--- a/src/comp/fe/lexer.rs
+++ b/src/comp/fe/lexer.rs
@@ -126,6 +126,62 @@ fn next_token(stdio_reader rdr) -> token.token {
             }
         }
 
+        case ('\'') {
+            // FIXME: general utf8-consumption support.
+            auto c2 = next(rdr);
+            if (c2 == '\\') {
+                c2 = next(rdr);
+                alt (c2) {
+                    case ('n') { c2 = '\n'; }
+                    case ('r') { c2 = '\r'; }
+                    case ('t') { c2 = '\t'; }
+                    case ('\\') { c2 = '\\'; }
+                    case ('\'') { c2 = '\''; }
+                    // FIXME: unicode numeric escapes.
+                    case (_) {
+                        log "unknown character escape";
+                        log c2;
+                        fail;
+                    }
+                }
+            }
+            if (next(rdr) != '\'') {
+                log "unterminated character constant";
+                fail;
+            }
+            ret token.LIT_CHAR(c2);
+        }
+
+        case ('"') {
+            // FIXME: general utf8-consumption support.
+            auto c2 = next(rdr);
+            while (c2 != '"') {
+                alt (c2) {
+                    case ('\\') {
+                        c2 = next(rdr);
+                        alt (c2) {
+                            case ('n') { accum_str += '\n' as u8; }
+                            case ('r') { accum_str += '\r' as u8; }
+                            case ('t') { accum_str += '\t' as u8; }
+                            case ('\\') { accum_str += '\\' as u8; }
+                            case ('"') { accum_str += '"' as u8; }
+                            // FIXME: unicode numeric escapes.
+                            case (_) {
+                                log "unknown string escape";
+                                log c2;
+                                fail;
+                            }
+                        }
+                    }
+                    case (_) {
+                        accum_str += c2 as u8;
+                    }
+                }
+                c2 = next(rdr);
+            }
+            ret token.LIT_STR(accum_str);
+        }
+
         case ('-') {
             auto c2 = next(rdr);
             if (c2 == '>') {