This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
forked from michaelballantyne/racket-peg-ee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.rkt
100 lines (86 loc) · 2.06 KB
/
core.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
#lang racket/base
(provide
; core forms
eps
seq
alt
*
!
:
=>
token
text
char
:src-span
; interface macros
define-peg
parse
; default #%peg-datum implementation
#%peg-datum
make-text
; result datatype
(struct-out parse-result)
; interfaces for macro definitions and local-expansion
(for-syntax
local-expand-peg
peg-macro
gen:peg-macro
peg-macro?
peg-macro/c
peg-macro-transform
peg
peg-literals))
(require
"private/forms.rkt"
"private/runtime.rkt"
(for-syntax
"private/env-reps.rkt"
"private/syntax-classes.rkt"
"private/expand.rkt"
"private/leftrec-check.rkt"
"private/compile.rkt"))
(require
(for-syntax
racket/base
syntax/parse
racket/syntax
ee-lib
syntax/id-table
(rename-in syntax/parse [define/syntax-parse def/stx])))
; Interface macros
(define-syntax define-peg
(module-macro
(syntax-parser
[(_ name:id peg-e:peg)
(when (not (eq? 'module (syntax-local-context)))
(raise-syntax-error #f "define-peg only works in module context" this-syntax))
(def/stx impl (generate-temporary #'name))
(syntax-local-lift-module-end-declaration
#'(define-peg-pass2 name peg-e))
#'(begin
(define impl (define-peg-rhs name))
(begin-for-syntax
(record-compiled-id! #'name #'impl))
(define-syntax name (peg-non-terminal-rep)))])))
(define-syntax define-peg-pass2
(syntax-parser
[(_ name peg-e)
(define-values (peg-e^) (expand-peg #'peg-e))
(lift-leftrec-check! #'name peg-e^)
#'(begin)]))
(define-syntax define-peg-rhs
(syntax-parser
[(_ name)
(define e (free-id-table-ref expanded-defs (syntax-local-introduce #'name)))
(define e^ (compile-peg e #'in))
#`(lambda (in) #,e^)]))
(define-syntax parse
(expression-macro
(syntax-parser
[(_ peg-name:nonterm-id in-e:expr)
(compile-parse #'peg-name #'in-e)])))
; Default implementation of #%peg-datum interposition point
(define-syntax #%peg-datum
(peg-macro
(syntax-parser
[(_ (~or* v:char v:string)) #'(text v)])))