我正在尝试使用 Clojure 和amotoen编写语法来解析一种简单的语言来描述鼓循环。语言看起来像这样:–</p>
# Test Loop
# this is a comment
BPM: 100
Samples:
BD: bd.wav
SD: sd.wav
CHH: chh.wav
CSH: csh.wav
Body:
BD: /---/---/---/---
SD: ---/--/--/-/--/-
CHH: --/---/---/---/-
CSH: /---------------
# this is another comment
我将语法定义如下:–</p>
(def g {
:Whitespace '(| \space \newline \tab \, :Comment)
:_* '(* :Whitespace)
:_ [:Whitespace '(* :Whitespace)]
:Comment [\# '(* (% \newline)) \newline]
:BPM [\B \P \M \: :_* '(* :Number) \newline]
:Number '(* :Digit)
:Digit (a/lpegs '| "0123456789")
:Samples [\S \a \m \p \l \e \s \: \newline '(* :SampleDef)]
:SampleDef [:_* :Name \: :_* :File \newline]
:Name '(* (% \:))
:File '(* (% \newline))
:Body [\B \o \d \y \: \newline '(* :Pattern)]
:Pattern [:_* :Name \: :_* '(* (| \/ \-)) '(| \newline \$)]
:Document [:_* :BPM :_* :Samples :_* :Body :_* \$]
})
当我单独调用pegasus
示例文件的每个部分时,它们会被正确解析。例如:--</p>
(pprint
(a/pegasus
:Body
g
(a/wrap-string
"Body:\n
BD: /---/---/---/---\n
SD: ---/--/--/-/--/-\n
CHH: --/---/---/---/-\n
CSH: /---------------\n")))
但是,当我打电话时(pprint (a/pegasus :Document g (a/wrap-string (slurp "sample.orc"))))
,我得到的只是nil
。同样,如果我替换(a/wrap-string (slurp "sample.orc"))
为包含sample.orc
.
所以,我的问题是:谁能发现我的语法有什么问题?我完全没有想法,我已经盯着它看了几天了。我敢肯定这是一件令人尴尬的简单事情,但我就是看不到!
提前致谢。