-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: report errors during evaluation
- Errors thrown in resolvables will now be wrapped and associated with the relevant AST node - The error is then formatted in a code frame that highlights the relevant region of the nanoweave code where the error occurred - Object/pair parsers now produce AST
- Loading branch information
1 parent
6d10347
commit 73c9556
Showing
35 changed files
with
630 additions
and
277 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
;; TODO DON'T EVALUATE THIS IN EASTWOOD | ||
;; SEEMS LIKE IT MIGHT CAUSE ISSUES | ||
;; EXCLUDE FROM LINTINGH???? | ||
|
||
(ns repl-env | ||
(:require [clojure.tools.namespace.repl :refer [refresh]])) | ||
|
||
(refresh) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,68 @@ | ||
(ns ^{:doc "Parses the basic structure of a transform definition.", :author "Sean Dawson"} | ||
nanoweave.parsers.base | ||
(:require [blancas.kern.core :refer [bind <|> <?> fwd return]] | ||
[blancas.kern.lexer.java-style :refer | ||
[colon brackets braces comma-sep string-lit identifier]] | ||
[nanoweave.ast.literals :refer [->ArrayLit]] | ||
[nanoweave.utils :refer [declare-extern]])) | ||
(:require [blancas.kern.core :refer [<|> bind put-state return]] | ||
[nanoweave.ast.base :refer [->AstPos ->AstSpan]] | ||
[nanoweave.utils :refer [declare-extern]] | ||
[schema.core :refer [validate]]) | ||
(:import [nanoweave.ast.base AstSpan])) | ||
|
||
; Forward declarations | ||
|
||
(declare-extern nanoweave.parsers.expr/expr) | ||
|
||
; JSON Elements | ||
|
||
(def pair | ||
"Parses the rule: pair := String ':' expr" | ||
(<?> (bind [key (<|> string-lit identifier) | ||
_ colon | ||
value (fwd nanoweave.parsers.expr/expr)] (return [key value])) | ||
"pair")) | ||
(def array | ||
"Parses the rule: array := '[' (expr (',' expr)*)* ']'" | ||
(<?> (brackets (bind [members (comma-sep (fwd nanoweave.parsers.expr/expr))] | ||
(return (->ArrayLit members)))) | ||
"array")) | ||
(def object | ||
"Parses the rule: object := '{' (pair (',' pair)*)* '}'" | ||
(<?> (braces (bind [members (comma-sep pair)] | ||
(return (apply hash-map (reduce concat [] members))))) | ||
"object")) | ||
;; Utility | ||
|
||
(defn <s> | ||
"Pushes the current position to the user state so it can be used to calculate the AST span" | ||
[p] | ||
(fn [current-state] | ||
(let [current-pos (:pos current-state) | ||
current-user-state (:user current-state) | ||
prev-stack (:prev-position current-user-state) | ||
orig-stack-len (count prev-stack) | ||
pushed-stack (conj prev-stack current-pos) | ||
final-state ((put-state {:prev-position pushed-stack}) current-state) | ||
output (p final-state) | ||
after-stack-length (count (:prev-position (:user current-state)))] | ||
(assert (= orig-stack-len after-stack-length) "Mismatched calls to <s> and pop-span. There should be one call to pop-span for each call to <s>") | ||
output))) | ||
|
||
(defn pop-span | ||
"Wraps an AST constructor so that the AST span is automatically applied as an argument after parsing" | ||
[current-state] | ||
(let [curr-pos (:pos current-state) | ||
current-user-state (:user current-state) | ||
prev-stack (:prev-position current-user-state) | ||
prev-pos (peek prev-stack) | ||
span (->AstSpan | ||
(->AstPos (:line prev-pos) (:col prev-pos) (:src prev-pos)) | ||
(->AstPos (:line curr-pos) (dec (:col curr-pos)) (:src curr-pos))) | ||
popped-stack (pop prev-stack) | ||
new-state ((put-state {:prev-position popped-stack}) current-state) | ||
constructor-fn (fn [rec & args] | ||
(fn [& more-args] | ||
(apply rec span (concat args more-args))))] | ||
((return constructor-fn) new-state))) | ||
|
||
; TODO Move this | ||
|
||
(defn merge-span | ||
"Takes two AST objects and creates a new AstSpan object that spans the two objects. | ||
Usually used to create span for binary operators so the span covers both sides of the operator." | ||
[a b] | ||
(let [span-a (:span a) span-b (:span b)] | ||
(validate AstSpan span-a) | ||
(validate AstSpan span-b) | ||
(->AstSpan (:start span-a) (:end span-b)))) | ||
|
||
(defn chainl1* | ||
"Specialisation of Kern's chainl1 function that ensures that binary operator AST | ||
have spans the cover both sides of the operator. | ||
Parses p; as long as there is a binary operator op, reads the op and | ||
another instance of p, then applies the operator on both values. | ||
The operator associates to the left." | ||
[p op] | ||
(letfn [(rest [a] (<|> (bind [f op b p] (rest (f (merge-span a b) a b))) | ||
(return a)))] | ||
(bind [a p] (rest a)))) |
Oops, something went wrong.