-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscan.go
More file actions
97 lines (85 loc) · 1.89 KB
/
scan.go
File metadata and controls
97 lines (85 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2014, Hǎiliàng Wáng. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package flow
import (
"io"
"h12.io/gombi/experiment/gre/scan"
)
const (
tokenEOF = iota
tokenComment
tokenLeftBrace
tokenRightBrace
tokenComma
tokenString
tokenSpace
)
type tokenType int
func (t tokenType) String() string {
switch t {
case tokenEOF:
return "tokenEOF"
case tokenComment:
return "tokenComment"
case tokenString:
return "tokenString"
case tokenLeftBrace:
return "tokenLeftBrace"
case tokenRightBrace:
return "tokenRightBrace"
case tokenComma:
return "tokenComma"
}
return "token unkown"
}
type scanner struct {
scan.Scanner
}
func (s *scanner) Scan() bool {
for s.Scanner.Scan() {
if s.Token().ID != tokenSpace {
return true
}
}
return false
}
func newScanner(r io.Reader) *scanner {
var (
char = scan.Char
pat = scan.Pat
merge = scan.Merge
or = scan.Or
con = scan.Con
nonctrl = char(`[:cntrl:]`).Negate()
indent = char(`\t `)
lineBreak = char(`\n\r`)
space = merge(indent, lineBreak)
any = merge(nonctrl, space)
inline = any.Exclude(lineBreak)
delim = char(`,{}`)
empty = pat(``)
//invalid = any.Negate()
newline = or(lineBreak, pat(`\r\n`))
inlineComment = con(pat(`//`), inline.ZeroOrMore(), or(newline, empty))
quoted = or(inline.Exclude(char(`"`)), pat(`\\"`))
quotedString = con(pat(`"`), quoted.ZeroOrMore(), pat(`"`))
unquoted = any.Exclude(delim, space)
unquotedString = unquoted.OneOrMore()
generalString = or(quotedString, unquotedString)
matcher = scan.NewMatcher(
inlineComment,
char(`{`),
char(`}`),
char(`,`),
generalString,
space.OneOrMore(),
)
)
s := scan.Scanner{Matcher: matcher}
err := s.SetReader(r)
if err != nil {
panic(err)
}
return &scanner{s}
}