Skip to content
Matt Bierner edited this page Mar 7, 2015 · 8 revisions

Parsers for working specifically with string data. These parses can provide more detailed error messages and other conveniences compared the core parsers which work for any data types.

Text

parse_text.character(c)

Parser that consumes character c.

var p = parse_text.character(`a`);
parse.run(p, ''); // Error! UnexpectError eof
parse.run(p, 'b'); // Error! ExpectError expected 'a' found 'b'
parse.run(p, 'a'); // 'a'
parse.run(p, 'abc'); // 'a'

Does a string equality compare, converting both c and found value to strings before comparing them.

var p = parse_text.character('1');
parse.run(p, stream.from[1, 2, 3]); // 1

parse_text.oneOf(chars)

Parser that consumes any character in chars.

var p = parse_text.oneOf('abc');

parse.run(p, ''); // Error! UnexpectError eof
parse.run(p, 'b'); // 'b'
parse.run(p, 'a'); // 'a'
parse.run(p, 'abc'); // 'a'
parse.run(p, 'z'); // Error!

parse_text.noneOf(chars)

Parser that consumes any character except those in chars.

var p = parse_text.noneOf('abc');

parse.run(p, ''); // Error! Unexpected eof
parse.run(p, 'x'); // 'x'
parse.run(p, 'a'); // Error!
parse.run(p, 'c'); // Error

parse_text.string(str)

Parser that consumes string str. Does a string equality compare, converting characters of str and found value to strings before comparing them.

Unlike consuming a sequence of characters with parse.sequence, parse_text.string does backtrack.

var p = parse_text.string('abc');
parse.run(p, ''); // Error! UnexpectError eof
parse.run(p, 'ab'); // Error! UnexpectError eof
parse.run(p, 'azbc'); // Error! ExpectError expected 'b' found 'z'
parse.run(p, 'abc'); // 'abc'
parse.run(p, 'abcdef'); // 'abc'

parse_text.trie(strs)

Parser that constructs a trie parser for an array of strings 'strs'. This has the same behavior as a choice parser of 'strs', but for large arrays with overlaps, will be more efficient.

var p = parse_text.trie(['ab', 'abc', 'abd']);
parse.run(p, ''); // Error! UnexpectError eof
parse.run(p, 'a'); // Error! UnexpectError eof
parse.run(p, 'ab'); // 'ab'
parse.run(p, 'abc'); // 'abc'
parse.run(p, 'abd'); // 'abd'

Built In

parse_text.anyChar

Parser that consumes any character.

var p = parse_text.anyChar;
parse.run(p, ''); // Error! UnexpectError eof
parse.run(p, 'a'); // 'a'
parse.run(p, 'z'); // 'z'
parse.run(p, '!'); // '!'
parse.run(p, 'abc'); // 'a'
parse.run(p, ' '); // Error!

parse_text.letter

Parser that consumes any letter character.

var p = parse_text.letter;
parse.run(p, ''); // Error! UnexpectError eof
parse.run(p, 'a'); // 'a'
parse.run(p, 'z'); // 'z'
parse.run(p, '!'); // Error!
parse.run(p, '2'); // Error!
parse.run(p, ' '); // Error!

parse_text.space

Parser that consumes any space character.

var p = parse_text.space;
parse.run(p, ''); // Error! UnexpectError eof
parse.run(p, 'a'); // Error!
parse.run(p, ' '); // ' '

parse_text.digit

Parser that consumes any digit character.

var p = parse_text.digit;
parse.run(p, ''); // Error! UnexpectError eof
parse.run(p, 'a'); // Error!
parse.run(p, ' '); // Error!
parse.run(p, '2'); // '2'
parse.run(p, '7'); // '7'