zuzu-rust 0.2.0

Rust implementation of ZuzuScript
Documentation
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
=encoding utf8

=head1 NAME

std/path/z/lexer - Pure Zuzu lexer for ZPath expressions.

=head1 IMPLEMENTATION SUPPORT

This module is supported by all implementations of ZuzuScript.

=head1 DESCRIPTION

This module provides the pure-Zuzu lexer used by ZPath.

=head1 EXPORTS

=head2 Classes

=over

=item C<< Lexer({ src: String, allowed_operators: Array }) >>

Constructs a ZPath lexer. Returns: C<Lexer>. Tokenizes C<src> using the
allowed operator table.

=over

=item C<< lexer.peek() >>

Parameters: none. Returns: C<Dict>. Returns the current token without
advancing.

=item C<< lexer.peek_n(n) >>

Parameters: C<n> is a zero-based lookahead offset. Returns: C<Dict>.
Returns a token ahead of the current position.

=item C<< lexer.peek_kind() >>

Parameters: none. Returns: C<String>. Returns the current token kind.

=item C<< lexer.peek_kind_n(n) >>

Parameters: C<n> is a zero-based lookahead offset. Returns: C<String>.
Returns a token kind ahead of the current position.

=item C<< lexer.next_tok() >>

Parameters: none. Returns: C<Dict>. Consumes and returns the current
token.

=item C<< lexer.expect(k) >>

Parameters: C<k> is the expected token kind. Returns: C<Dict>. Consumes
and returns the current token, throwing when it has a different kind.

=item C<< lexer.known_operators() >>

Parameters: none. Returns: C<Array>. Returns the allowed operator
definitions.

=back

=back

=head1 COPYRIGHT AND LICENCE

B<< std/path/z/lexer >> is copyright Toby Inkster.

It is free software; you may redistribute it and/or modify it under
the terms of either the Artistic License 1.0 or the GNU General Public
License version 2.

=cut

from std/string import substr;

class Lexer {
	let src := "";
	let scan_i := 0;
	let toks := [];
	let pos := 0;
	let allowed_operators;

	method __build__ () {
		die "Expected some operators" unless allowed_operators;
		toks := self._tokenize(src);
	}

	method peek () {
		return toks[pos];
	}
	
	method peek_n ( n ) {
		return toks[pos + n];
	}
	
	method peek_kind () {
		return toks[pos]{k};
	}

	method peek_kind_n ( n ) {
		return toks[pos + n]{k};
	}

	method next_tok () {
		let tok := toks[pos];
		pos++;
		return tok;
	}

	method expect ( k ) {
		let t := self.next_tok();
		die `Expected ${k}, got ${t{k}}` if t{k} ≢ k;
		return t;
	}

	method _is_ws ( c ) {
		return c ≢ null and c ~ /\s/;
	}

	method _prev_sig ( chars, idx ) {
		let j := idx - 1;
		while ( j >= 0 ) {
			if ( chars[j] ~ /\s/ ) {
				j--;
				next;
			}
			return chars[j];
		}
		return null;
	}

	method _next_sig ( chars, idx, n ) {
		let j := idx + 1;
		while ( j < n ) {
			if ( chars[j] ~ /\s/ ) {
				j++;
				next;
			}
			return chars[j];
		}
		return null;
	}

	method _is_path_ctx_prev ( c ) {
		return c ≡ "[" or c ≡ "(" or c ≡ "," or c ≡ ":" or c ≡ "?" or c ≡ "/";
	}

	method _is_path_ctx_next ( c ) {
		return c ≡ "]" or c ≡ ")" or c ≡ "," or c ≡ ":" or c ≡ "?" or c ≡ "/";
	}

	method _ws_on_both ( left, right ) {
		return self._is_ws(left) and self._is_ws(right);
	}

	method known_operators () {
		return allowed_operators;
	}

	method _sorted_operators () {
		return self.known_operators()
			.grep( fn o → not o.lexer_should_ignore )
			.sort( fn ( x, y ) → y.char_length <=> x.char_length );
	}

	method _operator_at ( chars, i, n, ops ) {
		let oi := 0;
		while ( oi < ops.length ) {
			let op := ops[oi];
			let spell := op{spelling};
			let m := length spell;
			if ( i + m <= n ) {
				let got := "";
				let j := 0;
				while ( j < m ) {
					got _= chars[i + j];
					j++;
				}
				if ( got ≡ spell ) {
					return op;
				}
			}
			oi++;
		}
		return null;
	}

	method _is_name_char ( c ) {
		return c ~ /[A-Za-z0-9_\-]/;
	}

	method _allows_operator_kind ( kind ) {
		return allowed_operators.first( fn op → op.get_kind() ≡ kind )
			? true
			: false;
	}

	method _tokenize ( String source ) {
		let t := [];
		let chars := [];
		let n := length source;
		let ops := self._sorted_operators();
		let z := 0;
		while ( z < n ) {
			chars.push( substr( source, z, 1 ) );
			z++;
		}

		let i := 0;
		while ( i < n ) {
			let ch := chars[i];

			if ( ch ~ /\s/ ) {
				i++;
				next;
			}

			let prev := i > 0 ? chars[i - 1] : null;
			let nxt := i + 1 < n ? chars[i + 1] : null;

			let prev_nonws := self._prev_sig( chars, i );
			let next_nonws := self._next_sig( chars, i, n );

			function push_token ( tok ) {
				tok{ws_before} := self._is_ws(prev) ? true : false;
				tok{ws_after}  := self._is_ws(nxt)  ? true : false;
				t.push( tok );
			}

			function push_sized_token ( tok, after ) {
				tok{ws_before} := self._is_ws(prev) ? true : false;
				tok{ws_after}  := self._is_ws(after) ? true : false;
				t.push( tok );
			}

			if ( ch ≡ "/" ) {
				if (
					self._ws_on_both( prev, nxt )
					and prev_nonws ≢ null and next_nonws ≢ null
					and not self._is_path_ctx_prev(prev_nonws)
					and not self._is_path_ctx_next(next_nonws)
				) {
					push_token( { k: "SLASH", v: "/" } );
					i++;
					next;
				}
				if (
					( self._is_ws(prev) xor self._is_ws(nxt) )
					and prev_nonws ≢ null and next_nonws ≢ null
					and not self._is_path_ctx_prev(prev_nonws)
					and not self._is_path_ctx_next(next_nonws)
				) {
					die `Binary operator '/' requires whitespace around it`;
				}
				t.push( { k: "SLASH_PATH", v: "/" } );
				i++;
				next;
			}

			let op := self._operator_at( chars, i, n, ops );
			if ( op ≢ null ) {
				let spell := op.get_spelling();
				let need_ws := op.requires_whitespace();
				let m := length spell;
				let op_prev := i > 0 ? chars[i - 1] : null;
				let op_next := i + m < n ? chars[i + m] : null;
				let path_ambiguous_star :=
					( spell ≡ "*" or spell ≡ "**" )
					and not self._ws_on_both( op_prev, op_next );

				let left_name := false;
				let right_name := false;
				if ( spell ~ /^[A-Za-z_]/ ) {
					left_name := op_prev ≢ null and self._is_name_char(op_prev);
					right_name := op_next ≢ null and self._is_name_char(op_next);
				}

				if (
					not path_ambiguous_star
					and not left_name
					and not right_name
				) {
					if ( need_ws ) {
						if ( self._ws_on_both( op_prev, op_next ) ) {
							push_sized_token( { k: op.get_kind(), v: spell }, op_next );
							i := i + m;
							next;
						}
						die `Binary operator '${spell}' requires whitespace around it`;
					}
					push_sized_token( { k: op.get_kind(), v: spell }, op_next );
					i := i + m;
					next;
				}
			}

			if ( ch ≡ "(" ) { t.push( { k: "LPAREN", v: "(" } ); i++; next; }
			if ( ch ≡ ")" ) { t.push( { k: "RPAREN", v: ")" } ); i++; next; }
			if ( ch ≡ "[" ) { t.push( { k: "LBRACK", v: "[" } ); i++; next; }
			if ( ch ≡ "]" ) { t.push( { k: "RBRACK", v: "]" } ); i++; next; }
			if ( ch ≡ "," ) { t.push( { k: "COMMA", v: "," } ); i++; next; }

			if ( ch ≡ "." ) {
				if ( i + 2 < n and chars[i + 1] ≡ "." and chars[i + 2] ≡ "*" ) {
					push_token( { k: "DOTDOTSTAR", v: "..*" } );
					i := i + 3;
					next;
				}
				if ( i + 1 < n and chars[i + 1] ≡ "." ) {
					push_token( { k: "DOTDOT", v: ".." } );
					i := i + 2;
					next;
				}
				push_token( { k: "DOT", v: "." } );
				i++;
				next;
			}

			if ( ch ≡ "*" and self._ws_on_both( prev, nxt ) ) {
				push_token( { k: "STAR", v: "*" } );
				i++;
				next;
			}

			if ( ch ≡ "*" ) {
				if ( i + 1 < n and chars[i + 1] ≡ "*" ) {
					push_token( { k: "STARSTAR", v: "**" } );
					i := i + 2;
					next;
				}
				push_token( { k: "STAR_PATH", v: "*" } );
				i++;
				next;
			}

			if ( self._allows_operator_kind( "ELVIS" ) and ch ≡ "?" and nxt ≡ ":" ) {
				let op_next := i + 2 < n ? chars[i + 2] : null;
				if ( self._ws_on_both( prev, op_next ) ) {
					t.push( {
						k: "ELVIS",
						v: "?:",
						ws_before: self._is_ws(prev) ? true : false,
						ws_after: self._is_ws(op_next) ? true : false,
					} );
					i := i + 2;
					next;
				}
				die "Elvis operator '?:' requires whitespace around it";
			}

			if ( ch ≡ "?" or ch ≡ ":" ) {
				if ( self._ws_on_both( prev, nxt ) ) {
					push_token( { k: ch ≡ "?" ? "QMARK" : "COLON", v: ch } );
					i++;
					next;
				}
				die `Ternary operator '${ch}' requires whitespace around it`;
			}

			if ( ch ≡ "\"" or ch ≡ "'" ) {
				let quote := ch;
				i++;
				let s := "";
				let esc := false;
				while ( i < n ) {
					let cc := chars[i];
					i++;
					if ( esc ) {
						if ( cc ≡ "\\" or cc ≡ quote or cc ≡ "\"" or cc ≡ "'" ) {
							s _= cc;
						}
						else {
							s _= self._unescape_char(cc);
						}
						esc := false;
						next;
					}
					if ( cc ≡ "\\" ) { esc := true; next; }
					last if cc ≡ quote;
					s _= cc;
				}
				push_token( { k: "STRING", v: s, q: quote } );
				next;
			}

			if ( ch ≡ "#" ) {
				let j := i + 1;
				let neg := false;
				if ( j < n and chars[j] ≡ "-" ) {
					neg := true;
					j++;
				}
				die "Invalid index '#'" if j >= n or not( chars[j] ~ /\d/ );
				let num := "";
				while ( j < n and chars[j] ~ /\d/ ) {
					num _= chars[j];
					j++;
				}
				let parsed := 0 + num;
				parsed := 0 - parsed if neg;
				push_token( { k: "INDEX", v: parsed } );
				i := j;
				next;
			}

			if ( ch ~ /[0-9]/ ) {
				let j := i;
				let num := "";
				while ( j < n and chars[j] ~ /[0-9.]/ ) {
					num _= chars[j];
					j++;
				}
				push_token( { k: "NUMBER", v: 0 + num } );
				i := j;
				next;
			}

			let name := self._read_name( chars, i );
			if ( name{v} ≢ "" ) {
				push_token( { k: "NAME", v: name{v} } );
				i := name{i};
				next;
			}

			die `Unexpected character '${ch}' at position ${i}`;
		}

		t.push( { k: "EOF", v: "" } );
		return t;
	}

	method _unescape_char ( c ) {
		return "\n" if c ≡ "n";
		return "\r" if c ≡ "r";
		return "\t" if c ≡ "t";
		return c;
	}

	method _read_name ( chars, start_i ) {
		let n := chars.length();
		let delim := {
			"\n": true,
			"\r": true,
			"\t": true,
			"(": true,
			")": true,
			"[": true,
			"]": true,
			"/": true,
			",": true,
			"=": true,
			"&": true,
			"|": true,
			"!": true,
			"<": true,
			">": true,
			"#": true,
			" ": true,
		};
		let buf := "";
		let esc := false;
		let i := start_i;

		while ( i < n ) {
			let c := chars[i];
			if ( esc ) {
				buf _= c;
				esc := false;
				i++;
				next;
			}

			if ( c ≡ "\\" ) {
				esc := true;
				i++;
				next;
			}

			last if delim.exists(c);
			last if c ~ /\s/;
			buf _= c;
			i++;
		}

		return { v: "", i: start_i } if buf ≡ "";
		return { v: buf, i: i };
	}
}