Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (def tag-query-grammar
- ~{:space (opt (some " "))
- :escape (sequence "\\" 1)
- :quoted-value (sequence "\"" (any (choice :escape (if-not "\"" 1))) "\"")
- :unquoted-value (some (choice :escape (range "az") (range "AZ") (range "09") (set "!$%&+*-./:<?>@^_|")))
- :value (cmt (capture (choice :quoted-value :unquoted-value)) ,unescape)
- :tag-op (choice "!=" "=")
- :tag-assertion
- (cmt
- (sequence
- :value
- :space
- (opt
- (sequence
- (capture :tag-op)
- :space
- (choice
- :value
- ,(parse-error "expected value after '='")))))
- ,make-tag-assertion)
- :paren-expr (sequence "(" :query :space ")")
- :base-expr (choice :tag-assertion :paren-expr)
- :or-expr ,(make-binop-rule "or" :or-expr :base-expr)
- :and-expr ,(make-binop-rule "and" :and-expr :or-expr)
- :query (sequence :space :and-expr)
- :main (sequence :query :space (opt (sequence 1 ,(parse-error "unexpected input after query end"))))})
- (def parser
- (peg/compile tag-query-grammar))
- (defn parse
- [input]
- (try
- [:ok (first (peg/match parser input))]
- ([err f]
- (when (not (struct? err))
- (propagate err f))
- [:error err])))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement