xlambein
2021-6-8 09:12:13

I’m struggling with matching strings as part of a syntax-case statement. In the context of a reader I’m making, I would like to be able to detect 2 or more strings containing a new line, followed by anything else. This is a minimal example: (define (detect-new-paragraph doc) (syntax-case doc () [("\n" "\n" ... . tail) #'(new-paragraph tail)] [_ #`(something-else #,doc)])) But when I try to match e.g. #'("\n" "\n" 'foo 'bar), I don’t get the expected result, which would be #'(new-paragraph 'foo 'bar). The problem seems to be with the ellipsis, because if I remove it, I can match exactly two newlines. Anybody has some idea what’s happening? Thanks! :blush:


sorawee
2021-6-8 09:38:48

This is either a limitation of syntax-case, or a bug. In any case, I would suggest you to use syntax-parse instead, which can handle your example correctly.


sorawee
2021-6-8 09:39:06

#lang racket (require syntax/parse) (define (detect-new-paragraph doc) (syntax-parse doc [("\n" "\n" ... . tail) #'(new-paragraph tail)] [_ #`(something-else #,doc)])) (detect-new-paragraph #'("\n" "\n" 'foo 'bar))


xlambein
2021-6-8 10:11:10

Oh alright, thanks! In general, would you recommend syntax-parse over syntax-case?


sorawee
2021-6-8 10:13:24

Yep


xlambein
2021-6-8 10:26:13

Cool, thanks for the advice!