feat(package): hemar: I think I was wrong

This commit is contained in:
2025-11-25 15:45:02 +00:00
parent 4729770b59
commit 8a08272f68
3 changed files with 107 additions and 35 deletions

View File

@@ -29,6 +29,7 @@ log notice "running"
# '{[' ws include-header ws ']}' # '{[' ws include-header ws ']}'
# '{[' ws "end" ws ']}' # '{[' ws "end" ws ']}'
# '{[' ws function ws ']}' # '{[' ws function ws ']}'
# '{[' ws '{[' ws ']}'
# #
# # loop tag # # loop tag
# loop-statemant # loop-statemant
@@ -96,6 +97,7 @@ log notice "running"
# 'r' # 'r'
# 't' # 't'
# 'u' hex hex hex hex # 'u' hex hex hex hex
# ws
# #
# hex # hex
# digit # digit
@@ -166,6 +168,7 @@ log notice "running"
# e = [Element] # elements array # e = [Element] # elements array
# } # }
AST=$(mktemp) AST=$(mktemp)
AST_key='.'
trap 'rm -f "$AST"' EXIT INT HUP trap 'rm -f "$AST"' EXIT INT HUP
yq -o j -i '.' "$AST" yq -o j -i '.' "$AST"
@@ -254,11 +257,10 @@ json_escape() {
# finds close pattern and store the char to the stage buffers separating by spaces # finds close pattern and store the char to the stage buffers separating by spaces
find_close_pattern() { find_close_pattern() {
local char="${1:?}" local buf char="${1:?}"
regular_char() { regular_char() {
[ ${TAG_ws_started+x} ] && { [ ${TAG_ws_started+x} ] && {
log debug 'b?'
unset TAG_ws_started unset TAG_ws_started
if [ "${TAG_first_ws_handled+x}" ]; then if [ "${TAG_first_ws_handled+x}" ]; then
buf_next buf_next
@@ -274,6 +276,8 @@ find_close_pattern() {
elif [ "${TAG_close_tag_flag+x}" ]; then elif [ "${TAG_close_tag_flag+x}" ]; then
unset TAG_close_tag_flag unset TAG_close_tag_flag
if [ "$char" = '}' ]; then if [ "$char" = '}' ]; then
log debug "cur buf: $WHITE$(cat "$STAGE_BUFFER_1")"
# removes first and last white spaces from the buffer # removes first and last white spaces from the buffer
sed -i 's/[[:space:]]$//g' "$CURRENT_STAGE_BUFFER" sed -i 's/[[:space:]]$//g' "$CURRENT_STAGE_BUFFER"
sed -i 's/^[[:space:]]//g' "$CURRENT_STAGE_BUFFER" sed -i 's/^[[:space:]]//g' "$CURRENT_STAGE_BUFFER"
@@ -284,30 +288,50 @@ find_close_pattern() {
fi fi
else else
# shellcheck disable=SC1003 # shellcheck disable=SC1003
if [ "$char" = '\' ]; then case "$char" in
TAG_escape_flag=1 '"')
fi if [ "${TAG_escape_flag+x}" ]; then
if [ "$char" = '"' ]; then unset TAG_escape_flag
if [ ${TAG_escape_flag+x} ]; then
unset TAG_escape_flag
else
if [ ${TAG_double_quote_flag+x} ]; then
unset TAG_double_quote_flag
else else
TAG_double_quote_flag=1 if [ ${TAG_double_quote_flag+x} ]; then
unset TAG_double_quote_flag
return 1
else
TAG_double_quote_flag=1
return 1
fi
fi fi
fi ;;
fi '\')
if [ "${TAG_escape_flag+x}" ]; then
unset TAG_escape_flag
else
TAG_escape_flag=1
return 1
fi
;;
'.'|'/'|b|f|n|r|t)
;;
']')
;;
u)
;;
*)
if [ "${TAG_escape_flag+x}" ]; then
if is_ws "$char"; then
unset TAG_escape_flag
else
log error "unexpected char \`$char\` after escape symbol"
exit 1
fi
elif is_ws "$char" && ! [ "${TAG_double_quote_flag+x}" ]; then
TAG_ws_started=1
return 1
fi
;;
esac
if is_ws "$char"; then regular_char "$char"
if [ "${TAG_double_quote_flag+x}" ]; then
regular_char "$char"
else
TAG_ws_started=1
fi
else
regular_char "$char"
fi
fi fi
return 1 return 1
@@ -321,8 +345,6 @@ find_open_pattern() {
elif [ "${open_tag_flag+x}" ]; then elif [ "${open_tag_flag+x}" ]; then
unset open_tag_flag unset open_tag_flag
if [ "$char" = '[' ]; then if [ "$char" = '[' ]; then
# removes last char from buffer ({) is part of open pattern
truncate -s -1 "$CURRENT_STAGE_BUFFER"
return 0 return 0
else else
printf '{%s' "$char" >> "$CURRENT_STAGE_BUFFER" printf '{%s' "$char" >> "$CURRENT_STAGE_BUFFER"
@@ -343,7 +365,7 @@ parse() {
if find_open_pattern "$char"; then if find_open_pattern "$char"; then
log debug "open pattern founded" log debug "open pattern founded"
buf=$(cat "$CURRENT_STAGE_BUFFER") buf=$(cat "$CURRENT_STAGE_BUFFER")
yq -o j -i ". += [{ yq -o j -i "$AST_key += [{
\"type\": \"text\", \"type\": \"text\",
\"value\": \"$(json_escape "$buf")\" \"value\": \"$(json_escape "$buf")\"
}]" "$AST" }]" "$AST"
@@ -354,18 +376,30 @@ parse() {
;; ;;
1) 1)
if find_close_pattern "$char"; then if find_close_pattern "$char"; then
case "$STAGE_BUFFER_1" in case "$(cat "$STAGE_BUFFER_1")" in
compute) compute)
log error 'compute unimplemented'
;; ;;
include) include)
log error 'include unimplemented'
;; ;;
for) for)
path=$STAGE_BUFFER_2
log error 'for unimplemented'
;; ;;
end) end)
log error 'end unimplemented'
;;
'{[')
yq -o j -i "$AST_key += [{
\"type\": \"text\",
\"value\": \"{[\"
}]" "$AST"
;; ;;
*) # interpolation tag *) # interpolation tag
buf=$(cat "$STAGE_BUFFER_1") buf=$(cat "$STAGE_BUFFER_1")
yq -o j -i ". += [{ yq -o j -i "$AST_key += [{
\"type\": \"interpolation\", \"type\": \"interpolation\",
\"path\": \"$(json_escape "$buf")\" \"path\": \"$(json_escape "$buf")\"
}]" "$AST" }]" "$AST"
@@ -433,7 +467,7 @@ if [ "$STAGE" -eq 0 ]; then
fi fi
buf=$(cat "$STAGE_BUFFER_1") buf=$(cat "$STAGE_BUFFER_1")
yq -o j -i ". += [{ yq -o j -i "$AST_key += [{
\"type\": \"text\", \"type\": \"text\",
\"value\": \"$(json_escape "$buf")\" \"value\": \"$(json_escape "$buf")\"
}]" "$AST" }]" "$AST"

View File

@@ -1,6 +1,6 @@
answer="$(echo 'some text' | hemar -c)" answer="$(printf '%s' 'some text' | hemar -c)"
expected="$(printf '[ expected="$(printf '%s' '[
{ {
"type": "text", "type": "text",
"value": "some text" "value": "some text"
@@ -9,9 +9,9 @@ expected="$(printf '[
json_diff "$answer" "$expected" json_diff "$answer" "$expected"
answer="$(echo 'some [] {} text' | hemar -c)" answer="$(printf '%s' 'some [] {} text' | hemar -c)"
expected="$(printf '[ expected="$(printf '%s' '[
{ {
"type": "text", "type": "text",
"value": "some [] {} text" "value": "some [] {} text"
@@ -20,9 +20,9 @@ expected="$(printf '[
json_diff "$answer" "$expected" json_diff "$answer" "$expected"
answer="$(echo 'some {' | hemar -c)" answer="$(printf '%s' 'some {' | hemar -c)"
expected="$(printf '[ expected="$(printf '%s' '[
{ {
"type": "text", "type": "text",
"value": "some {" "value": "some {"

View File

@@ -0,0 +1,38 @@
answer="$(printf '%s' 'text begind {[ "inn \\e\"r"-t\"ext ]}' | hemar -c)"
expected="$(printf '%s' '[
{
"type": "text",
"value": "text begind "
},
{
"type": "interpolation",
"path": "inn \\e\"r-t\"ext"
}
]')"
json_diff "$answer" "$expected"
[ "$(printf '%s' "$answer" | yq '.[1] | .path')" = 'inn \e"r-t"ext' ] || {
log error 'unexpected'
exit 1
}
answer="$(printf '%s' 'text begind {[ [" "] ]}' | hemar -c)"
expected="$(printf '%s' '[
{
"type": "text",
"value": "text begind "
},
{
"type": "interpolation",
"path": "[ ]"
}
]')"
json_diff "$answer" "$expected"
answer="$(printf '%s' 'text begind {[ [" "\ ] ]}' | hemar -c)"
json_diff "$answer" "$expected"