From 1fbfc88b906787180b2713db0f4435d4580df40e Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 2 Dec 2007 07:17:12 -0500 Subject: [PATCH] More regexp changes --- extra/regexp/regexp-tests.factor | 4 +++ extra/regexp/regexp.factor | 56 ++++++++++---------------------- 2 files changed, 21 insertions(+), 39 deletions(-) diff --git a/extra/regexp/regexp-tests.factor b/extra/regexp/regexp-tests.factor index 8e72c5c2f8..021592a772 100644 --- a/extra/regexp/regexp-tests.factor +++ b/extra/regexp/regexp-tests.factor @@ -156,3 +156,7 @@ IN: regexp-tests [ t ] [ "S" "\\0123" matches? ] unit-test [ t ] [ "SXY" "\\0123XY" matches? ] unit-test +[ t ] [ "x" "\\x78" matches? ] unit-test +[ f ] [ "y" "\\x78" matches? ] unit-test +[ t ] [ "x" "\\u0078" matches? ] unit-test +[ f ] [ "y" "\\u0078" matches? ] unit-test diff --git a/extra/regexp/regexp.factor b/extra/regexp/regexp.factor index 51cda83cdc..1de1d9b16e 100644 --- a/extra/regexp/regexp.factor +++ b/extra/regexp/regexp.factor @@ -7,23 +7,6 @@ IN: regexp : or-predicates ( quots -- quot ) [ \ dup add* ] map [ [ t ] ] f short-circuit \ nip add ; -: exactly-n ( parser n -- parser' ) - swap ; - -: at-most-n ( parser n -- parser' ) - dup zero? [ - 2drop epsilon - ] [ - 2dup exactly-n - -rot 1- at-most-n <|> - ] if ; - -: at-least-n ( parser n -- parser' ) - dupd exactly-n swap <*> <&> ; - -: from-m-to-n ( parser m n -- parser' ) - >r [ exactly-n ] 2keep r> swap - at-most-n <&> ; - : octal-digit? ( n -- ? ) CHAR: 0 CHAR: 7 between? ; : decimal-digit? ( n -- ? ) CHAR: 0 CHAR: 9 between? ; @@ -58,24 +41,25 @@ MACRO: fast-member? ( str -- quot ) [ "\\^*+?|(){}[" fast-member? not ] satisfy [ [ = ] curry ] <@ ; -: 'octal-digit' ( -- parser ) - [ octal-digit? ] satisfy ; +: 'octal-digit' ( -- parser ) [ octal-digit? ] satisfy ; : 'octal' ( -- parser ) - "0" token - 'octal-digit' 3 exactly-n - 'octal-digit' 1 2 from-m-to-n <|> - &> [ oct> [ = ] curry ] <@ ; + "0" token 'octal-digit' 1 3 from-m-to-n &> + [ oct> ] <@ ; : 'hex-digit' ( -- parser ) [ hex-digit? ] satisfy ; : 'hex' ( -- parser ) "x" token 'hex-digit' 2 exactly-n &> "u" token 'hex-digit' 4 exactly-n &> <|> - [ hex> [ = ] curry ] <@ ; + [ hex> ] <@ ; : 'control-character' ( -- parser ) - "c" token [ LETTER? ] satisfy [ [ = ] curry ] <@ &> ; + "c" token [ LETTER? ] satisfy &> ; + +: 'simple-escape' ( -- parser ) + 'octal' 'hex' 'control-character' <|> <|> + [ [ = ] curry ] <@ ; : satisfy-tokens ( assoc -- parser ) [ >r token r> [ nip ] curry <@ ] { } assoc>map ; @@ -120,11 +104,9 @@ MACRO: fast-member? ( str -- quot ) : 'escape' ( -- parser ) "\\" token - 'simple-escape-char' + 'simple-escape' + 'simple-escape-char' <|> 'predefined-char-class' <|> - 'octal' <|> - 'hex' <|> - 'control-character' <|> 'posix-character-class' <|> &> ; : 'any-char' "." token [ drop [ drop t ] ] <@ ; @@ -132,7 +114,8 @@ MACRO: fast-member? ( str -- quot ) : 'char' 'any-char' 'escape' 'ordinary-char' <|> <|> [ satisfy ] <@ ; -: 'string' 'char' <+> [ ] <@ ; +: 'string' + 'char' <+> [ ] <@ ; DEFER: 'regexp' @@ -183,16 +166,11 @@ C: group-result 'term' 'integer' "," token <& 'integer' <&> "{" "}" surrounded-by <&> [ first2 first2 from-m-to-n ] <@ <|> ; : 'repetition' ( -- parser ) - 'term' - [ "*+?" member? ] satisfy <&> [ - first2 { - { CHAR: * [ <*> ] } - { CHAR: + [ <+> ] } - { CHAR: ? [ ] } - } case - ] <@ ; + 'term' "*" token <& [ <*> ] <@ + 'term' "+" token <& [ <+> ] <@ <|> + 'term' "?" token <& [ ] <@ <|> ; -: 'simple' 'term' 'repetition' <|> 'interval' <|> ; +: 'simple' 'term' 'repetition' 'interval' <|> <|> ; LAZY: 'union' ( -- parser ) 'simple'