summaryrefslogtreecommitdiff
path: root/tnslc/src/parse/token.tnsl
blob: cbbb31f0cb2e91feed70bac9179e2ea7f0bd917b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/#
	Copyright 2021 Kyle Gunger

	This file is licensed under the CDDL 1.0 (the License)
	and may only be used in accordance with the License.
	You should have received a copy of the License with this
	software/source code. If you did not, a copy can be found
	at the following URL:

	https://opensource.org/licenses/CDDL-1.0

	THIS SOFTWARE/SOURCE CODE IS PROVIDED "AS IS" WITH NO
	WARRANTY, GUARANTEE, OR CLAIM OF FITNESS FOR ANY PURPOSE
	EXPRESS OR IMPLIED
#/

/# The various types of tokens #/
; enum TOKEN_TYPE [uint] {
	LINESEP = 0,
	INLNSEP = 1,
	DELIMIT = 2,
	AUGMENT = 3,
	LITERAL = 4,
	KEYTYPE = 5,
	PREWORD = 6,
	KEYWORD = 7,
	DEFWORD = 8
}

/# Token struct definition #/
;raw struct Token {
	uint
		token_type,
		line,
		col,

	~{}charp
		data
}

/; method Token

	/; operator delete
		;delete this.data
	;/

	/; add_char (`{}charp part)
		;uint l = len `this.data
		;realloc this.data, l + len part
		/;loop (uint i = 0; i < len part) [i++]
			`this.data{l + i} = part{i}
		;/
	;/
;/

/#
	Reserved words and characters, as well as
	helper funcs for checking their token types.
#/

;const {}{}charp PREWORDS = {
	"include",
	"define",
	"extern",
	"size",
	"align",
	"address",
	"rootfile",
	"if",
	"else",
	"abi"
}

;const {}{}charp KEYTYPES = {
	"bool",
	"char",
	"charp",

	"int8",
	"int16",
	"int32",
	"int64",
	"int",
	"uint8",
	"uint16",
	"uint32",
	"uint64",
	"uint",

	"float32",
	"float64",
	"float",

	"void",
	"type"
}

;const {}{}charp KEYWORDS = {
	"struct",
	"interface",
	"enum",
	"is",
	"extends",
	
	"loop",
	"continue",
	"break",
	
	"match",
	"case",
	"default",
	
	"label",
	"goto",
	
	"if",
	"else",
	
	"const",
	"static",
	"volatile",
	
	"method",
	"override",
	"self",
	"super",
	"operator",
	
	"raw",
	"asm",
	"inline",

	"delete",
	
	"module",
	"export",
}

;const {}{}charp LITERALS = {
	"true",
	"false"
}

;const {}charp DELIMITS = "()[]{}"
;const {}charp LINESEPS = ";:#"
;const {}charp INLNSEPS = ","
;const {}charp AUGMENTS = "~`.&|^><!+-*/%"

;const {}{}charp MDELIMITS = {
	# Code block
	"/;",
	";/",
	
	# Comment block
	"/#",
	"#/",
	
	# Preproc block
	"/:",
	":/",

	# Redef blocks
	";;",
	"::",
	";#",
	":#",
	"#;",
	"#:"
}

;const {}{}charp MAUGMENTS = {
	# Boolean
	"==",
	"&&",
	"||",

	# Bitwise shifts
	"<<",
	">>",

	# PREaugmented augmentors
	"&=",
	"|=",
	"^=",
	"+=",
	"-=",
	"*=",
	"/=",
	"%=",
	"~=",
	"`=",

	# POSTaugmented augmentors
	"!&",
	"!|",
	"!^",
	"!==",
	"!&&",
	"!||",
	"!>",
	"!<",
	">==",
	"<==",

	# Increment and De-increment
	"++",
	"--"
}

; const uint MAX_MRESERVED = 3

/##
	Checks if the character point p is in the string cmp

#; is_in_string (`const {}charp cmp, charp p) [bool]

	/; loop (int i = 0; i < len cmp) [i++]

		/; if (s == cmp{i})
			;return true
		;/
	;/

	;return false
;/


/##
	Checks if the string s is in the list cmp

#; is_in_string_list (`const {}{}charp cmp, `{}charp s) [bool]

	/; loop (int i = 0; i < len cmp) [i++]

		/; if (len s == len cmp{i})

			/; loop (int j = 0; j < len s) [j++]

				/; if (s{j} !== cmp{i}{j})
					;goto cont_outer
				;/
			;/

			;return true
		;/

		;label cont_outer
	;/

	;return false
;/

/#
	Get the token_type value for a given string of character points

#; get_token_type (`{}charp s) [int]

	/; if (len s > 1)

		/; if (is_in_string_list(~PREWORDS, ~s))
			;return TOKEN_TYPE.PREWORD
		;; else if (is_in_string_list(~KEYTYPES, ~s))
			;return TOKEN_TYPE.KEYTYPE
		;; else if (is_in_string_list(~KEYWORDS, ~s))
			;return TOKEN_TYPE.KEYWORD
		;; else if (is_in_string_list(~LITERALS, ~s))
			;return TOKEN_TYPE.LITERAL
		;; else if (is_in_string_list(~MDELIMITS, ~s))
			;return TOKEN_TYPE.DELIMIT
		;; else if (is_in_string_list(~MAUGMENTS, ~s))
			;return TOKEN_TYPE.AUGMENT
		;/

		;return TOKEN_TYPE.DEFWORD

	;;else if (len s == 1)

		/; if (is_in_string(~DELIMITS, s{0}))
			;return TOKEN_TYPE.DELIMIT
		;; else if (is_in_string(~LINESEPS, s{0}))
			;return TOKEN_TYPE.LINESEP
		;; else if (is_in_string(~INLNSEPS, s{0}))
			;return TOKEN_TYPE.INLNSEP
		;; else if (is_in_string(~AUGMENTS, s{0}))
			;return TOKEN_TYPE.AUGMENT
		;/

		;return TOKEN_TYPE.DEFWORD
	;/

	# What, we just produce vacant tokens now?
	# Something has gone wrong.

	;return -1
;/