guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: help with peg module and character classes


From: Matt Wette
Subject: Re: help with peg module and character classes
Date: Fri, 14 Feb 2020 17:13:48 -0800
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1

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.



I got farther.  For an input string of

(define global1
  "cc_library {
    name: \"libcutils\",
    vendor_available: true,
    vndk: {
        enabled: true,
        support_system_process: true,
    },
    recovery_available: true,
    srcs: [
        \"config_utils.cpp\",
        \"canned_fs_config.cpp\",
        \"iosched_policy.cpp\",
    ],
   }")

the program below generates

((key "cc_library")
 (object-val
   (key-val
     (key "name")
     (val (string-val "\"libcutils\"")))
   ((key-val
      (key "vendor_available")
      (val (bool-val "true")))
    (key-val
      (key "vndk")
      (val (object-val
             (key-val (key "enabled") (val (bool-val "true")))
             (key-val
               (key "support_system_process")
               (val (bool-val "true"))))))
    (key-val
      (key "recovery_available")
      (val (bool-val "true")))
    (key-val
      (key "srcs")
      (val (array-val
             (val (string-val "\"config_utils.cpp\""))
             ((val (string-val "\"canned_fs_config.cpp\""))
              (val (string-val "\"iosched_policy.cpp\"")))))))))



(use-modules (ice-9 peg))
(use-modules (ice-9 pretty-print))

(define-peg-pattern ws none (+ (or " " "\n")))

(define-peg-pattern comma none  (and (? ws) ","))

(define-peg-pattern colon none (and (? ws) ":"))

(define-peg-pattern obj< none "{")
(define-peg-pattern >obj none "}")

(define-peg-pattern vec< none "[")
(define-peg-pattern >vec none "]")

(define-peg-pattern c-comment none
  ;;(and "/*" (* (and (not-followed-by "*/") peg-any)) "*/"))
  (and "//" (* (and (not-followed-by "\n") peg-any)) "\n"))

(define-peg-pattern key all
  (and (? ws)
       (? (and c-comment (? ws)))
       (+ (or (range #\a #\z) "_"))))

(define-peg-pattern bool-val all
  (or "true" "false"))

(define-peg-pattern int-val all
  (and (? "-") (+ (range #\0 #\9))))

(define-peg-pattern string-val all
  (and "\"" (* (and (not-followed-by "\"") (or "\\\"" peg-any))) "\""))

(define-peg-pattern object-val all
  ;; one or more elements
  (and obj< key-val (* (and comma key-val)) (? comma) (? ws) >obj))

(define-peg-pattern array-val all
  ;; zero or more elements
  (or
   (and vec< (? ws) >vec)
   (and vec< (? ws) val (* (and comma (? ws) val)) (? comma) (? ws) >vec)))

(define-peg-pattern val all
  (or bool-val int-val string-val object-val array-val))

(define-peg-pattern key-val all
  (and key colon (? ws) val))

(define-peg-pattern bpfile body
  (and key ws object-val))


(define global1
  "cc_library {
    name: \"libcutils\",
    vendor_available: true,
    vndk: {
        enabled: true,
        support_system_process: true,
    },
    recovery_available: true,
    srcs: [
        \"config_utils.cpp\",
        \"canned_fs_config.cpp\",
        \"iosched_policy.cpp\",
    ],
   }")

(define *global* global1)

(define (doit)
  (let ((tree (peg:tree (match-pattern bpfile *global*)))
    )
    (pretty-print tree)))





reply via email to

[Prev in Thread] Current Thread [Next in Thread]