 ! Copyright (C) 2008 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors arrays assocs combinators io io.streams.string
-kernel math math.parser multi-methods namespaces qualified sets
+kernel math math.parser namespaces qualified sets
 quotations sequences splitting symbols vectors math.order
 unicode.categories strings regexp.backend regexp.utils
-unicode.case ;
+unicode.case words ;
 IN: regexp.parser
 
 FROM: math.ranges => [a,b] ;

: handle-left-parenthesis ( -- )
    peek1 CHAR: ? =
    [ drop1 (parse-special-group) ]
    [ nested-parse-regexp ] if ;

: handle-dot ( -- ) any-char push-stack ;
: handle-pipe ( -- ) pipe push-stack ;


DEFER: (parse-regexp)
: parse-special-group ( -- )
    ;
    ! beginning-of-group push-stack
    ! (parse-regexp) pop-stack make-non-capturing-group ;

ERROR: bad-special-group string ;

: nested-parse-regexp ( token ? -- )
    [ push-stack (parse-regexp) pop-stack ] dip
    [ <negation> ] when pop-stack boa push-stack ;

! non-capturing groups
: (parse-special-group) ( -- )
    read1 {
        { [ dup CHAR: # = ] ! comment
            [ drop comment-group f nested-parse-regexp pop-stack drop ] }
        { [ dup CHAR: : = ]
            [ drop non-capture-group f nested-parse-regexp ] }
        { [ dup CHAR: = = ]
            [ drop lookahead f nested-parse-regexp ] }
        { [ dup CHAR: ! = ]
            [ drop lookahead t nested-parse-regexp ] }
        { [ dup CHAR: > = ]
            [ drop non-capture-group f nested-parse-regexp ] }
        { [ dup CHAR: < = peek1 CHAR: = = and ]
            [ drop drop1 lookbehind f nested-parse-regexp ] }
        { [ dup CHAR: < = peek1 CHAR: ! = and ]
            [ drop drop1 lookbehind t nested-parse-regexp ] }
        [
            ":)" read-until
            [ swap prefix ] dip
            {
                { CHAR: : [ parse-options (parse-special-group) ] }
                { CHAR: ) [ parse-options ] }
                [ drop bad-special-group ]
            } case
