Skip to content

Commit

Permalink
Fixes Issue kristopolous#11: how to delete a property from an object?
Browse files Browse the repository at this point in the history
Adds a runtime directive of "delete()" as recommended by @coolaj86.
The parsing has been redone slightly to be more accommodating to
distinguish between variables and member functions.
  • Loading branch information
kristopolous committed Dec 22, 2011
1 parent bcf8383 commit 7537095
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -17,8 +17,11 @@ A few array manipulation runtime directives are supported:
* `push` <pre>\`\`arr.push(${arr_len})\`\`</pre>
* `pop` <pre>echo \`\`arr.pop()\`\`</pre>
* `shift` <pre>echo \`\`arr.shift()\`\`</pre>
* `deletet` <pre>echo \`\`key.value.delete()\`\`</pre>
* `items` <pre>for x in \`\`arr.items()\`\`; do echo "${x}"; done</pre>

Note that this doesn't preclude having variables by those names. You can have ``key.delete = 1`` and then ``key.delete.delete()``

Objects
---

Expand Down
9 changes: 9 additions & 0 deletions tests/delete.sh
@@ -0,0 +1,9 @@
#!/bin/bash
. ../ticktick.sh

`` key = {"value": 1} ``

echo ``key.value``
``key.value.delete()``
echo ``key.value``

14 changes: 7 additions & 7 deletions ticktick.sh
Expand Up @@ -100,7 +100,7 @@ __tick_json_parse() {

__tick_fun_tokenize_expression() {
CHAR='[A-Za-z_$\\]'
FUNCTION="(push|pop|shift|items|length)"
FUNCTION="(push|pop|shift|items|delete|length)[[:space:]]*\("
NUMBER='[0-9]*'
STRING="$CHAR*($CHAR*)*"
PAREN="[()]"
Expand All @@ -111,8 +111,6 @@ __tick_fun_tokenize_expression() {
}

__tick_fun_parse_expression() {
local paren=0

while read -r token; do
token=${token/#S/}
token=${token/%E/}
Expand All @@ -121,16 +119,18 @@ __tick_fun_parse_expression() {
suffix+="$token"
else
case "$token" in
push|pop|shift|items|length) function=$token ;;
'(') (( paren++ )) ;;
'push('|'pop('|'shift('|'items('|'delete('|'length(') function=$token ;;
')')
function=${function/%(/}
case $function in
items) echo '${!__tick_data_'"$Prefix"'*}' ;;
delete) echo 'unset __tick_data_'${Prefix/%_/} ;;
pop) echo '"$( __tick_runtime_last ${!__tick_data_'"$Prefix"'*} )"; __tick_runtime_pop ${!__tick_data_'"$Prefix"'*}' ;;
shift) echo '`__tick_runtime_first ${!__tick_data_'"$Prefix"'*}`; __tick_runtime_shift ${!__tick_data_'"$Prefix"'*}' ;;
length) echo '`__tick_runtime_length ${!__tick_data_'"$Prefix"'*}`' ;;
*) echo "__tick_runtime_$function \"$arguments\" __tick_data_$Prefix "'${!__tick_data_'"$Prefix"'*}'
esac
unset function

return
;;
Expand All @@ -140,8 +140,8 @@ __tick_fun_parse_expression() {
'"'|"'"|']') ;;
=) done=1 ;;
# Only respect a space if its in the args.
' ') [ $paren -gt 0 ] && arguments+="$token" ;;
*) [ $paren -gt 0 ] && arguments+="$token" || Prefix+="$token" ;;
' ') [ -n "$function" ] && arguments+="$token" ;;
*) [ -n "$function" ] && arguments+="$token" || Prefix+="$token" ;;
esac
fi
done
Expand Down

0 comments on commit 7537095

Please sign in to comment.