Skip to content
← Back

src/lexical/token_names.ghul

1
namespace Lexical is
2
use IO.Std
3
4
5
class TOKEN_NAMES is
6
_names: Collections.MAP[TOKEN,string]
7
8
_instance: TOKEN_NAMES? static
9
10
init() is
11
_names = Collections.MAP[TOKEN,string]()
12
13
_names[TOKEN.ABSTRACT] = "abstract"
14
_names[TOKEN.ARRAY_DEF] = "[]"
15
_names[TOKEN.ARROW_FAT] = "=>"
16
_names[TOKEN.ARROW_THIN] = "->"
17
_names[TOKEN.ASSERT] = "assert"
18
_names[TOKEN.ASSIGN] = "="
19
_names[TOKEN.AT] = "@"
20
_names[TOKEN.AWAIT] = "await"
21
_names[TOKEN.BAR_ARROW] = "|>"
22
_names[TOKEN.TILDE_ARROW] = "~>"
23
_names[TOKEN.BREAK] = "break"
24
_names[TOKEN.CASE] = "case"
25
_names[TOKEN.CAST] = "cast"
26
_names[TOKEN.CATCH] = "catch"
27
_names[TOKEN.CHAR_LITERAL] = "char literal"
28
_names[TOKEN.CLASS] = "class"
29
_names[TOKEN.COLON] = ":"
30
_names[TOKEN.COMMA] = ","
31
_names[TOKEN.CONTINUE] = "continue"
32
_names[TOKEN.DEFAULT] = "default"
33
_names[TOKEN.DO] = "do"
34
_names[TOKEN.DOT] = "."
35
_names[TOKEN.DOUBLE_LITERAL] = "double literal"
36
_names[TOKEN.ELIF] = "elif"
37
_names[TOKEN.ELSE] = "else"
38
_names[TOKEN.END_OF_INPUT] = "end of input"
39
_names[TOKEN.ENUM] = "enum"
40
_names[TOKEN.ESAC] = "esac"
41
_names[TOKEN.FALSE] = "false"
42
_names[TOKEN.FI] = "fi"
43
_names[TOKEN.FIELD] = "field"
44
_names[TOKEN.FINALLY] = "finally"
45
_names[TOKEN.FLOAT_LITERAL] = "float literal"
46
_names[TOKEN.FOR] = "for"
47
_names[TOKEN.IDENTIFIER] = "identifier"
48
_names[TOKEN.IF] = "if"
49
_names[TOKEN.IMPL] = "impl"
50
_names[TOKEN.IN] = "in"
51
_names[TOKEN.INNATE] = "innate"
52
_names[TOKEN.INT_LITERAL] = "int literal"
53
_names[TOKEN.IS] = "is"
54
_names[TOKEN.ISA] = "isa"
55
_names[TOKEN.LAV] = "lav"
56
_names[TOKEN.LE] = "<="
57
_names[TOKEN.LET] = "let"
58
_names[TOKEN.MUT] = "mut"
59
_names[TOKEN.NAMESPACE] = "namespace"
60
_names[TOKEN.NEW] = "new"
61
_names[TOKEN.NULL] = "null"
62
_names[TOKEN.OD] = "od"
63
_names[TOKEN.OPERATOR] = "operator"
64
_names[TOKEN.PAREN_CLOSE] = ")"
65
_names[TOKEN.PAREN_OPEN] = "("
66
_names[TOKEN.PARTIAL] = "partial"
67
_names[TOKEN.PRIVATE] = "private"
68
_names[TOKEN.PROTECTED] = "protected"
69
_names[TOKEN.PTR] = "ptr"
70
_names[TOKEN.PUBLIC] = "public"
71
_names[TOKEN.QUESTION] = "?"
72
_names[TOKEN.RANGE] = ".."
73
_names[TOKEN.REC] = "rec"
74
_names[TOKEN.REF] = "ref"
75
_names[TOKEN.RETURN] = "return"
76
_names[TOKEN.SEMICOLON] = ";"
77
_names[TOKEN.SHIFT_LEFT] = "<<"
78
_names[TOKEN.SHIFT_RIGHT] = ">>"
79
_names[TOKEN.SI] = "si"
80
_names[TOKEN.SQUARE_CLOSE] = "]"
81
_names[TOKEN.SQUARE_OPEN] = "["
82
_names[TOKEN.STATIC] = "static"
83
_names[TOKEN.STRING_LITERAL] = "string literal"
84
_names[TOKEN.STRUCT] = "struct"
85
_names[TOKEN.MINUS] = "-"
86
_names[TOKEN.SELF] = "self"
87
_names[TOKEN.SUPER] = "super"
88
_names[TOKEN.THEN] = "then"
89
_names[TOKEN.THROW] = "throw"
90
_names[TOKEN.TRAIT] = "trait"
91
_names[TOKEN.TRUE] = "true"
92
_names[TOKEN.TRY] = "try"
93
_names[TOKEN.TYPEOF] = "typeof"
94
_names[TOKEN.UNION] = "union"
95
_names[TOKEN.UNKNOWN] = "unknown"
96
_names[TOKEN.UNTIL] = "until"
97
_names[TOKEN.USE] = "use"
98
_names[TOKEN.VAL] = "val"
99
_names[TOKEN.VECTOR] = "vector"
100
_names[TOKEN.WHEN] = "when"
101
_names[TOKEN.WHILE] = "while"
102
_names[TOKEN.YIELD] = "yield"
103
_names[TOKEN.YRT] = "yrt"
104
_names[TOKEN.ENTER_STRING] = "{{"
105
_names[TOKEN.EXIT_STRING] = "}}"
106
_names[TOKEN.CONTINUE_STRING] = "string fragment"
107
_names[TOKEN.CANCEL_STRING] = "cancel string"
108
_names[TOKEN.FORMAT_STRING] = ":"
109
si
110
111
instance: TOKEN_NAMES private static is
112
let result = _instance ?? TOKEN_NAMES()
113
114
_instance = result
115
116
return result
117
si
118
119
[token: Lexical.TOKEN]: string static =>
120
let n = instance._names in
121
122
if n.contains_key(token) then
123
instance._names[token]
124
else
125
"(unknown token {cast int(token)})"
126
fi
127
128
[tokens: Collections.Iterable[Lexical.TOKEN]]: string static is
129
let sorted_tokens = Collections.LIST[string]()
130
131
for t in tokens do
132
sorted_tokens.add(TOKEN_NAMES[t])
133
od
134
135
sorted_tokens.sort()
136
137
let buffer = System.Text.StringBuilder()
138
let seen_any mut = false
139
140
for s in sorted_tokens do
141
if seen_any then
142
buffer.append(", ")
143
fi
144
buffer.append(s)
145
seen_any = true
146
od
147
148
let result mut = buffer.to_string()
149
150
let i = result.last_index_of(',')
151
152
if i >= 0 then
153
let left = result.substring(0, i)
154
let right = result.substring(i + 2)
155
result = "{left} or {right}"
156
fi
157
158
return result
159
si
160
si
161
si