support backslashes in patterns

hide-top-rule-name
Matthew Butterick 6 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) (define (unescape-lexeme lexeme quote-char)
;; convert the literal string representation back into an escape char with lookup table ;; 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))) (define pat (regexp (format "(?<=^~a\\\\).(?=~a$)" quote-char quote-char)))
(cond (cond
[(regexp-match pat lexeme) [(regexp-match pat lexeme)
@ -49,11 +49,11 @@
;; handle whitespace & escape chars within quotes as literal tokens: "\n" "\t" '\n' '\t' ;; 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 ;; 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 #\'))] (token-LIT (unescape-lexeme lexeme #\'))]
[(:: "\"" [(:: "\""
(:* (:or "\\\"" esc-chars (:~ "\"" "\\"))) (:or (:* (:or "\\\"" esc-chars (:~ "\"" "\\"))) "\\\\")
"\"") "\"")
(token-LIT (unescape-lexeme lexeme #\"))] (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