PasteRack.org
Paste # 51341
2015-08-29 14:57:06

Fork as a new paste.

Paste viewed 235 times.


Embed:

  1. #lang racket
  2. (require syntax/parse (for-syntax syntax/parse))
  3.  
  4. (syntax-parse #'(1 + 1)
  5.   [((~and (~not (~datum |,|)) x) ...)
  6.    (list #'(x ...))]
  7.   [((~and (~not (~datum |,|)) x) ... (~datum |,|) xs ...)
  8.    (cons #'(x ...) #'(xs ...))])
  9. ;;this 'works', for a given definition of 'works'.
  10. (define-syntax (split/delim stx)
  11.   (syntax-case stx ()
  12.     [(_ delim stx0)
  13.      #'(syntax-parse stx0
  14.          [((~and (~not (~datum delim)) x) (... ...))
  15.           (list #'(x (... ...)))]
  16.          [((~and (~not (~datum delim)) x) (... ...) (~datum delim) xs (... ...))
  17.           (cons #'(x (... ...)) #'(xs (... ...)))])]))
  18.  
  19. ;;this consumes all available memory and CPU when applied.
  20. #;
  21. (define-syntax (split/delim stx)
  22.   (syntax-case stx ()
  23.     [(_ delim stx0)
  24.      #'(syntax-parse stx0
  25.          [((~and (~not (~datum delim)) x) (... ...))
  26.           (list #'(x (... ...)))]
  27.          [((~and (~not (~datum delim)) x) (... ...) (~datum delim) xs (... ...))
  28.           (cons #'(x (... ...)) (split/delim delim #'(xs (... ...))))])]))
  29.  
  30.  
  31. (parse/delim |,| #'(1 + 1 |,| 5 + 1))
  32. (parse/delim |,| #'(1 + 1))
  33. (parse/delim |,| #'(1 + 1 |,| 3 * 5 |,| 25))

=>

'(#<syntax:2:0 (1 + 1)>)

parse/delim: undefined;

 cannot reference an identifier before its definition

  in module: 'm

parse/delim: undefined;

 cannot reference an identifier before its definition

  in module: 'm

parse/delim: undefined;

 cannot reference an identifier before its definition

  in module: 'm