This is part of a series of posts documenting Sprache:

Last time was all about parsing single characters, and all of the parsers returned char. This post goes through all of the primitive parsers that match multiple characters.

String

Parses a string of characters.

  • Parser<IEnumerable<char>> String(string s)

Note that the return type of this is IEnumerable<char> rather than string:

1
2
Parser<IEnumerable<char>> keywordReturn = Parse.String("return");
Assert.Equal(new[] {'r', 'e', 't', 'u', 'r', 'n'}, keywordReturn.Parse("return"));

Text

Convert a stream of characters to a string.

  • Parser<string> Text(this Parser<IEnumerable<char>> characters)

This doesn’t change what input the parser consumes, it just converts the type returned from the parser. Its intended for use with parser that return char streams like String, or parsers constructed using Many.

1
2
3
4
5
Parser<string> keywordReturn = Parse.String("return").Text();
Assert.Equal("return", keywordReturn.Parse("return"));

Parser<string> parser = Parse.CharExcept(',').Many().Text();
Assert.Equal("foo", parser.Parse("foo,bar"));

IgnoreCase

Parse a string in a case-insensitive fashion.

  • Parser<char> IgnoreCase(char c)
1
2
Parser<string> sprach = Parse.IgnoreCase("sprache").Text();
Assert.Equal("SprachE", sprach.Parse("SprachE"));

Number

Parses numbers as strings, note that the return type is string, so Text is not required:

1
2
3
4
Assert.Equal("123", Parse.Number.Parse("123"));

// This parser only returns "1", as '.' is not considered numeric
Assert.Equal("1", Parse.Number.Parse("1.23"));

Decimal

Parse a decimal number using the current culture’s separator character.

1
2
CultureInfo.CurrentCulture = new CultureInfo("fr-FR");
Assert.Equal("123,45", Parse.Decimal.Parse("123,45"));

DecimalInvariant

Parses a decimal number with the invariant culture.

1
2
CultureInfo.CurrentCulture = new CultureInfo("fr-FR");
Assert.Equal("123.45", Parse.DecimalInvariant.Parse("123.45"));

LineEnd

Parses line endings.

1
2
3
4
5
Assert.Equal("\r\n", Parse.LineEnd.Parse("\r\n"));
Assert.Equal("\n", Parse.LineEnd.Parse("\n"));

// Unexpected end of input reached; expected 
Assert.Throws<ParseException>(() => Parse.LineEnd.Parse("\r"));