File size: 8,014 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
var openParentheses = '('.charCodeAt(0)
var closeParentheses = ')'.charCodeAt(0)
var singleQuote = "'".charCodeAt(0)
var doubleQuote = '"'.charCodeAt(0)
var backslash = '\\'.charCodeAt(0)
var slash = '/'.charCodeAt(0)
var comma = ','.charCodeAt(0)
var colon = ':'.charCodeAt(0)
var star = '*'.charCodeAt(0)
var uLower = 'u'.charCodeAt(0)
var uUpper = 'U'.charCodeAt(0)
var plus = '+'.charCodeAt(0)
var isUnicodeRange = /^[a-f0-9?-]+$/i

module.exports = function (input) {
  var tokens = []
  var value = input

  var next, quote, prev, token, escape, escapePos, whitespacePos, parenthesesOpenPos
  var pos = 0
  var code = value.charCodeAt(pos)
  var max = value.length
  var stack = [{ nodes: tokens }]
  var balanced = 0
  var parent

  var name = ''
  var before = ''
  var after = ''

  while (pos < max) {
    // Whitespaces
    if (code <= 32) {
      next = pos
      do {
        next += 1
        code = value.charCodeAt(next)
      } while (code <= 32)
      token = value.slice(pos, next)

      prev = tokens[tokens.length - 1]
      if (code === closeParentheses && balanced) {
        after = token
      } else if (prev && prev.type === 'div') {
        prev.after = token
        prev.sourceEndIndex += token.length
      } else if (
        code === comma ||
        code === colon ||
        (code === slash &&
          value.charCodeAt(next + 1) !== star &&
          (!parent || (parent && parent.type === 'function' && false)))
      ) {
        before = token
      } else {
        tokens.push({
          type: 'space',
          sourceIndex: pos,
          sourceEndIndex: next,
          value: token,
        })
      }

      pos = next

      // Quotes
    } else if (code === singleQuote || code === doubleQuote) {
      next = pos
      quote = code === singleQuote ? "'" : '"'
      token = {
        type: 'string',
        sourceIndex: pos,
        quote: quote,
      }
      do {
        escape = false
        next = value.indexOf(quote, next + 1)
        if (~next) {
          escapePos = next
          while (value.charCodeAt(escapePos - 1) === backslash) {
            escapePos -= 1
            escape = !escape
          }
        } else {
          value += quote
          next = value.length - 1
          token.unclosed = true
        }
      } while (escape)
      token.value = value.slice(pos + 1, next)
      token.sourceEndIndex = token.unclosed ? next : next + 1
      tokens.push(token)
      pos = next + 1
      code = value.charCodeAt(pos)

      // Comments
    } else if (code === slash && value.charCodeAt(pos + 1) === star) {
      next = value.indexOf('*/', pos)

      token = {
        type: 'comment',
        sourceIndex: pos,
        sourceEndIndex: next + 2,
      }

      if (next === -1) {
        token.unclosed = true
        next = value.length
        token.sourceEndIndex = next
      }

      token.value = value.slice(pos + 2, next)
      tokens.push(token)

      pos = next + 2
      code = value.charCodeAt(pos)

      // Operation within calc
    } else if ((code === slash || code === star) && parent && parent.type === 'function' && true) {
      token = value[pos]
      tokens.push({
        type: 'word',
        sourceIndex: pos - before.length,
        sourceEndIndex: pos + token.length,
        value: token,
      })
      pos += 1
      code = value.charCodeAt(pos)

      // Dividers
    } else if (code === slash || code === comma || code === colon) {
      token = value[pos]

      tokens.push({
        type: 'div',
        sourceIndex: pos - before.length,
        sourceEndIndex: pos + token.length,
        value: token,
        before: before,
        after: '',
      })
      before = ''

      pos += 1
      code = value.charCodeAt(pos)

      // Open parentheses
    } else if (openParentheses === code) {
      // Whitespaces after open parentheses
      next = pos
      do {
        next += 1
        code = value.charCodeAt(next)
      } while (code <= 32)
      parenthesesOpenPos = pos
      token = {
        type: 'function',
        sourceIndex: pos - name.length,
        value: name,
        before: value.slice(parenthesesOpenPos + 1, next),
      }
      pos = next

      if (name === 'url' && code !== singleQuote && code !== doubleQuote) {
        next -= 1
        do {
          escape = false
          next = value.indexOf(')', next + 1)
          if (~next) {
            escapePos = next
            while (value.charCodeAt(escapePos - 1) === backslash) {
              escapePos -= 1
              escape = !escape
            }
          } else {
            value += ')'
            next = value.length - 1
            token.unclosed = true
          }
        } while (escape)
        // Whitespaces before closed
        whitespacePos = next
        do {
          whitespacePos -= 1
          code = value.charCodeAt(whitespacePos)
        } while (code <= 32)
        if (parenthesesOpenPos < whitespacePos) {
          if (pos !== whitespacePos + 1) {
            token.nodes = [
              {
                type: 'word',
                sourceIndex: pos,
                sourceEndIndex: whitespacePos + 1,
                value: value.slice(pos, whitespacePos + 1),
              },
            ]
          } else {
            token.nodes = []
          }
          if (token.unclosed && whitespacePos + 1 !== next) {
            token.after = ''
            token.nodes.push({
              type: 'space',
              sourceIndex: whitespacePos + 1,
              sourceEndIndex: next,
              value: value.slice(whitespacePos + 1, next),
            })
          } else {
            token.after = value.slice(whitespacePos + 1, next)
            token.sourceEndIndex = next
          }
        } else {
          token.after = ''
          token.nodes = []
        }
        pos = next + 1
        token.sourceEndIndex = token.unclosed ? next : pos
        code = value.charCodeAt(pos)
        tokens.push(token)
      } else {
        balanced += 1
        token.after = ''
        token.sourceEndIndex = pos + 1
        tokens.push(token)
        stack.push(token)
        tokens = token.nodes = []
        parent = token
      }
      name = ''

      // Close parentheses
    } else if (closeParentheses === code && balanced) {
      pos += 1
      code = value.charCodeAt(pos)

      parent.after = after
      parent.sourceEndIndex += after.length
      after = ''
      balanced -= 1
      stack[stack.length - 1].sourceEndIndex = pos
      stack.pop()
      parent = stack[balanced]
      tokens = parent.nodes

      // Words
    } else {
      next = pos
      do {
        if (code === backslash) {
          next += 1
        }
        next += 1
        code = value.charCodeAt(next)
      } while (
        next < max &&
        !(
          code <= 32 ||
          code === singleQuote ||
          code === doubleQuote ||
          code === comma ||
          code === colon ||
          code === slash ||
          code === openParentheses ||
          (code === star && parent && parent.type === 'function' && true) ||
          (code === slash && parent.type === 'function' && true) ||
          (code === closeParentheses && balanced)
        )
      )
      token = value.slice(pos, next)

      if (openParentheses === code) {
        name = token
      } else if (
        (uLower === token.charCodeAt(0) || uUpper === token.charCodeAt(0)) &&
        plus === token.charCodeAt(1) &&
        isUnicodeRange.test(token.slice(2))
      ) {
        tokens.push({
          type: 'unicode-range',
          sourceIndex: pos,
          sourceEndIndex: next,
          value: token,
        })
      } else {
        tokens.push({
          type: 'word',
          sourceIndex: pos,
          sourceEndIndex: next,
          value: token,
        })
      }

      pos = next
    }
  }

  for (pos = stack.length - 1; pos; pos -= 1) {
    stack[pos].unclosed = true
    stack[pos].sourceEndIndex = value.length
  }

  return stack[0].nodes
}