-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.rkt
88 lines (68 loc) · 3 KB
/
reader.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
#lang racket
(require "grammar.rkt"
"peg-ast.rkt"
"peg-wf.rkt"
racket/path
racket/syntax)
(provide (rename-out [peg-read read]
[peg-read-syntax read-syntax]))
(define (peg-read in)
(syntax->datum
(peg-read-syntax #f in)))
(define (msg-loops l)
(string-append "The following non-terminals are left-recursive: "
(string-join l ", ") )
)
(define (msg-ty-erros l)
(string-append* "The following expressions do not type:\n "
(map (lambda (s) (string-append " " (pe->err-string s) "\n")) l)))
(define (error-msgs pl)
(match pl
[(TyErr '() ty) (error (msg-ty-erros ty))]
[(TyErr lp '()) (error (msg-loops lp))]
[(TyErr lp ty) (error (string-append (msg-loops lp) "\n" (msg-ty-erros ty)))])
)
(define parse-base-name "parse")
(define (prefix-from-file pth)
(string->symbol (string-append (path->string (file-name-from-path (path-replace-extension pth #""))) ":") ))
(define (peg-read-syntax path port)
(define grammar (parse port))
(let ([types (infer-types grammar)]
[fname (prefix-from-file path)] )
(if (not (satisfied? types))
(error-msgs (get-errors types) )
(datum->syntax
#f
`(module peg-parser racket
(provide (all-from-out peg-parser/peg-simple-recognizer)
list-grammar
(all-from-out peg-parser/peg-ast)
(prefix-out ,fname parse)
(prefix-out ,fname parse-from-nt)
(prefix-out ,fname parse-file)
(prefix-out ,fname parse-file-from-nt)
)
(require peg-parser/peg-simple-recognizer
peg-parser/peg-ast
)
(define grm ,grammar)
(define (parse s)
(peg-parse grm (open-input-string s)))
(define (parse-from-nt i s)
(peg-parse-from grm i (open-input-string s)))
(define (parse-file fname)
(let* ([s (open-input-file fname #:mode 'text)]
[ast (peg-parse grm s)])
( close-input-port fname)
)
)
(define (parse-file-from-nt ntname fname)
(let* ([s (open-input-file fname #:mode 'text)]
[ast (parse-from-nt grm ntname s)])
( close-input-port fname))
)
(define (list-grammar)
(foldr string-append "" (peg->string grm)))
)
))
))