PageRenderTime 53ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/collects/unstable/automata/re.rkt

http://github.com/gmarceau/PLT
Racket | 76 lines | 71 code | 5 blank | 0 comment | 1 complexity | 289990b5b2e70ffb167b0f0dddc47800 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  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 (re-expand stx)
  14. (syntax-parse
  15. (syntax-disarm stx (current-code-inspector))
  16. #:literals (complement seq union star epsilon nullset dseq rec unquote)
  17. [((~and op complement) lhs:expr)
  18. (quasisyntax/loc stx
  19. (op #,(re-expand #'lhs)))]
  20. [((~and op rec) v:id lhs:expr)
  21. (quasisyntax/loc stx
  22. (op v #,(re-expand #'lhs)))]
  23. [((~and op unquote) e:expr)
  24. (quasisyntax/loc stx
  25. (op e))]
  26. [((~and op star) lhs:expr)
  27. (quasisyntax/loc stx
  28. (op #,(re-expand #'lhs)))]
  29. [((~and op seq) lhs:expr)
  30. (re-expand #'lhs)]
  31. [((~and op seq) lhs:expr rhs:expr)
  32. (quasisyntax/loc stx
  33. (op #,(re-expand #'lhs) #,(re-expand #'rhs)))]
  34. [((~and op seq) lhs:expr rest:expr ...)
  35. (quasisyntax/loc stx
  36. #,(re-expand #'(op lhs (op rest ...))))]
  37. [((~and op union) lhs:expr)
  38. (re-expand #'lhs)]
  39. [((~and op union) lhs:expr rhs:expr)
  40. (quasisyntax/loc stx
  41. (op #,(re-expand #'lhs) #,(re-expand #'rhs)))]
  42. [((~and op union) lhs:expr rest:expr ...)
  43. (quasisyntax/loc stx
  44. #,(re-expand #'(op lhs (op rest ...))))]
  45. [(~and e (~var transformer (static re-transformer? "re transformer")))
  46. (record-disappeared-uses (list #'transformer))
  47. (quasisyntax/loc stx
  48. #,(re-expand ((re-transformer->re (attribute transformer.value)) #'e)))]
  49. [(~and e ((~var transformer (static re-transformer? "re transformer")) . _))
  50. (record-disappeared-uses (list #'transformer))
  51. (quasisyntax/loc stx
  52. #,(re-expand ((re-transformer->re (attribute transformer.value)) #'e)))]
  53. [((~and op dseq) pat:expr rhs:expr)
  54. (quasisyntax/loc stx
  55. (op pat #,(re-expand #'rhs)))]
  56. [_
  57. stx]))
  58. (define-for-syntax (re-compile stx)
  59. (syntax-parse
  60. stx
  61. [the-re:sre
  62. (attribute the-re.best)]))
  63. (define-syntax (re stx)
  64. (with-disappeared-uses
  65. (syntax-case stx ()
  66. [(_ the-re)
  67. (re-compile (re-expand #'the-re))])))
  68. (provide
  69. complement seq union star epsilon nullset dseq rec unquote
  70. define-re-transformer
  71. re)