Skip to content

Commit 8c7e8c0

Browse files
committed
wip: Change everything
1 parent d4933f6 commit 8c7e8c0

File tree

6 files changed

+314
-210
lines changed

6 files changed

+314
-210
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
The _first_ Bash library parsing JSON in Bash
44

5+
Status: IN DEVELOPMENT
6+
57
## Summary
68

79
- Uses only builtins and no subshells
810
- Leverages [bash-object](https://github.com/hyperupcall/bash-object)
911

1012
## Installation
1113

12-
Use Basalt, a Bash package manager, to add this project as a dependency
14+
Use [Basalt](https://github.com/hyperupcall/basalt), a Bash package manager, to add this project as a dependency
1315

1416
```sh
1517
basalt add hyperupcall/bash-json

examples/file.json

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,2 @@
11
{
2-
"aa": 32,
3-
"bb": "three",
4-
"apple": {
5-
"keeyy": "valuee",
6-
"v": "b"
7-
},
8-
"other": 20
9-
}
2+
"a":}

pkg/src/public/bash-json.sh

+68-52
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,53 @@ bash_json.parse() {
88

99
local -a TOKENS=()
1010

11-
# Construct token sequence
12-
local -i col=1 row=1 i=
11+
# 1. Construct token sequence
12+
# 'scan_*' functions are modified in variuos 'tokenize_' functions
13+
local scan_step_fn='bash_json.tokenize_begin_value'
14+
local -a scan_stack=()
15+
local -i scan_col=1 scan_row=1
16+
17+
local i=
1318
for ((i=0; i<${#content}; ++i)); do
1419
local char="${content:$i:1}"
1520

16-
local prefix="$col:$row:"
17-
case $char in
18-
'{') TOKENS+=("$prefix"'TOKEN_OPEN_CURLEYBRACE') ;;
19-
'}') TOKENS+=("$prefix"'TOKEN_CLOSE_CURLEYBRACE') ;;
20-
'[') TOKENS+=("$prefix"'TOKEN_OPEN_SQUAREBRACKET') ;;
21-
']') TOKENS+=("$prefix"'TOKEN_CLOSE_SQUAREBRACKET') ;;
22-
':') TOKENS+=("$prefix"'TOKEN_COLON') ;;
23-
',') TOKENS+=("$prefix"'TOKEN_COMMA') ;;
24-
'"')
25-
local str=
26-
bash_json.tokenize_string || true
27-
TOKENS+=($'\x01'"${prefix}${str}")
28-
col+=$((${#str}+1))
29-
;;
30-
[[:digit:]])
31-
local str=
32-
bash_json.tokenize_number || true
33-
TOKENS+=($'\x02'"${prefix}${str}")
34-
col+=$((${#str}-1))
35-
;;
36-
$'\n')
37-
row+=1
38-
col=0
39-
;;
40-
' '|$'\t'|$'\r')
41-
;;
42-
*) core.print_die "Failed: $char"
43-
;;
44-
esac
45-
col+=1
21+
if "$scan_step_fn" "$char"; then
22+
core.print_die 'Error'
23+
fi
24+
# local prefix="$row:$col:"
25+
# case $char in
26+
# '{')
27+
# state_stack+=('scan_begin_object')
28+
# ;;
29+
# # '{')TOKENS+=("$prefix"'TOKEN_OPEN_CURLEYBRACE') ;;
30+
# # '}') TOKENS+=("$prefix"'TOKEN_CLOSE_CURLEYBRACE') ;;
31+
# # '[') TOKENS+=("$prefix"'TOKEN_OPEN_SQUAREBRACKET') ;;
32+
# # ']') TOKENS+=("$prefix"'TOKEN_CLOSE_SQUAREBRACKET') ;;
33+
# # ':') TOKENS+=("$prefix"'TOKEN_COLON') ;;
34+
# # ',') TOKENS+=("$prefix"'TOKEN_COMMA') ;;
35+
# '"')
36+
# local str=
37+
# bash_json.tokenize_string || true
38+
# TOKENS+=($'\x01'"${prefix}${str}")
39+
# col+=$((${#str}+1))
40+
# ;;
41+
# [[:digit:]])
42+
# local str=
43+
# bash_json.tokenize_number || true
44+
# TOKENS+=($'\x02'"${prefix}${str}")
45+
# col+=$((${#str}-1))
46+
# ;;
47+
# $'\n')
48+
# row+=1
49+
# col=0
50+
# ;;
51+
# ' '|$'\t'|$'\r')
52+
# ;;
53+
# *) core.print_die "Failed: $char"
54+
# ;;
55+
# esac
56+
57+
scan_col+=1
4658
done; unset -v i
4759

4860
# Print token sequence
@@ -53,28 +65,32 @@ bash_json.parse() {
5365
i=$((i+1))
5466
done
5567

56-
# Construct object using token sequence
57-
if [ "${TOKENS[0]##*:}" = 'TOKEN_OPEN_CURLEYBRACE' ]; then
58-
bash_json.parse_array_or_object "$variable_prefix" 0 '.'
68+
# if ((${#TOKENS[@]} == 0)); then
69+
# core.panic "JSON file empty"
70+
# fi
71+
72+
# # Construct object using token sequence
73+
# if [ "${TOKENS[0]##*:}" = 'TOKEN_OPEN_CURLEYBRACE' ]; then
74+
# bash_json.parse_object "$variable_prefix" 1 '.'
5975

60-
REPLY1=$variable_prefix
61-
REPLY2='object'
62-
elif [ "${TOKENS[0]##*:}" = 'TOKEN_OPEN_SQUAREBRACKET' ]; then
63-
bash_json.parse_array_or_object "$variable_prefix" 0 '.'
76+
# REPLY1=$variable_prefix
77+
# REPLY2='object'
78+
# elif [ "${TOKENS[0]##*:}" = 'TOKEN_OPEN_SQUAREBRACKET' ]; then
79+
# bash_json.parse_array_or_object "$variable_prefix" 0 '.'
6480

65-
REPLY1=$variable_prefix
66-
REPLY2='array'
67-
elif [ "${TOKENS[0]::1}" = $'\x01' ]; then
68-
bash_json.parse_primitive
81+
# REPLY1=$variable_prefix
82+
# REPLY2='array'
83+
# elif [ "${TOKENS[0]::1}" = $'\x01' ]; then
84+
# bash_json.parse_primitive
6985

70-
REPLY1=$REPLY
71-
REPLY2='string'
72-
elif [ "${TOKENS[0]::1}" = $'\x02' ]; then
73-
bash_json.parse_primitive
86+
# REPLY1=$REPLY
87+
# REPLY2='string'
88+
# elif [ "${TOKENS[0]::1}" = $'\x02' ]; then
89+
# bash_json.parse_primitive
7490

75-
REPLY1=$REPLY
76-
REPLY2='number'
77-
else
78-
core.panic 'Unexpected token'
79-
fi
91+
# REPLY1=$REPLY
92+
# REPLY2='number'
93+
# else
94+
# core.panic 'Unexpected token'
95+
# fi
8096
}

0 commit comments

Comments
 (0)