Skip to content

Instantly share code, notes, and snippets.

@soegaard
Last active August 29, 2015 13:59
Show Gist options
  • Select an option

  • Save soegaard/10641260 to your computer and use it in GitHub Desktop.

Select an option

Save soegaard/10641260 to your computer and use it in GitHub Desktop.
#lang racket
(require syntax/parse)
(define-syntax-class term
(pattern ((~literal t) t)))
(define-syntax-class operator
(pattern ((~literal o) op)))
(define-splicing-syntax-class term-op
(pattern (~seq t:term o:operator)))
(syntax-parse #'(e (t "2") (o "+") (t "3") (o "+") (t "4"))
[(e to:term-op ... t:term)
(syntax->datum #'(e to ... t ))])
@soegaard
Copy link
Author

Here is a version with defining syntax classes.

lang racket

(require syntax/parse)

(syntax-parse #'(e (t "2") (o "+") (t "3") (o "+") (t "4"))
[(e (~seq ((~literal t) T) ((~literal o) O)) ... ((~literal t) T-last))
(syntax->datum #'(e (T O) ... T-last))])

; Output: '(e ("2" "+") ("3" "+") "4")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment