60 lines
2.0 KiB
Go
60 lines
2.0 KiB
Go
// In this example, a Paparserrser is created which can parse and normalize Dutch postcodes
|
|
// The implementation uses only TokenHandler functions and does not implement a
|
|
// full-fledged state-based Parser for it.
|
|
package parsekit_test
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.makaay.nl/mauricem/go-parsekit"
|
|
)
|
|
|
|
func createPostcodeMatcher() *parsekit.Matcher {
|
|
// Easy access to the parsekit definitions.
|
|
c, a, m := parsekit.C, parsekit.A, parsekit.M
|
|
|
|
// TokenHandler functions are created and combined to satisfy these rules:
|
|
// - A Dutch postcode consists of 4 digits and 2 letters (1234XX).
|
|
// - The first digit is never a zero.
|
|
// - A space between letters and digits is optional.
|
|
// - It is good form to write the letters in upper case.
|
|
// - It is good form to use a single space between digits and letters.
|
|
digitNotZero := c.Except(c.Rune('0'), a.Digit)
|
|
pcDigits := c.Seq(digitNotZero, c.Rep(3, a.Digit))
|
|
pcLetter := c.Any(a.ASCIILower, a.ASCIIUpper)
|
|
pcLetters := m.ToUpper(c.Seq(pcLetter, pcLetter))
|
|
space := m.Replace(c.Opt(a.Whitespace), " ")
|
|
postcode := c.Seq(pcDigits, space, pcLetters)
|
|
|
|
// Create a Matcher, which wraps the 'postcode' TokenHandler and allows
|
|
// us to match some input against that handler.
|
|
return parsekit.NewMatcher(postcode, "a Dutch postcode")
|
|
}
|
|
|
|
func Example_dutchPostcodeUsingMatcher() {
|
|
pcParser := createPostcodeMatcher()
|
|
|
|
for i, input := range []string{
|
|
"1234 AB",
|
|
"2233Ab",
|
|
"1001\t\tab",
|
|
"1818ab",
|
|
"1234",
|
|
"huh",
|
|
} {
|
|
output, err, ok := pcParser.Parse(input)
|
|
if !ok {
|
|
fmt.Printf("[%d] Input: %q Error: %s\n", i, input, err)
|
|
} else {
|
|
fmt.Printf("[%d] Input: %q Output: %s\n", i, input, output)
|
|
}
|
|
}
|
|
// Output:
|
|
// [0] Input: "1234 AB" Output: 1234 AB
|
|
// [1] Input: "2233Ab" Output: 2233 AB
|
|
// [2] Input: "1001\t\tab" Output: 1001 AB
|
|
// [3] Input: "1818ab" Output: 1818 AB
|
|
// [4] Input: "1234" Error: unexpected character '1' (expected a Dutch postcode)
|
|
// [5] Input: "huh" Error: unexpected character 'h' (expected a Dutch postcode)
|
|
}
|