racketdefine-syntaxsyntax-rules

Provide syntax-rule Racket


How do I provide syntax rules in racket?

I have code which is similar to this:

(define-syntax SELECT 
  (syntax-rules (FROM WHERE star)
    [(SELECT colnames FROM relnames)
     ...]
    [(SELECT colnames FROM relnames WHERE . expression)
     ...]))

How do I use a provide statement in order to provide FROM WHERE and star?

This is how I provide SELECT:

(provide SELECT)

Solution

  • Here is one way to do it. First define the literals (and give a nice error messages if used outside SELECT), second provide them.

    #lang racket
    (provide SELECT FROM WHERE)
    
    (define-syntax FROM  (λ (stx) (raise-syntax-error 'FROM  "literal FROM used outside SELECT"  stx)))
    (define-syntax WHERE (λ (stx) (raise-syntax-error 'WHERE "literal WHERE used outside SELECT" stx)))
    
    (define-syntax SELECT 
      (syntax-rules (FROM WHERE star)
        [(SELECT colnames FROM relnames)                    ...]
        [(SELECT colnames FROM relnames WHERE . expression) ...]))