-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregexes.rkt
142 lines (128 loc) · 7.22 KB
/
regexes.rkt
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
#lang racket
(require "token.rkt")
(require "machine.rkt")
(require "expand-parenthesis.rkt")
(provide string->machine)
(define (char-range f l)
(map integer->char (range (char->integer f) (add1 (char->integer l)))))
(define (make-union-regex cl)
(define (make-escaped-string c)
(cond
[(not (char? c)) (error "what are you doing, ya dingus? chars only plox")]
[(ormap (curry char=? c) (string->list "()\\|~#")) (string #\\ c)]
[else (string c)]))
(string-join (map make-escaped-string cl) "|" #:before-first "(" #:after-last ")"))
(define (m-char-union cl)
(let ([start (gensym)]
[final (gensym)])
(machine (list start final)
start
(list final)
(map (lambda (x) (transition start x final)) cl)
empty)))
(define blocks
(let* ([get-machine (lambda (x list) (copy-machine (second (assoc x list))))]
[union-blocks (map (lambda (x) (list (first x) (m-char-union (second x))))
`((java-digit ,(char-range #\0 #\9))
(octal-digit ,(char-range #\0 #\7))
(java-letter ,(append (string->list "_$") (char-range #\a #\z) (char-range #\A #\Z)))
(one-to-nine ,(char-range #\1 #\9))
(zero-to-three ,(char-range #\0 #\3))
(exponent-indicator ,(string->list "eE"))
(float-suffix ,(string->list "fFdD"))
(reg-escape ,(string->list "btnfr'\"\\"))
(hex-digit ,(string->list "0123456789aAbBcCdDeEfF"))
(string-char ,(filter-not (lambda (x) (member x (string->list "\\\""))) (map integer->char (range 0 128))))
(char-char ,(filter-not (lambda (x) (member x (string->list "\\'"))) (map integer->char (range 0 128))))
(non-break-char ,(filter-not (lambda (x) (member x (string->list "\n\r"))) (map integer->char (range 0 128))))
(no-star ,(filter-not (lambda (x) (member x (string->list "*"))) (map integer->char (range 0 128))))
(no-slash ,(filter-not (lambda (x) (member x (string->list "/"))) (map integer->char (range 0 128))))
(no-star-slash ,(filter-not (lambda (x) (member x (string->list "*/"))) (map integer->char (range 0 128))))
(whitespace ,(string->list "\t\n\r\f "))
(sign ,(string->list "+-"))
))]
[composed-blocks `((digits ,(copy-machine (opt (concat (list (get-machine 'java-digit union-blocks)
(kleene-star (get-machine 'java-digit union-blocks)))))))
(oct-escape ,(copy-machine (opt (union (list (get-machine 'octal-digit union-blocks)
(concat (list (get-machine 'octal-digit union-blocks) (get-machine 'octal-digit union-blocks)))
(concat (list (get-machine 'zero-to-three union-blocks) (get-machine 'octal-digit union-blocks) (get-machine 'octal-digit union-blocks))))))))
(decimal-lit ,(copy-machine (opt (union (list (m-char-union (list #\0))
(concat (list (get-machine 'one-to-nine union-blocks) (kleene-star (get-machine 'java-digit union-blocks)))))))))
)]
[top-blocks `((signed-int ,(copy-machine (opt (concat (list (union (list (get-machine 'sign union-blocks) (m-only-epsilon)))
(get-machine 'digits composed-blocks))))))
)])
(append top-blocks union-blocks composed-blocks)))
;;(struct : empty-regex ())
(struct empty-regex ())
;;( struct : concatenation ([left : (union concatenation Char k-star alternation)] [right : (Union concatenation Char k-star alternation)]))
(struct concatenation (left right) #:transparent)
;;( struct : alternation ([options (Listof (union concatenation Char k-star alternation))]))
(struct alternation (options) #:transparent)
;;( struct : k-star ([body (union concatenation Char k-star alternation)]))
(struct k-star (body) #:transparent)
;( struct : block ([id symbol]))
(struct block (id) #:transparent)
;( struct : literal ([literal Char])) )
(struct literal (char) #:transparent)
;;(: regex->machine : (union concatenation Char k-star alternation) -> machine)
(define [regex->machine R]
(cond
[(empty-regex? R) (m-only-epsilon)]
[(char? R) (m-single-char R)]
[(concatenation? R) (concat (list (regex->machine (concatenation-left R))
(regex->machine (concatenation-right R))))]
[(alternation? R) (union (map regex->machine (alternation-options R)))]
[(k-star? R) (kleene-star (regex->machine (k-star-body R)))]
[(block? R) (second (assoc (block-id R) blocks))]
[else (error "Not a regular expression")]))
;;(: list->regex : (Listof Char) (union concatenation Char k-star alternation) ->blocks (union concatenation Char k-star alternation))
(define (list->regex lst R)
(match lst
[`() R]
[`(#\\ ,x ,y ...) (list->regex y (concatenation R x))]
[`(#\( ,x ...)
((lambda (P)
((lambda (Q)
(list->regex (first (rest P)) Q))
(concatenation R (list->regex (first P) (empty-regex)))))
(expand-parenthesis x empty 1))]
[`(#\* ,x ...)
(concatenation (k-star R) (list->regex x (empty-regex)))]
[`(#\| ,x ...)
((lambda (S)
(cond
;[(empty-regex? S) (cons R empty)]
[(alternation? S) (alternation (cons R (alternation-options S)))]
[else (alternation (cons R (cons S empty)))]))
(list->regex x (empty-regex)))]
[`(#\# #\( ,x ...)
((lambda (p)
((lambda (q)
(list->regex (first (rest p)) q))
(concatenation R (block (string->symbol (list->string (first p)))))))
(expand-parenthesis x empty 1))]
[`(#\~ ,x ...) (list->regex x (concatenation R (empty-regex)))]
[_ (list->regex (rest lst) (concatenation R (first lst)))]))
(define (tokenize lst)
(match lst
[`(#\| ,x ...) (cons 'ALT (tokenize x))]
[`(#\* ,x ...) (cons 'STAR (tokenize x))]
[`(#\~ ,x ...) (cons 'TILD (tokenize x))]
[`(#\# #\( ,x ...) (cons 'OHPRN (tokenize x))]
[`(#\( ,x ...) (cons 'OPARN (tokenize x))]
[`(#\) ,x ...) (cons 'CPARN (tokenize x))]
[`(#\\ ,x ,y ...) (cons (literal x) (tokenize y))]
[x (if (empty? x)
empty
(cons (literal (first x)) (tokenize (rest x))))]))
(define (sreg->machine str)
(regex->machine (list->regex (string->list str) (empty-regex))))
(define (string->machine S token-name)
(let ([machine-1 (regex->machine (list->regex (string->list S) (empty-regex)))])
(machine
(machine-states machine-1)
(machine-start machine-1)
(machine-accepting machine-1)
(machine-transitions machine-1)
(map (lambda (x) (list x token-name)) (machine-accepting machine-1)))))