-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgockl.go
159 lines (135 loc) · 3.17 KB
/
gockl.go
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package gockl
import (
"io"
"strings"
)
var spaceChars = " \t\r\n"
type Tokenizer struct {
Input string
Position int
}
func New(input string) *Tokenizer {
return &Tokenizer{Input: input}
}
func (me *Tokenizer) shift(end string) string {
if pos := strings.Index(me.Input[me.Position:], end); pos > -1 {
r := me.Input[me.Position : me.Position+pos+len(end)]
me.Position += pos + len(end)
return r
}
return me.shiftUntil('<')
}
func (me *Tokenizer) shiftToTagEnd() string {
type state uint8
const (
tagname state = iota // we're in the name of the tag still
tagcontent // past the name, looking for attributes or the end of the tag
attribname // ok, started the attribute name
attribvalue // started an attribute value
doublequote // inside a double quoted attribute value
singlequote // inside a single quoted attribute value
)
var s state = tagname
pos := me.Position
len := len(me.Input)
for i := pos + 1; i < len; i++ {
curr := me.Input[i]
if s == doublequote {
if curr == '"' {
s = tagname
}
continue
} else if s == singlequote {
if curr == '\'' {
s = tagname
}
continue
}
// tagname, tagcontent, attribname, attribvalue here
switch curr {
case ' ', '\t', '\r', '\n':
if s == tagname {
s = tagcontent
} else if s == attribvalue {
s = tagcontent
}
case '=':
if s == attribname {
s = attribvalue
}
case '<':
me.Position = i
return me.Input[pos:i]
case '>':
me.Position = i + 1
return me.Input[pos : i+1]
case '"':
if s == attribvalue {
s = doublequote
}
case '\'':
if s == attribvalue {
s = singlequote
}
default:
if s == tagcontent {
s = attribname
}
}
}
// eof
me.Position = len
return me.Input[pos:]
}
func (me *Tokenizer) shiftUntil(next rune) string {
if me.Position < len(me.Input) {
if pos := strings.IndexRune(me.Input[me.Position+1:], next); pos > -1 {
r := me.Input[me.Position : me.Position+pos+1]
me.Position += pos + 1
return r
}
}
r := me.Input[me.Position:]
me.Position = len(me.Input)
return r
}
func (me *Tokenizer) has(next string) bool {
return me.Position+len(next) <= len(me.Input) && me.Input[me.Position:me.Position+len(next)] == next
}
func (me *Tokenizer) Next() (Token, error) {
if me.Position >= len(me.Input) {
return nil, io.EOF
}
if me.Position >= len(me.Input)-3 {
goto dunno
}
switch me.Input[me.Position] {
case '<':
switch me.Input[me.Position+1] {
case '?':
return ProcInstToken(me.shift("?>")), nil
case '!':
if me.has("<!--") {
return CommentToken(me.shift("-->")), nil
}
if me.has("<![CDATA[") {
return CDATAToken(me.shift("]]>")), nil
}
r := me.shift(">")
if strings.HasPrefix(r, "<!DOCTYPE") && strings.Contains(r, "[") {
r += me.shift("]") + me.shift(">")
}
return DirectiveToken(r), nil
case '/':
return EndElementToken(me.shift(">")), nil
default:
raw := me.shiftToTagEnd()
if len(raw) >= 3 && raw[len(raw)-2] == '/' {
return EmptyElementToken(raw), nil
}
return StartElementToken(raw), nil
}
}
dunno:
return TextToken(me.shiftUntil('<')), nil
}