1
2
3
4
5
6 using System;
7 using System.Collections;
8
9 namespace System.Lex
10 {
11 public class Keyword
12 {
13 public Keyword() : str(null), tokenID(INVALID_TOKEN)
14 {
15 }
16 public Keyword(const uchar* str_, long tokenID_) : str(str_), tokenID(tokenID_)
17 {
18 }
19 public const uchar* str;
20 public long tokenID;
21 }
22
23 public class KeywordMap
24 {
25 public KeywordMap()
26 {
27 }
28 public void SetKeywords(const List<Keyword>& keywords)
29 {
30 for (const auto& kw : keywords)
31 {
32 Lexeme lexeme(kw.str, StrEnd(kw.str));
33 keywordMap[lexeme] = kw.tokenID;
34 }
35 }
36 public long GetKeywordToken(const Lexeme& lexeme) const
37 {
38 auto it = keywordMap.CFind(lexeme);
39 if (it != keywordMap.CEnd())
40 {
41 return it->second;
42 }
43 else
44 {
45 return INVALID_TOKEN;
46 }
47 }
48 private Map<Lexeme, long> keywordMap;
49 }
50
51 public inline const uchar* StrEnd(const uchar* s)
52 {
53 while (*s != u'\0')
54 {
55 ++s;
56 }
57 return s;
58 }
59