reverse forms implemented
[software/python-on-guile.git] / modules / language / python / module / re / parser.scm
1 (define-module (language python module re parser)
2 #:use-module (parser stis-parser)
3 #:export(parse-reg e-matcher))
4
5 (define-syntax-rule (mk n tag str) (define n (f-seq tag (f-tag str))))
6 (mk f-. #:dot ".")
7 (mk f-^ #:^ "^")
8 (mk f-$ #:$ "$")
9 (mk q* #:* "*")
10 (mk q? #:? "?")
11 (mk q+ #:+ "+")
12 (mk q*? #:*? "*?")
13 (mk q?? #:?? "??")
14 (mk q+? #:+? "+?")
15
16 (define subexpr (f-list #:sub
17 (f-seq (f-tag "(") (Ds ee) (f-tag ")"))))
18
19 (define f-back
20 (f-or (f-list #:class (mk-token (f-reg! "[AZbBdDsSwntr]")))
21 (mk-token (f-reg "."))))
22
23 (define anongroup (f-list #:?: "(?:" (Ds ee) ")"))
24 (define namegroup (f-list #:?P< "(?P<" (mk-token (f+ (f-not! (f-reg "[> ]")))) ">" (Ds ee) ")"))
25 (define (ch not)
26 (f-list #:ch
27 (f-or! (f-seq (f-char #\\) f-back)
28 (mk-token (f-not! not)))))
29 (define number (mk-token (f+ (f-reg! "[0-9]")) string->number))
30 (define incant (f-list #:?P= "(?P=" (f-or! number
31 (mk-token (f+ (f-not! (f-reg "[) ]"))))) ")"))
32 (define coment (f-seq "(?#" (f* (f-not (f-tag ")"))) ")"))
33 (define repn? (f-list #:rep? "{" number "}" "?"))
34 (define repnm? (f-list #:rep? "{" number "," number "}" "?"))
35 (define repn (f-list #:rep "{" number "}"))
36 (define repnm (f-list #:rep "{" number "," number "}"))
37
38 (define lookh (f-list #:?= "(?=" (Ds ee) ")"))
39 (define lookh! (f-list #:?! "(?!" (Ds ee) ")"))
40
41 (define rev (f-list #:?<= "(?<=" (Ds ee) ")"))
42 (define rev! (f-list #:?<! "(?<!" (Ds ee) ")"))
43
44 (define bbody (f-cons (ch (f-reg "[\\]")) (ff* (ch (f-reg "[]\\]")))))
45
46 (define (f-if a b c) (f-or! (f-seq a b) c))
47 (define choice
48 (f-cons #:bracket
49 (f-or!
50 (f-seq "[^]" (f-out (list (list #:ch (f-out #f) "^"))))
51 (f-cons*
52 (f-tag "[")
53 (f-if (f-tag "^") (f-out #t) (f-out #f))
54 bbody))))
55
56
57 (define f-bar (f-tag "|"))
58 (define qq (ch (f-reg "[][?+|*.$^() \\]")))
59 (define atom (f-or qq f-. choice subexpr anongroup namegroup incant coment lookh lookh! rev rev! f-^ f-$))
60 (define spec (f-list #:op atom (f-or! q+? q?? q*? q* q? q+ repn? repnm? repn repnm)))
61 (define aatom (f-or! spec atom))
62 (define line (f-cons* #:seq aatom (ff* aatom )))
63 (define ee (f-cons* #:or line (ff* (f-seq f-bar line))))
64
65 (define (parse-reg str) (parse str ee))
66
67 (define e-matcher ee)