-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathyaml.el
2908 lines (2576 loc) · 104 KB
/
yaml.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
;;; yaml.el --- YAML parser for Elisp -*- lexical-binding: t -*-
;; Copyright © 2021-2024 Free Software Foundation, Inc.
;; Author: Zachary Romero <[email protected]>
;; Version: 1.1.0
;; Homepage: https://github.com/zkry/yaml.el
;; Package-Requires: ((emacs "25.1"))
;; Keywords: tools
;; yaml.el requires at least GNU Emacs 25.1
;; This file 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, 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.
;; For a full copy of the GNU General Public License
;; see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; yaml.el contains the code for parsing YAML natively in Elisp with
;; no dependencies. The main function to parse YAML provided is
;; `yaml-parse-string'. `yaml-encode' is also provided to encode a
;; Lisp object to YAML. The following are some examples of its usage:
;;
;; (yaml-parse-string "key1: value1\nkey2: value2")
;; (yaml-parse-string "key1: value1\nkey2: value2" :object-type 'alist)
;; (yaml-parse-string "numbers: [1, 2, 3]" :sequence-type 'list)
;;
;; (yaml-encode '((count . 3) (value . 10) (items ("ruby" "diamond"))))
;;; Code:
(require 'subr-x)
(require 'seq)
(require 'cl-lib)
(defconst yaml-parser-version "0.5.1")
(defvar yaml--parse-debug nil
"Turn on debugging messages when parsing YAML when non-nil.
This flag is intended for development purposes.")
(defconst yaml--tracing-ignore '("s-space"
"s-tab"
"s-white"
"l-comment"
"b-break"
"b-line-feed"
"b-carriage-return"
"s-b-comment"
"b-comment"
"l-comment"
"ns-char"
"nb-char"
"b-char"
"c-printable"
"b-as-space"))
(defvar yaml--parsing-input ""
"The string content of the current item being processed.")
(defvar yaml--parsing-position 0
"The position that the parser is currently looking at.")
(defvar yaml--states nil
"Stack of parsing states.")
(defvar yaml--parsing-object-type nil)
(defvar yaml--parsing-object-key-type nil)
(defvar yaml--parsing-sequence-type nil)
(defvar yaml--parsing-null-object nil)
(defvar yaml--parsing-false-object nil)
(defvar yaml--parsing-store-position nil)
(defvar yaml--string-values nil)
(cl-defstruct (yaml--state (:constructor yaml--state-create)
(:copier nil))
doc tt m name lvl beg end)
(defmacro yaml--parse (data &rest forms)
"Parse DATA according to FORMS."
(declare (indent defun))
`(progn (setq yaml--parsing-input ,data)
(setq yaml--parsing-position 0)
(yaml--initialize-state)
,@forms))
(defun yaml--state-curr ()
"Return the current state."
(or (car yaml--states)
(yaml--state-create
:name nil :doc nil :lvl 0 :beg 0 :end 0 :m nil :tt nil)))
(defun yaml--state-set-m (val)
"Set the current value of t to VAL."
(let* ((states yaml--states))
(while states
(let* ((top-state (car states))
(new-state (yaml--state-create :doc (yaml--state-doc top-state)
:tt (yaml--state-tt top-state)
:m val
:name (yaml--state-name top-state)
:lvl (yaml--state-lvl top-state)
:beg (yaml--state-beg top-state)
:end (yaml--state-end top-state))))
(setcar states new-state))
(setq states (cdr states)))))
(defun yaml--state-set-t (val)
"Set the current value of t to VAL."
(let* ((states yaml--states))
(while states
(let* ((top-state (car states))
(new-state (yaml--state-create :doc (yaml--state-doc top-state)
:tt val
:m (yaml--state-m top-state)
:name (yaml--state-name top-state)
:lvl (yaml--state-lvl top-state)
:beg (yaml--state-beg top-state)
:end (yaml--state-end top-state))))
(setcar states new-state))
(setq states (cdr states)))))
(defun yaml--state-curr-doc ()
"Return the doc property of current state."
(yaml--state-doc (yaml--state-curr)))
(defun yaml--state-curr-t ()
"Return the doc property of current state."
(yaml--state-tt (yaml--state-curr)))
(defun yaml--state-curr-m ()
"Return the doc property of current state."
(or (yaml--state-m (yaml--state-curr)) 1))
(defun yaml--state-curr-end ()
"Return the doc property of current state."
(yaml--state-end (yaml--state-curr)))
(defun yaml--push-state (name)
"Add a new state frame with NAME."
(let* ((curr-state (yaml--state-curr))
(new-state (yaml--state-create
:doc (yaml--state-curr-doc)
:tt (yaml--state-curr-t)
:m (yaml--state-curr-m)
:name name
:lvl (1+ (yaml--state-lvl curr-state))
:beg yaml--parsing-position
:end nil)))
(push new-state yaml--states)))
(defun yaml--pop-state ()
"Pop the current state."
(let ((popped-state (car yaml--states)))
(setq yaml--states (cdr yaml--states))
(let ((top-state (car yaml--states)))
(when top-state
(setcar yaml--states
(yaml--state-create :doc (yaml--state-doc top-state)
:tt (yaml--state-tt top-state)
:m (yaml--state-m top-state)
:name (yaml--state-name top-state)
:lvl (yaml--state-lvl top-state)
:beg (yaml--state-beg popped-state)
:end yaml--parsing-position))))))
(defun yaml--initialize-state ()
"Initialize the yaml state for parsing."
(setq yaml--states
(list (yaml--state-create :doc nil
:tt nil
:m nil
:name nil
:lvl 0
:beg nil
:end nil))))
(defconst yaml--grammar-resolution-rules
'(("ns-plain" . literal))
"Alist determining how to resolve grammar rule.")
;;; Receiver Functions
(defvar yaml--document-start-version nil)
(defvar yaml--document-start-explicit nil)
(defvar yaml--document-end-explicit nil)
(defvar yaml--tag-map nil)
(defvar yaml--tag-handle nil)
(defvar yaml--document-end nil)
(defvar yaml--cache nil
"Stack of data for temporary calculations.")
(defvar yaml--object-stack nil
"Stack of objects currently being build.")
(defvar yaml--state-stack nil
"The state that the YAML parser is with regards to incoming events.")
(defvar yaml--root nil)
(defvar yaml--anchor-mappings nil
"Hashmap containing the anchor mappings of the current parsing run.")
(defvar yaml--resolve-aliases nil
"Flag determining if the event processing should attempt to resolve aliases.")
(defun yaml--parse-block-header (header)
"Parse the HEADER string returning chomping style and indent count."
(let* ((pos 0)
(chomp-indicator :clip)
(indentation-indicator nil)
(char (and (< pos (length header)) (aref header pos)))
(process-char (lambda (char)
(when char
(cond
((< ?0 char ?9)
(progn (setq indentation-indicator (- char ?0))))
((equal char ?\-) (setq chomp-indicator :strip))
((equal char ?\+) (setq chomp-indicator :keep)))
(setq pos (1+ pos))))))
(when (or (eq char ?\|) (eq char ?\>))
(setq pos (1+ pos))
(setq char (and (< pos (length header)) (aref header pos))))
(funcall process-char char)
(let ((char (and (< pos (length header)) (aref header pos)))) ;
(funcall process-char char)
(list chomp-indicator indentation-indicator))))
(defun yaml--chomp-text (text-body chomp)
"Change the ending newline of TEXT-BODY based on CHOMP."
(cond ((eq :clip chomp)
(concat (replace-regexp-in-string "\n*\\'" "" text-body) "\n"))
((eq :strip chomp)
(replace-regexp-in-string "\n*\\'" "" text-body))
((eq :keep chomp)
text-body)))
(defun yaml--process-folded-text (text)
"Remove the header line for a folded match and return TEXT body formatted."
(let* ((text (yaml--process-literal-text text))
(done))
(while (not done)
(let ((replaced (replace-regexp-in-string "\\([^\n]\\)\n\\([^\n ]\\)"
"\\1 \\2"
text)))
(when (equal replaced text)
(setq done t))
(setq text replaced)))
(replace-regexp-in-string
"\\(\\(?:^\\|\n\\)[^ \n][^\n]*\\)\n\\(\n+\\)\\([^\n ]\\)" "\\1\\2\\3"
text)))
(defun yaml--process-literal-text (text)
"Remove the header line for a folded match and return TEXT body formatted."
(let ((n (get-text-property 0 'yaml-n text)))
(remove-text-properties 0 (length text) '(yaml-n nil) text)
(let* ((header-line (substring text 0 (string-match "\n" text)))
(text-body (substring text (1+ (string-match "\n" text))))
(parsed-header (yaml--parse-block-header header-line))
(chomp (car parsed-header))
(starting-spaces-ct
(or (and (cadr parsed-header) (+ (or n 0) (cadr parsed-header)))
(let ((_ (string-match "^\n*\\( *\\)" text-body)))
(length (match-string 1 text-body)))))
(lines (split-string text-body "\n"))
(striped-lines
(seq-map (lambda (l)
(replace-regexp-in-string
(format "\\` \\{0,%d\\}" starting-spaces-ct) "" l))
lines))
(text-body (string-join striped-lines "\n")))
(yaml--chomp-text text-body chomp))))
;; TODO: Process tags and use them in this function.
(defun yaml--resolve-scalar-tag (scalar)
"Convert a SCALAR string to it's corresponding object."
(cond
(yaml--string-values
scalar)
;; tag:yaml.org,2002:null
((or (equal "null" scalar)
(equal "Null" scalar)
(equal "NULL" scalar)
(equal "~" scalar))
yaml--parsing-null-object)
;; tag:yaml.org,2002:bool
((or (equal "true" scalar)
(equal "True" scalar)
(equal "TRUE" scalar)) t)
((or (equal "false" scalar)
(equal "False" scalar)
(equal "FALSE" scalar))
yaml--parsing-false-object)
;; tag:yaml.org,2002:int
((string-match "^0$\\|^-?[1-9][0-9]*$" scalar)
(string-to-number scalar))
((string-match "^[-+]?[0-9]+$" scalar)
(string-to-number scalar))
((string-match "^0o[0-7]+$" scalar)
(string-to-number (substring scalar 2) 8))
((string-match "^0x[0-9a-fA-F]+$" scalar)
(string-to-number (substring scalar 2) 16))
;; tag:yaml.org,2002:float
((string-match
"^[-+]?\\(\\.[0-9]+\\|[0-9]+\\(\\.[0-9]*\\)?\\)\\([eE][-+]?[0-9]+\\)?$"
scalar)
(string-to-number scalar 10))
((string-match "^[-+]?\\(\\.inf\\|\\.Inf\\|\\.INF\\)$" scalar)
1.0e+INF)
((string-match "^[-+]?\\(\\.nan\\|\\.NaN\\|\\.NAN\\)$" scalar)
1.0e+INF)
((string-match "^0$\\|^-?[1-9]\\(\\.[0-9]*\\)?\\(e[-+][1-9][0-9]*\\)?$"
scalar)
(string-to-number scalar))
(t scalar)))
(defun yaml--hash-table-to-alist (hash-table)
"Convert HASH-TABLE to a alist."
(let ((alist nil))
(maphash
(lambda (k v)
(setq alist (cons (cons k v) alist)))
hash-table)
alist))
(defun yaml--hash-table-to-plist (hash-table)
"Convert HASH-TABLE to a plist."
(let ((plist nil))
(maphash
(lambda (k v)
(setq plist (cons k (cons v plist))))
hash-table)
plist))
(defun yaml--format-object (hash-table)
"Convert HASH-TABLE to alist of plist if specified."
(cond
((equal yaml--parsing-object-type 'hash-table)
hash-table)
((equal yaml--parsing-object-type 'alist)
(yaml--hash-table-to-alist hash-table))
((equal yaml--parsing-object-type 'plist)
(yaml--hash-table-to-plist hash-table))
(t hash-table)))
(defun yaml--format-list (l)
"Convert L to array if specified."
(cond
((equal yaml--parsing-sequence-type 'list)
l)
((equal yaml--parsing-sequence-type 'array)
(apply #'vector l))
(t l)))
(defun yaml--stream-start-event ()
"Create the data for a stream-start event."
'(:stream-start))
(defun yaml--stream-end-event ()
"Create the data for a stream-end event."
'(:stream-end))
(defun yaml--mapping-start-event (_)
"Process event indicating start of mapping."
;; NOTE: currently don't have a use for FLOW
(push :mapping yaml--state-stack)
(push (make-hash-table :test 'equal) yaml--object-stack))
(defun yaml--mapping-end-event ()
"Process event indicating end of mapping."
(pop yaml--state-stack)
(let ((obj (pop yaml--object-stack)))
(yaml--scalar-event nil obj))
'(:mapping-end))
(defun yaml--sequence-start-event (_)
"Process event indicating start of sequence according to FLOW."
;; NOTE: currently don't have a use for FLOW
(push :sequence yaml--state-stack)
(push nil yaml--object-stack)
'(:sequence-start))
(defun yaml--sequence-end-event ()
"Process event indicating end of sequence."
(pop yaml--state-stack)
(let ((obj (pop yaml--object-stack)))
(yaml--scalar-event nil obj))
'(:sequence-end))
(defun yaml--anchor-event (name)
"Process event indicating an anchor has been defined with NAME."
(push :anchor yaml--state-stack)
(push `(:anchor ,name) yaml--object-stack))
(defun yaml--scalar-event (style value)
"Process the completion of a scalar VALUE.
Note that VALUE may be a complex object here. STYLE is
currently unused."
(let ((top-state (car yaml--state-stack))
(value* (cond
((stringp value) (yaml--resolve-scalar-tag value))
((listp value) (yaml--format-list value))
((hash-table-p value) (yaml--format-object value))
((vectorp value) value)
((not value) nil))))
(cond
((not top-state)
(setq yaml--root value*))
((equal top-state :anchor)
(let* ((anchor (pop yaml--object-stack))
(name (nth 1 anchor)))
(puthash name value yaml--anchor-mappings)
(pop yaml--state-stack)
(yaml--scalar-event nil value)))
((equal top-state :sequence)
(let ((l (car yaml--object-stack)))
(setcar yaml--object-stack (append l (list value*)))))
((equal top-state :mapping)
(progn
(push :mapping-value yaml--state-stack)
(push value* yaml--cache)))
((equal top-state :mapping-value)
(progn
(let ((key (pop yaml--cache))
(table (car yaml--object-stack)))
(when (stringp key)
(cond
((eql 'symbol yaml--parsing-object-key-type)
(setq key (intern key)))
((eql 'keyword yaml--parsing-object-key-type)
(setq key (intern (format ":%s" key))))))
(puthash key value* table))
(pop yaml--state-stack)))
((equal top-state :trail-comments)
(pop yaml--state-stack)
(let ((comment-text (pop yaml--object-stack)))
(unless (stringp value*)
(error "Trail-comments can't be nested under non-string"))
(yaml--scalar-event
style
(replace-regexp-in-string (concat (regexp-quote comment-text) "\n*\\'")
""
value*))))
((equal top-state nil))))
'(:scalar))
(defun yaml--alias-event (name)
"Process a node has been defined via alias NAME."
(if yaml--resolve-aliases
(let ((resolved (gethash name yaml--anchor-mappings)))
(unless resolved (error "Undefined alias '%s'" name))
(yaml--scalar-event nil resolved))
(yaml--scalar-event nil (vector :alias name)))
'(:alias))
(defun yaml--trail-comments-event (text)
"Process trailing comments of TEXT which should be trimmed from parent."
(push :trail-comments yaml--state-stack)
(push text yaml--object-stack)
'(:trail-comments))
(defun yaml--check-document-end ()
"Return non-nil if at end of document."
;; NOTE: currently no need for this. May be needed in the future.
t)
(defun yaml--reverse-at-list ()
"Reverse the list at the top of the object stack.
This is needed to get the correct order as lists are processed in
reverse order."
(setcar yaml--object-stack (reverse (car yaml--object-stack))))
(defconst yaml--grammar-events-in
'(("l-yaml-stream" . (lambda ()
(yaml--stream-start-event)
(setq yaml--document-start-version nil)
(setq yaml--document-start-explicit nil)
(setq yaml--tag-map (make-hash-table))))
("c-flow-mapping" . (lambda ()
(yaml--mapping-start-event t)))
("c-flow-sequence" . (lambda ()
(yaml--sequence-start-event nil)))
("l+block-mapping" . (lambda ()
(yaml--mapping-start-event nil)))
("l+block-sequence" . (lambda ()
(yaml--sequence-start-event nil)))
("ns-l-compact-mapping" . (lambda ()
(yaml--mapping-start-event nil)))
("ns-l-compact-sequence" . (lambda ()
(yaml--sequence-start-event nil)))
("ns-flow-pair" . (lambda ()
(yaml--mapping-start-event t)))
("ns-l-block-map-implicit-entry" . (lambda ()))
("ns-l-compact-mapping" . (lambda ()))
("c-l-block-seq-entry" . (lambda ())))
"List of functions for matched rules that run on the entering of a rule.")
(defconst yaml--grammar-events-out
'(("c-b-block-header" .
(lambda (text)
nil))
("l-yaml-stream" .
(lambda (text)
(yaml--check-document-end)
(yaml--stream-end-event)))
("ns-yaml-version" .
(lambda (text)
(when yaml--document-start-version
(throw 'error "Multiple %YAML directives not allowed."))
(setq yaml--document-start-version text)))
("c-tag-handle" .
(lambda (text)
(setq yaml--tag-handle text)))
("ns-tag-prefix" .
(lambda (text)
(puthash yaml--tag-handle text yaml--tag-map)))
("c-directives-end" .
(lambda (text)
(yaml--check-document-end)
(setq yaml--document-start-explicit t)))
("c-document-end" .
(lambda (text)
(when (not yaml--document-end)
(setq yaml--document-end-explicit t))
(yaml--check-document-end)))
("c-flow-mapping" .
(lambda (text)
(yaml--mapping-end-event)))
("c-flow-sequence" .
(lambda (text)
(yaml--sequence-end-event )))
("l+block-mapping" .
(lambda (text)
(yaml--mapping-end-event)))
("l+block-sequence" .
(lambda (text)
(yaml--reverse-at-list)
(yaml--sequence-end-event)))
("ns-l-compact-mapping" .
(lambda (text)
(yaml--mapping-end-event)))
("ns-l-compact-sequence" .
(lambda (text)
(yaml--sequence-end-event)))
("ns-flow-pair" .
(lambda (text)
(yaml--mapping-end-event)))
("ns-plain" .
(lambda (text)
(let* ((replaced (if (and (zerop (length yaml--state-stack))
(string-match "\\(^\\|\n\\)\\.\\.\\.\\'" text))
;; Hack to not send the document parse end.
;; Will only occur with bare ns-plain at top level.
(replace-regexp-in-string "\\(^\\|\n\\)\\.\\.\\.\\'"
""
text)
text))
(replaced (replace-regexp-in-string
"\\(?:[ \t]*\r?\n[ \t]*\\)"
"\n"
replaced))
(replaced (replace-regexp-in-string
"\\(\n\\)\\(\n*\\)"
(lambda (x)
(if (> (length x) 1)
(substring x 1)
" "))
replaced)))
(yaml--scalar-event "plain" replaced))))
("c-single-quoted" .
(lambda (text)
(let* ((replaced (replace-regexp-in-string
"\\(?:[ \t]*\r?\n[ \t]*\\)"
"\n"
text))
(replaced (replace-regexp-in-string
"\\(\n\\)\\(\n*\\)"
(lambda (x)
(if (> (length x) 1)
(substring x 1)
" "))
replaced))
(replaced (if (not (equal "''" replaced))
(replace-regexp-in-string
"''"
(lambda (x)
(if (> (length x) 1)
(substring x 1)
"'"))
replaced)
replaced)))
(yaml--scalar-event "single"
(substring replaced 1 (1- (length replaced)))))))
("c-double-quoted" .
(lambda (text)
(let* ((replaced (replace-regexp-in-string
"\\(?:[ \t]*\r?\n[ \t]*\\)"
"\n"
text))
(replaced (replace-regexp-in-string
"\\(\n\\)\\(\n*\\)"
(lambda (x)
(if (> (length x) 1)
(substring x 1)
" "))
replaced))
(replaced (replace-regexp-in-string "\\\\\\([\"\\/]\\)"
"\\1"
replaced))
(replaced (replace-regexp-in-string "\\\\ " " " replaced))
(replaced (replace-regexp-in-string "\\\\ " " " replaced))
(replaced (replace-regexp-in-string "\\\\b" "\b" replaced))
(replaced (replace-regexp-in-string "\\\\t" "\t" replaced))
(replaced (replace-regexp-in-string "\\\\n" "\n" replaced))
(replaced (replace-regexp-in-string "\\\\r" "\r" replaced))
(replaced (replace-regexp-in-string "\\\\r" "\r" replaced))
(replaced (replace-regexp-in-string
"\\\\x\\([0-9a-fA-F]\\{2\\}\\)"
(lambda (x)
(let ((char-pt (substring 2 x)))
(string (string-to-number char-pt 16))))
replaced))
(replaced (replace-regexp-in-string
"\\\\x\\([0-9a-fA-F]\\{2\\}\\)"
(lambda (x)
(let ((char-pt (substring x 2)))
(string (string-to-number char-pt 16))))
replaced))
(replaced (replace-regexp-in-string
"\\\\x\\([0-9a-fA-F]\\{4\\}\\)"
(lambda (x)
(let ((char-pt (substring x 2)))
(string (string-to-number char-pt 16))))
replaced))
(replaced (replace-regexp-in-string
"\\\\x\\([0-9a-fA-F]\\{8\\}\\)"
(lambda (x)
(let ((char-pt (substring x 2)))
(string (string-to-number char-pt 16))))
replaced))
(replaced (replace-regexp-in-string
"\\\\\\\\"
"\\"
replaced))
(replaced (substring replaced 1 (1- (length replaced)))))
(yaml--scalar-event "double" replaced))))
("c-l+literal" .
(lambda (text)
(when (equal (car yaml--state-stack) :trail-comments)
(pop yaml--state-stack)
(let ((comment-text (pop yaml--object-stack)))
(setq text (replace-regexp-in-string
(concat (regexp-quote comment-text) "\n*\\'") "" text))))
(let* ((processed-text (yaml--process-literal-text text)))
(yaml--scalar-event "folded" processed-text))))
("c-l+folded" .
(lambda (text)
(when (equal (car yaml--state-stack) :trail-comments)
(pop yaml--state-stack)
(let ((comment-text (pop yaml--object-stack)))
(setq text (replace-regexp-in-string
(concat (regexp-quote comment-text) "\n*\\'") "" text))))
(let* ((processed-text (yaml--process-folded-text text)))
(yaml--scalar-event "folded" processed-text))))
("e-scalar" .
(lambda (text)
(yaml--scalar-event "plain" "null")))
("c-ns-anchor-property" .
(lambda (text)
(yaml--anchor-event (substring text 1))))
("c-ns-tag-property" .
(lambda (text)
;; TODO: Implement tags
))
("l-trail-comments" .
(lambda (text)
(yaml--trail-comments-event text)))
("c-ns-alias-node" .
(lambda (text)
(yaml--alias-event (substring text 1)))))
"List of functions for matched rules that run on the exiting of a rule.")
(defconst yaml--terminal-rules
'( "l-nb-literal-text"
"l-nb-diff-lines"
"ns-plain"
"c-single-quoted"
"c-double-quoted")
"List of rules that indicate at which the parse tree should stop.
This addition is a hack to prevent the parse tree from going too deep and thus
risk hitting the stack depth limit. Each of these rules are recursive and
repeat for each character in a text.")
(defun yaml--walk-events (tree)
"Event walker iterates over the parse TREE and signals events from the rules."
(when (consp tree)
(if (stringp (car tree))
(let ((grammar-rule (car tree))
(text (cadr tree))
(children (cl-caddr tree)))
(let ((in-fn (cdr (assoc grammar-rule yaml--grammar-events-in)))
(out-fn (cdr (assoc grammar-rule yaml--grammar-events-out))))
(when in-fn
(funcall in-fn))
(yaml--walk-events children)
(when out-fn
(funcall out-fn text))))
(yaml--walk-events (car tree))
(yaml--walk-events (cdr tree)))))
(defun yaml--end-of-stream ()
"Return non-nil if the current position is after the end of the document."
(>= yaml--parsing-position (length yaml--parsing-input)))
(defun yaml--char-at-pos (pos)
"Return the character at POS."
(aref yaml--parsing-input pos))
(defun yaml--slice (pos)
"Return the character at POS."
(substring yaml--parsing-input pos))
(defun yaml--at-char ()
"Return the current character."
(yaml--char-at-pos yaml--parsing-position))
(defun yaml--char-match (at &rest chars)
"Return non-nil if AT match any of CHARS."
(if (not chars)
nil
(or (equal at (car chars))
(apply #'yaml--char-match (cons at (cdr chars))))))
(defun yaml--chr (c)
"Try to match the character C."
(if (or (yaml--end-of-stream) (not (equal (yaml--at-char) c)))
nil
(setq yaml--parsing-position (1+ yaml--parsing-position))
t))
(defun yaml--chr-range (min max)
"Return non-nil if the current character is between MIN and MAX."
(if (or (yaml--end-of-stream) (not (<= min (yaml--at-char) max)))
nil
(setq yaml--parsing-position (1+ yaml--parsing-position))
t))
(defun yaml--run-all (&rest funcs)
"Return list of all evaluated FUNCS if all of FUNCS pass."
(let* ((start-pos yaml--parsing-position)
(ress '())
(break))
(while (and (not break) funcs)
(let ((res (funcall (car funcs))))
(when (not res)
(setq break t))
(setq ress (append ress (list res)))
(setq funcs (cdr funcs))))
(when break
(setq yaml--parsing-position start-pos))
(if break nil ress)))
(defmacro yaml--all (&rest forms)
"Pass and return all forms if all of FORMS pass."
`(yaml--run-all
,@(mapcar (lambda (form)
`(lambda () ,form))
forms)))
(defmacro yaml--any (&rest forms)
"Pass if any of FORMS pass."
(if (= 1 (length forms))
(car forms)
(let ((start-pos-sym (make-symbol "start"))
(rules-sym (make-symbol "rules"))
(res-sym (make-symbol "res")))
`(let ((,start-pos-sym yaml--parsing-position)
(,rules-sym ,(cons 'list
(seq-map (lambda (form) `(lambda () ,form))
forms)))
(,res-sym))
(while (and (not ,res-sym) ,rules-sym)
(setq ,res-sym (funcall (car ,rules-sym)))
(unless ,res-sym
(setq yaml--parsing-position ,start-pos-sym))
(setq ,rules-sym (cdr ,rules-sym)))
,res-sym))))
(defmacro yaml--exclude (_)
"Set the excluded characters according to RULE.
This is currently unimplemented."
;; NOTE: This is currently not implemented.
't)
(defmacro yaml--max (_)
"Automatically pass."
t)
(defun yaml--empty ()
"Return non-nil indicating that empty rule needs nothing to pass."
't)
(defun yaml--sub (a b)
"Return A minus B."
(- a b))
(defun yaml--match ()
"Return the content of the previous sibling completed."
(let* ((states yaml--states)
(res nil))
(while (and states (not res))
(let ((top-state (car states)))
(if (yaml--state-end top-state)
(let ((beg (yaml--state-beg top-state))
(end (yaml--state-end top-state)))
(setq res (substring yaml--parsing-input beg end)))
(setq states (cdr states)))))
res))
(defun yaml--auto-detect (n)
"Detect the indentation given N."
(let* ((slice (yaml--slice yaml--parsing-position))
(match (string-match
"^.*\n\\(\\(?: *\n\\)*\\)\\( *\\)"
slice)))
(if (not match)
1
(let ((pre (match-string 1 slice))
(m (- (length (match-string 2 slice)) n)))
(if (< m 1)
1
(when (string-match (format "^.\\{%d\\}." m) pre)
(error "Spaces found after indent in auto-detect (5LLU)"))
m)))))
(defun yaml--auto-detect-indent (n)
"Detect the indentation given N."
(let* ((pos yaml--parsing-position)
(in-seq (and
(> pos 0)
(yaml--char-match (yaml--char-at-pos (1- pos)) ?\- ?\? ?\:)))
(slice (yaml--slice pos))
(_ (string-match
"^\\(\\(?: *\\(?:#.*\\)?\n\\)*\\)\\( *\\)"
slice))
(pre (match-string 1 slice))
(m (length (match-string 2 slice))))
(if (and in-seq (= (length pre) 0))
(when (= n -1)
(setq m (1+ m)))
(setq m (- m n)))
(when (< m 0)
(setq m 0))
m))
(defun yaml--the-end ()
"Return non-nil if at the end of input (?)."
(or (>= yaml--parsing-position (length yaml--parsing-input))
(and (yaml--state-curr-doc)
(yaml--start-of-line)
(string-match
"\\^g\\(?:---|\\.\\.\\.\\)\\([[:blank:]]\\|$\\)"
(substring yaml--parsing-input yaml--parsing-position)))))
(defun yaml--ord (f)
"Convert an ASCII number returned by F to a number."
(let ((res (funcall f)))
(- (aref res 0) 48)))
(defun yaml--but (&rest fs)
"Match the first FS but none of the others."
(if (yaml--the-end)
nil
(let ((pos1 yaml--parsing-position))
(if (not (funcall (car fs)))
nil
(let ((pos2 yaml--parsing-position))
(setq yaml--parsing-position pos1)
(if (equal ':error (catch 'break
(dolist (f (cdr fs))
(if (funcall f)
(progn
(setq yaml--parsing-position pos1)
(throw 'break ':error))))))
nil
(setq yaml--parsing-position pos2)
t))))))
(defmacro yaml--rep (min max func)
"Repeat FUNC between MIN and MAX times."
(declare (indent 2))
`(yaml--rep2 ,min ,max ,func))
(defun yaml--rep2 (min max func)
"Repeat FUNC between MIN and MAX times."
(declare (indent 2))
(if (and max (< max 0))
nil
(let* ((res-list '())
(count 0)
(pos yaml--parsing-position)
(pos-start pos)
(break nil))
(while (and (not break) (or (not max) (< count max)))
(let ((res (funcall func)))
(if (or (not res) (= yaml--parsing-position pos))
(setq break t)
(setq res-list (cons res res-list))
(setq count (1+ count))
(setq pos yaml--parsing-position))))
(if (and (>= count min)
(or (not max) (<= count max)))
(progn
(setq yaml--parsing-position pos)
(if (zerop count)
t
res-list))
(setq yaml--parsing-position pos-start)
nil))))
(defun yaml--start-of-line ()
"Return non-nil if start of line."
(or (= yaml--parsing-position 0)
(>= yaml--parsing-position (length yaml--parsing-input))
(equal (yaml--char-at-pos (1- yaml--parsing-position)) ?\n)))
(defun yaml--top ()
"Perform top level YAML parsing rule."
(yaml--parse-from-grammar 'l-yaml-stream))
(defmacro yaml--set (variable value)
"Set the current state of VARIABLE to VALUE."
(let ((res-sym (make-symbol "res")))
`(let ((,res-sym ,value))
(when ,res-sym
(,(cond ((equal "m" (symbol-name variable)) 'yaml--state-set-m)
((equal "t" (symbol-name variable)) 'yaml--state-set-t))
,res-sym)
,res-sym))))
(defmacro yaml--chk (type expr)
"Check if EXPR is non-nil at the parsing position.
If TYPE is \"<=\" then check at the previous position. If TYPE
is \"!\" ensure that EXPR is nil. Otherwise, if TYPE is \"=\"
then check EXPR at the current position."
(let ((start-symbol (make-symbol "start"))
(ok-symbol (make-symbol "ok")))
`(let ((,start-symbol yaml--parsing-position)
(_ (when (equal ,type "<=")
(setq yaml--parsing-position (1- yaml--parsing-position))))
(,ok-symbol (and (>= yaml--parsing-position 0) ,expr)))
(setq yaml--parsing-position ,start-symbol)
(if (equal ,type "!")
(not ,ok-symbol)
,ok-symbol))))
(cl-defun yaml--initialize-parsing-state
(&key (null-object :null)
(false-object :false)
object-type
object-key-type
sequence-type
string-values)
"Initialize state required for parsing according to plist ARGS."
(setq yaml--cache nil)
(setq yaml--object-stack nil)
(setq yaml--state-stack nil)
(setq yaml--root nil)
(setq yaml--anchor-mappings (make-hash-table :test 'equal))
(setq yaml--resolve-aliases nil)
(setq yaml--parsing-null-object null-object)
(setq yaml--parsing-false-object false-object)
(cond
((or (not object-type)
(equal object-type 'hash-table))
(setq yaml--parsing-object-type 'hash-table))
((equal 'alist object-type)
(setq yaml--parsing-object-type 'alist))
((equal 'plist object-type)
(setq yaml--parsing-object-type 'plist))
(t (error "Invalid object-type. Must be hash-table, alist, or plist")))
(cond
((or (not object-key-type)
(equal 'symbol object-key-type))
(if (equal 'plist yaml--parsing-object-type)
(setq yaml--parsing-object-key-type 'keyword)
(setq yaml--parsing-object-key-type 'symbol)))