Documentation
ΒΆ
Overview ΒΆ
Package chomp provides a parser combinator library for chomping strings (a byte at a time) in Go. A more intuitive way to parse text without having to write a single regex
Index ΒΆ
- Variables
- type Combinator
- func All[T Result](c ...Combinator[T]) Combinator[[]string]
- func Alpha() Combinator[string]
- func Alpha0() Combinator[string]
- func Alphanumeric() Combinator[string]
- func Alphanumeric0() Combinator[string]
- func Any(str string) Combinator[string]
- func AnyChar() Combinator[string]
- func BinaryDigit() Combinator[string]
- func BinaryDigit0() Combinator[string]
- func BracketAngled() Combinator[string]
- func BracketSquare() Combinator[string]
- func Char(c rune) Combinator[string]
- func Crlf() Combinator[string]
- func Delimited[T, U, V Result](left Combinator[T], str Combinator[U], right Combinator[V]) Combinator[U]
- func Digit() Combinator[string]
- func Digit0() Combinator[string]
- func Eol() Combinator[string]
- func Escaped(normal Combinator[string], escape rune, escapable Combinator[string]) Combinator[string]
- func EscapedTransform(normal Combinator[string], escape rune, transform Combinator[string]) Combinator[string]
- func First[T Result](c ...Combinator[T]) Combinator[T]
- func Flatten(c Combinator[[]string]) Combinator[string]
- func HexDigit() Combinator[string]
- func HexDigit0() Combinator[string]
- func I(c Combinator[[]string], i int) Combinator[string]
- func Many[T Result](c Combinator[T]) Combinator[[]string]
- func ManyN[T Result](c Combinator[T], n uint) Combinator[[]string]
- func Multispace() Combinator[string]
- func Multispace0() Combinator[string]
- func Newline() Combinator[string]
- func NoneOf(str string) Combinator[string]
- func Not(str string) Combinator[string]
- func NotLineEnding() Combinator[string]
- func OctalDigit() Combinator[string]
- func OctalDigit0() Combinator[string]
- func OneOf(str string) Combinator[string]
- func Opt[T Result](c Combinator[T]) Combinator[T]
- func Pair[T, U Result](c1 Combinator[T], c2 Combinator[U]) Combinator[[]string]
- func Parentheses() Combinator[string]
- func Peek[T Result](c Combinator[T]) Combinator[T]
- func Prefixed(c, pre Combinator[string]) Combinator[string]
- func QuoteDouble() Combinator[string]
- func QuoteSingle() Combinator[string]
- func Repeat[T Result](c Combinator[T], n uint) Combinator[[]string]
- func RepeatRange[T Result](c Combinator[T], n, m uint) Combinator[[]string]
- func S(c Combinator[string]) Combinator[[]string]
- func Satisfy(pred func(rune) bool) Combinator[string]
- func SepPair[T, U, V Result](c1 Combinator[T], sep Combinator[U], c2 Combinator[V]) Combinator[[]string]
- func Space() Combinator[string]
- func Space0() Combinator[string]
- func Suffixed(c, suf Combinator[string]) Combinator[string]
- func Tab() Combinator[string]
- func Tag(str string) Combinator[string]
- func TagNoCase(str string) Combinator[string]
- func Take(n uint) Combinator[string]
- func TakeUntil1(str string) Combinator[string]
- func Until(str string) Combinator[string]
- func While(p Predicate) Combinator[string]
- func WhileN(p Predicate, n uint) Combinator[string]
- func WhileNM(p Predicate, n, m uint) Combinator[string]
- func WhileNot(p Predicate) Combinator[string]
- func WhileNotN(p Predicate, n uint) Combinator[string]
- func WhileNotNM(p Predicate, n, m uint) Combinator[string]
- type CombinatorParseError
- type MappedCombinator
- type ParserError
- type Predicate
- type RangedParserError
- type RangedParserExec
- type Result
Constants ΒΆ
This section is empty.
Variables ΒΆ
var ( // IsDigit determines whether a rune is a decimal digit. A rune is classed // as a digit if it is between the ASCII range of '0' or '9', or if it belongs // within the Unicode [Nd] category. // // [Nd]: https://www.fileformat.info/info/unicode/category/Nd/list.htm IsDigit = isDigit{} // IsLetter determines if a rune is a letter. A rune is classed as a letter // if it is between the ASCII range of 'a' and 'z' (including its uppercase // equivalents), or it belongs within any of the Unicode letter categories: // [Lu] [LI] [Lt] [Lm] [Lo]. // // [Lu]: https://www.fileformat.info/info/unicode/category/Lu/list.htm // [LI]: https://www.fileformat.info/info/unicode/category/Ll/list.htm // [Lt]: https://www.fileformat.info/info/unicode/category/Lt/list.htm // [Lm]: https://www.fileformat.info/info/unicode/category/Lm/list.htm // [Lo]: https://www.fileformat.info/info/unicode/category/Lo/list.htm IsLetter = isLetter{} // IsAlphanumeric determines whether a rune is a decimal digit or a letter. // This convenience method wraps the existing [IsDigit] and [IsLetter] // predicates. IsAlphanumeric = isAlphanumeric{} // IsLineEnding determines whether a rune is one of the following ASCII // line ending characters '\r' or '\n'. IsLineEnding = isLineEnding{} // IsSpace determines whether a rune is a space character. A rune is classed // as a space if it is either a space ' ' or a tab '\t'. IsSpace = isSpace{} // IsMultispace determines whether a rune is a whitespace character. A rune // is classed as whitespace if it is a space ' ', tab '\t', newline '\n', // or carriage return '\r'. IsMultispace = isMultispace{} // IsHexDigit determines whether a rune is a hexadecimal digit. A rune is // classed as a hex digit if it is between '0'-'9', 'a'-'f', or 'A'-'F'. IsHexDigit = isHexDigit{} // IsOctalDigit determines whether a rune is an octal digit. A rune is classed // as an octal digit if it is between '0' and '7'. IsOctalDigit = isOctalDigit{} // IsBinaryDigit determines whether a rune is a binary digit. A rune is classed // as a binary digit if it is either '0' or '1'. IsBinaryDigit = isBinaryDigit{} )
Functions ΒΆ
This section is empty.
Types ΒΆ
type Combinator ΒΆ
Combinator is a higher-order function capable of parsing text under a defined condition. Combinators can be combined to form more complex parsers. Upon success, a combinator will return both the unparsed and parsed text. All combinators are strict and must parse its input. Any failure to do so should raise a CombinatorParseError.
func All ΒΆ
func All[T Result](c ...Combinator[T]) Combinator[[]string]
All will match the input text against a series of [Combinator]s. All combinators must match in the order provided.
chomp.All(
chomp.Tag("Hello"),
chomp.Until("W"),
chomp.Tag("World!"))("Hello, World!")
// ("", []string{"Hello", ", ", "World!"}, nil)
func Alpha ΒΆ added in v0.5.0
func Alpha() Combinator[string]
Alpha matches one or more ASCII or Unicode letters. Equivalent to While(IsLetter).
chomp.Alpha()("Hello123")
// ("123", "Hello", nil)
func Alpha0 ΒΆ added in v0.5.0
func Alpha0() Combinator[string]
Alpha0 matches zero or more ASCII or Unicode letters. Equivalent to WhileN(IsLetter, 0).
chomp.Alpha0()("123Hello")
// ("123Hello", "", nil)
func Alphanumeric ΒΆ added in v0.5.0
func Alphanumeric() Combinator[string]
Alphanumeric matches one or more alphanumeric characters. Equivalent to While(IsAlphanumeric).
chomp.Alphanumeric()("Hello123!")
// ("!", "Hello123", nil)
func Alphanumeric0 ΒΆ added in v0.5.0
func Alphanumeric0() Combinator[string]
Alphanumeric0 matches zero or more alphanumeric characters. Equivalent to WhileN(IsAlphanumeric, 0).
chomp.Alphanumeric0()("!Hello123")
// ("!Hello123", "", nil)
func Any ΒΆ
func Any(str string) Combinator[string]
Any must match at least one character from the provided sequence at the beginning of the input text. Parsing stops upon the first unmatched character.
chomp.Any("eH")("Hello, World!")
// ("llo, World!", "He", nil)
func AnyChar ΒΆ added in v0.5.0
func AnyChar() Combinator[string]
AnyChar matches any single character at the beginning of the input text.
chomp.AnyChar()("Hello")
// ("ello", "H", nil)
func BinaryDigit ΒΆ added in v0.5.0
func BinaryDigit() Combinator[string]
BinaryDigit matches one or more binary digits (0-1). Equivalent to While(IsBinaryDigit).
chomp.BinaryDigit()("1010 rest")
// (" rest", "1010", nil)
func BinaryDigit0 ΒΆ added in v0.5.0
func BinaryDigit0() Combinator[string]
BinaryDigit0 matches zero or more binary digits (0-1). Equivalent to WhileN(IsBinaryDigit, 0).
chomp.BinaryDigit0()("234")
// ("234", "", nil)
func BracketAngled ΒΆ
func BracketAngled() Combinator[string]
BracketAngled will match any text delimited (or surrounded) by a pair of <angled brackets>.
chomp.BracketAngled()("<Hello, World!>")
// ("", "Hello, World!", nil)
func BracketSquare ΒΆ
func BracketSquare() Combinator[string]
BracketSquare will match any text delimited (or surrounded) by a pair of [square brackets].
chomp.BracketSquare()("[Hello, World!]")
// ("", "Hello, World!", nil)
func Char ΒΆ added in v0.5.0
func Char(c rune) Combinator[string]
Char matches a specific single character at the beginning of the input text.
chomp.Char(',')(",,rest")
// (",rest", ",", nil)
func Crlf ΒΆ
func Crlf() Combinator[string]
Crlf must match either a CR '\r' or CRLF '\r\n' line ending.
chomp.Crlf()("\r\nHello")
// ("Hello", "\r\n", nil)
func Delimited ΒΆ
func Delimited[T, U, V Result](left Combinator[T], str Combinator[U], right Combinator[V]) Combinator[U]
Delimited will match a series of combinators against the input text. All must match, with the delimiters being discarded.
chomp.Delimited(
chomp.Tag("'"),
chomp.Tag("Hello, World!"),
chomp.Tag("'"))("'Hello, World!'")
// ("", "Hello, World!", nil)
func Digit ΒΆ added in v0.5.0
func Digit() Combinator[string]
Digit matches one or more decimal digits. Equivalent to While(IsDigit).
chomp.Digit()("123abc")
// ("abc", "123", nil)
func Digit0 ΒΆ added in v0.5.0
func Digit0() Combinator[string]
Digit0 matches zero or more decimal digits. Equivalent to WhileN(IsDigit, 0).
chomp.Digit0()("abc123")
// ("abc123", "", nil)
func Eol ΒΆ added in v0.3.0
func Eol() Combinator[string]
Eol will scan and return any text before any ASCII line ending characters. Line endings are discarded.
chomp.Eol()(`Hello, World!\nIt's a great day!`)
// ("It's a great day!", "Hello, World!", nil)
func Escaped ΒΆ added in v0.5.0
func Escaped(normal Combinator[string], escape rune, escapable Combinator[string]) Combinator[string]
Escaped parses a string containing escape sequences. It takes a normal content combinator, an escape character, and a combinator that matches valid characters after the escape. The escape sequences are preserved in the output as-is.
chomp.Escaped(chomp.While(chomp.IsLetter), '\\', chomp.OneOf(`"n\`))(`Hello\"World`)
// ("", `Hello\"World`, nil)
func EscapedTransform ΒΆ added in v0.5.0
func EscapedTransform(normal Combinator[string], escape rune, transform Combinator[string]) Combinator[string]
EscapedTransform parses a string containing escape sequences and transforms them. It takes a normal content combinator, an escape character, and a transform function that converts escape sequences to their actual values.
transform := func(s string) (string, string, error) {
switch s[0] {
case 'n':
return s[1:], "\n", nil
case '"':
return s[1:], "\"", nil
case '\\':
return s[1:], "\\", nil
}
return s, "", errors.New("invalid escape")
}
chomp.EscapedTransform(chomp.While(chomp.IsLetter), '\\', transform)(`Hello\nWorld`)
// ("", "Hello\nWorld", nil)
func First ΒΆ
func First[T Result](c ...Combinator[T]) Combinator[T]
First will match the input text against a series of [Combinator]s. Matching stops as soon as the first combinator succeeds. One Combinator must match. For better performance, try and order the combinators from most to least likely to match.
chomp.First(
chomp.Tag("Good Morning"),
chomp.Tag("Hello"))("Good Morning, World!")
// (" ,World!", "Good Morning", nil)
func Flatten ΒΆ added in v0.4.0
func Flatten(c Combinator[[]string]) Combinator[string]
Flatten the output from a Combinator by joining all extracted values into a string.
chomp.Flatten(
chomp.Many(chomp.Parentheses()),
)("(H)(el)(lo), World!")
// (", World!", "Hello", nil)
func HexDigit ΒΆ added in v0.5.0
func HexDigit() Combinator[string]
HexDigit matches one or more hexadecimal digits (0-9, a-f, A-F). Equivalent to While(IsHexDigit).
chomp.HexDigit()("1a2B3c rest")
// (" rest", "1a2B3c", nil)
func HexDigit0 ΒΆ added in v0.5.0
func HexDigit0() Combinator[string]
HexDigit0 matches zero or more hexadecimal digits (0-9, a-f, A-F). Equivalent to WhileN(IsHexDigit, 0).
chomp.HexDigit0()("xyz")
// ("xyz", "", nil)
func I ΒΆ
func I(c Combinator[[]string], i int) Combinator[string]
I extracts and returns a single string from the result of the inner Combinator. Combinators of differing return types can be successfully chained together while using this conversion combinator.
chomp.I(chomp.SepPair(
chomp.Tag("Hello"),
chomp.Tag(", "),
chomp.Tag("World")), 1)("Hello, World!")
// ("!", "World", nil)
func Many ΒΆ added in v0.2.0
func Many[T Result](c Combinator[T]) Combinator[[]string]
Many will scan the input text, and it must match the Combinator at least once. This Combinator is greedy and will continuously execute until the first failed match. It is the equivalent of calling ManyN with an argument of 1.
chomp.Many(one.Of("Ho"))("Hello, World!")
// ("ello, World!", []string{"H"}, nil)
func ManyN ΒΆ added in v0.2.0
func ManyN[T Result](c Combinator[T], n uint) Combinator[[]string]
ManyN will scan the input text and match the Combinator a minimum number of times. This Combinator is greedy and will continuously execute until the first failed match.
chomp.ManyN(chomp.OneOf("W"), 0)("Hello, World!")
// ("Hello, World!", nil, nil)
func Multispace ΒΆ added in v0.5.0
func Multispace() Combinator[string]
Multispace matches one or more whitespace characters (space, tab, newline, carriage return). Equivalent to While(IsMultispace).
chomp.Multispace()(" \n\tHello")
// ("Hello", " \n\t", nil)
func Multispace0 ΒΆ added in v0.5.0
func Multispace0() Combinator[string]
Multispace0 matches zero or more whitespace characters (space, tab, newline, carriage return). Equivalent to WhileN(IsMultispace, 0).
chomp.Multispace0()("Hello")
// ("Hello", "", nil)
func Newline ΒΆ added in v0.5.0
func Newline() Combinator[string]
Newline matches a single newline character '\n'.
chomp.Newline()("\nHello")
// ("Hello", "\n", nil)
func NoneOf ΒΆ
func NoneOf(str string) Combinator[string]
NoneOf must not match a single character at the beginning of the text from the provided sequence.
chomp.NoneOf("loWrd!e")("Hello, World!")
// ("ello, World!", "H", nil)
func Not ΒΆ
func Not(str string) Combinator[string]
Not must not match at least one character at the beginning of the input text from the provided sequence. Parsing stops upon the first matched character.
chomp.Not("ol")("Hello, World!")
// ("llo, World!", "He", nil)
func NotLineEnding ΒΆ added in v0.5.0
func NotLineEnding() Combinator[string]
NotLineEnding matches any characters until a line ending ('\n' or '\r'). Requires at least one character to be matched.
chomp.NotLineEnding()("Hello, World!\nNext line")
// ("\nNext line", "Hello, World!", nil)
func OctalDigit ΒΆ added in v0.5.0
func OctalDigit() Combinator[string]
OctalDigit matches one or more octal digits (0-7). Equivalent to While(IsOctalDigit).
chomp.OctalDigit()("0127 rest")
// (" rest", "0127", nil)
func OctalDigit0 ΒΆ added in v0.5.0
func OctalDigit0() Combinator[string]
OctalDigit0 matches zero or more octal digits (0-7). Equivalent to WhileN(IsOctalDigit, 0).
chomp.OctalDigit0()("89")
// ("89", "", nil)
func OneOf ΒΆ
func OneOf(str string) Combinator[string]
OneOf must match a single character at the beginning of the text from the provided sequence.
chomp.OneOf("!,eH")("Hello, World!")
// ("ello, World!", "H", nil)
func Opt ΒΆ
func Opt[T Result](c Combinator[T]) Combinator[T]
Opt allows a Combinator to be optional by discarding its returned error and not modifying the input text upon failure.
chomp.Opt(chomp.Tag("Hey"))("Hello, World!")
// ("Hello, World!", "", nil)
func Pair ΒΆ
func Pair[T, U Result](c1 Combinator[T], c2 Combinator[U]) Combinator[[]string]
Pair will scan the input text and match each Combinator in turn. Both combinators must match.
chomp.Pair(chomp.Tag("Hello,"), chomp.Tag(" World"))("Hello, World!")
// ("!", []string{"Hello,", " World"}, nil)
func Parentheses ΒΆ
func Parentheses() Combinator[string]
Parentheses will match any text delimited (or surrounded) by a pair of (parentheses).
chomp.Parentheses()("(Hello, World!)")
// ("", "Hello, World!", nil)
func Peek ΒΆ added in v0.3.0
func Peek[T Result](c Combinator[T]) Combinator[T]
Peek will scan the text and apply the Combinator without consuming any input. Useful if you need to look ahead.
chomp.Peek(chomp.Tag("Hello"))("Hello, World!")
// ("Hello, World!", "Hello", nil)
chomp.Peek(
chomp.Many(chomp.Suffixed(chomp.Tag(" "), chomp.Until(" "))),
)("Hello and Good Morning!")
// ("Hello and Good Morning!", []string{"Hello", "and", "Good"}, nil)
func Prefixed ΒΆ added in v0.2.0
func Prefixed(c, pre Combinator[string]) Combinator[string]
Prefixed will scan the input text for a defined prefix and discard it before matching the remaining text against the Combinator. Both combinators must match.
chomp.Prefixed(
chomp.Tag("Hello"),
chomp.Tag(`"`))(`"Hello, World!"`)
// (`, World!"`, "Hello", nil)
func QuoteDouble ΒΆ
func QuoteDouble() Combinator[string]
QuoteDouble will match any text delimited (or surrounded) by a pair of "double quotes".
chomp.DoubleQuote()(`"Hello, World!"`)
// ("", "Hello, World!", nil)
func QuoteSingle ΒΆ
func QuoteSingle() Combinator[string]
QuoteSingle will match any text delimited (or surrounded) by a pair of 'single quotes'.
chomp.QuoteSingle()("'Hello, World!'")
// ("", "Hello, World!", nil)
func Repeat ΒΆ
func Repeat[T Result](c Combinator[T], n uint) Combinator[[]string]
Repeat will scan the input text and match the combinator the defined number of times. Every execution must match.
chomp.Repeat(chomp.Parentheses(), 2)("(Hello)(World)(!)")
// ("(!)", []string{"(Hello)", "(World)"}, nil)
func RepeatRange ΒΆ added in v0.2.0
func RepeatRange[T Result](c Combinator[T], n, m uint) Combinator[[]string]
RepeatRange will scan the input text and match the Combinator between a minimum and maximum number of times. It must match the expected minimum number of times.
chomp.RepeatRange(chomp.OneOf("Hleo"), 1, 8)("Hello, World!")
// (", World!", []string{"H", "e", "l", "l", "o"}, nil)
func S ΒΆ
func S(c Combinator[string]) Combinator[[]string]
S wraps the result of the inner Combinator within a string slice. Combinators of differing return types can be successfully chained together while using this conversion combinator.
chomp.S(chomp.Until(","))("Hello, World!")
// (", World!", []string{"Hello"}, nil)
func Satisfy ΒΆ added in v0.5.0
func Satisfy(pred func(rune) bool) Combinator[string]
Satisfy matches a single character at the beginning of the input text that satisfies the given predicate function.
chomp.Satisfy(func(r rune) bool { return r >= 'A' && r <= 'Z' })("Hello")
// ("ello", "H", nil)
func SepPair ΒΆ
func SepPair[T, U, V Result](c1 Combinator[T], sep Combinator[U], c2 Combinator[V]) Combinator[[]string]
SepPair will scan the input text and match each Combinator, discarding the separator's output. All combinators must match.
chomp.SepPair(
chomp.Tag("Hello"),
chomp.Tag(", "),
chomp.Tag("World"))("Hello, World!")
// ("!", []string{"Hello", "World"}, nil)
func Space ΒΆ added in v0.5.0
func Space() Combinator[string]
Space matches one or more space or tab characters. Equivalent to While(IsSpace).
chomp.Space()(" Hello")
// ("Hello", " ", nil)
func Space0 ΒΆ added in v0.5.0
func Space0() Combinator[string]
Space0 matches zero or more space or tab characters. Equivalent to WhileN(IsSpace, 0).
chomp.Space0()("Hello")
// ("Hello", "", nil)
func Suffixed ΒΆ added in v0.2.0
func Suffixed(c, suf Combinator[string]) Combinator[string]
Suffixed will scan the input text against the Combinator before matching a suffix and discarding it. Both combinators must match.
chomp.Suffixed(
chomp.Tag("Hello"),
chomp.Tag(", "))("Hello, World!")
// ("World!", "Hello", nil)
func Tab ΒΆ added in v0.5.0
func Tab() Combinator[string]
Tab matches a single tab character '\t'.
chomp.Tab()("\tHello")
// ("Hello", "\t", nil)
func Tag ΒΆ
func Tag(str string) Combinator[string]
Tag must match a series of characters at the beginning of the input text in the exact order and case provided.
chomp.Tag("Hello")("Hello, World!")
// (", World!", "Hello", nil)
func TagNoCase ΒΆ added in v0.5.0
func TagNoCase(str string) Combinator[string]
TagNoCase must match a series of characters at the beginning of the input text in the exact order provided, but ignoring case. The matched text from the input is returned (preserving the original casing).
chomp.TagNoCase("hello")("HELLO, World!")
// (", World!", "HELLO", nil)
func Take ΒΆ added in v0.5.0
func Take(n uint) Combinator[string]
Take will consume exactly n characters from the beginning of the input text. Unicode characters are handled correctly by counting runes, not bytes.
chomp.Take(5)("Hello, World!")
// (", World!", "Hello", nil)
func TakeUntil1 ΒΆ added in v0.5.0
func TakeUntil1(str string) Combinator[string]
TakeUntil1 will scan the input text for the first occurrence of the provided series of characters, requiring at least one character to be matched before the delimiter. Everything until that point in the text will be matched.
chomp.TakeUntil1(",")("Hello, World!")
// (", World!", "Hello", nil)
chomp.TakeUntil1(",")(",World!")
// Error: must match at least one character
func Until ΒΆ
func Until(str string) Combinator[string]
Until will scan the input text for the first occurrence of the provided series of characters. Everything until that point in the text will be matched.
chomp.Until("World")("Hello, World!")
// ("World!", "Hello, ", nil)
func While ΒΆ
func While(p Predicate) Combinator[string]
While will scan the input text, testing each character against the provided Predicate. The Predicate must match at least one character.
chomp.While(chomp.IsLetter)("Hello, World!")
// (", World!", "Hello", nil)
func WhileN ΒΆ added in v0.3.0
func WhileN(p Predicate, n uint) Combinator[string]
WhileN will scan the input text, testing each character against the provided Predicate. The Predicate must match at least n characters. If n is zero, this becomes an optional combinator.
chomp.WhileN(chomp.IsLetter, 1)("Hello, World!")
// (", World!", "Hello", nil)
chomp.WhileN(chomp.IsDigit, 0)("Hello, World!")
// ("Hello, World!", "", nil)
func WhileNM ΒΆ added in v0.3.0
func WhileNM(p Predicate, n, m uint) Combinator[string]
WhileNM will scan the input text, testing each character against the provided Predicate. The Predicate must match a minimum of n and upto a maximum of m characters. If n is zero, this becomes an optional combinator.
chomp.WhileNM(chomp.IsLetter, 1, 8)("Hello, World!")
// (", World!", "Hello", nil)
func WhileNot ΒΆ
func WhileNot(p Predicate) Combinator[string]
WhileNot will scan the input text, testing each character against the provided Predicate. The Predicate must not match at least one character. It has the inverse behavior of While.
chomp.WhileNot(chomp.IsDigit)("Hello, World!")
// ("", "Hello, World!", nil)
func WhileNotN ΒΆ added in v0.3.0
func WhileNotN(p Predicate, n uint) Combinator[string]
WhileNotN will scan the input text, testing each character against the provided Predicate. The Predicate must not match at least n characters. If n is zero, this becomes an optional combinator. It has the inverse behavior of WhileN.
chomp.WhileNotN(chomp.IsDigit, 1)("Hello, World!")
// ("", "Hello, World!", nil)
chomp.WhileNotN(chomp.IsLetter, 0)("Hello, World!")
// ("Hello, World!", "", nil)
func WhileNotNM ΒΆ added in v0.3.0
func WhileNotNM(p Predicate, n, m uint) Combinator[string]
WhileNotNM will scan the input text, testing each character against the provided Predicate. The Predicate must not match a minimum of n and upto a maximum of m characters. If n is zero, this becomes an optional combinator. It has the inverse behavior of WhileNM.
chomp.WhileNotNM(chomp.IsLetter, 1, 8)("20240709 was a great day")
// (" was a great day", "20240709", nil)
type CombinatorParseError ΒΆ
type CombinatorParseError struct {
// Input to the [Combinator]. This can be empty, as a combinator may
// not require any input to parse the text.
Input string
// Text that was being parsed by the [Combinator]. This will be truncated
// in the error message.
Text string
// Type of [Combinator] that failed.
Type string
}
CombinatorParseError defines an error that is raised when a combinator fails to parse the input text under its expected condition.
func (CombinatorParseError) Error ΒΆ
func (e CombinatorParseError) Error() string
Error returns a friendly string representation of the current error.
type MappedCombinator ΒΆ added in v0.3.0
MappedCombinator is a function capable of converting the output from a Combinator into any given type. Upon success, it will return the unparsed text, along with the mapped value. All combinators are strict and must parse its input. Any failure to do so should raise a CombinatorParseError. It is designed for exclusive use by the Map function
func Map ΒΆ added in v0.3.0
func Map[S any, T Result](c Combinator[T], mapper func(in T) S) MappedCombinator[S, T]
Map the result of a Combinator to any other type
chomp.Map(
chomp.While(chomp.IsDigit),
func (in string) int { return len(in) })("123456")
// ("", 6, nil)
type ParserError ΒΆ
type ParserError struct {
// Err contains the [CombinatorParseError] that caused the parser to fail.
Err error
// Type of [Parser] that failed.
Type string
}
ParserError defines an error that is raised when a parser fails to parse the input text due to a failed Combinator.
func (ParserError) Error ΒΆ
func (e ParserError) Error() string
Error returns a friendly string representation of the current error.
func (ParserError) Unwrap ΒΆ
func (e ParserError) Unwrap() error
Unwrap returns the inner CombinatorParseError.
type Predicate ΒΆ
type Predicate interface {
// Match a rune against a defined expression, returning true
// if the condition is met
Match(r rune) bool
// Returns the name of the predicate for error handling
fmt.Stringer
}
Predicate defines an expression that will return either true or false
type RangedParserError ΒΆ added in v0.2.0
type RangedParserError struct {
// Err contains the [CombinatorParseError] that caused the parser to fail.
Err error
// Range contains the execution details of the ranged parser.
Exec RangedParserExec
// Type of [Parser] that failed.
Type string
}
RangedParserError defines an error that is raised when a ranged parser fails to parse the input text due to a failed Combinator within the expected execution range.
func (RangedParserError) Error ΒΆ added in v0.2.0
func (e RangedParserError) Error() string
Error returns a friendly string representation of the current error.
func (RangedParserError) Unwrap ΒΆ added in v0.2.0
func (e RangedParserError) Unwrap() error
Unwrap returns the inner CombinatorParseError.
type RangedParserExec ΒΆ added in v0.2.0
type RangedParserExec struct {
// Min is the minimum number of expected executions.
Min uint
// Max is the maximum number of possible executions.
Max uint
// Count contains the number of executions.
Count uint
}
RangedParserExec details how a ranged Combinator was executed.
func RangeExecution ΒΆ added in v0.2.0
func RangeExecution(i ...uint) RangedParserExec
RangeExecution is a utility method for setting a RangedParserExec.
- With one argument, the [RangeParserExec.Count] is set.
- With two arguments, the [RangeParserExec.Count] and [RangeParserExec.Min] are set.
- With three arguments, the [RangeParserExec.Count]], [RangeParserExec.Min] and [RangeParserExec.Max] are set.
- If four or more arguments are provided, a default RangedParserExec will be returned.
func (RangedParserExec) String ΒΆ added in v0.2.0
func (e RangedParserExec) String() string
String returns a string representation of a RangedParserExec.
type Result ΒΆ
Result is the expected output from a Combinator.