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.BRACE_CLOSE] = "}}"
83
_names[TOKEN.BRACE_OPEN] = "{{"
84
_names[TOKEN.STATIC] = "static"
85
_names[TOKEN.STRING_LITERAL] = "string literal"
86
_names[TOKEN.STRUCT] = "struct"
87
_names[TOKEN.MINUS] = "-"
88
_names[TOKEN.SELF] = "self"
89
_names[TOKEN.SUPER] = "super"
90
_names[TOKEN.THEN] = "then"
91
_names[TOKEN.THROW] = "throw"
92
_names[TOKEN.TRAIT] = "trait"
93
_names[TOKEN.TRUE] = "true"
94
_names[TOKEN.TRY] = "try"
95
_names[TOKEN.TYPEOF] = "typeof"
96
_names[TOKEN.UNION] = "union"
97
_names[TOKEN.UNKNOWN] = "unknown"
98
_names[TOKEN.UNTIL] = "until"
99
_names[TOKEN.USE] = "use"
100
_names[TOKEN.VAL] = "val"
101
_names[TOKEN.VECTOR] = "vector"
102
_names[TOKEN.WHEN] = "when"
103
_names[TOKEN.WHILE] = "while"
104
_names[TOKEN.YIELD] = "yield"
105
_names[TOKEN.YRT] = "yrt"
106
_names[TOKEN.ENTER_STRING] = "{{"
107
_names[TOKEN.EXIT_STRING] = "}}"
108
_names[TOKEN.CONTINUE_STRING] = "string fragment"
109
_names[TOKEN.CANCEL_STRING] = "cancel string"
110
_names[TOKEN.FORMAT_STRING] = ":"
111
si
112
113
instance: TOKEN_NAMES private static is
114
let result = _instance ?? TOKEN_NAMES()
115
116
_instance = result
117
118
return result
119
si
120
121
[token: Lexical.TOKEN]: string static =>
122
let n = instance._names in
123
124
if n.contains_key(token) then
125
instance._names[token]
126
else
127
"(unknown token {cast int(token)})"
128
fi
129
130
[tokens: Collections.Iterable[Lexical.TOKEN]]: string static is
131
let sorted_tokens = Collections.LIST[string]()
132
133
for t in tokens do
134
sorted_tokens.add(TOKEN_NAMES[t])
135
od
136
137
sorted_tokens.sort()
138
139
let buffer = System.Text.StringBuilder()
140
let seen_any mut = false
141
let previous: string? mut = null
142
143
// Tokens that read the same to the user, such as a brace and the
144
// start of an interpolated string, are listed once.
145
for s in sorted_tokens do
146
if previous? /\ s =~ previous then
147
continue
148
fi
149
150
if seen_any then
151
buffer.append(", ")
152
fi
153
buffer.append(s)
154
seen_any = true
155
previous = s
156
od
157
158
let result mut = buffer.to_string()
159
160
let i = result.last_index_of(',')
161
162
if i >= 0 then
163
let left = result.substring(0, i)
164
let right = result.substring(i + 2)
165
result = "{left} or {right}"
166
fi
167
168
return result
169
si
170
si
171
si