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-lexer.rkt

80 lines
1.9 KiB
Racket

8 years ago
#lang racket/base
(module+ test
8 years ago
(require yaragg/rules/lexer
rackunit
yaragg/parser-tools/lex)
8 years ago
(define (l s)
(define t (lex/1 (open-input-string s)))
(list (token-name (position-token-token t))
(token-value (position-token-token t))
(position-offset (position-token-start-pos t))
(position-offset (position-token-end-pos t))))
8 years ago
;; WARNING: the offsets are not in terms of file positions. So they
;; start counting at 1, not 0.
(check-equal? (l " hi")
'(ID "hi" 2 4))
8 years ago
(check-equal? (l " hi")
'(ID "hi" 3 5))
8 years ago
(check-equal? (l "hi")
'(ID "hi" 1 3))
8 years ago
(check-equal? (l "# foobar\nhi")
'(ID "hi" 10 12))
8 years ago
(check-equal? (l "# foobar\rhi")
'(ID "hi" 10 12))
8 years ago
(check-equal? (l "# foobar\r\nhi")
'(ID "hi" 11 13))
8 years ago
(check-equal? (l "hi:")
'(RULE_HEAD "hi:" 1 4))
8 years ago
(check-equal? (l "hi :")
'(RULE_HEAD "hi :" 1 7))
8 years ago
(check-equal? (l "|")
'(PIPE "|" 1 2))
8 years ago
(check-equal? (l "(")
'(LPAREN "(" 1 2))
8 years ago
(check-equal? (l "[")
'(LBRACKET "[" 1 2))
8 years ago
(check-equal? (l ")")
'(RPAREN ")" 1 2))
8 years ago
(check-equal? (l "]")
'(RBRACKET "]" 1 2))
8 years ago
;; 220111: lexer now converts single-quoted lexemes
;; to standard Racket-style double-quoted string literal
(check-equal? (l "'hello'")
'(LIT "\"hello\"" 1 8))
8 years ago
(check-equal? (l "'he\\'llo'")
'(LIT "\"he'llo\"" 1 10))
8 years ago
(check-equal? (l "/")
'(HIDE "/" 1 2))
8 years ago
(check-equal? (l " /")
'(HIDE "/" 2 3))
8 years ago
(check-equal? (l "@")
'(SPLICE "@" 1 2))
(check-equal? (l " @")
'(SPLICE "@" 2 3))
; lexer skips kwarg
(check-equal? (l "#:prefix-out val:")
(list 'EOF eof 18 18)))