Skip to content

incremental

Matt Bierner edited this page Apr 3, 2014 · 8 revisions

Almost any parser can be run incrementally

Starting Parsing

runInc(p, [userData])

Start parsing p incrementally. Returns a paused computation. Once the computation is finished, returns success results directly and throws error results.

var c = runInc(
    eager <| many(character('a')));

var c1 = provideString('a', c);
finish c1; // 'a'

var c2 = provideString('aa', c1);
finish c2; // 'aaa';

var c3 = provideString('x', c2);
finish c3; // 'aaa';

runInc(p, state)

Start parsing p incrementally with a custom state. Returns a paused computation. Once the computation completes, returns success results directly and throws error results.

parserInc(p, ud, ok, err)

Start parsing p incrementally with a custom user state. Returns a paused computation. Once the computation is finished, runs ok with success results or err with failure results and throws error results. The computations are only evaluated when finish is called

var c = parseInc(
    eager <| time(2, character('a')),
    null,
    \x -> console.log("suc" + x),
    \x -> console.log("error" + x));

var c1 = provideString('a', c);
finish c1; // logcs error

var c2 = provideString('a', c1);
finish c2; // logs 'aa';

var c3 = provideString('x', c2);
finish c3; // 'aa';

parserIncState(p, state, ok, err)

Start parsing p incrementally with a custom state and completions.

Feed

provide(stream, r)

Provide a chunk of data stream to paused parser r. stream is a potentially infinite Nu stream. Runs as much as possible before pausing.

var c = runInc(
    eager <| many(anyToken));

var c1 = provide(stream.from('a'), c);
finish c1; // 'a'

var c2 = provide(stream.from('bc'), c1);
finish c2; // 'abc';

// Feeding alt input to `c1`.
var c3 = provide(stream.from('yx'), c1);
finish c3; // 'axy';

provideStream(arr, r)

Provide a chunk of data arr to paused parser r. arr is a array of values. Runs as much as possible before pausing.

var c = runInc(
    eager <| many(anyToken));

var c1 = provideString(['a', 'b'], c);
finish c1; // 'ab'

// This is the same as
var c1 = provideStream('ab', c);
finish c1; // 'ab'

finish(r)

Signal paused parser r that no more input is coming. Returns result of completions. r is not mutated and can be feed more data, so finish can be used to inspect the working state.

Clone this wiki locally