[Top][All Lists]

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

[Guile-commits] 01/07: Refactor PEG records

From: Andy Wingo
Subject: [Guile-commits] 01/07: Refactor PEG records
Date: Tue, 22 Oct 2019 10:23:20 -0400 (EDT)

wingo pushed a commit to branch master
in repository guile.

commit f152d7cd961aa1637e6e71e99141ec0617e6f1ed
Author: Andy Wingo <address@hidden>
Date:   Mon Oct 21 16:51:08 2019 +0200

    Refactor PEG records
    * module/ice-9/peg/using-parsers.scm (prec): Use SRFI-9 to define PEG
      record types.
      (peg:start, peg:end, peg:string, peg:tree, peg:substring): Implement
      in a more efficient way.
 module/ice-9/peg/using-parsers.scm | 41 +++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 21 deletions(-)

diff --git a/module/ice-9/peg/using-parsers.scm 
index 076de29..359527c 100644
--- a/module/ice-9/peg/using-parsers.scm
+++ b/module/ice-9/peg/using-parsers.scm
@@ -21,10 +21,30 @@
   #:use-module (ice-9 peg simplify-tree)
   #:use-module (ice-9 peg codegen)
   #:use-module (ice-9 peg cache)
+  #:use-module (srfi srfi-9)
   #:export (match-pattern define-peg-pattern search-for-pattern
             prec make-prec peg:start peg:end peg:string
             peg:tree peg:substring peg-record?))
+(define-record-type prec
+  (make-prec start end string tree)
+  peg-record?
+  (start prec-start)
+  (end prec-end)
+  (string prec-string)
+  (tree prec-tree))
+(define (peg:start pm)
+  (and pm (prec-start pm)))
+(define (peg:end pm)
+  (and pm (prec-end pm)))
+(define (peg:string pm)
+  (and pm (prec-string pm)))
+(define (peg:tree pm)
+  (and pm (prec-tree pm)))
+(define (peg:substring pm)
+  (and pm (substring (prec-string pm) (prec-start pm) (prec-end pm))))
 ;;; Helper Macros
@@ -93,24 +113,3 @@ execute the STMTs and try again."
                         at end string
                         (string-collapse match))))))))))))
-;; Pretty self-explanatory.
-(define prec
-  (make-record-type "peg" '(start end string tree)))
-(define make-prec
-  (record-constructor prec '(start end string tree)))
-(define (peg:start pm)
-  (if pm ((record-accessor prec 'start) pm) #f))
-(define (peg:end pm)
-  (if pm ((record-accessor prec 'end) pm) #f))
-(define (peg:string pm)
-  (if pm ((record-accessor prec 'string) pm) #f))
-(define (peg:tree pm)
-  (if pm ((record-accessor prec 'tree) pm) #f))
-(define (peg:substring pm)
-  (if pm (substring (peg:string pm) (peg:start pm) (peg:end pm)) #f))
-(define peg-record? (record-predicate prec))

reply via email to

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