Ejemplo n.º 1
0
func ExampleSeqRight() {
	digit := kern.Regexp(`\p{Nd}`)
	letter := kern.Regexp(`\pL`)

	p := kern.SeqRight(digit, letter)

	t := utf88.Text("7efg")
	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: e
}
Ejemplo n.º 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
}
Ejemplo n.º 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
}