vxx / vlib / v3 / token / token.v
392 lines · 379 sloc · 9.83 KB · 6c4d26f1f98c5464b012a375667ab345e462f7e0
Raw
1module token
2
3// Token lists token values used by token.
4pub enum Token {
5 amp // &
6 and // &&
7 and_assign // &=
8 arrow // <-
9 assign // =
10 attribute
11 bit_not // ~
12 char // `A` - rune
13 colon // :
14 comma // ,
15 comment
16 dec // --
17 decl_assign // :=
18 div // /
19 div_assign // /=
20 dollar // $
21 dot // .
22 dotdot // ..
23 ellipsis // ...
24 eof
25 eq // ==
26 ge // >=
27 gt // >
28 hash // #
29 inc // ++
30 key_as
31 key_asm
32 key_assert
33 key_atomic
34 key_break
35 key_const
36 key_continue
37 key_defer
38 key_dump
39 key_else
40 key_enum
41 key_false
42 key_fn
43 key_for
44 key_global
45 key_go
46 key_goto
47 key_if
48 key_import
49 key_in
50 key_interface
51 key_is
52 key_isreftype
53 key_likely
54 key_lock
55 key_match
56 key_module
57 key_mut
58 key_nil
59 key_none
60 key_offsetof
61 key_or
62 key_pub
63 key_return
64 key_rlock
65 key_select
66 key_shared
67 key_sizeof
68 key_spawn
69 key_static
70 key_struct
71 key_true
72 key_type
73 key_typeof
74 key_union
75 key_unlikely
76 key_unsafe
77 key_volatile
78 lcbr // {
79 le // <=
80 left_shift // <<
81 left_shift_assign // >>=
82 logical_or // ||
83 lpar // (
84 lsbr // [
85 lt // <
86 minus // -
87 minus_assign // -=
88 mod // %
89 mod_assign // %=
90 mul // *
91 mul_assign // *=
92 name // user
93 ne // !=
94 not // !
95 not_in // !in
96 not_is // !is
97 number // 123
98 or_assign // |=
99 pipe // |
100 plus // +
101 plus_assign // +=
102 question // ?
103 rcbr // }
104 right_shift // >>
105 right_shift_assign // <<=
106 right_shift_unsigned // >>>
107 right_shift_unsigned_assign // >>>=
108 rpar // )
109 rsbr // ]
110 semicolon // ;
111 str_dollar
112 string // 'foo'
113 unknown
114 xor // ^
115 xor_assign // ^=
116}
117
118// BindingPower lists binding power values used by token.
119pub enum BindingPower {
120 lowest
121 logical_or // ||
122 logical_and // &&
123 compare // ==, !=, <, <=, >, >=, in, !in, is, !is
124 bit_or // |
125 bit_xor // ^
126 shift // <<, >>, >>>
127 add // +, -
128 product // *, /, %, &
129 highest
130}
131
132const token_and_id = 1
133const token_arrow_id = 3
134const token_assign_id = 4
135const token_bit_not_id = 6
136const token_dec_id = 11
137const token_decl_assign_id = 12
138const token_div_id = 13
139const token_div_assign_id = 14
140const token_eq_id = 20
141const token_ge_id = 21
142const token_gt_id = 22
143const token_inc_id = 24
144const token_key_in_id = 44
145const token_key_is_id = 46
146const token_le_id = 74
147const token_left_shift_id = 75
148const token_left_shift_assign_id = 76
149const token_logical_or_id = 77
150const token_lt_id = 80
151const token_minus_id = 81
152const token_minus_assign_id = 82
153const token_mod_id = 83
154const token_mod_assign_id = 84
155const token_mul_id = 85
156const token_mul_assign_id = 86
157const token_ne_id = 88
158const token_not_id = 89
159const token_not_in_id = 90
160const token_not_is_id = 91
161const token_or_assign_id = 93
162const token_pipe_id = 94
163const token_plus_id = 95
164const token_plus_assign_id = 96
165const token_right_shift_id = 99
166const token_right_shift_assign_id = 100
167const token_right_shift_unsigned_id = 101
168const token_right_shift_unsigned_assign_id = 102
169const token_xor_id = 109
170const token_xor_assign_id = 110
171
172// left_binding_power supports left binding power handling for Token.
173@[inline]
174pub fn (t Token) left_binding_power() BindingPower {
175 tv := int(t)
176 if tv == token_logical_or_id {
177 return BindingPower.logical_or
178 }
179 if tv == token_and_id {
180 return BindingPower.logical_and
181 }
182 if tv == token_eq_id || tv == token_ne_id || tv == token_lt_id || tv == token_le_id
183 || tv == token_gt_id || tv == token_ge_id || tv == token_key_in_id || tv == token_not_in_id
184 || tv == token_key_is_id || tv == token_not_is_id {
185 return BindingPower.compare
186 }
187 if tv == token_pipe_id {
188 return BindingPower.bit_or
189 }
190 if tv == token_xor_id {
191 return BindingPower.bit_xor
192 }
193 if tv == token_left_shift_id || tv == token_right_shift_id
194 || tv == token_right_shift_unsigned_id {
195 return BindingPower.shift
196 }
197 if tv == token_plus_id || tv == token_minus_id {
198 return BindingPower.add
199 }
200 if tv == token_mul_id || tv == token_div_id || tv == token_mod_id || tv == 0 {
201 return BindingPower.product
202 }
203 return BindingPower.lowest
204}
205
206// right_binding_power supports right binding power handling for Token.
207@[inline]
208pub fn (t Token) right_binding_power() BindingPower {
209 return unsafe { BindingPower((int(t.left_binding_power()) + 1)) }
210}
211
212// is_keyword reports whether is keyword applies in token.
213@[inline]
214pub fn (t Token) is_keyword() bool {
215 return int(t) >= int(Token.key_as) && int(t) <= int(Token.key_volatile)
216}
217
218// is_prefix reports whether is prefix applies in token.
219@[inline]
220pub fn (t Token) is_prefix() bool {
221 tv := int(t)
222 return tv == token_minus_id || tv == 0 || tv == token_and_id || tv == token_mul_id
223 || tv == token_not_id || tv == token_bit_not_id || tv == token_arrow_id
224}
225
226// is_infix reports whether is infix applies in token.
227@[inline]
228pub fn (t Token) is_infix() bool {
229 tv := int(t)
230 return tv == 0 || tv == token_and_id || tv == token_arrow_id || tv == token_div_id
231 || tv == token_eq_id || tv == token_ge_id || tv == token_gt_id || tv == token_key_in_id
232 || tv == token_key_is_id || tv == token_le_id || tv == token_left_shift_id
233 || tv == token_logical_or_id || tv == token_lt_id || tv == token_minus_id
234 || tv == token_mod_id || tv == token_mul_id || tv == token_ne_id || tv == token_not_in_id
235 || tv == token_not_is_id || tv == token_pipe_id || tv == token_plus_id
236 || tv == token_right_shift_id || tv == token_right_shift_unsigned_id || tv == token_xor_id
237}
238
239// is_postfix reports whether is postfix applies in token.
240@[inline]
241pub fn (t Token) is_postfix() bool {
242 tv := int(t)
243 return tv == token_dec_id || tv == token_inc_id
244}
245
246// is_assignment reports whether is assignment applies in token.
247@[inline]
248pub fn (t Token) is_assignment() bool {
249 tv := int(t)
250 return tv == 2 || tv == token_assign_id || tv == token_decl_assign_id
251 || tv == token_div_assign_id || tv == token_left_shift_assign_id
252 || tv == token_minus_assign_id || tv == token_mod_assign_id || tv == token_mul_assign_id
253 || tv == token_or_assign_id || tv == token_plus_assign_id
254 || tv == token_right_shift_assign_id || tv == token_right_shift_unsigned_assign_id
255 || tv == token_xor_assign_id
256}
257
258// is_overloadable reports whether is overloadable applies in token.
259@[inline]
260pub fn (t Token) is_overloadable() bool {
261 tv := int(t)
262 return tv == token_div_id || tv == token_eq_id || tv == token_ge_id || tv == token_gt_id
263 || tv == token_le_id || tv == token_lt_id || tv == token_minus_id || tv == token_mod_id
264 || tv == token_mul_id || tv == token_ne_id || tv == token_pipe_id || tv == token_plus_id
265 || tv == token_xor_id
266}
267
268// is_comparison reports whether is comparison applies in token.
269@[inline]
270pub fn (t Token) is_comparison() bool {
271 tv := int(t)
272 return tv == token_eq_id || tv == token_ge_id || tv == token_gt_id || tv == token_key_in_id
273 || tv == token_key_is_id || tv == token_le_id || tv == token_lt_id || tv == token_ne_id
274 || tv == token_not_in_id || tv == token_not_is_id
275}
276
277// str returns the string form for Token.
278pub fn (t Token) str() string {
279 return match t {
280 .amp { '&' }
281 .and { '&&' }
282 .and_assign { '&=' }
283 .arrow { '<-' }
284 .assign { '=' }
285 .attribute { '@[' }
286 .bit_not { '~' }
287 .char { 'char' }
288 .colon { ':' }
289 .comma { ',' }
290 .comment { '// comment' }
291 .dec { '--' }
292 .decl_assign { ':=' }
293 .div { '/' }
294 .div_assign { '/=' }
295 .dollar { '$' }
296 .dot { '.' }
297 .dotdot { '..' }
298 .ellipsis { '...' }
299 .eof { 'eof' }
300 .eq { '==' }
301 .ge { '>=' }
302 .gt { '>' }
303 .hash { '#' }
304 .inc { '++' }
305 .key_as { 'as' }
306 .key_asm { 'asm' }
307 .key_assert { 'assert' }
308 .key_atomic { 'atomic' }
309 .key_break { 'break' }
310 .key_const { 'const' }
311 .key_continue { 'continue' }
312 .key_defer { 'defer' }
313 .key_dump { 'dump' }
314 .key_else { 'else' }
315 .key_enum { 'enum' }
316 .key_false { 'false' }
317 .key_fn { 'fn' }
318 .key_for { 'for' }
319 .key_global { '__global' }
320 .key_go { 'go' }
321 .key_goto { 'goto' }
322 .key_if { 'if' }
323 .key_import { 'import' }
324 .key_in { 'in' }
325 .key_interface { 'interface' }
326 .key_is { 'is' }
327 .key_isreftype { 'isreftype' }
328 .key_likely { '_likely_' }
329 .key_lock { 'lock' }
330 .key_match { 'match' }
331 .key_module { 'module' }
332 .key_mut { 'mut' }
333 .key_nil { 'nil' }
334 .key_none { 'none' }
335 .key_offsetof { '__offsetof' }
336 .key_or { 'or' }
337 .key_pub { 'pub' }
338 .key_return { 'return' }
339 .key_rlock { 'rlock' }
340 .key_select { 'select' }
341 .key_shared { 'shared' }
342 .key_sizeof { 'sizeof' }
343 .key_spawn { 'spawn' }
344 .key_static { 'static' }
345 .key_struct { 'struct' }
346 .key_true { 'true' }
347 .key_type { 'type' }
348 .key_typeof { 'typeof' }
349 .key_union { 'union' }
350 .key_unlikely { '_unlikely_' }
351 .key_unsafe { 'unsafe' }
352 .key_volatile { 'volatile' }
353 .lcbr { '{' }
354 .le { '<=' }
355 .left_shift { '<<' }
356 .left_shift_assign { '<<=' }
357 .logical_or { '||' }
358 .lpar { '(' }
359 .lsbr { '[' }
360 .lt { '<' }
361 .minus { '-' }
362 .minus_assign { '-=' }
363 .mod { '%' }
364 .mod_assign { '%=' }
365 .mul { '*' }
366 .mul_assign { '*=' }
367 .name { 'name' }
368 .ne { '!=' }
369 .not { '!' }
370 .not_in { '!in' }
371 .not_is { '!is' }
372 .number { 'number' }
373 .or_assign { '|=' }
374 .pipe { '|' }
375 .plus { '+' }
376 .plus_assign { '+=' }
377 .question { '?' }
378 .rcbr { '}' }
379 .right_shift { '>>' }
380 .right_shift_assign { '>>=' }
381 .right_shift_unsigned { '>>>' }
382 .right_shift_unsigned_assign { '>>>=' }
383 .rpar { ')' }
384 .rsbr { ']' }
385 .semicolon { ';' }
386 .str_dollar { '\${' }
387 .string { 'string' }
388 .unknown { 'unknown' }
389 .xor { '^' }
390 .xor_assign { '^=' }
391 }
392}
393