pacc 4.17.0 → 4.18.0

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pacc",
3
- "version": "4.17.0",
3
+ "version": "4.18.0",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
@@ -19,7 +19,7 @@ import {
19
19
  EOF
20
20
  } from "./tokens.mjs";
21
21
 
22
- export function binop(op, left, right) {
22
+ export function binop(op, left, right, fallback) {
23
23
  switch (op) {
24
24
  case DOUBLE_BAR:
25
25
  return left || right;
@@ -46,6 +46,8 @@ export function binop(op, left, right) {
46
46
  case DIVIDE:
47
47
  return left / right;
48
48
  }
49
+
50
+ return fallback(op, left, right);
49
51
  }
50
52
 
51
53
  export function parse(context) {
@@ -55,6 +57,23 @@ export function parse(context) {
55
57
  const error = new Error(message);
56
58
  throw error;
57
59
  }
60
+ function binopError(op, left, right) {
61
+ error(`Unexpected '${op.str || op}'`);
62
+ }
63
+
64
+ const pathEval = node => {
65
+ let result = context.root;
66
+
67
+ for (const p of node.path) {
68
+ switch (typeof p) {
69
+ case "string":
70
+ case "number":
71
+ result = result[p];
72
+ }
73
+ }
74
+
75
+ return result;
76
+ };
58
77
 
59
78
  const advance = () => {
60
79
  const { value, done } = context.tokens.next();
@@ -81,7 +100,7 @@ export function parse(context) {
81
100
  const node = expression(0);
82
101
  expect(CLOSE_BRACKET);
83
102
  if (typeof node === "number") {
84
- return { path: [node] };
103
+ return { eval: pathEval, path: [node] };
85
104
  }
86
105
  return node;
87
106
  }
@@ -93,7 +112,7 @@ export function parse(context) {
93
112
 
94
113
  switch (typeof token) {
95
114
  case "string":
96
- return { path: [token] };
115
+ return { eval: pathEval, path: [token] };
97
116
  case "number":
98
117
  case "bigint":
99
118
  case "boolean":
@@ -113,7 +132,7 @@ export function parse(context) {
113
132
  case "number":
114
133
  case "bigint":
115
134
  case "boolean":
116
- return binop(token, left, right);
135
+ return binop(token, left, right, binopError);
117
136
  }
118
137
  }
119
138
  return {
@@ -129,7 +148,7 @@ export function parse(context) {
129
148
  switch (typeof left) {
130
149
  case "number":
131
150
  case "bigint":
132
- return binop(token, left, right);
151
+ return binop(token, left, right, binopError);
133
152
  }
134
153
  }
135
154
  if (token === DOT) {
@@ -142,13 +161,19 @@ export function parse(context) {
142
161
  right.path.unshift(left);
143
162
  return right;
144
163
  }
145
- return { path: [left.token, right.token] };
164
+ return { eval: pathEval, path: [left.token, right.token] };
146
165
  }
147
166
 
148
167
  if (right.token === EOF) {
149
168
  error("unexpected EOF");
150
169
  }
170
+
151
171
  return {
172
+ eval: (node) => {
173
+ const left = node.left?.eval ? node.left.eval(node.left) : node.left;
174
+ const right = node.right?.eval ? node.right.eval(node.right) : node.right;
175
+ return binop(node.token,left,right);
176
+ },
152
177
  token,
153
178
  left,
154
179
  right
@@ -184,5 +209,11 @@ export function parse(context) {
184
209
 
185
210
  advance();
186
211
 
187
- return expression(token.precedence ?? 0);
212
+ const result = expression(token.precedence ?? 0);
213
+
214
+ if(context.exec !== false &&result?.eval) {
215
+ return result.eval(result);
216
+ }
217
+
218
+ return result;
188
219
  }
@@ -27,7 +27,7 @@ import { convertValue } from "./attributes.mjs";
27
27
  * @param {Object} definition type def
28
28
  */
29
29
  export function setAttribute(object, expression, value, definition) {
30
- const { path } = parse({ tokens: tokens(expression) });
30
+ const { path } = parse({ tokens: tokens(expression), exec: false });
31
31
 
32
32
  let anchor, anchorKey;
33
33
 
@@ -60,7 +60,7 @@ export function setAttribute(object, expression, value, definition) {
60
60
  * @returns {any} value associated with the given property name
61
61
  */
62
62
  export function getAttribute(object, expression, definition) {
63
- const { path } = parse({ tokens: tokens(expression) });
63
+ const { path } = parse({ tokens: tokens(expression), exec: false });
64
64
 
65
65
  for (const key of path) {
66
66
  if (object !== undefined) {
@@ -1,2 +1,2 @@
1
- export function binop(op: any, left: any, right: any): any;
1
+ export function binop(op: any, left: any, right: any, fallback: any): any;
2
2
  export function parse(context: any): any;