Documentation ¶
Overview ¶
package combinator provides additional parsers that combines parsers.
Index ¶
- func Alt[E comparable, O parser.ParseOutput](parsers ...parser.Parser[E, O]) parser.Parser[E, O]
- func Branches[E comparable, O parser.ParseOutput](rules map[E]parser.Parser[E, O]) parser.Parser[E, O]
- func Delimited[E comparable, O1 parser.ParseOutput, O2 parser.ParseOutput, ...](begin parser.Parser[E, O1], contents parser.Parser[E, O2], ...) parser.Parser[E, O2]
- func Many0[E comparable, SO parser.ParseOutput](sub parser.Parser[E, SO]) parser.Parser[E, []SO]
- func Many1[E comparable, SO parser.ParseOutput](sub parser.Parser[E, SO]) parser.Parser[E, []SO]
- func ManyMinMax[E comparable, SO parser.ParseOutput](sub parser.Parser[E, SO], min uint, max uint) parser.Parser[E, []SO]
- func Map[E comparable, SO parser.ParseOutput, O parser.ParseOutput](sub parser.Parser[E, SO], fn func(SO) (O, error)) parser.Parser[E, O]
- func Preceded[E comparable, O1 parser.ParseOutput, O2 parser.ParseOutput](predecessor parser.Parser[E, O1], successor parser.Parser[E, O2]) parser.Parser[E, O2]
- func Satisfy[E comparable](pred Predicate[E]) parser.Parser[E, E]
- func Separated1[E comparable, EO parser.ParseOutput, SO parser.ParseOutput](element parser.Parser[E, EO], separator parser.Parser[E, SO]) parser.Parser[E, []EO]
- func Sequence[E comparable, SO parser.ParseOutput](subs []parser.Parser[E, SO]) parser.Parser[E, []SO]
- func Take[E comparable, SO parser.ParseOutput](count uint, sub parser.Parser[E, SO]) parser.Parser[E, []SO]
- func Terminated[E comparable, O1 parser.ParseOutput, O2 parser.ParseOutput](predecessor parser.Parser[E, O1], successor parser.Parser[E, O2]) parser.Parser[E, O1]
- func Twin[E comparable, SO1 parser.ParseOutput, SO2 parser.ParseOutput](s1 parser.Parser[E, SO1], s2 parser.Parser[E, SO2]) parser.Parser[E, TwinResult[SO1, SO2]]
- type AllParsersFailedError
- type ApplicableRuleIsNotFoundError
- type NotSatisfiedCountError
- type NotSatisfiedError
- type Predicate
- type TwinResult
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Alt ¶
func Alt[E comparable, O parser.ParseOutput](parsers ...parser.Parser[E, O]) parser.Parser[E, O]
Alt initializes a parser that applies all given parsers. if all of them are failed to parse, Alt() parser also returns an error. otherwise Alt() succeeds to parse.
Example ¶
package main import ( "fmt" "github.com/Drumato/peachcomb/pkg/combinator" "github.com/Drumato/peachcomb/pkg/strparse" ) func main() { p1 := strparse.Rune('a') p2 := strparse.Rune('b') p := combinator.Many1(combinator.Alt(p1, p2)) i := strparse.NewCompleteInput("abababc") _, o, err := p(i) fmt.Println(string(o)) fmt.Println(err) }
Output: ababab <nil>
func Branches ¶ added in v0.1.8
func Branches[E comparable, O parser.ParseOutput](rules map[E]parser.Parser[E, O]) parser.Parser[E, O]
Branches initializes a parser that receives multiple syntax-rules and determine one of them. In almost cases, user can enumerate all syntax rules before starting parsing. so branches receives map. We recommend you to initialize the map at once and refer multiple times. It may be efficient. if no applicable rule exists in the rules, Branches() parser returns an error. if all of them are failed to parse, Branches() parser also returns an error.
Example ¶
package main import ( "fmt" "github.com/Drumato/peachcomb/pkg/byteparse" "github.com/Drumato/peachcomb/pkg/combinator" "github.com/Drumato/peachcomb/pkg/parser" ) func main() { m := make(map[byte]parser.Parser[byte, string]) m[0x00] = combinator.Map(byteparse.UInt8(), func(v uint8) (string, error) { return "0x00", nil }) m[0x01] = combinator.Map(byteparse.UInt8(), func(v uint8) (string, error) { return "0x01", nil }) p := combinator.Many1(combinator.Branches(m)) i := byteparse.NewCompleteInput([]byte{0x00, 0x01, 0x00, 0x01, 0x02}) _, o, err := p(i) fmt.Println(len(o)) fmt.Printf("%s %s %s %s\n", o[0], o[1], o[2], o[3]) fmt.Println(err) }
Output: 4 0x00 0x01 0x00 0x01 <nil>
func Delimited ¶
func Delimited[ E comparable, O1 parser.ParseOutput, O2 parser.ParseOutput, O3 parser.ParseOutput, ]( begin parser.Parser[E, O1], contents parser.Parser[E, O2], end parser.Parser[E, O3], ) parser.Parser[E, O2]
Delimited initializes a parser that parses a delimited sequence. (e.g. '[foobar]')
Example ¶
package main import ( "fmt" "github.com/Drumato/peachcomb/pkg/combinator" "github.com/Drumato/peachcomb/pkg/strparse" ) func main() { begin := strparse.Rune('(') end := strparse.Rune(')') contents := strparse.Digit1() p := combinator.Delimited(begin, contents, end) i := strparse.NewCompleteInput("(12321)") _, o, err := p(i) fmt.Println(o) fmt.Println(err) }
Output: 12321 <nil>
func Many0 ¶ added in v0.1.7
func Many0[E comparable, SO parser.ParseOutput](sub parser.Parser[E, SO]) parser.Parser[E, []SO]
Many0 initializes a parser that applies the given sub-parser several times.
Example ¶
package main import ( "fmt" "github.com/Drumato/peachcomb/pkg/combinator" "github.com/Drumato/peachcomb/pkg/strparse" ) func main() { p := combinator.Many0(strparse.Rune('a')) i := strparse.NewCompleteInput("baaaa") _, o, err := p(i) fmt.Println(string(o)) fmt.Println(err) }
Output: <nil>
func Many1 ¶ added in v0.1.7
func Many1[E comparable, SO parser.ParseOutput](sub parser.Parser[E, SO]) parser.Parser[E, []SO]
Many1 initializes a parser that applies the given sub-parser several times. if the sub parser fails to parse and the count of application times is 0 Many11 parser return an error.
Example ¶
package main import ( "fmt" "github.com/Drumato/peachcomb/pkg/combinator" "github.com/Drumato/peachcomb/pkg/strparse" ) func main() { p := combinator.Many1(strparse.Rune('a')) i := strparse.NewCompleteInput("aaaabaa") _, o, err := p(i) fmt.Println(string(o)) fmt.Println(err) }
Output: aaaa <nil>
func ManyMinMax ¶ added in v0.2.2
func ManyMinMax[E comparable, SO parser.ParseOutput](sub parser.Parser[E, SO], min uint, max uint) parser.Parser[E, []SO]
ManyMinMax initializes a parser that applies the given sub-parser several times. It fails if the sub parser does not succeed at least min times. It also fails if the sub parser does succeed over max times.
Example ¶
package main import ( "fmt" "github.com/Drumato/peachcomb/pkg/combinator" "github.com/Drumato/peachcomb/pkg/strparse" ) func main() { p := combinator.ManyMinMax(strparse.Rune('a'), 3, 5) i := strparse.NewCompleteInput("aaaabbb") _, o, err := p(i) fmt.Println(string(o)) fmt.Println(err) }
Output: aaaa <nil>
func Map ¶
func Map[E comparable, SO parser.ParseOutput, O parser.ParseOutput](sub parser.Parser[E, SO], fn func(SO) (O, error)) parser.Parser[E, O]
Map initializes a parser that applies a sub-parser and give the it's output to fn.
Example ¶
package main import ( "fmt" "github.com/Drumato/peachcomb/pkg/combinator" "github.com/Drumato/peachcomb/pkg/strparse" ) func main() { subsubP := strparse.Rune('a') subP := combinator.Many1(subsubP) p := combinator.Map(subP, func(s []rune) (int, error) { return len(s), nil }) i := strparse.NewCompleteInput("aaaabaaaa") _, o, err := p(i) fmt.Println(o) fmt.Println(err) }
Output: 4 <nil>
func Preceded ¶ added in v0.1.5
func Preceded[ E comparable, O1 parser.ParseOutput, O2 parser.ParseOutput, ](predecessor parser.Parser[E, O1], successor parser.Parser[E, O2]) parser.Parser[E, O2]
Preceded initializes a parser that applies given parsers but discards predecessor's output.
Example ¶
package main import ( "fmt" "github.com/Drumato/peachcomb/pkg/combinator" "github.com/Drumato/peachcomb/pkg/strparse" ) func main() { predecessor := strparse.Rune('*') successor := strparse.Rune('a') p := combinator.Preceded(predecessor, successor) i := strparse.NewCompleteInput("*a") _, o, err := p(i) fmt.Printf("%c\n", o) fmt.Println(err) }
Output: a <nil>
func Satisfy ¶
func Satisfy[E comparable](pred Predicate[E]) parser.Parser[E, E]
Satisfy initializes a parser that checks the head of the input satisfies the predicate.
Example ¶
package main import ( "fmt" "github.com/Drumato/peachcomb/pkg/combinator" "github.com/Drumato/peachcomb/pkg/strparse" ) func main() { i := strparse.NewCompleteInput("abc") _, o, err := combinator.Satisfy(func(ch rune) bool { return ch == 'a' })(i) fmt.Printf("%c\n", o) fmt.Println(err) }
Output: a <nil>
func Separated1 ¶
func Separated1[E comparable, EO parser.ParseOutput, SO parser.ParseOutput](element parser.Parser[E, EO], separator parser.Parser[E, SO]) parser.Parser[E, []EO]
Example ¶
package main import ( "fmt" "github.com/Drumato/peachcomb/pkg/combinator" "github.com/Drumato/peachcomb/pkg/strparse" ) func main() { element := strparse.Digit1() separator := strparse.Rune('|') p := combinator.Separated1(element, separator) i := strparse.NewCompleteInput("123|456|789Drumato") _, o, err := p(i) fmt.Printf("%d\n", len(o)) fmt.Printf("%s %s %s\n", o[0], o[1], o[2]) fmt.Println(err) }
Output: 3 123 456 789 <nil>
func Sequence ¶ added in v1.0.1
func Sequence[E comparable, SO parser.ParseOutput](subs []parser.Parser[E, SO]) parser.Parser[E, []SO]
Sequence initializes a parser that applies a sequence of sub-parsers.
Example ¶
package main import ( "fmt" "github.com/Drumato/peachcomb/pkg/combinator" "github.com/Drumato/peachcomb/pkg/parser" "github.com/Drumato/peachcomb/pkg/strparse" ) func main() { a := strparse.Rune('a') b := strparse.Rune('b') c := strparse.Rune('c') p := combinator.Sequence([]parser.Parser[rune, rune]{a, b, c}) i := strparse.NewCompleteInput("abc") _, o, err := p(i) fmt.Println(len(o)) fmt.Printf("%c %c %c\n", o[0], o[1], o[2]) fmt.Println(err) }
Output: 3 a b c <nil>
func Take ¶ added in v0.2.3
func Take[E comparable, SO parser.ParseOutput](count uint, sub parser.Parser[E, SO]) parser.Parser[E, []SO]
Take initializes a parser that applies sub-parser count times.
Example ¶
package main import ( "fmt" "github.com/Drumato/peachcomb/pkg/combinator" "github.com/Drumato/peachcomb/pkg/strparse" ) func main() { p := combinator.Take(5, strparse.Rune('a')) i := strparse.NewCompleteInput("aaaaabbb") _, o, err := p(i) fmt.Println(len(o)) fmt.Println(err) }
Output: 5 <nil>
func Terminated ¶ added in v0.1.6
func Terminated[ E comparable, O1 parser.ParseOutput, O2 parser.ParseOutput, ](predecessor parser.Parser[E, O1], successor parser.Parser[E, O2]) parser.Parser[E, O1]
Terminated initializes a parser that applies given parsers but discards successor's output.
Example ¶
package main import ( "fmt" "github.com/Drumato/peachcomb/pkg/combinator" "github.com/Drumato/peachcomb/pkg/strparse" ) func main() { predecessor := strparse.Rune('a') successor := strparse.Rune('+') p := combinator.Terminated(predecessor, successor) i := strparse.NewCompleteInput("a+") _, o, err := p(i) fmt.Printf("%c\n", o) fmt.Println(err) }
Output: a <nil>
func Twin ¶ added in v1.0.1
func Twin[E comparable, SO1 parser.ParseOutput, SO2 parser.ParseOutput]( s1 parser.Parser[E, SO1], s2 parser.Parser[E, SO2], ) parser.Parser[E, TwinResult[SO1, SO2]]
Twin initializes a parser that applies two sub-parsers sequentially.
Example ¶
package main import ( "fmt" "github.com/Drumato/peachcomb/pkg/combinator" "github.com/Drumato/peachcomb/pkg/strparse" ) func main() { one := strparse.Rune('1') two := combinator.Map(strparse.Rune('2'), func(s rune) (string, error) { return "two", nil }) p := combinator.Twin(one, two) i := strparse.NewCompleteInput("12") _, o, err := p(i) fmt.Println(string(o.One)) fmt.Println(o.Two) fmt.Println(err) }
Output: 1 two <nil>
Types ¶
type AllParsersFailedError ¶ added in v0.1.8
type AllParsersFailedError struct{}
AllParsersFailedError notifies all of given parsers are failed to parse.
func (*AllParsersFailedError) Error ¶ added in v0.1.8
func (e *AllParsersFailedError) Error() string
Error implements error interface.
type ApplicableRuleIsNotFoundError ¶ added in v0.1.8
type ApplicableRuleIsNotFoundError[E comparable] struct { // contains filtered or unexported fields }
ApplicableRuleIsNotFoundError notifies all of given parsers don't match the head of the input.
func (*ApplicableRuleIsNotFoundError[E]) Error ¶ added in v0.1.8
func (e *ApplicableRuleIsNotFoundError[E]) Error() string
Error implements error interface.
type NotSatisfiedCountError ¶
type NotSatisfiedCountError struct { }
NotSatisfiedCountError notifies the count of sub-parser success are not satisfied.
func (*NotSatisfiedCountError) Error ¶
func (e *NotSatisfiedCountError) Error() string
Error implements error interface.
type NotSatisfiedError ¶
type NotSatisfiedError[E comparable] struct { // contains filtered or unexported fields }
NotsatisfiedError notifies that the given predicate is not satisfied.
func (*NotSatisfiedError[E]) Error ¶
func (e *NotSatisfiedError[E]) Error() string
Error implements error interface
type Predicate ¶
type Predicate[E comparable] func(element E) bool
Predicate is the condition that satisfyParser uses for consuming one element.
type TwinResult ¶ added in v1.0.1
type TwinResult[SO1 parser.ParseOutput, SO2 parser.ParseOutput] struct { One SO1 Two SO2 }