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.
brag/tests/test-errors.rkt

140 lines
3.3 KiB
Racket

8 years ago
#lang racket/base
(module+ test
8 years ago
(require rackunit
(for-syntax racket/base))
8 years ago
;; The tests in this module make sure we produce proper error messages
;; on weird grammars.
8 years ago
(define-namespace-anchor anchor)
(define ns (namespace-anchor->namespace anchor))
(define (c prog)
(parameterize ([current-namespace ns]
[read-accept-reader #t])
(define ip (open-input-string prog))
(port-count-lines! ip)
(compile (read-syntax #f ip))))
8 years ago
;; Helper to let me quickly write compile-error checks.
(define-syntax (check-compile-error stx)
(syntax-case stx ()
[(_ prog expected-msg)
(quasisyntax/loc stx
(begin #,(syntax/loc stx
(check-exn (regexp (regexp-quote expected-msg))
(lambda ()
(c prog))))
#,(syntax/loc stx
(check-exn exn:fail:syntax?
(lambda ()
(c prog))))))]))
8 years ago
;; errors with position are sensitive to length of lang line
(define lang-line "#lang yaragg")
8 years ago
(check-compile-error (format "~a" lang-line)
"The grammar does not appear to have any rules")
8 years ago
(check-compile-error (format "~a\nfoo" lang-line)
"Error while parsing grammar near: foo [line=2, column=0, position=14]")
8 years ago
(check-compile-error (format "~a\nnumber : 42" lang-line)
"Error while parsing grammar near: 42 [line=2, column=9, position=23]")
8 years ago
(check-compile-error (format "~a\nnumber : 1" lang-line)
"Error while parsing grammar near: 1 [line=2, column=9, position=23]")
8 years ago
(check-compile-error "#lang yaragg\n x: NUMBER\nx:STRING"
"Rule x has a duplicate definition")
8 years ago
;; Check to see that missing definitions for rules also raise good syntax
;; errors:
8 years ago
(check-compile-error "#lang yaragg\nx:y"
"Rule y has no definition")
8 years ago
(check-compile-error "#lang yaragg\nnumber : 1flarbl"
"Rule 1flarbl has no definition")
8 years ago
(check-compile-error "#lang yaragg\nprogram: EOF"
"Token EOF is reserved and can not be used in a grammar")
8 years ago
;; Nontermination checks:
(check-compile-error "#lang yaragg\nx : x"
"Rule x has no finite derivation")
8 years ago
(check-compile-error #<<EOF
3 years ago
#lang yaragg
8 years ago
x : x y
y : "y"
EOF
"Rule x has no finite derivation")
8 years ago
; This should be illegal too:
(check-compile-error #<<EOF
3 years ago
#lang yaragg
8 years ago
a : "a" b
b : a | b
EOF
"Rule a has no finite derivation")
8 years ago
(check-compile-error #<<EOF
3 years ago
#lang yaragg
8 years ago
a : [b]
b : [c]
c : c
EOF
"Rule c has no finite derivation")
8 years ago
(check-compile-error #<<EOF
3 years ago
#lang yaragg
8 years ago
a : [b]
b : c
c : c
EOF
"Rule b has no finite derivation")
8 years ago
(check-compile-error #<<EOF
3 years ago
#lang yaragg
8 years ago
a : [a]
b : [b]
c : c
EOF
"Rule c has no finite derivation")
8 years ago
(check-compile-error #<<EOF
8 years ago
#lang racket/base
(require yaragg/examples/simple-line-drawing)
8 years ago
(define bad-parser (make-rule-parser crunchy))
EOF
"Rule crunchy is not defined in the grammar"
))