File: pcre.lisp

package info (click to toggle)
clisp 1%3A2.41-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 49,804 kB
  • ctags: 16,291
  • sloc: lisp: 75,912; ansic: 49,247; xml: 24,289; asm: 21,993; sh: 11,234; fortran: 6,692; cpp: 2,660; objc: 2,481; makefile: 2,355; perl: 164; sed: 55
file content (50 lines) | stat: -rw-r--r-- 1,841 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
;; Module for PCRE / CLISP
;; <http://www.pcre.org/>
;; Sam Steingold 2003-2004

(defpackage "PCRE"
  (:documentation
   "PCRE - Perl Compatible Regular Expressions - <http://www.pcre.org/>")
  (:use "LISP")
  (:export "PCRE-VERSION" "PCRE-CONFIG" "PCRE-COMPILE" "PCRE-EXEC" "PATTERN"
           "PATTERN-INFO" "PCRE-NAME-TO-INDEX" "MATCH-SUBSTRING"
           "PCRE-MATCHER"
           "MATCH-STRINGS" "MATCH-STRING" "MATCH" "MATCH-START" "MATCH-END"))

(in-package "PCRE")
(pushnew :pcre *features*)
(push "PCRE" custom:*system-package-list*)
(setf (documentation (find-package "PCRE") 'sys::impnotes) "pcre")

(defstruct (pattern (:constructor make-pat (compiled study)))
  (compiled nil :read-only t)
  (study nil :read-only t))

(defstruct (match (:constructor make-match-boa (start end)) (:constructor))
  (start nil :read-only t)
  (end nil :read-only t))

(defun match-substring (match subject)
  "Return the substring corresponding to the match."
  (subseq subject (match-start match) (match-end match)))

(defun match-strings (ret-vec subject)
  "Return a vector of all substring that match any sub-patterns."
  (map 'vector (lambda (match)
                 (when match
                   (subseq subject (match-start match) (match-end match))))
       ret-vec))

(defun match-string (ret-vec which subject &optional pattern)
  "Return the substring that matches the given sub-pattern.
If which is a name of the sub-pattern, pattern must be supplied."
  (match-substring
   (svref ret-vec (etypecase which
                    (integer which)
                    (string (pcre-name-to-index pattern which))))
   subject))

(defun pcre-matcher (pattern)
  "A valid value for *APROPOS-MATCHER*."
  (let ((compiled (pcre-compile pattern :extended t :ignore-case t :study t)))
    (lambda (name) (pcre-exec compiled name :boolean t))))