-
Notifications
You must be signed in to change notification settings - Fork 43
/
parser-debug.js
60 lines (59 loc) · 1.94 KB
/
parser-debug.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
'use strict'
const Parser = require('./parser.js')
const util = require('util')
const dump = _ => util.inspect(_, {colors: true, depth: 10, breakLength: Infinity})
class DebugParser extends Parser {
stateName (state) {
// istanbul ignore next
return (state.parser && state.parser.name) || state.name || ('anonymous')
}
runOne () {
const callStack = this.stack.concat(this.state).map(_ => this.stateName(_)).join(' <- ')
console.log('RUN', callStack, dump({line: this.line, col: this.col, char: this.char, ret: this.state.returned}))
return super.runOne()
}
finish () {
const obj = super.finish()
// istanbul ignore if
if (this.stack.length !== 0) {
throw new Parser.Error('All states did not return by end of stream')
}
return obj
}
callStack () {
const callStack = this.stack.map(_ => this.stateName(_)).join(' ').replace(/\S/g, ' ')
return callStack ? callStack + ' ' : ''
}
next (fn) {
console.log(' ', this.callStack(), 'NEXT', this.stateName(fn))
return super.next(fn)
}
goto (fn) {
console.log(' ', this.callStack(), 'GOTO', this.stateName(fn))
super.next(fn)
return false
}
call (fn, returnWith) {
console.log(' ', this.callStack(), 'CALL', fn.name, returnWith ? '-> ' + returnWith.name : '')
if (returnWith) super.next(returnWith)
this.stack.push(this.state)
this.state = {parser: fn, buf: '', returned: null}
}
callNow (fn, returnWith) {
console.log(' ', this.callStack(), 'CALLNOW', fn.name, returnWith ? '-> ' + returnWith.name : '')
if (returnWith) super.next(returnWith)
this.stack.push(this.state)
this.state = {parser: fn, buf: '', returned: null}
return false
}
return (value) {
console.log(' ', this.callStack(), 'RETURN')
return super.return(value)
}
returnNow (value) {
console.log(' ', this.callStack(), 'RETURNNOW')
super.return(value)
return false
}
}
module.exports = DebugParser