diff options
author | Eduardo Julian | 2016-05-12 22:19:01 -0400 |
---|---|---|
committer | Eduardo Julian | 2016-05-12 22:19:01 -0400 |
commit | 163bb7ebc77b423f09ff3bcf277eadd3c3423dfc (patch) | |
tree | bedfd6ef3ee26e649d69a4fc1c8ca106e2850cb7 | |
parent | 5d4cd81c9dbaff906487f99b2ab39d089b9a3378 (diff) |
- Now with better localization for reader, lexer & parser errors.
Diffstat (limited to '')
-rw-r--r-- | src/lux/analyser.clj | 16 | ||||
-rw-r--r-- | src/lux/base.clj | 10 | ||||
-rw-r--r-- | src/lux/lexer.clj | 4 | ||||
-rw-r--r-- | src/lux/parser.clj | 10 | ||||
-rw-r--r-- | src/lux/reader.clj | 6 | ||||
-rw-r--r-- | src/lux/repl.clj | 2 |
6 files changed, 24 insertions, 24 deletions
diff --git a/src/lux/analyser.clj b/src/lux/analyser.clj index 03b217f85..93092c9ac 100644 --- a/src/lux/analyser.clj +++ b/src/lux/analyser.clj @@ -44,16 +44,6 @@ )) )) -(defn ^:private add-loc [meta ^String msg] - (if (.startsWith msg "@") - msg - (|let [[file line col] meta] - (str "@ " file "," line "," col "\n" msg)))) - -(defn ^:private fail-with-loc [msg] - (fn [state] - (fail* (add-loc (&/get$ &/$cursor state) msg)))) - (defn ^:private aba1 [analyse optimize eval! compile-module compilers exo-type token] (|let [[compile-def compile-program compile-class compile-interface] compilers] (|case token @@ -152,7 +142,7 @@ (&&lux/analyse-program analyse optimize compile-program ?args ?body) _ - (fail-with-loc (str "[Analyser Error] Unknown syntax: " (prn-str (&/show-ast (&/T [(&/T ["" -1 -1]) token]))))) + (&/fail-with-loc (str "[Analyser Error] Unknown syntax: " (prn-str (&/show-ast (&/T [(&/T ["" -1 -1]) token]))))) ))) (defn ^:private analyse-basic-ast [analyse optimize eval! compile-module compilers exo-type token] @@ -165,8 +155,8 @@ (&/$Left msg) (if (= "" msg) - (fail* (add-loc (&/get$ &/$cursor state) (str "[Analyser Error] Unrecognized token: " (&/show-ast token)))) - (fail* (add-loc (&/get$ &/$cursor state) msg))) + (fail* (&/add-loc (&/get$ &/$cursor state) (str "[Analyser Error] Unrecognized token: " (&/show-ast token)))) + (fail* (&/add-loc (&/get$ &/$cursor state) msg))) )))) (defn ^:private just-analyse [analyser syntax] diff --git a/src/lux/base.clj b/src/lux/base.clj index d514e17e5..622b5b1fc 100644 --- a/src/lux/base.clj +++ b/src/lux/base.clj @@ -1214,3 +1214,13 @@ (defn |partition [n xs] (->> xs ->seq (partition-all n) (map ->list) ->list)) + +(defn add-loc [meta ^String msg] + (if (.startsWith msg "@") + msg + (|let [[file line col] meta] + (str "@ " file "," line "," col "\n" msg)))) + +(defn fail-with-loc [msg] + (fn [state] + (fail* (add-loc (get$ $cursor state) msg)))) diff --git a/src/lux/lexer.clj b/src/lux/lexer.clj index 8262e5be0..490f37ac3 100644 --- a/src/lux/lexer.clj +++ b/src/lux/lexer.clj @@ -40,7 +40,7 @@ (.equals ^Object escaped "\\\"") (return "\"") (.equals ^Object escaped "\\\\") (return "\\") :else - (fail (str "[Lexer Error] Unknown escape character: " escaped)))) + (&/fail-with-loc (str "[Lexer Error] Unknown escape character: " escaped)))) (defn ^:private escape-char* [escaped] "(-> Text Text)" @@ -100,7 +100,7 @@ (return pre-quotes**)) [pre-quotes post-quotes] (if (.endsWith pre-quotes* "\\") (if eol? - (fail "[Lexer Error] Can't leave dangling back-slash \\") + (&/fail-with-loc "[Lexer Error] Can't leave dangling back-slash \\") (if (if-let [^String back-slashes (re-find #"\\+$" pre-quotes*)] (odd? (.length back-slashes))) (|do [[_ eol?* _] (&reader/read-regex #"^([\"])") diff --git a/src/lux/parser.clj b/src/lux/parser.clj index 0534ac973..5b0bfce57 100644 --- a/src/lux/parser.clj +++ b/src/lux/parser.clj @@ -7,7 +7,7 @@ (:require [clojure.template :refer [do-template]] clojure.core.match clojure.core.match.array - (lux [base :as & :refer [|do return fail |case]] + (lux [base :as & :refer [|do return |case]] [lexer :as &lexer]))) ;; [Utils] @@ -41,7 +41,7 @@ (return (<tag> (&/fold &/|++ &/$Nil elems))) _ - (fail (str "[Parser Error] Unbalanced " <description> ".")) + (&/fail-with-loc (str "[Parser Error] Unbalanced " <description> ".")) ))) ^:private parse-form &lexer/$Close_Paren "parantheses" &/$FormS @@ -56,10 +56,10 @@ [meta (&lexer/$Close_Brace _)] (if (even? (&/|length elems)) (return (&/$RecordS (&/|as-pairs elems))) - (fail (str base-uneven-record-error))) + (&/fail-with-loc base-uneven-record-error)) _ - (fail (str "[Parser Error] Unbalanced braces.")) + (&/fail-with-loc "[Parser Error] Unbalanced braces.") ))) ;; [Interface] @@ -107,5 +107,5 @@ (return (&/|list (&/T [meta syntax])))) _ - (fail "[Parser Error] Unknown lexer token.") + (&/fail-with-loc "[Parser Error] Unknown lexer token.") ))) diff --git a/src/lux/reader.clj b/src/lux/reader.clj index b81b7c826..5a7734061 100644 --- a/src/lux/reader.clj +++ b/src/lux/reader.clj @@ -7,7 +7,7 @@ (:require [clojure.string :as string] clojure.core.match clojure.core.match.array - [lux.base :as & :refer [defvariant |do return* return fail fail* |let |case]])) + [lux.base :as & :refer [defvariant |do return* return fail* |let |case]])) ;; [Tags] (defvariant @@ -26,7 +26,7 @@ more) (|case (body file-name line-num column-num line) ($No msg) - (fail* msg) + ((&/fail-with-loc msg) state) ($Done output) (return* (&/set$ &/$source more state) @@ -45,7 +45,7 @@ match) (&/$Left msg) - (fail* msg) + ((&/fail-with-loc msg) state) ))) (defn ^:private re-find! [^java.util.regex.Pattern regex column ^String line] diff --git a/src/lux/repl.clj b/src/lux/repl.clj index ce835ff5e..0c4202650 100644 --- a/src/lux/repl.clj +++ b/src/lux/repl.clj @@ -75,7 +75,7 @@ (&/$Left ^String ?message) (if (or (= "[Reader Error] EOF" ?message) - (.startsWith ?message "[Parser Error] Unbalanced ")) + (.contains ?message "[Parser Error] Unbalanced ")) (recur state* (inc repl-line)) (do (println ?message) (recur state (inc repl-line)))) |