-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.rkt
37 lines (31 loc) · 1.02 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
#lang racket
(require "grammar.rkt"
"typing/infer.rkt"
"parser.rkt"
"pretty.rkt"
"tree.rkt"
syntax/strip-context)
(provide (rename-out [peg-read read]
[peg-read-syntax read-syntax]))
(define (peg-read in)
(syntax->datum
(peg-read-syntax #f in)))
(define (peg-read-syntax path port)
(define grammar (parse port))
(let ([types (infer grammar)])
(if (eq? (cdr types) 'unsat)
(error "The grammar isn't well-typed! It can loop on some inputs.")
(datum->syntax
#f
`(module peg-mod racket
(provide parser
pretty
(all-from-out typed-peg-stack/tree))
(require typed-peg-stack/parser
typed-peg-stack/pretty
typed-peg-stack/tree
typed-peg-stack/typing/infer)
(define (parser s)
(peg-parse ,grammar s))
(define (pretty t)
(peg-pretty ,grammar t)))))))