-
Notifications
You must be signed in to change notification settings - Fork 0
/
Context.js
227 lines (213 loc) · 5.6 KB
/
Context.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
class FSM {
constructor(source) {
this.Contexts = source
}
static include(name) {
return name
}
static item(name, regexp) {
if (typeof regexp === 'string') {
regexp = new RegExp('^(' + regexp + ')')
}
return {
patt: regexp,
token(match) {
const result = { Type: name }
if (match[1]) {
Object.assign(result, { Content: match[1] })
}
return result
}
}
}
static next(name, ...event) {
const result = event.map(regex => {
return {
patt: regex,
pop: true
}
})
result.push(name)
return result
}
// syntax:
// patt: regex
// push: sub-state
// pop: true
// token: callback
tokenize(string, state, epi = true) {
let valid = true, index = 0, last = 0
let result = [], warnings = [], scoping = []
const syntax = this.getContext(state)
if (typeof state !== 'string') state = 'anonymous'
while (string.length > 0) {
let i, pop = false
for (i = 0; i < syntax.length; i++) {
const stx = syntax[i]
const match = string.match(stx.patt)
if (match) {
let content = []
const position = index
index += match[0].length
string = string.slice(match[0].length)
if (stx.push) {
const subtoken = this.tokenize(string, stx.push, epi)
warnings.push(...subtoken.Warnings.map(msg => {
return {
Err: msg.Err,
Src: msg.Src,
Pos: msg.Pos + index
}
}))
scoping.push(
{ start: last, end: index, name: state },
...subtoken.Scoping.map(scope => {
return {
start: scope.start + index,
end: scope.end + index,
name: scope.name
}
})
)
index += subtoken.Index
last = index
string = string.slice(subtoken.Index)
content = subtoken.Content
}
if (stx.pop) pop = true
if (stx.token) {
const tok = stx.token(match, content)
if (stx.locate === true || stx.locate === undefined) {
tok.Pos = position
}
result.push(tok)
}
break
}
}
if (pop) break
if (i === syntax.length) {
if (valid) {
valid = false
warnings.push({
Err: 'Undefined',
Src: '',
Pos: index
})
}
warnings[warnings.length - 1].Src += string.charAt(0)
string = string.slice(1)
index += 1
} else {
valid = true
}
}
scoping.push({ start: last, end: index, name: state })
if (epi) result = FSM.arrange(result)
return {
Index: index,
Content: result,
Scoping: scoping,
Warnings: warnings
}
}
getContext(state) {
let result
if (typeof state === 'string') {
result = this.Contexts[state]
if (result === undefined) throw new Error()
} else {
result = state
}
let i = result.length
while (i--) {
if (typeof result[i] === 'string') {
result.splice(i, 1, ...this.getContext(result[i]))
}
}
return result
}
static isSubtrack(token) {
if (token.Type === 'Note' || token.Type === 'Macrotrack') {
return true
} else if (token.Type === 'Function' && !token.VoidQ) {
return true
} else if (token.Type === 'Subtrack' && token.Content.some(FSM.isSubtrack)) {
return true
} else {
return false
}
}
static arrange(content) {
let prior = FSM.findPrior(content)
while (prior) {
let left = prior.Id, right = prior.Id
if (prior.LID !== undefined) {
left = prior.Id - 1
while (left >= 0 && !FSM.isSubtrack(content[left])) {
left -= 1
}
if (left < 0) {
throw new Error()
} else {
prior.Args[prior.LID] = {
Type: 'Subtrack',
Content: content.slice(left, prior.Id)
}
}
}
if (prior.RID !== undefined) {
right = prior.Id + 1
while (right < content.length && !FSM.isSubtrack(content[right])) {
right += 1
}
if (right === content.length) {
throw new Error()
} else {
prior.Args[prior.RID] = {
Type: 'Subtrack',
Content: content.slice(prior.Id + 1, right + 1)
}
}
}
content.splice(left, right - left + 1, {
Type: 'Function',
Name: prior.Name,
Args: prior.Args,
Pos: prior.Pos,
Alias: prior.Order,
VoidQ: prior.VoidQ
})
prior = FSM.findPrior(content)
}
return content
}
static findPrior(content) {
let index
const priorPrec = content
.filter(tok => tok.Type === '@alias')
.reduce((prec, tok) => Math.min(prec, tok.Prec), FSM.MaxPrec)
if (priorPrec === FSM.MaxPrec) {
return false
} else if (priorPrec % 2 === 0) {
// Left Associative
index = content.findIndex(tok => tok.Prec === priorPrec)
} else {
// Right Associative
index = findLastIndex(content, tok => tok.Prec === priorPrec)
}
return Object.assign(content[index], { Id: index })
}
}
function findLastIndex(array, callback, thisArg) {
let index = array.length
while (index > 0) {
index -= 1
if (callback.call(thisArg, array[index], index, array)) {
return index
}
}
return -1
}
FSM.MaxPrec = 10000
module.exports = FSM