support backslashes in patterns

hide-top-rule-name
Matthew Butterick 7 years ago
parent 54fb498c63
commit ce347b018d

@ -0,0 +1,2 @@
#lang brag
start: "a" "\"" "'" "\\" 'a' '"' '\'' '\\'

@ -1,2 +0,0 @@
#lang brag
start: "a" "\"" "'" 'a' '"' '\''

@ -36,7 +36,7 @@
(define (unescape-lexeme lexeme quote-char)
;; convert the literal string representation back into an escape char with lookup table
(define unescapes (hash "a" 7 "b" 8 "t" 9 "n" 10 "v" 11 "f" 12 "r" 13 "e" 27 "\"" 34 "'" 39))
(define unescapes (hash "a" 7 "b" 8 "t" 9 "n" 10 "v" 11 "f" 12 "r" 13 "e" 27 "\"" 34 "'" 39 "\\" 92))
(define pat (regexp (format "(?<=^~a\\\\).(?=~a$)" quote-char quote-char)))
(cond
[(regexp-match pat lexeme)
@ -49,11 +49,11 @@
;; handle whitespace & escape chars within quotes as literal tokens: "\n" "\t" '\n' '\t'
;; match the escaped version, and then unescape them before they become token-LITs
[(:: "'"
(:* (:or "\\'" esc-chars (:~ "'" "\\")))
(:or (:* (:or "\\'" esc-chars (:~ "'" "\\"))) "\\\\")
"'")
(token-LIT (unescape-lexeme lexeme #\'))]
[(:: "\""
(:* (:or "\\\"" esc-chars (:~ "\"" "\\")))
(:or (:* (:or "\\\"" esc-chars (:~ "\"" "\\"))) "\\\\")
"\"")
(token-LIT (unescape-lexeme lexeme #\"))]
["("

@ -0,0 +1,6 @@
#lang racket/base
(require brag/examples/quotation-marks-and-backslashes
brag/support
rackunit)
(check-equal? (parse-tree "a\"'\\a\"'\\") '(start "a" "\"" "'" "\\" "a" "\"" "'" "\\"))

@ -1,6 +0,0 @@
#lang racket/base
(require brag/examples/quotation-marks
brag/support
rackunit)
(check-equal? (parse-tree "a\"'a\"'") '(start "a" "\"" "'" "a" "\"" "'"))
Loading…
Cancel
Save