bare-script 2.1.0 → 2.1.1

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/README.md CHANGED
@@ -33,7 +33,7 @@ import {parseScript} from 'bare-script/lib/parser.js';
33
33
  // Parse the script
34
34
  const script = parseScript(`\
35
35
  # Double a number
36
- function double(n)
36
+ function double(n):
37
37
  return n * 2
38
38
  endfunction
39
39
 
@@ -58,9 +58,9 @@ This outputs:
58
58
  includes a set of built-in functions for mathematical operations, object manipulation, array
59
59
  manipulation, regular expressions, HTTP fetch and more. The following example demonstrates the use
60
60
  of the
61
- [systemFetch](https://craigahobbs.github.io/bare-script/library/#var.vName='systemFetch'),
62
- [objectGet](https://craigahobbs.github.io/bare-script/library/#var.vName='objectGet'), and
63
- [arrayLength](https://craigahobbs.github.io/bare-script/library/#var.vName='arrayLength')
61
+ [systemFetch](https://craigahobbs.github.io/bare-script/library/#var.vGroup='System'&systemfetch),
62
+ [objectGet](https://craigahobbs.github.io/bare-script/library/#var.vGroup='Object'&objectget), and
63
+ [arrayLength](https://craigahobbs.github.io/bare-script/library/#var.vGroup='Array'&arraylength)
64
64
  functions.
65
65
 
66
66
  ~~~ javascript
@@ -134,7 +134,7 @@ bare script.bare
134
134
  ~~~
135
135
 
136
136
  **Note:** In the BareScript CLI, import statements and the
137
- [systemFetch](https://craigahobbs.github.io/bare-script/library/#var.vName='systemFetch')
137
+ [systemFetch](https://craigahobbs.github.io/bare-script/library/#var.vGroup='System'&systemfetch)
138
138
  function read non-URL paths from the local file system.
139
139
 
140
140
 
package/lib/model.js CHANGED
@@ -79,6 +79,9 @@ struct FunctionStatement
79
79
  # The function's argument names
80
80
  optional string[len > 0] args
81
81
 
82
+ # If true, the function's last argument is the array of all remaining arguments
83
+ optional bool lastArgArray
84
+
82
85
  # The function's statements
83
86
  ScriptStatement[] statements
84
87
 
package/lib/parser.js CHANGED
@@ -9,8 +9,10 @@ const rScriptLineSplit = /\r?\n/;
9
9
  const rScriptContinuation = /\\\s*$/;
10
10
  const rScriptComment = /^\s*(?:#.*)?$/;
11
11
  const rScriptAssignment = /^\s*(?<name>[A-Za-z_]\w*)\s*=\s*(?<expr>.+)$/;
12
- const rScriptFunctionBegin =
13
- /^\s*(?:(?<async>async)\s+)?function\s+(?<name>[A-Za-z_]\w*)\s*\(\s*(?<args>[A-Za-z_]\w*(?:\s*,\s*[A-Za-z_]\w*)*)?\s*\)\s*$/;
12
+ const rScriptFunctionBegin = new RegExp(
13
+ '^(?<async>\\s*async)?\\s*function\\s+(?<name>[A-Za-z_]\\w*)\\s*\\(' +
14
+ '\\s*(?<args>[A-Za-z_]\\w*(?:\\s*,\\s*[A-Za-z_]\\w*)*)?(?<lastArgArray>\\s*\\.\\.\\.)?\\s*\\)(?:\\s*:)?\\s*$'
15
+ );
14
16
  const rScriptFunctionArgSplit = /\s*,\s*/;
15
17
  const rScriptFunctionEnd = /^\s*endfunction\s*$/;
16
18
  const rScriptLabel = /^\s*(?<name>[A-Za-z_]\w*)\s*:\s*$/;
@@ -119,14 +121,18 @@ export function parseScript(scriptText, startLineNumber = 1) {
119
121
  functionDef = {
120
122
  'function': {
121
123
  'name': matchFunctionBegin.groups.name,
122
- 'args': typeof matchFunctionBegin.groups.args !== 'undefined'
123
- ? matchFunctionBegin.groups.args.split(rScriptFunctionArgSplit) : [],
124
124
  'statements': []
125
125
  }
126
126
  };
127
- if (matchFunctionBegin.groups.async === 'async') {
127
+ if (typeof matchFunctionBegin.groups.args !== 'undefined') {
128
+ functionDef.function.args = matchFunctionBegin.groups.args.split(rScriptFunctionArgSplit);
129
+ }
130
+ if (typeof matchFunctionBegin.groups.async !== 'undefined') {
128
131
  functionDef.function.async = true;
129
132
  }
133
+ if (typeof matchFunctionBegin.groups.lastArgArray !== 'undefined') {
134
+ functionDef.function.lastArgArray = true;
135
+ }
130
136
  statements.push(functionDef);
131
137
  continue;
132
138
  }
package/lib/runtime.js CHANGED
@@ -134,8 +134,11 @@ export function executeScriptHelper(statements, options, locals) {
134
134
  const funcLocals = {};
135
135
  if ('args' in statement.function) {
136
136
  const argsLength = args.length;
137
- for (let ixArg = 0; ixArg < statement.function.args.length; ixArg++) {
138
- funcLocals[statement.function.args[ixArg]] = (ixArg < argsLength ? args[ixArg] : null);
137
+ const funcArgsLength = statement.function.args.length;
138
+ const ixArgLast = (statement.function.lastArgArray ?? null) && (funcArgsLength - 1);
139
+ for (let ixArg = 0; ixArg < funcArgsLength; ixArg++) {
140
+ const argName = statement.function.args[ixArg];
141
+ funcLocals[argName] = (ixArg < argsLength ? (ixArg === ixArgLast ? args.slice(ixArg) : args[ixArg]) : null);
139
142
  }
140
143
  }
141
144
  return executeScriptHelper(statement.function.statements, fnOptions, funcLocals);
@@ -104,8 +104,11 @@ async function executeScriptHelperAsync(statements, options, locals) {
104
104
  const funcLocals = {};
105
105
  if ('args' in statement.function) {
106
106
  const argsLength = args.length;
107
- for (let ixArg = 0; ixArg < statement.function.args.length; ixArg++) {
108
- funcLocals[statement.function.args[ixArg]] = (ixArg < argsLength ? args[ixArg] : null);
107
+ const funcArgsLength = statement.function.args.length;
108
+ const ixArgLast = (statement.function.lastArgArray ?? null) && (funcArgsLength - 1);
109
+ for (let ixArg = 0; ixArg < funcArgsLength; ixArg++) {
110
+ const argName = statement.function.args[ixArg];
111
+ funcLocals[argName] = (ixArg < argsLength ? (ixArg === ixArgLast ? args.slice(ixArg) : args[ixArg]) : null);
109
112
  }
110
113
  }
111
114
  return executeScriptHelperAsync(statement.function.statements, fnOptions, funcLocals);
@@ -115,8 +118,11 @@ async function executeScriptHelperAsync(statements, options, locals) {
115
118
  const funcLocals = {};
116
119
  if ('args' in statement.function) {
117
120
  const argsLength = args.length;
118
- for (let ixArg = 0; ixArg < statement.function.args.length; ixArg++) {
119
- funcLocals[statement.function.args[ixArg]] = (ixArg < argsLength ? args[ixArg] : null);
121
+ const funcArgsLength = statement.function.args.length;
122
+ const ixArgLast = (statement.function.lastArgArray ?? null) && (funcArgsLength - 1);
123
+ for (let ixArg = 0; ixArg < funcArgsLength; ixArg++) {
124
+ const argName = statement.function.args[ixArg];
125
+ funcLocals[argName] = (ixArg < argsLength ? (ixArg === ixArgLast ? args.slice(ixArg) : args[ixArg]) : null);
120
126
  }
121
127
  }
122
128
  return executeScriptHelper(statement.function.statements, fnOptions, funcLocals);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "bare-script",
4
- "version": "2.1.0",
4
+ "version": "2.1.1",
5
5
  "description": "BareScript; a lightweight scripting and expression language",
6
6
  "keywords": [
7
7
  "expression",