-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsl.js
232 lines (216 loc) · 6.63 KB
/
dsl.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
import spanner from './spanner.js'
import iso8601 from './iso8601.js'
const isNumber = n => n >= '0' && n <= '9'
const isAlpha = n => n >= 'a' && n <= 'z' || n >= 'A' && n <= 'Z'
const isAlphanumeric = n => isNumber(n) || isAlpha(n)
const isDef = n => isAlphanumeric(n) || n == '_'
const isOperation = n => n == '+' || n == '-' || n == '*'
const isEmpty = n => n == ' ' || n == '\t' || n == '\n'
const isSpannerOperation = n => n == '/' || n == '+' || n == '-' || n == '('
const isSpannerChar = n => isAlphanumeric(n) || ['_', '+', '-', '(', ')', '/'].indexOf(n) != -1
export default moment => ({
parse: (s, inschedules, inconstants) => {
const constants = { now: moment.utc() }
const schedules = {}
let i = 0
const printContext = () => {
let back = 0
while (back < 10 && i - back - 1 >= 0 && s[i - back - 1] != '\n')
back++
let forward = 0
while (back < 10 && i + forward < s.length && s[i + forward] != '\n')
forward++
const context = s.substring(i - back, i + forward)
let position = ''
for (let x = 1; x < back; x++) position += ' '
position += '^'
console.log(context)
console.log(position)
}
const emitError = msg => {
printContext()
throw new Error(msg)
}
const forward = () => {
while (i < s.length && isEmpty(s[i])) i++
if (i + 1 < s.length && s[i] == '/' && s[i + 1] == '/')
while (i < s.length && s[i] != '\n') i++
if (i < s.length && isEmpty(s[i])) forward()
}
const readdef = () => {
if (i < s.length && !isAlpha(s[i]))
emitError(`Expecting definition, not ${s[i]}`)
let n = 0
while (i + n < s.length && isDef(s[i+n])) n++
const res = s.substring(i, i + n)
i += n
return res
}
// 2018-07-01T00:00:00Z
const readISO8601 = () => {
if (i + 20 >= s.length) emitError('Incomplete date')
const dateString = s.substr(i, i + 20)
const date = moment.utc(dateString, iso8601)
if (!date.isValid()) emitError(`'${dateString}'' is not a valid date`)
i += 20
return date
}
const readSpannerContent = () => {
let n = 0
let brackets = 0
while (i < s.length && isSpannerChar(s[i + n])) {
if (s[i + n] == '(') brackets++
else if (s[i + n] == ')') {
if (brackets == 0) break
brackets--
}
n++
}
const adjustment = s.substring(i, i + n)
i += n
return adjustment
}
const readSpanner = (anchor) => {
const adjustment = readSpannerContent()
return spanner(anchor, adjustment, null, Object.assign({}, inconstants, constants))
}
const readCountAndUnit = () => {
let n = 0
while (i < s.length && isNumber(s[i + n])) n++
if (i < s.length && s[i + n] == '.') {
n++
while (i < s.length && isNumber(s[i + n])) n++
}
const count = parseFloat(s.substring(i, i + n))
i += n
if (i >= s.length) emitError('Expecting unit')
n = 0
while (i < s.length && isAlpha(s[i + n])) n++
const unit = s.substring(i, i + n)
i += n
return {
count: count,
unit: unit
}
}
const readInterval = () => {
i++
forward()
if (i >= s.length) emitError('Expecting interval params')
const start = readSpannerContent()
forward()
if (i + 1 >= s.length || s[i] != ',')
emitError('Expecting interval params')
i++
forward()
if (i >= s.length) emitError('Expecting interval params')
const duration = readSpannerContent()
forward()
if (i + 1 >= s.length || s[i] != ',')
emitError('Expecting interval params')
i++
forward()
if (i >= s.length) emitError('Expecting interval params')
const adjustment = readCountAndUnit()
forward()
if (i >= s.length || s[i] != ')')
emitError('Expecting interval close bracket')
i++
return {
type: 'interval',
start: start,
count: adjustment.count,
unit: adjustment.unit,
duration: duration
}
}
const readRange = () => {
i++
forward()
if (i >= s.length) emitError('Expecting range params')
const start = readSpannerContent()
forward()
if (i + 1 >= s.length || s[i] != ',')
emitError('Expecting range params')
i++
forward()
if (i >= s.length) emitError('Expecting range params')
const end = readSpannerContent()
forward()
if (i >= s.length || s[i] != ')')
emitError('Expecting range close bracket')
i++
return {
type: 'rang',
start: start,
end: end
}
}
const readReference = () => {
const id = readdef()
if (schedules[id]) return schedules[id]
else if (inschedules[id]) return inschedules[id]
else emitError(`Schedule ${id} not found`)
}
const readSchedule = (def) => {
let res = null
const operation = s[i]
i++
forward()
if (i >= s.length) emitError('Expecting schedule terms')
const id = readdef()
forward()
if (i < s.length && s[i] == '(') {
if (id == 'interval') res = readInterval()
else if (id == 'range') res = readRange()
else emitError(`Unknown function ${id}`)
} else res = {
type: 'schedule',
schedule: id
}
res.operation = operation == '+'
? 'or'
: operation == '-'
? 'not'
: operation == '*'
? 'and'
: null
return res
}
const readConstant = () => {
let anchor = null
if (isNumber(s[i])){
anchor = readISO8601()
} else {
const id = readdef()
if (constants[id]) anchor = constants[id].clone()
else if (inconstants[id]) anchor = inconstants[id].clone()
else emitError(`Constant ${id} not found`)
}
if (i < s.length && isSpannerOperation(s[i])) return readSpanner(anchor)
return anchor
}
while (i < s.length) {
forward()
const def = readdef()
forward()
if (def == '' && i >= s.length) break
if (i >= s.length) emitError('Expecting : after definition')
if (i < s.length && s[i] != ':')
emitError('Expecting : after definition')
i++
forward()
if (i >= s.length) emitError('Expecting definition terms')
if (isOperation(s[i])) {
const schedule = []
while (i < s.length && isOperation(s[i])) {
schedule.push(readSchedule(def))
forward()
}
schedules[def] = schedule
}
else constants[def] = readConstant()
}
return { schedules, constants }
}
})