Parsing 使用amotoen解析问题

Parsing 使用amotoen解析问题,parsing,clojure,peg,Parsing,Clojure,Peg,我正在尝试编写一个语法来解析一个简单的语言来描述鼓循环,使用Clojure和。语言如下所示:—— # 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: /--------------- # t

我正在尝试编写一个语法来解析一个简单的语言来描述鼓循环,使用Clojure和。语言如下所示:——

# 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
我对语法的定义如下:——

(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
时,它们被正确解析。例如:——

(pprint
  (a/pegasus
    :Body
    g
    (a/wrap-string
      "Body:\n
        BD: /---/---/---/---\n
        SD: ---/--/--/-/--/-\n
       CHH: --/---/---/---/-\n
       CSH: /---------------\n")))
然而,当我调用
(pprint(a/pegasus:documentg(a/wrap string(slurp“sample.orc”)))
,我得到的只是
nil
。类似地,如果我将
(a/wrap string(slurp“sample.orc”)
替换为包含
sample.orc
中文本的字符串

所以,我的问题是:有人能发现我的语法有什么问题吗?我已经没有主意了,我已经盯着它看了几天了。我肯定这是件让人尴尬的简单事情,但我就是看不见


提前感谢。

示例规则使用
:Body
:File
可以为空,输入结束应标记为
:$
而不是
\$
。这里有一条经过修改的语法:

(def g                                                                                                                                                                  
  {                                                                                                                                                                     
   :Whitespace '(| \space \tab \, :Comment)                                                                                                                             
   :n* '(* (| \newline :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) (* (% \newline))]                                                                                                                               
   :Body [\B \o \d \y \: \newline '(* :Pattern)]                                                                                                                        
   :Pattern [:_* :Name \: :_* '(* (| \/ \-)) '(| \newline :$)]                                                                                                          
   :Document [:n* :BPM :n* :Samples :n* :Body :n* :$]                                                                                                                   
   })

;; sample.orc contains the example input from the question text
(pprint (a/pegasus :Document g (a/wrap-string (slurp "sample.orc"))))

;; output:
{:Document
 [{:n*
   ({:Comment [\# (\space \T \e \s \t \space \L \o \o \p) \newline]}
    {:Comment
     [\#
      (\space
       \t
       \h
       \i
       \s
       \space
       \i
       \s
       \space
       \a
       \space
       \c
       \o
       \m
       \m
       \e
       \n
       \t)
      \newline]}
    \newline)}
  {:BPM
   [\B
    \P
    \M
    \:
    {:_* {:Whitespace \space}}
    {:Number ({:Digit \1} {:Digit \0} {:Digit \0})}
    \newline]}
  {:n* \newline}
  {:Samples
   [\S
    \a
    \m
    \p
    \l
    \e
    \s
    \:
    \newline
    ({:SampleDef
      [{:_* {:Whitespace \space}}
       {:Name [\B \D]}
       \:
       {:_* {:Whitespace \space}}
       {:File [\b (\d \. \w \a \v)]}
       \newline]}
     {:SampleDef
      [{:_* {:Whitespace \space}}
       {:Name [\S \D]}
       \:
       {:_* {:Whitespace \space}}
       {:File [\s (\d \. \w \a \v)]}
       \newline]}
     {:SampleDef
      [{:_* ()}
       {:Name [\C (\H \H)]}
       \:
       {:_* {:Whitespace \space}}
       {:File [\c (\h \h \. \w \a \v)]}
       \newline]}
     {:SampleDef
      [{:_* ()}
       {:Name [\C (\S \H)]}
       \:
       {:_* {:Whitespace \space}}
       {:File [\c (\s \h \. \w \a \v)]}
       \newline]})]}
  {:n* \newline}
  {:Body
   [\B
    \o
    \d
    \y
    \:
    \newline
    ({:Pattern
      [{:_* {:Whitespace \space}}
       {:Name [\B \D]}
       \:
       {:_* {:Whitespace \space}}
       (\/ \- \- \- \/ \- \- \- \/ \- \- \- \/ \- \- \-)
       \newline]}
     {:Pattern
      [{:_* {:Whitespace \space}}
       {:Name [\S \D]}
       \:
       {:_* {:Whitespace \space}}
       (\- \- \- \/ \- \- \/ \- \- \/ \- \/ \- \- \/ \-)
       \newline]}
     {:Pattern
      [{:_* ()}
       {:Name [\C (\H \H)]}
       \:
       {:_* {:Whitespace \space}}
       (\- \- \/ \- \- \- \/ \- \- \- \/ \- \- \- \/ \-)
       \newline]}
     {:Pattern
      [{:_* ()}
       {:Name [\C (\S \H)]}
       \:
       {:_* {:Whitespace \space}}
       (\/ \- \- \- \- \- \- \- \- \- \- \- \- \- \- \-)
       \newline]})]}
  {:n*
   (\newline
    {:Comment
     [\#
      (\space
       \t
       \h
       \i
       \s
       \space
       \i
       \s
       \space
       \a
       \n
       \o
       \t
       \h
       \e
       \r
       \space
       \c
       \o
       \m
       \m
       \e
       \n
       \t)
      \newline]})}
  :$]}