add support for newline & tab literals in grammars

dev-srcloc
Matthew Butterick 8 years ago
parent 273753f1d5
commit ae07f3da64

@ -80,7 +80,10 @@
(lookup (string->symbol next-token) next-token no-position no-position)]
[(? char?)
(lookup (string->symbol (string next-token)) next-token no-position no-position)]
(lookup (cond
[(eqv? #\newline next-token) '|\n|] ; supports handling whitespace char
[(eqv? #\tab next-token) '|\t|] ; as literal token
[else (string->symbol (string next-token))]) next-token no-position no-position)]
;; Compatibility
[(? lex:token?)

@ -0,0 +1,6 @@
#lang brag
start: (tab | space | newline | letter)*
tab: '\t'
space: " "
newline: "\n"
letter: "x" | "y" | "z"

@ -31,12 +31,13 @@
(define lex/1
(lexer-src-pos
;; handle whitespace chars within quotes as literal tokens: "\n" "\t" '\n' '\t'
[(:: "'"
(:* (:or "\\'" (:~ "'" "\\")))
(:* (:or "\\'" "\\n" "\\t" (:~ "'" "\\")))
"'")
(token-LIT lexeme)]
[(:: "\""
(:* (:or "\\\"" (:~ "\"" "\\")))
(:* (:or "\\\"" "\\n" "\\t" (:~ "\"" "\\")))
"\"")
(token-LIT lexeme)]
["("

@ -0,0 +1,12 @@
#lang racket/base
(require brag/examples/whitespace
brag/support
rackunit)
(check-equal?
(parse-tree "\ty\n x\tz")
'(start (tab "\t") (letter "y") (newline "\n") (space " ") (letter "x") (tab "\t") (letter "z")))
(check-equal?
(parse-tree "\t\n \t")
'(start (tab "\t") (newline "\n") (space " ") (tab "\t")))
Loading…
Cancel
Save