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