Skip to content

Instantly share code, notes, and snippets.

@qerub
Created May 12, 2011 19:53
Show Gist options
  • Select an option

  • Save qerub/969308 to your computer and use it in GitHub Desktop.

Select an option

Save qerub/969308 to your computer and use it in GitHub Desktop.

Revisions

  1. qerub revised this gist May 27, 2011. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions gistfile1.sls
    Original file line number Diff line number Diff line change
    @@ -9,18 +9,21 @@
    (if (stx-list? stx)
    (let ((stx* (stx-map rewrite-method-calls stx)))
    (if (eq? (syntax-property stx 'paren-shape) #\[)
    #`(send #,stx*)
    (match (stx->list stx*)
    ((list obj msg args ...)
    #`(send #,obj '#,msg #,@args)))
    stx*))
    stx))

    (define (objective-r-read . args)
    (let ((stx (apply read-syntax #f args)))
    (let ((stx (apply objective-r-read-syntax #f args)))
    (if (eof-object? stx) stx (syntax->datum stx))))

    (define (objective-r-read-syntax . args)
    ; TODO: Do something with the rest of the arguments
    (rewrite-method-calls (read-syntax (first args) (second args))))

    ; Example:

    (read (open-input-string "[[document get-element-by-id stuff] remove]"))
    ; => (send (send document get-element-by-id stuff) remove)
    (objective-r-read (open-input-string "[[document get-element-by-id stuff] remove]"))
    ; => (send (send document 'get-element-by-id stuff) 'remove)
  2. qerub revised this gist May 25, 2011. 1 changed file with 6 additions and 12 deletions.
    18 changes: 6 additions & 12 deletions gistfile1.sls
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,10 @@
    #lang racket
    #lang racket ; Requires Racket >= 5.1.1

    (provide read read-syntax)

    (require (rename-in racket/base (read vanilla-read)
    (read-syntax vanilla-read-syntax)))
    (provide (rename-out (objective-r-read read)
    (objective-r-read-syntax read-syntax)))

    (require syntax/stx)

    ; Not available in Racket 5.1 :(
    (define (stx-map proc stxl)
    (datum->syntax stxl (map proc (stx->list stxl))))

    (define (rewrite-method-calls stx)
    (if (stx-list? stx)
    (let ((stx* (stx-map rewrite-method-calls stx)))
    @@ -19,12 +13,12 @@
    stx*))
    stx))

    (define (read . args)
    (define (objective-r-read . args)
    (let ((stx (apply read-syntax #f args)))
    (if (eof-object? stx) stx (syntax->datum stx))))

    (define (read-syntax . args)
    (rewrite-method-calls (vanilla-read-syntax (first args) (second args))))
    (define (objective-r-read-syntax . args)
    (rewrite-method-calls (read-syntax (first args) (second args))))

    ; Example:

  3. qerub revised this gist May 24, 2011. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion gistfile1.sls
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,8 @@
    (let ((stx (apply read-syntax #f args)))
    (if (eof-object? stx) stx (syntax->datum stx))))

    (define read-syntax (compose rewrite-method-calls vanilla-read-syntax))
    (define (read-syntax . args)
    (rewrite-method-calls (vanilla-read-syntax (first args) (second args))))

    ; Example:

  4. qerub revised this gist May 24, 2011. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions gistfile1.sls
    Original file line number Diff line number Diff line change
    @@ -11,14 +11,11 @@
    (define (stx-map proc stxl)
    (datum->syntax stxl (map proc (stx->list stxl))))

    (define (stx-cons stx stxl)
    (datum->syntax stx (cons stx stxl)))

    (define (rewrite-method-calls stx)
    (if (stx-list? stx)
    (let ((stx* (stx-map rewrite-method-calls stx)))
    (if (eq? (syntax-property stx 'paren-shape) #\[)
    (stx-cons #'send stx*)
    #`(send #,stx*)
    stx*))
    stx))

  5. qerub revised this gist May 24, 2011. 1 changed file with 23 additions and 19 deletions.
    42 changes: 23 additions & 19 deletions gistfile1.sls
    Original file line number Diff line number Diff line change
    @@ -1,30 +1,34 @@
    #lang racket

    ; TODO: Add support for nested method calls
    (provide read read-syntax)

    (require (rename-in racket/base (read vanilla-read)
    (read-syntax vanilla-read-syntax)))

    (require syntax/stx)

    (provide read read-syntax)
    ; Not available in Racket 5.1 :(
    (define (stx-map proc stxl)
    (datum->syntax stxl (map proc (stx->list stxl))))

    (define (stx-cons stx stxl)
    (datum->syntax stx (cons stx stxl)))

    (define (rewrite-method-calls stx)
    (if (stx-list? stx)
    (let ((stx* (stx-map rewrite-method-calls stx)))
    (if (eq? (syntax-property stx 'paren-shape) #\[)
    (stx-cons #'send stx*)
    stx*))
    stx))

    (define (read in)
    (let ((stx (read-syntax #f in)))
    (define (read . args)
    (let ((stx (apply read-syntax #f args)))
    (if (eof-object? stx) stx (syntax->datum stx))))

    (define (read-syntax src in)
    (read-syntax/recursive src in #f (objective-r-readtable)))

    (define (objective-r-readtable)
    (define (handle-method-call ch in src line col pos)
    (rewrite (read-syntax/recursive (object-name in) in #\[ #f)))

    (define (rewrite stx)
    (match (stx->list stx)
    ((list obj message args ...)
    #`(send #,obj '#,message #,@args))))

    (make-readtable (current-readtable)
    #\[ 'terminating-macro handle-method-call))
    (define read-syntax (compose rewrite-method-calls vanilla-read-syntax))

    ; Example:

    (read (open-input-string "[[document get-element-by-id stuff] remove]"))
    (read (open-input-string "[[document get-element-by-id stuff] remove]"))
    ; => (send (send document get-element-by-id stuff) remove)
  6. qerub created this gist May 12, 2011.
    30 changes: 30 additions & 0 deletions gistfile1.sls
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #lang racket

    ; TODO: Add support for nested method calls

    (require syntax/stx)

    (provide read read-syntax)

    (define (read in)
    (let ((stx (read-syntax #f in)))
    (if (eof-object? stx) stx (syntax->datum stx))))

    (define (read-syntax src in)
    (read-syntax/recursive src in #f (objective-r-readtable)))

    (define (objective-r-readtable)
    (define (handle-method-call ch in src line col pos)
    (rewrite (read-syntax/recursive (object-name in) in #\[ #f)))

    (define (rewrite stx)
    (match (stx->list stx)
    ((list obj message args ...)
    #`(send #,obj '#,message #,@args))))

    (make-readtable (current-readtable)
    #\[ 'terminating-macro handle-method-call))

    ; Example:

    (read (open-input-string "[[document get-element-by-id stuff] remove]"))