You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.3 KiB
Racket
51 lines
1.3 KiB
Racket
#lang racket/base
|
|
|
|
(require brag/examples/0n1
|
|
brag/support
|
|
rackunit)
|
|
|
|
(define (lex ip)
|
|
(port-count-lines! ip)
|
|
(lambda ()
|
|
(define next-char (read-char ip))
|
|
(cond [(eof-object? next-char)
|
|
(token eof)]
|
|
[(char=? next-char #\0)
|
|
(token "0" "0")]
|
|
[(char=? next-char #\1)
|
|
(token "1" "1")])))
|
|
|
|
|
|
(check-equal? (syntax->datum (parse #f (lex (open-input-string "1"))))
|
|
'(rule "1"))
|
|
|
|
|
|
(check-equal? (syntax->datum (parse #f (lex (open-input-string "01"))))
|
|
'(rule "0" "1"))
|
|
|
|
|
|
(check-equal? (syntax->datum (parse #f (lex (open-input-string "001"))))
|
|
'(rule "0" "0" "1"))
|
|
|
|
|
|
(check-exn exn:fail:parsing?
|
|
(lambda ()
|
|
(parse #f (lex (open-input-string "0")))))
|
|
|
|
(check-exn exn:fail:parsing?
|
|
(lambda ()
|
|
(parse #f (lex (open-input-string "10")))))
|
|
|
|
(check-exn exn:fail:parsing?
|
|
(lambda ()
|
|
(parse #f (lex (open-input-string "010")))))
|
|
|
|
|
|
;; This should fail predictably because we're passing in tokens
|
|
;; that the parser doesn't know.
|
|
(check-exn exn:fail:parsing?
|
|
(lambda () (parse '("zero" "one" "zero"))))
|
|
(check-exn (regexp (regexp-quote
|
|
"Encountered unexpected token \"zero\" (\"zero\") while parsing"))
|
|
(lambda () (parse '("zero" "one" "zero"))))
|