-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathregex-dsl.el
203 lines (184 loc) · 5.72 KB
/
regex-dsl.el
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
;;; regex-dsl.el --- lisp syntax for regexps
;; Copyright (C) 2007,2010 Aliaksey Kandratsenka
;; Author: Aliaksey Kandratsenka <alk@tut.by>
;; Keywords:
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;;; Code:
(defconst redsl-precedence '((group . 0)
(cap-group . 0)
(\? . 1)
(* . 2)
(+ . 2)
(repeat-= . 2)
(repeat . 2)
(*\? . 2)
(+\? . 2)
(\?\? . 2)
(char-set . 0)
(word-char . 0)
(non-word-char . 0)
(char-class . 0)
(neg-char-set . 0)
(line-begin . 0)
(line-end . 0)
(match-start . 0)
(match-end . 0)
(point . 0)
(word-bound . 0)
(non-word-bound . 0)
(word-start . 0)
(word-end . 0)
(sym-start . 0)
(sym-end . 0)
(backref . 0)
(whitespace . 0)
(literal . 0)
(anychar . 0)
(concat . 5)
(or . 10)))
(defun redsl-find-precedence (re)
(let* (car
cadr
(found (and (consp re) (assoc (setq car (car re)) redsl-precedence))))
(unless found
(error "Given re %S is invalid redsl" re))
(if (and (eq car 'concat)
(stringp (setq cadr (cadr re)))
(null (cddr re))
(= (length cadr) 1))
0
(cdr found))))
(defun redsl-complex-p (re)
(or (memq (car re) '(group cap-group \? * + repeat-= repeat *\? ?\? +\? or))
(and (eq 'concat (car re))
(not (and (stringp (cadr re))
(null (cddr re)))))))
(defun redsl-put-parenteses (re &optional parent-precedence)
(let* ((my-precedence (redsl-find-precedence re))
(precedence-for-child (if (memq (car re) '(cap-group group))
1000
my-precedence))
(myself re))
(setq parent-precedence (or parent-precedence 1000))
(when (redsl-complex-p re)
(let ((childs (if (memq (car re) '(repeat repeat-=))
(list* (redsl-put-parenteses (cadr re) precedence-for-child) (cddr re))
(mapcar (lambda (child) (redsl-put-parenteses child precedence-for-child))
(cdr re)))))
(setq myself (cons (car re) childs))))
(if (<= my-precedence parent-precedence)
myself
(list 'group myself))))
(defun redsl-process-literals (re)
(cond ((consp re)
(if (redsl-complex-p re)
(if (memq (car re) '(repeat-= repeat))
(list* (car re) (redsl-process-literals (cadr re)) (cddr re))
(cons (car re) (mapcar #'redsl-process-literals (cdr re))))
re))
((stringp re)
(list 'concat re))
((symbolp re)
(list 'concat (prin1-to-string re)))
(t
(error "Given object (%S) is not RE" re))))
(defun redsl-to-regexp-repeat (re from-count to-count)
(if (= from-count 0)
(cond ((null to-count)
(concat re "*"))
((= to-count 1)
(concat re "?"))
(t
(format "%s\\{,%d\\}" re to-count)))
(cond ((null to-count)
(if (= from-count 1)
(concat re "+")
(format "%s\\{%d,\\}" re from-count)))
((= to-count from-count)
(if (= to-count 1)
re
(format "%s\\{%d\\}" re to-count)))
(t
(format "%s\\{%d,%d\\}" re from-count to-count)))))
(defun redsl-to-regexp-char-set (positive chars)
(format (if positive
"[%s]"
"[^%s]")
chars))
(defun redsl-to-regexp-rec (re)
(cl-case (car re)
(group
(format "\\(?:%s\\)" (redsl-to-regexp-rec (cadr re))))
(cap-group
(format "\\(%s\\)" (redsl-to-regexp-rec (cadr re))))
(\?
(redsl-to-regexp-rec (list 'repeat (cadr re) 0 1)))
(*
(redsl-to-regexp-rec (list 'repeat (cadr re) 0 nil)))
(+
(redsl-to-regexp-rec (list 'repeat (cadr re) 1 nil)))
(repeat-=
(let ((subre (cadr re))
(arg (caddr re)))
(redsl-to-regexp-rec (list 'repeat subre arg arg))))
(repeat
(apply #'redsl-to-regexp-repeat (redsl-to-regexp-rec (cadr re)) (cddr re)))
(*\?
(concat (redsl-to-regexp-rec (list* '* (cdr re))) "?"))
(+\?
(concat (redsl-to-regexp-rec (list* '+ (cdr re))) "?"))
(\?\?
(concat (redsl-to-regexp-rec (list* '? (cdr re))) "?"))
(char-set
(apply #'redsl-to-regexp-char-set t (cdr re)))
(word-char
"\\w")
(non-word-char
"\\W")
(char-class
(format "[[%s:]]" (cadr re)))
(neg-char-set
(apply #'redsl-to-regexp-char-set nil (cdr re)))
(line-begin "^")
(line-end "$")
(match-start "\\`")
(match-end "\\'")
(point "\\=")
(anychar ".")
(word-bound "\\b")
(non-word-bound "\\B")
(word-start "\\<")
(word-end "\\>")
(sym-start "\\_<")
(sym-end "\\_>")
(backref (format "\\%d" (cadr re)))
(whitespace "\\s-")
(literal (cadr re))
(concat
(if (redsl-complex-p re)
(apply #'concat (mapcar #'redsl-to-regexp-rec (cdr re)))
(regexp-quote (cadr re))))
(or
(let ((list (list (redsl-to-regexp-rec (cadr re)))))
(dolist (item (cddr re))
(setq list (list* (redsl-to-regexp-rec item) "\\|" list)))
(apply #'concat (nreverse list))))))
(defun redsl-pre-process (re)
(redsl-put-parenteses (redsl-process-literals re)))
(defun redsl-to-regexp (re)
(redsl-to-regexp-rec (redsl-pre-process re)))
(provide 'regex-dsl)
;;; regex-dsl.el ends here