scanner: cut keyword lookups in half

pull/5235/head
joe-conigliaro 2020-06-06 19:49:50 +10:00
parent a19aaf2b90
commit 34af7ccba9
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
2 changed files with 6 additions and 4 deletions

View File

@ -580,8 +580,8 @@ pub fn (mut s Scanner) scan() token.Token {
// tmp hack to detect . in ${} // tmp hack to detect . in ${}
// Check if not .eof to prevent panic // Check if not .eof to prevent panic
next_char := if s.pos + 1 < s.text.len { s.text[s.pos + 1] } else { `\0` } next_char := if s.pos + 1 < s.text.len { s.text[s.pos + 1] } else { `\0` }
if token.is_key(name) { kind := token.keywords[name]
kind := token.key_to_token(name) if kind != .unknown {
if kind == .key_fn { if kind == .key_fn {
s.struct_name = s.ident_struct_name() s.struct_name = s.ident_struct_name()
s.fn_name = s.ident_fn_name() s.fn_name = s.ident_fn_name()

View File

@ -14,6 +14,7 @@ pub:
} }
pub enum Kind { pub enum Kind {
unknown
eof eof
name // user name // user
number // 123 number // 123
@ -133,8 +134,8 @@ const (
) )
// build_keys genereates a map with keywords' string values: // build_keys genereates a map with keywords' string values:
// Keywords['return'] == .key_return // Keywords['return'] == .key_return
fn build_keys() map[string]int { fn build_keys() map[string]Kind {
mut res := map[string]int mut res := map[string]Kind
for t in int(Kind.keyword_beg) + 1 .. int(Kind.keyword_end) { for t in int(Kind.keyword_beg) + 1 .. int(Kind.keyword_end) {
key := token_str[t] key := token_str[t]
res[key] = 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') ... }` // TODO remove once we have `enum Kind { name('name') if('if') ... }`
fn build_token_str() []string { fn build_token_str() []string {
mut s := [''].repeat(nr_tokens) mut s := [''].repeat(nr_tokens)
s[Kind.unknown] = 'unknown'
s[Kind.eof] = 'eof' s[Kind.eof] = 'eof'
s[Kind.name] = 'name' s[Kind.name] = 'name'
s[Kind.number] = 'number' s[Kind.number] = 'number'