parser - examples_test.go
1 package parser_test
2
3 import (
4 "fmt"
5 "strings"
6
7 "vimagination.zapto.org/parser"
8 )
9
10 func ExampleNewByteTokeniser() {
11 p := parser.NewByteTokeniser([]byte("Hello, World!"))
12 alphaNum := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
13 p.AcceptRun(alphaNum)
14 word := p.Get()
15 fmt.Println("got word:", word)
16
17 p.ExceptRun(alphaNum)
18 p.Get()
19
20 p.AcceptRun(alphaNum)
21 word = p.Get()
22 fmt.Println("got word:", word)
23 // Output:
24 // got word: Hello
25 // got word: World
26 }
27
28 func ExampleNewStringTokeniser() {
29 p := parser.NewStringTokeniser("Hello, World!")
30 alphaNum := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
31 p.AcceptRun(alphaNum)
32 word := p.Get()
33 fmt.Println("got word:", word)
34
35 p.ExceptRun(alphaNum)
36 p.Get()
37
38 p.AcceptRun(alphaNum)
39 word = p.Get()
40 fmt.Println("got word:", word)
41 // Output:
42 // got word: Hello
43 // got word: World
44 }
45
46 func ExampleNewReaderTokeniser() {
47 p := parser.NewReaderTokeniser(strings.NewReader("Hello, World!"))
48 alphaNum := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
49 p.AcceptRun(alphaNum)
50 word := p.Get()
51 fmt.Println("got word:", word)
52
53 p.ExceptRun(alphaNum)
54 p.Get()
55
56 p.AcceptRun(alphaNum)
57 word = p.Get()
58 fmt.Println("got word:", word)
59 // Output:
60 // got word: Hello
61 // got word: World
62 }
63