From 1c559733d5135d302cc54c28559806daf26885f0 Mon Sep 17 00:00:00 2001 From: "Steinar V. Kaldager" Date: Thu, 28 Jan 2016 10:28:19 +0000 Subject: [PATCH] improve error for parsing from a nonexistent lhs --- src/instaparse/gll.clj | 5 +++++ test/instaparse/core_test.clj | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/src/instaparse/gll.clj b/src/instaparse/gll.clj index a95bea8..360bb22 100644 --- a/src/instaparse/gll.clj +++ b/src/instaparse/gll.clj @@ -872,6 +872,11 @@ ;; Parsing functions (defn start-parser [tramp parser partial?] + (let [starting-prod (:keyword parser) + known-productions (into #{} (map first (:grammar tramp)))] + (when (not (known-productions starting-prod)) + (throw (IllegalArgumentException. + (str "Nonexistent starting production: " starting-prod))))) (if partial? (push-listener tramp [0 parser] (TopListener tramp)) (push-full-listener tramp [0 parser] (TopListener tramp)))) diff --git a/test/instaparse/core_test.clj b/test/instaparse/core_test.clj index bf6cafd..8775591 100644 --- a/test/instaparse/core_test.clj +++ b/test/instaparse/core_test.clj @@ -684,6 +684,15 @@ 15 )) +(deftest error-nonexistent-starting-production + (let [parser (insta/parser "cat = 'mjau'")] + (is (thrown-with-msg? + IllegalArgumentException + #"Nonexistent starting production: :dog" + (insta/parse parser "mjau" :start :dog))) + (is (= [:cat "mjau"] + (insta/parse parser "mjau" :start :cat))))) + (defn round-trip [parser] (insta/parser (prn-str parser)))