factor/extra/regexp/regexp.factor

227 lines
6.0 KiB
Factor
Raw Normal View History

2007-11-26 13:59:04 -05:00
USING: arrays combinators kernel lazy-lists math math.parser
2007-11-25 04:56:04 -05:00
namespaces parser parser-combinators parser-combinators.simple
2007-12-02 07:07:32 -05:00
promises quotations sequences combinators.lib strings macros
assocs prettyprint.backend ;
2007-11-25 04:51:30 -05:00
IN: regexp
2007-12-02 07:07:32 -05:00
: or-predicates ( quots -- quot )
[ \ dup add* ] map [ [ t ] ] f short-circuit \ nip add ;
2007-11-25 04:51:30 -05:00
MACRO: fast-member? ( str -- quot )
[ dup ] H{ } map>assoc [ key? ] curry ;
: octal-digit? ( n -- ? )
CHAR: 0 CHAR: 7 between? ;
: decimal-digit? ( n -- ? )
CHAR: 0 CHAR: 9 between? ;
2007-12-02 07:07:32 -05:00
: hex-digit? ( n -- ? )
dup decimal-digit?
swap CHAR: a CHAR: f between? or ;
: control-char? ( n -- ? )
dup 0 HEX: 1f between?
swap HEX: 7f = or ;
: punct? ( n -- ? )
2007-12-02 07:07:32 -05:00
"!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" fast-member? ;
: c-identifier-char? ( ch -- ? )
2007-12-02 07:07:32 -05:00
dup alpha? swap CHAR: _ = or ;
: java-blank? ( n -- ? )
2007-11-26 13:59:04 -05:00
{
CHAR: \t CHAR: \n CHAR: \r
HEX: c HEX: 7 HEX: 1b
2007-12-02 07:07:32 -05:00
} fast-member? ;
2007-11-26 13:59:04 -05:00
: java-printable? ( n -- ? )
dup alpha? swap punct? or ;
2007-11-25 04:51:30 -05:00
: 'ordinary-char' ( -- parser )
2007-12-02 07:07:32 -05:00
[ "\\^*+?|(){}[" fast-member? not ] satisfy
[ [ = ] curry ] <@ ;
2007-12-02 07:17:12 -05:00
: 'octal-digit' ( -- parser ) [ octal-digit? ] satisfy ;
2007-11-25 04:51:30 -05:00
: 'octal' ( -- parser )
2007-12-02 07:17:12 -05:00
"0" token 'octal-digit' 1 3 from-m-to-n &>
[ oct> ] <@ ;
: 'hex-digit' ( -- parser ) [ hex-digit? ] satisfy ;
: 'hex' ( -- parser )
2007-12-02 07:07:32 -05:00
"x" token 'hex-digit' 2 exactly-n &>
"u" token 'hex-digit' 4 exactly-n &> <|>
2007-12-02 07:17:12 -05:00
[ hex> ] <@ ;
2007-12-02 07:07:32 -05:00
: satisfy-tokens ( assoc -- parser )
[ >r token r> [ nip ] curry <@ ] { } assoc>map <or-parser> ;
: 'simple-escape-char' ( -- parser )
{
2007-12-02 07:07:32 -05:00
{ "\\" CHAR: \\ }
{ "t" CHAR: \t }
{ "n" CHAR: \n }
{ "r" CHAR: \r }
{ "f" HEX: c }
{ "a" HEX: 7 }
{ "e" HEX: 1b }
} [ [ = ] curry ] assoc-map satisfy-tokens ;
: 'predefined-char-class' ( -- parser )
{
2007-12-02 07:07:32 -05:00
{ "d" [ digit? ] }
{ "D" [ digit? not ] }
{ "s" [ java-blank? ] }
{ "S" [ java-blank? not ] }
{ "w" [ c-identifier-char? ] }
{ "W" [ c-identifier-char? not ] }
} satisfy-tokens ;
: 'posix-character-class' ( -- parser )
{
2007-12-02 07:07:32 -05:00
{ "Lower" [ letter? ] }
{ "Upper" [ LETTER? ] }
{ "ASCII" [ 0 HEX: 7f between? ] }
{ "Alpha" [ Letter? ] }
{ "Digit" [ digit? ] }
{ "Alnum" [ alpha? ] }
{ "Punct" [ punct? ] }
{ "Graph" [ java-printable? ] }
{ "Print" [ java-printable? ] }
{ "Blank" [ " \t" member? ] }
{ "Cntrl" [ control-char? ] }
{ "XDigit" [ hex-digit? ] }
{ "Space" [ java-blank? ] }
} satisfy-tokens "p{" "}" surrounded-by ;
: 'simple-escape' ( -- parser )
'octal'
'hex' <|>
"c" token [ LETTER? ] satisfy &> <|>
any-char-parser <|>
[ [ = ] curry ] <@ ;
2007-12-02 07:07:32 -05:00
: 'escape' ( -- parser )
"\\" token
'simple-escape-char'
'predefined-char-class' <|>
'posix-character-class' <|>
'simple-escape' <|> &> ;
: 'any-char'
"." token [ drop [ drop t ] ] <@ ;
2007-11-25 04:51:30 -05:00
2007-12-02 07:07:32 -05:00
: 'char'
'any-char' 'escape' 'ordinary-char' <|> <|> [ satisfy ] <@ ;
2007-11-25 04:51:30 -05:00
DEFER: 'regexp'
TUPLE: group-result str ;
C: <group-result> group-result
: 'grouping'
'regexp' [ [ <group-result> ] <@ ] <@
2007-12-02 07:07:32 -05:00
"(" ")" surrounded-by ;
: 'range' ( -- parser )
any-char-parser "-" token <& any-char-parser <&>
2007-12-02 07:07:32 -05:00
[ first2 [ between? ] 2curry ] <@ ;
: 'character-class-term' ( -- parser )
'range'
'escape' <|>
[ "\\]" member? not ] satisfy [ [ = ] curry ] <@ <|> ;
2007-12-02 07:07:32 -05:00
: 'positive-character-class' ( -- parser )
"]" token [ drop [ CHAR: ] = ] ] <@ 'character-class-term' <*> <&:>
'character-class-term' <+> <|>
[ or-predicates ] <@ ;
2007-12-02 07:07:32 -05:00
: 'negative-character-class' ( -- parser )
"^" token 'positive-character-class' &>
[ [ not ] append ] <@ ;
: 'character-class' ( -- parser )
2007-12-02 07:07:32 -05:00
'negative-character-class' 'positive-character-class' <|>
"[" "]" surrounded-by [ satisfy ] <@ ;
: 'escaped-seq' ( -- parser )
any-char-parser <*> [ token ] <@ "\\Q" "\\E" surrounded-by ;
: 'simple' ( -- parser )
2007-12-02 07:07:32 -05:00
'escaped-seq'
2007-11-25 04:51:30 -05:00
'grouping' <|>
'char' <|>
'character-class' <|> ;
2007-11-25 04:51:30 -05:00
: 'interval' ( -- parser )
'simple' 'integer' "{" "}" surrounded-by <&> [ first2 exactly-n ] <@
'simple' 'integer' "{" ",}" surrounded-by <&> [ first2 at-least-n ] <@ <|>
'simple' 'integer' "{," "}" surrounded-by <&> [ first2 at-most-n ] <@ <|>
'simple' 'integer' "," token <& 'integer' <&> "{" "}" surrounded-by <&> [ first2 first2 from-m-to-n ] <@ <|> ;
2007-11-25 04:51:30 -05:00
: 'repetition' ( -- parser )
'simple' "*" token <& [ <*> ] <@
'simple' "+" token <& [ <+> ] <@ <|>
'simple' "?" token <& [ <?> ] <@ <|> ;
2007-12-02 07:17:12 -05:00
: 'term' ( -- parser )
'simple' 'repetition' 'interval' <|> <|>
<+> [ <and-parser> ] <@ ;
2007-11-25 04:51:30 -05:00
LAZY: 'regexp' ( -- parser )
'term' "|" token nonempty-list-of [ <or-parser> ] <@ ;
TUPLE: regexp source parser ;
2007-11-25 04:51:30 -05:00
: <regexp> dup 'regexp' just parse-1 regexp construct-boa ;
2007-11-25 04:51:30 -05:00
GENERIC: >regexp ( obj -- parser )
M: string >regexp <regexp> ;
2007-11-25 04:51:30 -05:00
M: object >regexp ;
: matches? ( string regexp -- ? )
>regexp regexp-parser just parse nil? not ;
2007-11-25 04:51:30 -05:00
! Literal syntax for regexps
2007-11-25 04:51:30 -05:00
: parse-regexp ( accum end -- accum )
lexer get dup skip-blank [
[ index* dup 1+ swap ] 2keep swapd subseq swap
] change-column <regexp> parsed ;
: R! CHAR: ! parse-regexp ; parsing
2007-11-25 04:51:30 -05:00
: R" CHAR: " parse-regexp ; parsing
: R# CHAR: # parse-regexp ; parsing
2007-11-25 04:51:30 -05:00
: R' CHAR: ' parse-regexp ; parsing
: R( CHAR: ) parse-regexp ; parsing
: R/ CHAR: / parse-regexp ; parsing
: R@ CHAR: @ parse-regexp ; parsing
: R[ CHAR: ] parse-regexp ; parsing
2007-11-25 04:51:30 -05:00
: R` CHAR: ` parse-regexp ; parsing
: R{ CHAR: } parse-regexp ; parsing
: R| CHAR: | parse-regexp ; parsing
: find-regexp-syntax ( string -- prefix suffix )
{
{ "R/ " "/" }
{ "R! " "!" }
{ "R\" " "\"" }
{ "R# " "#" }
{ "R' " "'" }
{ "R( " ")" }
{ "R@ " "@" }
{ "R[ " "]" }
{ "R` " "`" }
{ "R{ " "}" }
{ "R| " "|" }
} swap [ subseq? not nip ] curry assoc-find drop ;
M: regexp pprint*
dup regexp-source dup find-regexp-syntax pprint-string ;