choice

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

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func And

func And[T stream.Token, O any](parsers ...parser.Func[T, O]) parser.Func[T, []O]

And parses with `parsers`. Succeeds if all parsers succeed, otherwise fails. Returns a slice with all values on success.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
	"github.com/flier/gocombine/pkg/parser/choice"
)

func main() {
	p := choice.And(char.Digit(), char.Char('i'))

	fmt.Println(p([]rune("9i456")))
	fmt.Println(p([]rune("123")))

}
Output:

[57 105] [52 53 54] <nil>
[49 50] [51] and, char, expected 'i', actual '2', unexpected

func Optional

func Optional[T stream.Token, O any](parser parser.Func[T, O]) parser.Func[T, option.Option[O]]

Optional parses `parser` and outputs `Some(value)` if it succeeds, `None` if it fails without consuming any input.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
	"github.com/flier/gocombine/pkg/parser/choice"
	"github.com/flier/gocombine/pkg/parser/to"
)

func main() {
	p := choice.Optional(to.String(char.String("hello")))

	fmt.Println(p([]rune("hello")))
	fmt.Println(p([]rune("world")))

}
Output:

hello [] <nil>
<none> [119 111 114 108 100] <nil>

func Or

func Or[T stream.Token, O any](parsers ...parser.Func[T, O]) parser.Func[T, O]

Or returns a parser which attempts to parse using `parsers`.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
	"github.com/flier/gocombine/pkg/parser/choice"
	"github.com/flier/gocombine/pkg/parser/combinator"
	"github.com/flier/gocombine/pkg/parser/to"
)

func main() {
	p := choice.Or(
		to.String(char.String("let")),
		combinator.Map(char.Digit(), func(r rune) string { return "digit" }),
		to.String(char.String("led")),
	)

	fmt.Println(p([]rune("let")))
	fmt.Println(p([]rune("1")))
	fmt.Println(p([]rune("lost")))

}
Output:

let [] <nil>
digit [] <nil>
 [115 116] or, 3 errors occurred:
	* map, string, cmp, expected "let", actual "lo", unexpected
	* map, digit, satisfy, actual 'l', unexpected
	* map, string, cmp, expected "led", actual "lo", unexpected

Types

This section is empty.

Jump to

Keyboard shortcuts

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