On 2/13/20 11:37 AM, Malte Frank Gerdes wrote:
Hey guile-user,
i'm currently trying to use the peg module to parse android bp files. I
have never used pegs before so i might have done an obvious mistake.
Maybe start with something simple. I hacked this today. First PEG
program.
Though I prefer S-expression format. This just parses key : "value"
with
optional comments.
(use-modules (ice-9 peg))
(use-modules (ice-9 pretty-print))
(define-peg-pattern ws none
(+ (or " " "\n")))
(define-peg-pattern c-comment none
(and "/*" (* (and (not-followed-by "*/") peg-any)) "*/"))
(define-peg-pattern key all
(and (? ws)
(? (and c-comment (? ws)))
(+ (or (range #\a #\z) "_"))))
(define-peg-pattern string-val all
(and "\"" (* (and (not-followed-by "\"") (or "\\\"" peg-any))) "\""))
(define-peg-pattern object-val all
(and "{" (* key-val) "}"))
(define-peg-pattern key-val body
(and key (? ws) ":" (? ws) string-val))
;; (peg:tree (match-pattern key-val "abc : \"def\""))
;; => ((key "abc") ":" (string-val "\"def\""))