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
|
2007-12-03 19:20:47 -05:00
|
|
|
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
|
|
|
|
2007-12-03 19:20:47 -05:00
|
|
|
MACRO: fast-member? ( str -- quot )
|
|
|
|
[ dup ] H{ } map>assoc [ key? ] curry ;
|
2007-11-30 20:20:02 -05:00
|
|
|
|
2007-12-03 19:20:47 -05:00
|
|
|
: octal-digit? ( n -- ? )
|
|
|
|
CHAR: 0 CHAR: 7 between? ;
|
|
|
|
|
|
|
|
: decimal-digit? ( n -- ? )
|
|
|
|
CHAR: 0 CHAR: 9 between? ;
|
2007-11-30 20:20:02 -05:00
|
|
|
|
2007-12-02 07:07:32 -05:00
|
|
|
: hex-digit? ( n -- ? )
|
|
|
|
dup decimal-digit?
|
|
|
|
swap CHAR: a CHAR: f between? or ;
|
2007-11-30 20:20:02 -05:00
|
|
|
|
|
|
|
: control-char? ( n -- ? )
|
|
|
|
dup 0 HEX: 1f between?
|
|
|
|
swap HEX: 7f = or ;
|
|
|
|
|
|
|
|
: punct? ( n -- ? )
|
2007-12-02 07:07:32 -05:00
|
|
|
"!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" fast-member? ;
|
2007-11-30 20:20:02 -05:00
|
|
|
|
|
|
|
: c-identifier-char? ( ch -- ? )
|
2007-12-02 07:07:32 -05:00
|
|
|
dup alpha? swap CHAR: _ = or ;
|
2007-11-30 20:20:02 -05:00
|
|
|
|
|
|
|
: java-blank? ( n -- ? )
|
2007-11-26 13:59:04 -05:00
|
|
|
{
|
2007-11-30 20:20:02 -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
|
|
|
|
2007-11-30 20:20:02 -05:00
|
|
|
: java-printable? ( n -- ? )
|
|
|
|
dup alpha? swap punct? or ;
|
2007-11-25 04:51:30 -05:00
|
|
|
|
2007-11-30 20:20:02 -05:00
|
|
|
: 'ordinary-char' ( -- parser )
|
2007-12-02 07:07:32 -05:00
|
|
|
[ "\\^*+?|(){}[" fast-member? not ] satisfy
|
|
|
|
[ [ = ] curry ] <@ ;
|
2007-11-30 20:20:02 -05:00
|
|
|
|
2007-12-02 07:17:12 -05:00
|
|
|
: 'octal-digit' ( -- parser ) [ octal-digit? ] satisfy ;
|
2007-11-25 04:51:30 -05:00
|
|
|
|
2007-11-30 20:20:02 -05:00
|
|
|
: 'octal' ( -- parser )
|
2007-12-02 07:17:12 -05:00
|
|
|
"0" token 'octal-digit' 1 3 from-m-to-n &>
|
|
|
|
[ oct> ] <@ ;
|
2007-11-30 20:20:02 -05:00
|
|
|
|
|
|
|
: '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-11-30 20:20:02 -05:00
|
|
|
|
2007-12-02 07:07:32 -05:00
|
|
|
: satisfy-tokens ( assoc -- parser )
|
|
|
|
[ >r token r> [ nip ] curry <@ ] { } assoc>map <or-parser> ;
|
2007-11-30 20:20:02 -05:00
|
|
|
|
|
|
|
: '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 ;
|
2007-11-30 20:20:02 -05:00
|
|
|
|
|
|
|
: '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 ;
|
2007-11-30 20:20:02 -05:00
|
|
|
|
|
|
|
: '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 ;
|
|
|
|
|
2007-12-03 19:20:47 -05:00
|
|
|
: 'simple-escape' ( -- parser )
|
|
|
|
'octal'
|
|
|
|
'hex' <|>
|
|
|
|
"c" token [ LETTER? ] satisfy &> <|>
|
|
|
|
any-char-parser <|>
|
|
|
|
[ [ = ] curry ] <@ ;
|
|
|
|
|
2007-12-02 07:07:32 -05:00
|
|
|
: 'escape' ( -- parser )
|
|
|
|
"\\" token
|
2007-12-03 19:20:47 -05:00
|
|
|
'simple-escape-char'
|
2007-11-30 20:20:02 -05:00
|
|
|
'predefined-char-class' <|>
|
2007-12-03 19:20:47 -05:00
|
|
|
'posix-character-class' <|>
|
|
|
|
'simple-escape' <|> &> ;
|
2007-11-30 20:20:02 -05:00
|
|
|
|
2007-12-03 19:20:47 -05:00
|
|
|
: '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 ;
|
2007-11-26 18:19:29 -05:00
|
|
|
|
2007-11-30 20:20:02 -05:00
|
|
|
: 'range' ( -- parser )
|
2007-11-26 18:19:29 -05:00
|
|
|
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-11-26 18:19:29 -05:00
|
|
|
|
2007-12-02 07:07:32 -05:00
|
|
|
: 'positive-character-class' ( -- parser )
|
|
|
|
"]" token [ drop [ CHAR: ] = ] ] <@ 'character-class-term' <*> <&:>
|
|
|
|
'character-class-term' <+> <|>
|
|
|
|
[ or-predicates ] <@ ;
|
2007-11-30 20:20:02 -05:00
|
|
|
|
2007-12-02 07:07:32 -05:00
|
|
|
: 'negative-character-class' ( -- parser )
|
|
|
|
"^" token 'positive-character-class' &>
|
|
|
|
[ [ not ] append ] <@ ;
|
2007-11-26 18:19:29 -05:00
|
|
|
|
2007-11-30 20:20:02 -05:00
|
|
|
: '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 ;
|
2007-11-26 18:19:29 -05:00
|
|
|
|
2007-12-03 19:20:47 -05:00
|
|
|
: 'simple' ( -- parser )
|
2007-12-02 07:07:32 -05:00
|
|
|
'escaped-seq'
|
2007-11-25 04:51:30 -05:00
|
|
|
'grouping' <|>
|
2007-12-03 19:20:47 -05:00
|
|
|
'char' <|>
|
|
|
|
'character-class' <|> ;
|
2007-11-25 04:51:30 -05:00
|
|
|
|
2007-11-30 20:20:02 -05:00
|
|
|
: 'interval' ( -- parser )
|
2007-12-03 19:20:47 -05:00
|
|
|
'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
|
|
|
|
2007-11-30 20:20:02 -05:00
|
|
|
: 'repetition' ( -- parser )
|
2007-12-03 19:20:47 -05:00
|
|
|
'simple' "*" token <& [ <*> ] <@
|
|
|
|
'simple' "+" token <& [ <+> ] <@ <|>
|
|
|
|
'simple' "?" token <& [ <?> ] <@ <|> ;
|
2007-12-02 07:17:12 -05:00
|
|
|
|
2007-12-03 19:20:47 -05:00
|
|
|
: 'term' ( -- parser )
|
|
|
|
'simple' 'repetition' 'interval' <|> <|>
|
|
|
|
<+> [ <and-parser> ] <@ ;
|
2007-11-25 04:51:30 -05:00
|
|
|
|
|
|
|
LAZY: 'regexp' ( -- parser )
|
2007-12-03 19:20:47 -05:00
|
|
|
'term' "|" token nonempty-list-of [ <or-parser> ] <@ ;
|
|
|
|
|
|
|
|
TUPLE: regexp source parser ;
|
2007-11-25 04:51:30 -05:00
|
|
|
|
2007-12-03 19:20:47 -05:00
|
|
|
: <regexp> dup 'regexp' just parse-1 regexp construct-boa ;
|
2007-11-25 04:51:30 -05:00
|
|
|
|
|
|
|
GENERIC: >regexp ( obj -- parser )
|
2007-12-03 19:20:47 -05:00
|
|
|
|
|
|
|
M: string >regexp <regexp> ;
|
|
|
|
|
2007-11-25 04:51:30 -05:00
|
|
|
M: object >regexp ;
|
|
|
|
|
2007-12-03 19:20:47 -05:00
|
|
|
: matches? ( string regexp -- ? )
|
|
|
|
>regexp regexp-parser just parse nil? not ;
|
2007-11-25 04:51:30 -05:00
|
|
|
|
2007-12-03 19:20:47 -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 ;
|
|
|
|
|
2007-12-03 19:20:47 -05:00
|
|
|
: R! CHAR: ! parse-regexp ; parsing
|
2007-11-25 04:51:30 -05:00
|
|
|
: R" CHAR: " parse-regexp ; parsing
|
2007-12-03 19:20:47 -05:00
|
|
|
: R# CHAR: # parse-regexp ; parsing
|
2007-11-25 04:51:30 -05:00
|
|
|
: R' CHAR: ' parse-regexp ; parsing
|
2007-12-03 19:20:47 -05:00
|
|
|
: 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
|
2007-12-03 19:20:47 -05:00
|
|
|
: 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 ;
|