Пример #1
0
func ExampleBind() {
	var r interface{}
	var ok bool
	prt := func() {
		if ok {
			fmt.Printf("Result: %s\n", r)
		} else {
			fmt.Printf("Error: %v\n", r)
		}
	}

	lower := kern.Regexp(`\p{Ll}`)
	intNum := kern.Regexp(`\p{Nd}*`)

	p := kern.Bind(lower, func(c interface{}) kern.Parser {
		return kern.Bind(intNum, func(d interface{}) kern.Parser {
			return kern.Bind(lower, func(e interface{}) kern.Parser {
				return kern.Return(utf88.Surr(c.(utf88.Text)) + "," +
					utf88.Surr(d.(utf88.Text)) + "," +
					utf88.Surr(e.(utf88.Text)))
			})
		})
	})

	t := utf88.Text("e789fg")
	r, ok = kern.ParseText(p, t)
	prt()

	// Output:
	// Result: e,789,f
}
Пример #2
0
func ExampleSepBy() {
	var r interface{}
	var ok bool
	prt := func() {
		if ok {
			fmt.Printf("Result: %c\n", r)
		} else {
			fmt.Printf("Error: %v\n", r)
		}
	}

	letter := kern.Regexp(`\pL`)
	punc := kern.Regexp(`\pP`)
	p := kern.SepBy(punc, letter)

	t := utf88.Text("a;b:c789")
	u := utf88.Text(";789")

	r, ok = kern.ParseText(p, t)
	prt()
	r, ok = kern.ParseText(p, u)
	prt()

	// Output:
	// Result: [[a] [b] [c]]
	// Result: []
}
Пример #3
0
func ExampleAlt() {
	var r interface{}
	var ok bool
	prt := func() {
		if ok {
			fmt.Printf("Result: %s\n", utf88.Surr(r.(utf88.Text)))
		} else {
			fmt.Printf("Error: %v\n", r)
		}
	}

	lower := kern.Regexp(`\p{Ll}`)
	upper := kern.Regexp(`\p{Lu}`)
	digit := kern.Regexp(`\p{Nd}`)

	p := kern.Alt(lower, digit)
	q := kern.Alt(lower, digit, upper)

	t := utf88.Text("7ef")
	u := utf88.Text(";ef")

	r, ok = kern.ParseText(p, t)
	prt()

	r, ok = kern.ParseText(p, u)
	prt()

	r, ok = kern.ParseText(q, t)
	prt()

	// Output:
	// Result: 7
	// Error: No alternatives selected.
	// Result: 7
}
Пример #4
0
func ExampleTry() {
	p := kern.Flatten(kern.Regexp(`\pL`), kern.Regexp(`\pL`))
	q := kern.Flatten(kern.Regexp(`\pL`), kern.Regexp(`\pN`))

	t := utf88.Text("a7bcde")

	var r interface{}
	var ok bool
	prt := func() {
		if ok {
			fmt.Printf("Result: %s\n", utf88.Surr(r.(utf88.Text)))
		} else {
			fmt.Printf("Error: %v\n", r)
		}
	}

	r, ok = kern.ParseText(kern.Alt(p, q), t)
	prt()

	r, ok = kern.ParseText(kern.Alt(kern.Try(p), q), t)
	prt()

	// Output:
	// Error: Unexpected 7 input.
	// Result: a7
}
Пример #5
0
func ExampleOption() {
	var r interface{}
	var ok bool
	prt := func() {
		if ok {
			fmt.Printf("Result: %s\n", utf88.Surr(r.(utf88.Text)))
		} else {
			fmt.Printf("Error: %v\n", r)
		}
	}

	letter := kern.Regexp(`\pL`)
	p := kern.Option(utf88.Text("None."), letter)

	t := utf88.Text("abc")
	u := utf88.Text("789")

	r, ok = kern.ParseText(p, t)
	prt()
	r, ok = kern.ParseText(p, u)
	prt()

	// Output:
	// Result: a
	// Result: None.
}
Пример #6
0
func ExampleMany() {
	var r interface{}
	var ok bool
	prt := func() {
		if ok {
			fmt.Printf("Result: %c\n", r)
		} else {
			fmt.Printf("Error: %v\n", r)
		}
	}

	letter := kern.Regexp(`\pL`)
	p := kern.Many(letter)

	t := utf88.Text("abc78d")
	u := utf88.Text("789def")

	r, ok = kern.ParseText(p, t)
	prt()
	r, ok = kern.ParseText(p, u)
	prt()

	// Output:
	// Result: [[a] [b] [c]]
	// Result: []
}
Пример #7
0
func ExampleCollect() {
	letter := kern.Regexp(`\pL`)
	digit := kern.Regexp(`\p{Nd}`)

	p := kern.Collect(letter, digit, letter)

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

	// Output:
	// Result: [[a] [5] [b]]
}
Пример #8
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
}
Пример #9
0
func ExampleAsk() {
	letter := kern.Regexp(`\pL`)
	digit := kern.Regexp(`\p{Nd}`)

	p := kern.Ask(kern.Collect(digit, letter), utf88.Text("digit,letter"))

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

	// Output:
	// Error: Unexpected ; input. Expecting digit,letter
}
Пример #10
0
func ExampleRegexp() {
	p := kern.Regexp(`z|a|b`)

	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: a
}
Пример #11
0
func ExampleApply() {
	letter := kern.Regexp(`\pL`)

	p := kern.Apply(func(c interface{}) interface{} {
		cs := utf88.Surr(c.(utf88.Text))
		return cs + cs + cs
	}, letter)

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

	// Output:
	// Result: aaa
}