Instaparse: there's an error but it is not reported -
i trying build grammar instaparse. quite find code fails first assertion, emitting "empty list":
(defn parse-it [] (let [parser (insta/parser ebnf) res (insta/parses parser input) _ (assert (seq res) (str "empty list")) choices (count res) _ (assert (= choices 1))] (first res)))
i resolve problem, involves trial , error. there way error can pinpointed?
an example of fixing problem removing trailing spaces file becomes input
in above code.
edit
based on stefan's answer i've changed code:
(defn parse-it [] (let [my-parser (insta/parser ebnf) xs (insta/parses my-parser input) num-choices (count xs) msg (cond (zero? num-choices) "insta/parses might able corrected" (> num-choices 1) (str "insta/parses shows more 1 way parse: " num-choices) (= 1 num-choices) "insta/parses shows 1 choice, good") res (cond ;; fix there being more 1 rather show them ;(> num-choices 1) xs (= num-choices 1) (first xs) (zero? num-choices) (insta/parse my-parser input)) _ (assert res (str "no result. num of choices is: " num-choices))] [msg res]))
the above code trick: pinpointed answer. me not obvious after insta/parses
returns empty list, insta/parse
needs called error information. using parse errors documentation result in better code above. shows how error information there in metadata, , how retrieve - answer question in documentation!
when use parser
on input instead of going through insta/parses
, prints pretty exact message of error @ repl.
an example:
(def ebnf "expr = dot = 'a' dot = '.'") user> ((insta/parser ebnf) "a.") [:expr [:a "a"] [:dot "."]] user> ((insta/parser ebnf) "b.") parse error @ line 1, column 1: b. ^ expected: "a"
Comments
Post a Comment