コード例 #1
0
func ExampleToken() {
	p := kern.Token(utf88.Text("ab"))

	t := utf88.Text("abc")
	r, ok := kern.ParseText(p, t)
	if ok {
		fmt.Printf("Result: %s\n", utf88.Surr(r.(utf88.Text)))
	} else {
		fmt.Printf("Error: %v\n", r)
	}

	// Output:
	// Result: ab
}
コード例 #2
0
func ExampleFwdWithParams() {
	var expr func(...interface{}) kern.Parser

	paren := func(as ...interface{}) kern.Parser {
		return kern.SeqRight(kern.Token(utf88.Text(string('('))),
			kern.SeqLeft(kern.FwdWithParams(expr, as...), // FwdWithParams will evaluate passed expression lazily
				kern.Token(utf88.Text(string(')')))))
	}
	expr = func(as ...interface{}) kern.Parser { // will parse string enclosed in parenthesis pairs to any depth
		return kern.Alt(kern.Token(utf88.Text(string('a'))),
			paren(as...))
	}

	t := utf88.Text("(((a)))")
	r, ok := kern.ParseText(expr(101, 102), t) // call expr with extra (unused in this e.g.) args
	if ok {
		fmt.Printf("Result: %s\n", utf88.Surr(r.(utf88.Text)))
	} else {
		fmt.Printf("Error: %v\n", r)
	}

	// Output:
	// Result: a
}
コード例 #3
0
func ExampleFwd() {
	var expr func() kern.Parser

	paren := func() kern.Parser {
		return kern.SeqRight(kern.Token(utf88.Text(string('('))),
			kern.SeqLeft(kern.Fwd(expr), // Fwd will evaluate passed expression lazily
				kern.Token(utf88.Text(string(')')))))
	}
	expr = func() kern.Parser { // will parse string enclosed in parenthesis pairs to any depth
		return kern.Alt(kern.Token(utf88.Text(string('a'))),
			paren())
	}

	t := utf88.Text("(((a)))")
	r, ok := kern.ParseText(expr(), t)
	if ok {
		fmt.Printf("Result: %s\n", utf88.Surr(r.(utf88.Text)))
	} else {
		fmt.Printf("Error: %v\n", r)
	}

	// Output:
	// Result: a
}