Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bj-1line.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# (C) Isaac Freeman (memotype@gmail.com). See https://github.com/memotype/bj.sh
bj()([[ $1 = - ]]||exec<<<"$1";shift;c=;r(){ l=$c;IFS= read -rN1 c;};p(){ printf %s "${o[@]}";};s(){ p=$1;[[ $p ]]||o=();while r;do [[ $p&&! $q ]]&&o+=("$l");case $c in \\)r||break;[[ $p&&$q ]]||o+=("$l${c::!p}");;\")break;;*)[[ $p ]]||o+=("$c");;esac;done;};co(){ n= b=1;while r;do [[ $q ]]||o+=("$l");case $c in \")[[ $1||! $q ]]&&s 1||{ s;k=$(p);};;[|{)((b++));;]|\})((--b))||{ [[ $q ]]&&exit 1;o+=("$c");break;};;:)[[ $1$b = 1&&$q&&$k = "$q" ]]&&break;;,)((0$1&&b==1))&&[[ $q = $((++n)) ]]&&break;;esac;done;};for q in "$@" "";do shift&&[[ ! $q ]]&&return 2;x=0;o=();while r;do case $q$c in *[[:space:]])! :;;\")s;;[tfn0-9-])while o+=("$c")&&r&&[[ $c =~ [-+.0-9Ea-z] ]];do :;done;;*{)co;;*[)[[ $q = 0 ]]||co 1;;?)return 2;;*)return 1;;esac&&x=1&&break;done;done;p;((x));)
bj()(set +u;[[ $1 = - ]]||exec<<<"$1";shift;c=;r(){ l=$c;IFS= read -rN1 c;};p(){ printf %s "${o[@]}";};s(){ p=$1;[[ $p ]]||o=();while r;do [[ $p&&! $q ]]&&o+=("$l");case $c in \\)r||break;[[ $p&&$q ]]||o+=("$l${c::!p}");;\")break;;*)[[ $p ]]||o+=("$c");;esac;done;};co(){ n= b=1;while r;do [[ $q ]]||o+=("$l");case $c in \")[[ $1||! $q ]]&&s 1||{ s;k=$(p);};;[|{)((b++));;]|\})((--b))||{ [[ $q ]]&&exit 1;o+=("$c");break;};;:)[[ $1$b = 1&&$q&&$k = "$q" ]]&&break;;,)((0$1&&b==1))&&[[ $q = $((++n)) ]]&&break;;esac;done;};for q in "$@" "";do shift&&[[ ! $q ]]&&return 2;x=0;o=();while r;do case $q$c in *[[:space:]])! :;;\")s;;[tfn0-9-])while o+=("$c")&&r&&[[ $c =~ [-+.0-9Ea-z] ]];do :;done;;*{)co;;*[)[[ $q = 0 ]]||co 1;;?)return 2;;*)return 1;;esac&&x=1&&break;done;done;p;((x));)
6 changes: 3 additions & 3 deletions bj-80-col.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# (C) Isaac Freeman (memotype@gmail.com). See https://github.com/memotype/bj.sh
bj()([[ $1 = - ]]||exec<<<"$1";shift;c=;r(){ l=$c;IFS= read -rN1 c;};p(){ \
printf %s "${o[@]}";};s(){ p=$1;[[ $p ]]||o=();while r;do [[ $p&&! $q ]]&&o+=(\
"$l");case $c in \\)r||break;[[ $p&&$q ]]||o+=("$l${c::!p}");;\")break;;*)[[ \
bj()(set +u;[[ $1 = - ]]||exec<<<"$1";shift;c=;r(){ l=$c;IFS= read -rN1 c;};p()\
{ printf %s "${o[@]}";};s(){ p=$1;[[ $p ]]||o=();while r;do [[ $p&&! $q ]]&&o+=\
("$l");case $c in \\)r||break;[[ $p&&$q ]]||o+=("$l${c::!p}");;\")break;;*)[[ \
$p ]]||o+=("$c");;esac;done;};co(){ n= b=1;while r;do [[ $q ]]||o+=("$l");case \
$c in \")[[ $1||! $q ]]&&s 1||{ s;k=$(p);};;[|{)((b++));;]|\})((--b))||{ [[ $q \
]]&&exit 1;o+=("$c");break;};;:)[[ $1$b = 1&&$q&&$k = "$q" ]]&&break;;,)((0$1&&
Expand Down
18 changes: 9 additions & 9 deletions bj-90-col.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# (C) Isaac Freeman (memotype@gmail.com). See https://github.com/memotype/bj.sh
bj()([[ $1 = - ]]||exec<<<"$1";shift;c=;r(){ l=$c;IFS= read -rN1 c;};p(){ printf %s \
"${o[@]}";};s(){ p=$1;[[ $p ]]||o=();while r;do [[ $p&&! $q ]]&&o+=("$l");case $c in \\)r\
||break;[[ $p&&$q ]]||o+=("$l${c::!p}");;\")break;;*)[[ $p ]]||o+=("$c");;esac;done;};co\
(){ n= b=1;while r;do [[ $q ]]||o+=("$l");case $c in \")[[ $1||! $q ]]&&s 1||{ s;k=$(p);}
;;[|{)((b++));;]|\})((--b))||{ [[ $q ]]&&exit 1;o+=("$c");break;};;:)[[ $1$b = 1&&$q&&$k \
= "$q" ]]&&break;;,)((0$1&&b==1))&&[[ $q = $((++n)) ]]&&break;;esac;done;};for q in "$@" \
"";do shift&&[[ ! $q ]]&&return 2;x=0;o=();while r;do case $q$c in *[[:space:]])! :;;\")s
;;[tfn0-9-])while o+=("$c")&&r&&[[ $c =~ [-+.0-9Ea-z] ]];do :;done;;*{)co;;*[)[[ $q = 0 \
]]||co 1;;?)return 2;;*)return 1;;esac&&x=1&&break;done;done;p;((x));)
bj()(set +u;[[ $1 = - ]]||exec<<<"$1";shift;c=;r(){ l=$c;IFS= read -rN1 c;};p(){ printf \
%s "${o[@]}";};s(){ p=$1;[[ $p ]]||o=();while r;do [[ $p&&! $q ]]&&o+=("$l");case $c in \
\\)r||break;[[ $p&&$q ]]||o+=("$l${c::!p}");;\")break;;*)[[ $p ]]||o+=("$c");;esac;done;}
co(){ n= b=1;while r;do [[ $q ]]||o+=("$l");case $c in \")[[ $1||! $q ]]&&s 1||{ s;k=$(p)
};;[|{)((b++));;]|\})((--b))||{ [[ $q ]]&&exit 1;o+=("$c");break;};;:)[[ $1$b = 1&&$q&&
$k = "$q" ]]&&break;;,)((0$1&&b==1))&&[[ $q = $((++n)) ]]&&break;;esac;done;};for q in \
"$@" "";do shift&&[[ ! $q ]]&&return 2;x=0;o=();while r;do case $q$c in *[[:space:]])! :;;
\")s;;[tfn0-9-])while o+=("$c")&&r&&[[ $c =~ [-+.0-9Ea-z] ]];do :;done;;*{)co;;*[)[[ $q \
= 0 ]]||co 1;;?)return 2;;*)return 1;;esac&&x=1&&break;done;done;p;((x));)
2 changes: 2 additions & 0 deletions bj.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# Copyright Isaac Freeman (memotype@gmail.com), licensed under the MIT license

bj() (
set +u

# If $1 is -, just read from stdin, otherwise we push $1 to stdin.
# The JSON value is read as a stream of characters from stdin.
[[ $1 = - ]] || exec <<<"$1"
Expand Down
34 changes: 34 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,40 @@ runstdin '[1,2]' '[1,2]'
runstdin '{"a":1}' '{"a":1}'
runstdin node-1 '{"metadata":{"name":"node-1"}}' metadata name

# The parser must work when inherited shell options include nounset without
# changing the caller's option state.
echo "*** nounset caller: $src"
bash -u /dev/stdin "$src" <<'EOF' \
|| fail "$src failed with nounset enabled"
src=$1
. "$src"

check() {
local expected=$1
shift
local c r
r=$(bj "$@")
c=$?
[[ $r = "$expected" && $c = 0 ]] || exit 1
}

json='{"object":{"string":"value","number":42,"true":true,"false":false,"null":null,"array":[0,{"nested":"hit"}]}}'
check '{"string":"value","number":42,"true":true,"false":false,"null":null,"array":[0,{"nested":"hit"}]}' "$json" object
check value "$json" object string
check 42 "$json" object number
check true "$json" object true
check false "$json" object false
check null "$json" object null
check '[0,{"nested":"hit"}]' "$json" object array
check hit "$json" object array 1 nested

r=$(bj "$json" object missing)
c=$?
[[ -z $r && $c = 1 ]] || exit 1
[[ $- = *u* ]] || exit 1
EOF
echo pass

# Array out of bounds test
runteststatus 1 '' '[0, 1, 2, 3]' 4

Expand Down