-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.sh
1899 lines (1646 loc) · 49.3 KB
/
parse.sh
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
#!/bin/bash
#%
#% ${PROGNAME} - Wrapper to read and parse yaml files
#%
#% usage: ${PROGNAME}.sh <-c|-p|-f> -f <files>
#%
#% where:
#%
#% -h|--help : show this help
#% -v|--verbose : verbose mode
#% -d|--debug : debug mode, set -x
#% -c|--check : check the file is yaml
#% -p|--parse : parse the content and store it in memory
#% -f|--file : file or files. Wilcards allowed. Must be last option
#%
#% history:
#% 2016-01-15 : created by Rubén Ibáñez Carmona
#%
# leyenda:
# The placeholder OJO will show you the possible issues.
# We'll have problems with the variable scope. It will be necessary to
# dynamically scope them. Now, the 95% of them are local.
# see: http://mywiki.wooledge.org/BashFAQ/084
# Revisar los workaround del clásico explode.
#
# La clave de todo va a estar en los siguientes métodos en utils:
# [ x ] array_send
# [ x ] indexed_send
# [ x ] associative_send
# [ x ] array_receive
# [ x ] indexed_receive
# [ x ] associative_receive
#
#
# Hecho. Ejemplo de uso:
# saluda() {
# local a2r="$(array_receive nueva "$1")"
# eval $a2r
# for i in "${!nueva[@]}"; do
# echo key=$i , value="${nueva[$i]}"
# done
# }
# hola() {
# local -A translations=([apple]="manzana" [lemon]="limon" [banana]="platano" )
# a2send="$(array_send translations)"
# saluda "${a2send}"
# }
# BASH SETTINGS:
# ==============
set -eu -o pipefail
trap "echo ':: Exitting...'" INT TERM EXIT
# CONSTANTS:
# ==========
readonly PROGNAME=$(sed 's/\.sh$//' <<<"${0##*/}") \
SCRIPTNAME=$(basename $0) \
SCRIPTDIR=$(readlink -m $(dirname $0)) \
TMPDIR=/tmp/${PROGNAME}.$$ \
REMPTY=$'\0\0\0\0\0' \
ARGS="$@"
# GLOBALS:
# ========
## Set to 1 to turn on:
typeset -i setting_dump_force_quotes=0 \
setting_use_syck_is_possible=0 \
_containsGroupAnchor=0 \
_containsGroupAlias=0
## Typed:
typeset -i _dumpIndent _dumpWordWrap INDENT=2 WORDWRAP=40
typeset -A SavedGroups delayedPath
## Untyped:
path=
result=
LiteralPlaceHolder='___YAML_Literal_Block___'
_nodeId=
# REQUIREMENTS:
# =============
. $SCRIPTDIR/functions/utils.sh
. $SCRIPTDIR/parser/{constants,methods}.sh
# METHOD DEFINITIONS:
# ===================
# public functions get generic names.
# private functions are prepended by two underscores (RedHat convention).
setx=load
#==============================
# Load valid YAML string to libby:
#==============================
load() {
local input="$@"
__loadString $input
} # load
setx=loadFile
#==============================
# Load a valid YAML file to libby:
#==============================
loadFile() {
local file=$1
__load $file
} # loadFile
setx=YAMLLoad
#==============================
# Load a valid YAML file to the best bash structure:
#==============================
YAMLLoad() {
export LIBBY='LIBBY_'
local file=$1
__load $file
} # YAMLLoad
setx=YAMLLoad
#==============================
# Load a valid YAML string to the best bash structure.
# It should handle these sort of strings:
# "---\n0: hello world\n"
#==============================
YAMLLoadString() {
export LIBBY='LIBBY_'
local input=$1
__loadString $input
} # YAMLLoadString
setx=YAMLDump
#==============================
# return strings
# Dump YAML from BASH file with var declarations statically.
# Don't pass the parameters you want to use with its default values.
# -f file : array BASH vars
# -i int : $indent Pass in false to use the default, which is 2
# -w int : int $wordwrap Pass in 0 for no wordwrap, false for default (40)
# -n : int $no_opening_dashes Do not start YAML file with "---\n".
#==============================
YAMLDump() {
local array
local -i OPTIND=1 \
indent=${INDENT} \
wordwrap=${WORDWRAP} \
no_opening_dashes=1 # use 0 to avoid this header.
while getopts :f:i:w:n opt ; do
case "$opt" in
f) array="$OPTARG";;
i) is_integer "$OPTARG" && indent="$OPTARG" || : ;;
w) is_integer "$OPTARG" && wordwrap="$OPTARG" || : ;;
n) no_opening_dashes=0 ;;
esac
done
shift $(($OPTIND - 1))
export LIBBY='LIBBY_'
if [ ${no_opening_dashes} -eq 1 ]; then
dump -f "${array}" -i "${indent}" -w "${wordwrap}"
else
dump -f "${array}" -i "${indent}" -w "${wordwrap}" -n
fi
} # YAMLDump
setx=dump
#==============================
# return strings: dumps clean YAML.
# Dump YAML from BASH file with var declarations statically.
# Don't pass the parameters you want to use with its default values.
# -f file : array BASH vars
# -i int : $indent Pass in false to use the default, which is 2
# -w int : int $wordwrap Pass in 0 for no wordwrap, false for default (40)
# -n : int $no_opening_dashes Do not start YAML file with "---\n".
#==============================
dump() {
local array string=
local -i OPTIND=1 \
indent=${INDENT}} \
wordwrap=${WORDWRAP} \
no_opening_dashes=1 # use 0 to avoid this header.
while getopts :f:i:w:n opt ; do
case "$opt" in
f) array="$OPTARG";;
i) is_integer "$OPTARG" && indent="$OPTARG" || : ;;
w) is_integer "$OPTARG" && wordwrap="$OPTARG" || : ;;
n) no_opening_dashes=0 ;;
esac
done
shift $(($OPTIND - 1))
_dumpIndent=${indent}
_dumpWordWrap=${wordwrap}
[ ${no_opening_dashes} -eq 0 ] || string="---\n"
# OJO: no me gusta el if -f.
# New YAML document
if [ -f ${array} ]; then
array=( "${array}" ) # ¿quizás debería mapfile?
local -i previous_key=-1
local key value
for key in "${!array[@]}"; do
value="${array[${key}]}"
is_declared first_key || first_key="${key}"
string+="$(__yamelize -k "${key}" \
-v "${value}" \
-i 0 \
-p "${previous_key}" \
-f "${first_key}" \
-- "${array[@]}" )"
previous_key="${key}"
done
fi
printf '%s\n' "${string}"
} # dump
setx='__yamelize'
#==============================
# Attempts to convert a key / value array item to YAML
# private
# return string
# -k $key : The name of the key
# -v $value : The value of the item
# -i $indent : The indent of the current node
# -p $previous_key : by default -1
# -f $first_key : by default 0
# -- $source_array : by default none.
#==============================
__yamelize() {
local key value
local -i OPTIND=1 \
indent \
previous_key=-1 \
first_key=0
while getopts :k:v:i:p:f:s: opt ; do
case "$opt" in
k) key="$OPTARG";;
v) value="$OPTARG";;
i) is_integer "$OPTARG" && indent="$OPTARG" || : ;;
p) is_integer "$OPTARG" && previous_key="$OPTARG" || : ;;
f) is_integer "$OPTARG" && first_key="$OPTARG" || : ;;
esac
done
shift $(($OPTIND - 1))
local -a source_array=( "$@" )
###############################################################################
# OJO!!
if is_array "${value}"; then
if [[ -z "${value}" ]]; then
__dumpNode -k "${key}" \
-v array() \
-i ${indent} \
-p "${previous_key}" \
-f "${first_key}" \
-- "${source_array}"
# // It has children. What to do?
# // Make it the right kind of item
# $string = $this->_dumpNode($key, self::REMPTY, $indent, $previous_key, $first_key, $source_array);
# // Add the indent
# $indent += $this->_dumpIndent;
# // Yamlize the array
# $string .= $this->_yamlizeArray($value,$indent);
elif ! is_array "${value}"; then
# // It doesn't have children. Yip.
# $string = $this->_dumpNode($key, $value, $indent, $previous_key, $first_key, $source_array);
fi
printf '%s' "${string}"
###############################################################################
} # __yamleize
setx='__yamelizeArray'
#==============================
# Attempts to convert an array to YAML
# @access private
# @return string
# @param $array The array you want to convert
# @param $indent The indent of the current level
#==============================
__yamelizeArray() {
# OJO: FALTA EL WRAPPER.
#($array,$indent)
# if (is_array($array)) {
if _IS_ARRAY_($ARRAY); then
local string= key= value=
local -i previous_key=-1
for key in "${!array[@]}"; do
value="${array[$key]}"
[[ -n "${first_key}" ]] || first_key="${key}"
string+="$(__yamelize -k "${key}" \
-v "${value}" \
-i ${indent} \
-p "${previous_key}" \
-f "${first_key}" \
-- "${array[@]}" )"
previous_key="${key}"
done
printf '%s' "${string}"
else
return 1
fi
} # __yamleizeArray
setx='__dumpNode'
#==============================
# Returns YAML from a key and a value
# Prints out a string
# -k $key : The name of the key
# -v $value : The value of the item
# -i $indent : The indent of the current node
#==============================
__dumpNode() {
local key value source_array='null' # OJO: cómo definir source_array?
local -i indent previous_key=-1 first_key=0 OPTIND=1
# local -a source_array=()
local regex="(\n|: |- |\*|#|<|>|%| |\[|]|\{|}|&|'|!)"
while getopts :k:v:i:p:f:s: opt ; do
case "$opt" in
k) key="$OPTARG";;
v) value="$OPTARG";;
i) is_integer "$OPTARG" && indent="$OPTARG" || : ;;
p) is_integer "$OPTARG" && previous_key="$OPTARG" || : ;;
f) is_integer "$OPTARG" && first_key="$OPTARG" || : ;;
s) source_array="$OPTARG";;
esac
done
shift $(($OPTIND - 1))
# OJO: función is_string, quizás debería ser un método y no utils
if is_string "${value}" && \
( [[ "${value}" =~ ${regex} ]] || [[ "${value: -1:1}" == ':' ]] )
then
value="$(__doLiteralBlock -i "${indent}" -v "${value}")"
else
value="$(__doFolding -i "${indent}" -v "${value}")"
fi
# OJO: debería preguntar por ${!value} ?
# OJO: debería ser un pseudo tipo, y no el verdadero tipo?
# me refiero a @_ o [_]
# el if original pregunta si es un array.
# if ($value === array()) $value = '[ ]';
if ! is_scalar "${value}" && is_empty_array "${value}"; then
value='[ ]'
fi
if [[ -z ${value} ]]; then
value='""'
fi
if __isTranslationWord "${value}"; then
value="$(__doLiteralBlock -i "${indent}" -v "${value}")"
fi
if [[ "$(strip -s "${value}")" != "${value}" ]]; then
value="$(__doLiteralBlock -i "${indent}" -v "${value}")"
fi
# if (is_bool($value)) {
# $value = $value ? "true" : "false";
# } OJO: crear método.
if __is_bool "${value}"; then
value=$( [[ ${value} ]] && printf true || printf false )
fi
if __is_null "${value}"; then value='null'; fi
if [[ "${value}" == "'$REMPTY'" ]]; then
value='null'
fi
spaces="$(printf '%*s' ${indent})"
# OJO:
if is_array "${source_array}" && \
array_keys($source_array) === range(0, count($source_array) - 1))
then
# It's a sequence
string="${spaces}- ${value}\n"
else
# It's mapped
local rx=':|#'
if ! [[ "${key}" =~ ${rx} ]]; then
key="\"${key}\""
fi
string="$(rstrip -s "${spaces}${key}: ${value}\n")"
fi
printf '%s' "${string}"
} # __dumpNode
setx='__doLiteralBlock'
#==============================
# Creates a literal block for dumping
# Prints out a string
# -i $indent : The value of the indent
# -v $value : The value
#==============================
__doLiteralBlock() {
local indent="$1"; shift
local value="$@"
if [[ "${value}" == '\n' ]]; then
printf '%s' '\n'
return 0
fi
if ! [[ "${value}" =~ '\n' ]] && ! [[ "${value}" =~ "'" ]]; then
printf "'%s'" "${value}"
return 0
fi
if ! [[ "${value}" =~ '\n' ]] && ! [[ "${value}" =~ '"' ]]; then
printf '"%s"' "${value}"
return 0
fi
# OJO : ojo al explode.
mapfile -t exploded <<<"$(printf '%b' "${value}")"
newValue='|'
if is_declared exploded[0] && \
( [[ "${exploded[0]]}" == '|' ]] || \
[[ "${exploded[0]]}" == '|-' ]] || \
[[ "${exploded[0]]}" == '>' ]] )
then
newValue="${exploded[0]]}"
unset exploded[0]
fi
# OJO: check the unset and the for quotes.
indent="${_dumpIndent}"
spaces="$(printf '%*s' ${indent})"
# OJO: los paréntesis del if
for line in "${exploded[@]}"; do
line="$(strip -s "${line}")"
lenLine="${#line}"
if ( [ $(index -s "${line}" -c '"') -eq 0 ] && \
[ $(rindex -s "${line}" -c '"') -eq $(( lenLine - 1 )) ] ) || \
( [ $(index -s "${line}" -c "'") -eq 0 ] && \
[ $(rindex -s "${line}" -c "'") -eq $(( lenLine - 1 )) ] )
then
line="${line:1:-1}"
fi
# OJO : $newValue .= "\n" . $spaces . ($line);
newValue+="\n${spaces}${line}"
done
printf '%s' "${newValue}"
} # __doLiteralBlock
setx='__doFolding'
#==============================
# Folds a string of text, if necessary
# Prints out a string
# $1 $indent
# $2..n $value : The string you wish to fold
#==============================
__doFolding() {
local indent="$1"; shift
local value="$@"
# Don't do anything if wordwrap is set to 0
if [ $(__dumpWordWrap) -ne 0 ] &&\
! is_integer "${value}" &&\
is_scalar "${value}" &&\
[ "${#value}" -gt $(__dumpWordWrap) ]
then
(( indent += _dumpIndent ))
indent="$(printf '%*s' ${indent})"
wrapped="$(word_wrap -w "$(__dumpWordWrap)"-b "\n${indent}" -- "${value}")"
value=">\n${indent}${wrapped}"
else
if [ ${setting_dump_force_quotes} -eq 1 ] &&\
! is_integer "${value}" &&\
is_scalar "${value}" &&\
[[ "${value}" != "${REMPTY}" ]]
then
value="\"${value}\""
fi
local rx='[0-9ex\\ ]+'
if [[ "${value}" =~ ${rx} ]] && is_scalar "${value}"; then
value="\"${value}\""
fi
fi
printf '%s' "${value}"
} # __doFolding
setx='isTrueWord'
#==============================
# Detect any word with true value as meaning
#==============================
__isTrueWord() {
local value=$1 pattern
local -a words=()
words=( $(__getTranslations true on yes y) )
# Check if value is in words
pattern=$(printf '%s|' "${words[@]}")
pattern="+(${pattern%|})"
shopt -s extglob
case ${value} in
${pattern}) rc=0 ;;
*) rc=1 ;;
esac
shopt -u extglob
return $rc
} # __isTrueWord
setx='isFalseWord'
#==============================
# Detect any word with false value as meaning
#==============================
__isFalseWord() {
local value=$1 pattern
local -a words=()
words=( $(__getTranslations false off no n) )
# Check if value is in words
pattern=$(printf '%s|' "${words[@]}")
pattern="+(${pattern%|})"
shopt -s extglob
case ${value} in
${pattern}) rc=0 ;;
*) rc=1 ;;
esac
shopt -u extglob
return $rc
} # __isFalseWord
setx='isNullWord'
#==============================
# Detect any word with null value as meaning
#==============================
__isNullWord() {
local value=$1 pattern
local -a words=()
words=( $(__getTranslations null '~') )
# Check if value is in words
pattern=$(printf '%s|' "${words[@]}")
pattern="+(${pattern%|})"
shopt -s extglob
case ${value} in
${pattern}) rc=0 ;;
*) rc=1 ;;
esac
shopt -u extglob
return $rc
} # __isNullWord
setx='isTranslationWord'
#==============================
# Detect any word with translation value as meaning
#==============================
__isTranslationWord() {
local value="$@"
__isTrueWord ${value} || \
__isFalseWord ${value} || \
__isNullWord ${value}
} # __isTranslationWord
setx='__coerceValue'
#==============================
# Coerce a string into a native type
# Reference: http://yaml.org/type/bool.html
# TODO: Use only words from the YAML spec.
# USAGE: variable=$(__coerceValue $variable)
# @param $value The value to coerce
#==============================
__coerceValue() {
local value=$1
if __isTrueWord "${value}"; then
value=true
elif __isFalseWord "${value}"; then
value=false
elif __isNullWord "${value}"; then
value=null
fi
printf '%s' "${value}"
} # __coerceValue
setx='getTranslations'
#==============================
# Given a set of words, perform the appropriate translations on them to
# match the YAML 1.1 specification for type coercing.
# $@ : The words to translate
# return a list of words space separated
#==============================
__getTranslations() {
local words="$@" i
local -a result=()
for i in ${words}; do
result+=(
${i^}
${i^^}
${i,,}
)
done
echo -n ${result[@]}
} # __getTranslations
# LOADING METHODS:
# ================
setx='__load'
#==============================
# Guess the source and execute __loadWithSource with it.
#==============================
__load() {
local input="$@"
local Source
Source="$(__loadFromSource "${input}")"
__loadWithSource "${Source}"
} # __load
setx='__loadString'
#==============================
# Guess the source from a string and execute __loadWithSource with it.
#==============================
__loadString() {
local input=$1
local Source
Source="$(__loadFromString "${input}")"
__loadWithSource "${Source}"
} # __loadString
setx='__loadWithSource'
#==============================
# Returns an array
#==============================
__loadWithSource() {
local -a Source=( "$@" )
[[ -n "${Source}" ]] || return 0 # return array();
if [ ${setting_use_syck_is_possible} -ne 0 ] && is_function syck_load; then
array=( $(syck_load "$(printq "${Source}")") )
if ! is_scalar array; then
printb "${array[@]}"
return 0
else
return 1 # OJO
fi
fi
# $array = syck_load (implode ("\n", $Source));
# return is_array($array) ? $array : array();
local line \
lstripLine \
tempPath \
path \
literalBlockStyle \
literalBlock \
lstripPlusOne
# OJO : revisar el uso de estos arrays. Creo deberían ser globales.
local -a path \
result
local -i i \
cnt="${#Source[@]}" \
indent \
lenLine \
lstripLenLine \
literal_block_indent
for (( i = 0; i < cnt; i++ )); do
line="${Source[i]}"
lenLine="${#line}"
lstripLine="$(lstrip -s "${line}")"
lstripLenLine="${#lstripLine}"
indent=$(( lenLine - lstripLenLine ))
tempPath="$(__getParentPathByIndent -i "${indent}")"
line="$(__stripIndent -i "${indent}" -l "${line}")"
! __isComment "${line}" || continue
! __isEmpty "${line}" || continue
path="${tempPath}"
literalBlockStyle="$(__startsLiteralBlock "${line}")" # OJO : quizás aquí
# se necesita boolean
# aunque no lo creo.
if [[ -z "${literalBlockStyle}" ]]; then
line="$(rstrip -s "${line}" -c "${literalBlockStyle} \n")"
literalBlock=''
line+=" ${LiteralPlaceHolder}"
lstripPlusOne="$(lstrip -s "${Source[i+1]}")"
literal_block_indent=$(( ${#Source[i+1]} - ${#lstripPlusOne} ))
while (( ++i < cnt )) && __literalBlockContinues ${indent} "${Source[i]}"
do
literalBlock="$(__addLiteralLine -b "${literalBlock}" \
-l "${Source[i]}" \
-s "${literalBlockStyle}" \
-i "${literal_block_indent}")"
done
(( i-- ))
fi
# Strip out comments
local rx="[[:space:]]*#([^\"']+)$"
if [[ "${line}" =~ '#' ]]; then
if [[ "${line}" =~ ${rx} ]]; then
line="${line/${BASH_REMATCH[0]}}"
fi
fi
while (( ++i < cnt )) && __greedilyNeedNextLine "${line}"; do
line="$(rstrip -s "${line}") $(lstrip -s "${Source[i]}" -c ' \t')"
done
(( i-- ))
# OJO : esto qué
lineArray="$(__parseLine "${line}" )"
if [[ -z "${literalBlockStyle}" ]]; then
lineArray="$(__revertLiteralPlaceHolder -b "${literalBlock}"
-- "${lineArray[@]}")"
fi
# $this->addArray($lineArray, $this->indent);
local ind delPa
for ind in ${!delayedPath[@]}; do
delPa="${delayedPath[${ind}]}"
path[$ind]="${delPa}"
done
delayedPath=()
done
# return $this->result;
} # __loadWithSource
setx='__loadFromSource'
#==============================
# OJO
__loadFromSource() {
v local input="$@"
if [[ -n "${input}" ]] && \
! index -s "${index}" -c '\n' && \
[ -f "${input}" ]
then
input=$(<"${input}")
else
# return $this->loadFromString($input);
__loadFromString "${input}"
fi
} # __loadFromSource
setx='__loadFromString'
#==============================
# Explodes the string on the '\n', creates the array lines.
#==============================
__loadFromString() {
local input="$1" k v
local -a lines
mapfile -t lines <<<"${input}"
for k in "${!lines[@]}"; do
v="${lines[$k]}"
lines[$k]=$(rstrip -s "${v}" -c '\r')
done
printf '%s\n' "${lines[@]}"
} # __loadFromString
setx='__parseLine'
#==============================
# Parses YAML code and returns an array for a node
# @access private
# @return array
# @param string $line A line from the YAML file
#==============================
__parseLine() {
local line="$@"
# OJO : quizás debería devolver un error?
[[ -n $line ]] || return 0
# if (!$line) return array();
line=$(strip -s "${line}")
[[ -n $line ]] || return 0
local -a array=()
local group=$(__nodeContainsGroup "${line}")
if [[ -n "${group}"]]; then
__addGroup -l "${line}" -g "${group}"
line=$(__stripGroup -l "${line}" -g "${group}")
fi
if __startsMappedSequence "${line}"; then
__returnMappedSequence "${line}"
fi
if __isArrayElement "${line}"; then
__returnArrayElement "${line}"
fi
if __isPlainArray "${line}"; then
__returnPlainArray "${line}"
fi
__returnKeyValuePair "${line}"
} # __parseLine
setx='__toType'
#==============================
# Finds the type of the passed value, returns the value as the new type.
# @access private
# @param string $value
# @return mixed
#==============================
__toType() {
local value="$1"
if [[ -z "${value}" ]]; then
printf '%s' "${value}"
return 0
fi
first_character="${value:0:1}"
last_character="${value: -1:1}"
is_quoted=1 # means false
while : ; do
[[ -n "${value}" ]] || break
if [[ "${first_character}" != '"' ]] && [[ "${first_character}" != "'" ]]
then
break
fi
if [[ "${last_character}" != '"' ]] && [[ "${last_character}" != "'" ]]
then
break
fi
is_quoted=0 # means true
break
done
if [ ${is_quoted} -eq 0 ]; then
value="$(printf '%b' "${value}")" # substituía literal \n por el autentico.
# quizás aquí no sea necesario. OJO :
# haciendo esto ahora lo estamos quitando
local strtr="${value:1:-1}"
local -A patterns=(
["\\\""]=\"
[\'\']=\'
[\\\']=\'
)
for from in "${!patterns[@]}"; do
to="${arr[${from}]}"
strtr="${strtr//${from}/${to}}"
done
printf '%s' "${strtr}"
return 0
fi
if [[ "${value}" =~ ' #' ]] && [ ${is_quoted} -ne 0 ]; then
local rx="[[:space:]]+#(.+)$"
[[ "${value}" =~ ${rx} ]] && value="${value/${BASH_REMATCH[0]}}" || :
fi
if [[ "${first_character}" == '[' ]] && [[ "${last_character}" == ']' ]]
then
# Take out strings sequences and mappings
local innerValue="$(strip -s "${value:1:-1}")"
if [[ -z "${innerValue}" ]]; then
# OJO : return array();
return 0
fi
# OJO : raro.
local explode="$(__inlineEscape "${innerValue}")"
# Propagate value array
value=( $(echo) )
local v
for v in "${explode[@]}"; do
# OJO : revisar en el php las asignaciones arr[] = something. son un +=()
# OJO : RECURSION
value+=( "$(__toType "${v}")" )
done
printf '%s\n' "${value[@]}"
return 0
fi
if [[ "${value}" =~ ': ' ]] && [[ "${first_character}" != '{' ]]; then
array=( "${value%%: *}" "${value#*: }" )
key="$(strip -s "${array[0]}")"
value="${array[1]}"
# OJO : RECURSION
value="$(__toType "${value}")"
# OJO: return array($key => $value);
printf '%s\n' "${key}" "${value}"
return 0
fi
if [[ "${first_character}" == '{' ]] && [[ "${last_character}" == '}' ]]
then
innerValue="$(strip -s "${value:1:-1}")"
if [[ -z "${innerValue}" ]]; then
# Inline Mapping
# Take out strings sequences and mappings
explode=( "$(__inlineEscape "${innerValue}")" )
# Propagate value array
array=()
for v in "${explode[@]}"; do
SubArr=( "$(__toType "${v}")" )
[[ -n ${!SubArr[@]} ]] || continue
# y ahora viene experimento mío para probar de iterar con puntero sobre
# un array:
# enviamos todo el array al file descriptor 3 y, una vez allí, usaremos
# read para obtener un valor moviendo el puntero linea a linea sin
# peligro.
# OJO : if (is_array ($SubArr)) {
# if ! [ -t 3 ]; then
# exec 3< <(printf '%s\n' "${SubArr[@]}")
# fi
if ! is_scalar SubArr; then
local keysubarr="${!SubArr[0]}" # OJO : realmente se necesita solo el
# primer valor??
array[${keysubarr}]="${SubArr[${keysubarr}]}"
# $array[key($SubArr)] = $SubArr[key($SubArr)]
continue
fi
# exec 3>&-
done
array+=( "${SubArr[@]}" )
fi
# return $array;
printf '%s\n' "${array[@]}"
return 0
fi
rx='null|NULL|Null|~'
if [[ "${value}" =~ ${rx} ]]; then
printf 'null'
return 0
fi
rx='^(-|)[1-9]+[0-9]*$'
if [[ "${value}" =~ ${rx} ]]; then
local -i intvalue=${value}
# if ($intvalue != PHP_INT_MAX)
# $value = $intvalue;
printf '%d' "${value}"
return 0
fi
rx='^0[xX][0-9a-fA-F]+$'
if [[ "${value}" =~ ${rx} ]]; then
local -i intvalue=${value,,}
printf '%x' "${value}"
return 0
fi
# value="$(__coerceValue "${value}")"
# if (is_numeric($value)) {
# if ($value === '0') return 0;
# if (rtrim ($value, 0) === $value)
# $value = (float)$value;
# return $value;
# }
printf '%s' "${value}"
return 0
} # __toType
setx='__inlineEscape'
#==============================
# Used in inlines to check for more inlines or quoted strings
# Prints out an array
#==============================
__inlineEscape() {
local inline="$@" #($inline)