This repository has been archived by the owner on Feb 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.js
66 lines (54 loc) · 1.51 KB
/
repl.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
import fs from 'fs'
import {parser as parserCommonMark} from './parsers/commonmark'
import {NodeInfo} from './common'
function parse(src) {
const tree = parserCommonMark.parse(src)
let hasError = false
const stack = [new NodeInfo()]
tree.iterate({
// from: 0,
// to: tree.length,
enter(...args) {
const node = new NodeInfo(...args)
stack.push(node)
if (node.error) {
hasError = true
}
// console.log('enter', nodeTypeRepr(node.type), start, end)
},
leave() {
const node = stack.pop()
const parent = stack[stack.length - 1]
parent.children.push(node)
// console.log('leave', nodeTypeRepr(node.type), start, end)
},
})
function getExpr(node, level=0) {
const arr = []
const repr = `${node.repr()}: "${src.substring(node.start, node.end).replace(/\n/g, '↵')}"`
const indent = ' '.repeat(level * 2)
if (node.error) {
// id == 0 can be treated as an error?
arr.push(indent + '! [ERROR] ' + repr)
} else {
arr.push(indent + '* ' + repr)
}
// is it terminal?
node.children.forEach(child => {
getExpr(child, level + 1).forEach(token => {
arr.push(token)
})
})
return arr
}
const treeRoot = stack[0].children[0]
getExpr(treeRoot).forEach(ln => console.log(ln))
// returns if the syntax is "correct"
return !hasError
}
// parse('(8 + 9) * 17 + 20 * 20 - ')
function mainLoop() {
const inp = fs.readFileSync('/dev/stdin', 'utf-8')
parse(inp)
}
mainLoop()