-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
create-tokenizer.js
379 lines (305 loc) · 9.14 KB
/
create-tokenizer.js
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
module.exports = createTokenizer
var assert = require('assert')
var debug = require('debug')('micromark')
var assign = require('../constant/assign')
var codes = require('../character/codes')
var markdownLineEnding = require('../character/markdown-line-ending')
var own = require('../constant/has-own-property')
var shallow = require('../util/shallow')
var serializeChunks = require('../util/serialize-chunks')
var sliceChunks = require('../util/slice-chunks')
function createTokenizer(parser, initialize, from) {
var point = from ? assign({}, from) : {line: 1, column: 1, offset: 0}
var columnStart = []
var attachedResolveAlls = []
var chunks = []
var index = 0
var bufferIndex = -1
var consumed = true
// Current open tokens.
var stack = []
// Tools used for tokenizing.
var effects = {
consume: consume,
enter: enter,
exit: exit,
attempt: constructFactory(onsuccessfulconstruct),
check: constructFactory(onsuccessfulcheck),
interrupt: constructFactory(onsuccessfulcheck, {interrupt: true}),
lazy: constructFactory(onsuccessfulcheck, {lazy: true})
}
// State and tools for resolving, serializing.
var context = {
previous: codes.eof,
events: [],
parser: parser,
sliceStream: sliceStream,
sliceSerialize: sliceSerialize,
now: now,
defineSkip: defineSkip,
write: write
}
var state = initialize.tokenize.call(context, effects)
var expectedCode
point._index = index
point._bufferIndex = bufferIndex
return context
function write(value) {
var length
var index
chunks.push(value)
main()
// Exit if we’re not done, resolve might change stuff.
if (value !== codes.eof) {
return []
}
addResult(initialize, 0)
// Otherwise, resolve, and exit.
length = attachedResolveAlls.length
index = -1
while (++index < length) {
context.events = attachedResolveAlls[index](context.events, context)
}
// Add EOF token.
context.events.push(value)
return context.events
}
//
// Tools.
//
function sliceSerialize(token) {
return serializeChunks(sliceChunks(chunks, token))
}
function sliceStream(token) {
return sliceChunks(chunks, token)
}
function now() {
return shallow(point)
}
function defineSkip(value) {
columnStart[value.line] = value.column
// If we’re currently at that point:
if (point.column === 1 && point.line === value.line) {
point.column = value.column
point.offset += value.column - 1
}
}
//
// State management.
//
// Main loop (note that `index` and `bufferIndex` are modified by `consume`).
function main() {
var chunk
var chunkIndex
while (index < chunks.length) {
chunk = chunks[index]
if (typeof chunk === 'string') {
chunkIndex = index
while (index === chunkIndex && bufferIndex < chunk.length) {
if (bufferIndex < 0) {
bufferIndex = 0
}
assert.equal(consumed, true, 'expected character to be consumed')
expectedCode = chunk.charCodeAt(bufferIndex)
debug('main: passing `%s` to %s (buffer)', expectedCode, state.name)
consumed = undefined
state = state(expectedCode)
}
} else {
expectedCode = chunk
consumed = undefined
debug('main: passing `%s` to %s (chunk)', expectedCode, state.name)
state = state(expectedCode)
}
}
}
// Move a character forward.
function consume(code) {
assert.equal(
code,
expectedCode,
'expected given code to equal consumed code'
)
debug('consume: `%s`', code)
assert.equal(consumed, undefined, 'expected code to not have been consumed')
consumed = true
assert(
code === null
? !context.events.length ||
context.events[context.events.length - 1][0] === 'exit'
: context.events[context.events.length - 1][0] === 'enter',
'expected last token to be open'
)
if (markdownLineEnding(code)) {
point.line++
if (point.line in columnStart) {
point.column = columnStart[point.line]
point.offset += columnStart[point.line] - 1
} else {
point.column = 1
}
}
// Anything else.
else if (code !== codes.virtualSpace) {
point.column++
}
// Not in a string chunk.
if (bufferIndex < 0) {
index++
} else {
bufferIndex++
}
// At end of string chunk.
if (bufferIndex > -1 && bufferIndex === chunks[index].length) {
bufferIndex = -1
index++
}
if (code !== codes.virtualSpace) {
point.offset += code === codes.carriageReturnLineFeed ? 2 : 1
}
point._bufferIndex = bufferIndex
point._index = index
context.previous = code
}
// Start a token.
function enter(type) {
var token = {type: type, start: now()}
assert.equal(typeof type, 'string', 'expected string type')
assert.notEqual(type.length, 0, 'expected non-empty string')
debug('enter: `%s`', type)
context.events.push(['enter', token, context])
stack.push(token)
return token
}
// Stop a token.
function exit(type) {
assert.equal(typeof type, 'string', 'expected string type')
assert.notEqual(type.length, 0, 'expected non-empty string')
assert(stack.length, 'cannot close w/o open tokens')
var token = stack.pop()
token.end = now()
assert.equal(type, token.type, 'expected exit token to match current token')
assert(
!(
token.start._index === token.end._index &&
token.start._bufferIndex === token.end._bufferIndex
),
'expected non-empty token (`' + type + '`)'
)
debug('exit: `%s`', token.type)
context.events.push(['exit', token, context])
return token
}
// Use results.
function onsuccessfulconstruct(construct, info) {
addResult(construct, info.from)
}
// Discard results.
function onsuccessfulcheck(construct, info) {
info.restore()
}
function constructFactory(onreturn, props) {
return hook
function hook(hooks, returnState, bogusState) {
var constructs
var multiple
var hookIndex
var currentConstruct
var info
return hooks.tokenize || 'length' in hooks
? handleConstructs(hooks)
: handleHooks
function handleHooks(code) {
if (own.call(hooks, code)) {
return handleConstructs(hooks[code])(code)
}
return bogusState(code)
}
function handleConstructs(list) {
constructs = list
multiple = 'length' in constructs
hookIndex = 0
currentConstruct = multiple ? constructs[hookIndex] : constructs
return handleConstruct(currentConstruct)
}
function handleConstruct(construct) {
return start
function start(code) {
info = store(construct)
if (!construct.partial) {
context.currentConstruct = construct
}
return construct.tokenize.call(
assign({}, context, props),
effects,
ok,
nok
)(code)
}
}
function ok(code) {
assert.equal(code, expectedCode, 'expected code')
consumed = true
onreturn(currentConstruct, info)
return returnState
}
function nok(code) {
assert.equal(code, expectedCode, 'expected code')
consumed = true
info.restore()
if (multiple && ++hookIndex < constructs.length) {
currentConstruct = constructs[hookIndex]
return handleConstruct(currentConstruct)
}
return bogusState
}
}
}
function addResult(construct, from) {
if (
construct.resolveAll &&
attachedResolveAlls.indexOf(construct.resolveAll) < 0
) {
attachedResolveAlls.push(construct.resolveAll)
}
if (construct.resolve) {
context.events = context.events
.slice(0, from)
.concat(construct.resolve(context.events.slice(from), context))
}
if (construct.resolveTo) {
context.events = construct.resolveTo(context.events, context)
}
assert(
context.events.length === 0 ||
context.events[context.events.length - 1][0] === 'exit',
'expected last token to end'
)
}
function store() {
var startPoint = now()
var startIndex = index
var startBufferIndex = bufferIndex
var startPrevious = context.previous
var startCurrentConstruct = context.currentConstruct
var startEventsIndex = context.events.length
// To do: perf of `concat` may be slow?
var startStack = stack.concat()
return {restore: restore, from: startEventsIndex}
function restore() {
point = startPoint
index = startIndex
// Sometimes, we reset to directly after a line ending.
// Make sure to indent.
if (point.line in columnStart && point.column === 1) {
point.column = columnStart[point.line]
point.offset += columnStart[point.line] - 1
}
bufferIndex = startBufferIndex
context.previous = startPrevious
context.currentConstruct = startCurrentConstruct
context.events.length = startEventsIndex
stack = startStack
}
}
}