Module: mona/strings

mona/strings

String-related parsers and combinators.

Source:

Methods

alpha()

Returns a parser that matches a single non-unicode alphabetical character.

Source:
Example
parse(alpha(), "a"); // => "a"
parse(alpha(), "A"); // => "A"

alphaLower()

Returns a parser that matches a single non-unicode lowercase alphabetical character.

Source:
Example
parse(alphaLower(), "d"); // => "d"

alphanum(base)

Returns a parser that matches an alphanumeric character.

Parameters:
Name Type Argument Default Description
base Integer <optional>
10

Optional base for numeric parsing.

Source:
Example
parse(alphanum(), "1"); // => "1"
parse(alphanum(), "a"); // => "a"
parse(alphanum(), "A"); // => "A"

alphaUpper()

Returns a parser that matches a single non-unicode uppercase alphabetical character.

Source:
Example
parse(alphaUpper(), "D"); // => "D"

digit(base)

Returns a parser that parses a single digit character token from the input.

Parameters:
Name Type Argument Default Description
base Integer <optional>
10

Optional base for the digit.

Source:
Example
parse(digit(), "5"); // => "5"

noneOf(matches, caseSensitive, parser)

Returns a parser that fails if the next token or string matches one of the given inputs. If the third parser argument is given, that parser will be used to collect the actual value of noneOf.

Parameters:
Name Type Argument Default Description
matches String | Array

Characters or strings to match. If this argument is a string, it will be treated as if matches.split("") were passed in.

caseSensitive Boolean <optional>
true

Whether to match char case exactly.

parser Parser <optional>
token()

What to actually parse if none of the given matches succeed.

Source:
Example
parse(noneOf("abc"), "d"); // => "d"
parse(noneOf(["foo", "bar", "baz"]), "frob"); // => "f"
parse(noneOf(["foo", "bar", "baz"], true, text()), "frob"); // => "frob"

oneOf(matches, caseSensitive)

Returns a parser that succeeds if the next token or string matches one of the given inputs.

Parameters:
Name Type Argument Default Description
matches String | Array

Characters or strings to match. If this argument is a string, it will be treated as if matches.split("") were passed in.

caseSensitive Boolean <optional>
true

Whether to match char case exactly.

Source:
Example
parse(oneOf("abcd"), "c"); // => "c"
parse(oneOf(["foo", "bar", "baz"]), "bar"); // => "bar"

space()

Returns a parser that matches one whitespace character.

Source:
Example
parse(space(), "\r"); // => "\r"

spaces()

Returns a parser that matches one or more whitespace characters. Returns a single space character as its result, regardless of which whitespace characters were matched.

Source:
Example
parse(spaces(), "   \r\n\t \r \n"); // => " "

string(str, caseSensitive)

Returns a parser that succeeds if str matches the next str.length inputs, consuming the string and returning it as a value.

Parameters:
Name Type Argument Default Description
str String

String to match against.

caseSensitive Boolean <optional>
true

Whether to match char case exactly.

Source:
Example
parse(string("foo"), "foo"); // => "foo"

stringOf(parser)

Returns a string containing the concatenated results returned by applying parser. parser must be a combinator that returns an array of string parse results.

Parameters:
Name Type Description
parser Parser

Parser that results in an array of strings.

Source:
Example
parse(stringOf(collect(token())), "aaa"); // => "aaa"

text(parser, opts)

Returns a parser that collects between min and max tokens matching parser. The result is returned as a single string. This parser is essentially collect() for strings.

Parameters:
Name Type Argument Default Description
parser Parser <optional>
token()

Parser to use to collect the results.

opts Object <optional>
Properties
Name Type Argument Default Description
min Integer <optional>
0

Minimum number of matches.

max Integer <optional>
Infinity

Maximum number of matches.

Source:
Example
parse(text(), "abcde"); // => "abcde"
parse(text(noneOf("a")), "bcde"); // => "bcde"

trim(parser)

Returns a parser that trims any whitespace surrounding parser.

Parameters:
Name Type Description
parser Parser

Parser to match after cleaning up whitespace.

Source:
Example
parse(trim(token()), "    \r\n  a   \t"); // => "a"

trimLeft(parser)

Returns a parser that trims any leading whitespace before parser.

Parameters:
Name Type Description
parser Parser

Parser to match after cleaning up whitespace.

Source:
Example
parse(trimLeft(token()), "    \r\n  a"); // => "a"

trimRight(parser)

Returns a parser that trims any trailing whitespace before parser.

Parameters:
Name Type Description
parser Parser

Parser to match after cleaning up whitespace.

Source:
Example
parse(trimRight(token()), "a   \r\n"); // => "a"