op

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2021 License: Apache-2.0 Imports: 0 Imported by: 2

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type And

type And []interface{}

And (&&) represents a sequence of values.

Example
package main

import (
	"fmt"
	"github.com/di-wu/parser"
	"github.com/di-wu/parser/op"
)

func main() {
	p, _ := parser.New([]byte("foo bar baz"))

	_, err := p.Expect(op.And{"foo", ' ', "bar", '_'})
	fmt.Println(err)
	_, err = p.Expect(op.And{"foo", ' ', "bar", ' ', "baz"})
	fmt.Println(err)
}
Output:

parse conflict [00:007]: expected op.And and["foo" ' ' "bar" '_'] but got "foo bar "
<nil>

type Ensure added in v0.1.6

type Ensure struct {
	Value interface{}
}

Ensure (&) represents a positive lookup of the Value. This should not consume data. e.g. Ensure{"abc"} should check if the strings is present.

Example
package main

import (
	"fmt"
	"github.com/di-wu/parser"
	"github.com/di-wu/parser/op"
)

func main() {
	p, _ := parser.New([]byte("bar"))

	fmt.Println(p.Expect(op.Ensure{Value: "ba"}))
	fmt.Println(p.Expect(op.Ensure{Value: "baz"}))
	fmt.Println(p.Expect("bar"))
}
Output:

<nil> <nil>
<nil> parse conflict [00:002]: expected string "baz" but got "bar"
U+0072: r <nil>

type Not

type Not struct {
	Value interface{}
}

Not (!) represents a negation of the Value. This should not consume data. e.g. Not{'a'} should check if the first rune is not an 'a'.

Example
package main

import (
	"fmt"
	"github.com/di-wu/parser"
	"github.com/di-wu/parser/op"
)

func main() {
	p, _ := parser.New([]byte("bar"))

	_, err := p.Expect(op.Not{Value: "bar"})
	fmt.Println(err)
	_, err = p.Expect(op.Not{Value: "baz"})
	fmt.Println(err)
}
Output:

parse conflict [00:002]: expected op.Not !"bar" but got "bar"
<nil>

type Or

type Or []interface{}

Or (||) represents a sequence of alternative values. This is an ordered list, if a valid match is found it wil not try the remaining values.

Example
package main

import (
	"fmt"
	"github.com/di-wu/parser"
	"github.com/di-wu/parser/op"
)

func main() {
	p, _ := parser.New([]byte("data"))

	mark, _ := p.Expect(op.Or{'d', "da", "data"})
	fmt.Printf("%U: %c\n", mark.Rune, mark.Rune)
	mark, _ = p.Expect(op.Or{"at", 'a', "ata"})
	fmt.Printf("%U: %c\n", mark.Rune, mark.Rune)
	fmt.Println(p.Expect(op.Or{'d', 't', op.Not{'a'}}))
}
Output:

U+0064: d
U+0074: t
<nil> parse conflict [00:003]: expected op.Or or['d' 't' !'a'] but got 'a'

type Range

type Range struct {
	// Min indicates the lower bound of the range. Values less than 0 will get
	// interpreted as 0.
	Min int
	// Max indicates the lower bound of the range. Values less than Min will be
	// set equal to Min. On exception: -1 indicates that there is no upper bound.
	Max int
	// Value to check.
	Value interface{}
}

Range ([]) represents a range of repeated values. e.g. Range{1, -1, 'a'} expects at least one rune and consumes all following matching runes until it encounters another one.

func Min

func Min(min int, i interface{}) Range

Min returns the range '[min:['.

Example
package main

import (
	"fmt"
	"github.com/di-wu/parser"
	"github.com/di-wu/parser/op"
)

func main() {
	p, _ := parser.New([]byte("aaa"))
	start := p.Mark() // Mark to first 'a'.

	fmt.Println(p.Expect(op.Min(3, 'a'))) // 3 * 'a'

	// Reset parser to start.
	p.Jump(start)
	fmt.Println(p.Expect(op.Min(4, 'a'))) // err
}
Output:

U+0061: a <nil>
<nil> parse conflict [00:002]: expected op.Range 'a'{4:-1} but got "aaa"

func MinMax

func MinMax(min, max int, i interface{}) Range

MinMax returns the range '[min:max]'.

Example
package main

import (
	"fmt"
	"github.com/di-wu/parser"
	"github.com/di-wu/parser/op"
)

func main() {
	p, _ := parser.New([]byte("aaab"))

	fmt.Println(p.Expect(op.MinMax(1, 2, 'a')))  // 2 * 'a'
	fmt.Println(p.Expect(op.MinMax(1, 3, 'a')))  // 1 * 'a'
	fmt.Println(p.Expect(op.MinMax(0, 1, 'b')))  // 1 * 'b'
	fmt.Println(p.Expect(op.MinMax(1, -1, 'c'))) // err
}
Output:

U+0061: a <nil>
U+0061: a <nil>
U+0062: b <nil>
<nil> parse conflict [00:004]: expected op.Range 'c'+ but got ""

func MinOne

func MinOne(i interface{}) Range

MinOne returns the range '[1:['.

Example
package main

import (
	"fmt"
	"github.com/di-wu/parser"
	"github.com/di-wu/parser/op"
)

func main() {
	p, _ := parser.New([]byte("aaab"))

	fmt.Println(p.Expect(op.MinOne('a'))) // 3 * 'a'
	fmt.Println(p.Expect(op.MinOne('b'))) // 1 * 'b'
	fmt.Println(p.Expect(op.MinOne('c'))) // err
}
Output:

U+0061: a <nil>
U+0062: b <nil>
<nil> parse conflict [00:004]: expected op.Range 'c'+ but got ""

func MinZero

func MinZero(i interface{}) Range

MinZero returns the range '[0:['.

Example
package main

import (
	"fmt"
	"github.com/di-wu/parser"
	"github.com/di-wu/parser/op"
)

func main() {
	p, _ := parser.New([]byte("aaab"))

	fmt.Println(p.Expect(op.MinZero('a'))) // 3 * 'a'
	fmt.Println(p.Expect(op.MinZero('b'))) // 1 * 'b'
	fmt.Println(p.Expect(op.MinZero('c'))) // 0 * 'c'
}
Output:

U+0061: a <nil>
U+0062: b <nil>
<nil> <nil>

func Optional

func Optional(i interface{}) Range

Optional returns the range '[0:1]'. It represents an optional value.

Example
package main

import (
	"fmt"
	"github.com/di-wu/parser"
	"github.com/di-wu/parser/op"
)

func main() {
	p, _ := parser.New([]byte("ac"))

	fmt.Println(p.Expect(op.Optional('a'))) // 1 * 'a'
	fmt.Println(p.Expect(op.Optional('b'))) // 0 * 'a'
	fmt.Println(p.Expect(op.Optional('c'))) // 1 * 'a'
}
Output:

U+0061: a <nil>
<nil> <nil>
U+0063: c <nil>

func Repeat

func Repeat(c int, i interface{}) Range

Repeat returns the range '[c:c]'. It checks for a specific number of values. The count needs to be greater or equal to 1.

Example
package main

import (
	"fmt"
	"github.com/di-wu/parser"
	"github.com/di-wu/parser/op"
)

func main() {
	p, _ := parser.New([]byte("abbbc"))

	fmt.Println(p.Expect(op.Repeat(1, 'a'))) // 1 * 'a'
	fmt.Println(p.Expect(op.Repeat(2, 'b'))) // 2 * b'
	fmt.Println(p.Expect(op.Repeat(1, 'c'))) // err
}
Output:

U+0061: a <nil>
U+0062: b <nil>
<nil> parse conflict [00:003]: expected op.Range 'c'{1:1} but got 'b'

type XOr

type XOr []interface{}

XOr represents a sequence of exclusive alternative values. Only one of the values van be valid. It can contain only one valid match.

Example
package main

import (
	"fmt"
	"github.com/di-wu/parser"
	"github.com/di-wu/parser/op"
)

func main() {
	p, _ := parser.New([]byte("data"))

	_, err := p.Expect(op.XOr{'d', "da", "data"})
	fmt.Println(err)
	_, err = p.Expect(op.XOr{'a', 't'})
	fmt.Println(err)
}
Output:

parse conflict [00:001]: expected op.XOr xor['d' "da" "data"] but got "da"
parse conflict [00:000]: expected op.XOr xor['a' 't'] but got 'd'

Jump to

Keyboard shortcuts

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