Add \Q...\E (escape all characters between \Q and \E)

release
Doug Coleman 2007-11-30 19:34:17 -06:00
parent b629d6ebe1
commit 8fadc570fc
2 changed files with 11 additions and 5 deletions

View File

@ -149,3 +149,8 @@ IN: regexp-tests
[ f ] [ "abc" "[\\p{Upper}]{3}" matches? ] unit-test
[ t ] [ "ABC" "[\\p{Upper}]{3}" matches? ] unit-test
[ t ] [ "" "\\Q\\E" matches? ] unit-test
[ f ] [ "a" "\\Q\\E" matches? ] unit-test
[ t ] [ "|*+" "\\Q|*+\\E" matches? ] unit-test
[ f ] [ "abc" "\\Q|*+\\E" matches? ] unit-test

View File

@ -109,11 +109,17 @@ IN: regexp
{ "\\p{Space}" [ java-blank? ] }
} [ first2 satisfy-token ] [ <|> ] map-reduce ;
: 'escaped-seq' ( -- parser )
"\\Q" token
any-char-parser <*> [ token ] <@ &>
"\\E" token <& ;
: 'escape-seq' ( -- parser )
'simple-escape-char'
'predefined-char-class' <|>
'octal' <|>
'hex' <|>
'escaped-seq' <|>
'control-character' <|>
'posix-character-class' <|> ;
@ -239,8 +245,3 @@ M: object >regexp ;
: R" CHAR: " parse-regexp ; parsing
: R' CHAR: ' parse-regexp ; parsing
: R` CHAR: ` parse-regexp ; parsing
! \Q \E
! Must escape to use as literals
! : meta-chars "[\\^$.|?*+()" ;