feat: initial commit

This commit is contained in:
ptrcnull 2022-08-05 22:47:29 +02:00
commit c349fae24a
5 changed files with 130 additions and 0 deletions

22
LICENSE Normal file
View file

@ -0,0 +1,22 @@
Copyright (c) 2022 ptrcnull
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

24
README.md Normal file
View file

@ -0,0 +1,24 @@
# execline-json
> why would you even do that
json parser written (mostly) in execline
requirements:
- execline
- [execline-state](https://git.ddd.rip/ptrcnull/execline-state)
- s6-portable-utils
- sed
- tr
- cut (with delimiter/fields support)
### usage
```sh
#!/bin/execlineb
backtick -E users_json { curl https://gitlab.com/api/v4/users?username=ptrcnull }
json users "$users_json"
foreground { s6-echo "${users[0]}" }
s6-echo "${users[0][id]}"
```

17
json Executable file
View file

@ -0,0 +1,17 @@
#!/bin/execlineb
multisubstitute {
importas -ui name 1
importas -ui input 2
}
backtick defines {
backtick -E cleaned { pipeline { s6-echo "$input" } tr -d "\\n" }
json-defines "$name" "$cleaned"
}
importas -uisd "\n" defines defines
shift -n 2
elgetpositionals
$defines
$@

35
json-defines Executable file
View file

@ -0,0 +1,35 @@
#!/bin/execlineb
multisubstitute {
importas -ui name 1
importas -ui input 2
}
foreground {
s6-echo "define
${name}
${input}"
}
case "$input" {
"\\{.*\\}" {
pipeline { s6-unquote -d "{}" "$input" }
pipeline { json-split-entries }
forstdin -E entry
backtick -E quoted_key { pipeline { s6-echo "${entry}" } cut -d ":" -f1 }
backtick -E value { pipeline { s6-echo $entry } cut -d ":" -f2- }
backtick -E key { s6-unquote "$quoted_key" }
json-defines "${name}[${key}]" "$value"
}
"\\[.*\\]" {
pipeline { s6-unquote -d "[]" $input }
pipeline { json-split-entries }
pipeline { s6-grep -n . }
forstdin -E entry
backtick -E lua_index { pipeline { s6-echo $entry } cut -d ":" -f1 }
backtick -I -E index { s6-expr "$lua_index" - 1 }
backtick -E value { pipeline { s6-echo $entry } cut -d ":" -f2- }
json-defines "${name}[${index}]" "$value"
}
}

32
json-split-entries Executable file
View file

@ -0,0 +1,32 @@
#!/bin/execlineb
pipeline { sed "s/./\\0\\n/g" }
init-state level
save-state level 0
foreground {
forstdin -E char
case "$char" {
"(\\{|\\[)" {
load-state level cur
backtick -I -E new { s6-expr "$cur" + 1 }
save-state level $new
s6-echo -n "$char"
}
"(\\}|\\])" {
load-state level cur
backtick -I -E new { s6-expr "$cur" - 1 }
save-state level $new
s6-echo -n "$char"
}
"," {
load-state level cur
save-state level "$cur"
ifelse { s6-test "$cur" = 0 } {
s6-echo
}
s6-echo -n "$char"
}
}
s6-echo -n "$char"
}
s6-echo