@halleyassist/rule-parser 1.0.12 → 1.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/RuleParser.js +22 -6
- package/src/RuleParser.production.js +22 -6
package/package.json
CHANGED
package/src/RuleParser.js
CHANGED
|
@@ -68,7 +68,7 @@ class RuleParser {
|
|
|
68
68
|
ParserCache = new Parser(ParserRules, {debug: false})
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
ret = ParserCache.getAST(txt, 'statement_main');
|
|
71
|
+
ret = ParserCache.getAST(txt.trim(), 'statement_main');
|
|
72
72
|
|
|
73
73
|
if(ret){
|
|
74
74
|
return ret.children[0]
|
|
@@ -137,20 +137,36 @@ class RuleParser {
|
|
|
137
137
|
}
|
|
138
138
|
return ["TimePeriodConst", tp.text]
|
|
139
139
|
case 'time_period_ago_between': {
|
|
140
|
-
// time_period_ago_between has
|
|
141
|
-
|
|
140
|
+
// time_period_ago_between has: number_time (WS+ number_time)* WS+ AGO WS+ between_tod_only
|
|
141
|
+
// We need to extract all number_time children and sum them up, then return TimePeriodBetweenAgo
|
|
142
|
+
let totalSeconds = 0
|
|
143
|
+
let betweenTodOnly = null
|
|
144
|
+
|
|
145
|
+
// Find all number_time children and the between_tod_only child
|
|
146
|
+
for (let i = 0; i < tp.children.length; i++) {
|
|
147
|
+
if (tp.children[i].type === 'number_time') {
|
|
148
|
+
totalSeconds += RuleParser.__parseValue(tp.children[i])
|
|
149
|
+
} else if (tp.children[i].type === 'between_tod_only') {
|
|
150
|
+
betweenTodOnly = tp.children[i]
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// This should always be present based on the grammar, but check defensively
|
|
155
|
+
if (!betweenTodOnly) {
|
|
156
|
+
throw new Error('time_period_ago_between requires between_tod_only child')
|
|
157
|
+
}
|
|
158
|
+
|
|
142
159
|
const betweenTod = betweenTodOnly.children[0]
|
|
143
160
|
let startTod = RuleParser.__parseValue(betweenTod.children[0])
|
|
144
161
|
let endTod = RuleParser.__parseValue(betweenTod.children[1])
|
|
145
162
|
|
|
146
163
|
// Check if there's a dow_range at betweenTod.children[2]
|
|
164
|
+
// Note: startTod and endTod should always be objects from number_tod parsing
|
|
147
165
|
if (betweenTod.children.length > 2) {
|
|
148
|
-
if(typeof startTod === 'number') startTod = {seconds: startTod, dow: null}
|
|
149
|
-
if(typeof endTod === 'number') endTod = {seconds: endTod, dow: null}
|
|
150
166
|
RuleParser._addDowToTods(startTod, endTod, betweenTod.children[2])
|
|
151
167
|
}
|
|
152
168
|
|
|
153
|
-
return ["
|
|
169
|
+
return ["TimePeriodBetweenAgo", totalSeconds, startTod, endTod]
|
|
154
170
|
}
|
|
155
171
|
case 'between_tod_only': {
|
|
156
172
|
// between_tod_only has children[0] = between_tod node
|
|
@@ -68,7 +68,7 @@ class RuleParser {
|
|
|
68
68
|
ParserCache = new Parser(ParserRules, {debug: false})
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
ret = ParserCache.getAST(txt, 'statement_main');
|
|
71
|
+
ret = ParserCache.getAST(txt.trim(), 'statement_main');
|
|
72
72
|
|
|
73
73
|
if(ret){
|
|
74
74
|
return ret.children[0]
|
|
@@ -137,20 +137,36 @@ class RuleParser {
|
|
|
137
137
|
}
|
|
138
138
|
return ["TimePeriodConst", tp.text]
|
|
139
139
|
case 'time_period_ago_between': {
|
|
140
|
-
// time_period_ago_between has
|
|
141
|
-
|
|
140
|
+
// time_period_ago_between has: number_time (WS+ number_time)* WS+ AGO WS+ between_tod_only
|
|
141
|
+
// We need to extract all number_time children and sum them up, then return TimePeriodBetweenAgo
|
|
142
|
+
let totalSeconds = 0
|
|
143
|
+
let betweenTodOnly = null
|
|
144
|
+
|
|
145
|
+
// Find all number_time children and the between_tod_only child
|
|
146
|
+
for (let i = 0; i < tp.children.length; i++) {
|
|
147
|
+
if (tp.children[i].type === 'number_time') {
|
|
148
|
+
totalSeconds += RuleParser.__parseValue(tp.children[i])
|
|
149
|
+
} else if (tp.children[i].type === 'between_tod_only') {
|
|
150
|
+
betweenTodOnly = tp.children[i]
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// This should always be present based on the grammar, but check defensively
|
|
155
|
+
if (!betweenTodOnly) {
|
|
156
|
+
throw new Error('time_period_ago_between requires between_tod_only child')
|
|
157
|
+
}
|
|
158
|
+
|
|
142
159
|
const betweenTod = betweenTodOnly.children[0]
|
|
143
160
|
let startTod = RuleParser.__parseValue(betweenTod.children[0])
|
|
144
161
|
let endTod = RuleParser.__parseValue(betweenTod.children[1])
|
|
145
162
|
|
|
146
163
|
// Check if there's a dow_range at betweenTod.children[2]
|
|
164
|
+
// Note: startTod and endTod should always be objects from number_tod parsing
|
|
147
165
|
if (betweenTod.children.length > 2) {
|
|
148
|
-
if(typeof startTod === 'number') startTod = {seconds: startTod, dow: null}
|
|
149
|
-
if(typeof endTod === 'number') endTod = {seconds: endTod, dow: null}
|
|
150
166
|
RuleParser._addDowToTods(startTod, endTod, betweenTod.children[2])
|
|
151
167
|
}
|
|
152
168
|
|
|
153
|
-
return ["
|
|
169
|
+
return ["TimePeriodBetweenAgo", totalSeconds, startTod, endTod]
|
|
154
170
|
}
|
|
155
171
|
case 'between_tod_only': {
|
|
156
172
|
// between_tod_only has children[0] = between_tod node
|