summaryrefslogtreecommitdiff
path: root/tokenizer.sml
diff options
context:
space:
mode:
authorVladimir Azarov <avm@intermediate-node.net>2025-03-27 23:31:10 +0100
committerVladimir Azarov <avm@intermediate-node.net>2025-03-27 23:31:10 +0100
commitd7d4830443f1e385af862462f976553c8a9033e1 (patch)
tree0fbdef6579a26e962b249c55e6ae407cd5b8dfda /tokenizer.sml
parent11e14dd4b93154964c87fc97cfcee62c52edf97a (diff)
Hashtable-based findKeyword function
Diffstat (limited to 'tokenizer.sml')
-rw-r--r--tokenizer.sml32
1 files changed, 22 insertions, 10 deletions
diff --git a/tokenizer.sml b/tokenizer.sml
index 41f8d38..104ae61 100644
--- a/tokenizer.sml
+++ b/tokenizer.sml
@@ -8,7 +8,6 @@ structure Tokenizer:> TOKENIZER = struct
IntConst of intType * string * intSfx |
FloatConst of string * floatSfx
-
datatype token =
Invalid |
NewLine |
@@ -500,15 +499,28 @@ structure Tokenizer:> TOKENIZER = struct
s
end
- fun findKeyword s =
- case List.find
- (fn (_, repr) =>
- String.sub (repr, 0) = kwPrefix andalso
- String.extract (repr, 1, NONE) = s)
- tokenRepr
- of
- SOME (tk, _) => tk
- | NONE => Id s
+ fun keywordHashtableGen () =
+ let
+ open Hashtable
+ val table = create 128
+ val () =
+ List.app
+ (fn (tk, repr) =>
+ if String.sub (repr, 0) = kwPrefix then
+ insert table (String.extract (repr, 1, NONE)) tk
+ else
+ ())
+ tokenRepr
+ in
+ table
+ end
+
+ val keywordHashtable = lazy keywordHashtableGen
+
+ fun findKeyword str =
+ case Hashtable.lookup (keywordHashtable ()) str of
+ NONE => Id str
+ | SOME tk => tk
fun idParser () (stream, startOff) c =
let