-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaml.tcl
1290 lines (1144 loc) · 36.6 KB
/
yaml.tcl
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 parser for Tcl.
#
# See http://www.yaml.org/spec/1.1/
#
# yaml.tcl,v 0.3.6 2011-08-23 15:06:25 KATO Kanryu(kanryu6@users.sourceforge.net)
#
# It is published with the terms of tcllib's BSD-style license.
# See the file named license.terms.
#
# It currently supports a very limited subsection of the YAML spec.
#
#
package require Tcl 8.5
package provide yaml 0.3.8
package require cmdline
package require huddle 0.1.7
namespace eval ::yaml {
namespace export load setOptions dict2dump list2dump
variable data
array set data {}
# fixed value groups for some yaml-types.
variable fixed
# a plane scalar is worked for matching and converting to the specific type.
# proc some_command {value} {
# return [list !!type $treatmented-value]
# or
# return ""
# }
variable parsers
# scalar/collection treatment for matched specific yaml-tag
# proc some_composer {type value} {
# return [list 1 $result-type $treatmented-value]
# or
# return ""
# }
variable composer
variable defaults
array set defaults {
isfile 0
validate 0
types {timestamp int float null true false}
composer {
!!binary ::yaml::_composeBinary
}
parsers {
timestamp ::yaml::_parseTimestamp
}
shorthands {
!! {tag:yaml.org,2002:}
}
fixed {
null:Value ""
null:Group {null "" ~}
true:Value 1
true:Group {true on + yes y}
false:Value 0
false:Group {false off - no n}
}
}
variable _dumpIndent 2
variable _dumpWordWrap 40
variable opts [lrange [::cmdline::GetOptionDefaults {
{file {input is filename}}
{stream {input is stream}}
{m.arg "" {fixed-modifiers bulk settings(null/true/false)}}
{m:null.arg "" {null modifier settings(default {"" {null "" ~}})}}
{m:true.arg "" {true modifier settings(default {1 {true on + yes y}})}}
{m:false.arg "" {false modifier settings(default {0 {false off - no n}})}}
{types.arg "" {modifier list settings(default {nop timestamp integer null true false})}}
{validate {to validate the input(not dumped tcl content)}}
} result] 2 end] ;# Remove ? and help.
variable errors
array set errors {
TAB_IN_PLAIN {Tabs can be used only in comments, and in quoted "..." '...'.}
AT_IN_PLAIN {Reserved indicators {@} can't start a plain scalar.}
BT_IN_PLAIN {Reserved indicators {`} can't start a plain scalar.}
SEQEND_NOT_IN_SEQ {There is a flow-sequence end '\]' not in flow-sequence [v, ...].}
MAPEND_NOT_IN_MAP {There is a flow-mapping end '\}' not in flow-mapping {k: v, ...}.}
ANCHOR_NOT_FOUND {Could not find the anchor-name(current-version, "after refering" is not supported)}
MALFORM_D_QUOTE {Double quote "..." parsing error. end of quote is missing?}
MALFORM_S_QUOTE {Single quote '...' parsing error. end of quote is missing?}
TAG_NOT_FOUND {The "$p1" handle wasn't declared.}
INVALID_MERGE_KEY {merge-key "<<" is not impremented in not mapping scope(e.g. in sequence).}
MALFORMED_MERGE_KEY {malformed merge-key "<<" using.}
}
}
####################
# Public APIs
####################
proc ::yaml::yaml2dict {args} {
_getOption $args
set result [_parseBlockNode]
set a [huddle get_stripped $result]
if {$yaml::data(validate)} {
set result [string map "{\n} {\\n}" $result]
}
return [huddle get_stripped $result]
}
proc ::yaml::yaml2huddle {args} {
_getOption $args
set result [_parseBlockNode]
if {$yaml::data(validate)} {
set result [string map "{\n} {\\n}" $result]
}
return $result
}
proc ::yaml::setOptions {argv} {
variable defaults
array set options [_imp_getOptions argv]
array set defaults [array get options]
}
# Dump TCL List to YAML
#
proc ::yaml::list2yaml {list {indent 2} {wordwrap 40}} {
return [huddle2yaml [huddle list {*}$list] $indent $wordwrap]
}
proc ::yaml::dict2yaml {dict {indent 2} {wordwrap 40}} {
return [huddle2yaml [huddle create {*}$dict] $indent $wordwrap]
}
proc ::yaml::huddle2yaml {huddle {indent 2} {wordwrap 40}} {
set yaml::_dumpIndent $indent
set yaml::_dumpWordWrap $wordwrap
# Start at the base of the array and move through it.
set out [join [list "---\n" [_imp_huddle2yaml $huddle] "\n"] ""]
return $out
}
####################
# Option settings
####################
proc ::yaml::_getOption {argv} {
variable data
variable parsers
variable fixed
variable composer
# default settings
array set options [_imp_getOptions argv]
array set fixed $options(fixed)
array set parsers $options(parsers)
array set composer $options(composer)
array set data [list validate $options(validate) types $options(types)]
set isfile $options(isfile)
foreach {buffer} $argv break
if {$isfile} {
set fd [open $buffer r]
set buffer [read $fd]
close $fd
}
set data(buffer) $buffer
set data(start) 0
set data(length) [string length $buffer]
set data(current) 0
set data(finished) 0
}
proc ::yaml::_imp_getOptions {{argvvar argv}} {
upvar 1 $argvvar argv
variable defaults
variable opts
array set options [array get defaults]
# default settings
array set fixed $options(fixed)
# parse argv
set argc [llength $argv]
while {[set err [::cmdline::getopt argv $opts opt arg]]} {
if {$err eq -1} break
switch -- $opt {
"file" {
set options(isfile) 1
}
"stream" {
set options(isfile) 0
}
"m" {
array set options(fixed) $arg
}
"validate" {
set options(validate) 1
}
"types" {
set options(types) $arg
}
default {
if {[regexp {m:(\w+)} $opt nop type]} {
if {$arg eq ""} {
set fixed(${type}:Group) ""
} else {
foreach {value group} $arg {
set fixed(${type}:Value) $value
set fixed(${type}:Group) $group
}
}
}
}
}
}
set options(fixed) [array get fixed]
return [array get options]
}
#########################
# Scalar/Block Composers
#########################
proc ::yaml::_composeTags {tag value} {
if {$tag eq ""} {return $value}
set value [huddle get_stripped $value]
if {$tag eq "!!str"} {
set pair [list $tag $value]
} elseif {[info exists yaml::composer($tag)]} {
set pair [$yaml::composer($tag) $value]
} else {
error [_getErrorMessage TAG_NOT_FOUND $tag]
}
return [huddle wrap $pair]
}
proc ::yaml::_composeBinary {value} {
package require base64
return [list !!binary [::base64::decode $value]]
}
proc ::yaml::_composePlain {value} {
if {$value ne ""} {
if {[huddle type $value] ne "plain"} {return $value}
set value [huddle get_stripped $value]
}
set pair [_toType $value]
return [huddle wrap $pair]
}
proc ::yaml::_toType {value} {
if {$value eq ""} {return [list !!str ""]}
set lowerval [string tolower $value]
foreach {type} $yaml::data(types) {
if {[info exists yaml::parsers($type)]} {
set pair [$yaml::parsers($type) $value]
if {$pair ne ""} {return $pair}
continue
}
switch -- $type {
int {
# YAML 1.1
if {[regexp {^-?\d[\d,]*\d$|^\d$} $value]} {
regsub -all "," $value "" integer
return [list !!int $integer]
}
}
float {
# don't run before "integer"
regsub -all "," $value "" val
if {[string is double $val]} {
return [list !!float $val]
}
}
default {
# !!null !!true !!false
if {[info exists yaml::fixed($type:Group)] \
&& [lsearch $yaml::fixed($type:Group) $lowerval] >= 0} {
set value $yaml::fixed($type:Value)
return [list !!$type $value]
}
}
}
}
# the others
return [list !!str $value]
}
####################
# Block Node parser
####################
proc ::yaml::_parseBlockNode {{status ""} {indent -1}} {
variable data
set prev {}
set result {}
set scalar 0
set pos 0
set tag ""
while {1} {
if {$data(finished) == 1} {
break
}
_skipSpaces 1
set type [_getc]
set current [_getCurrent]
if {$type eq "-"} {
set cc "[_getc][_getc]"
if {"$type$cc" eq "---" && $current == 0} {
set result {}
continue
} else {
_ungetc 2
# [Spec]
# Since people perceive theg-hindicator as indentation,
# nested block sequences may be indented by one less space
# to compensate, except, of course,
# if nested inside another block sequence.
incr current
}
}
if {$type eq "."} {
set cc "[_getc][_getc]"
if {"$type$cc" eq "..." && $current == 0} {
set data(finished) 1
break
} else {
_ungetc 2
# # [Spec]
# # Since people perceive theg-hindicator as indentation,
# # nested block sequences may be indented by one less space
# # to compensate, except, of course,
# # if nested inside another block sequence.
# incr current
}
}
if {$type eq "" || $current <= $indent} { ; # end document
_ungetc
break
}
switch -- $type {
"-" { ; # block sequence entry
set pos $current
# [196] l-block-seq-entry(n,c)
foreach {scalar value} [_parseSubBlock $pos "SEQUENCE"] break
}
"?" { ; # mapping key
foreach {scalar nop} [_parseSubBlock $pos ""] break
}
":" { ; # mapping value
if {$current < $pos} {set pos [expr {$current+1}]}
foreach {scalar value} [_parseSubBlock $pos "MAPPING"] break
}
"|" { ; # literal block scalar
set value [_parseBlockScalar $indent "\n"]
}
">" { ; # folded block scalar
set value [_parseBlockScalar $indent " "]
}
"<" { ; # mergeing
set c [_getc]
if {"$type$c" eq "<<"} {
set pos [_getCurrent]
_skipSpaces 1
set c [_getc]
if {$c ne ":"} {error [_getErrorMessage INVALID_MERGE_KEY]}
if {$status ne "" && $status ne "MAPPING"} {error [_getErrorMessage INVALID_MERGE_KEY]}
set status "MAPPING"
foreach {result prev} [_mergeExpandedAliases $result $pos $prev] break
} else {
_ungetc
set scalar 1
}
}
"&" { ; # node's anchor property
set anchor [_getToken]
}
"*" { ; # alias node
set alias [_getToken]
if {$yaml::data(validate)} {
set status "ALIAS"
set value *$alias
} else {
set value [_getAnchor $alias]
}
}
"!" { ; # node's tag
_ungetc
set tag [_getToken]
}
"%" { ; # directive line
_getLine
}
default {
if {[regexp {^[\[\]\{\}\"']$} $type]} {
set pos [expr {1 + $current}]
_ungetc
set value [_parseFlowNode]
} else {
set scalar 1
}
}
}
if {$scalar} {
set pos [_getCurrent]
_ungetc
set value [_parseScalarNode $type "BLOCK" $pos]
set value [_composeTags $tag $value]
set tag ""
set scalar 0
}
if {[info exists value]} {
if {$status eq "NODE"} {return $value}
foreach {result prev} [_pushValue $result $prev $status $value "BLOCK"] break
unset value
}
}
if {$status eq "SEQUENCE"} {
set result [huddle sequence {*}$result]
} elseif {$status eq "MAPPING"} {
if {[llength $prev] == 2} {
set result [_set_huddle_mapping $result $prev]
}
} else {
if {[info exists prev]} {
set result $prev
}
set result [lindex $result 0]
set result [_composePlain $result]
if {![huddle isHuddle $result]} {
set result [huddle wrap [list !!str $result]]
}
}
if {$tag ne ""} {
set result [_composeTags $tag $result]
unset tag
}
if {[info exists anchor]} {
_setAnchor $anchor $result
unset anchor
}
return $result
}
proc ::yaml::_mergeExpandedAliases {result pos prev} {
if {$result eq ""} {set result [huddle mapping]}
if {$prev ne ""} {
if {[llength $prev] < 2} {error [_getErrorMessage MALFORMED_MERGE_KEY]}
set result [_set_huddle_mapping $result $prev]
set prev {}
}
set value [_parseBlockNode "" $pos]
set type_name [huddle type $value]
if {$type_name eq "list" || $type_name eq "sequence"} {
set len [huddle llength $value]
for {set i 0} {$i < $len} {incr i} {
set sub [huddle get $value $i]
set result [huddle combine $result $sub]
}
unset sub len
} else {
set result [huddle combine_relaxed $result $value]
}
return [list $result $prev]
}
proc ::yaml::_parseSubBlock {pos statusnew} {
upvar 1 status status
set scalar 0
set value ""
if {[_next_is_blank]} {
if {$statusnew ne ""} {
set status $statusnew
set value [_parseBlockNode "" $pos]
}
} else {
_ungetc
set scalar 1
}
return [list $scalar $value]
}
proc ::yaml::_set_huddle_mapping {result prev} {
foreach {key val} $prev break
set val [_composePlain $val]
if {[huddle isHuddle $key]} {
set key [huddle get_stripped $key]
}
if {$result eq ""} {
set result [huddle mapping $key $val]
} else {
huddle append result $key $val
}
return $result
}
# remove duplications with saving key order
proc ::yaml::_remove_duplication {dict} {
array set tmp $dict
array set tmp2 {}
foreach {key nop} $dict {
if {[info exists tmp2($key)]} continue
lappend result $key $tmp($key)
set tmp2($key) 1
}
return $result
}
# literal "|" (line separator is "\n")
# folding ">" (line separator is " ")
proc ::yaml::_parseBlockScalar {base separator} {
foreach {explicit chomping} [_parseBlockIndicator] break
set idch [string repeat " " $explicit]
set sep $separator
foreach {indent c line} [_getLine] break
if {$indent < $base} {return ""}
# the first line, NOT ignored comment (as a normal-string)
set first $indent
set value $line
set stop 0
while {![_eof]} {
set pos [_getpos]
foreach {indent c line} [_getLine] break
if {$line eq ""} {
regsub " " $sep "" sep
append sep "\n"
continue
}
if {$c eq "#"} {
# skip comments
continue
}
if {$indent <= $base} {
set stop 1
break
}
append value $sep[string repeat " " [expr {$indent - $first}]]$line
set sep $separator
}
if {[info exists pos] && $stop} {_setpos $pos}
switch -- $chomping {
"strip" {
}
"keep" {
append value $sep
}
"clip" {
append value "\n"
}
}
return [huddle wrap [list !!str $value]]
}
# in {> |}
proc ::yaml::_parseBlockIndicator {} {
set chomping "clip"
set explicit 0
while {1} {
set type [_getc]
if {[regexp {[1-9]} $type digit]} { ; # block indentation
set explicit $digit
} elseif {$type eq "-"} { ; # strip chomping
set chomping "strip"
} elseif {$type eq "+"} { ; # keep chomping
set chomping "keep"
} else {
_ungetc
break
}
}
# Note: skipped after the indicator
_getLine
return [list $explicit $chomping]
}
# [162] ns-plain-multi(n,c)
proc ::yaml::_parsePlainScalarInBlock {base {loop 0}} {
if {$loop == 5} { return }
variable data
set start $data(start)
set reStr {(?:[^:#\t \n]*(?::[^\t \n]+)*(?:#[^\t \n]+)* *)*[^:#\t \n]*}
set result [_getFoldedString $reStr]
set result [string trim $result]
set c [_getc 0]
if {$c eq "\n" || $c eq "#"} { ; # multi-line
set lb ""
while {1} {
set fpos [_getpos]
foreach {indent nop line} [_getLine] break
if {[_eof]} {break}
if {$line ne "" && [string index $line 0] ne "#"} {
break
}
append lb "\n"
}
set lb [string range $lb 1 end]
if {!$yaml::data(finished)} {
_setpos $fpos
}
if {$start == $data(start)} {
return $result
}
if {$base <= $indent} {
if {$lb eq ""} {
set lb " "
}
set subs [_parsePlainScalarInBlock $base [expr {$loop+1}]]
if {$subs ne ""} {
append result "$lb$subs"
}
}
}
return $result
}
####################
# Flow Node parser
####################
proc ::yaml::_parseFlowNode {{status ""}} {
set scalar 0
set result {}
set tag ""
set prev {}
while {1} {
_skipSpaces 1
set type [_getc]
switch -- $type {
"" {
break
}
"?" -
":" { ; # mapping value
if {[_next_is_blank]} {
set value [_parseFlowNode "NODE"]
} else {
set scalar 1
}
}
"," { ; # ends a flow collection entry
if {$status eq"NODE"} {
_ungetc
return $value
}
}
"\{" { ; # starts a flow mapping
set value [_parseFlowNode "MAPPING"]
}
"\}" { ; # ends a flow mapping
if {$status ne "MAPPING"} {error [_getErrorMessage MAPEND_NOT_IN_MAP] }
return $result
}
"\[" { ; # starts a flow sequence
set value [_parseFlowNode "SEQUENCE"]
}
"\]" { ; # ends a flow sequence
if {$status ne "SEQUENCE"} {error [_getErrorMessage SEQEND_NOT_IN_SEQ] }
set result [huddle sequence {*}$result]
return $result
}
"&" { ; # node's anchor property
set anchor [_getToken]
}
"*" { ; # alias node
set alias [_getToken]
set value [_getAnchor $alias]
}
"!" { ; # node's tag
_ungetc
set tag [_getToken]
}
"%" { ; # directive line
_ungetc
_parseDirective
}
default {
set scalar 1
}
}
if {$scalar} {
_ungetc
set value [_parseScalarNode $type "FLOW"]
set value [_composeTags $tag $value]
set tag ""
set scalar 0
}
if {[info exists value]} {
if {[info exists anchor]} {
_setAnchor $anchor $value
unset anchor
}
if {$status eq "" || $status eq "NODE"} {return $value}
foreach {result prev} [_pushValue $result $prev $status $value "FLOW"] break
unset value
}
}
return $result
}
proc ::yaml::_pushValue {result prev status value scope} {
switch -- $status {
"SEQUENCE" {
lappend result [_composePlain $value]
}
"MAPPING" {
if {$scope eq "BLOCK"} {
if {[llength $prev] == 2} {
set result [_set_huddle_mapping $result $prev]
set prev [list $value]
} else {
lappend prev $value
}
} else {
lappend prev $value
if {[llength $prev] == 2} {
set result [_set_huddle_mapping $result $prev]
set prev ""
}
}
}
default {
if {$scope eq "BLOCK"} {lappend prev $value}
}
}
return [list $result $prev]
}
proc ::yaml::_parseScalarNode {type scope {pos 0}} {
set tag !!str
switch -- $type {
\" { ; # surrounds a double-quoted flow scalar
set value [_parseDoubleQuoted]
}
{'} { ; # surrounds a single-quoted flow scalar
set value [_parseSingleQuoted]
}
"\t" {error [_getErrorMessage TAB_IN_PLAIN] }
"@" {error [_getErrorMessage AT_IN_PLAIN] }
"`" {error [_getErrorMessage BT_IN_PLAIN] }
default {
# Plane Scalar
if {$scope eq "FLOW"} {
set value [_parsePlainScalarInFlow]
} elseif {$scope eq "BLOCK"} {
set value [_parsePlainScalarInBlock $pos]
}
set tag !!plain
}
}
return [huddle wrap [list $tag $value]]
}
# [time scanning at JST]
# 2001-12-15T02:59:43.1Z => 1008385183
# 2001-12-14t21:59:43.10-05:00 => 1008385183
# 2001-12-14 21:59:43.10 -5 => 1008385183
# 2001-12-15 2:59:43.10 => 1008352783
# 2002-12-14 => 1039791600
proc ::yaml::_parseTimestamp {scalar} {
if {![regexp {^\d\d\d\d-\d\d-\d\d} $scalar]} {return ""}
set datestr {\d\d\d\d-\d\d-\d\d}
set timestr {\d\d?:\d\d:\d\d}
set timezone {Z|[-+]\d\d?(?::\d\d)?}
set canonical [subst -nobackslashes -nocommands {^($datestr)[Tt ]($timestr)\.\d+ ?($timezone)?$}]
set dttm [subst -nobackslashes -nocommands {^($datestr)(?:[Tt ]($timestr))?$}]
if {$::tcl_version < 8.5} {
if {[regexp $canonical $scalar nop dt tm zone]} {
# Canonical
if {$zone eq ""} {
return [list !!timestamp [clock scan "$dt $tm"]]
} elseif {$zone eq "Z"} {
return [list !!timestamp [clock scan "$dt $tm" -gmt 1]]
}
if {[regexp {^([-+])(\d\d?)$} $zone nop sign d]} {set zone [format "$sign%02d:00" $d]}
regexp {^([-+]\d\d):(\d\d)} $zone nop h m
set m [expr {$h > 0 ? $h*60 + $m : $h*60 - $m}]
return [list !!timestamp [clock scan "[expr {-$m}] minutes" -base [clock scan "$dt $tm" -gmt 1]]]
} elseif {[regexp $dttm $scalar nop dt tm]} {
if {$tm ne ""} {
return [list !!timestamp [clock scan "$dt $tm"]]
} else {
return [list !!timestamp [clock scan $dt]]
}
}
} else {
if {[regexp $canonical $scalar nop dt tm zone]} {
# Canonical
if {$zone ne ""} {
if {[regexp {^([-+])(\d\d?)$} $zone nop sign d]} {set zone [format "$sign%02d:00" $d]}
return [list !!timestamp [clock scan "$dt $tm $zone" -format {%Y-%m-%d %k:%M:%S %Z}]]
} else {
return [list !!timestamp [clock scan "$dt $tm" -format {%Y-%m-%d %k:%M:%S}]]
}
} elseif {[regexp $dttm $scalar nop dt tm]} {
if {$tm ne ""} {
return [list !!timestamp [clock scan "$dt $tm" -format {%Y-%m-%d %k:%M:%S}]]
} else {
return [list !!timestamp [clock scan $dt -format {%Y-%m-%d}]]
}
}
}
return ""
}
proc ::yaml::_parseDirective {} {
variable data
variable shorthands
set directive [_getToken]
if {[regexp {^%YAML} $directive]} {
# YAML directive
_skipSpaces
set version [_getToken]
set data(YAMLVersion) $version
if {![regexp {^\d\.\d$} $version]} { error [_getErrorMessage ILLEGAL_YAML_DIRECTIVE] }
} elseif {[regexp {^%TAG} $directive]} {
# TAG directive
_skipSpaces
set handle [_getToken]
if {![regexp {^!$|^!\w*!$} $handle]} { error [_getErrorMessage ILLEGAL_YAML_DIRECTIVE] }
_skipSpaces
set prefix [_getToken]
if {![regexp {^!$|^!\w*!$} $prefix]} { error [_getErrorMessage ILLEGAL_YAML_DIRECTIVE] }
set shorthands(handle) $prefix
}
}
proc ::yaml::_parseTagHandle {} {
set token [_getToken]
if {[regexp {^(!|!\w*!)(.*)} $token nop handle named]} {
# shorthand or non-specific Tags
switch -- $handle {
! { ; # local or non-specific Tags
}
!! { ; # yaml Tags
}
default { ; # shorthand Tags
}
}
if {![info exists prefix($handle)]} { error [_getErrorMessage TAG_NOT_FOUND] }
} elseif {[regexp {^!<(.+)>} $token nop uri]} {
# Verbatim Tags
if {![regexp {^[\w:/]$} $token nop uri]} { error [_getErrorMessage ILLEGAL_TAG_HANDLE] }
} else {
error [_getErrorMessage ILLEGAL_TAG_HANDLE]
}
return "!<$prefix($handle)$named>"
}
proc ::yaml::_parseDoubleQuoted {} {
# capture quoted string with backslash sequences
set reStr {(?:(?:\")(?:[^\\\"]*(?:\\.[^\\\"]*)*)(?:\"))}
set result [_getFoldedString $reStr]
if {$result eq ""} { error [_getErrorMessage MALFORM_D_QUOTE] }
# [116] nb-double-multi-line
regsub -all {[ \t]*\n[\t ]*} $result "\r" result
regsub -all {([^\r])\r} $result {\1 } result
regsub -all { ?\r} $result "\n" result
# [112] s-s-double-escaped(n)
# is not impremented.(specification ???)
# chop off outer ""s and substitute backslashes
# This does more than the RFC-specified backslash sequences,
# but it does cover them all
set chopped [subst -nocommands -novariables \
[string range $result 1 end-1]]
return $chopped
}
proc ::yaml::_parseSingleQuoted {} {
set reStr {(?:(?:')(?:[^']*(?:''[^']*)*)(?:'))}
set result [_getFoldedString $reStr]
if {$result eq ""} { error [_getErrorMessage MALFORM_S_QUOTE] }
# [126] nb-single-multi-line
regsub -all {[ \t]*\n[\t ]*} $result "\r" result
regsub -all {([^\r])\r} $result {\1 } result
regsub -all { ?\r} $result "\n" result
regsub -all {''} [string range $result 1 end-1] {'} chopped
return $chopped
}
# [155] nb-plain-char-in
proc ::yaml::_parsePlainScalarInFlow {} {
set sep {\t \n,\[\]\{\}}
set reStr {(?:[^$sep:#]*(?::[^$sep]+)*(?:#[^$sep]+)* *)*[^$sep:#]*}
set reStr [subst -nobackslashes -nocommands $reStr]
set result [_getFoldedString $reStr]
set result [string trim $result]
if {[_getc 0] eq "#"} {
_getLine
set result "$result [_parsePlainScalarInFlow]"
}
return $result
}
####################
# Generic parser
####################
proc ::yaml::_getFoldedString {reStr} {
variable data
set buff [string range $data(buffer) $data(start) end]
regexp $reStr $buff token
if {![info exists token]} {return}
set len [string length $token]
if {[string first "\n" $token] >= 0} { ; # multi-line
set data(current) [expr {$len - [string last "\n" $token]}]
} else {
incr data(current) $len
}
incr data(start) $len
return $token
}
# get a space separated token
proc ::yaml::_getToken {} {
variable data
set reStr {^[^ \t\n,\]]+}
set result [_getFoldedString $reStr]
return $result
}
proc ::yaml::_skipSpaces {{commentSkip 0}} {
variable data
while {1} {
set ch [string index $data(buffer) $data(start)]
incr data(start)
switch -- $ch {
" " {
incr data(current)
continue
}
"\n" {
set data(current) 0
continue
}
"\#" {
if {$commentSkip} {
_getLine
continue
}
}
}
break
}
incr data(start) -1
}
# get a line of stream(line-end trimed)