regexps.scm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. ;;; Regular expressions
  2. ;;; Copyright (C) 2024 David Thompson <dave@spritely.institute>
  3. ;;;
  4. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  5. ;;; you may not use this file except in compliance with the License.
  6. ;;; You may obtain a copy of the License at
  7. ;;;
  8. ;;; http://www.apache.org/licenses/LICENSE-2.0
  9. ;;;
  10. ;;; Unless required by applicable law or agreed to in writing, software
  11. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  12. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. ;;; See the License for the specific language governing permissions and
  14. ;;; limitations under the License.
  15. ;;; Commentary:
  16. ;;;
  17. ;;; Regular expressions.
  18. ;;;
  19. ;;; Code:
  20. (library (hoot regexps)
  21. (export make-regexp
  22. regexp?
  23. regexp-exec
  24. regexp-match?
  25. regexp-match-string
  26. regexp-match-start
  27. regexp-match-end
  28. regexp-match-count
  29. regexp-match-substring
  30. regexp-match-prefix
  31. regexp-match-suffix)
  32. (import (hoot cond-expand)
  33. (hoot eq)
  34. (hoot errors)
  35. (hoot ffi)
  36. (hoot lists)
  37. (hoot match)
  38. (hoot numbers)
  39. (only (hoot primitives)
  40. guile:make-regexp
  41. guile:regexp?
  42. guile:regexp-exec
  43. guile:regexp-match?
  44. guile:match:string
  45. guile:match:start
  46. guile:match:end
  47. guile:match:count
  48. guile:match:substring)
  49. (hoot pairs)
  50. (hoot strings)
  51. (hoot syntax))
  52. (define-external-type <regexp>
  53. regexp?
  54. wrap-regexp
  55. unwrap-regexp)
  56. (define-external-type <regexp-match>
  57. regexp-match?
  58. wrap-regexp-match
  59. unwrap-regexp-match)
  60. (cond-expand
  61. (guile-vm
  62. (define make-regexp guile:make-regexp)
  63. (define regexp? guile:regexp?)
  64. (define regexp-exec guile:regexp-exec)
  65. (define regexp-match? guile:regexp-match?)
  66. (define regexp-match-string guile:match:string)
  67. (define regexp-match-start guile:match:start)
  68. (define regexp-match-end guile:match:end)
  69. (define regexp-match-count guile:match:count)
  70. (define regexp-match-substring guile:match:substring))
  71. (hoot
  72. (define-foreign %make-regexp "rt" "make_regexp"
  73. (ref string) (ref string) -> (ref extern))
  74. (define-foreign %regexp-exec "rt" "regexp_exec"
  75. (ref extern) (ref string) -> (ref null extern))
  76. (define-foreign %regexp-match-string "rt" "regexp_match_string"
  77. (ref extern) -> (ref string))
  78. (define-foreign %regexp-match-start "rt" "regexp_match_start"
  79. (ref extern) -> i32)
  80. (define-foreign %regexp-match-end "rt" "regexp_match_end"
  81. (ref extern) -> i32)
  82. (define-foreign %regexp-match-count "rt" "regexp_match_count"
  83. (ref extern) -> i32)
  84. (define-foreign %regexp-match-substring "rt" "regexp_match_substring"
  85. (ref extern) i32 -> (ref null string))
  86. (define (make-regexp pattern . flags)
  87. (check-type pattern string? 'make-regexp)
  88. (let ((flags (list->string
  89. (fold (lambda (flag chars)
  90. (match flag
  91. ('case-insensitive (cons #\i chars))
  92. ('multiline (cons #\m chars))
  93. ;; This is the only regexp type and it's
  94. ;; just here for compatibility with
  95. ;; Guile's existing API. "Basic" regexps
  96. ;; are not supported.
  97. ('extended chars)))
  98. '() flags))))
  99. (wrap-regexp (%make-regexp pattern flags))))
  100. ;; TODO: Is it possible to respect flags on the web? Doesn't seem
  101. ;; so.
  102. (define* (regexp-exec regexp str #:optional (start 0) (flags '()))
  103. (check-type str string? 'regexp-exec)
  104. (match (%regexp-exec (unwrap-regexp regexp)
  105. (if (eq? start 0) str (string-copy str start)))
  106. ((? external-null?) #f)
  107. (m (wrap-regexp-match m))))
  108. (define (regexp-match-string m)
  109. (%regexp-match-string (unwrap-regexp-match m)))
  110. (define (regexp-match-start m)
  111. (%regexp-match-start (unwrap-regexp-match m)))
  112. (define (regexp-match-end m)
  113. (%regexp-match-end (unwrap-regexp-match m)))
  114. (define (regexp-match-count m)
  115. (%regexp-match-count (unwrap-regexp-match m)))
  116. (define* (regexp-match-substring m #:optional (n 0))
  117. (check-size n (1- (regexp-match-count m)) 'regexp-match-substring)
  118. (%regexp-match-substring (unwrap-regexp-match m) n))))
  119. (define (regexp-match-prefix m)
  120. (string-copy (regexp-match-string m) 0 (regexp-match-start m)))
  121. (define (regexp-match-suffix m)
  122. (string-copy (regexp-match-string m) (regexp-match-end m))))