guile-devel
[Top][All Lists]
Advanced

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

guile-syntax-parse


From: Stefan Israelsson Tampe
Subject: guile-syntax-parse
Date: Mon, 9 May 2011 14:11:28 +0200

Hi, check out guile-syntax-parse at
https://gitorious.org/guile-syntax-parse/guile-syntax-parse

(You have to patch some ice-9 routines to make this work (syntax-_expression_ and syntax-object? need to be in the environment - the files are included)

So by writing

#|
Syntax parse examples
|#
(use-modules (parse syntax-parse))
(use-modules (parse match-parse))


(define <list2>
  (<parse-list>
   #t                           ;; signal error if  #t else record error if #f

   ;; error msg if not a list
   "in let decl, (symbol val) malformed"   

   ;; error msg in case of wrong number of elements
   "list must have length 2"   

   2))                          ;; the length of the list


(define <arg2>
  (<parse-list..>
   #t                          
   "not a list Bug! args are a list"   
   "either * and/or code1 is missing in:~%   (let * code1 code ...)"
   3))


(define <list>
  (<parse-list..>
   #t                          
   "declaration form in let is not a list"                
   "Bug if this is printed"    
   ))                         

;; here is an example of designing a matcher out of pieces
(define <arg2..>
  (lambda (x)
    (match-parse -abs ((<arg2> x) (<list> y))  ;; matchers needs declaration
                 x

          ;; if 2 args followed by a list of 0 ... elements then success
          ((<arg2> <list>) (list #t))            
          (_               (error "Bug in let macro")))))
          
;; here is a simple checked let form

;; Using the symbol tool
(define <sym> (<symbol> "variable decl in let"))

;;; THE MAIN MACRO
(define-syntax my-let
  (syntax-parse ()
     ((_ (((s : <sym>) v : <list2>) ... : <list>)
         code2 ... : <arg2..> )
      (let ((s v) ...) code2 ...))))


We get, (line number buggs still in the codebase)
.............................................................................................
scheme@(guile-user)> (my-let a)
While compiling _expression_:
ERROR: error at 1, either * and/or code1 is missing in:
   (let * code1 code ...)

scheme@(guile-user)> (my-let a 1)
While compiling _expression_:
ERROR: error at -1, declaration form in let is not a list

scheme@(guile-user)> (my-let (a 1) a)
While compiling _expression_:
ERROR: error at -1, in let decl, (symbol val) malformed

scheme@(guile-user)> (my-let ((1 1)) a)
While compiling _expression_:
ERROR: error at 4, variable decl in let, not a symbol

scheme@(guile-user)> (my-let ((a 1)) a)
$1 = 1

scheme@(guile-user)> (my-let ((a 1)) 1 a)
$2 = 1

scheme@(guile-user)> (my-let ((a 1) b) 1 a)
While compiling _expression_:
ERROR: error at -1, in let decl, (symbol val) malformed
.............................................................................................

We should be able to use the Parser code-base in stead of my match macro and then this is really a small setup.

/Stefan

reply via email to

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