/
core.js
427 lines (374 loc) · 14.5 KB
/
core.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// This file contains the lowest level of objects in parsinator.js. The code here handles keeping track of the current
// location being parsed, parser state, the resulting values of parsing, chain continuations, and
// debug information gathering. The exports in here are documented in docs/core.md.
const proto = require("proto")
const {isNonFunctionParser, getPossibleParser, name} = require('./basicParsers')
const internal = {} // Indicates internal to prevent misuse.
// The primary class for parsinator.js.
const Parser = exports.Parser = proto(function() {
this.init = function() {
;[this.name,
this.action, // A function that returns a ParseResult.
this.chainContinuations, // A list of continuations registered by `chain`. This is intended to only be used internally.
] = arguments
if(!(this.action instanceof Function)) throw new Error("No action passed to Parser constructor")
if(this.chainContinuations === undefined) this.chainContinuations = []
this.shouldDebug = false
this.hideFromDebugRecord = false
}
this.parse = function(
// arguments[0] - One of:
// * input // If the argument is a string, it will parse that string from the beginning.
// * context // If the argument is a Context, it will parse context.input from context.index. This
// is intended only for internal use.
// arguments[1] - internal: will be set for internal calls of parse. This is so debug exception propagation can
// work properly without allowing outside code to do unexpected things that could cause an exception
// to be thrown in the middle of parsing without being recorded properly in the debug log.
) {
if(typeof(arguments[0]) !== 'string' && !(arguments[0] instanceof Context)) {
throw new Error("Argument passed to parse is neither a string nor a Context object.")
}
const isInternal = arguments[1] === internal
if(arguments[0] instanceof Context && !isInternal) {
throw new Error("If you're calling Parser.parse inside another parser, please use" +
" this.parse(parser, context) instead for debuggability.")
}
let context
if(arguments[0] instanceof Context) {
context = arguments[0]
} else {
const input = arguments[0]
context = Context(input, /*index */ 0, new Map(), InputInfoCache(input))
if(this.shouldDebug) {
context.initDebug()
}
}
let name = this.name
let chainContinuations = this.chainContinuations
const isChain = this.chainContinuations.length !== 0
if(isChain) {
name = "chain"
chainContinuations = [() => {
const chainedParser = Parser(this.name, this.action)
chainedParser.shouldDebug = this.shouldDebug
chainedParser.hideFromDebugRecord = this.hideFromDebugRecord
return chainedParser
}].concat(chainContinuations)
}
if(context.debug) context.addDebugParseInit(name, context.index, context._state, this.hideFromDebugRecord)
return maybeTryCatch(this.name, context, isInternal, () => {
if(isChain) {
var result = runContinuations(context, chainContinuations)
} else {
var result = this.action.apply(context)
}
if(context.debug) context.addDebugResult(result)
return result
})
}
// Runs an action that should return a ParseResult and catches and propagates errors properly.
function maybeTryCatch(name, context, isInternal, action) {
try {
return action()
} catch(e) {
if(context.debug) {
// The idea here is that any exception should be caught, recorded in the debug
// record, and re-thrown to propagate it up and then returned as a top-level error result.
// An uncaught exception is not a normal parse failure and it basically should be
// treated like a critical failure.
if(e instanceof InternalError) {
var result = e.result
e = e.result.error
} else {
e._parsinatorName = name
var result = ParseResult(false, context, undefined, ['no error'], e)
e.result = result
}
context.addDebugResult(result)
if(isInternal) {
throw InternalError(result)
} else {
throw e
}
} else {
if (!e._parsinatorName) {
e._parsinatorName = name
e.message = "In '"+e._parsinatorName+"', "+e.message
}
throw e
}
}
}
// Runs a set of continuations in the form passed to `chain`.
// Returns the result of the chain.
// As far as the debug record is concerned, this records all chained continuations as subRecord siblings
// (under the 'chain' record set up in the `parse` code that calls this.
function runContinuations(context, chainContinuations) {
let prevResult = {ok:true, context: context} // Set up fake previous result.
for(let n=0; n<chainContinuations.length; n++) {
if(prevResult.ok) {
const continuation = chainContinuations[n]
if (isNonFunctionParser(continuation)) {
var parser = getPossibleParser(continuation)
} else {
var parser = getPossibleParser(continuation.call(prevResult.context, prevResult.value))
}
if(!(parser instanceof Parser)) {
throw new Error("Function passed to `chain` did not return a parser. The function is: "+continuation.toString())
}
prevResult = context.parse(parser, prevResult.context)
} else {
break
}
}
return prevResult
}
this.join = function() {
return this.value(list => joinInternal(list))
function joinInternal(list) {
if (typeof(list) === 'string') {
return list
}
if(!(list instanceof Array)) {
throw new Error("used `join` on a parser result that isn't only a nested array of strings: "+JSON.stringify(list))
}
var s = []
for (const item of list) {
if(typeof(item) === 'string') {
s.push(item)
} else {
s.push(joinInternal(item))
}
}
return s.join('')
}
}
this.chain = function(continuation) {
// The name of this parser should be the same as the parser chain is being called on, because that will be the name
// reported for the first leg of the chain.
const chainedParser = Parser(this.name, this.action, this.chainContinuations.concat(continuation))
chainedParser.shouldDebug = this.shouldDebug
chainedParser.hideFromDebugRecord = this.hideFromDebugRecord
return chainedParser
}
this.isolate = function(stateMapper) {
const parser = this
return hideFromDebug(Parser(this.name, function() {
const result = this.parse(parser, this.copy())
const oldState = result.context._state
const newState = new Map(this._state)
if (stateMapper) stateMapper(oldState, newState)
result.context._state = newState
return result
}))
}
this.isolateFromDebugRecord = function() {
const parser = this
return hideFromDebug(Parser(this.name, function() {
const isolateFromDebugRecord = this.isolateFromDebugRecord
this.isolateFromDebugRecord = true
return this.parse(parser.value(function(v) {
if (!isolateFromDebugRecord) {
delete this.isolateFromDebugRecord
}
return v
}), this)
}))
}
this.deisolateFromDebugRecord = function() {
const parser = this
return hideFromDebug(Parser(this.name, function() {
const isolateFromDebugRecord = this.isolateFromDebugRecord
this.isolateFromDebugRecord = false
return this.parse(parser.value(function(v) {
if (isolateFromDebugRecord) {
this.isolateFromDebugRecord = true
}
return v
}), this)
}))
}
this.value = function(valueMapper) {
const parser = this
return hideFromDebug(Parser(this.name, function() {
const result = this.parse(parser, this)
if(result.ok) {
result.value = valueMapper.call(result.context, result.value)
}
return result
}))
}
this.map = function(mapper) {
return hideFromDebug(name(this.name, this.value(function(values) {
const context = this
return values.map(function() {
return mapper.apply(context, arguments)
})
})))
}
this.debug = function(shouldDebug) {
if(shouldDebug === undefined) shouldDebug = true
this.shouldDebug = shouldDebug
return this
}
})
const ParseResult = proto(function() {
this.init = function() {
;[this.ok, // Either true or false.
this.context, // The Context that the parser parsed to. When this.ok is false, this indicates the furthest
// it was able to parse.
this.value, // The success value returned by the parser, if this.ok is true.
this.expected, // A Set of expected parser matches if this.ok is false.
this.error, // An exception, if one happened while running in debug mode. This should only possibly exist
// if this.ok is false.
] = arguments
// this.debugRecord // Can be set in debug mode.
}
this.copy = function() {
return ParseResult(this.ok, this.context, this.value, this.expected, this.error)
}
})
const Context = proto(function() {
this.init = function() {
;[this.input, // The string source being parsed.
this.index, // The current index the parser is at in the input.
this._state, // A Map of named values set by parsers.
this.inputInfoCache, // An InputInfoCache
this.debug, // If true, the context creates debug records.
this.isolateFromDebugRecord // If set to true, all inner parsers will be hidden from the record.
] = arguments
}
this.move = function(index) {
if(index < this.index) {
throw new Error("Parser attempted to move an index backward to index: "+index+" from index"+this.index+".")
}
const newContext = Context(this.input, index, new Map(this._state), this.inputInfoCache, this.debug, this.isolateFromDebugRecord)
if(this.debug) {
newContext.debugRecord = this.debugRecord
}
return newContext
}
this.copy = function() {
return this.move(this.index)
}
this.ok = function(
index, // The new 'next' index. The parser succeeded up to this index.
value // The resulting value to return from the parser.
) {
return ParseResult(true, this.move(index), value)
}
this.fail = function(
index, // The farthest index it was able to successfully parse to. The parser
// succeeded up through the previous index.
expected // A list (Array or Set) of strings where each string is a failed expectation to be
// displayed to the user in a list like "Expected to find ..., ..., and ... in the input".
) {
if(!(expected instanceof Set)) {
if(!(expected instanceof Array)) expected = [expected]
expected = new Set(expected)
}
return ParseResult(false, this.move(index), undefined, expected)
}
// Gets a state value.
this.get = function(key) {
return this._state.get(key)
}
// Sets a state value.
this.set = function(key, value) {
this._state.set(key, value)
}
this.parse = function(parser, curContext) {
if(!curContext) throw new Error("Context.parse not passed a curContext.")
parser = getPossibleParser(parser)
if(this === curContext) {
curContext = this.copy()
}
if(curContext.debug) {
this.createNewDebugSubrecord(curContext)
}
return parser.parse(curContext, internal)
}
// == Debug-related methods ==
// Initializes a new debug record.
this.initDebug = function() {
const newRecord = {}
this.debugRecord = newRecord
this.curDebugSection = newRecord
this.debug = true
}
// Creates a new debug subrecord in this Context, the target of the subrecord being owned by the passed Context.
// Note that this expects curContext to not already have a debugRecord or curDebugSubsection.
this.createNewDebugSubrecord = function(curContext) {
if(this.curDebugSection.subRecords === undefined) {
this.curDebugSection.subRecords = []
}
curContext.debugRecord = this.debugRecord
curContext.curDebugSection = {}
this.curDebugSection.subRecords.push(curContext.curDebugSection)
}
// Adds debug info available before parsing has started.
this.addDebugParseInit = function(name, startIndex, startState, hideFromDebugRecord) {
this.curDebugSection.name = name
this.curDebugSection.startIndex = startIndex
this.curDebugSection.startState = new Map(startState)
if (hideFromDebugRecord || this.isolateFromDebugRecord) {
this.curDebugSection.hideFromDebugRecord = true
}
}
// Adds the result to the debug record.
this.addDebugResult = function(result) {
// Copy this so any state changes don't mutate this record.
this.curDebugSection.result = Object.assign({}, result)
this.curDebugSection.result.context = result.context.copy()
}
})
// An error that holds a ParseResult.
const InternalError = exports.InternalError = proto(Error, function() {
this.init = function(result) {
this.result = result
}
})
const hideFromDebug = exports.hideFromDebug = function(parser) {
parser.hideFromDebugRecord = true
return parser
}
const InputInfoCache = exports.InputInfoCache = proto(function LineCache() {
this.init = function(input) {
this.input = input
this.initialized = false
}
this.initialize = function() {
if (!this.initialized) {
// A map of 1-based line to the first character index in that line.
this.lineCache = {1: 0}
let line = 1
for (let index=0; index<this.input.length; index++) {
const char = this.input[index]
if (char === '\n') {
line++
this.lineCache[line] = index+1
}
}
this.lines = line
this.initialized = true
}
}
// Returns the start index at line.
this.getLineIndex = function(line) {
return this.lineCache[line]
}
this.get = function(index) {
if(index < 0 || this.input.length < index) {
throw new Error("Asking for info about an index not contained in the target string: "+index+'.')
}
let lastLine
for (let line=1; line<=this.lines; line++) {
lastLine = line
const startIndex = this.lineCache[line]
if (startIndex > index) {
return {line: line-1, column: 1 + index - this.lineCache[line-1]}
}
}
return {line: lastLine, column: 1 + index - this.lineCache[lastLine]}
}
})