bytes

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 7, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package bytes provides parsers for recognizing bytes

Package bytes provides parsers for recognizing bytes

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Byte

func Byte(match byte) parser.Parser[parser.Reader, 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

func One() parser.Parser[parser.Reader, byte]

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

func OneOf(bytes ...byte) parser.Parser[parser.Reader, byte]

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

func OneOf0(bytes ...byte) parser.Parser[parser.Reader, []byte]

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

func OneOf1(bytes ...byte) parser.Parser[parser.Reader, []byte]

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

func Tag(b []byte) parser.Parser[parser.Reader, []byte]

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

func Take(n uint) parser.Parser[parser.Reader, []byte]

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

func TakeWhile(predicate parser.Predicate[byte]) parser.Parser[parser.Reader, []byte]

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

func TakeWhile1(predicate parser.Predicate[byte]) parser.Parser[parser.Reader, []byte]

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL