From d6a922459ed3ebc77ba5d79cd65144078f43e321 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 31 Dec 2017 10:41:42 +0300 Subject: Lexer: basic chars & lifetimes --- src/lexer/mod.rs | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) (limited to 'src/lexer/mod.rs') diff --git a/src/lexer/mod.rs b/src/lexer/mod.rs index 24c14add0..3f277bd2b 100644 --- a/src/lexer/mod.rs +++ b/src/lexer/mod.rs @@ -34,7 +34,9 @@ fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind { } if is_dec_digit(c) { - return scan_number(c, ptr); + let kind = scan_number(c, ptr); + scan_literal_suffix(ptr); + return kind; } // One-byte tokens. @@ -98,6 +100,8 @@ fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind { } _ => NOT, }, + + // '\'' => scan_char_or_lifetime(ptr), _ => (), } ERROR @@ -116,6 +120,45 @@ fn scan_ident(c: char, ptr: &mut Ptr) -> SyntaxKind { IDENT } +fn scan_char_or_lifetime(ptr: &mut Ptr) -> SyntaxKind { + // Either a character constant 'a' OR a lifetime name 'abc + let c = match ptr.bump() { + Some(c) => c, + None => return CHAR, // TODO: error reporting is upper in the stack + }; + + // If the character is an ident start not followed by another single + // quote, then this is a lifetime name: + if is_ident_start(c) && !ptr.next_is('\'') { + while ptr.next_is_p(is_ident_continue) { + ptr.bump(); + } + + // lifetimes shouldn't end with a single quote + // if we find one, then this is an invalid character literal + if ptr.next_is('\'') { + ptr.bump(); + return CHAR; + } + return LIFETIME; + } + scan_char_or_byte(ptr); + if !ptr.next_is('\'') { + return CHAR; // TODO: error reporting + } + ptr.bump(); + scan_literal_suffix(ptr); + CHAR +} + +fn scan_literal_suffix(ptr: &mut Ptr) { + +} + +fn scan_char_or_byte(ptr: &mut Ptr) { + ptr.bump(); +} + fn string_literal_start(c: char, c1: Option, c2: Option) -> bool { match (c, c1, c2) { ('r', Some('"'), _) | -- cgit v1.2.3