[plt-scheme] Macro Question

From: Edwin Zacharias (ehz4 at yahoo.com)
Date: Fri Apr 4 16:58:51 EST 2003

I'm trying to write a parser that uses simple functions such as (fixed 4) that
would return 4 characters from a string and increase a cursor by 4.  My
current implementation works, but it's very unsafe and it's difficult to
Below is the module and the test module.  Is there a way to do this using a
safer syntax transformation?  I've tried things like nested macros, but
haven't been able to get it to work.

- Edwin

;;  Parses a byte-string using functions that adjust a cursor as they work.  

(module parser mzscheme
  (require (lib "defmacro.ss")
           ;; A grint is a variable length integer, like a UTF-8 charachter.
  (provide parse
           (all-from "grint.ss"))

  ;; Defines the variables and the parsing functions.
  (define-macro (parse input . body)
    `(let* ((cursor 0)
            (byte-string ,input)
            (fixed (lambda (length)
                     (set! cursor (+ cursor length))
                     (substring byte-string (- cursor length) cursor)))
            (grint (lambda ()
                     (let ((result (string->grint byte-string cursor)))
                       (set! cursor (+ cursor (string->grint-length byte-string
       , at body)))


(module parser-test mzscheme
  (require "parser.ss")

  ;; returns "he"
  (define (test-0)
    (parse "hello"
           (fixed 2)))

  ;; returns (response "abcd" "Hello")
  (define (test-1)
    (parse "\2abcd\5Hello"     

      ;; I can't define these outside parse.
      (define (data) (fixed (grint)))
      (define (message-id) (fixed 4))  
      (case (grint)
        ((0) 'info)
        ((1) (list 'echo (data)))
        ((2) (list 'response (message-id) (data)))))))

Do you Yahoo!?
Yahoo! Tax Center - File online, calculators, forms, and more

Posted on the users mailing list.