Documentation ¶
Overview ¶
Package bytes provides parsers for recognizing bytes
Package bytes provides parsers for recognizing bytes
Index ¶
- func Byte(match byte) parser.Parser[parser.Reader, byte]
- func One() parser.Parser[parser.Reader, byte]
- func OneOf(bytes ...byte) parser.Parser[parser.Reader, byte]
- func OneOf0(bytes ...byte) parser.Parser[parser.Reader, []byte]
- func OneOf1(bytes ...byte) parser.Parser[parser.Reader, []byte]
- func Skip(p parser.Predicate[byte]) parser.Parser[parser.Reader, parser.Empty]
- func Skip0(p parser.Predicate[byte]) parser.Parser[parser.Reader, parser.Empty]
- func Skip1(p parser.Predicate[byte]) parser.Parser[parser.Reader, parser.Empty]
- func Tag(b []byte) parser.Parser[parser.Reader, []byte]
- func Take(n uint) parser.Parser[parser.Reader, []byte]
- func TakeWhile(predicate parser.Predicate[byte]) parser.Parser[parser.Reader, []byte]
- func TakeWhile1(predicate parser.Predicate[byte]) parser.Parser[parser.Reader, []byte]
- func TakeWhileMinMax(min, max int, predicate parser.Predicate[byte]) parser.Parser[parser.Reader, []byte]
Examples ¶
- Byte (EndOfFile)
- Byte (Match)
- Byte (NoMatch)
- One (EndOfFile)
- One (Match)
- OneOf (EndOfFile)
- OneOf (Match)
- OneOf (NoMatch)
- OneOf0 (EndOfFile)
- OneOf0 (Match)
- OneOf0 (NoMatch)
- OneOf1 (EndOfFile)
- OneOf1 (Match)
- OneOf1 (NoMatch)
- Tag (EndOfFile)
- Tag (Match)
- Tag (NoMatch)
- Take (EndOfFile)
- Take (Match)
- Take (UnexpectedEndOfFile)
- TakeWhile (EndOfFile)
- TakeWhile (Match)
- TakeWhile (NoMatch)
- TakeWhile1 (EndOfFile)
- TakeWhile1 (Match)
- TakeWhile1 (NoMatch)
- TakeWhileMinMax (EndOfFile)
- TakeWhileMinMax (Match)
- TakeWhileMinMax (NoMatch)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Byte ¶
Byte matches a single byte
The input data will be compared to the match argument.
- If the input matches the argument, it will return the match.
- If the input is empty, it will return io.EOF
- If the input doesn't match the argument, it will return parser.ErrNotMatched
Example (EndOfFile) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("") byteParser := bytes.Byte('a') match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: %d, Error: '%v', Remainder: '%s'", match, err, string(remainder)) }
Output: Match: 0, Error: 'EOF', Remainder: ''
Example (Match) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("abc") byteParser := bytes.Byte('a') match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: '%s', Error: %v, Remainder: '%s'", string(match), err, string(remainder)) }
Output: Match: 'a', Error: <nil>, Remainder: 'bc'
Example (NoMatch) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("abc") byteParser := bytes.Byte('b') match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: %d, Error: '%v', Remainder: '%s'", match, err, string(remainder)) }
Output: Match: 0, Error: 'not matched', Remainder: 'abc'
func One ¶
One reads a single byte
- If the input isn't empty, it will return a single byte.
- If the input is empty, it will return io.EOF
Example (EndOfFile) ¶
package main import ( "bytes" "fmt" gobble "github.com/roblovelock/gobble/pkg/parser/bytes" "io" ) func main() { input := bytes.NewReader([]byte{}) numericParser := gobble.One() match, err := numericParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: %d, Error: '%v', Remainder: %v", match, err, remainder) }
Output: Match: 0, Error: 'EOF', Remainder: []
Example (Match) ¶
package main import ( "bytes" "fmt" gobble "github.com/roblovelock/gobble/pkg/parser/bytes" "io" ) func main() { input := bytes.NewReader([]byte{1, 2, 3}) numericParser := gobble.One() match, err := numericParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: %d, Error: %v, Remainder: %v", match, err, remainder) }
Output: Match: 1, Error: <nil>, Remainder: [2 3]
func OneOf ¶
OneOf matches one of the argument bytes
- If the input matches the argument, it will return a single matched byte.
- If the input is empty, it will return io.EOF
- If the input doesn't match the argument, it will return parser.ErrNotMatched
Example (EndOfFile) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("") byteParser := bytes.OneOf('a', 'b', 'c') match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: %d, Error: '%v', Remainder: '%s'", match, err, string(remainder)) }
Output: Match: 0, Error: 'EOF', Remainder: ''
Example (Match) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("abc123") byteParser := bytes.OneOf('a', 'b', 'c') match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: '%s', Error: %v, Remainder: '%s'", string(match), err, string(remainder)) }
Output: Match: 'a', Error: <nil>, Remainder: 'bc123'
Example (NoMatch) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("123") byteParser := bytes.OneOf('a', 'b', 'c') match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: %d, Error: '%v', Remainder: '%s'", match, err, string(remainder)) }
Output: Match: 0, Error: 'not matched', Remainder: '123'
func OneOf0 ¶
OneOf0 matches zero or more bytes matching one of the argument bytes
- If the input matches the argument, it will return a slice of all matched bytes.
- If the input is empty, it will return an empty slice.
- If the input doesn't match, it will return an empty slice.
Example (EndOfFile) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("") byteParser := bytes.OneOf0('a', 'b', 'c') match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: '%s', Error: %v, Remainder: '%s'", string(match), err, string(remainder)) }
Output: Match: '', Error: <nil>, Remainder: ''
Example (Match) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("abc123") byteParser := bytes.OneOf0('a', 'b', 'c') match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: '%s', Error: %v, Remainder: '%s'", string(match), err, string(remainder)) }
Output: Match: 'abc', Error: <nil>, Remainder: '123'
Example (NoMatch) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("123") byteParser := bytes.OneOf0('a', 'b', 'c') match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: '%s', Error: %v, Remainder: '%s'", string(match), err, string(remainder)) }
Output: Match: '', Error: <nil>, Remainder: '123'
func OneOf1 ¶
OneOf1 matches one or more bytes matching one of the argument bytes
- If the input matches the argument, it will return a slice of all matched bytes.
- If the input is empty, it will return io.EOF
- If the input doesn't match the argument, it will return parser.ErrNotMatched
Example (EndOfFile) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("") byteParser := bytes.OneOf1('a', 'b', 'c') match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: '%s', Error: '%v', Remainder: '%s'", string(match), err, string(remainder)) }
Output: Match: '', Error: 'EOF', Remainder: ''
Example (Match) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("abc123") byteParser := bytes.OneOf1('a', 'b', 'c') match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: '%s', Error: %v, Remainder: '%s'", string(match), err, string(remainder)) }
Output: Match: 'abc', Error: <nil>, Remainder: '123'
Example (NoMatch) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("123") byteParser := bytes.OneOf1('a', 'b', 'c') match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: '%s', Error: '%v', Remainder: '%s'", string(match), err, string(remainder)) }
Output: Match: '', Error: 'not matched', Remainder: '123'
func Tag ¶
Tag matches the argument
- If the input matches the argument, it will return the tag.
- If the input is empty, it will return io.EOF
- If the input doesn't match the argument, it will return parser.ErrNotMatched
Example (EndOfFile) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("") byteParser := bytes.Tag([]byte("ab")) match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: '%s', Error: '%v', Remainder: '%s'", string(match), err, string(remainder)) }
Output: Match: '', Error: 'EOF', Remainder: ''
Example (Match) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("abc") byteParser := bytes.Tag([]byte("ab")) match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: '%s', Error: %v, Remainder: '%s'", string(match), err, string(remainder)) }
Output: Match: 'ab', Error: <nil>, Remainder: 'c'
Example (NoMatch) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("abc") byteParser := bytes.Tag([]byte("bc")) match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: '%s', Error: '%v', Remainder: '%s'", string(match), err, string(remainder)) }
Output: Match: '', Error: 'not matched', Remainder: 'abc'
func Take ¶
Take returns a slice of n bytes from the input
- If the input contains n bytes, it will return a slice of n bytes.
- If the input doesn't contain n bytes, it will return io.EOF
Example (EndOfFile) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("") byteParser := bytes.Take(4) match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: '%s', Error: '%v', Remainder: '%s'", string(match), err, string(remainder)) }
Output: Match: '', Error: 'EOF', Remainder: ''
Example (Match) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("abc") byteParser := bytes.Take(2) match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: '%s', Error: %v, Remainder: '%s'", string(match), err, string(remainder)) }
Output: Match: 'ab', Error: <nil>, Remainder: 'c'
Example (UnexpectedEndOfFile) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("abc") byteParser := bytes.Take(4) match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: '%s', Error: '%v', Remainder: '%s'", string(match), err, string(remainder)) }
Output: Match: '', Error: 'EOF', Remainder: 'abc'
func TakeWhile ¶
TakeWhile Returns zero or more bytes that match the predicate.
- If the input matches the predicate, it will return the matched bytes.
- If the input is empty, it will return an empty slice
- If the input doesn't match the predicate, it will return an empty slice
Example (EndOfFile) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/ascii" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("") byteParser := bytes.TakeWhile(ascii.IsDigit) match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: '%s', Error: %v, Remainder: '%s'", string(match), err, string(remainder)) }
Output: Match: '', Error: <nil>, Remainder: ''
Example (Match) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/ascii" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("abc123") byteParser := bytes.TakeWhile(ascii.IsLetter) match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: '%s', Error: %v, Remainder: '%s'", string(match), err, string(remainder)) }
Output: Match: 'abc', Error: <nil>, Remainder: '123'
Example (NoMatch) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/ascii" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("abc") byteParser := bytes.TakeWhile(ascii.IsDigit) match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: '%s', Error: %v, Remainder: '%s'", string(match), err, string(remainder)) }
Output: Match: '', Error: <nil>, Remainder: 'abc'
func TakeWhile1 ¶
TakeWhile1 Returns one or more bytes that match the predicate.
- If the input matches the predicate, it will return the matched bytes.
- If the input is empty, it will return io.EOF
- If the input doesn't match the predicate, it will return parser.ErrNotMatched
Example (EndOfFile) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/ascii" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("") byteParser := bytes.TakeWhile1(ascii.IsDigit) match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: '%s', Error: '%v', Remainder: '%s'", string(match), err, string(remainder)) }
Output: Match: '', Error: 'EOF', Remainder: ''
Example (Match) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/ascii" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("abc123") byteParser := bytes.TakeWhile1(ascii.IsLetter) match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: '%s', Error: %v, Remainder: '%s'", string(match), err, string(remainder)) }
Output: Match: 'abc', Error: <nil>, Remainder: '123'
Example (NoMatch) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/ascii" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("abc") byteParser := bytes.TakeWhile1(ascii.IsDigit) match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: '%s', Error: '%v', Remainder: '%s'", string(match), err, string(remainder)) }
Output: Match: '', Error: 'not matched', Remainder: 'abc'
func TakeWhileMinMax ¶
func TakeWhileMinMax(min, max int, predicate parser.Predicate[byte]) parser.Parser[parser.Reader, []byte]
TakeWhileMinMax Returns the longest (m <= len <= n) input slice that matches the predicate.
- If the input matches the predicate and (m <= len <= n), it will return the matched bytes.
- If the input is empty and m > 0, it will return io.EOF
- If the number of matched bytes < m, it will return parser.ErrNotMatched
Example (EndOfFile) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/ascii" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("") byteParser := bytes.TakeWhileMinMax(1, 2, ascii.IsDigit) match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: '%s', Error: '%v', Remainder: '%s'", string(match), err, string(remainder)) }
Output: Match: '', Error: 'EOF', Remainder: ''
Example (Match) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/ascii" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("abc") byteParser := bytes.TakeWhileMinMax(1, 2, ascii.IsLetter) match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: '%s', Error: %v, Remainder: '%s'", string(match), err, string(remainder)) }
Output: Match: 'ab', Error: <nil>, Remainder: 'c'
Example (NoMatch) ¶
package main import ( "fmt" "github.com/roblovelock/gobble/pkg/parser/ascii" "github.com/roblovelock/gobble/pkg/parser/bytes" "io" "strings" ) func main() { input := strings.NewReader("abc") byteParser := bytes.TakeWhileMinMax(1, 2, ascii.IsDigit) match, err := byteParser(input) remainder, _ := io.ReadAll(input) fmt.Printf("Match: '%s', Error: '%v', Remainder: '%s'", string(match), err, string(remainder)) }
Output: Match: '', Error: 'not matched', Remainder: 'abc'
Types ¶
This section is empty.