Compare commits
No commits in common. "83ce75c6eaf0d55571916905020692fdfda20345" and "efffbafdba1dde0a954127b58936799d977d38ad" have entirely different histories.
83ce75c6ea
...
efffbafdba
|
@ -84,7 +84,7 @@ The idea, however, is that Jesus: we must not have test-rule and apply-rule: com
|
|||
nil))
|
||||
|
||||
(defn apply-table
|
||||
"Applies a table-based rule."
|
||||
"Applies a struct-based rule."
|
||||
[rule parser]
|
||||
(def curr (current parser))
|
||||
(def rule-fn (get rule :rule))
|
||||
|
@ -109,9 +109,6 @@ The idea, however, is that Jesus: we must not have test-rule and apply-rule: com
|
|||
"Renames a rule. By convention, rule names are keywords."
|
||||
[rule name] (put rule :name name))
|
||||
|
||||
### As noted above: we should not use this
|
||||
### Instead, combinators which might not advance should stash state
|
||||
### These are: !, *, ...?
|
||||
(defn test
|
||||
"Tests a rule: returns whether a rule passes but does not consume any tokens."
|
||||
[rule parser]
|
||||
|
@ -125,9 +122,7 @@ The idea, however, is that Jesus: we must not have test-rule and apply-rule: com
|
|||
[rule]
|
||||
@{:name (keyword (string "!" (name rule)))
|
||||
:rule (fn !* [parser]
|
||||
(def origin (stash parser))
|
||||
(def result (apply-rule rule parser))
|
||||
(restore parser origin)
|
||||
(def result (test rule parser))
|
||||
(if-not result
|
||||
(do (advance parser) true)
|
||||
nil))})
|
||||
|
@ -169,7 +164,10 @@ The idea, however, is that Jesus: we must not have test-rule and apply-rule: com
|
|||
~(set ,name (put ,name :rule (,rule :rule)))
|
||||
~(var ,name (rename ,rule ,(keyword name)))))
|
||||
|
||||
(comment (defn panic
|
||||
# If we wanted to be robust, we wouldn't hard-code this
|
||||
(defp stop (+ :newline :semicolon :break))
|
||||
|
||||
(defn panic
|
||||
[rule &opt msg]
|
||||
@{:name (keyword (string "panic-" (name rule)))
|
||||
:rule (fn panic* [parser]
|
||||
|
@ -186,11 +184,7 @@ The idea, however, is that Jesus: we must not have test-rule and apply-rule: com
|
|||
(array/push (parser :errors) the-error)
|
||||
(array/push (parser :captured) the-error)
|
||||
true
|
||||
)}))
|
||||
|
||||
# If we wanted to be robust, we wouldn't hard-code this
|
||||
# To consider: use a stack of "panic-until" on a parser to dynamically decide how far to panic
|
||||
(defp stop (+ :newline :semicolon :break))
|
||||
)})
|
||||
|
||||
(defn panic
|
||||
"Panics the parser, consuming all tokens until the rule matches (including the match). It also adds an error node to both the capture and the error stacks."
|
||||
|
@ -202,7 +196,7 @@ The idea, however, is that Jesus: we must not have test-rule and apply-rule: com
|
|||
(while (not passing)
|
||||
(array/push skipped (current parser))
|
||||
(advance parser)
|
||||
(set passing (apply-rule stop parser))
|
||||
(set passing (apply-rule rule parser))
|
||||
(print "phew; I'm done panicking")
|
||||
(pprint (current parser))
|
||||
(def the-error {:type :error :expected expected :token origin :skipped skipped})
|
||||
|
@ -210,7 +204,7 @@ The idea, however, is that Jesus: we must not have test-rule and apply-rule: com
|
|||
(array/push (parser :captured) the-error)
|
||||
(error parser)))
|
||||
|
||||
(defn *
|
||||
(defn *
|
||||
"Sequences rules: matches if all rules are matched, in sequence."
|
||||
[& rules]
|
||||
@{:name (keyword (string/join (map name rules) "*"))
|
||||
|
@ -219,8 +213,7 @@ The idea, however, is that Jesus: we must not have test-rule and apply-rule: com
|
|||
(var passing true)
|
||||
(def origin (stash parser))
|
||||
(pprint origin)
|
||||
(def failing nil)
|
||||
(def passing true)
|
||||
(set failing nil)
|
||||
(loop [rule :in rules :while passing]
|
||||
(def pass? (apply-rule rule parser))
|
||||
(when (not pass?)
|
||||
|
@ -249,13 +242,13 @@ The idea, however, is that Jesus: we must not have test-rule and apply-rule: com
|
|||
## is this right?
|
||||
|
||||
|
||||
(comment (defn order-1 [& rules]
|
||||
(defn order-1 [& rules]
|
||||
@{:name (keyword (string/join (map name rules) "*"))
|
||||
:rule (fn order-1 [parser]
|
||||
(def result (test (first rules) parser))
|
||||
(if result
|
||||
(apply-rule (order-0 ;rules)) ### compile error: unknown symbol order-0
|
||||
nil))}))
|
||||
(apply-rule (order-0 ;rules))
|
||||
nil))})
|
||||
|
||||
(defn capture-group
|
||||
"Takes a parser and an origin state: takes everything captured since the origin, gathers it up in a single array, pops it all off the capture stack, and pushes the gathered captures back on the stack."
|
||||
|
@ -335,9 +328,9 @@ The idea, however, is that Jesus: we must not have test-rule and apply-rule: com
|
|||
(def parser (new-parser tokens))
|
||||
(try
|
||||
(do (apply-rule rule parser) parser)
|
||||
([err] err)))
|
||||
([err] err))
|
||||
|
||||
(upscope #XXX for easier repl use
|
||||
(upscope
|
||||
|
||||
(defn literal->ast [token] {:type (get token :type) :data (get token :literal) :token token})
|
||||
|
||||
|
@ -349,7 +342,7 @@ The idea, however, is that Jesus: we must not have test-rule and apply-rule: com
|
|||
|
||||
(declare expression simple nonbinding)
|
||||
|
||||
(defp separator (some (+ :newline :comma :break)))
|
||||
(defp separator (+ :newline :comma :break))
|
||||
(defp separators? (any (+ :newline :comma :break)))
|
||||
(defp terminator (some (+ :newline :semicolon :break)))
|
||||
(defp terminators? (any (+ :newline :semicolon :break)))
|
||||
|
|
4
justfile
4
justfile
|
@ -4,7 +4,7 @@ build:
|
|||
|
||||
# open a janet repl in a different os window
|
||||
repl:
|
||||
kitten @ launch --type=os-window --allow-remote-control --cwd=current --title=hx_repl:ludus --keep-focus
|
||||
kitten @ launch --type=os-window --allow-remote-control --cwd=current --title=hx_repl:ludus
|
||||
kitten @ send-text -m "title:hx_repl:ludus" "janet -s\n"
|
||||
|
||||
restart:
|
||||
|
@ -20,5 +20,3 @@ buffer:
|
|||
sd "$" "\n" | sd "\n\n" "\n" > .repl-buffer.janet
|
||||
kitten @ send-text -m "title:hx_repl:ludus" "(import ./.repl-buffer :prefix \"\")"
|
||||
|
||||
doc:
|
||||
sd "$" "\n" | sd "\n\n" "\n" | xargs -I _ echo "(doc " _ ")" | kitten @ send-text -m "title:hx_repl:ludus" --stdin
|
||||
|
|
Loading…
Reference in New Issue
Block a user