Skip to content

Commit 9c8de65

Browse files
committed
refactor: Improve whitespace and remove 'is_empty' tests
1 parent 753013b commit 9c8de65

File tree

4 files changed

+59
-63
lines changed

4 files changed

+59
-63
lines changed

pkg/lib/commands/parse.sh

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,50 +22,42 @@ bash_toml.do_parse() {
2222
case "$mode" in
2323
# State in which parser starts, and before any given TOML construct
2424
MODE_DEFAULT)
25-
if bash_toml.is.whitespace "$char"; then
26-
:
27-
elif bash_toml.is.newline "$char"; then
28-
:
29-
elif bash_toml.is.octothorp "$char"; then
25+
if bash_toml.is.octothorp "$char"; then
3026
mode='MODE_IN_COMMENT'
31-
elif bash_toml.is.table "$char"; then
32-
bash_toml.parse_fail 'NOT_IMPLEMENTED' "Tables are not supported"
33-
return 1
27+
3428
elif bash_toml.is.double_quote "$char"; then
3529
bash_toml.init_key_string ''
3630
mode='MODE_QUOTEDKEY_DURING_KEY'
31+
3732
elif bash_toml.is.valid_bare_key_char "$char"; then
3833
bash_toml.init_key_string "$char"
3934
mode="MODE_BAREKEY_DURING_KEY"
40-
else
41-
# If after only gobbling up whitespace, and there is nothign left,
42-
# we are done
43-
return 0
35+
36+
elif bash_toml.is.table "$char"; then
37+
bash_toml.parse_fail 'NOT_IMPLEMENTED' "Tables are not supported"
38+
return 1
4439
fi
4540
;;
4641
MODE_IN_COMMENT)
4742
if bash_toml.is.newline "$char"; then
4843
mode='MODE_DEFAULT'
49-
elif bash_toml.is.empty "$char"; then
50-
mode='MODE_DEFAULT'
51-
else
52-
:
5344
fi
5445
;;
5546
MODE_ANY_BEFORE_VALUE)
5647
if bash_toml.is.whitespace "$char"; then
5748
:
58-
elif bash_toml.is.double_quote "$char"; then
59-
mode='MODE_DOUBLEQUOTE_DURING_VALUE'
60-
bash_toml.init_value_string
49+
6150
elif bash_toml.is.single_quote "$char"; then
6251
mode='MODE_SINGLEQUOTE_DURING_VALUE'
6352
bash_toml.init_value_string
53+
54+
elif bash_toml.is.double_quote "$char"; then
55+
mode='MODE_DOUBLEQUOTE_DURING_VALUE'
56+
bash_toml.init_value_string
57+
6458
elif bash_toml.is.newline "$char"; then
6559
bash_toml.parse_fail 'UNEXPECTED_NEWLINE' 'Expected to find value on the same line'
66-
elif bash_toml.is.empty "$char"; then
67-
bash_toml.parse_fail 'UNEXPECTED_EOF' 'Expected to find value on the same line'
68-
return 1
60+
6961
else
7062
bash_toml.parse_fail 'NOT_IMPLEMENTED' "Construct is not valid or not yet implemented"
7163
return 1
@@ -74,16 +66,17 @@ bash_toml.do_parse() {
7466
MODE_BAREKEY_DURING_KEY)
7567
if bash_toml.is.whitespace "$char"; then
7668
mode="MODE_EQUALS_BEFORE"
69+
7770
elif bash_toml.is.equals_sign "$char"; then
7871
mode='MODE_ANY_BEFORE_VALUE'
72+
7973
elif bash_toml.is.newline "$char"; then
80-
bash_toml.parse_fail 'KEY_INVALID' "Key name found without value"
74+
bash_toml.parse_fail 'KEY_INVALID' "Key does not have a value"
8175
return 1
76+
8277
elif bash_toml.is.valid_bare_key_char "$char"; then
8378
bash_toml.append_key_string "$char"
84-
elif bash_toml.is.empty "$char"; then
85-
bash_toml.parse_fail 'KEY_INVALID'
86-
return 1
79+
8780
else
8881
bash_toml.parse_fail 'UNEXPECTED_CHARACTER' "Char '$char' is not valid in toml bare keys"
8982
return 1
@@ -92,24 +85,22 @@ bash_toml.do_parse() {
9285
MODE_QUOTEDKEY_DURING_KEY)
9386
if bash_toml.is.double_quote "$char"; then
9487
mode="MODE_EQUALS_BEFORE"
88+
9589
elif bash_toml.is.newline "$char"; then
96-
bash_toml.parse_fail 'KEY_INVALID' 'Quoted key was not finished on the same line'
97-
return 1
98-
elif bash_toml.is.empty "$char"; then
99-
bash_toml.parse_fail 'KEY_INVALID' 'Quoted key was not finished on the same line'
90+
bash_toml.parse_fail 'KEY_INVALID' 'Key does not have a value'
10091
return 1
92+
10193
else
10294
bash_toml.append_key_string "$char"
10395
fi
10496
;;
10597
MODE_EQUALS_BEFORE)
10698
if bash_toml.is.whitespace "$char"; then
10799
:
100+
108101
elif bash_toml.is.equals_sign "$char"; then
109102
mode="MODE_ANY_BEFORE_VALUE"
110-
elif bash_toml.is.empty "$char"; then
111-
bash_toml.parse_fail 'UNEXPECTED_EOF' "No equals sign found"
112-
return 1
103+
113104
else
114105
bash_toml.parse_fail 'KEY_INVALID'
115106
return 1
@@ -119,11 +110,14 @@ bash_toml.do_parse() {
119110
MODE_DOUBLEQUOTE_DURING_VALUE)
120111
if bash_toml.is.double_quote "$char"; then
121112
mode='MODE_DEFAULT_END'
113+
122114
elif bash_toml.is.backslash "$char"; then
123115
mode='MODE_DOUBLEQUOTE_DURING_ESCAPE_SEQUENCE'
116+
124117
elif bash_toml.is.newline "$char"; then
125118
bash_toml.parse_fail 'UNEXPECTED_NEWLINE' "Literal newlines must not be present in double quotes"
126119
return 1
120+
127121
elif bash_toml.is.control_character "$char"; then
128122
# TODO: this code path won't get activated
129123
:
@@ -168,18 +162,22 @@ bash_toml.do_parse() {
168162
MODE_DOUBLEQUOTE_DURING_ESCAPE_SEQUENCE_UNICODE_START)
169163
local -i unicode_nth_digit=1
170164
local unicode_code_point=
165+
171166
if bash_toml.is.hex_digit "$char"; then
172167
unicode_code_point+="$char"
173168
mode='MODE_DOUBLEQUOTE_DURING_ESCAPE_SEQUENCE_UNICODE_DURING'
169+
174170
else
175171
bash_toml.parse_fail 'UNEXPECTED_CHARACTER' "Encountered character '$char', which is not a valid hex digit as part of a unicode scalar value"
176172
return 1
177173
fi
178174
;;
179175
MODE_DOUBLEQUOTE_DURING_ESCAPE_SEQUENCE_UNICODE_DURING)
180176
unicode_nth_digit=$((unicode_nth_digit+1))
177+
181178
if bash_toml.is.hex_digit "$char"; then
182179
unicode_code_point+="$char"
180+
183181
else
184182
bash_toml.parse_fail 'UNEXPECTED_CHARACTER' "Encountered character '$char', which is not a valid hex digit as part of a unicode scalar value"
185183
return 1
@@ -223,25 +221,25 @@ bash_toml.do_parse() {
223221
MODE_SINGLEQUOTE_DURING_VALUE)
224222
if bash_toml.is.single_quote "$char"; then
225223
mode='MODE_DEFAULT_END'
224+
226225
elif bash_toml.is.newline "$char"; then
227226
bash_toml.parse_fail 'UNEXPECTED_NEWLINE' "Newlines are not valid in single quote"
228227
return 1
229-
elif bash_toml.is.empty "$char"; then
230-
bash_toml.parse_fail 'UNEXPECTED_EOF' "Must complete the literal string with a single quote"
231-
return 1
228+
232229
else
233230
bash_toml.append_value_string "$char"
234231
fi
235232
;;
236233
MODE_DEFAULT_END)
237234
if bash_toml.is.whitespace "$char"; then
238235
:
236+
239237
elif bash_toml.is.newline "$char"; then
240238
mode='MODE_DEFAULT'
241-
elif bash_toml.is.empty "$char"; then
242-
mode='MODE_DEFAULT'
239+
243240
elif bash_toml.is.octothorp "$char"; then
244241
mode='MODE_IN_COMMENT'
242+
245243
else
246244
bash_toml.parse_fail 'UNEXPECTED_CHARACTER' "Encountered character '$char' when a newline was expected"
247245
return 1
@@ -251,13 +249,14 @@ bash_toml.do_parse() {
251249
done
252250

253251
case "$mode" in
254-
MODE_DEFAULT|MODE_DEFAULT_END)
252+
MODE_DEFAULT|MODE_IN_COMMENT|MODE_DEFAULT_END)
255253
;;
256254
*)
257255
bash_toml.parse_fail 'UNEXPECTED_EOF' 'Did not finish parsing construct'
258256
return 1
259257
;;
260258
esac
259+
261260
# If we try to set an empty key with a value, then later on,
262261
# we can access any value (using a non-integer key), and we
263262
# will get a result that equals the original value value

pkg/lib/util/is.sh

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,3 @@ bash_toml.is.equals_sign() {
9191
return 1
9292
fi
9393
}
94-
95-
bash_toml.is.empty() {
96-
if [[ "$1" == '' ]]; then
97-
return 0
98-
else
99-
return 1
100-
fi
101-
}

tests/comment.bats

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,27 @@
33
load './util/init.sh'
44

55
@test "fails on invalid comment 1" {
6-
run bash_toml.do_parse <<-"EOF"
7-
k = # comment
8-
EOF
6+
run bash_toml.do_parse < <(printf 'k = # comment')
97

108
assert_failure
119
assert_output -p "NOT_IMPLEMENTED"
1210
}
1311

1412
@test "succeeds on valid comment 1" {
13+
run bash_toml.do_parse < <(printf '# comment uwu')
14+
15+
assert_success
16+
}
17+
18+
@test "succeeds on valid comment 2" {
1519
run bash_toml.do_parse <<-"EOF"
1620
# comment uwu
1721
EOF
1822

1923
assert_success
2024
}
2125

22-
@test "succeeds on valid comment 2" {
26+
@test "succeeds on valid comment 3" {
2327
bash_toml.do_parse <<-"EOF"
2428
k = 'woof' # comment
2529
EOF
@@ -28,7 +32,7 @@ load './util/init.sh'
2832
assert test_util.toml.key_has_value 'k' 'woof'
2933
}
3034

31-
@test "succeeds on valid comment 3" {
35+
@test "succeeds on valid comment 4" {
3236
bash_toml.do_parse <<-"EOF"
3337
k = "WOOF" # comment
3438
EOF

tests/succeed.bats

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,31 @@
33
load './util/init.sh'
44

55
@test "succeeds on empty 1" {
6-
printf '' | bash_toml.do_parse
6+
run bash_toml.do_parse < <(printf '')
7+
8+
assert_success
79
}
810

911
@test "succeeds on empty 2" {
10-
printf ' ' | bash_toml.do_parse
12+
run bash_toml.do_parse < <(printf '\n')
13+
14+
assert_success
1115
}
1216

1317
@test "succeeds on empty 3" {
14-
printf ' \n' | bash_toml.do_parse
15-
}
18+
run bash_toml.do_parse < <(printf ' ')
1619

17-
@test "succeeds on empty 4" {
18-
printf ' \n \t\n\t \n\n ' | bash_toml.do_parse
20+
assert_success
1921
}
2022

21-
@test "succeeds on empty 5" {
22-
run bash_toml.do_parse <<< ""
23+
@test "succeeds on empty 4" {
24+
run bash_toml.do_parse < <(printf ' \n')
2325

2426
assert_success
2527
}
2628

27-
@test "succeeds on empty 6" {
28-
run bash_toml.do_parse <<"EOF"
29-
EOF
29+
@test "succeeds on empty 5" {
30+
run bash_toml.do_parse < <(printf ' \n \t\n\t \n\n ')
3031

3132
assert_success
3233
}

0 commit comments

Comments
 (0)