Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Escape non janet values from the repl as a string #214

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
55 changes: 52 additions & 3 deletions src/janetsh
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

(var *get-prompt*
(fn *get-prompt* [p]
(string (sh/shrink-path (os/cwd)) " " (parser/state p) "$ ")))
(string (sh/shrink-path (os/cwd)) " " (parser/state p :delimiters) "$ ")))

(if *script*
(sh/init true)
Expand All @@ -83,17 +83,66 @@
(defn- want-implicit-parens [buf p]
(and (not *parens*)
(> (length buf) 1)
(empty? (parser/state p))
(empty? (parser/state p :delimiters))
(peg/match implicit-checker-peg buf)))

(def- symchars
"peg for valid janet symbol characters."
'(+ (range "09" "AZ" "az" "\x80\xFF") (set "!$%&*+-./:<?=>@^_|")))

# Taken from https://github.com/bakpakin/mendoza/blob/master/mendoza/markup.janet
(def- janet-value
"Grammar to detect valid janet values. Note this does not allow for a 'bare'
symbol as a value, only structs, arrays, tuples, etc."
~{:ws (set " \v\t\r\f\n\0")
:readermac (set "';~,")
:symchars ,symchars
:token (some :symchars)
:hex (range "09" "af" "AF")
:escape (* "\\" (+ (set "ntrvzf0e\"\\") (* "x" :hex :hex)))
:comment (* "#" (any (if-not (+ "\n" -1) 1)))
:symbol (if-not (range "09") :token)
:bytes (* (? "@") "\"" (any (+ :escape (if-not "\"" 1))) "\"")
:long-bytes {:delim (some "`")
:open (capture :delim :n)
:close (cmt (* (not (> -1 "`")) (-> :n) ':delim) ,=)
:main (drop (* (? "@") :open (any (if-not :close 1)) :close))}
:number (drop (cmt ':token ,scan-number))
:raw-value (+ :comment :number :bytes :long-bytes
:ptuple :btuple :struct :symbol)
:value (* (any (+ :ws :readermac)) :raw-value)
:root (any :value)
:root2 (any (* :value :value))
:ptuple (* (? "@") "(" :root (any :ws) ")")
:btuple (* (? "@") "[" :root (any :ws) "]")
:struct (* (? "@") "{" :root2 (any :ws) "}")
:main (+ :btuple :struct :ptuple)})

(defn- as-str [s]
(string `"` s `"`))

(def- janetsh-grammar
"Grammar to escape the non janet values with strings."
~{:ws (capture (set " \v\t\r\f\n\0"))
:janet (capture (drop ,janet-value))
:pipe (capture "|")
:redirect (capture (set "<>"))
:token (/ (capture (some ,symchars)) ,as-str)
:value (+ :janet :pipe :redirect :token :ws)
:main (% (any :value))})

(defn- escape-raw-line [line]
"Escape the non-janet values in 'line' as strings."
(get (peg/match janetsh-grammar line) 0))

(defn- getchunk [buf p]
(sh/update-all-jobs-status)
(def prompt (try (*get-prompt* p) ([e] "$ ")))
(when (getline prompt buf)
(when (want-implicit-parens buf p)
(let [line (string buf)]
(buffer/clear buf)
(buffer/format buf "(sh/$? %s)\n" line)
(buffer/format buf "(sh/$? %s)\n" (escape-raw-line line))
true))))

(var *show-exit-code* true)
Expand Down
2 changes: 1 addition & 1 deletion support/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -uex

janetver="95eb54045fe124b83298666d38404f5bff46f515"
janetver="9c89d1c658a11a40c4a97bd88a9321382c26fda5"
janeturl="https://github.com/janet-lang/janet/archive/${janetver}.tar.gz"
mkdir -p ci_builds
prefix="$(readlink -f ./ci_builds)/installed"
Expand Down