-
Notifications
You must be signed in to change notification settings - Fork 11
lang
Parsers for language elements
Parser that consumes p
exactly n
times and succeeds with a Nu stream of results.
var p = parse_lang.times(2, parse_text.character('a'));
parse.run(p, ''); // Error!
parse.run(p, 'z'); // Error!
parse.run(p, 'a'); // Error!
parse.run(p, 'aa'); // stream of ['a', 'a']
parse.run(p, 'aaaa'); // stream of ['a', 'a']
Parser that consumes p
between 'min' and 'max' times and succeeds with a Nu stream of results.
var p = parse_lang.betweenTimes(2, 4, parse_text.character('a'));
parse.run(p, ''); // Error!
parse.run(p, 'z'); // Error!
parse.run(p, 'a'); // Error!
parse.run(p, 'aa'); // stream of ['a', 'a']
parse.run(p, 'aaaa'); // stream of ['a', 'a', 'a', 'a']
parse.run(p, 'aaaba'); // stream of ['a', 'a', 'a']
parse.run(p, 'aaaaaa'); // stream of ['a', 'a', 'a', 'a']
Parser that consumes p
then q
, returning result from p
.
var p = parse_lang.then(
parse_text.character('a'),
parse_text.character('b'));
parse.run(p, 'ab'); // 'a'
parse.run(p, 'abc'); // 'a'
parse.run(p, 'a'); // Error!
parse.run(p, 'aab'); // Error!
parse.run(p, 'ba'); // Error!
Parser that parses 'open' then p
then 'close' and returns result from p
.
var p = parse_lang.between(parse_text.character('['), parse_text.character(']'),
parse.many(parse_text.character('a')));
parse.run(p, ''); // Error!
parse.run(p, '['); // Error!
parse.run(p, ']'); // Error!
parse.run(p, '[]'); // stream of []
parse.run(p, '[a]'); // stream of ['a']
parse.run(p, '[aa'); // Error!
parse.run(p, '[aaa]'); // stream of ['a', 'a', 'a']
Parser that consumes p
separated by sep
zero or more times. Returns Nu stream of results.
var p = parse_lang.sepBy(parse_text.character(','),
parse_text.character('a'));
parse.run(p, ''); // stream of []
parse.run(p, 'x'); // stream of []
parse.run(p, ','); // stream of []
parse.run(p, 'a'); // stream of ['a']
parse.run(p, 'abc'); // stream of ['a']
parse.run(p, 'a,a'); // stream of ['a', 'a']
parse.run(p, 'a,xyz'); // stream of ['a']
Parser that consumes p
separated by sep
one or more times. Returns Nu stream of results.
var p = parse_lang.sepBy1(parse_text.character(','),
parse_text.character('a'));
parse.run(p, ''); // Error!
parse.run(p, 'x'); // Error!
parse.run(p, ','); // Error!
parse.run(p, 'a'); // stream of ['a']
parse.run(p, 'abc'); // stream of ['a']
parse.run(p, 'a,a'); // stream of ['a', 'a']
parse.run(p, 'a,xyz'); // stream of ['a']
Parser that consumes p
separated by sep
zero or more times and optional ended by end. Returns Nu stream of results from p
.
var p = parse.then(
parse_lang.sepEndBy(parse_text.character(','),
parse_text.character('a')),
parse.eof());
parse.run(p, ''); // stream of []
parse.run(p, 'x'); // Error, expected elf
parse.run(p, ','); // stream of []
parse.run(p, 'a'); // stream of ['a']
parse.run(p, 'a,'); // stream of ['a']
parse.run(p, 'abc'); // Error, expected eof after consuming 'a'
parse.run(p, 'a,a'); // stream of ['a', 'a']
parse.run(p, 'a,a,'); // stream of ['a', 'a']
parse.run(p, 'a,xyz'); // Error, expected eof after consuming 'a,'
Parser that consumes p
separated by sep
one or more times. Returns Nu stream of results.
var p = parse.then(
parse_lang.sepEndBy1(parse_text.character(','),
parse_text.character('a')),
parse.eof());
parse.run(p, ''); //Error
parse.run(p, 'x'); // Error
parse.run(p, ','); // Error
parse.run(p, 'a'); // stream of ['a']
parse.run(p, 'a,'); // stream of ['a']
parse.run(p, 'abc'); // Error, expected eof after consuming 'a'
parse.run(p, 'a,a'); // stream of ['a', 'a']
parse.run(p, 'a,a,'); // stream of ['a', 'a']
parse.run(p, 'a,xyz'); // Error, expected eof after consuming 'a,'
Parser that consumes p
separated by sep
zero or more times and ended by sep
. Returns Nu stream of results from p
.
var p = parse.then(
parse_lang.endBy(parse_text.character(','),
parse_text.character('a')),
parse.eof());
parse.run(p, ''); // Error
parse.run(p, 'x'); // Error
parse.run(p, ','); // Stream of []
parse.run(p, 'a'); // Error, expected ending ',' after consuming 'a'
parse.run(p, 'a,'); // stream of ['a']
parse.run(p, 'abc'); // Error, expected ',' after consuming 'a'
parse.run(p, 'a,a'); // Error, expected ending ',' after consuming 'a'
parse.run(p, 'a,a,'); // stream of ['a', 'a']
parse.run(p, 'a,xyz'); // Error, expected eof after consuming 'a,'
Parser that consumes p
separated by sep
one or more times and ended by sep
. Returns Nu stream of results from p
.
var p = parse.then(
parse_lang.endBy1(parse_text.character(','),
parse_text.character('a')),
parse.eof());
parse.run(p, ''); //Error
parse.run(p, 'x'); // Error
parse.run(p, ','); // Error
parse.run(p, 'a'); // Error, expected ending ',' after consuming 'a'
parse.run(p, 'a,'); // stream of ['a']
parse.run(p, 'abc'); // Error, expected ',' after consuming 'a'
parse.run(p, 'a,a'); // Error, expected ending ',' after consuming 'a'
parse.run(p, 'a,a,'); // stream of ['a', 'a']
parse.run(p, 'a,xyz'); // Error, expected eof after consuming 'a,'
Parser that consumes p
zero or more times separated by op
and returns result of left associative application of resulting function from op
to p
values. Return 'def' if p
consumed zero times.
var p = parse_lang.chainl(
parse.next(
parse_text.character('.'),
parse.always((x, y) -> [x, y])),
[],
parse_text.digit));
parse.run(p, ''); // []
parse.run(p, '4'); // 4
parse.run(p, 'a'); // []
parse.run(p, '4.a'); // Error, expected digit found 'a'
parse.run(p, '4.5.1'); // [[4, 5], ,1]
Parser that consumes p
one or more times separated by op
and returns result of left associative application of resulting function from op
to p
values.
var p = parse_lang.chainl1(
parse.next(
parse_text.character('.'),
parse.always(\x y -> [x, y])),
parse_text.digit));
parse.run(p, ''); // Error
parse.run(p, '4'); // 4
parse.run(p, 'a'); // Error
parse.run(p, '4.a'); // Error, expected digit found 'a'
parse.run(p, '4.5'); // [4, 5]
parse.run(p, '4.5.1'); // [[4, 5], 1]
Parser that consumes p
zero or more times separated by op
and returns result of left associative application of resulting function from op
to p
values. Return 'def' if p
consumed zero times.
var p = parse_lang.chainl(
parse.next(parse_text.character('.'), parse.always(\x, y -> [x, y])),
[],
parse_text.digit));
parse.run(p, ''); // []
parse.run(p, '4'); // 4
parse.run(p, 'a'); // []
parse.run(p, '4.a'); // Error, expected digit found 'a'
parse.run(p, '4.5.1'); // [4, [5, ,1]]
Parser that consumes p
one or more times separated by op
and returns result of right associative application of resulting function from op
to p
values.
var p = parse_lang.chainr1(
parse.next(
parse_text.character('.'),
parse.always(\x y -> [x, y])),
parse_text.digit));
parse.run(p, ''); // Error
parse.run(p, '4'); // 4
parse.run(p, 'a'); // Error
parse.run(p, '4.a'); // Error, expected digit found 'a'
parse.run(p, '4.5'); // [4, 5]
parse.run(p, '4.5.1'); // [4, [5, 1]]