     [ 0 get-line 1+ set-line ] [ get-column 1+ ] if
     set-column ;
 
-: (next) ( -- char ) ! this normalizes \r\n and \r
+! (next) normalizes \r\n and \r
+: (next) ( -- char )
     get-next read1
     2dup swap CHAR: \r = [
         CHAR: \n =

! 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 ;
IN: state-parser

! * Basic underlying words


C: <spot> spot

: get-char ( -- char ) spot get char>> ;
: set-char ( char -- ) spot get swap >>char drop ;
: get-line ( -- line ) spot get line>> ;
: set-line ( line -- ) spot get swap >>line drop ;
: get-column ( -- column ) spot get column>> ;
: set-column ( column -- ) spot get swap >>column drop ;
: get-next ( -- char ) spot get next>> ;
: set-next ( char -- ) spot get swap >>next drop ;

! * Errors
TUPLE: parsing-error line column ;

: parsing-error ( class -- obj )
    new
        get-line >>line
        get-column >>column ;
M: parsing-error summary ( obj -- str )
    [
        "Parsing error" print
        "Line: " write dup line>> .
        "Column: " write column>> .
    ] with-string-writer ;

TUPLE: expected < parsing-error should-be was ;
: expected ( should-be was -- * )
    \ expected parsing-error
        swap >>was
        swap >>should-be throw ;
M: expected summary ( obj -- str )
    [
        dup call-next-method write
        "Token expected: " write dup should-be>> print
        "Token present: " write was>> print
    ] with-string-writer ;

TUPLE: unexpected-end < parsing-error ;
: unexpected-end \ unexpected-end parsing-error throw ;
M: unexpected-end summary ( obj -- str )
    [
        call-next-method write
        "File unexpectedly ended." print
    ] with-string-writer ;

TUPLE: missing-close < parsing-error ;
: missing-close \ missing-close parsing-error throw ;
M: missing-close summary ( obj -- str )
    [
        call-next-method write
        "Missing closing token." print
    ] with-string-writer ;

SYMBOL: prolog-data

