bare-script 2.2.9 → 2.2.10

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.
Files changed (2) hide show
  1. package/lib/parser.js +28 -7
  2. package/package.json +1 -1
package/lib/parser.js CHANGED
@@ -56,6 +56,7 @@ export function parseScript(scriptText, startLineNumber = 1) {
56
56
  // Process each line
57
57
  const lineContinuation = [];
58
58
  let functionDef = null;
59
+ let functionLabelDefDepth = null;
59
60
  const labelDefs = [];
60
61
  let labelIndex = 0;
61
62
  let ixLine;
@@ -118,6 +119,7 @@ export function parseScript(scriptText, startLineNumber = 1) {
118
119
  }
119
120
 
120
121
  // Add the function definition statement
122
+ functionLabelDefDepth = labelDefs.length;
121
123
  functionDef = {
122
124
  'function': {
123
125
  'name': matchFunctionBegin.groups.name,
@@ -143,7 +145,17 @@ export function parseScript(scriptText, startLineNumber = 1) {
143
145
  if (functionDef === null) {
144
146
  throw new BareScriptParserError('No matching function definition', line, 1, startLineNumber + ixLine);
145
147
  }
148
+
149
+ // Check for un-matched label definitions
150
+ if (labelDefs.length > functionLabelDefDepth) {
151
+ const labelDef = labelDefs.pop();
152
+ const [defKey] = Object.keys(labelDef);
153
+ const def = labelDef[defKey];
154
+ throw new BareScriptParserError(`Missing end${defKey} statement`, def.line, 1, def.lineNumber);
155
+ }
156
+
146
157
  functionDef = null;
158
+ functionLabelDefDepth = null;
147
159
  continue;
148
160
  }
149
161
 
@@ -173,7 +185,8 @@ export function parseScript(scriptText, startLineNumber = 1) {
173
185
  const matchIfElseIf = line.match(rScriptIfElseIf);
174
186
  if (matchIfElseIf !== null) {
175
187
  // Get the if-then definition
176
- const ifthen = (labelDefs.length > 0 ? (labelDefs[labelDefs.length - 1].if ?? null) : null);
188
+ const labelDefDepth = (functionDef !== null ? functionLabelDefDepth : 0);
189
+ const ifthen = (labelDefs.length > labelDefDepth ? (labelDefs[labelDefs.length - 1].if ?? null) : null);
177
190
  if (ifthen === null) {
178
191
  throw new BareScriptParserError('No matching if statement', line, 1, startLineNumber + ixLine);
179
192
  }
@@ -204,7 +217,8 @@ export function parseScript(scriptText, startLineNumber = 1) {
204
217
  const matchIfElse = line.match(rScriptIfElse);
205
218
  if (matchIfElse !== null) {
206
219
  // Get the if-then definition
207
- const ifthen = (labelDefs.length > 0 ? (labelDefs[labelDefs.length - 1].if ?? null) : null);
220
+ const labelDefDepth = (functionDef !== null ? functionLabelDefDepth : 0);
221
+ const ifthen = (labelDefs.length > labelDefDepth ? (labelDefs[labelDefs.length - 1].if ?? null) : null);
208
222
  if (ifthen === null) {
209
223
  throw new BareScriptParserError('No matching if statement', line, 1, startLineNumber + ixLine);
210
224
  }
@@ -227,7 +241,8 @@ export function parseScript(scriptText, startLineNumber = 1) {
227
241
  const matchIfEnd = line.match(rScriptIfEnd);
228
242
  if (matchIfEnd !== null) {
229
243
  // Pop the if-then definition
230
- const ifthen = (labelDefs.length > 0 ? (labelDefs.pop().if ?? null) : null);
244
+ const labelDefDepth = (functionDef !== null ? functionLabelDefDepth : 0);
245
+ const ifthen = (labelDefs.length > labelDefDepth ? (labelDefs.pop().if ?? null) : null);
231
246
  if (ifthen === null) {
232
247
  throw new BareScriptParserError('No matching if statement', line, 1, startLineNumber + ixLine);
233
248
  }
@@ -269,7 +284,8 @@ export function parseScript(scriptText, startLineNumber = 1) {
269
284
  const matchWhileEnd = line.match(rScriptWhileEnd);
270
285
  if (matchWhileEnd !== null) {
271
286
  // Pop the while-do definition
272
- const whiledo = (labelDefs.length > 0 ? (labelDefs.pop().while ?? null) : null);
287
+ const labelDefDepth = (functionDef !== null ? functionLabelDefDepth : 0);
288
+ const whiledo = (labelDefs.length > labelDefDepth ? (labelDefs.pop().while ?? null) : null);
273
289
  if (whiledo === null) {
274
290
  throw new BareScriptParserError('No matching while statement', line, 1, startLineNumber + ixLine);
275
291
  }
@@ -319,7 +335,8 @@ export function parseScript(scriptText, startLineNumber = 1) {
319
335
  const matchForEnd = line.match(rScriptForEnd);
320
336
  if (matchForEnd !== null) {
321
337
  // Pop the foreach definition
322
- const foreach = (labelDefs.length > 0 ? (labelDefs.pop().for ?? null) : null);
338
+ const labelDefDepth = (functionDef !== null ? functionLabelDefDepth : 0);
339
+ const foreach = (labelDefs.length > labelDefDepth ? (labelDefs.pop().for ?? null) : null);
323
340
  if (foreach === null) {
324
341
  throw new BareScriptParserError('No matching for statement', line, 1, startLineNumber + ixLine);
325
342
  }
@@ -346,7 +363,9 @@ export function parseScript(scriptText, startLineNumber = 1) {
346
363
  const matchBreak = line.match(rScriptBreak);
347
364
  if (matchBreak !== null) {
348
365
  // Get the loop definition
349
- const labelDef = (labelDefs.length > 0 ? (labelDefs.findLast((def) => !('if' in def)) ?? null) : null);
366
+ const labelDefDepth = (functionDef !== null ? functionLabelDefDepth : 0);
367
+ const ixLabelDef = labelDefs.findLastIndex((def) => !('if' in def));
368
+ const labelDef = (ixLabelDef >= labelDefDepth ? labelDefs[ixLabelDef] : null);
350
369
  if (labelDef === null) {
351
370
  throw new BareScriptParserError('Break statement outside of loop', line, 1, startLineNumber + ixLine);
352
371
  }
@@ -362,7 +381,9 @@ export function parseScript(scriptText, startLineNumber = 1) {
362
381
  const matchContinue = line.match(rScriptContinue);
363
382
  if (matchContinue !== null) {
364
383
  // Get the loop definition
365
- const labelDef = (labelDefs.length > 0 ? (labelDefs.findLast((def) => !('if' in def)) ?? null) : null);
384
+ const labelDefDepth = (functionDef !== null ? functionLabelDefDepth : 0);
385
+ const ixLabelDef = labelDefs.findLastIndex((def) => !('if' in def));
386
+ const labelDef = (ixLabelDef >= labelDefDepth ? labelDefs[ixLabelDef] : null);
366
387
  if (labelDef === null) {
367
388
  throw new BareScriptParserError('Continue statement outside of loop', line, 1, startLineNumber + ixLine);
368
389
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "bare-script",
4
- "version": "2.2.9",
4
+ "version": "2.2.10",
5
5
  "description": "BareScript; a lightweight scripting and expression language",
6
6
  "keywords": [
7
7
  "expression",