-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcypher.js
197 lines (174 loc) · 5.24 KB
/
cypher.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
export function parse(text, log=()=>{}) {
const r = {}, x = {} // rules defined and traced
let left = '', right = text // text parsed and pending
let branch = [] // abstract syntax tree in progress
const tree = [branch]
// Non-Terminal Symbols
r.match = () => x.sp() && x.term('match') && x.node() && x.chain() && x.eot()
r.node = () => x.ch('(') && x.elem() && x.ch(')')
r.chain = () => any(() => x.rel() && x.node() && x.chain())
r.rel = () => one(() => x.in(), () => x.out(), () => x.both())
r.in = () => x.term('<-') && opt(() => x.ch('[') && x.elem() && x.ch(']')) && x.ch('-')
r.out = () => x.ch('-') && opt(() => x.ch('[') && x.elem() && x.ch(']')) && x.term('->')
r.both = () => x.ch('-') && opt(() => x.ch('[') && x.elem() && x.ch(']')) && x.ch('-')
r.elem = () => opt(() => x.bind()) && opt(() => x.ch(':') && x.type()) && opt(() => x.prop())
r.prop = () => x.ch('{') && x.word() && x.ch(':') && x.expr() && x.ch('}')
r.expr = () => x.strn()
r.bind = () => x.word()
r.type = () => x.word()
// Terminal Symbols
r.word = () => match(/^[A-Za-z][A-Za-z0-9_]*/) && x.sp()
r.term = want => right.startsWith(want) && accept(want.length) && keep(want) && x.sp()
r.strn = () => x.ch('"') && match(/^[^"]{0,20}/) && x.ch('"') && x.sp()
r.ch = char => right.startsWith(char) && accept(1) && keep(char) && x.sp()
r.sp = () => match(/^\s*/)
r.eot = () => !right.length
// Parse Instrumentation
for (const op in r) {
x[op] = (...args) => {
log(`${left}%c${op}%c${right}`,"color:red","color:black");
const here = branch
branch = [op]
const success = r[op](...args)
if (success) here.push(branch)
branch = here
return success
}
}
// Parser State Updates
function match(regex) {
const m = right.match(regex)
m && accept(m[0].length) && keep(m[0])
return !!m
}
function keep(text) {
branch.push(text)
return true
}
function accept(n) {
left += right.substring(0,n)
right = right.substring(n)
return true
}
// Backtracking Operators
function any (rule) { // rule zero or more times
const save = [left,right]
if (rule()) {
any(rule)
} else {
[left,right] = save
}
return true
}
function opt (rule) { // rule zero or one times
const save = [left,right]
if (!rule()) {
[left,right] = save
}
return true
}
function one (...rules) { // first rule of many
const save = [left,right]
for (const rule of rules) {
if(rule()) {return true}
[left,right] = save
}
false
}
x.match()
return tree
}
export function gen(level, tree, code, log=()=>{}) {
const tab = () => ' |'.repeat(level)
switch (tree[0]) {
case 'sp':
case 'ch':
case 'term':
break
case 'bind':
case 'type':
log(tab(), tree[0], `"${tree[1][1]}"`)
code[tree[0]] = tree[1][1]
break
case 'prop':
log(tab(), tree[0], `"${tree[2][1]}"`, tree[4][1][2])
code[tree[0]] = [tree[2][1], tree[4][1][2]]
break
case 'node':
case 'rel':
case 'chain':
log(tab(), tree[0])
{const sub = {}
code[tree[0]] = sub
for (const branch of tree.slice(1)) gen(level+1,branch,sub,log)}
break
case 'in':
case 'out':
case 'both':
log(tab(), tree[0])
code['dir'] = tree[0]
for (const branch of tree.slice(1)) gen(level+1,branch,code,log)
break
case 'match':
case 'elem':
log(tab(), tree[0])
for (const branch of tree.slice(1)) gen(level+1,branch,code,log)
break
case 'eot':
log(tab(), 'end')
break
default:
log(tab(), 'unknown', tree[0])
}
return code
}
export function check(tally, code, errors) {
if(code?.node?.type && errors) {
if(!tally.nodes[code.node.type]) {
errors.push(`No node of type "${code.node.type}" in the graph.`)
}
}
if(code?.rel?.type && errors) {
if(!tally.rels[code.rel.type]) {
errors.push(`No relation of type "${code.rel.type}" in the graph.`)
}
}
if(Object.keys(code.chain).length) {
check(tally,code.chain, errors)
}
}
export function apply(graph, code) {
const nodes = graph.nodes
const rels = graph.rels
const results = []
for (const node of nodes) {
chain(node, code, {})
}
return results
function chain(node, code, maybe) {
if ((!code.node.type || node.type == code.node.type) &&
(!code.node.prop || node.props[code.node.prop[0]] == code.node.prop[1])) {
if (code.node.bind)
maybe[code.node.bind] = node
if (code.chain.rel) {
if (['in','both'].includes(code.chain.rel.dir))
links(node.in, 'from')
if (['out','both'].includes(code.chain.rel.dir))
links(node.out, 'to')
} else {
results.push(maybe)
}
}
function links(rids, dir) {
rids.forEach(rid => {
if ((!code.chain.rel.type || rels[rid].type == code.chain.rel.type) &&
(!code.chain.rel.prop || rels[rid].props[code.chain.rel.prop[0]] == code.chain.rel.prop[1])) {
maybe = {...maybe}
if (code.chain.rel.bind)
maybe[code.chain.rel.bind] = rels[rid]
chain(nodes[rels[rid][dir]], code.chain, maybe)
}
})
}
}
}