stream

package
v0.0.0-...-1dacd80 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2022 License: Apache-2.0, MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Checkpoint

func Checkpoint[T Token](s []T) []T

func Distance

func Distance[T Token](cur, end []T) int
Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/stream"
)

func main() {
	input := []rune{'a', 'b'}

	before := stream.Checkpoint(input)
	fmt.Println(stream.Distance(input, before))

	b, input, err := stream.Uncons(input)
	if err != nil {
		panic(err)
	}

	fmt.Println(stream.Distance(input, before), b)

	b, input, err = stream.Uncons(input)
	if err != nil {
		panic(err)
	}

	fmt.Println(stream.Distance(input, before), b)

	_, _, err = stream.Uncons(input)
	fmt.Println(err)

}
Output:

0
1 97
2 98
unexpected EOF

func Empty

func Empty[T Token](s []T) bool

Empty returns true if the stream is empty.

func Len

func Len[T Token](s []T) int

Len returns the number of tokens in the stream.

func Uncons

func Uncons[T Token](input []T) (tok T, remaining []T, err error)

Uncons takes a stream `input` and removes its first token, yielding the `tok` and the `remaining` of the elements. Returns `err` if no element could be retrieved.

func UnconsRange

func UnconsRange[T Token](input []T, size int) (tokens []T, remaining []T, err error)

UnconsRange takes `size` elements from the stream. Fails if the length of the stream is less than `size`.

func UnconsUntil

func UnconsUntil[T Token](input []T, predicate func(T) bool) (tokens []T, remaining []T, err error)

UnconsUntil takes items from stream, testing each one with `predicate`. returns the range of items which not passed `predicate`.

func UnconsWhile

func UnconsWhile[T Token](input []T, predicate func(T) bool) (tokens []T, remaining []T, err error)

UnconsWhile takes items from stream, testing each one with `predicate`. returns the range of items which passed `predicate`.

Types

type LineNumber

type LineNumber struct {
	Line   int
	Column int
}

func NewLineNumber

func NewLineNumber[T Token](input []T) *LineNumber

type Offset

type Offset uintptr
const EOI Offset = Offset(^uintptr(0))

func Pos

func Pos[T Token](s []T) Offset

type Position

type Position uint

func Translate

func Translate[T Token](s []T, off Offset) Position

type Span

type Span struct {
	Start, End Offset
}

type Token

type Token interface {
	~byte | ~uint16 | ~rune
}

Token represents a token.

Jump to

Keyboard shortcuts

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