forked from jasom/nyaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nyaml.lisp
1787 lines (1522 loc) · 43 KB
/
nyaml.lisp
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
;;;; nyaml.lisp
;;;; The main meat of the grammar for generating a parse tree from YAML
(in-package #:nyaml)
(named-readtables:in-readtable :fare-quasiquote)
(defvar *warning* (lambda (fmt-string &rest args) (declare (ignore fmt-string args))))
(defvar *error* (lambda (fmt-string args)
(apply 'error fmt-string args)))
;;; "nyaml" goes here. Hacks and glory await!
;; rule 1
(defrule c-printable
(character-ranges #.(code-char #x9)
#.(code-char #xa)
#.(code-char #xd)
(#.(code-char #x20) #.(code-char #x7e))
#.(code-char #x85)
(#.(code-char #xa0) #.(code-char #xd7ff))
(#.(code-char #xe000) #.(code-char #xfffd))
(#.(code-char #x10000) #.(code-char #x10ffff))))
;; rule 2
(defrule nb-json (character-ranges #.(code-char #x9)
(#.(code-char #x20) #.(code-char #x10ffff))))
;; rule 3
(defrule c-byte-order-mark #.(code-char #xfeff))
;; rule 4
(defrule c-sequence-entry #\-)
;; rule 5
(defrule c-mapping-key #\?)
;; rule 6
(defrule c-mapping-value #\:)
;; rule 7
(defrule c-collect-entry #\,)
;; rule 8
(defrule c-sequence-start #\[)
;; rule 9
(defrule c-sequence-end #\])
;; rule 10
(defrule c-mapping-start #\{)
;; rule 11
(defrule c-mapping-end #\})
;; rule 12
(defrule c-comment #\#)
;; rule 13
(defrule c-anchor #\&)
;; rule 14
(defrule c-alias #\*)
;; rule 15
(defrule c-tag #\!)
;; rule 16
(defrule c-literal #\|)
;; rule 17
(defrule c-folded #\>)
;; rule 18
(defrule c-single-quote #\')
;; rule 19
(defrule c-double-quote #\")
;;rule 20
(defrule c-directive #\%)
;; rule 21
(defrule c-reserved (character-ranges #\@ #\`))
;; rule 22
(defrule c-indicator
(character-ranges #\- #\? #\: #\, #\[ #\] #\{ #\} #\# #\&
#\* #\! #\| #\> #\' #\" #\% #\@ #\`))
;; rule 23
(defrule c-flow-indicator
(character-ranges #\, #\[ #\] #\{ #\}))
;; rule 24
(defrule b-line-feed (character-ranges #.(code-char #xa)))
;; rule 25
(defrule b-carriage-return (character-ranges #.(code-char #xd)))
;; rule 26
(defrule b-char (or b-line-feed b-carriage-return))
;; rule 27
(defrule nb-char (and (! (or b-char c-byte-order-mark)) c-printable))
;; rule 28
(defrule b-break (or
(and b-carriage-return b-line-feed)
b-carriage-return
b-line-feed))
;; rule 29
(defrule b-as-line-feed b-break (:constant #\Newline))
;; rule 30
(defrule b-non-content b-break (:constant nil))
;; rule 31
(defrule s-space (character-ranges #.(code-char #x20)))
;; rule 32
(defrule s-tab (character-ranges #.(code-char #x9)))
;; rule 33
(defrule s-white (or s-space s-tab))
;;rule 34
(defrule ns-char (and (! s-white) nb-char))
;;rule 35
(defrule ns-dec-digit (character-ranges (#\0 #\9)))
(defrule ns-dec-digit-positive (character-ranges (#\1 #\9)))
;;rule 36
(defrule ns-hex-digit
(or ns-dec-digit
(character-ranges (#\A #\F)
(#\a #\f))))
;;rule 37
(defrule ns-ascii-letter
(character-ranges (#\a #\z)
(#\A #\Z)))
;; rule 38
(defrule ns-word-char
(or ns-dec-digit ns-ascii-letter #\-))
;; rule 39
(defrule ns-uri-char
(or
(and #\% ns-hex-digit ns-hex-digit)
ns-word-char
(character-ranges
#\# #\; #\/ #\? #\: #\@ #\& #\= #\+ #\$
#\, #\_ #\. #\! #\~ #\* #\' #\( #\) #\[ #\])))
;; rule 40
(defrule ns-tag-char
(and
(! (or #\! c-flow-indicator))
ns-uri-char))
;; rule 41
(defrule c-escape #\\)
;; rule 42
(defrule ns-esc-null #\0 (:constant #\Nul))
;; rule 43
(defrule ns-esc-bell #\a (:constant (code-char #x7)))
;; rule 44
(defrule ns-esc-backspace #\b (:constant (code-char #x8)))
;; rule 45
(defrule ns-esc-horizontal-tab #\t (:constant (code-char #x9)))
(defrule ns-esc-line-feed #\n (:constant (code-char #xa)))
(defrule ns-esc-vertical-tab #\v (:constant (code-char #xb)))
(defrule ns-esc-form-feed #\f (:constant (code-char #xc)))
(defrule ns-esc-carriage-return #\r (:constant (code-char #xd)))
(defrule ns-esc-escape #\e (:constant (code-char #x1b)))
(defrule ns-esc-space #\Space)
(defrule ns-esc-double-quote #\")
(defrule ns-esc-slash #\/)
(defrule ns-esc-backslash #\\)
(defrule ns-esc-next-line #\N (:constant (code-char #x85)))
(defrule ns-esc-non-breaking-space #\_ (:constant (code-char #xa0)))
(defrule ns-esc-line-separator #\L (:constant (code-char #x2028)))
(defrule ns-esc-paragraph-separator #\P (:constant (code-char #x2029)))
(defrule ns-esc-8-bit
(and #\x ns-hex-digit ns-hex-digit)
(:lambda (result)
(code-char
(parse-integer (coerce (cdr result) 'string) :radix 16))))
(defrule ns-esc-16-bit
(and #\u
ns-hex-digit ns-hex-digit
ns-hex-digit ns-hex-digit)
(:lambda (result)
(code-char
(parse-integer (coerce (cdr result) 'string) :radix 16))))
(defrule ns-esc-32-bit
(and #\U
ns-hex-digit ns-hex-digit
ns-hex-digit ns-hex-digit
ns-hex-digit ns-hex-digit
ns-hex-digit ns-hex-digit)
(:lambda (result)
(code-char
(parse-integer (coerce (cdr result) 'string) :radix 16))))
(defrule c-ns-esc-char
(and #\\
(or ns-esc-null ns-esc-bell ns-esc-backspace
ns-esc-horizontal-tab ns-esc-line-feed
ns-esc-vertical-tab ns-esc-form-feed
ns-esc-carriage-return ns-esc-escape ns-esc-space
ns-esc-double-quote ns-esc-slash ns-esc-backslash
ns-esc-next-line ns-esc-non-breaking-space
ns-esc-line-separator ns-esc-paragraph-separator
ns-esc-8-bit ns-esc-16-bit ns-esc-32-bit ))
(:function second))
;; rule 63
#+(or)(eval-when (:compile-toplevel :load-toplevel :execute)
(defun expand-arguments (arglist &optional so-far)
(let (name values)
(cond
((null arglist) (return-from expand-arguments so-far))
((listp (car arglist))
(setf name (caar arglist)
values (cdar arglist)))
(t (setf name (car arglist)
values (loop for i from 0 to 10 collect i))))
(expand-arguments
(cdr arglist)
(if so-far
(loop for i in so-far
nconc (loop for j in values
collect `(,@i (,name ,j))))
(loop for i in values collect `((,name ,i))))))))
#+(or)(eval-when (:compile-toplevel :load-toplevel :execute)
(defun prule (name &rest arguments)
(intern (format nil "~a~{-~a~}" name arguments) *package*)))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun generate-transform (options)
(let ((transform (list ''identity)))
(loop for option in options
do
(case (car option)
(:constant
(push `(constantly ,(cadr option)) transform))
(:function
(push `',(cadr option) transform))
(:identity t)
(:text (push ''text transform))
(:lambda
(when (member '&bounds (cadr option))
(error "This feature not yet implemented"))
(push `(alexandria:compose
(lambda ,(cadr option) ,@(cddr option)))
transform))
(:destructure
(let ((arg (gensym)))
(push `(alexandria:compose
(lambda (,arg)
(destructuring-bind ,(cadr option)
,arg
,@(cddr option))))
transform)))
(t (error "This feature not yet implemented"))))
`(alexandria:compose ,@transform))))
;; TODO parse out declaraction &ct
(defmacro define-parameterized-rule (name arguments &body b)
`(defun ,name (input position end ,@(mapcar (lambda (x) (if (listp x) (car x) x)) arguments))
(let* ((pcalls
(let ((pcalls2))
(flet ((prule (name &rest args)
#+debug(format t "~&PRULE: ~A~%" name)
(push `(,(gensym (format nil "~a-~{~a-~}" name args)) ,name ,@args) pcalls2)))
,(car b))
(reverse pcalls2)))
(unbind-me (mapcar #'car pcalls)))
#+debug(format t "~&PCALLS: ~S~%" pcalls)
(unwind-protect
(progn (loop for item in pcalls
do (setf (fdefinition (car item))
(let ((item item))
(lambda (input position end)
(apply (cadr item) input position end (cddr item)))))
(assert (not (find-rule (car item))))
(add-rule (car item) (make-instance 'rule
:expression `(function ,(car item))))
#+nyaml-trace(trace-rule (car item) :recursive t))
(multiple-value-bind (production position result)
(esrap:parse
(flet ((prule (&rest ignoreme)
(declare (ignore ignoreme))
(car (pop pcalls))))
,(car b))
input :start position :end end :junk-allowed t)
(if result
(values (funcall ,(generate-transform (cdr b)) production)
position result)
(values production position result))))
(loop for item in unbind-me do (fmakunbound item)
(esrap:remove-rule item)))))
#+(or)`(progn
#+(or)(eval-when (:compile-toplevel :load-toplevel :execute)
(defun ,name ,(mapcar (lambda (x) (if (listp x) (car x) x)) arguments)
(intern (format nil "~a~{-~a~}" ',name (list ,@(mapcar (lambda (x) (if (listp x) (car x) x)) arguments))) ,*package*)))
,@(loop
for args in (expand-arguments arguments)
collect `(defrule
,(intern (format nil "~a~{-~a~}"
name
(mapcar (lambda (x) (eval (cadr x))) args)))
,(eval `(let ,args
,(car b)))
,@(cdr b)))))
(defun parse-parameterized-rule (name parameters string)
(apply name string 0 (length string) parameters))
;; rule 63
(define-parameterized-rule s-indent (n)
(if (non-negative-integer-p n)
`(and ,@(loop repeat n collect 's-space))
`(or))
(:text t))
;; rule 64
(define-parameterized-rule s-indent-< (n)
`(or
,@(loop for i from (1- n) downto 0
collect `(and ,(prule 's-indent i)
(& (! s-space))))
)
(:text t))
;; rule 65
(define-parameterized-rule s-indent-<= (n)
`(or ,(prule 's-indent n) ,(prule 's-indent-< n))
(:text t))
(defrule s-separate-in-line
(or
(+ s-white)
(function start-of-line)))
;; rule 68
(define-parameterized-rule s-block-line-prefix (n)
(prule 's-indent n))
;; rule 69
(define-parameterized-rule s-flow-line-prefix (n)
`(and ,(prule 's-indent n) (? s-separate-in-line)))
;; rule 67
(define-parameterized-rule s-line-prefix (n (c :block-out :block-in :flow-out :flow-in))
(if
(or (eql c :block-out)
(eql c :block-in))
(prule 's-block-line-prefix n)
(prule 's-flow-line-prefix n)))
;; rule 70
(define-parameterized-rule l-empty (n (c :block-out :block-in :flow-out :flow-in))
`(and
(or ,(prule 's-line-prefix n c)
,(prule 's-indent-< n))
b-as-line-feed)
(:constant #\Newline))
;; rule 71
(define-parameterized-rule b-l-trimmed (n (c :block-out :block-in :flow-out :flow-in))
`(and b-non-content
(+
,(prule 'l-empty n c)))
(:lambda (x)
(apply #'esrap:text (rest x))))
;; rule 72
(defrule b-as-space b-break (:constant #\Space))
;; rule 73
(define-parameterized-rule b-l-folded (n (c :block-out :block-in :flow-out :flow-in))
`(or ,(prule 'b-l-trimmed n c)
b-as-space)
(:text t))
(define-parameterized-rule s-flow-folded (n)
`(and (? s-separate-in-line)
,(prule 'b-l-folded n :flow-in)
,(prule 's-flow-line-prefix n))
(:function second))
(defrule c-nb-comment-text (and #\# (* nb-char)))
;; Not a part of YAML spec
(defrule eof (! character))
(defrule b-comment (or b-non-content eof))
;; rule 77
(defrule s-b-comment
(and
(?
(and
s-separate-in-line
(? c-nb-comment-text)))
b-comment))
;; rule 78
(defrule l-comment
(and s-separate-in-line
(? c-nb-comment-text)
b-comment))
;; l-comment consumes no characters at EOF
;; so we need to handle that situation for *
;; and + productions to not infinite loop
(defrule l-comment-*
(or
l-comment-+
(and)))
(defrule l-comment-+
(or
eof
(and
l-comment
l-comment-+)
l-comment))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun start-of-line (input position end)
(declare (ignore end))
(if (or (= position 0)
(char= (elt input (1- position)) #\Newline)
(char= (elt input (1- position)) #\Return))
(values "" position t)
(values nil position))))
(defrule start-of-line
(function start-of-line ))
;; rule 79
(defrule s-l-comments
(and
(or s-b-comment start-of-line)
l-comment-*))
;; rule 81
(define-parameterized-rule s-separate-lines (n)
`(or
(and s-l-comments ,(prule 's-flow-line-prefix n))
s-separate-in-line))
;; rule 80
(define-parameterized-rule s-separate (n (c :block-out :block-in :flow-out :flow-in
:block-key :flow-key))
(if (member c '(:block-out :block-in :flow-out :flow-in))
(prule 's-separate-lines n)
's-separate-in-line))
;; rule 82
(defrule l-directive
(and "%"
(or ns-yaml-directive
ns-tag-directive
ns-reserved-directive)
s-l-comments)
(:lambda (x)
`(directive ,(second x))))
;; rule 83
(defrule ns-reserved-directive
(and (!
(and
(or "TAG" "YAML")
s-white))
ns-directive-name
(* (and s-separate-in-line ns-directive-parameter)))
(:destructure (_ name args)
(declare (ignore _))
`(reserved ,(make-keyword (text name)) ,@(loop for (_ arg) in args collect (text arg)))))
;; rule 84
(defrule ns-directive-name (+ ns-char))
;; rule 85
(defrule ns-directive-parameter (+ ns-char))
;; rule 86
(defrule ns-yaml-directive
(and "YAML"
s-separate-in-line
ns-yaml-version)
(:destructure (_1 _2 version)
(declare (ignore _1 _2))
`(yaml ,(text version))))
;; rule 87
(defrule ns-yaml-version
(and
(+ ns-dec-digit)
#\.
(+ ns-dec-digit))
(:lambda (production)
(when (or (> (parse-integer (text (first production))) 1)
(> (parse-integer (text (third production))) 2))
(funcall *warning* "YAML version declared: ~A newer than 1.2"
(text production)))
(text production)))
;; rule 88
(defrule ns-tag-directive
(and
"TAG"
s-separate-in-line
c-tag-handle
s-separate-in-line
ns-tag-prefix)
(:destructure (_1 _2 handle _3 prefix)
(declare (ignore _1 _2 _3))
`(tag ,(text handle) ,(text prefix))))
;; rule 89
(defrule c-tag-handle
(or c-named-tag-handle c-secondary-tag-handle c-primary-tag-handle))
;; rule 90
(defrule c-primary-tag-handle #\!)
;;rule 91
(defrule c-secondary-tag-handle "!!")
;;rule 92
(defrule c-named-tag-handle
(and #\!
(+ ns-word-char)
#\!))
;; rule 93
(defrule ns-tag-prefix
(or c-ns-local-tag-prefix
ns-global-tag-prefix))
;; rule 94
(defrule c-ns-local-tag-prefix
(and
#\!
(* ns-uri-char)))
;; rule 95
(defrule ns-global-tag-prefix
(and
ns-tag-char
(* ns-uri-char)))
;; rule 96
(define-parameterized-rule c-ns-properties (n (c :block-out :block-in :flow-out :flow-in
:block-key :flow-key))
`(or
(and
c-ns-tag-property
(? (and ,(prule 's-separate n c) c-ns-anchor-property)))
(and
c-ns-anchor-property
(? (and ,(prule 's-separate n c) c-ns-tag-property))))
(:destructure (prop1 prop2)
;;(print prop2)
`(properties ,prop1 ,@(cdr prop2))))
;; rule 97
(defrule c-ns-tag-property
(or c-verbatim-tag
c-ns-shorthand-tag
c-non-specific-tag))
;; rule 98
;; TODO handle invalid tags
(defrule c-verbatim-tag
(and
"!<"
(+ ns-uri-char)
">")
(:lambda (x)
`(tag ,(text (second x)))))
;; rule 99
(defrule c-ns-shorthand-tag
(and c-tag-handle (+ ns-tag-char))
(:destructure (tag-handle tag-name)
`(tag ,(text tag-handle) ,(text tag-name))))
;;rule 100
(defrule c-non-specific-tag "!"
(:constant '(tag nonspecific)))
;;rule 101
(defrule c-ns-anchor-property
(and #\&
ns-anchor-name)
(:destructure (_ name)
(declare (ignore _))
`(anchor ,(text name))))
;; rule 102
(defrule ns-anchor-char
(and
(! c-flow-indicator)
ns-char))
;;rule 103
(defrule ns-anchor-name
(+ ns-anchor-char))
;; rule 104
(defrule c-ns-alias-node
(and #\*
ns-anchor-name)
(:destructure (star name)
(declare (ignore star))
`(alias ,(text name))))
;; rule 105
(defrule e-scalar
(and)
(:constant 'yaml-null))
(defrule e-node e-scalar)
;; rule 107
(defrule nb-double-char
(or c-ns-esc-char
(and (! (character-ranges #\\ #\"))
nb-json)))
;; rule 108
(defrule ns-double-char
(and (! s-white)
nb-double-char))
;;rule 114
(defrule nb-ns-double-in-line
(*
(and
(* s-white)
ns-double-char)))
;;rule 115
(define-parameterized-rule s-double-next-line (n)
`(and
,(prule 's-double-break n)
(?
(and
ns-double-char nb-ns-double-in-line
(or
,(prule 's-double-next-line n)
(* s-white))))))
;; rule 116
(define-parameterized-rule nb-double-multi-line (n)
`(and nb-ns-double-in-line
(or ,(prule 's-double-next-line n)
(* s-white))))
;; rule 110
(define-parameterized-rule nb-double-text (n (c :flow-out :flow-in
:block-key :flow-key))
(if (member c '(:flow-out :flow-in))
(prule 'nb-double-multi-line n)
'nb-double-one-line))
;; rule 109
(define-parameterized-rule c-double-quoted (n (c :flow-out :flow-in
:block-key :flow-key))
`(and #\"
,(prule 'nb-double-text n c)
#\")
(:lambda (stuff)
(destructuring-bind (sq meat eq) stuff
(declare (ignore sq eq))
`(dq-string ,(text meat)))))
;; rule 111
(defrule nb-double-one-line
(* nb-double-char))
;; rule 112
(define-parameterized-rule s-double-escaped (n)
`(and
(* s-white)
#\\
b-non-content
(* ,(prule 'l-empty n :flow-in))
,(prule 's-flow-line-prefix n))
(:lambda (x)
(car x)))
;; rule 113
(define-parameterized-rule s-double-break (n)
`(or
,(prule 's-double-escaped n)
,(prule 's-flow-folded n)))
;; rule 117
(defrule c-quoted-quote "''" (:constant #\'))
;;rule 118
(defrule nb-single-char
(or c-quoted-quote
(and
(! #\')
nb-json)))
;; rule 119
(defrule ns-single-char
(and
(! s-white)
nb-single-char))
;;rule 120
(define-parameterized-rule c-single-quoted (n (c :flow-out :flow-in :block-key :flow-key))
`(and
#\'
,(prule 'nb-single-text n c)
#\')
(:lambda (stuff)
(destructuring-bind (sq meat eq) stuff
(declare (ignore sq eq))
`(sq-string ,(text meat)))))
;;rule 121
(define-parameterized-rule nb-single-text (n (c :flow-out :flow-in :block-key :flow-key))
(if (member c '(:flow-out :flow-in))
(prule 'nb-single-multi-line n)
'nb-single-one-line))
;;rule 122
(defrule nb-single-one-line (* nb-single-char))
;;rule 123
(defrule nb-ns-single-in-line (* (and (* s-white) ns-single-char)))
;;rule 124
(define-parameterized-rule s-single-next-line (n)
`(and
,(prule 's-flow-folded n)
(?
(and
ns-single-char
nb-ns-single-in-line
(or ,(prule 's-single-next-line n)
(* s-white))))))
;;rule 125
(define-parameterized-rule nb-single-multi-line (n)
`(and nb-ns-single-in-line
(or ,(prule 's-single-next-line n)
(* s-white))))
(define-parameterized-rule ns-plain-safe-follows ((c :flow-out :flow-in :block-key :flow-key))
`(& ,(prule 'ns-plain-safe c))
(:constant nil))
;;rule 126
(define-parameterized-rule ns-plain-first ((c :flow-out :flow-in :block-key :flow-key))
`(or
(and
(! c-indicator)
ns-char)
(and
(character-ranges #\? #\: #\-)
,(prule 'ns-plain-safe-follows c))))
;;rule 127
(define-parameterized-rule ns-plain-safe ((c :flow-out :flow-in :block-key :flow-key))
(if (member c '(:flow-out :block-key))
'ns-plain-safe-out
'ns-plain-safe-in))
;;rule 128
(defrule ns-plain-safe-out ns-char)
;;rule 129
(defrule ns-plain-safe-in (and (! c-flow-indicator) ns-char))
(defun ns-char-preceding (input position end)
(declare (ignore end))
(if (and (/= position 0)
(multiple-value-bind (production new-position result)
(esrap:parse 'ns-char input :start (1- position) :junk-allowed t)
(declare (ignore production))
(or result
(and new-position (>= new-position position)))))
(values nil position t)
(values nil position)))
;;rule 130
(define-parameterized-rule ns-plain-char ((c :flow-out :flow-in :block-key :flow-key))
`(or
(and
(! (character-ranges #\: #\#))
,(prule 'ns-plain-safe c))
(and
(function ns-char-preceding)
#\#)
(and
#\:
,(prule 'ns-plain-safe-follows c))))
;;rule 131
(define-parameterized-rule ns-plain (n (c :flow-out :flow-in :block-key :flow-key))
(if (member c '(:flow-out :flow-in))
(prule 'ns-plain-multi-line n c)
(prule 'ns-plain-one-line c))
(:text t))
;;rule 132
(define-parameterized-rule nb-ns-plain-in-line ((c :flow-out :flow-in :block-key :flow-key))
`(*
(and (* s-white)
,(prule 'ns-plain-char c))))
;;rule 133
(define-parameterized-rule ns-plain-one-line ((c :flow-out :flow-in :block-key :flow-key))
`(and
,(prule 'ns-plain-first c)
,(prule 'nb-ns-plain-in-line c)))
;;rule 134
(define-parameterized-rule s-ns-plain-next-line (n (c :flow-out :flow-in :block-key :flow-key))
`(and
,(prule 's-flow-folded n)
,(prule 'ns-plain-char c)
,(prule 'nb-ns-plain-in-line c)))
;;rule 135
(define-parameterized-rule ns-plain-multi-line (n (c :flow-out :flow-in :block-key :flow-key))
`(and
,(prule 'ns-plain-one-line c)
(* ,(prule 's-ns-plain-next-line n c))))
;; rule 136
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun in-flow (c)
(if (member c '(:flow-out :flow-in))
:flow-in
:flow-key)))
;; rule 137
(define-parameterized-rule c-flow-sequence (n (c :flow-out :flow-in
:block-key :flow-key))
`(and
#\[
(? ,(prule 's-separate n c))
(? ,(prule 'ns-s-flow-seq-entries n (in-flow c)))
#\])
(:lambda (stuff)
(destructuring-bind (ob sep meat cb)
stuff
(declare (ignorable ob sep cb))
(cons 'seq meat))))
;; rule 138
(define-parameterized-rule ns-s-flow-seq-entries (n (c :flow-out :flow-in
:block-key :flow-key))
`(and
,(prule 'ns-flow-seq-entry n c)
(? ,(prule 's-separate n c))
(?
(and #\,
(? ,(prule 's-separate n c))
(? ,(prule 'ns-s-flow-seq-entries n c)))))
(:lambda (stuff)
(destructuring-bind
(first _ maybe-more) stuff
(declare (ignore _))
(cons first (third maybe-more)))))
;; rule 139
(define-parameterized-rule ns-flow-seq-entry (n (c :flow-out :flow-in
:block-key :flow-key))
`(or
,(prule 'ns-flow-pair n c)
,(prule 'ns-flow-node n c)))
;;rule 140
(define-parameterized-rule c-flow-mapping (n (c :flow-out :flow-in
:block-key :flow-key))
`(and
"{"
(? ,(prule 's-separate n c))
(? ,(prule 'ns-s-flow-map-entries n (in-flow c)))
"}")
(:lambda (stuff)
(destructuring-bind (ob sep meat cb) stuff
(declare (ignore ob sep cb))
(cons 'map meat))))
;;rule 141
(define-parameterized-rule ns-s-flow-map-entries (n (c :flow-out :flow-in
:block-key :flow-key))
`(and
,(prule 'ns-flow-map-entry n c)
(? ,(prule 's-separate n c))
(?
(and #\,
(? ,(prule 's-separate n c))
(? ,(prule 'ns-s-flow-map-entries n c)))))
(:lambda (stuff)
(destructuring-bind
(first _ maybe-more) stuff
(declare (ignorable _))
(cons first (third maybe-more)))))
;; rule 142
(define-parameterized-rule ns-flow-map-entry (n (c :flow-out :flow-in
:block-key :flow-key))
`(or
(and "?"
,(prule 's-separate n c)
,(prule 'ns-flow-map-explicit-entry n c))
,(prule 'ns-flow-map-implicit-entry n c)))
;;rule 143
(define-parameterized-rule ns-flow-map-explicit-entry (n (c :flow-out :flow-in
:block-key :flow-key))
`(or
,(prule 'ns-flow-map-implicit-entry n c)
(and e-node e-node)))
;; rule 144
(define-parameterized-rule ns-flow-map-implicit-entry (n (c :flow-out :flow-in
:block-key :flow-key))
`(or
,(prule 'ns-flow-map-yaml-key-entry n c)
,(prule 'c-ns-flow-map-empty-key-entry n c)
,(prule 'c-ns-flow-map-json-key-entry n c)))
;; rule 145
(define-parameterized-rule ns-flow-map-yaml-key-entry (n (c :flow-out :flow-in
:block-key :flow-key))
`(and
,(prule 'ns-flow-yaml-node n c)
(or
(and (? ,(prule 's-separate n c))
,(prule 'c-ns-flow-map-separate-value n c))
e-node))
(:lambda (stuff)
(destructuring-bind (key value)
stuff
`(entry ,key
,(if (eql value 'yaml-null)
value
(second value))))))
;;rule 146
(define-parameterized-rule c-ns-flow-map-empty-key-entry (n (c :flow-out :flow-in
:block-key :flow-key))
`(and e-node
,(prule 'c-ns-flow-map-separate-value n c))
(:lambda (x)