runes

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: 5 Imported by: 0

Documentation

Overview

Package runes provides parsers for recognizing runes

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func EscapedString

func EscapedString() parser.Parser[parser.Reader, string]

func One

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

One reads a single rune

  • If the input isn't empty, it will return a single rune.
  • If the input is empty, it will return io.EOF
Example (EndOfFile)
package main

import (
	"bytes"
	"fmt"
	"github.com/roblovelock/gobble/pkg/parser/runes"
	"io"
)

func main() {
	input := bytes.NewReader([]byte{})
	numericParser := runes.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 (
	"fmt"
	"github.com/roblovelock/gobble/pkg/parser/runes"
	"io"
	"strings"
)

func main() {
	input := strings.NewReader("𒀀𒀀")
	numericParser := runes.One()

	match, err := numericParser(input)
	remainder, _ := io.ReadAll(input)
	fmt.Printf("Match: '%s', Error: %v, Remainder: '%s'", string(match), err, remainder)

}
Output:

Match: '𒀀', Error: <nil>, Remainder: '𒀀'

func OneOf

func OneOf(runes ...rune) parser.Parser[parser.Reader, rune]

OneOf matches one of the argument runes

  • If the input matches the argument, it will return a single matched rune.
  • 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/runes"
	"io"
	"strings"
)

func main() {
	input := strings.NewReader("")
	runeParser := runes.OneOf('𒀀', 'a')

	match, err := runeParser(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/runes"
	"io"
	"strings"
)

func main() {
	input := strings.NewReader("𒀀a𒀀")
	runeParser := runes.OneOf('𒀀', 'a')

	match, err := runeParser(input)
	remainder, _ := io.ReadAll(input)
	fmt.Printf("Match: '%s', Error: %v, Remainder: '%s'", string(match), err, string(remainder))

}
Output:

Match: '𒀀', Error: <nil>, Remainder: 'a𒀀'
Example (NoMatch)
package main

import (
	"fmt"
	"github.com/roblovelock/gobble/pkg/parser/runes"
	"io"
	"strings"
)

func main() {
	input := strings.NewReader("123")
	runeParser := runes.OneOf('𒀀', 'a')

	match, err := runeParser(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(runes ...rune) parser.Parser[parser.Reader, string]

OneOf0 matches zero or more runes matching one of the argument runes

  • If the input matches the argument, it will return a string of all matched runes.
  • If the input is empty, it will return an empty string.
  • If the input doesn't match, it will return an empty string.
Example (EndOfFile)
package main

import (
	"fmt"
	"github.com/roblovelock/gobble/pkg/parser/runes"
	"io"
	"strings"
)

func main() {
	input := strings.NewReader("")
	runeParser := runes.OneOf0('𒀀', 'a')

	match, err := runeParser(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/runes"
	"io"
	"strings"
)

func main() {
	input := strings.NewReader("𒀀a𒀀123")
	runeParser := runes.OneOf0('𒀀', 'a')

	match, err := runeParser(input)
	remainder, _ := io.ReadAll(input)
	fmt.Printf("Match: '%s', Error: %v, Remainder: '%s'", string(match), err, string(remainder))

}
Output:

Match: '𒀀a𒀀', Error: <nil>, Remainder: '123'
Example (NoMatch)
package main

import (
	"fmt"
	"github.com/roblovelock/gobble/pkg/parser/runes"
	"io"
	"strings"
)

func main() {
	input := strings.NewReader("123")
	runeParser := runes.OneOf0('𒀀', 'a')

	match, err := runeParser(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(runes ...rune) parser.Parser[parser.Reader, string]

OneOf1 matches one or more runes matching one of the argument runes

  • If the input matches the argument, it will return a string of all matched runes.
  • 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/runes"
	"io"
	"strings"
)

func main() {
	input := strings.NewReader("")
	runeParser := runes.OneOf1('𒀀', 'a')

	match, err := runeParser(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/runes"
	"io"
	"strings"
)

func main() {
	input := strings.NewReader("𒀀a𒀀123")
	runeParser := runes.OneOf1('𒀀', 'a')

	match, err := runeParser(input)
	remainder, _ := io.ReadAll(input)
	fmt.Printf("Match: '%s', Error: %v, Remainder: '%s'", string(match), err, string(remainder))

}
Output:

Match: '𒀀a𒀀', Error: <nil>, Remainder: '123'
Example (NoMatch)
package main

import (
	"fmt"
	"github.com/roblovelock/gobble/pkg/parser/runes"
	"io"
	"strings"
)

func main() {
	input := strings.NewReader("123")
	runeParser := runes.OneOf1('𒀀', 'a')

	match, err := runeParser(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 Rune

func Rune(r rune) parser.Parser[parser.Reader, rune]

Rune matches a single rune

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/runes"
	"io"
	"strings"
)

func main() {
	input := strings.NewReader("")
	byteParser := runes.Rune('𒀁')

	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/runes"
	"io"
	"strings"
)

func main() {
	input := strings.NewReader("𒀀a𒀀")
	byteParser := runes.Rune('𒀀')

	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: 'a𒀀'
Example (NoMatch)
package main

import (
	"fmt"
	"github.com/roblovelock/gobble/pkg/parser/runes"
	"io"
	"strings"
)

func main() {
	input := strings.NewReader("𒀀a𒀀")
	byteParser := runes.Rune('𒀁')

	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: '𒀀a𒀀'

func Take

func Take(n int) parser.Parser[parser.Reader, string]

Take returns a string containing n runes from the input

  • If the input contains n runes, it will return a string.
  • If the input doesn't contain n runes, it will return io.EOF.
Example (EndOfFile)
package main

import (
	"fmt"
	"github.com/roblovelock/gobble/pkg/parser/runes"
	"io"
	"strings"
)

func main() {
	input := strings.NewReader("")
	byteParser := runes.Take(4)

	match, err := byteParser(input)
	remainder, _ := io.ReadAll(input)
	fmt.Printf("Match: '%s', Error: '%v', Remainder: '%s'", match, err, string(remainder))

}
Output:

Match: '', Error: 'EOF', Remainder: ''
Example (Match)
package main

import (
	"fmt"
	"github.com/roblovelock/gobble/pkg/parser/runes"
	"io"
	"strings"
)

func main() {
	input := strings.NewReader("𒀀a𒀀")
	byteParser := runes.Take(2)

	match, err := byteParser(input)
	remainder, _ := io.ReadAll(input)
	fmt.Printf("Match: '%s', Error: %v, Remainder: '%s'", match, err, string(remainder))

}
Output:

Match: '𒀀a', Error: <nil>, Remainder: '𒀀'
Example (UnexpectedEndOfFile)
package main

import (
	"fmt"
	"github.com/roblovelock/gobble/pkg/parser/runes"
	"io"
	"strings"
)

func main() {
	input := strings.NewReader("𒀀a𒀀")
	byteParser := runes.Take(4)

	match, err := byteParser(input)
	remainder, _ := io.ReadAll(input)
	fmt.Printf("Match: '%s', Error: '%v', Remainder: '%s'", match, err, string(remainder))

}
Output:

Match: '', Error: 'EOF', Remainder: '𒀀a𒀀'

func TakeWhile

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

TakeWhile returns a string containing zero or more Returns a string containing that match the predicate.

  • If the input matches the predicate, it will return the matched runes.
  • If the input is empty, it will return an empty string
  • If the input doesn't match the predicate, it will return an empty string
Example (EndOfFile)
package main

import (
	"fmt"
	"github.com/roblovelock/gobble/pkg/parser/runes"
	"io"
	"strings"
	"unicode"
)

func main() {
	input := strings.NewReader("")
	runeParser := runes.TakeWhile(unicode.IsDigit)

	match, err := runeParser(input)
	remainder, _ := io.ReadAll(input)
	fmt.Printf("Match: '%s', Error: %v, Remainder: '%s'", match, err, string(remainder))

}
Output:

Match: '', Error: <nil>, Remainder: ''
Example (Match)
package main

import (
	"fmt"
	"github.com/roblovelock/gobble/pkg/parser/runes"
	"io"
	"strings"
	"unicode"
)

func main() {
	input := strings.NewReader("abc123")
	runeParser := runes.TakeWhile(unicode.IsLetter)

	match, err := runeParser(input)
	remainder, _ := io.ReadAll(input)
	fmt.Printf("Match: '%s', Error: %v, Remainder: '%s'", match, err, string(remainder))

}
Output:

Match: 'abc', Error: <nil>, Remainder: '123'
Example (NoMatch)
package main

import (
	"fmt"
	"github.com/roblovelock/gobble/pkg/parser/runes"
	"io"
	"strings"
	"unicode"
)

func main() {
	input := strings.NewReader("abc")
	runeParser := runes.TakeWhile(unicode.IsDigit)

	match, err := runeParser(input)
	remainder, _ := io.ReadAll(input)
	fmt.Printf("Match: '%s', Error: %v, Remainder: '%s'", match, err, string(remainder))

}
Output:

Match: '', Error: <nil>, Remainder: 'abc'

func TakeWhile1

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

TakeWhile1 returns a string containing one or more runes that match the predicate.

  • If the input matches the predicate, it will return the matched runes.
  • 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/runes"
	"io"
	"strings"
	"unicode"
)

func main() {
	input := strings.NewReader("")
	runeParser := runes.TakeWhile1(unicode.IsDigit)

	match, err := runeParser(input)
	remainder, _ := io.ReadAll(input)
	fmt.Printf("Match: '%s', Error: '%v', Remainder: '%s'", match, err, string(remainder))

}
Output:

Match: '', Error: 'EOF', Remainder: ''
Example (Match)
package main

import (
	"fmt"
	"github.com/roblovelock/gobble/pkg/parser/runes"
	"io"
	"strings"
	"unicode"
)

func main() {
	input := strings.NewReader("abc123")
	runeParser := runes.TakeWhile1(unicode.IsLetter)

	match, err := runeParser(input)
	remainder, _ := io.ReadAll(input)
	fmt.Printf("Match: '%s', Error: %v, Remainder: '%s'", match, err, string(remainder))

}
Output:

Match: 'abc', Error: <nil>, Remainder: '123'
Example (NoMatch)
package main

import (
	"fmt"
	"github.com/roblovelock/gobble/pkg/parser/runes"
	"io"
	"strings"
	"unicode"
)

func main() {
	input := strings.NewReader("abc")
	runeParser := runes.TakeWhile1(unicode.IsDigit)

	match, err := runeParser(input)
	remainder, _ := io.ReadAll(input)
	fmt.Printf("Match: '%s', Error: '%v', Remainder: '%s'", match, err, string(remainder))

}
Output:

Match: '', Error: 'not matched', Remainder: 'abc'

func TakeWhileMinMax

func TakeWhileMinMax(min, max int, predicate parser.Predicate[rune]) parser.Parser[parser.Reader, string]

TakeWhileMinMax returns a string of length (m <= len <= n) containing runes that match the predicate.

  • If the input matches the predicate, it will return the matched runes.
  • 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/runes"
	"io"
	"strings"
	"unicode"
)

func main() {
	input := strings.NewReader("")
	runeParser := runes.TakeWhileMinMax(1, 2, unicode.IsDigit)

	match, err := runeParser(input)
	remainder, _ := io.ReadAll(input)
	fmt.Printf("Match: '%s', Error: '%v', Remainder: '%s'", match, err, string(remainder))

}
Output:

Match: '', Error: 'EOF', Remainder: ''
Example (Match)
package main

import (
	"fmt"
	"github.com/roblovelock/gobble/pkg/parser/runes"
	"io"
	"strings"
	"unicode"
)

func main() {
	input := strings.NewReader("abc")
	runeParser := runes.TakeWhileMinMax(1, 2, unicode.IsLetter)

	match, err := runeParser(input)
	remainder, _ := io.ReadAll(input)
	fmt.Printf("Match: '%s', Error: %v, Remainder: '%s'", match, err, string(remainder))

}
Output:

Match: 'ab', Error: <nil>, Remainder: 'c'
Example (NoMatch)
package main

import (
	"fmt"
	"github.com/roblovelock/gobble/pkg/parser/runes"
	"io"
	"strings"
	"unicode"
)

func main() {
	input := strings.NewReader("abc")
	runeParser := runes.TakeWhileMinMax(1, 2, unicode.IsDigit)

	match, err := runeParser(input)
	remainder, _ := io.ReadAll(input)
	fmt.Printf("Match: '%s', Error: '%v', Remainder: '%s'", 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