-USING: combinators kernel lazy-lists math math.parser
+USING: arrays combinators kernel lazy-lists math math.parser
 namespaces parser parser-combinators parser-combinators.simple
-promises sequences strings ;
+promises sequences sequences.lib strings ;
 USING: continuations io prettyprint ;
 IN: regexp
 
 : 'any-char'
     "." token [ drop any-char-parser ] <@ ;
 
+: escaped-char
+    {
+        { CHAR: d [ [ digit? ] satisfy ] }
+        { CHAR: D [ [ digit? not ] satisfy ] }
+        { CHAR: s [ [ blank? ] satisfy ] }
+        { CHAR: S [ [ blank? not ] satisfy ] }
+        [ ]
+    } case ;
+
 : 'escaped-char'
-    "\\" token any-char-parser &> ;
+    "\\" token any-char-parser &> [ escaped-char ] <@ ;
 
 : 'ordinary-char'
-    [ "*+?|(){}" member? not ] satisfy ;
+    [ "^*+?|(){}[]" member? not ] satisfy [ 1string token ] <@ ;
 
 : 'char' 'escaped-char' 'ordinary-char' <|> ;
 
-: 'string' 'char' <+> [ >string token ] <@ ;
+: 'string' 'char' <+> [ [ <&> ] reduce* ] <@ ;
 
 : exactly-n ( parser n -- parser' )
     swap <repetition> and-parser construct-boa ;


: <regexp> 'regexp' just parse-1 ;


GENERIC: >regexp ( obj -- parser )
M: string >regexp 'regexp' just parse-1 ;
M: object >regexp ;

    ] <@ ;

: 'interval'
    'term' "{" token <& 'integer' <&> "}" token <& [ first2 exactly-n ] <@
    'term' "{" token <& 'integer' <&> "," token <& "}" token <&
        [ first2 at-least-n ] <@ <|>
    'term' "{" token <& "," token <& 'integer' <&> "}" token <&
        [ first2 at-most-n ] <@ <|>
    'term' "{" token <& 'integer' <&> "," token <& 'integer' <:&> "}" token <&
        [ first3 from-m-to-n ] <@ <|> ;

: 'repetition'
    'term'
