> On Dec 20, 2016, at 12:59 AM, Daniel Prager <daniel.a.pra...@gmail.com> wrote:
> 
> This isn't too bad, but What I'd *really* like is a "string-match" form to 
> more elegantly process structured data, via a few strings based on a simple 
> (and greedy) left-to-right algorithm.
> 
> But my macro-fu is too weak.
> 
> 1. Can someone show me how to write this style of macro?
> 2. Is this of more general interest?



IIRC Alex Knauth's `match-string` package does what you want. 

But vanilla syntax patterns will match strings and numbers literally, and thus 
arguably amount to a "simple (and greedy) left-to-right algorithm".

#lang racket
(require (for-syntax racket/string))

(define-syntax (sm stx)
  (syntax-case stx ()
    [(_ "whole-string") #''whole]
    [(_ str) (with-syntax ([TOKS (string-split (syntax->datum #'str) 
#px"--|\\s")])
               #'(sm . TOKS))]
    [(_ X1 X2 X3 "end") #'(list (string->symbol X1) (string->number X2) X3)]
    [(_ "the" ANIMAL "is" COLOR) #'(string-join (list COLOR ANIMAL) " ")]
    [else #''no-match-found]))

(sm "abc--123 foo end") ;-> '(abc 123 "foo")
(sm "the fox is black") ;-> "black fox"
(sm "whole-string") ;-> 'whole
(sm "abc--123--456 bar end") ;-> 'no-match-found ; greedy strategy keeps things 
simple and explicit


The tokenizer stashed in the second case is a little shameful. If you were 
making a DSL it would be cleaner to put that part in the reader, and then `sm` 
could omit that case.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to