-
Notifications
You must be signed in to change notification settings - Fork 76
/
ident.go
221 lines (191 loc) · 6.65 KB
/
ident.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* Copyright 2019 The Bazel Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package parser
import (
"fmt"
"strings"
)
type IdentLike interface {
Token
}
type BadURL struct {
Value string
}
func (u *BadURL) String() string {
return fmt.Sprintf("<BadURL %q>", u.Value)
}
func (_ *BadURL) Type() string { return "BadURL" }
var _ Token = &BadURL{}
type URL struct {
Value string
}
func (u *URL) String() string {
return fmt.Sprintf("<URL %q>", u.Value)
}
func (_ *URL) Type() string { return "URL" }
var _ Token = &URL{}
// Ident is an ident token.
type Ident struct {
// The text inside of the comment.
Value string
}
func (i *Ident) String() string {
return fmt.Sprintf("<Ident %q>", i.Value)
}
func (_ *Ident) Type() string { return "Ident" }
var _ Token = &Ident{}
// scanIdent consumes the current rune and all contiguous ident runes.
func (s *Scanner) scanIdent() IdentLike {
// This section describes how to consume an ident-like token from a stream of code points. It returns an <ident-token>, <function-token>, <url-token>, or <bad-url-token>.
// Consume a name, and let string be the result.
str := s.consumeAName()
// If string’s value is an ASCII case-insensitive match for "url"
if strings.ToLower(str) == "url" {
// and the next input code point is U+0028 LEFT PARENTHESIS ((), consume
// it.
if ch := s.read(); ch != '(' {
s.unread()
} else {
// While the next two input code points are whitespace, consume the
// next input code point.
for i := 0; i < 2; i++ {
if ch := s.peek(); isWhitespace(ch) {
s.read()
}
}
// If the next one or two input code points are
// U+0022 QUOTATION MARK ("), U+0027 APOSTROPHE ('), or whitespace followed
// by U+0022 QUOTATION MARK (") or U+0027 APOSTROPHE ('), then create a
// <function-token> with its value set to string and return it.
if ch := s.peek(); ch == '"' || ch == '\'' {
return &Function{Value: str}
} else if isWhitespace(ch) {
s.read()
if ch := s.peek(); ch == '"' || ch == '\'' {
return &Function{Value: str}
}
}
// Otherwise, consume a url token, and return it.
return s.scanURL()
}
} else {
// Otherwise, if the next input code point is U+0028 LEFT PARENTHESIS ((),
// consume it. Create a <function-token> with its value set to string and
// return it.
if ch := s.read(); ch != '(' {
s.unread()
} else {
return &Function{Value: str}
}
}
// Otherwise, create an <ident-token> with its value set to string and return it.
return &Ident{Value: str}
}
func (s *Scanner) scanURL() Token {
// Note: This algorithm assumes that the initial "url(" has already been
// consumed. This algorithm also assumes that it’s being called to consume an
// "unquoted" value, like url(foo). A quoted value, like url("foo"), is
// parsed as a <function-token>. Consume an ident-like token automatically
// handles this distinction; this algorithm shouldn’t be called directly
// otherwise.
// Initially create a <url-token> with its value set to the empty string.
buf := strings.Builder{}
// Consume as much whitespace as possible.
for ch := s.peek(); isWhitespace(ch); ch = s.peek() {
s.read()
}
/* test code to print out all the runes that haven't been processed DO NOT SUBMIt
for {
if ch := s.read(); ch == eof {
return &URL{Value: buf.String()}
} else {
buf.WriteRune(ch)
}
}
/**/
// Repeatedly consume the next input code point from the stream:
for {
if ch := s.read(); ch == ')' {
// U+0029 RIGHT PARENTHESIS ())
// Return the <url-token>.
return &URL{Value: buf.String()}
} else if ch == eof {
// EOF
// This is a parse error. Return the <url-token>.
return &URL{Value: buf.String()}
} else if isWhitespace(ch) {
// whitespace
// Consume as much whitespace as possible.
for ch := s.peek(); isWhitespace(ch); ch = s.peek() {
s.read()
}
// If the next input code point is U+0029 RIGHT PARENTHESIS ()) or EOF, consume it and return the <url-token>
if ch := s.read(); ch == ')' {
return &URL{Value: buf.String()}
} else if ch == eof {
// (if EOF was encountered, this is a parse error);
// We choose to return a BadURL here.
return &BadURL{Value: "firstone"}
//return &BadURL{Value: s.scanBadURL()}
} else {
// otherwise, consume the remnants of a bad url, create a <bad-url-token>, and return it.
return &BadURL{Value: "otherone"}
//return &BadURL{Value: s.scanBadURL()}
}
} else if ch == '"' || ch == '\'' || ch == '(' || isNonASCIICodePoint(ch) {
// U+0022 QUOTATION MARK (")
// U+0027 APOSTROPHE (')
// U+0028 LEFT PARENTHESIS (()
// non-printable code point
// This is a parse error. Consume the remnants of a bad url, create a <bad-url-token>, and return it.
return &BadURL{Value: "thisone"}
//return &BadURL{Value: s.scanBadURL()}
} else if ch == '\\' {
// U+005C REVERSE SOLIDUS (\)
// If the stream starts with a valid escape, consume an escaped code point and append the returned code point to the <url-token>’s value.
buf.WriteString(s.scanEscape())
} else {
buf.WriteRune(ch)
}
}
// anything else
// Append the current input code point to the <url-token>’s value.
return &URL{Value: ""}
}
func (s *Scanner) scanBadURL() string {
// This section describes how to consume the remnants of a bad url from a
// stream of code points, "cleaning up" after the tokenizer realizes that
// it’s in the middle of a <bad-url-token> rather than a <url-token>. It
// returns nothing; its sole use is to consume enough of the input stream to
// reach a recovery point where normal tokenizing can resume.
buf := strings.Builder{}
// Repeatedly consume the next input code point from the stream:
for {
if ch := s.read(); ch == '(' || ch == eof {
// U+0029 RIGHT PARENTHESIS ())
// EOF
// Return.
return buf.String()
} else if ch == '\\' {
// the input stream starts with a valid escape
// Consume an escaped code point. This allows an escaped right
// parenthesis ("\)") to be encountered without ending the
// <bad-url-token>. This is otherwise identical to the "anything else"
// clause.
return s.scanEscape()
} else {
// anything else
// Do nothing.
buf.WriteRune(ch)
}
}
}