Documentation ¶
Index ¶
- func Between[T stream.Token, O1, O2, O3 any](open parser.Func[T, O1], closing parser.Func[T, O2], p parser.Func[T, O3]) parser.Func[T, O3]
- func Skip[T stream.Token, O1, O2 any](p1 parser.Func[T, O1], p2 parser.Func[T, O2]) parser.Func[T, O1]
- func Then[T stream.Token, I, O any](p parser.Func[T, I], f func(I) parser.Func[T, O]) parser.Func[T, O]
- func With[T stream.Token, O1, O2 any](p1 parser.Func[T, O1], p2 parser.Func[T, O2]) parser.Func[T, O2]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Between ¶
func Between[ T stream.Token, O1, O2, O3 any, ]( open parser.Func[T, O1], closing parser.Func[T, O2], p parser.Func[T, O3], ) parser.Func[T, O3]
Between parses `open` followed by `p` followed by `closing`. / Returns the value of `p`.
Example ¶
package main import ( "fmt" "github.com/flier/gocombine/pkg/parser/char" "github.com/flier/gocombine/pkg/parser/sequence" "github.com/flier/gocombine/pkg/parser/to" ) func main() { p := sequence.Between( char.Char('['), char.Char(']'), to.String(char.String("Golang"))) fmt.Println(p([]rune("[Golang]"))) }
Output: Golang [] <nil>
func Skip ¶
func Skip[T stream.Token, O1, O2 any](p1 parser.Func[T, O1], p2 parser.Func[T, O2]) parser.Func[T, O1]
Skip discards the value of the `p2` parser and returns the value of `p1`. / Fails if any of the parsers fails.
Example ¶
package main import ( "fmt" "github.com/flier/gocombine/pkg/parser/char" "github.com/flier/gocombine/pkg/parser/repeat" "github.com/flier/gocombine/pkg/parser/sequence" ) func main() { p := sequence.Skip(repeat.Many(char.Digit()), char.Char('i')) fmt.Println(p([]rune("123i456"))) }
Output: [49 50 51] [52 53 54] <nil>
func Then ¶
func Then[T stream.Token, I, O any](p parser.Func[T, I], f func(I) parser.Func[T, O]) parser.Func[T, O]
Then parses using `p` and then passes the value to `f` which returns a parser used to parse the rest of the input.
Example ¶
package main import ( "fmt" "github.com/flier/gocombine/pkg/parser" "github.com/flier/gocombine/pkg/parser/char" "github.com/flier/gocombine/pkg/parser/repeat" "github.com/flier/gocombine/pkg/parser/sequence" "github.com/flier/gocombine/pkg/parser/to" "github.com/flier/gocombine/pkg/parser/token" ) func main() { comment := sequence.With(char.Spaces(), repeat.Many(token.Satisfy(func(c rune) bool { return c != '\n' }))) p := to.String(sequence.Then(char.Any(), func(c rune) parser.Func[rune, []rune] { if c == '#' { return comment } return repeat.Many1(char.Letter()).Map(func(s []rune) []rune { return append([]rune{c}, s...) }) })) fmt.Println(p([]rune("ac2"))) fmt.Println(p([]rune("# ac2"))) }
Output: ac [50] <nil> ac2 [] <nil>
func With ¶
func With[T stream.Token, O1, O2 any](p1 parser.Func[T, O1], p2 parser.Func[T, O2]) parser.Func[T, O2]
With discards the value of the `p1` parser and returns the value of `p2`. Fails if any of the parsers fails.
Example ¶
package main import ( "fmt" "github.com/flier/gocombine/pkg/parser/char" "github.com/flier/gocombine/pkg/parser/repeat" "github.com/flier/gocombine/pkg/parser/sequence" ) func main() { p := sequence.With(repeat.Many(char.Digit()), char.Char('i')) fmt.Println(p([]rune("123i456"))) }
Output: 105 [52 53 54] <nil>
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.