 ! Copyright (C) 2005, 2006 Daniel Ehrenberg
 ! See http://factorcode.org/license.txt for BSD license.
 USING: io io.streams.string kernel math namespaces sequences
-strings circular prettyprint debugger ascii ;
+strings circular prettyprint debugger ascii sbufs fry inspector
+accessors sequences.lib ;
 IN: state-parser
 
 ! * Basic underlying words

    [ 0 get-line 1+ set-line ] [ get-column 1+ ] if
    set-column ;

: (next) ( -- char ) ! this normalizes \r\n and \r
    get-next read1
    2dup swap CHAR: \r = [
        CHAR: \n =

    #! Take the substring of a string starting at spot
    #! from code until the quotation given is true and
    #! advance spot to after the substring.
    10 <sbuf> [
        '[ @ [ t ] [ get-char , push f ] if ] skip-until
    ] keep >string ; inline

: take-rest ( -- string )
    [ f ] take-until ;

: take-char ( ch -- string )
    [ dup get-char = ] take-until nip ;

TUPLE: not-enough-characters < parsing-error ;
: not-enough-characters
    \ not-enough-characters parsing-error throw ;
M: not-enough-characters summary ( obj -- str )
    [
        call-next-method write
        "Not enough characters" print
    ] with-string-writer ;

: take ( n -- string )
    [ 1- ] [ <sbuf> ] bi [
        '[ drop get-char [ next , push f ] [ t ] if* ] attempt-each drop
    ] keep get-char [ over push ] when* >string ;

: pass-blank ( -- )
    #! Advance code past any whitespace, including newlines
