-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpeg-in-peg.rkt
22 lines (22 loc) · 1.46 KB
/
peg-in-peg.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(module anything racket
(provide (all-defined-out))
(require peg/peg)
(begin
(require "s-exp.rkt")
(define-peg/drop _ (* (or (or #\space #\tab #\newline) (and "//" (* (and (! #\newline) (any-char)))))))
(define-peg/drop SLASH (and "/" _))
(define-peg/tag name (and (or (range #\a #\z) (range #\A #\Z) #\_) (* (or (range #\a #\z) (range #\A #\Z) (range #\0 #\9) #\- #\_ #\.)) _))
(define-peg/tag rule (and name (or "<---" "<--" "<-" "<") _ pattern (? (and "->" _ s-exp _)) ";" _))
(define-peg/tag pattern (and alternative (* (and SLASH alternative))))
(define-peg/tag alternative (+ expression))
(define-peg/tag expression (and (? (and name (drop ":") _)) (? (and (or #\! #\& #\~) _)) primary (? (and (or #\* #\+ #\?) _))))
(define-peg/tag primary (or (and "(" _ pattern ")" _) (and "." _) literal charclass name))
(define-peg/tag literal (and (drop #\') (* (or (and (drop #\\) (or #\' #\\)) (and (! (or #\' #\\)) (any-char)))) (drop #\') _))
(define-peg/tag charclass (and (drop "[") (? "^") (+ (or cc-range cc-escape cc-single)) (drop "]") _))
(define-peg/tag cc-range (and cc-char (drop "-") cc-char))
(define-peg/tag cc-escape (and (drop #\\) (any-char)))
(define-peg/tag cc-single cc-char)
(define-peg cc-char (or (and (! cc-escape-char) (any-char)) "n" "t"))
(define-peg cc-escape-char (or "[" "]" "-" "^" "\\" "n" "t"))
(define-peg/tag peg (and _ (* import) (+ rule)))
(define-peg/tag import (and s-exp _ ";" _))))