PasteRack.org
Paste # 23923
2021-10-20 01:09:09

Fork as a new paste.

Paste viewed 484 times.


Embed:

  1. #lang racket
  2.  
  3. (require
  4.   (for-syntax
  5.    syntax/parse)
  6.   )
  7.  
  8. (begin-for-syntax
  9.  
  10.   (define-splicing-syntax-class enum-attribute-properties
  11.     #:attributes (norm)
  12.     (pattern (~seq #:default ~! val:expr) ;; default value to use when initializing an attribute generated from this enum entry (e.g., usually 0 for additive attributes or 1 for multipliers)
  13.              #:attr norm #'[#:default . val])
  14.     (pattern (~seq #:set ~! val:expr) ;; name of the attribute set that this enum entry belongs to (allows for a single define-enum to generate multiple attribute sets)
  15.              #:attr norm #'[#:attribute-set . val])
  16.     (pattern (~seq #:prefix ~! val:expr) ;; prefix to apply to the name of the attribute (default is no prefix, and attribute will be named the same as the enum entry)
  17.              #:attr norm #'[#:attribute-prefix . val])
  18.     (pattern (~seq #:max ~! val:expr) ;; name of the attribute used to cap the upper limit of this attribute
  19.              #:attr norm #'[#:attribute-max . val])
  20.     (pattern (~seq #:comment ~! val:expr) ;; comment that will be added to the generated UENUM and UAttributeSet values generated from this enum entry
  21.              #:attr norm #'[#:comment . val])
  22.     )
  23.  
  24.   (define-syntax-class enum-entry
  25.     #:attributes (spec name)
  26.     (pattern name:id
  27.              #:attr spec #'()
  28.              )
  29.     (pattern (name:id #:attribute-properties ~! (p:enum-attribute-properties ...))
  30.              #:attr spec #'(#:attribute-properties . (p.norm ...))
  31.              ))
  32.   )
  33.  
  34.  
  35. (define-syntax (define-enum stx)
  36.   (syntax-parse stx
  37.     ((_ name:id
  38.         (fs:enum-entry ...))
  39.      #;#'(list 'name
  40.                '(fs.name ...)
  41.                '(fs.spec ...))
  42.      #'(list 'name (list 'fs.name 'fs.spec)
  43.              ...)
  44.      )
  45.     ))
  46.  
  47. (define-enum resource-type
  48.   (
  49.    none
  50.  
  51.    (context
  52.     #:attribute-properties
  53.     (#:set ResourceTypes
  54.      #:prefix resource
  55.      #:default 0
  56.      #:max max-resource-context))
  57.  
  58.    asdf
  59.    ))

=>

'(resource-type

  (none ())

  (context

   (#:attribute-properties

    (#:attribute-set . ResourceTypes)

    (#:attribute-prefix . resource)

    (#:default . 0)

    (#:attribute-max . max-resource-context)))

  (asdf ()))