Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. (def tag-query-grammar
  2. ~{:space (opt (some " "))
  3. :escape (sequence "\\" 1)
  4. :quoted-value (sequence "\"" (any (choice :escape (if-not "\"" 1))) "\"")
  5. :unquoted-value (some (choice :escape (range "az") (range "AZ") (range "09") (set "!$%&+*-./:<?>@^_|")))
  6. :value (cmt (capture (choice :quoted-value :unquoted-value)) ,unescape)
  7. :tag-op (choice "!=" "=")
  8. :tag-assertion
  9. (cmt
  10. (sequence
  11. :value
  12. :space
  13. (opt
  14. (sequence
  15. (capture :tag-op)
  16. :space
  17. (choice
  18. :value
  19. ,(parse-error "expected value after '='")))))
  20. ,make-tag-assertion)
  21. :paren-expr (sequence "(" :query :space ")")
  22. :base-expr (choice :tag-assertion :paren-expr)
  23. :or-expr ,(make-binop-rule "or" :or-expr :base-expr)
  24. :and-expr ,(make-binop-rule "and" :and-expr :or-expr)
  25. :query (sequence :space :and-expr)
  26. :main (sequence :query :space (opt (sequence 1 ,(parse-error "unexpected input after query end"))))})
  27.  
  28. (def parser
  29. (peg/compile tag-query-grammar))
  30.  
  31. (defn parse
  32. [input]
  33. (try
  34. [:ok (first (peg/match parser input))]
  35. ([err f]
  36. (when (not (struct? err))
  37. (propagate err f))
  38. [:error err])))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement