@traqula/rules-sparql-1-1-adjust 0.0.16 → 0.0.17

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/index.cjs +256 -150
  2. package/package.json +4 -4
package/lib/index.cjs CHANGED
@@ -9768,6 +9768,11 @@ var LexerBuilder = class _LexerBuilder {
9768
9768
 
9769
9769
  // ../core/lib/Transformers.js
9770
9770
  var TransformerType = class {
9771
+ clone(obj) {
9772
+ const newObj = Object.create(Object.getPrototypeOf(obj));
9773
+ Object.defineProperties(newObj, Object.getOwnPropertyDescriptors(obj));
9774
+ return newObj;
9775
+ }
9771
9776
  safeObjectVisit(value, mapper) {
9772
9777
  if (value && typeof value === "object") {
9773
9778
  if (Array.isArray(value)) {
@@ -9777,104 +9782,213 @@ var TransformerType = class {
9777
9782
  }
9778
9783
  return value;
9779
9784
  }
9780
- transformNode(curObject, nodeCallBacks) {
9781
- let continueCheck;
9782
- let transformation;
9783
- const casted = curObject;
9784
- if (casted.type) {
9785
- continueCheck = nodeCallBacks[casted.type]?.continue;
9786
- transformation = nodeCallBacks[casted.type]?.transform;
9787
- }
9788
- let shouldContinue = true;
9789
- if (continueCheck) {
9790
- shouldContinue = continueCheck(curObject);
9791
- }
9792
- if (!shouldContinue) {
9793
- return curObject;
9794
- }
9795
- const copy3 = { ...curObject };
9796
- for (const [key, value] of Object.entries(copy3)) {
9797
- copy3[key] = this.safeObjectVisit(value, (obj) => this.transformNode(obj, nodeCallBacks));
9798
- }
9799
- if (transformation) {
9800
- return transformation(copy3);
9801
- }
9802
- return copy3;
9785
+ /**
9786
+ * Recursively transforms all objects that are not arrays. Mapper is called on deeper objects first.
9787
+ * @param startObject object to start iterating from
9788
+ * @param mapper mapper to transform the various objects - argument is a copy of the original
9789
+ * @param preVisitor callback that is evaluated before iterating deeper.
9790
+ * If continues is false, we do not iterate deeper, current object is still mapped. - default: true
9791
+ * If shortcut is true, we do not iterate deeper, nor do we branch out, this mapper will be the last one called.
9792
+ * - Default false
9793
+ */
9794
+ transformObject(startObject, mapper, preVisitor = () => ({})) {
9795
+ let didShortCut = false;
9796
+ const recurse = (curObject) => {
9797
+ const copy3 = this.clone(curObject);
9798
+ const context = preVisitor(copy3);
9799
+ didShortCut = context.shortcut ?? false;
9800
+ const continues = context.continue ?? true;
9801
+ if (continues && !didShortCut) {
9802
+ for (const [key, value] of Object.entries(copy3)) {
9803
+ if (didShortCut) {
9804
+ return copy3;
9805
+ }
9806
+ copy3[key] = this.safeObjectVisit(value, (obj) => recurse(obj));
9807
+ }
9808
+ }
9809
+ return mapper(copy3);
9810
+ };
9811
+ return recurse(startObject);
9803
9812
  }
9804
- visitNode(curObject, nodeCallBacks) {
9805
- let callback;
9806
- const casted = curObject;
9807
- if (casted.type) {
9808
- callback = nodeCallBacks[casted.type];
9809
- }
9810
- let shouldIterate = true;
9811
- if (callback) {
9812
- shouldIterate = callback(curObject);
9813
- }
9814
- if (shouldIterate) {
9815
- for (const value of Object.values(curObject)) {
9816
- this.safeObjectVisit(value, (obj) => this.visitNode(obj, nodeCallBacks));
9813
+ /**
9814
+ * Visitor that visits all objects. Visits deeper objects first.
9815
+ */
9816
+ visitObject(startObject, visitor, preVisitor = () => ({})) {
9817
+ let didShortCut = false;
9818
+ const recurse = (curObject) => {
9819
+ const context = preVisitor(curObject);
9820
+ didShortCut = context.shortcut ?? false;
9821
+ const continues = context.continue ?? true;
9822
+ if (continues && !didShortCut) {
9823
+ for (const value of Object.values(curObject)) {
9824
+ if (didShortCut) {
9825
+ return;
9826
+ }
9827
+ this.safeObjectVisit(value, (obj) => recurse(obj));
9828
+ }
9817
9829
  }
9818
- }
9830
+ visitor(curObject);
9831
+ };
9832
+ recurse(startObject);
9833
+ }
9834
+ transformNode(startObject, nodeCallBacks) {
9835
+ const transformWrapper = (curObject) => {
9836
+ const casted = curObject;
9837
+ if (casted.type) {
9838
+ const ogFunc = nodeCallBacks[casted.type]?.transform;
9839
+ if (ogFunc) {
9840
+ return ogFunc(casted);
9841
+ }
9842
+ }
9843
+ return curObject;
9844
+ };
9845
+ const preTransformWrapper = (curObject) => {
9846
+ const casted = curObject;
9847
+ if (casted.type) {
9848
+ const ogFunc = nodeCallBacks[casted.type]?.preVisitor;
9849
+ if (ogFunc) {
9850
+ return ogFunc(casted);
9851
+ }
9852
+ }
9853
+ return {};
9854
+ };
9855
+ return this.transformObject(startObject, transformWrapper, preTransformWrapper);
9856
+ }
9857
+ visitNode(startObject, nodeCallBacks) {
9858
+ const visitWrapper = (curObject) => {
9859
+ const casted = curObject;
9860
+ if (casted.type) {
9861
+ const ogFunc = nodeCallBacks[casted.type]?.visitor;
9862
+ if (ogFunc) {
9863
+ ogFunc(casted);
9864
+ }
9865
+ }
9866
+ };
9867
+ const preVisitWrapper = (curObject) => {
9868
+ const casted = curObject;
9869
+ if (casted.type) {
9870
+ const ogFunc = nodeCallBacks[casted.type]?.preVisitor;
9871
+ if (ogFunc) {
9872
+ return ogFunc(casted);
9873
+ }
9874
+ }
9875
+ return {};
9876
+ };
9877
+ this.visitObject(startObject, visitWrapper, preVisitWrapper);
9878
+ }
9879
+ traverseNodes(currentNode, traverse) {
9880
+ let didShortCut = false;
9881
+ const recurse = (curNode) => {
9882
+ const traverser = traverse[curNode.type];
9883
+ if (traverser) {
9884
+ const { next, shortcut } = traverser(curNode);
9885
+ didShortCut = shortcut ?? false;
9886
+ if (!didShortCut) {
9887
+ for (const node of next ?? []) {
9888
+ if (didShortCut) {
9889
+ return;
9890
+ }
9891
+ recurse(node);
9892
+ }
9893
+ }
9894
+ }
9895
+ };
9896
+ recurse(currentNode);
9819
9897
  }
9820
9898
  };
9821
9899
  var TransformerSubType = class extends TransformerType {
9822
- // Public visitObjects(curObject: object, visitor: (current: object) => void): void {
9823
- // for (const value of Object.values(curObject)) {
9824
- // this.safeObjectVisit(value, obj => this.visitObjects(obj, visitor));
9825
- // }
9826
- // }
9827
- transformNodeSpecific(curObject, nodeCallBacks, nodeSpecificCallBacks = {}) {
9828
- let continueCheck;
9829
- let transformation;
9830
- const casted = curObject;
9831
- if (casted.type && casted.subType) {
9832
- const specific = nodeSpecificCallBacks[casted.type];
9833
- if (specific) {
9834
- continueCheck = specific[casted.subType]?.continue;
9835
- transformation = specific[casted.subType]?.transform;
9836
- }
9837
- }
9838
- let shouldContinue = true;
9839
- if (continueCheck) {
9840
- shouldContinue = continueCheck(curObject);
9841
- }
9842
- if (!shouldContinue) {
9843
- return curObject;
9844
- }
9845
- const copy3 = { ...curObject };
9846
- for (const [key, value] of Object.entries(copy3)) {
9847
- copy3[key] = this.safeObjectVisit(value, (obj) => this.transformNodeSpecific(obj, nodeCallBacks, nodeSpecificCallBacks));
9848
- }
9849
- if (transformation) {
9850
- return transformation(copy3);
9851
- }
9852
- return copy3;
9900
+ transformNodeSpecific(startObject, nodeCallBacks, nodeSpecificCallBacks) {
9901
+ const transformWrapper = (curObject) => {
9902
+ let ogTransform;
9903
+ const casted = curObject;
9904
+ if (casted.type && casted.subType) {
9905
+ const specific = nodeSpecificCallBacks[casted.type];
9906
+ if (specific) {
9907
+ ogTransform = specific[casted.subType]?.transform;
9908
+ }
9909
+ if (!ogTransform) {
9910
+ ogTransform = nodeCallBacks[casted.type]?.transform;
9911
+ }
9912
+ }
9913
+ return ogTransform ? ogTransform(casted) : curObject;
9914
+ };
9915
+ const preVisitWrapper = (curObject) => {
9916
+ let ogPreVisit;
9917
+ const casted = curObject;
9918
+ if (casted.type && casted.subType) {
9919
+ const specific = nodeSpecificCallBacks[casted.type];
9920
+ if (specific) {
9921
+ ogPreVisit = specific[casted.subType]?.preVisitor;
9922
+ }
9923
+ if (!ogPreVisit) {
9924
+ ogPreVisit = nodeCallBacks[casted.type]?.preVisitor;
9925
+ }
9926
+ }
9927
+ return ogPreVisit ? ogPreVisit(casted) : curObject;
9928
+ };
9929
+ return this.transformObject(startObject, transformWrapper, preVisitWrapper);
9853
9930
  }
9854
9931
  /**
9855
9932
  * When both nodeCallBack and NodeSpecific callBack are matched, will only look at nodeSpecifCallback
9856
9933
  */
9857
- visitNodeSpecific(curObject, nodeCallBacks, nodeSpecificCallBacks = {}) {
9858
- let callback;
9859
- const casted = curObject;
9860
- if (casted.type && casted.subType) {
9861
- const specific = nodeSpecificCallBacks[casted.type];
9862
- if (specific) {
9863
- callback = specific[casted.subType];
9934
+ visitNodeSpecific(startObject, nodeCallBacks, nodeSpecificCallBacks) {
9935
+ const visitWrapper = (curObject) => {
9936
+ let ogTransform;
9937
+ const casted = curObject;
9938
+ if (casted.type && casted.subType) {
9939
+ const specific = nodeSpecificCallBacks[casted.type];
9940
+ if (specific) {
9941
+ ogTransform = specific[casted.subType]?.visitor;
9942
+ }
9943
+ if (!ogTransform) {
9944
+ ogTransform = nodeCallBacks[casted.type]?.visitor;
9945
+ }
9864
9946
  }
9865
- }
9866
- if (!callback && casted.type) {
9867
- callback = nodeCallBacks[curObject.type];
9868
- }
9869
- let shouldIterate = true;
9870
- if (callback) {
9871
- shouldIterate = callback(curObject) ?? true;
9872
- }
9873
- if (shouldIterate) {
9874
- for (const value of Object.values(curObject)) {
9875
- this.safeObjectVisit(value, (obj) => this.visitNodeSpecific(obj, nodeCallBacks, nodeSpecificCallBacks));
9947
+ if (ogTransform) {
9948
+ ogTransform(casted);
9876
9949
  }
9877
- }
9950
+ };
9951
+ const preVisitWrapper = (curObject) => {
9952
+ let ogPreVisit;
9953
+ const casted = curObject;
9954
+ if (casted.type && casted.subType) {
9955
+ const specific = nodeSpecificCallBacks[casted.type];
9956
+ if (specific) {
9957
+ ogPreVisit = specific[casted.subType]?.preVisitor;
9958
+ }
9959
+ if (!ogPreVisit) {
9960
+ ogPreVisit = nodeCallBacks[casted.type]?.preVisitor;
9961
+ }
9962
+ }
9963
+ return ogPreVisit ? ogPreVisit(casted) : curObject;
9964
+ };
9965
+ this.visitObject(startObject, visitWrapper, preVisitWrapper);
9966
+ }
9967
+ traverseSubNodes(currentNode, traverseNode, traverseSubNode) {
9968
+ let didShortCut = false;
9969
+ const recurse = (curNode) => {
9970
+ let traverser;
9971
+ const subObj = traverseSubNode[curNode.type];
9972
+ if (subObj) {
9973
+ traverser = subObj[curNode.subType];
9974
+ }
9975
+ if (!traverser) {
9976
+ traverser = traverseNode[curNode.type];
9977
+ }
9978
+ if (traverser) {
9979
+ const { next, shortcut } = traverser(curNode);
9980
+ didShortCut = shortcut ?? false;
9981
+ if (!didShortCut) {
9982
+ for (const node of next ?? []) {
9983
+ if (didShortCut) {
9984
+ return;
9985
+ }
9986
+ recurse(node);
9987
+ }
9988
+ }
9989
+ }
9990
+ };
9991
+ recurse(currentNode);
9878
9992
  }
9879
9993
  };
9880
9994
 
@@ -11023,12 +11137,12 @@ var CommonIRIs;
11023
11137
  CommonIRIs2["NIL"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#nil";
11024
11138
  CommonIRIs2["TYPE"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
11025
11139
  })(CommonIRIs || (CommonIRIs = {}));
11026
- var TransformerSparql11 = class extends TransformerSubType {
11140
+ var AstTransformer = class extends TransformerSubType {
11027
11141
  };
11028
11142
 
11029
11143
  // ../rules-sparql-1-1/lib/validation/validators.js
11030
11144
  var F = new AstFactory();
11031
- var transformer = new TransformerSparql11();
11145
+ var transformer = new AstTransformer();
11032
11146
  function getAggregatesOfExpression(expression2) {
11033
11147
  if (F.isExpressionAggregate(expression2)) {
11034
11148
  return [expression2];
@@ -11107,77 +11221,69 @@ function queryIsGood(query2) {
11107
11221
  }
11108
11222
  }
11109
11223
  }
11224
+ function notUndefined(some2) {
11225
+ return some2 !== void 0;
11226
+ }
11110
11227
  function findPatternBoundedVars(iter, boundedVars) {
11111
- if (F.isQuery(iter) || F.isUpdate(iter)) {
11112
- if (F.isQuerySelect(iter) || F.isQueryDescribe(iter)) {
11113
- if (iter.where && iter.variables.some((x) => F.isWildcard(x))) {
11114
- findPatternBoundedVars(iter.where, boundedVars);
11115
- } else {
11116
- for (const v of iter.variables) {
11117
- findPatternBoundedVars(v, boundedVars);
11118
- }
11119
- }
11120
- if (iter.solutionModifiers.group) {
11121
- const grouping = iter.solutionModifiers.group;
11122
- for (const g of grouping.groupings) {
11123
- if ("variable" in g) {
11124
- findPatternBoundedVars(g.variable, boundedVars);
11125
- }
11126
- }
11127
- }
11128
- if (iter.values?.values && iter.values.values.length > 0) {
11129
- const values3 = iter.values.values;
11130
- for (const v of Object.keys(values3[0])) {
11228
+ transformer.traverseSubNodes(iter, {
11229
+ query: (op) => ({ next: [
11230
+ op.solutionModifiers.group
11231
+ ].filter(notUndefined) }),
11232
+ triple: (op) => ({ next: [op.subject, op.predicate, op.object] }),
11233
+ path: (op) => ({ next: op.items }),
11234
+ tripleCollection: (op) => ({ next: [op.identifier, ...op.triples] })
11235
+ }, {
11236
+ query: {
11237
+ select: (op) => ({ next: [
11238
+ ...op.variables.some((x) => F.isWildcard(x)) ? [op.where] : op.variables,
11239
+ op.solutionModifiers.group,
11240
+ op.values
11241
+ ].filter(notUndefined) }),
11242
+ describe: (op) => ({ next: [
11243
+ ...op.variables.some((x) => F.isWildcard(x)) ? [op.where] : op.variables,
11244
+ op.solutionModifiers.group,
11245
+ op.values
11246
+ ].filter(notUndefined) })
11247
+ },
11248
+ solutionModifier: {
11249
+ group: (op) => ({
11250
+ next: op.groupings.filter((g) => "variable" in g).map((x) => x.variable)
11251
+ }),
11252
+ having: (op) => ({ next: op.having }),
11253
+ order: (op) => ({ next: op.orderDefs.map((x) => x.expression) })
11254
+ },
11255
+ pattern: {
11256
+ values: (op) => {
11257
+ for (const v of Object.keys(op.values.at(0) ?? {})) {
11131
11258
  boundedVars.add(v);
11132
11259
  }
11260
+ return {};
11261
+ },
11262
+ bgp: (op) => ({ next: op.triples }),
11263
+ group: (op) => ({ next: op.patterns }),
11264
+ union: (op) => ({ next: op.patterns }),
11265
+ optional: (op) => ({ next: op.patterns }),
11266
+ service: (op) => ({ next: [op.name, ...op.patterns] }),
11267
+ bind: (op) => ({ next: [op.variable] }),
11268
+ graph: (op) => ({ next: [op.name, ...op.patterns] }),
11269
+ minus: (op) => ({ next: op.patterns.slice(0, 1) })
11270
+ },
11271
+ term: {
11272
+ variable: (op) => {
11273
+ boundedVars.add(op.value);
11274
+ return {};
11133
11275
  }
11134
11276
  }
11135
- } else if (F.isTerm(iter)) {
11136
- if (F.isTermVariable(iter)) {
11137
- boundedVars.add(iter.value);
11138
- }
11139
- } else if (F.isTriple(iter)) {
11140
- findPatternBoundedVars(iter.subject, boundedVars);
11141
- findPatternBoundedVars(iter.predicate, boundedVars);
11142
- findPatternBoundedVars(iter.object, boundedVars);
11143
- } else if (F.isPath(iter)) {
11144
- if (!F.isTerm(iter)) {
11145
- for (const item of iter.items) {
11146
- findPatternBoundedVars(item, boundedVars);
11147
- }
11148
- }
11149
- } else if (F.isTripleCollection(iter) || F.isPatternBgp(iter)) {
11150
- for (const triple of iter.triples) {
11151
- findPatternBoundedVars(triple, boundedVars);
11152
- }
11153
- } else if (F.isPatternGroup(iter) || F.isPatternUnion(iter) || F.isPatternOptional(iter) || F.isPatternService(iter)) {
11154
- for (const pattern of iter.patterns) {
11155
- findPatternBoundedVars(pattern, boundedVars);
11156
- }
11157
- if (F.isPatternService(iter)) {
11158
- findPatternBoundedVars(iter.name, boundedVars);
11159
- }
11160
- } else if (F.isPatternBind(iter)) {
11161
- findPatternBoundedVars(iter.variable, boundedVars);
11162
- } else if (F.isPatternValues(iter)) {
11163
- for (const variable of Object.keys(iter.values.at(0) ?? {})) {
11164
- boundedVars.add(variable);
11165
- }
11166
- } else if (F.isPatternGraph(iter)) {
11167
- findPatternBoundedVars(iter.name, boundedVars);
11168
- for (const pattern of iter.patterns) {
11169
- findPatternBoundedVars(pattern, boundedVars);
11170
- }
11171
- }
11277
+ });
11172
11278
  }
11173
11279
  function checkNote13(patterns) {
11174
11280
  for (const [index, pattern] of patterns.entries()) {
11175
11281
  if (F.isPatternBind(pattern) && index > 0 && F.isPatternBgp(patterns[index - 1])) {
11176
11282
  const bgp = patterns[index - 1];
11177
11283
  const variables = [];
11178
- transformer.visitNodeSpecific(bgp, {}, { term: { variable: (var_2) => {
11284
+ transformer.visitNodeSpecific(bgp, {}, { term: { variable: { visitor: (var_2) => {
11179
11285
  variables.push(var_2);
11180
- } } });
11286
+ } } } });
11181
11287
  if (variables.some((var_2) => var_2.value === pattern.variable.value)) {
11182
11288
  throw new Error(`Variable used to bind is already bound (?${pattern.variable.value})`);
11183
11289
  }
@@ -11203,12 +11309,12 @@ function updateNoReuseBlankNodeLabels(updateQuery) {
11203
11309
  const operation = update2.operation;
11204
11310
  if (operation.subType === "insertdata") {
11205
11311
  const blankNodesHere = /* @__PURE__ */ new Set();
11206
- transformer.visitNodeSpecific(operation, {}, { term: { blankNode: (blankNode2) => {
11312
+ transformer.visitNodeSpecific(operation, {}, { term: { blankNode: { visitor: (blankNode2) => {
11207
11313
  blankNodesHere.add(blankNode2.label);
11208
11314
  if (blankLabelsUsedInInsertData.has(blankNode2.label)) {
11209
11315
  throw new Error("Detected reuse blank node across different INSERT DATA clauses");
11210
11316
  }
11211
- } } });
11317
+ } } } });
11212
11318
  for (const blankNode2 of blankNodesHere) {
11213
11319
  blankLabelsUsedInInsertData.add(blankNode2);
11214
11320
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@traqula/rules-sparql-1-1-adjust",
3
3
  "type": "module",
4
- "version": "0.0.16",
4
+ "version": "0.0.17",
5
5
  "description": "Traqula Lexer and Grammar Rules for sparql 1.1-ADJUST",
6
6
  "lsd:module": true,
7
7
  "license": "MIT",
@@ -42,8 +42,8 @@
42
42
  "build:transpile": " node \"../../node_modules/esbuild/bin/esbuild\" --format=cjs --bundle --log-level=error --outfile=lib/index.cjs lib/index.ts"
43
43
  },
44
44
  "dependencies": {
45
- "@traqula/core": "^0.0.16",
46
- "@traqula/rules-sparql-1-1": "^0.0.16"
45
+ "@traqula/core": "^0.0.17",
46
+ "@traqula/rules-sparql-1-1": "^0.0.17"
47
47
  },
48
- "gitHead": "2de4ee608a2337bed63ecc5cf336b1a57c39e45f"
48
+ "gitHead": "e4c1ef096da34d42ff86ac01d9affd428754c0b1"
49
49
  }