PageRenderTime 50ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/collects/unstable/automata/re.rkt

http://github.com/dyoo/racket
Racket | 79 lines | 73 code | 6 blank | 0 comment | 1 complexity | f72179cdc2b0de4223d985ffacf7c858 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #lang racket/base
  2. (require "machine.rkt"
  3. "re-help.rkt"
  4. racket/match
  5. (for-syntax syntax/parse
  6. racket/syntax
  7. unstable/syntax
  8. racket/base
  9. "re-help.rkt"
  10. "re-compile.rkt"))
  11. (define-syntax-rule (define-re-transformer id lam)
  12. (define-syntax id (re-transformer lam)))
  13. (define-for-syntax code-insp (variable-reference->module-declaration-inspector
  14. (#%variable-reference)))
  15. (define-for-syntax (re-expand stx)
  16. (syntax-parse
  17. (syntax-disarm stx code-insp)
  18. #:literals (complement seq union star epsilon nullset dseq rec unquote)
  19. [((~and op complement) lhs:expr)
  20. (quasisyntax/loc stx
  21. (op #,(re-expand #'lhs)))]
  22. [((~and op rec) v:id lhs:expr)
  23. (quasisyntax/loc stx
  24. (op v #,(re-expand #'lhs)))]
  25. [((~and op unquote) e:expr)
  26. (quasisyntax/loc stx
  27. (op e))]
  28. [((~and op star) lhs:expr)
  29. (quasisyntax/loc stx
  30. (op #,(re-expand #'lhs)))]
  31. [((~and op seq) lhs:expr)
  32. (re-expand #'lhs)]
  33. [((~and op seq) lhs:expr rhs:expr)
  34. (quasisyntax/loc stx
  35. (op #,(re-expand #'lhs) #,(re-expand #'rhs)))]
  36. [((~and op seq) lhs:expr rest:expr ...)
  37. (quasisyntax/loc stx
  38. #,(re-expand #'(op lhs (op rest ...))))]
  39. [((~and op union) lhs:expr)
  40. (re-expand #'lhs)]
  41. [((~and op union) lhs:expr rhs:expr)
  42. (quasisyntax/loc stx
  43. (op #,(re-expand #'lhs) #,(re-expand #'rhs)))]
  44. [((~and op union) lhs:expr rest:expr ...)
  45. (quasisyntax/loc stx
  46. #,(re-expand #'(op lhs (op rest ...))))]
  47. [(~and e (~var transformer (static re-transformer? "re transformer")))
  48. (record-disappeared-uses (list #'transformer))
  49. (quasisyntax/loc stx
  50. #,(re-expand ((re-transformer->re (attribute transformer.value)) #'e)))]
  51. [(~and e ((~var transformer (static re-transformer? "re transformer")) . _))
  52. (record-disappeared-uses (list #'transformer))
  53. (quasisyntax/loc stx
  54. #,(re-expand ((re-transformer->re (attribute transformer.value)) #'e)))]
  55. [((~and op dseq) pat:expr rhs:expr)
  56. (quasisyntax/loc stx
  57. (op pat #,(re-expand #'rhs)))]
  58. [_
  59. stx]))
  60. (define-for-syntax (re-compile stx)
  61. (syntax-parse
  62. stx
  63. [the-re:sre
  64. (attribute the-re.best)]))
  65. (define-syntax (re stx)
  66. (with-disappeared-uses
  67. (syntax-case stx ()
  68. [(_ the-re)
  69. (re-compile (re-expand #'the-re))])))
  70. (provide
  71. complement seq union star epsilon nullset dseq rec unquote
  72. define-re-transformer
  73. re)