Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojure 在Instaparse中以字符串形式获取错误消息的最简单方法?_Clojure_Instaparse - Fatal编程技术网

Clojure 在Instaparse中以字符串形式获取错误消息的最简单方法?

Clojure 在Instaparse中以字符串形式获取错误消息的最简单方法?,clojure,instaparse,Clojure,Instaparse,可以将nice错误消息复制到REPL => (negative-lookahead-example "abaaaab") Parse error at line 1, column 1: abaaaab ^ Expected: NOT "ab" 但是我找不到一个内置函数来获取字符串形式的消息。如何做到这一点?您可以始终使用包装它,而不使用str: (with-out-str (negative-lookahead-example "abaaaab")) 您可能还对将与err st

可以将nice错误消息复制到REPL

=> (negative-lookahead-example "abaaaab")
Parse error at line 1, column 1:
abaaaab
^
Expected:
NOT "ab"

但是我找不到一个内置函数来获取字符串形式的消息。如何做到这一点?

您可以始终使用
包装它,而不使用str

(with-out-str 
  (negative-lookahead-example "abaaaab"))
您可能还对将
与err str一起使用感兴趣


我不记得instaparse是否写入stdout或stderr,但其中一个可以满足您的需要。

您可以使用
包装它,而不使用str

(with-out-str 
  (negative-lookahead-example "abaaaab"))
您可能还对将
与err str一起使用感兴趣


我不记得
instaparse
是否写入到stdout或stderr,但其中一个可以满足您的需要。

让我们看看失败情况下
parse
的返回类型:

(p/parse (p/parser "S = 'x'") "y")
=> Parse error at line 1, column 1:
y
^
Expected:
"x" (followed by end-of-string)

(class *1)
=> instaparse.gll.Failure
这种漂亮的打印行为在Instaparse中定义如下:

(defrecord Failure [index reason])  
(defmethod clojure.core/print-method Failure [x writer]
  (binding [*out* writer]
    (fail/pprint-failure x)))
在REPL中,这是一个有用的人类可读描述,但也可以将其视为地图:

(keys (p/parse (p/parser "S = 'x'") "y"))
=> (:index :reason :line :column :text)
(:reason (p/parse (p/parser "S = 'x'") "y"))
=> [{:tag :string, :expecting "x", :full true}]
你可以这样做:

(with-out-str
  (instaparse.failure/pprint-failure
    (p/parse (p/parser "S = 'x'") "y")))
=> "Parse error at line 1, column 1:\ny\n^\nExpected:\n\"x\" (followed by end-of-string)\n"

或者编写自己版本的
pprint failure
,生成一个字符串而不是打印它。

让我们看看失败情况下
parse
的返回类型:

(p/parse (p/parser "S = 'x'") "y")
=> Parse error at line 1, column 1:
y
^
Expected:
"x" (followed by end-of-string)

(class *1)
=> instaparse.gll.Failure
这种漂亮的打印行为在Instaparse中定义如下:

(defrecord Failure [index reason])  
(defmethod clojure.core/print-method Failure [x writer]
  (binding [*out* writer]
    (fail/pprint-failure x)))
在REPL中,这是一个有用的人类可读描述,但也可以将其视为地图:

(keys (p/parse (p/parser "S = 'x'") "y"))
=> (:index :reason :line :column :text)
(:reason (p/parse (p/parser "S = 'x'") "y"))
=> [{:tag :string, :expecting "x", :full true}]
你可以这样做:

(with-out-str
  (instaparse.failure/pprint-failure
    (p/parse (p/parser "S = 'x'") "y")))
=> "Parse error at line 1, column 1:\ny\n^\nExpected:\n\"x\" (followed by end-of-string)\n"
或者编写自己版本的
pprint failure
,生成字符串而不是打印它