-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtemplatel.el
2209 lines (1897 loc) · 75 KB
/
templatel.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
;;; templatel.el --- Templating language; -*- lexical-binding: t -*-
;;
;; Author: Lincoln Clarete <[email protected]>
;; URL: https://clarete.li/templatel
;; Version: 0.1.6
;; Package-Requires: ((emacs "25.1"))
;;
;; Copyright (C) 2020-2021 Lincoln Clarete
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;
;;; Commentary:
;;
;; This language compiles templates into Emacs Lisp functions that can
;; be called with different sets of variables. This work is inspired
;; by Jinja and among its main features, it supports if statements,
;; for loops, and a good amount of expressions that make it simpler to
;; manipulate data within the template.
;;
;; (require 'templatel)
;;
;; (templatel-render-string
;; "<h1>{{ title }}</h1>
;; <ul>
;; {% for user in users %}
;; <li><a href=\"{{ user.url }}\">{{ user.name }}</a></li>
;; {% endfor %}
;; </ul>"
;; '(("title" . "A nice web page")
;; ("users" . ((("url" . "http://clarete.li")
;; ("name" . "link"))
;; (("url" . "http://gnu.org")
;; ("name" . "Gnu!!"))))))
;;
;; This library also provides template in heritance and automatic HTML
;; entity escaping among other things. Take a look at the
;; documentation website for all the features:
;; https://clarete.li/templatel/doc.
;;
;;; Code:
(require 'seq)
(require 'subr-x)
(define-error 'templatel-syntax-error "Syntax Error" 'templatel-error)
(define-error 'templatel-runtime-error "Runtime Error" 'templatel-error)
(define-error 'templatel-backtracking "Backtracking" 'templatel-internal)
;; --- String utilities ---
(defun templatel--decoded (&rest bytes)
"Decode BYTES as a utf-8 string."
(decode-coding-string (apply #'unibyte-string bytes) 'utf-8))
(defun templatel--string (rune)
"Convert RUNE numeric value into a UTF-8 string."
(cond
((<= rune (- (ash 1 7) 1))
(byte-to-string rune))
((<= rune (- (ash 1 11) 1))
(templatel--decoded
(logior #xC0 (ash rune -6))
(logior #x80 (logand rune #x3F))))
((<= rune (- (ash 1 16) 1))
(templatel--decoded
(logior #xE0 (ash rune -12))
(logior #x80 (logand (ash rune -6) #x3F))
(logior #x80 (logand rune #x3F))))
(t
(templatel--decoded
(logior #xF0 (ash rune -18))
(logior #x80 (logand (ash rune -12) #x3F))
(logior #x80 (logand (ash rune -6) #x3F))
(logior #x80 (logand rune #x3F))))))
(defun templatel--join-chars (chars)
"Join all the CHARS forming a string."
(string-join (mapcar #'templatel--string chars) ""))
;; --- Scanner ---
(defun templatel--scanner-new (input file-name)
"Create scanner state for INPUT named FILE-NAME."
(list input 0 0 0 file-name))
(defun templatel--scanner-input (scanner)
"Input that SCANNER is operating on."
(car scanner))
(defun templatel--scanner-cursor (scanner)
"Cursor position of SCANNER."
(cadr scanner))
(defun templatel--scanner-cursor-set (scanner value)
"Set SCANNER's cursor to VALUE."
(setf (cadr scanner) value))
(defun templatel--scanner-cursor-incr (scanner)
"Increment SCANNER's cursor."
(templatel--scanner-cursor-set scanner (+ 1 (templatel--scanner-cursor scanner))))
(defun templatel--scanner-file (scanner)
"Line the SCANNER's cursor is in."
(elt scanner 4))
(defun templatel--scanner-line (scanner)
"Line the SCANNER's cursor is in."
(caddr scanner))
(defun templatel--scanner-line-set (scanner value)
"Set SCANNER's line to VALUE."
(setf (caddr scanner) value))
(defun templatel--scanner-line-incr (scanner)
"Increment SCANNER's line and reset col."
(templatel--scanner-col-set scanner 0)
(templatel--scanner-line-set scanner (+ 1 (templatel--scanner-line scanner))))
(defun templatel--scanner-col (scanner)
"Column the SCANNER's cursor is in."
(cadddr scanner))
(defun templatel--scanner-col-set (scanner value)
"Set column of the SCANNER as VALUE."
(setf (cadddr scanner) value))
(defun templatel--scanner-col-incr (scanner)
"Increment SCANNER's col."
(templatel--scanner-col-set scanner (+ 1 (templatel--scanner-col scanner))))
(defun templatel--scanner-state (scanner)
"Return a copy o SCANNER's state."
(copy-sequence (cdr scanner)))
(defun templatel--scanner-state-set (scanner state)
"Set SCANNER's state with STATE."
(templatel--scanner-cursor-set scanner (car state))
(templatel--scanner-line-set scanner (cadr state))
(templatel--scanner-col-set scanner (caddr state)))
(defun templatel--scanner-current (scanner)
"Peak the nth cursor of SCANNER's input."
(if (templatel--scanner-eos scanner)
(templatel--scanner-error scanner "EOF")
(elt (templatel--scanner-input scanner)
(templatel--scanner-cursor scanner))))
(defun templatel--scanner-error (_scanner msg)
"Generate error in SCANNER and document with MSG."
(signal 'templatel-backtracking msg))
(defun templatel--scanner-eos (scanner)
"Return t if cursor is at the end of SCANNER's input."
(eq (templatel--scanner-cursor scanner)
(length (templatel--scanner-input scanner))))
(defun templatel--scanner-next (scanner)
"Push SCANNER's cursor one character."
(if (templatel--scanner-eos scanner)
(templatel--scanner-error scanner "EOF")
(progn
(templatel--scanner-col-incr scanner)
(templatel--scanner-cursor-incr scanner))))
(defun templatel--scanner-any (scanner)
"Match any character on SCANNER's input minus EOF."
(let ((current (templatel--scanner-current scanner)))
(templatel--scanner-next scanner)
current))
(defun templatel--scanner-match (scanner c)
"Match current character under SCANNER's to C."
(if (eq c (templatel--scanner-current scanner))
(progn (templatel--scanner-next scanner) c)
(templatel--scanner-error scanner
(format
"Expected %s, got %s" c (templatel--scanner-current scanner)))))
(defun templatel--scanner-matchs (scanner s)
"Match SCANNER's input to string S."
(mapcar (lambda (i) (templatel--scanner-match scanner i)) s))
(defun templatel--scanner-range (scanner a b)
"Succeed if SCANNER's current entry is between A and B."
(let ((c (templatel--scanner-current scanner)))
(if (and (>= c a) (<= c b))
(templatel--scanner-any scanner)
(templatel--scanner-error scanner (format "Expected %s-%s, got %s" a b c)))))
(defun templatel--scanner-or (scanner options)
"Read the first one of OPTIONS that works SCANNER."
(if (null options)
(templatel--scanner-error scanner "No valid options")
(let ((state (templatel--scanner-state scanner)))
(condition-case nil
(funcall (car options))
(templatel-internal
(progn (templatel--scanner-state-set scanner state)
(templatel--scanner-or scanner (cdr options))))))))
(defun templatel--scanner-optional (scanner expr)
"Read EXPR from SCANNER returning nil if it fails."
(let ((state (templatel--scanner-state scanner)))
(condition-case nil
(funcall expr)
(templatel-internal
(templatel--scanner-state-set scanner state)
nil))))
(defun templatel--scanner-not (scanner expr)
"Fail if EXPR succeed, succeed when EXPR fail using SCANNER."
(let ((cursor (templatel--scanner-cursor scanner))
(succeeded (condition-case nil
(funcall expr)
(templatel-internal
nil))))
(templatel--scanner-cursor-set scanner cursor)
(if succeeded
(templatel--scanner-error scanner "Not meant to succeed")
t)))
(defun templatel--scanner-zero-or-more (scanner expr)
"Read EXPR zero or more time from SCANNER."
(let (output
(running t))
(while running
(let ((state (templatel--scanner-state scanner)))
(condition-case nil
(setq output (cons (funcall expr) output))
(templatel-internal
(progn
(templatel--scanner-state-set scanner state)
(setq running nil))))))
(reverse output)))
(defun templatel--scanner-one-or-more (scanner expr)
"Read EXPR one or more time from SCANNER."
(cons (funcall expr)
(templatel--scanner-zero-or-more scanner expr)))
(defun templatel--token-expr-op (scanner)
"Read '{{' off SCANNER's input."
(templatel--scanner-matchs scanner "{{"))
(defun templatel--token-stm-op- (scanner)
"Read '{%' off SCANNER's input."
(templatel--scanner-matchs scanner "{%"))
(defun templatel--token-stm-op (scanner)
"Read '{%' off SCANNER's input, with optional spaces afterwards."
(templatel--token-stm-op- scanner)
(templatel--parser-_ scanner))
(defun templatel--token-comment-op (scanner)
"Read '{#' off SCANNER's input."
(templatel--scanner-matchs scanner "{#")
(templatel--parser-_ scanner))
;; Notice these two tokens don't consume white spaces right after the
;; closing tag. That gets us a little closer to preserving entirely
;; the input provided to the parser.
(defun templatel--token-expr-cl (scanner)
"Read '}}' off SCANNER's input."
(templatel--scanner-matchs scanner "}}"))
(defun templatel--token-stm-cl (scanner)
"Read '%}' off SCANNER's input."
(templatel--scanner-matchs scanner "%}"))
(defun templatel--token-comment-cl (scanner)
"Read '#}' off SCANNER's input."
(templatel--scanner-matchs scanner "#}"))
(defun templatel--token-dot (scanner)
"Read '.' off SCANNER's input."
(templatel--scanner-matchs scanner ".")
(templatel--parser-_ scanner))
(defun templatel--token-comma (scanner)
"Read ',' off SCANNER's input."
(templatel--scanner-matchs scanner ",")
(templatel--parser-_ scanner))
(defun templatel--token-if (scanner)
"Read 'if' off SCANNER's input."
(templatel--scanner-matchs scanner "if")
(templatel--parser-_ scanner))
(defun templatel--token-elif (scanner)
"Read 'elif' off SCANNER's input."
(templatel--scanner-matchs scanner "elif")
(templatel--parser-_ scanner))
(defun templatel--token-else (scanner)
"Read 'else' off SCANNER's input."
(templatel--scanner-matchs scanner "else")
(templatel--parser-_ scanner))
(defun templatel--token-endif (scanner)
"Read 'endif' off SCANNER's input."
(templatel--scanner-matchs scanner "endif")
(templatel--parser-_ scanner))
(defun templatel--token-for (scanner)
"Read 'for' off SCANNER's input."
(templatel--scanner-matchs scanner "for")
(templatel--parser-_ scanner))
(defun templatel--token-endfor (scanner)
"Read 'endfor' off SCANNER's input."
(templatel--scanner-matchs scanner "endfor")
(templatel--parser-_ scanner))
(defun templatel--token-block (scanner)
"Read 'block' off SCANNER's input."
(templatel--scanner-matchs scanner "block")
(templatel--parser-_ scanner))
(defun templatel--token-endblock (scanner)
"Read 'endblock' off SCANNER's input."
(templatel--scanner-matchs scanner "endblock")
(templatel--parser-_ scanner))
(defun templatel--token-extends (scanner)
"Read 'extends' off SCANNER's input."
(templatel--scanner-matchs scanner "extends")
(templatel--parser-_ scanner))
(defun templatel--token-include (scanner)
"Read 'include' off SCANNER's input."
(templatel--scanner-matchs scanner "include")
(templatel--parser-_ scanner))
(defun templatel--token-in (scanner)
"Read 'in' off SCANNER's input."
(let ((m (templatel--scanner-matchs scanner "in")))
(templatel--parser-_ scanner)
(templatel--join-chars m)))
(defun templatel--token-is (scanner)
"Read 'is' off SCANNER's input."
(let ((m (templatel--scanner-matchs scanner "is")))
(templatel--parser-_ scanner)
(templatel--join-chars m)))
(defun templatel--token-and (scanner)
"Read 'and' off SCANNER's input."
(let ((m (templatel--scanner-matchs scanner "and")))
(templatel--parser-_ scanner)
(templatel--join-chars m)))
(defun templatel--token-not (scanner)
"Read 'not' off SCANNER's input."
(let ((m (templatel--scanner-matchs scanner "not")))
(templatel--parser-_ scanner)
(templatel--join-chars m)))
(defun templatel--token-or (scanner)
"Read 'or' off SCANNER's input."
(let ((m (templatel--scanner-matchs scanner "or")))
(templatel--parser-_ scanner)
(templatel--join-chars m)))
(defun templatel--token-paren-op (scanner)
"Read '(' off SCANNER's input."
(templatel--scanner-matchs scanner "(")
(templatel--parser-_ scanner))
(defun templatel--token-paren-cl (scanner)
"Read ')' off SCANNER's input."
(templatel--scanner-matchs scanner ")")
(templatel--parser-_ scanner))
(defun templatel--token-| (scanner)
"Read '|' off SCANNER's input."
(let ((m (templatel--scanner-matchs scanner "|")))
(templatel--parser-_ scanner)
(templatel--join-chars m)))
(defun templatel--token-+ (scanner)
"Read '+' off SCANNER's input."
(let ((m (templatel--scanner-matchs scanner "+")))
(templatel--parser-_ scanner)
(templatel--join-chars m)))
(defun templatel--token-- (scanner)
"Read '-' off SCANNER's input."
(let ((m (templatel--scanner-matchs scanner "-")))
(templatel--parser-_ scanner)
(templatel--join-chars m)))
(defun templatel--token-* (scanner)
"Read '*' off SCANNER's input."
(let ((m (templatel--scanner-matchs scanner "*")))
(templatel--parser-_ scanner)
(templatel--join-chars m)))
(defun templatel--token-** (scanner)
"Read '**' off SCANNER's input."
(let ((m (templatel--scanner-matchs scanner "**")))
(templatel--parser-_ scanner)
(templatel--join-chars m)))
(defun templatel--token-slash (scanner)
"Read '/' off SCANNER's input."
(let ((m (templatel--scanner-matchs scanner "/")))
(templatel--parser-_ scanner)
(templatel--join-chars m)))
(defun templatel--token-dslash (scanner)
"Read '//' off SCANNER's input."
(let ((m (templatel--scanner-matchs scanner "//")))
(templatel--parser-_ scanner)
(templatel--join-chars m)))
(defun templatel--token-= (scanner)
"Read '=' off SCANNER's input."
(let ((m (templatel--scanner-matchs scanner "=")))
(templatel--parser-_ scanner)
(templatel--join-chars m)))
(defun templatel--token-== (scanner)
"Read '==' off SCANNER's input."
(let ((m (templatel--scanner-matchs scanner "==")))
(templatel--parser-_ scanner)
(templatel--join-chars m)))
(defun templatel--token-!= (scanner)
"Read '!=' off SCANNER's input."
(let ((m (templatel--scanner-matchs scanner "!=")))
(templatel--parser-_ scanner)
(templatel--join-chars m)))
(defun templatel--token-> (scanner)
"Read '>' off SCANNER's input."
(let ((m (templatel--scanner-matchs scanner ">")))
(templatel--parser-_ scanner)
(templatel--join-chars m)))
(defun templatel--token-< (scanner)
"Read '<' off SCANNER's input."
(let ((m (templatel--scanner-matchs scanner "<")))
(templatel--parser-_ scanner)
(templatel--join-chars m)))
(defun templatel--token->= (scanner)
"Read '>=' off SCANNER's input."
(let ((m (templatel--scanner-matchs scanner ">=")))
(templatel--parser-_ scanner)
(templatel--join-chars m)))
(defun templatel--token-<= (scanner)
"Read '<=' off SCANNER's input."
(let ((m (templatel--scanner-matchs scanner "<=")))
(templatel--parser-_ scanner)
(templatel--join-chars m)))
(defun templatel--token-% (scanner)
"Read '%' off SCANNER's input."
;; This is needed or allowing a cutting point to be introduced right
;; after the operator of a binary expression.
(templatel--scanner-not scanner (lambda() (templatel--token-stm-cl scanner)))
(let ((m (templatel--scanner-matchs scanner "%")))
(templatel--parser-_ scanner)
(templatel--join-chars m)))
(defun templatel--token-^ (scanner)
"Read '^' off SCANNER's input."
(let ((m (templatel--scanner-matchs scanner "^")))
(templatel--parser-_ scanner)
(templatel--join-chars m)))
;; --- Parser ---
(defun templatel--parser-rstrip-comment (scanner thing)
"Parse THING then try to consume spaces from SCANNER."
(let ((value (funcall thing scanner)))
(templatel--scanner-zero-or-more
scanner
(lambda() (templatel--parser-comment scanner)))
value))
;; GR: Template <- Comment* (Text Comment* / Statement Comment* / Expression Comment*)+
(defun templatel--parser-template (scanner)
"Parse Template entry from SCANNER's input."
(templatel--scanner-zero-or-more
scanner
(lambda() (templatel--parser-comment scanner)))
(cons
"Template"
(templatel--scanner-one-or-more
scanner
(lambda() (templatel--scanner-or
scanner
(list (lambda() (templatel--parser-rstrip-comment scanner #'templatel--parser-text))
(lambda() (templatel--parser-rstrip-comment scanner #'templatel--parser-statement))
(lambda() (templatel--parser-rstrip-comment scanner #'templatel--parser-expression))))))))
;; GR: Text <- (!(_EXPR_OPEN / _STM_OPEN / _COMMENT_OPEN) .)+
(defun templatel--parser-text (scanner)
"Parse Text entries from SCANNER's input."
(cons
"Text"
(templatel--join-chars
(templatel--scanner-one-or-more
scanner
(lambda()
(templatel--scanner-not
scanner
(lambda()
(templatel--scanner-or
scanner
(list
(lambda() (templatel--token-expr-op scanner))
(lambda() (templatel--token-stm-op- scanner))
(lambda() (templatel--token-comment-op scanner))))))
(let ((chr (templatel--scanner-any scanner)))
(if (eq chr ?\n)
(templatel--scanner-line-incr scanner))
chr))))))
;; GR: Statement <- IfStatement
;; GR: / ForStatement
;; GR: / BlockStatement
;; GR: / ExtendsStatement
;; GR: / IncludeStatement
(defun templatel--parser-statement (scanner)
"Parse a statement from SCANNER."
(templatel--scanner-or
scanner
(list
(lambda() (templatel--parser-if-stm scanner))
(lambda() (templatel--parser-for-stm scanner))
(lambda() (templatel--parser-block-stm scanner))
(lambda() (templatel--parser-extends-stm scanner))
(lambda() (templatel--parser-include-stm scanner)))))
;; GR: IfStatement <- _Elif / _Else / _If Expr _STM_CLOSE Template _EndIf
(defun templatel--parser-if-stm (scanner)
"SCANNER."
(templatel--scanner-or
scanner
(list (lambda() (templatel--parser-if-stm-elif scanner))
(lambda() (templatel--parser-if-stm-else scanner))
(lambda() (templatel--parser-if-stm-endif scanner)))))
(defun templatel--parser-stm-cl (scanner)
"Read stm-cl off SCANNER or error out if it's not there."
(templatel--parser-cut
scanner
(lambda() (templatel--token-stm-cl scanner))
"Statement not closed with \"%}\""))
;; GR: _Elif <- _If Expr _STM_CLOSE Template Elif+ Else?
(defun templatel--parser-if-stm-elif (scanner)
"Parse elif from SCANNER."
(templatel--parser-if scanner)
(let* ((expr (templatel--parser-expr scanner))
(_ (templatel--parser-stm-cl scanner))
(tmpl (templatel--parser-template scanner))
(elif (templatel--scanner-one-or-more scanner (lambda() (templatel--parser-elif scanner))))
(else (templatel--scanner-optional scanner (lambda() (templatel--parser-else scanner)))))
(if else
(cons "IfElif" (list expr tmpl elif else))
(progn
(templatel--parser-endif scanner)
(cons "IfElif" (list expr tmpl elif))))))
;; GR: _Else <- _If Expr _STM_CLOSE Template Else
(defun templatel--parser-if-stm-else (scanner)
"Parse else from SCANNER."
(templatel--parser-if scanner)
(let* ((expr (templatel--parser-expr scanner))
(_ (templatel--parser-stm-cl scanner))
(tmpl (templatel--parser-template scanner))
(else (templatel--parser-else scanner)))
(cons "IfElse" (list expr tmpl else))))
;; _If Expr _STM_CLOSE Template _EndIf -- No grammar annotation because it's repeated
(defun templatel--parser-if-stm-endif (scanner)
"Parse endif from SCANNER."
(templatel--parser-if scanner)
(let* ((expr (templatel--parser-expr scanner))
(_ (templatel--parser-stm-cl scanner))
(tmpl (templatel--parser-template scanner))
(_ (templatel--parser-endif scanner)))
(cons "IfStatement" (list expr tmpl))))
;; GR: Elif <- _STM_OPEN _elif Expr _STM_CLOSE Template
(defun templatel--parser-elif (scanner)
"Parse elif expression off SCANNER."
(templatel--token-stm-op scanner)
(templatel--token-elif scanner)
(let ((expr (templatel--parser-expr scanner))
(_ (templatel--parser-stm-cl scanner))
(tmpl (templatel--parser-template scanner)))
(cons "Elif" (list expr tmpl))))
;; GR: _If <- _STM_OPEN _if
(defun templatel--parser-if (scanner)
"Parse if condition off SCANNER."
(templatel--token-stm-op scanner)
(templatel--token-if scanner))
;; GR: Else <- _STM_OPEN _else _STM_CLOSE Template _EndIf
(defun templatel--parser-else (scanner)
"Parse else expression off SCANNER."
(templatel--token-stm-op scanner)
(templatel--token-else scanner)
(templatel--parser-stm-cl scanner)
(let ((tmpl (templatel--parser-template scanner)))
(templatel--parser-endif scanner)
(cons "Else" (list tmpl))))
;; GR: _EndIf <- _STM_OPEN _endif _STM_CLOSE
(defun templatel--parser-endif (scanner)
"Parse endif tag off SCANNER."
(templatel--parser-cut
scanner
(lambda()
(templatel--token-stm-op scanner)
(templatel--token-endif scanner)
(templatel--parser-stm-cl scanner))
"Missing endif statement"))
;; GR: ForStatement <- _For Expr _in Expr _STM_CLOSE Template _EndFor
;; GR: _For <- _STM_OPEN _for
(defun templatel--parser-for-stm (scanner)
"Parse for statement from SCANNER."
(templatel--token-stm-op scanner)
(templatel--token-for scanner)
(let ((iter (templatel--parser-identifier scanner))
(_ (templatel--token-in scanner))
(iterable (templatel--parser-expr scanner))
(_ (templatel--parser-stm-cl scanner))
(tmpl (templatel--parser-template scanner))
(_ (templatel--parser-endfor scanner)))
(cons "ForStatement" (list iter iterable tmpl))))
;; GR: _EndFor <- _STM_OPEN _endfor _STM_CLOSE
(defun templatel--parser-endfor (scanner)
"Parse {% endfor %} statement from SCANNER."
(templatel--parser-cut
scanner
(lambda()
(templatel--token-stm-op scanner)
(templatel--token-endfor scanner)
(templatel--parser-stm-cl scanner))
"Missing endfor statement"))
;; GR: BlockStatement <- _Block String _STM_CLOSE Template? _EndBlock
(defun templatel--parser-block-stm (scanner)
"Parse block statement from SCANNER."
(templatel--token-stm-op scanner)
(templatel--token-block scanner)
(let ((name (templatel--parser-cut
scanner
(lambda() (templatel--parser-identifier scanner))
"Missing block name"))
(_ (templatel--parser-_ scanner))
(_ (templatel--parser-stm-cl scanner))
(tmpl (templatel--scanner-optional
scanner
(lambda() (templatel--parser-template scanner)))))
(templatel--parser-endblock scanner)
(cons "BlockStatement" (list name tmpl))))
;; GR: _EndBlock <- _STM_OPEN _endblock _STM_CLOSE
(defun templatel--parser-endblock (scanner)
"Parse {% endblock %} statement from SCANNER."
(templatel--parser-cut
scanner
(lambda()
(templatel--token-stm-op scanner)
(templatel--token-endblock scanner)
(templatel--parser-stm-cl scanner))
"Missing endblock statement"))
;; GR: ExtendsStatement <- _STM_OPEN _extends String _STM_CLOSE
(defun templatel--parser-extends-stm (scanner)
"Parse extends statement from SCANNER."
(templatel--token-stm-op scanner)
(templatel--token-extends scanner)
(let ((name (templatel--parser-cut
scanner
(lambda() (templatel--parser-string scanner))
"Missing template name in extends statement")))
(templatel--parser-_ scanner)
(templatel--parser-stm-cl scanner)
(cons "ExtendsStatement" (list name))))
;; GR: IncludeStatement <- _STM_OPEN _include String _STM_CLOSE
(defun templatel--parser-include-stm (scanner)
"Parse extends statement from SCANNER."
(templatel--token-stm-op scanner)
(templatel--token-include scanner)
(let ((name (templatel--parser-cut
scanner
(lambda() (templatel--parser-string scanner))
"Missing template name in include statement")))
(templatel--parser-_ scanner)
(templatel--parser-stm-cl scanner)
(cons "IncludeStatement" (list name))))
;; GR: Expression <- _EXPR_OPEN Expr _EXPR_CLOSE
(defun templatel--parser-expression (scanner)
"SCANNER."
(templatel--token-expr-op scanner)
(templatel--parser-_ scanner)
(let ((expr (templatel--parser-expr scanner)))
(templatel--parser-cut
scanner
(lambda() (templatel--token-expr-cl scanner))
"Unclosed bracket")
(cons "Expression" (list expr))))
;; GR: Expr <- Logical
(defun templatel--parser-expr (scanner)
"Read an expression from SCANNER."
(cons
"Expr"
(list (templatel--parser-logical scanner))))
(defun templatel--parser-cut (scanner fn msg)
"Try to parse FN off SCANNER or error with MSG.
There are two types of errors emitted by this parser:
1. Backtracking (internal), which is caught by most scanner
functions, like templatel--scanner-or and templatel--scanner-zero-or-more.
2. Syntax Error (public), which signals an unrecoverable parsing
error.
This function catches backtracking errors and transform them in
syntax errors. It must be carefully explicitly on places where
backtracking should be interrupted earlier."
(condition-case nil
(funcall fn)
(templatel-internal
(signal 'templatel-syntax-error
(format "%s at %s,%s: %s"
(or (templatel--scanner-file scanner) "<string>")
(1+ (templatel--scanner-line scanner))
(1+ (templatel--scanner-col scanner))
msg)))))
(defun templatel--parser-item-or-named-collection (name first rest)
"NAME FIRST REST."
(if (null rest)
first
(cons name (cons first rest))))
(defun templatel--parser-binary (scanner name randfn ratorfn)
"Parse binary operator NAME from SCANNER.
A binary operator needs two functions: one for reading the
operands (RANDFN) and another one to read the
operator (RATORFN)."
(templatel--parser-item-or-named-collection
(if (null name) "BinOp" name)
(funcall randfn scanner)
(templatel--scanner-zero-or-more
scanner
(lambda()
(cons
(funcall ratorfn scanner)
(templatel--parser-cut
scanner
(lambda() (funcall randfn scanner))
"Missing operand after binary operator"))))))
;; GR: Logical <- Comparison ((AND / OR) Comparison)*
(defun templatel--parser-logical (scanner)
"Read Logical from SCANNER."
(templatel--parser-binary
scanner
nil ; "Logical"
#'templatel--parser-comparison
(lambda(s)
(templatel--scanner-or
s
(list
(lambda() (templatel--token-and s))
(lambda() (templatel--token-or s)))))))
;; GR: Comparison <- Term ((EQ / NEQ / LTE / GTE / LT / GT / IN) Term)*
(defun templatel--parser-comparison (scanner)
"Read a Comparison from SCANNER."
(templatel--parser-binary
scanner
nil ; "Comparison"
#'templatel--parser-term
(lambda(s)
(templatel--scanner-or
s
(list
(lambda() (templatel--token-== s))
(lambda() (templatel--token-!= s))
(lambda() (templatel--token-<= s))
(lambda() (templatel--token->= s))
(lambda() (templatel--token-< s))
(lambda() (templatel--token-> s))
(lambda() (templatel--token-in s)))))))
;; GR: Term <- Factor ((PLUS / MINUS) Factor)*
(defun templatel--parser-term (scanner)
"Read Term from SCANNER."
(templatel--parser-binary
scanner
nil ; "Term"
#'templatel--parser-factor
(lambda(s)
(templatel--scanner-or
s
(list
(lambda() (templatel--token-+ s))
(lambda() (templatel--token-- s)))))))
;; GR: Factor <- Power ((STAR / DSLASH / SLASH) Power)*
(defun templatel--parser-factor (scanner)
"Read Factor from SCANNER."
(templatel--parser-binary
scanner
nil ; "Factor"
#'templatel--parser-power
(lambda(s)
(templatel--scanner-or
s
(list
(lambda() (templatel--token-* s))
(lambda() (templatel--token-slash s))
(lambda() (templatel--token-dslash s)))))))
;; GR: Power <- Test ((POWER / MOD) Test)*
(defun templatel--parser-power (scanner)
"Read Power from SCANNER."
(templatel--parser-binary
scanner
nil ; "Power"
#'templatel--parser-test
(lambda(s)
(templatel--scanner-or
s
(list
(lambda() (templatel--token-** s))
(lambda() (templatel--token-% s)))))))
;; GR: Test <- Filter (_IS Filter)*
(defun templatel--parser-test (scanner)
"Read Test from SCANNER."
(templatel--parser-binary
scanner
"Test"
#'templatel--parser-filter #'templatel--token-is))
;; GR: Filter <- Unary (_PIPE Unary)*
(defun templatel--parser-filter (scanner)
"Read Filter from SCANNER."
(templatel--parser-binary
scanner
"Filter"
#'templatel--parser-unary #'templatel--token-|))
;; GR: UnaryOp <- PLUS / MINUS / NOT / BNOT
(defun templatel--parser-unary-op (scanner)
"Read an Unary operator from SCANNER."
(templatel--scanner-or
scanner
(list
(lambda() (templatel--token-+ scanner))
(lambda() (templatel--token-- scanner))
(lambda() (templatel--token-not scanner)))))
;; GR: Unary <- UnaryOp Unary / UnaryOp Primary / Primary
(defun templatel--parser-unary (scanner)
"Read Unary from SCANNER."
(templatel--scanner-or
scanner
(list
(lambda()
(cons
"Unary"
(list
(templatel--parser-unary-op scanner)
(templatel--parser-unary scanner))))
(lambda()
(cons
"Unary"
(list
(templatel--parser-unary-op scanner)
(templatel--parser-cut
scanner
(lambda() (templatel--parser-primary scanner))
"Missing operand after unary operator"))))
(lambda() (templatel--parser-primary scanner)))))
;; GR: Primary <- _PAREN_OPEN Expr _PAREN_CLOSE
;; GR: / Element
(defun templatel--parser-primary (scanner)
"Read Primary from SCANNER."
(templatel--scanner-or
scanner
(list
(lambda()
(templatel--token-paren-op scanner)
(let ((expr (templatel--parser-expr scanner)))
(templatel--token-paren-cl scanner)
expr))
(lambda() (templatel--parser-element scanner)))))
;; GR: Attribute <- Identifier (_dot Identifier)+
(defun templatel--parser-attribute (scanner)
"Read an Attribute from SCANNER."
(cons
"Attribute"
(cons
(templatel--parser-identifier scanner)
(templatel--scanner-one-or-more
scanner
(lambda()
(templatel--token-dot scanner)
(templatel--parser-identifier scanner))))))
;; GR: Element <- Value / Attribute / FnCall / Identifier
(defun templatel--parser-element (scanner)
"Read Element off SCANNER."
(cons
"Element"
(list
(templatel--scanner-or
scanner
(list
(lambda() (templatel--parser-value scanner))
(lambda() (templatel--parser-attribute scanner))
(lambda() (templatel--parser-fncall scanner))
(lambda() (templatel--parser-identifier scanner)))))))
(defun templatel--parser-paren-cl (scanner)
"Read a closed parentheses with a cutting point from SCANNER."
(templatel--parser-cut
scanner
(lambda() (templatel--token-paren-cl scanner))
"Unclosed parentheses"))
;; GR: FnCall <- Identifier ParamList
(defun templatel--parser-fncall (scanner)
"Read FnCall off SCANNER."
(cons
"FnCall"
(cons
(templatel--parser-identifier scanner)
(templatel--parser-paramlist scanner))))
;; GR: ParamList <- _ParamListOnlyNamed
;; GR: / _ParamListPosNamed
;; GR: / _ParamListOnlyPos
;; GR: / _PAREN_OPEN _PAREN_CLOSE
(defun templatel--parser-paramlist (scanner)
"Read parameter list off SCANNER."
(templatel--scanner-or
scanner
(list
(lambda() (templatel--parser--paramlist-only-named scanner))
(lambda() (templatel--parser--paramlist-pos-named scanner))
(lambda() (templatel--parser--paramlist-only-pos scanner))
(lambda()
(templatel--token-paren-op scanner)
(templatel--parser-paren-cl scanner)
nil))))
;; GR: _ParamListOnlyNamed <- _PAREN_OPEN NamedParams _PAREN_CLOSE
(defun templatel--parser--paramlist-only-named (scanner)
"Read exclusively named params from SCANNER."
(templatel--token-paren-op scanner)
(let ((params (templatel--parser-namedparams scanner)))
(templatel--parser-paren-cl scanner)
params))
;; GR: _ParamListPosNamed <- _PAREN_OPEN Params NamedParams _PAREN_CLOSE
(defun templatel--parser--paramlist-pos-named (scanner)
"Read positionnal and named parameters from SCANNER."
(templatel--token-paren-op scanner)
(let* ((positional (templatel--parser-params scanner))
(_ (templatel--token-comma scanner))
(named (templatel--parser-namedparams scanner)))