PageRenderTime 58ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/reference/scsh-0.6.6/scsh/rx/re-syntax.scm

https://github.com/cardmagic/lucash
Scheme | 110 lines | 48 code | 22 blank | 40 comment | 0 complexity | 55f38392fcdfc8b2716238e50526afce MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1, AGPL-1.0
  1. ;;; SRE syntax support for regular expressions
  2. ;;; Olin Shivers, June 1998.
  3. ;;; Export SRE-FORM?, EXPAND-RX
  4. ;;; Is the form an SRE expression?
  5. ;;; We only shallowly check the initial keyword of a compound form.
  6. (define (sre-form? exp r same?) ; An SRE is
  7. (let ((kw? (lambda (x kw) (same? x (r kw)))))
  8. (or (string? exp) ; "foo"
  9. (and (pair? exp)
  10. (let ((head (car exp)))
  11. (or (every string? exp) ; ("aeiou")
  12. (kw? head '*) ; (* re ...)
  13. (kw? head '+) ; (+ re ...)
  14. (kw? head '?) ; (? re ...)
  15. (kw? head '=) ; (= n re ...)
  16. (kw? head '>=) ; (>= n re ...)
  17. (kw? head '**) ; (** m n re ...)
  18. (kw? head '|) ; (| re ...)
  19. (kw? head 'or) ; (| re ...)
  20. (kw? head ':) ; (: re ...)
  21. (kw? head 'seq) ; (: re ...)
  22. (kw? head '-) ; (- re ...)
  23. (kw? head '&) ; (& re ...)
  24. (kw? head '~) ; (~ re ...)
  25. (kw? head 'submatch) ; (submatch re ...)
  26. (kw? head 'dsm) ; (dsm pre post re ...)
  27. (kw? head 'uncase) ; (uncase re ...)
  28. (kw? head 'w/case) ; (w/case re ...)
  29. (kw? head 'w/nocase) ; (w/nocase re ...)
  30. (kw? head 'unquote) ; ,exp
  31. (kw? head 'unquote-splicing) ; ,@exp
  32. (kw? head 'posix-string)))) ; (posix-string string)
  33. (kw? exp 'any) ; any
  34. (kw? exp 'nonl) ; nonl
  35. (kw? exp 'bos) (kw? exp 'eos) ; bos / eos
  36. (kw? exp 'bol) (kw? exp 'eol) ; bol / eol
  37. (kw? exp 'lower-case) (kw? exp 'lower); The char class names
  38. (kw? exp 'upper-case) (kw? exp 'upper)
  39. (kw? exp 'alphabetic) (kw? exp 'alpha)
  40. (kw? exp 'numeric) (kw? exp 'num) (kw? exp 'digit)
  41. (kw? exp 'alphanumeric) (kw? exp 'alphanum) (kw? exp 'alnum)
  42. (kw? exp 'blank)
  43. (kw? exp 'control) (kw? exp 'cntrl)
  44. (kw? exp 'printing) (kw? exp 'print)
  45. (kw? exp 'punctuation) (kw? exp 'punct)
  46. (kw? exp 'hex-digit) (kw? exp 'hex) (kw? exp 'xdigit)
  47. (kw? exp 'graphic) (kw? exp 'graph)
  48. (kw? exp 'whitespace) (kw? exp 'white) (kw? exp 'space)
  49. (kw? exp 'ascii))))
  50. ;;; (if-sre-form form conseq-form alt-form)
  51. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  52. ;;; If FORM is an SRE, expand into CONSEQ-FORM, otherwise ALT-FORM.
  53. ;;; This is useful for expanding a subform of a macro that can
  54. ;;; be either a regexp or something else, e.g.
  55. ;;; (if-sre-form test ; If TEST is a regexp,
  56. ;;; (regexp-search? (rx test) line) ; match it against the line,
  57. ;;; (test line)) ; otw it's a predicate.
  58. ;;; The macro is actually defined directly in the module file.
  59. ;;; (define-syntax if-sre-form
  60. ;;; (lambda (exp r c)
  61. ;;; (if (sre-form? (cadr exp) r c)
  62. ;;; (caddr exp)
  63. ;;; (cadddr exp))))
  64. ;;; (RX re ...)
  65. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  66. ;;; The basic SRE form.
  67. (define (expand-rx exp r c)
  68. (let ((re (simplify-regexp (parse-sres (cdr exp) r c))))
  69. ;; If it's static, pre-compute the Posix string & tvec now,
  70. ;; so the re->scheme unparser will find it and toss it into
  71. ;; the constructor. We do this only for the top-level regexp.
  72. (if (static-regexp? re) (compile-regexp re))
  73. (regexp->scheme re r)))
  74. ;(define-syntax rx (syntax-rules () ((rx stuff ...) (really-rx stuff ...))))
  75. ;(define-syntax really-rx
  76. ; (syntax-rules () ((really-rx stuff ...) (rx/cs stuff ...))))
  77. ;
  78. ;(define-syntax rx/cs (lambda (exp r c) (expand-rx exp #t r c)))
  79. ;(define-syntax rx/ci (lambda (exp r c) (expand-rx exp #f r c)))
  80. ;
  81. ;(define-syntax case-sensitive
  82. ; (lambda (exp r c)
  83. ; (let ((%ls (r 'let-syntax))
  84. ; (%really-rx (r 'really-rx))
  85. ; (%sr (r 'syntax-rules))
  86. ; (%rx/cs (r 'rx/cs)))
  87. ; `(,ls ((,%really-rx (,sr () ((,%really-rx stuff ...) (,%rx/cs stuff ...)))))
  88. ; . ,(cdr exp)))))