Skip to content

Commit 32409a2

Browse files
awoodbeckgriesemer
authored andcommitted
text/scanner: add examples
Added examples for use of Mode, Whitespace, and IsIdentRune properties. Fixes #23768 Change-Id: I2528e14fde63a4476f3c25510bf0c5b73f38ba5d Reviewed-on: https://go-review.googlesource.com/93199 Run-TryBot: Robert Griesemer <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent f69ad10 commit 32409a2

File tree

1 file changed

+105
-1
lines changed

1 file changed

+105
-1
lines changed

src/text/scanner/example_test.go

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2015 The Go Authors. All rights reserved.
1+
// Copyright 2018 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"strings"
1010
"text/scanner"
11+
"unicode"
1112
)
1213

1314
func Example() {
@@ -16,6 +17,7 @@ func Example() {
1617
if a > 10 {
1718
someParsable = text
1819
}`
20+
1921
var s scanner.Scanner
2022
s.Init(strings.NewReader(src))
2123
s.Filename = "example"
@@ -34,3 +36,105 @@ if a > 10 {
3436
// example:4:17: text
3537
// example:5:1: }
3638
}
39+
40+
func Example_isIdentRune() {
41+
const src = "%var1 var2%"
42+
43+
var s scanner.Scanner
44+
s.Init(strings.NewReader(src))
45+
s.Filename = "default"
46+
47+
for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
48+
fmt.Printf("%s: %s\n", s.Position, s.TokenText())
49+
}
50+
51+
fmt.Println()
52+
s.Init(strings.NewReader(src))
53+
s.Filename = "percent"
54+
55+
// treat leading '%' as part of an identifier
56+
s.IsIdentRune = func(ch rune, i int) bool {
57+
return ch == '%' && i == 0 || unicode.IsLetter(ch) || unicode.IsDigit(ch) && i > 0
58+
}
59+
60+
for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
61+
fmt.Printf("%s: %s\n", s.Position, s.TokenText())
62+
}
63+
64+
// Output:
65+
// default:1:1: %
66+
// default:1:2: var1
67+
// default:1:7: var2
68+
// default:1:11: %
69+
//
70+
// percent:1:1: %var1
71+
// percent:1:7: var2
72+
// percent:1:11: %
73+
}
74+
75+
func Example_mode() {
76+
const src = `
77+
// Comment begins at column 5.
78+
79+
This line should not be included in the output.
80+
81+
/*
82+
This multiline comment
83+
should be extracted in
84+
its entirety.
85+
*/
86+
`
87+
88+
var s scanner.Scanner
89+
s.Init(strings.NewReader(src))
90+
s.Filename = "comments"
91+
s.Mode ^= scanner.SkipComments // don't skip comments
92+
93+
for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
94+
txt := s.TokenText()
95+
if strings.HasPrefix(txt, "//") || strings.HasPrefix(txt, "/*") {
96+
fmt.Printf("%s: %s\n", s.Position, txt)
97+
}
98+
}
99+
100+
// Output:
101+
// comments:2:5: // Comment begins at column 5.
102+
// comments:6:1: /*
103+
// This multiline comment
104+
// should be extracted in
105+
// its entirety.
106+
// */
107+
}
108+
109+
func Example_whitespace() {
110+
// tab-separated values
111+
const src = `aa ab ac ad
112+
ba bb bc bd
113+
ca cb cc cd
114+
da db dc dd`
115+
116+
var (
117+
col, row int
118+
s scanner.Scanner
119+
tsv [4][4]string // large enough for example above
120+
)
121+
s.Init(strings.NewReader(src))
122+
s.Whitespace ^= 1<<'\t' | 1<<'\n' // don't skip tabs and new lines
123+
124+
for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
125+
switch tok {
126+
case '\n':
127+
row++
128+
col = 0
129+
case '\t':
130+
col++
131+
default:
132+
tsv[row][col] = s.TokenText()
133+
}
134+
}
135+
136+
fmt.Print(tsv)
137+
138+
// Output:
139+
// [[aa ab ac ad] [ba bb bc bd] [ca cb cc cd] [da db dc dd]]
140+
}

0 commit comments

Comments
 (0)