// Manages state for parsing operations outside of a Parser. // // The state is immutable; all update operations return a new state. // // Since #x is covariant, any ParseState can convert to ParseState, and // ParseState can convert to all other ParseState. concrete ParseState<|#x> { // Consumes all input and returns the result. @category consumeAll<#y> (Parser<#y>,String) -> (ErrorOr<#y>) } // A self-contained parser operation. // // Since #x is covariant, any Parser can convert to Parser, and Parser // can convert to all other Parser. @value interface Parser<|#x> { run (ParseContext) -> (ParseState<#x>) } // Parser context available when running a Parser. // // Since #x is covariant, any ParseContext can convert to ParseContext, and // ParseContext can convert to all other ParseContext. @value interface ParseContext<|#x> { // Continue computation. run<#y> (Parser<#y>) -> (ParseContext<#y>) runAndGet<#y> (Parser<#y>) -> (ParseContext,ErrorOr<#y>) getValue () -> (ErrorOr<#x>) // End computation and pass on the next state. convertError () -> (ParseState) setValue<#y> (ErrorOr<#y>) -> (ParseState<#y>) setBrokenInput (Formatted) -> (ParseState) toState () -> (ParseState<#x>) // Context metadata. getPosition () -> (String) atEof () -> (Bool) hasAnyError () -> (Bool) hasBrokenInput () -> (Bool) // Reading data. current () -> (Char) advance () -> (#self) }