Skip to content
mattbierner edited this page Dec 9, 2014 · 15 revisions

Parsers for language elements

Iterative

parse_lang.times(n, p)

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'] 

parse_lang.betweenTimes(min, max, p)

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'] 

Order

parse_lang.then(p, q)

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!

parse_lang.between(open, close, p)

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'] 

parse_lang.sepBy(sep, p)

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']

parse_lang.sepBy1(sep, p)

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']

parse_lang.sepEndBy(sep, p)

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,'

parse.sepBy1(sep, p)

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,'

parse_lang.endBy(sep, p)

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,'

parse_lang.endBy1(sep, p)

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,'

Chaining

parse_lang.chainl(op, def, p)

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]

parse_lang.chainl1(op, p)

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]

parse_lang.chainr(op, def, p)

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]]

parse_lang.chainr1(op, p)

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]]