add config to node_re_lex
[protos/libecoli.git] / parse-yaml.sh
1 #!/bin/sh
2
3 set -e
4
5 # use a safer version of echo (no option)
6 echo()
7 {
8         printf "%s\n" "$*"
9 }
10
11 debug()
12 {
13         echo "$@" >&2
14 }
15
16 # $1: node sequence number (ex: ec_node4)
17 ec_parse_get_first_child()
18 {
19         local first_child=${1}_first_child
20         echo $(eval 'echo ${'$first_child'}')
21 }
22
23 # $1: node sequence number (ex: ec_node4)
24 ec_parse_get_next()
25 {
26         local next=${1}_next
27         echo $(eval 'echo ${'$next'}')
28 }
29
30 # $1: node sequence number (ex: ec_node4)
31 ec_parse_iter_next()
32 {
33         local seq=${1#ec_node}
34         seq=$((seq+1))
35         local next=ec_node${seq}
36         if [ "$(ec_parse_get_id $next)" != "" ]; then
37                 echo $next
38         fi
39 }
40
41 # $1: node sequence number (ex: ec_node4)
42 ec_parse_get_id()
43 {
44         local id=${1}_id
45         echo $(eval 'echo ${'$id'}')
46 }
47
48 # $1: node sequence number (ex: ec_node4)
49 ec_parse_get_strvec_len()
50 {
51         local strvec_len=${1}_strvec_len
52         echo $(eval 'echo ${'$strvec_len'}')
53 }
54
55 # $1: node sequence number (ex: ec_node4)
56 # $2: index in strvec
57 ec_parse_get_str()
58 {
59         if [ $# -ne 2 ]; then
60                 return
61         fi
62         local str=${1}_str${2}
63         echo $(eval 'echo ${'$str'}')
64 }
65
66 # $1: node sequence number (ex: ec_node4)
67 # $2: node id (string)
68 ec_parse_find_first()
69 {
70         if [ $# -ne 2 ]; then
71                 return
72         fi
73         local node_seq=$1
74         while [ "$node_seq" != "" ]; do
75                 local id=$(ec_parse_get_id $node_seq)
76                 if [ "$id" = "$2" ]; then
77                         echo $node_seq
78                         return 0
79                 fi
80                 node_seq=$(ec_parse_iter_next $node_seq)
81         done
82 }
83
84 path=$(dirname $0)
85
86 yaml=$(mktemp)
87 cat << EOF > $yaml
88 type: or
89 children:
90 - type: seq
91   id: hello
92   help: Say hello to someone
93   children:
94   - type: str
95     string: hello
96   - type: or
97     id: name
98     help: Name of the person to greet
99     children:
100     - type: str
101       string: john
102     - type: str
103       string: mike
104 - type: seq
105   id: goodbye
106   help: Say good bye to someone
107   children:
108   - type: str
109     string: good
110   - type: str
111     string: bye
112   - type: or
113     id: name
114     help: Name of the person to greet
115     children:
116     - type: str
117       string: mary
118     - type: str
119       string: jessica
120 EOF
121
122 output=$(mktemp)
123 match=1
124 $path/build/parse-yaml -i $yaml -o $output || match=0
125 if [ "$match" = "1" ]; then
126         cat $output
127         . $output
128         name=$(ec_parse_get_str $(ec_parse_find_first ec_node1 name) 0)
129         hello=$(ec_parse_get_str $(ec_parse_find_first ec_node1 hello) 0)
130
131         if [ "$hello" != "" ]; then
132                 echo "$name says hello to you!"
133         else
134                 echo "$name says good bye to you!"
135         fi
136 else
137         echo "no match"
138 fi
139 rm $output
140 rm $yaml