-
Notifications
You must be signed in to change notification settings - Fork 0
/
peg-capture-recognizer.rkt
146 lines (125 loc) · 5.15 KB
/
peg-capture-recognizer.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
143
144
145
;#lang typed/racket
#lang typed/racket/no-check
(require "peg-ast.rkt")
(provide
PegTree
(struct-out PTFail)
;(struct-out PTSym)
;(struct-out PTStr)
(struct-out PTVar)
;(struct-out PTList)
peg-parse
peg-parse-from
peg-parse-file
peg-parse-file-from
)
(define stk : (Listof Natural) (list) )
(define (mrk [f : Input-Port]) (set! stk (cons (file-position f) stk)))
(define (rstr [f : Input-Port])
(cond
[(null? stk) (error "nothing to restore")]
[else (begin
(file-position f (car stk))
(set! stk (cdr stk)))]
)
)
(define (mrk-pop)
(cond
[(null? stk) stk]
[else (set! stk (cdr stk))]
)
)
(define-type PegTree (Union PTFail PTVar (Listof Any)))
(struct PTFail () #:transparent)
(struct PTVar ([var : String] [t : PegTree]) #:transparent)
(define (flatten [x : PegTree] ) : String
(match x
[(? char? c) (string c)]
[(? string? s) s]
[(? symbol? _) (~a x)]
[(PTVar _ t) (flatten t)]
[(cons _ _) (foldr string-append "" (map flatten x))]
)
)
(define (mk-pcat [l : PegTree] [r : PegTree] ) : PegTree
(match (cons l r)
[(cons (PTFail) _) (PTFail)]
[(cons _ (PTFail)) (PTFail)]
[(cons (PTVar s t) (cons _ _)) (cons l r)]
[(cons (PTVar s t) (list)) (list l)]
[(cons (list) (PTVar s t) ) (list r)]
[(cons (cons _ _) (PTVar s t) ) (append l (list r))]
[(cons (cons _ _) (list) ) l]
[(cons (list) (cons _ _) ) r]
[(cons (cons _ _) (cons _ _) ) (append l r)]
[(cons x y) (list x y)]
)
)
(define (spe-parse [g : PEG] [pe : PE] [f : Input-Port ] ) : PegTree
(match pe
[(Eps _) (list)]
[(Any _) (let [(ch1 : (Union Char EOF) (read-char f))]
(cond [(eof-object? ch1) (PTFail)]
[else (list ch1) ]))]
[(Sym _ ch) (let [(ch1 : (Union Char EOF) (read-char f))]
(cond [(eof-object? ch1) (PTFail)]
[(char=? ch ch1) (list ch1)]
[else (PTFail)]))]
[(Rng _ s e) (let [(ch1 : (Union Char EOF) (read-char f))]
(cond [(eof-object? ch1) (PTFail)]
[(and (char>? ch1 s) (char<? ch1 e)) (list ch1)]
[else (PTFail)]))]
[(Var _ ig s) (let ([r : PegTree (spe-parse g (nonTerminal g s) f)])
(match r
[(PTFail) (PTFail)]
[x (if ig x (PTVar s x))])
)]
[(Annot _ 'Silent e) (match (spe-parse g e f)
[(PTFail) (PTFail)]
[_ (list)] )]
[(Annot _ 'Flat e) (match (spe-parse g e f)
[(PTFail) (PTFail)]
[x (list (flatten x))] )]
[(Cat _ e d) (let ([te (spe-parse g e f)])
(cond [(not (PTFail? te)) (mk-pcat te (spe-parse g d f))]
[else (PTFail)]))]
[(Alt _ e d) (begin (mrk f)
(match (spe-parse g e f)
[(PTFail) (begin (rstr f)
(spe-parse g d f))]
[x (begin (mrk-pop)
x) ]))]
[(Rep _ e) (begin (mrk f)
(match (spe-parse g e f)
[(PTFail) (begin (rstr f) (list) )]
[x (begin (mrk-pop)
(mk-pcat x (spe-parse g pe f)))]))]
[(Not _ e) (begin (mrk f)
(match (spe-parse g e f)
[(PTFail) (begin (rstr f) (list) )]
[x (begin (rstr f)
(PTFail))]))]
)
)
(define (peg-parse [g : PEG] [f : Input-Port ] ) : PegTree
(spe-parse g (PEG-start g) f)
)
(define (peg-parse-from [g : PEG] [start : String] [f : Input-Port ] ) : PegTree
(spe-parse g (nonTerminal g start) f)
)
(define (peg-parse-file [g : PEG] [filename : String] ) : PegTree
(let ([h : Input-Port (open-input-file filename #:mode 'text)])
(begin
(spe-parse g (PEG-start g) h)
(close-input-port h)
)
)
)
(define (peg-parse-file-from [g : PEG] [start : String] [filename : String] ) : PegTree
(let ([h : Input-Port (open-input-file filename #:mode 'text)])
(begin
(spe-parse g (nonTerminal g start) h)
(close-input-port h)
)
)
)