From 34af7ccba9a78dbe69e593e767db247e26e02c32 Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Sat, 6 Jun 2020 19:49:50 +1000 Subject: [PATCH] scanner: cut keyword lookups in half --- vlib/v/scanner/scanner.v | 4 ++-- vlib/v/token/token.v | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index 23120503e9..b99cd8530a 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -580,8 +580,8 @@ pub fn (mut s Scanner) scan() token.Token { // tmp hack to detect . in ${} // Check if not .eof to prevent panic next_char := if s.pos + 1 < s.text.len { s.text[s.pos + 1] } else { `\0` } - if token.is_key(name) { - kind := token.key_to_token(name) + kind := token.keywords[name] + if kind != .unknown { if kind == .key_fn { s.struct_name = s.ident_struct_name() s.fn_name = s.ident_fn_name() diff --git a/vlib/v/token/token.v b/vlib/v/token/token.v index b6eaebff87..a881b5ec93 100644 --- a/vlib/v/token/token.v +++ b/vlib/v/token/token.v @@ -14,6 +14,7 @@ pub: } pub enum Kind { + unknown eof name // user number // 123 @@ -133,8 +134,8 @@ const ( ) // build_keys genereates a map with keywords' string values: // Keywords['return'] == .key_return -fn build_keys() map[string]int { - mut res := map[string]int +fn build_keys() map[string]Kind { + mut res := map[string]Kind for t in int(Kind.keyword_beg) + 1 .. int(Kind.keyword_end) { key := token_str[t] res[key] = t @@ -145,6 +146,7 @@ fn build_keys() map[string]int { // TODO remove once we have `enum Kind { name('name') if('if') ... }` fn build_token_str() []string { mut s := [''].repeat(nr_tokens) + s[Kind.unknown] = 'unknown' s[Kind.eof] = 'eof' s[Kind.name] = 'name' s[Kind.number] = 'number'