Skip to content

Instantly share code, notes, and snippets.

@dmitry-vsl
Last active August 29, 2015 14:15
Show Gist options
  • Save dmitry-vsl/6b1363a8e5311e31577b to your computer and use it in GitHub Desktop.
Save dmitry-vsl/6b1363a8e5311e31577b to your computer and use it in GitHub Desktop.
/* @flow */
type ParseContext = {string: string; position: number;};
type Error = {reason: string; position: number};
type Result<T> = {value: ?T; error: ?Error};
type Parser<T> = (c:ParseContext) => [ParseContext, Result<T>];
function then<T,U>(p: Parser<T>, f: (t: T) => Parser<U>): Parser<U>{
return function(cxt){
var [newCxt, result] = p(cxt);
if(result.value != null){
var [newCxt2, result2] = f(result.value)(newCxt);
if(result2.value != null){
return [newCxt2, {value: result2.value, error: null}];
}else{
return [cxt, {error: result2.error, value: null}];
}
}else{
return [cxt, {error: result.error, value: null}];
}
}
}
function eof(c): Parser<[]>{
return function(c){
if(c.position >= c.string.length){
return [c, {value: [], error: null}];
}else{
var result = {
value: null,
error: {reason: "Expected EOF", position: c.position}
};
return [c, result];
}
};
};
function string(s): Parser<string>{
return function(cxt){
if(cxt.string.slice(cxt.position,s.length) == s){
return [
{position: cxt.position + s.length, string: cxt.string},
{value: s, error: null}
];
}else{
return [
cxt,
{value: null,
error: {
reason: `Expected '${s}'`,
position: cxt.position
}
}
];
}
};
}
var fooeof = then(string('foo1'),(eof));
console.log(fooeof({string: "foo", position: 0}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment