File tree 12 files changed +162
-119
lines changed
12 files changed +162
-119
lines changed Original file line number Diff line number Diff line change 1
1
# 包名
2
2
3
- 函数列表
3
+ 包列表
4
4
5
- - xxx1
6
- - xxx2
5
+ - [ text/scanner ] ( scanner )
6
+ - xxx2
Original file line number Diff line number Diff line change
1
+ const (
2
+ ScanIdents = 1 << -Ident
3
+ ScanInts = 1 << -Int
4
+ ScanFloats = 1 << -Float // includes Ints
5
+ ScanChars = 1 << -Char
6
+ ScanStrings = 1 << -String
7
+ ScanRawStrings = 1 << -RawString
8
+ ScanComments = 1 << -Comment
9
+ SkipComments = 1 << -skipComment // if set with ScanComments, comments become white space
10
+ GoTokens = ScanIdents | ScanFloats | ScanChars | ScanStrings | ScanRawStrings | ScanComments | SkipComments
11
+ )
12
+
13
+ 用于控制词法单元识别的预定义模式位。比如,通过以下模式位设置,可以配置一个只识别(Go)标识符、整型、并且忽略注释的词法解析器:
14
+
15
+ ScanIdents | ScanInts | SkipComments
16
+
17
+ const (
18
+ EOF = -(iota + 1)
19
+ Ident
20
+ Int
21
+ Float
22
+ Char
23
+ String
24
+ RawString
25
+ Comment
26
+ )
27
+
28
+ 通过Scan方法可以获取的词法单元,此外还可以获取Unicode字符。
29
+
30
+ const GoWhitespace = 1<<'\t' | 1<<'\n' | 1<<'\r' | 1<<' '
31
+
32
+ GoWhitespace,Scanner的空白字符的默认值。
33
+
Original file line number Diff line number Diff line change 14
14
15
15
代码实例:
16
16
17
- package main
17
+ package main
18
18
19
- import(
20
- "fmt"
21
- "strings"
22
- "text/scanner"
23
- )
19
+ import(
20
+ "fmt"
21
+ "strings"
22
+ "text/scanner"
23
+ )
24
24
25
- func main(){
25
+ func main(){
26
26
27
- src := strings.NewReader("int hello = 3; hello+23; print hello;")
27
+ src := strings.NewReader("int hello = 3; hello+23; print hello;")
28
28
29
- fmt.Println(src)
29
+ fmt.Println(src)
30
30
31
- var s scanner.Scanner
32
- s.Init(src)
31
+ var s scanner.Scanner
32
+ s.Init(src)
33
33
34
- tok := s.Scan()
35
- fmt.Println(s.TokenText())
36
- for tok != scanner.EOF{
37
- tok = s.Scan()
34
+ tok := s.Scan()
38
35
fmt.Println(s.TokenText())
36
+ for tok != scanner.EOF{
37
+ tok = s.Scan()
38
+ fmt.Println(s.TokenText())
39
+ }
39
40
}
40
- }
41
41
Original file line number Diff line number Diff line change 14
14
15
15
代码实例:
16
16
17
- package main
17
+ package main
18
18
19
- import(
20
- "fmt"
21
- "strings"
22
- "text/scanner"
23
- )
19
+ import(
20
+ "fmt"
21
+ "strings"
22
+ "text/scanner"
23
+ )
24
24
25
- func main(){
25
+ func main(){
26
26
27
- src := strings.NewReader("int num = 1;")
28
- var s scanner.Scanner
27
+ src := strings.NewReader("int num = 1;")
28
+ var s scanner.Scanner
29
29
30
- s.Init(src)
30
+ s.Init(src)
31
31
32
- s.Next()
32
+ s.Next()
33
33
34
- pos := s.Pos()
35
- fmt.Println(pos.IsValid())
34
+ pos := s.Pos()
35
+ fmt.Println(pos.IsValid())
36
36
37
- }
37
+ }
38
38
Original file line number Diff line number Diff line change 14
14
15
15
代码实例:
16
16
17
- package main
17
+ package main
18
18
19
- import (
20
- "fmt"
21
- "strings"
22
- "text/scanner"
23
- )
19
+ import (
20
+ "fmt"
21
+ "strings"
22
+ "text/scanner"
23
+ )
24
24
25
- func main() {
25
+ func main() {
26
26
27
- src := strings.NewReader("int num = 1;")
27
+ src := strings.NewReader("int num = 1;")
28
28
29
- var s scanner.Scanner
29
+ var s scanner.Scanner
30
30
31
- s.Init(src)
31
+ s.Init(src)
32
32
33
- //this will print "i" to stdout
34
- fmt.Println(scanner.TokenString(s.Next()))
33
+ //this will print "i" to stdout
34
+ fmt.Println(scanner.TokenString(s.Next()))
35
35
36
- }
36
+ }
37
37
Original file line number Diff line number Diff line change 14
14
15
15
代码实例:
16
16
17
- package main
17
+ package main
18
18
19
- import(
20
- "fmt"
21
- "strings"
22
- "text/scanner"
23
- )
19
+ import(
20
+ "fmt"
21
+ "strings"
22
+ "text/scanner"
23
+ )
24
24
25
- func main(){
25
+ func main(){
26
26
27
- src := strings.NewReader("int num = 1;")
27
+ src := strings.NewReader("int num = 1;")
28
28
29
- var s scanner.Scanner
30
- s.Init(src)
29
+ var s scanner.Scanner
30
+ s.Init(src)
31
31
32
- //this will print "i" to stdout
33
- fmt.Println(scanner.TokenString(s.Peek()))
34
- }
32
+ //this will print "i" to stdout
33
+ fmt.Println(scanner.TokenString(s.Peek()))
34
+ }
35
35
Original file line number Diff line number Diff line change
1
+ type Position struct {
2
+ Filename string // 文件名,可能为空
3
+ Offset int // 位移,从0计
4
+ Line int // 行数,从1计
5
+ Column int // 列数,从1计(按字符计算)
6
+ }
7
+
8
+ Position用来表示源中的位置。行数Line > 0的情况下,Position合法。
9
+
Original file line number Diff line number Diff line change 1
1
# text/scanner包函数列表
2
2
3
- Constants
3
+ - [ Constants] ( Constants.md )
4
4
- [ func TokenString(tok rune) string] ( TokenString.md )
5
- type Position
5
+ - [ type Position] ( Position.md )
6
6
- [ func (pos * Position) IsValid() bool] ( IsValid.md )
7
7
- [ func (pos Position) String() string] ( String.md )
8
- type Scanner
8
+ - type Scanner
9
+ Scanner用于从io.Reader中读取Unicode字符或词法单元
9
10
- [func (s *Scanner) Init(src io.Reader) *Scanner](Init.md)
10
11
- [func (s *Scanner) Next() rune](Next.md)
11
12
- [func (s *Scanner) Peek() rune](Peek.md)
Original file line number Diff line number Diff line change @@ -14,23 +14,23 @@ Scan方法读取源中下一个token或字符,并返回。如果读到源的
14
14
15
15
代码实例:
16
16
17
- package main
17
+ package main
18
18
19
- import(
20
- "fmt"
21
- "strings"
22
- "text/scanner"
23
- )
19
+ import(
20
+ "fmt"
21
+ "strings"
22
+ "text/scanner"
23
+ )
24
24
25
- func main(){
25
+ func main(){
26
26
27
- src := strings.NewReader("int num = 1;")
28
- var s scanner.Scanner
27
+ src := strings.NewReader("int num = 1;")
28
+ var s scanner.Scanner
29
29
30
- s.Init(src)
30
+ s.Init(src)
31
31
32
- s.Scan()
33
- //this will print the next token "int "to stdout
34
- fmt.Println(s.TokenText())
35
- }
32
+ s.Scan()
33
+ //this will print the next token "int "to stdout
34
+ fmt.Println(s.TokenText())
35
+ }
36
36
Original file line number Diff line number Diff line change 14
14
15
15
代码实例:
16
16
17
- package main
17
+ package main
18
18
19
- import(
20
- "fmt"
21
- "strings"
22
- "text/scanner"
23
- )
19
+ import(
20
+ "fmt"
21
+ "strings"
22
+ "text/scanner"
23
+ )
24
24
25
- func main(){
25
+ func main(){
26
26
27
- src := strings.NewReader("int num = 1;")
28
- var s scanner.Scanner
27
+ src := strings.NewReader("int num = 1;")
28
+ var s scanner.Scanner
29
29
30
- s.Init(src)
30
+ s.Init(src)
31
31
32
- s.Scan()
33
- //this will print "1:4" to stdout
34
- fmt.Println(s.Pos().String())
32
+ s.Scan()
33
+ //this will print "1:4" to stdout
34
+ fmt.Println(s.Pos().String())
35
35
36
- s.Next()
37
- //this will print "1:5" to stdout
38
- fmt.Println(s.Pos().String())
39
- }
36
+ s.Next()
37
+ //this will print "1:5" to stdout
38
+ fmt.Println(s.Pos().String())
39
+ }
40
40
Original file line number Diff line number Diff line change 14
14
15
15
代码实例:
16
16
17
- package main
17
+ package main
18
18
19
- import(
20
- "fmt"
21
- "text/scanner"
22
- )
23
- func main(){
19
+ import(
20
+ "fmt"
21
+ "text/scanner"
22
+ )
23
+ func main(){
24
24
25
- toks := []rune("abcdef")
25
+ toks := []rune("abcdef")
26
26
27
- for _, tok := range toks{
28
- fmt.Print(scanner.TokenString(tok))
27
+ for _, tok := range toks{
28
+ fmt.Print(scanner.TokenString(tok))
29
+ }
29
30
}
30
- }
31
31
Original file line number Diff line number Diff line change 14
14
15
15
代码实例:
16
16
17
- package main
17
+ package main
18
18
19
- import(
20
- "fmt"
21
- "strings"
22
- "text/scanner"
23
- )
19
+ import(
20
+ "fmt"
21
+ "strings"
22
+ "text/scanner"
23
+ )
24
24
25
- func main(){
25
+ func main(){
26
26
27
- src := strings.NewReader("int num = 1;")
28
- var s scanner.Scanner
27
+ src := strings.NewReader("int num = 1;")
28
+ var s scanner.Scanner
29
29
30
- s.Init(src)
30
+ s.Init(src)
31
31
32
- //this will print nothing
33
- fmt.Print(s.TokenText())
32
+ //this will print nothing
33
+ fmt.Print(s.TokenText())
34
34
35
- s.Scan()
35
+ s.Scan()
36
36
37
- //this will print "int" to stdout
38
- fmt.Print(s.TokenText())
37
+ //this will print "int" to stdout
38
+ fmt.Print(s.TokenText())
39
39
40
- }
40
+ }
41
41
42
42
You can’t perform that action at this time.
0 commit comments