File size: 9,324 Bytes
3dcad1f |
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; PEG test suite.
;; Tests the parsing capabilities of (ice-9 peg). Could use more
;; tests for edge cases.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-module (test-suite test-peg)
:use-module (test-suite lib)
:use-module (ice-9 peg)
:use-module (ice-9 pretty-print)
:use-module (srfi srfi-1))
;; Doubled up for pasting into REPL.
(use-modules (test-suite lib))
(use-modules (ice-9 peg))
(use-modules (ice-9 pretty-print))
(use-modules (srfi srfi-1))
;; Evaluates an expression at the toplevel. Not the prettiest
;; solution to runtime issues ever, but m3h. Runs at toplevel so that
;; symbols are bound globally instead of in the scope of the pass-if
;; expression.
(define (eeval exp)
(eval exp (interaction-environment)))
(define make-prec (@@ (ice-9 peg) make-prec))
;; Maps the nonterminals defined in the PEG parser written as a PEG to
;; the nonterminals defined in the PEG parser written with
;; S-expressions.
(define grammar-mapping
'((grammar peg-grammar)
(pattern peg-pattern)
(alternative peg-alternative)
(suffix peg-suffix)
(primary peg-primary)
(literal peg-literal)
(charclass peg-charclass)
(CCrange charclass-range)
(CCsingle charclass-single)
(nonterminal peg-nonterminal)
(sp peg-sp)))
;; Transforms the nonterminals defined in the PEG parser written as a PEG to the nonterminals defined in the PEG parser written with S-expressions.
(define (grammar-transform x)
(let ((res (assoc x grammar-mapping)))
(if res (cadr res) x)))
;; Maps a function onto a tree (recurses until it finds atoms, then calls the function on the atoms).
(define (tree-map fn lst)
(if (list? lst)
(if (null? lst)
lst
(cons (tree-map fn (car lst))
(tree-map fn (cdr lst))))
(fn lst)))
;; Tests to make sure that we can parse a PEG defining a grammar for
;; PEGs, then uses that grammar to parse the same PEG again to make
;; sure we get the same result (i.e. make sure our PEG grammar
;; expressed as a PEG is equivalent to our PEG grammar expressed with
;; S-expressions).
(with-test-prefix "PEG Grammar"
(pass-if
"defining PEGs with PEG"
(and (eeval `(define-peg-string-patterns ,(@@ (ice-9 peg) peg-as-peg))) #t))
(pass-if
"equivalence of definitions"
(equal?
(peg:tree (match-pattern (@@ (ice-9 peg) peg-grammar) (@@ (ice-9 peg) peg-as-peg)))
(tree-map
grammar-transform
(peg:tree (match-pattern grammar (@@ (ice-9 peg) peg-as-peg)))))))
;; A grammar for pascal-style comments from Wikipedia.
(define comment-grammar
"Begin <-- '(*'
End <-- '*)'
C <- Begin N* End
N <- C / (!Begin !End Z)
Z <- .")
;; A short /etc/passwd file.
(define *etc-passwd*
"root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
messagebus:x:103:107::/var/run/dbus:/bin/false
")
;; A grammar for parsing /etc/passwd files.
(define-peg-string-patterns
"passwd <-- entry* !.
entry <-- login CO pass CO uid CO gid CO nameORcomment CO homedir CO shell NL*
login <-- text
pass <-- text
uid <-- [0-9]*
gid <-- [0-9]*
nameORcomment <-- text
homedir <-- path
shell <-- path
path <-- (SLASH pathELEMENT)*
pathELEMENT <-- (!NL !CO !'/' .)*
text <- (!NL !CO .)*
CO < ':'
NL < '\n'
SLASH < '/'")
;; Tests some actual parsing using PEGs.
(with-test-prefix "Parsing"
(eeval `(define-peg-string-patterns ,comment-grammar))
(pass-if
;; Pascal-style comment parsing
"simple comment"
(equal?
(match-pattern C "(*blah*)")
(make-prec 0 8 "(*blah*)"
'((Begin "(*") "blah" (End "*)")))))
(pass-if
"simple comment padded"
(equal?
(match-pattern C "(*blah*)abc")
(make-prec 0 8 "(*blah*)abc"
'((Begin "(*") "blah" (End "*)")))))
(pass-if
"nested comment"
(equal?
(match-pattern C "(*1(*2*)*)")
(make-prec 0 10 "(*1(*2*)*)"
'((Begin "(*") ("1" ((Begin "(*") "2" (End "*)"))) (End "*)")))))
(pass-if
"early termination"
(not (match-pattern C "(*blah")))
(pass-if
"never starts"
(not (match-pattern C "blah")))
;; /etc/passwd parsing
(pass-if
"/etc/passwd"
(equal?
(match-pattern passwd *etc-passwd*)
(make-prec 0 220 *etc-passwd*
'(passwd (entry (login "root") (pass "x") (uid "0") (gid "0") (nameORcomment "root") (homedir (path (pathELEMENT "root"))) (shell (path (pathELEMENT "bin") (pathELEMENT "bash")))) (entry (login "daemon") (pass "x") (uid "1") (gid "1") (nameORcomment "daemon") (homedir (path (pathELEMENT "usr") (pathELEMENT "sbin"))) (shell (path (pathELEMENT "bin") (pathELEMENT "sh")))) (entry (login "bin") (pass "x") (uid "2") (gid "2") (nameORcomment "bin") (homedir (path (pathELEMENT "bin"))) (shell (path (pathELEMENT "bin") (pathELEMENT "sh")))) (entry (login "sys") (pass "x") (uid "3") (gid "3") (nameORcomment "sys") (homedir (path (pathELEMENT "dev"))) (shell (path (pathELEMENT "bin") (pathELEMENT "sh")))) (entry (login "nobody") (pass "x") (uid "65534") (gid "65534") (nameORcomment "nobody") (homedir (path (pathELEMENT "nonexistent"))) (shell (path (pathELEMENT "bin") (pathELEMENT "sh")))) (entry (login "messagebus") (pass "x") (uid "103") (gid "107") nameORcomment (homedir (path (pathELEMENT "var") (pathELEMENT "run") (pathELEMENT "dbus"))) (shell (path (pathELEMENT "bin") (pathELEMENT "false")))))))))
;; Tests the functions for pulling data out of PEG Match Records.
(with-test-prefix "PEG Match Records"
(define-peg-pattern bs all (peg "'b'+"))
(pass-if
"basic parameter extraction"
(equal?
(let ((pm (search-for-pattern bs "aabbcc")))
`((string ,(peg:string pm))
(start ,(peg:start pm))
(end ,(peg:end pm))
(substring ,(peg:substring pm))
(tree ,(peg:tree pm))
(record? ,(peg-record? pm))))
'((string "aabbcc")
(start 2)
(end 4)
(substring "bb")
(tree (bs "bb"))
(record? #t)))))
;; PEG for parsing right-associative equations.
(define-peg-string-patterns
"expr <- sum
sum <-- (product ('+' / '-') sum) / product
product <-- (value ('*' / '/') product) / value
value <-- number / '(' expr ')'
number <-- [0-9]+")
;; Functions to actually evaluate the equations parsed with the PEG.
(define (parse-sum sum left . rest)
(if (null? rest)
(apply parse-product left)
(list (string->symbol (car rest))
(apply parse-product left)
(apply parse-sum (cadr rest)))))
(define (parse-product product left . rest)
(if (null? rest)
(apply parse-value left)
(list (string->symbol (car rest))
(apply parse-value left)
(apply parse-product (cadr rest)))))
(define (parse-value value first . rest)
(if (null? rest)
(string->number (cadr first))
(apply parse-sum (car rest))))
(define parse-expr parse-sum)
(define (eq-parse str) (apply parse-expr (peg:tree (match-pattern expr str))))
(with-test-prefix "Parsing right-associative equations"
(pass-if
"1"
(equal? (eq-parse "1") 1))
(pass-if
"1+2"
(equal? (eq-parse "1+2") '(+ 1 2)))
(pass-if
"1+2+3"
(equal? (eq-parse "1+2+3") '(+ 1 (+ 2 3))))
(pass-if
"1+2*3+4"
(equal? (eq-parse "1+2*3+4") '(+ 1 (+ (* 2 3) 4))))
(pass-if
"1+2/3*(4+5)/6-7-8"
(equal? (eq-parse "1+2/3*(4+5)/6-7-8")
'(+ 1 (- (/ 2 (* 3 (/ (+ 4 5) 6))) (- 7 8)))))
(pass-if
"1+1/2*3+(1+1)/2"
(equal? (eq-parse "1+1/2*3+(1+1)/2")
'(+ 1 (+ (/ 1 (* 2 3)) (/ (+ 1 1) 2))))))
;; PEG for parsing left-associative equations (normal ones).
(define-peg-string-patterns
"expr <- sum
sum <-- (product ('+' / '-'))* product
product <-- (value ('*' / '/'))* value
value <-- number / '(' expr ')'
number <-- [0-9]+")
;; Functions to actually evaluate the equations parsed with the PEG.
(define (make-left-parser next-func)
(lambda (sum first . rest)
(if (null? rest)
(apply next-func first)
(if (string? (cadr first))
(list (string->symbol (cadr first))
(apply next-func (car first))
(apply next-func (car rest)))
(car
(reduce
(lambda (l r)
(list (list (cadr r) (car r) (apply next-func (car l)))
(string->symbol (cadr l))))
'ignore
(append
(list (list (apply next-func (caar first))
(string->symbol (cadar first))))
(cdr first)
(list (append rest '("done"))))))))))
(define (parse-value value first . rest)
(if (null? rest)
(string->number (cadr first))
(apply parse-sum (car rest))))
(define parse-product (make-left-parser parse-value))
(define parse-sum (make-left-parser parse-product))
(define parse-expr parse-sum)
(define (eq-parse str) (apply parse-expr (peg:tree (match-pattern expr str))))
(with-test-prefix "Parsing left-associative equations"
(pass-if
"1"
(equal? (eq-parse "1") 1))
(pass-if
"1+2"
(equal? (eq-parse "1+2") '(+ 1 2)))
(pass-if
"1+2+3"
(equal? (eq-parse "1+2+3") '(+ (+ 1 2) 3)))
(pass-if
"1+2*3+4"
(equal? (eq-parse "1+2*3+4") '(+ (+ 1 (* 2 3)) 4)))
(pass-if
"1+2/3*(4+5)/6-7-8"
(equal? (eq-parse "1+2/3*(4+5)/6-7-8")
'(- (- (+ 1 (/ (* (/ 2 3) (+ 4 5)) 6)) 7) 8)))
(pass-if
"1+1/2*3+(1+1)/2"
(equal? (eq-parse "1+1/2*3+(1+1)/2")
'(+ (+ 1 (* (/ 1 2) 3)) (/ (+ 1 1) 2)))))
|