forked from eutro/zenscript-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zenscript-parser.el
2218 lines (1775 loc) · 70.3 KB
/
zenscript-parser.el
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; zenscript-parser.el --- ZenScript parser -*- lexical-binding: t -*-
;; Copyright (c) 2020 Eutro
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;; The above copyright notice and this permission notice shall be included in all
;; copies or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
;;; Commentary:
;; This module of zenscript-mode provides a ZenScript parser,
;; reading in characters from a buffer, tokenizing them
;; and generating a syntax tree.
;;; Code:
(defun zenscript--skip-ws-and-comments ()
"Skip across any whitespace characters or comments."
(skip-syntax-forward " >")
(when (>= (point-max) (+ (point) 2))
(let ((ppss (save-excursion
(parse-partial-sexp (point)
(+ (point) 2)
() () () t))))
(when (nth 4 ppss)
(parse-partial-sexp (point)
(point-max)
() () ppss 'syntax-table)
(zenscript--skip-ws-and-comments)))))
(defun zenscript--map* (kwargs &rest kvs)
"Make a hashmap.
KWARGS are additional arguments to `make-hash-table'.
Each odd element of KVS is a key corresponding to the value
immediately after it."
(let ((table (apply #'make-hash-table
:size (/ (length kvs) 2)
kwargs)))
(while kvs
(puthash (car kvs) (cadr kvs) table)
(setq kvs (cddr kvs)))
table))
(define-hash-table-test 'zenscript--string-test-single
(lambda (_a _b) t)
(lambda (k)
(pcase k
(?\\ 0)
(?\' 1)
(_ 2))))
(define-hash-table-test 'zenscript--string-test-double
(lambda (_a _b) t)
(lambda (k)
(pcase k
(?\\ 0)
(?\" 1)
(_ 2))))
(defconst zenscript--token-dfa-states
(vector
(zenscript--map*
()
?a 1 ?b 1 ?c 1 ?d 1 ?e 1 ?f 1 ?g 1 ?h 1 ?i 1 ?j 1 ?k 1 ?l 1 ?m 1
?n 1 ?o 1 ?p 1 ?q 1 ?r 1 ?s 1 ?t 1 ?u 1 ?v 1 ?w 1 ?x 1 ?y 1 ?z 1
?A 1 ?B 1 ?C 1 ?D 1 ?E 1 ?F 1 ?G 1 ?H 1 ?I 1 ?J 1 ?K 1 ?L 1 ?M 1
?N 1 ?O 1 ?P 1 ?Q 1 ?R 1 ?S 1 ?T 1 ?U 1 ?V 1 ?W 1 ?X 1 ?Y 1 ?Z 1
?_ 1
?\{ 2 ?\} 3 ?\[ 4 ?\] 5
?. 6 ?, 8
?+ 9 ?- 11 ?* 13 ?/ 15 ?% 17
?| 19 ?& 22
?^ 25
?? 27 ?: 28 ?\( 29 ?\) 30
?~ 31
?\; 33
?< 34 ?> 36 ?= 38 ?! 40
?$ 42
?\' 43 ?\" 49
?0 56
?1 58 ?2 58 ?3 58 ?4 58 ?5 58 ?6 58 ?7 58 ?8 58 ?9 58)
(zenscript--map* ; identifiers
()
?a 1 ?b 1 ?c 1 ?d 1 ?e 1 ?f 1 ?g 1 ?h 1 ?i 1 ?j 1 ?k 1 ?l 1 ?m 1
?n 1 ?o 1 ?p 1 ?q 1 ?r 1 ?s 1 ?t 1 ?u 1 ?v 1 ?w 1 ?x 1 ?y 1 ?z 1
?A 1 ?B 1 ?C 1 ?D 1 ?E 1 ?F 1 ?G 1 ?H 1 ?I 1 ?J 1 ?K 1 ?L 1 ?M 1
?N 1 ?O 1 ?P 1 ?Q 1 ?R 1 ?S 1 ?T 1 ?U 1 ?V 1 ?W 1 ?X 1 ?Y 1 ?Z 1
?_ 1 ?0 1 ?1 1 ?2 1 ?3 1 ?4 1 ?5 1 ?6 1 ?7 1 ?8 1 ?9 1)
(zenscript--map* ()) ; {
(zenscript--map* ()) ; }
(zenscript--map* ()) ; [
(zenscript--map* ()) ; ]
(zenscript--map* () ?. 7) ; .
(zenscript--map* ()) ; ..
(zenscript--map* ()) ; ,
(zenscript--map* () ?= 10) ; +
(zenscript--map* ()) ; +=
(zenscript--map*
()
?= 12
?0 56
?1 58 ?2 58 ?3 58 ?4 58 ?5 58 ?6 58 ?7 58 ?8 58 ?9 58) ; -
(zenscript--map* ()) ; -=
(zenscript--map* () ?= 14) ; *
(zenscript--map* ()) ; *=
(zenscript--map* () ?= 16) ; /
(zenscript--map* ()); /=
(zenscript--map* () ?= 18) ; %
(zenscript--map* ()) ; %=
(zenscript--map* () ?= 20 ?| 21) ; |
(zenscript--map* ()) ; |=
(zenscript--map* ()) ; ||
(zenscript--map* () ?= 23 ?& 24) ; &
(zenscript--map* ()) ; &=
(zenscript--map* ()) ; &&
(zenscript--map* () ?= 26) ; ^
(zenscript--map* ()) ; ^=
(zenscript--map* ()) ; ?
(zenscript--map* ()) ; :
(zenscript--map* ()) ; (
(zenscript--map* ()) ; )
(zenscript--map* () ?= 32) ; ~
(zenscript--map* ()) ; ~=
(zenscript--map* ()) ; ;
(zenscript--map* () ?= 35) ; <
(zenscript--map* ()) ; <=
(zenscript--map* () ?= 37) ; >
(zenscript--map* ()) ; >=
(zenscript--map* () ?= 39) ; =
(zenscript--map* ()) ; ==
(zenscript--map* () ?= 41) ; !
(zenscript--map* ()) ; !=
(zenscript--map* ()) ; $
;; ' strings
(zenscript--map*
'(:test zenscript--string-test-single)
?\\ 44
?' 55
?. 43)
(zenscript--map*
()
?u 45
?\' 43 ?\" 43 ?\\ 43 ?/ 43 ?b 43 ?f 43 ?n 43 ?r 43 ?t 43)
(zenscript--map*
()
?0 46 ?1 46 ?2 46 ?3 46 ?4 46 ?5 46 ?6 46 ?7 46 ?8 46 ?9 46
?a 46 ?b 46 ?c 46 ?d 46 ?e 46 ?f 46
?A 46 ?B 46 ?C 46 ?D 46 ?E 46 ?F 46)
(zenscript--map*
()
?0 47 ?1 47 ?2 47 ?3 47 ?4 47 ?5 47 ?6 47 ?7 47 ?8 47 ?9 47
?a 47 ?b 47 ?c 47 ?d 47 ?e 47 ?f 47
?A 47 ?B 47 ?C 47 ?D 47 ?E 47 ?F 47)
(zenscript--map*
()
?0 48 ?1 48 ?2 48 ?3 48 ?4 48 ?5 48 ?6 48 ?7 48 ?8 48 ?9 48
?a 48 ?b 48 ?c 48 ?d 48 ?e 48 ?f 48
?A 48 ?B 48 ?C 48 ?D 48 ?E 48 ?F 48)
(zenscript--map*
()
?0 43 ?1 43 ?2 43 ?3 43 ?4 43 ?5 43 ?6 43 ?7 43 ?8 43 ?9 43
?a 43 ?b 43 ?c 43 ?d 43 ?e 43 ?f 43
?A 43 ?B 43 ?C 43 ?D 43 ?E 43 ?F 43)
;; " strings
(zenscript--map*
'(:test zenscript--string-test-double)
?\\ 50
?\" 55
?. 49)
(zenscript--map*
()
?u 51
?\' 49 ?\" 49 ?\\ 49 ?/ 49 ?b 49 ?f 49 ?n 49 ?r 49 ?t 49)
(zenscript--map*
()
?0 52 ?1 52 ?2 52 ?3 52 ?4 52 ?5 52 ?6 52 ?7 52 ?8 52 ?9 52
?a 52 ?b 52 ?c 52 ?d 52 ?e 52 ?f 52
?A 52 ?B 52 ?C 52 ?D 52 ?E 52 ?F 52)
(zenscript--map*
()
?0 53 ?1 53 ?2 53 ?3 53 ?4 53 ?5 53 ?6 53 ?7 53 ?8 53 ?9 53
?a 53 ?b 53 ?c 53 ?d 53 ?e 53 ?f 53
?A 53 ?B 53 ?C 53 ?D 53 ?E 53 ?F 53)
(zenscript--map*
()
?0 54 ?1 54 ?2 54 ?3 54 ?4 54 ?5 54 ?6 54 ?7 54 ?8 54 ?9 54
?a 54 ?b 54 ?c 54 ?d 54 ?e 54 ?f 54
?A 54 ?B 54 ?C 54 ?D 54 ?E 54 ?F 54)
(zenscript--map*
()
?0 49 ?1 49 ?2 49 ?3 49 ?4 49 ?5 49 ?6 49 ?7 49 ?8 49 ?9 49
?a 49 ?b 49 ?c 49 ?d 49 ?e 49 ?f 49
?A 49 ?B 49 ?C 49 ?D 49 ?E 49 ?F 49)
(zenscript--map* ()) ;; finished strings
;; 0 start number
(zenscript--map*
()
?x 57
?. 59) ; 0
;; hex numbers
;; -0x... shouldn't actually go here,
;; but it doesn't make a difference:
;; -0x... is valid ZenScript anyway,
;; it just gets immediately unary operator-ed
(zenscript--map*
()
?0 57 ?1 57 ?2 57 ?3 57 ?4 57 ?5 57 ?6 57 ?7 57 ?8 57 ?9 57
?a 57 ?b 57 ?c 57 ?d 57 ?e 57 ?f 57
?A 57 ?B 57 ?C 57 ?D 57 ?E 57 ?F 57)
;; integers
(zenscript--map*
()
?0 58 ?1 58 ?2 58 ?3 58 ?4 58 ?5 58 ?6 58 ?7 58 ?8 58 ?9 58
?. 59)
;; floats
(zenscript--map*
()
?0 59 ?1 59 ?2 59 ?3 59 ?4 59 ?5 59 ?6 59 ?7 59 ?8 59 ?9 59
?e 60 ?E 60
?f 63 ?F 63 ?d 63 ?D 63) ;; required first digit
(zenscript--map*
()
?0 59 ?1 59 ?2 59 ?3 59 ?4 59 ?5 59 ?6 59 ?7 59 ?8 59 ?9 59
?e 60 ?E 60
?f 63 ?F 63 ?d 63 ?D 63) ;; repeated digits (terminal)
(zenscript--map*
()
?0 62 ?1 62 ?2 62 ?3 62 ?4 62 ?5 62 ?6 62 ?7 62 ?8 62 ?9 62
?+ 61 ?- 61) ;; [eE][\+\-]?[0-9]+
(zenscript--map*
() ;; [0-9]+ required first digit
?0 62 ?1 62 ?2 62 ?3 62 ?4 62 ?5 62 ?6 62 ?7 62 ?8 62 ?9 62
?f 63 ?F 63 ?d 63 ?D 63)
(zenscript--map*
()
?0 62 ?1 62 ?2 62 ?3 62 ?4 62 ?5 62 ?6 62 ?7 62 ?8 62 ?9 62
?f 63 ?F 63 ?d 63 ?D 63) ;; [0-9]+ remaining digits (terminal)
(zenscript--map* ()) ;; post-[fFdD] (terminal)
)
"A vector of states for the DFA.
Each state is a hash-table of characters
to the next state index.")
(defconst zenscript--token-dfa-finals
(zenscript--map*
()
1 'T_ID
2 'T_AOPEN
3 'T_ACLOSE
4 'T_SQBROPEN
5 'T_SQBRCLOSE
6 'T_DOT
7 'T_DOT2
8 'T_COMMA
9 'T_PLUS
10 'T_PLUSASSIGN
11 'T_MINUS
12 'T_MINUSASSIGN
13 'T_MUL
14 'T_MULASSIGN
15 'T_DIV
16 'T_DIVASSIGN
17 'T_MOD
18 'T_MODASSIGN
19 'T_OR
20 'T_ORASSIGN
21 'T_OR2
22 'T_AND
23 'T_ANDASSIGN
24 'T_AND2
25 'T_XOR
26 'T_XORASSIGN
27 'T_QUEST
28 'T_COLON
29 'T_BROPEN
30 'T_BRCLOSE
31 'T_TILDE
32 'T_TILDEASSIGN
33 'T_SEMICOLON
34 'T_LT
35 'T_LTEQ
36 'T_GT
37 'T_GTEQ
38 'T_ASSIGN
39 'T_EQ
40 'T_NOT
41 'T_NOTEQ
42 'T_DOLLAR
55 'T_STRINGVALUE
56 'T_INTVALUE
57 'T_INTVALUE
58 'T_INTVALUE
59 'T_FLOATVALUE
62 'T_FLOATVALUE
63 'T_FLOATVALUE)
"A hashmap of final states indeces.
Each final state is mapped to the token it represents.")
(defun zenscript--tokenize-buffer (&optional from to no-error)
"Read the buffer into a list of tokens.
FROM is the start position, and defaults to `point-min'.
TO is the end position, and defaults to `point-max'.
If a token is unrecognised, and NO-ERROR is nil,
'zenscript-unrecognised-token is thrown with point.
If NO-ERROR is non-nil, then parsing stops instead, returning the partially
accumulated list of tokens, and leaving point where it is.
If parsing concludes, then point is left at TO.
Note: this uses the syntax table to handle comments."
(goto-char (or from (point-min)))
(let ((to (or to (point-max)))
(continue t)
tokens)
(zenscript--skip-ws-and-comments)
(while (and continue (char-after))
(let ((start (point))
(next-token (zenscript--next-token)))
(when (or (>= (point) to)
(not next-token))
(setq continue ()))
(if next-token
(if (> (point) to)
(goto-char start)
(setq tokens (cons next-token tokens))
(when (< (point) to)
(zenscript--skip-ws-and-comments)))
(unless no-error
(throw 'zenscript-unrecognized-token
(point))))))
(reverse tokens)))
(defconst zenscript--keyword-map
(zenscript--map*
'(:test equal)
"frigginConstructor" 'T_ZEN_CONSTRUCTOR
"zenConstructor" 'T_ZEN_CONSTRUCTOR
"frigginClass" 'T_ZEN_CLASS
"zenClass" 'T_ZEN_CLASS
"instanceof" 'T_INSTANCEOF
"static" 'T_STATIC
"global" 'T_GLOBAL
"import" 'T_IMPORT
"false" 'T_FALSE
"true" 'T_TRUE
"null" 'T_NULL
"break" 'T_BREAK
"while" 'T_WHILE
"val" 'T_VAL
"var" 'T_VAR
"return" 'T_RETURN
"for" 'T_FOR
"else" 'T_ELSE
"if" 'T_IF
"version" 'T_VERSION
"as" 'T_AS
"void" 'T_VOID
"has" 'T_IN
"in" 'T_IN
"function" 'T_FUNCTION
"string" 'T_STRING
"double" 'T_DOUBLE
"float" 'T_FLOAT
"long" 'T_LONG
"int" 'T_INT
"short" 'T_SHORT
"byte" 'T_BYTE
"bool" 'T_BOOL
"any" 'T_ANY)
"A hash-table of keywords to tokens.")
(defun zenscript--next-token (&optional skip-whitespace)
"Parse the next ZenScript token after point.
If SKIP-WHITESPACE is non-nil, whitespace and comments
are skipped according to `syntax-table'.
Return a list of the form
(type val pos)
or nil if no token was recognised.
type:
The type of the token, as seen here
https://docs.blamejared.com/1.12/en/Dev_Area/ZenTokens/
val:
The string value of the token.
pos:
The position at which the token occured.
file:
The file name of the buffer from which the token was read.
point is put after token, if one was found."
(let ((begin (point)))
(when skip-whitespace (zenscript--skip-ws-and-comments))
(let ((token-start (point))
(state 0))
(while (and (char-after)
(let ((newstate
(gethash (char-after)
(aref zenscript--token-dfa-states
state))))
(when newstate (setq state newstate))
newstate))
(forward-char))
(let ((type (gethash state zenscript--token-dfa-finals))
(val (buffer-substring-no-properties token-start (point))))
(when (eq type 'T_ID)
(setq type
(or (gethash val zenscript--keyword-map)
'T_ID)))
(if type
(list type val token-start)
(goto-char begin)
())))))
(defmacro zenscript--cdr! (list)
"Set LIST to the cdr of LIST."
`(setq ,list (cdr ,list)))
(defmacro zenscript--cons! (car list)
"Do (cons CAR LIST) and set LIST to it."
`(setq ,list (cons ,car ,list)))
(defvar zenscript-parse-error-hook ()
"A list of hooks run when an error occurs while parsing.
Each hook is called with the error message and the token where it occured.")
(defun zenscript--throw-parse-error (message token)
"Run `zenscript-parse-error-hook' and throw 'zenscript-parse-error with (MESSAGE TOKEN)."
(run-hook-with-args 'zenscript-parse-error-hook message token)
(throw 'zenscript-parse-error
(list message token)))
(defun zenscript--make-tokenstream (token-list)
"Make a tokenstream from a list of tokens, TOKEN-LIST, as returned by `zenscript--tokenize-buffer'."
(lambda (op &rest args)
(pcase op
('PEEK (car token-list))
('NEXT (prog1 (car token-list)
(zenscript--cdr! token-list)))
('OPTIONAL (when (eq (car args) (caar token-list))
(prog1 (car token-list)
(zenscript--cdr! token-list))))
('REQUIRE (if (eq (car args) (caar token-list))
(prog1 (car token-list)
(zenscript--cdr! token-list))
(zenscript--throw-parse-error
(cadr args) (car token-list)))))))
(defun zenscript--require-token (type tokens message)
"Require a token of type TYPE from TOKENS.
Return the first token if it is of the correct type, otherwise
zenscript--throw-parse-error with MESSAGE.
TOKENS must be a tokenstream from `zenscript--make-tokenstream'."
(funcall tokens 'REQUIRE type message))
(defun zenscript--peek-token (tokens)
"Look at the next token in the stream TOKENS, without consuming it.
TOKENS must be a tokenstream from `zenscript--make-tokenstream'."
(funcall tokens 'PEEK))
(defun zenscript--get-token (tokens)
"Get the next token in the stream TOKENS, consuming it.
TOKENS must be a tokenstream from `zenscript--make-tokenstream'."
(funcall tokens 'NEXT))
(defun zenscript--optional-token (type tokens)
"Get the next token if it is of type TYPE.
TOKENS must be a tokenstream from `zenscript--make-tokenstream'."
(funcall tokens 'OPTIONAL type))
(defun zenscript--has-next-token (tokens)
"Return t if TOKENS has any more tokens remaining.
TOKENS must be a tokenstream from `zenscript--make-tokenstream'."
(when (zenscript--peek-token tokens) t))
(defun zenscript--parse-tokens (tokenlist)
"Parse a list of ZenScript tokens.
TOKENLIST is a list of tokens as returned by `zenscript--tokenize-buffer'.
Returns a list of the form
(imports functions zenclasses statements globals)
Which are lists of elements of the formats:
imports:
(fqname pos rename)
fqname:
A list of strings representing the fully qualified name.
pos:
The position at which the import appears.
rename:
The name by which fqname should be referenced, or nil
if the last name in fqname should be used.
functions:
See `zenscript--parse-function'.
zenclasses:
See `zenscript--parse-zenclass'.
statements:
See `zenscript--parse-statement'.
globals:
See `zenscript--parse-global'."
(let ((tokens (zenscript--make-tokenstream tokenlist))
imports functions zenclasses statements globals)
(catch 'zenscript-parse-error
(while (and (zenscript--has-next-token tokens)
(eq (car (zenscript--peek-token tokens))
'T_IMPORT))
(let (fqname
(pos (nth 2 (zenscript--get-token tokens)))
rename)
(zenscript--cons! (cadr (zenscript--require-token 'T_ID tokens
"identifier expected"))
fqname)
(while (zenscript--optional-token 'T_DOT tokens)
(zenscript--cons! (cadr (zenscript--require-token 'T_ID tokens
"identifier expected"))
fqname))
(when (zenscript--optional-token 'T_AS tokens)
(setq rename (cadr (zenscript--require-token 'T_ID tokens
"identifier expected"))))
(zenscript--require-token 'T_SEMICOLON tokens
"; expected")
(zenscript--cons! (list (reverse fqname)
pos
rename)
imports))))
(while (zenscript--has-next-token tokens)
(catch 'zenscript-parse-error
(pcase (car (zenscript--peek-token tokens))
((or 'T_GLOBAL 'T_STATIC)
(zenscript--cons! (zenscript--parse-global tokens)
globals))
('T_FUNCTION
(zenscript--cons! (zenscript--parse-function tokens)
functions))
('T_ZEN_CLASS
(zenscript--cons! (zenscript--parse-zenclass tokens)
zenclasses))
(_ (zenscript--cons! (zenscript--parse-statement tokens)
statements)))))
(list (reverse imports)
(reverse functions)
(reverse zenclasses)
(reverse statements)
(reverse globals))))
(defun zenscript--parse-function (tokens)
"Parse the next static function definition from TOKENS.
Return a list of the form:
(name arguments type statements start end)
name:
The token that is the name of the function.
arguments:
A list of arguments as returned by `zenscript--parse-function-arguments'.
type:
The ZenType that the function returns.
statements:
A list of statements, which are as returned by `zenscript--parse-statement'.
start:
The position after the opening {
end:
The position before the closing }
function name(argname [as type], argname [as type], ...) [as type] {
...statements...
}"
(zenscript--require-token 'T_FUNCTION tokens
"function expected")
(let ((name (zenscript--require-token 'T_ID tokens
"identifier expected"))
(arguments (zenscript--parse-function-arguments tokens))
(type (if (zenscript--optional-token 'T_AS tokens)
(zenscript--parse-zentype tokens)
'(C_RAW "any")))
(start (1+ (nth 2 (zenscript--require-token 'T_AOPEN tokens
"{ expected"))))
statements end)
(while (and (zenscript--has-next-token tokens)
(not (let ((close (zenscript--optional-token 'T_ACLOSE tokens)))
(and close (setq end (nth 2 close))))))
(zenscript--cons! (zenscript--parse-statement tokens) statements))
(list name
arguments
type
(reverse statements)
start
end)))
(defun zenscript--parse-function-arguments (tokens)
"Parse a list of function arguments from TOKENS.
A list of arguments of the form:
(name type)
name:
The token that is the identifier of this binding.
type:
The ZenType of this binding."
(let (arguments)
(zenscript--require-token 'T_BROPEN tokens
"( expected")
(unless (zenscript--optional-token 'T_BRCLOSE tokens)
(let (break)
(while (not break)
(zenscript--cons! (list (zenscript--require-token 'T_ID tokens
"identifier expected")
(if (zenscript--optional-token 'T_AS tokens)
(zenscript--parse-zentype tokens)
'(C_RAW "any")))
arguments)
(unless (zenscript--optional-token 'T_COMMA tokens)
(zenscript--require-token 'T_BRCLOSE tokens
") or , expected")
(setq break t)))))
(reverse arguments)))
(defun zenscript--parse-zenclass (tokens)
"Parse the next class definition from TOKENS.
A list of the form:
(name fields constructors methods start end)
name:
The token that is the name of this class.
fields:
A list of fields, which are as returned by
`zenscript--parse-zenclass-field'.
constructors:
A list of constructors, which are as returned by
`zenscript--parse-zenclass-constructor'.
methods:
A list of methods, which are as returned by
`zenscript--parse-zenclass-method'.
start:
The position after the opening {
end:
The position before the closing }"
(zenscript--require-token 'T_ZEN_CLASS tokens
"zenClass expected")
(let ((id (zenscript--require-token 'T_ID tokens
"identifier expected"))
(start (1+ (nth 2 (zenscript--require-token 'T_AOPEN tokens
"{ expected"))))
keyword
fields constructors methods)
(while (setq keyword
(or (zenscript--optional-token 'T_VAL tokens)
(zenscript--optional-token 'T_VAR tokens)
(zenscript--optional-token 'T_STATIC tokens)
(zenscript--optional-token 'T_ZEN_CONSTRUCTOR tokens)
(zenscript--optional-token 'T_FUNCTION tokens)))
(pcase (car keyword)
((or 'T_VAL 'T_VAR
'T_STATIC)
(zenscript--cons! (zenscript--parse-zenclass-field tokens (eq (car keyword)
'T_STATIC))
fields))
('T_ZEN_CONSTRUCTOR
(zenscript--cons! (zenscript--parse-zenclass-constructor tokens)
constructors))
('T_FUNCTION
(zenscript--cons! (zenscript--parse-zenclass-method tokens)
methods))))
(list id
(reverse fields)
(reverse constructors)
(reverse methods)
start
(nth 2 (zenscript--require-token 'T_ACLOSE tokens
"} expected")))))
(defun zenscript--parse-zenclass-field (tokens static)
"Parse a field definition of a ZenClass from TOKENS.
A list of the form:
(name type init static)
name:
The token that is the name of this field.
type:
The ZenType of the field.
init:
The expression by which this field is initialized.
static:
t if the field is static, nil otherwise.
STATIC should be true if the class field is static."
(let ((id (zenscript--require-token 'T_ID tokens
"identifier expected"))
(type (if (zenscript--optional-token 'T_AS tokens)
(zenscript--parse-zentype tokens)
'(C_RAW "any")))
(init (when (zenscript--optional-token 'T_ASSIGN tokens)
(zenscript--parse-expression tokens))))
(zenscript--require-token 'T_SEMICOLON tokens
"; expected")
(list id type init static)))
(defun zenscript--parse-zenclass-constructor (tokens)
"Parse a constructor definition of a ZenClass from TOKENS.
A list of the form:
(arguments statements start end)
arguments:
The list of arguments, as returned by `zenscript--parse-function-arguments'.
statements:
A list of statements, which are as returned by `zenscript--parse-statement'.
start:
The position after the opening {
end:
The position before the closing }"
(let ((arguments (zenscript--parse-function-arguments tokens))
(start (1+ (nth 2 (zenscript--require-token 'T_AOPEN tokens
"{ expected"))))
statements end)
(while (and (zenscript--has-next-token tokens)
(not (let ((close (zenscript--optional-token 'T_ACLOSE tokens)))
(and close (setq end (nth 2 close))))))
(zenscript--cons! (zenscript--parse-statement tokens) statements))
(list arguments
(reverse statements)
start
end)))
(defun zenscript--parse-zenclass-method (tokens)
"Parse a method definition of a ZenClass from TOKENS.
A list of the form:
(name arguments type statements start end)
name:
The token that is the name of this method.
arguments:
The list of arguments, as returned by `zenscript--parse-function-arguments'.
type:
The ZenType that the method returns.
statements:
A list of statements, which are as returned by `zenscript--parse-statement'.
start:
The position after the opening {
end:
The position before the closing }"
(let ((id (zenscript--require-token 'T_ID tokens
"identifier expected"))
(arguments (zenscript--parse-function-arguments tokens))
(type (if (zenscript--optional-token 'T_AS tokens)
(zenscript--parse-zentype tokens)
'(C_RAW "any")))
(start (1+ (nth 2 (zenscript--require-token 'T_AOPEN tokens
"{ expected"))))
statements end)
(while (and (zenscript--has-next-token tokens)
(not (let ((close (zenscript--optional-token 'T_ACLOSE tokens)))
(and close (setq end (nth 2 close))))))
(zenscript--cons! (zenscript--parse-statement tokens) statements))
(list id
arguments
type
(reverse statements)
start
end)))
(defun zenscript--parse-statement (tokens)
"Parse the next statement from TOKENS.
A list of the form:
(type start after . value)
type:
The type of the statement, see below for possible values.
start:
The position at which this statement started.
after:
The position of the next token after this statement, or `point-max'.
value:
The value of the statement, which varies by type. See below.
The following types are possible:
S_BLOCK:
value: (statements end)
statements:
A list of statements, which are as returned by
`zenscript--parse-statement'.
end:
The position after the closing }
S_RETURN:
value: (expression)
expression:
An expression that is the value of this return
statement. See `zenscript--parse-expression'.
S_VAR:
value: (name type initializer final)
name:
The token that is the name of this variable.
type:
The explicit ZenType of this variable.
initializer:
The expression that initializes this variable.
final:
t if this variable is final, nil otherwise.
S_IF:
value: (predicate then else)
predicate:
The expression that is evaluated to determine
whether to evaluate THEN or ELSE.
then:
The statement to evaluate if predicate is true.
else:
The statement to evaluate if predicate is false.
S_FOR:
value: (names expression statement)
names:
A list of tokens that are bound variables.
expression:
The expression of what is being iterated over.
statement:
The expression to run in this for loop.
S_WHILE:
value: (predicate statement)
predicate:
The expression to test if the loop should be run.