@traqula/parser-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 (3) hide show
  1. package/lib/Parser.d.ts +198 -198
  2. package/lib/index.cjs +256 -150
  3. package/package.json +7 -7
package/lib/index.cjs CHANGED
@@ -9744,6 +9744,11 @@ ${firstError.message}`);
9744
9744
 
9745
9745
  // ../../packages/core/lib/Transformers.js
9746
9746
  var TransformerType = class {
9747
+ clone(obj) {
9748
+ const newObj = Object.create(Object.getPrototypeOf(obj));
9749
+ Object.defineProperties(newObj, Object.getOwnPropertyDescriptors(obj));
9750
+ return newObj;
9751
+ }
9747
9752
  safeObjectVisit(value, mapper) {
9748
9753
  if (value && typeof value === "object") {
9749
9754
  if (Array.isArray(value)) {
@@ -9753,104 +9758,213 @@ var TransformerType = class {
9753
9758
  }
9754
9759
  return value;
9755
9760
  }
9756
- transformNode(curObject, nodeCallBacks) {
9757
- let continueCheck;
9758
- let transformation;
9759
- const casted = curObject;
9760
- if (casted.type) {
9761
- continueCheck = nodeCallBacks[casted.type]?.continue;
9762
- transformation = nodeCallBacks[casted.type]?.transform;
9763
- }
9764
- let shouldContinue = true;
9765
- if (continueCheck) {
9766
- shouldContinue = continueCheck(curObject);
9767
- }
9768
- if (!shouldContinue) {
9769
- return curObject;
9770
- }
9771
- const copy3 = { ...curObject };
9772
- for (const [key, value] of Object.entries(copy3)) {
9773
- copy3[key] = this.safeObjectVisit(value, (obj) => this.transformNode(obj, nodeCallBacks));
9774
- }
9775
- if (transformation) {
9776
- return transformation(copy3);
9777
- }
9778
- return copy3;
9761
+ /**
9762
+ * Recursively transforms all objects that are not arrays. Mapper is called on deeper objects first.
9763
+ * @param startObject object to start iterating from
9764
+ * @param mapper mapper to transform the various objects - argument is a copy of the original
9765
+ * @param preVisitor callback that is evaluated before iterating deeper.
9766
+ * If continues is false, we do not iterate deeper, current object is still mapped. - default: true
9767
+ * If shortcut is true, we do not iterate deeper, nor do we branch out, this mapper will be the last one called.
9768
+ * - Default false
9769
+ */
9770
+ transformObject(startObject, mapper, preVisitor = () => ({})) {
9771
+ let didShortCut = false;
9772
+ const recurse = (curObject) => {
9773
+ const copy3 = this.clone(curObject);
9774
+ const context = preVisitor(copy3);
9775
+ didShortCut = context.shortcut ?? false;
9776
+ const continues = context.continue ?? true;
9777
+ if (continues && !didShortCut) {
9778
+ for (const [key, value] of Object.entries(copy3)) {
9779
+ if (didShortCut) {
9780
+ return copy3;
9781
+ }
9782
+ copy3[key] = this.safeObjectVisit(value, (obj) => recurse(obj));
9783
+ }
9784
+ }
9785
+ return mapper(copy3);
9786
+ };
9787
+ return recurse(startObject);
9779
9788
  }
9780
- visitNode(curObject, nodeCallBacks) {
9781
- let callback;
9782
- const casted = curObject;
9783
- if (casted.type) {
9784
- callback = nodeCallBacks[casted.type];
9785
- }
9786
- let shouldIterate = true;
9787
- if (callback) {
9788
- shouldIterate = callback(curObject);
9789
- }
9790
- if (shouldIterate) {
9791
- for (const value of Object.values(curObject)) {
9792
- this.safeObjectVisit(value, (obj) => this.visitNode(obj, nodeCallBacks));
9789
+ /**
9790
+ * Visitor that visits all objects. Visits deeper objects first.
9791
+ */
9792
+ visitObject(startObject, visitor, preVisitor = () => ({})) {
9793
+ let didShortCut = false;
9794
+ const recurse = (curObject) => {
9795
+ const context = preVisitor(curObject);
9796
+ didShortCut = context.shortcut ?? false;
9797
+ const continues = context.continue ?? true;
9798
+ if (continues && !didShortCut) {
9799
+ for (const value of Object.values(curObject)) {
9800
+ if (didShortCut) {
9801
+ return;
9802
+ }
9803
+ this.safeObjectVisit(value, (obj) => recurse(obj));
9804
+ }
9793
9805
  }
9794
- }
9806
+ visitor(curObject);
9807
+ };
9808
+ recurse(startObject);
9809
+ }
9810
+ transformNode(startObject, nodeCallBacks) {
9811
+ const transformWrapper = (curObject) => {
9812
+ const casted = curObject;
9813
+ if (casted.type) {
9814
+ const ogFunc = nodeCallBacks[casted.type]?.transform;
9815
+ if (ogFunc) {
9816
+ return ogFunc(casted);
9817
+ }
9818
+ }
9819
+ return curObject;
9820
+ };
9821
+ const preTransformWrapper = (curObject) => {
9822
+ const casted = curObject;
9823
+ if (casted.type) {
9824
+ const ogFunc = nodeCallBacks[casted.type]?.preVisitor;
9825
+ if (ogFunc) {
9826
+ return ogFunc(casted);
9827
+ }
9828
+ }
9829
+ return {};
9830
+ };
9831
+ return this.transformObject(startObject, transformWrapper, preTransformWrapper);
9832
+ }
9833
+ visitNode(startObject, nodeCallBacks) {
9834
+ const visitWrapper = (curObject) => {
9835
+ const casted = curObject;
9836
+ if (casted.type) {
9837
+ const ogFunc = nodeCallBacks[casted.type]?.visitor;
9838
+ if (ogFunc) {
9839
+ ogFunc(casted);
9840
+ }
9841
+ }
9842
+ };
9843
+ const preVisitWrapper = (curObject) => {
9844
+ const casted = curObject;
9845
+ if (casted.type) {
9846
+ const ogFunc = nodeCallBacks[casted.type]?.preVisitor;
9847
+ if (ogFunc) {
9848
+ return ogFunc(casted);
9849
+ }
9850
+ }
9851
+ return {};
9852
+ };
9853
+ this.visitObject(startObject, visitWrapper, preVisitWrapper);
9854
+ }
9855
+ traverseNodes(currentNode, traverse) {
9856
+ let didShortCut = false;
9857
+ const recurse = (curNode) => {
9858
+ const traverser = traverse[curNode.type];
9859
+ if (traverser) {
9860
+ const { next, shortcut } = traverser(curNode);
9861
+ didShortCut = shortcut ?? false;
9862
+ if (!didShortCut) {
9863
+ for (const node of next ?? []) {
9864
+ if (didShortCut) {
9865
+ return;
9866
+ }
9867
+ recurse(node);
9868
+ }
9869
+ }
9870
+ }
9871
+ };
9872
+ recurse(currentNode);
9795
9873
  }
9796
9874
  };
9797
9875
  var TransformerSubType = class extends TransformerType {
9798
- // Public visitObjects(curObject: object, visitor: (current: object) => void): void {
9799
- // for (const value of Object.values(curObject)) {
9800
- // this.safeObjectVisit(value, obj => this.visitObjects(obj, visitor));
9801
- // }
9802
- // }
9803
- transformNodeSpecific(curObject, nodeCallBacks, nodeSpecificCallBacks = {}) {
9804
- let continueCheck;
9805
- let transformation;
9806
- const casted = curObject;
9807
- if (casted.type && casted.subType) {
9808
- const specific = nodeSpecificCallBacks[casted.type];
9809
- if (specific) {
9810
- continueCheck = specific[casted.subType]?.continue;
9811
- transformation = specific[casted.subType]?.transform;
9812
- }
9813
- }
9814
- let shouldContinue = true;
9815
- if (continueCheck) {
9816
- shouldContinue = continueCheck(curObject);
9817
- }
9818
- if (!shouldContinue) {
9819
- return curObject;
9820
- }
9821
- const copy3 = { ...curObject };
9822
- for (const [key, value] of Object.entries(copy3)) {
9823
- copy3[key] = this.safeObjectVisit(value, (obj) => this.transformNodeSpecific(obj, nodeCallBacks, nodeSpecificCallBacks));
9824
- }
9825
- if (transformation) {
9826
- return transformation(copy3);
9827
- }
9828
- return copy3;
9876
+ transformNodeSpecific(startObject, nodeCallBacks, nodeSpecificCallBacks) {
9877
+ const transformWrapper = (curObject) => {
9878
+ let ogTransform;
9879
+ const casted = curObject;
9880
+ if (casted.type && casted.subType) {
9881
+ const specific = nodeSpecificCallBacks[casted.type];
9882
+ if (specific) {
9883
+ ogTransform = specific[casted.subType]?.transform;
9884
+ }
9885
+ if (!ogTransform) {
9886
+ ogTransform = nodeCallBacks[casted.type]?.transform;
9887
+ }
9888
+ }
9889
+ return ogTransform ? ogTransform(casted) : curObject;
9890
+ };
9891
+ const preVisitWrapper = (curObject) => {
9892
+ let ogPreVisit;
9893
+ const casted = curObject;
9894
+ if (casted.type && casted.subType) {
9895
+ const specific = nodeSpecificCallBacks[casted.type];
9896
+ if (specific) {
9897
+ ogPreVisit = specific[casted.subType]?.preVisitor;
9898
+ }
9899
+ if (!ogPreVisit) {
9900
+ ogPreVisit = nodeCallBacks[casted.type]?.preVisitor;
9901
+ }
9902
+ }
9903
+ return ogPreVisit ? ogPreVisit(casted) : curObject;
9904
+ };
9905
+ return this.transformObject(startObject, transformWrapper, preVisitWrapper);
9829
9906
  }
9830
9907
  /**
9831
9908
  * When both nodeCallBack and NodeSpecific callBack are matched, will only look at nodeSpecifCallback
9832
9909
  */
9833
- visitNodeSpecific(curObject, nodeCallBacks, nodeSpecificCallBacks = {}) {
9834
- let callback;
9835
- const casted = curObject;
9836
- if (casted.type && casted.subType) {
9837
- const specific = nodeSpecificCallBacks[casted.type];
9838
- if (specific) {
9839
- callback = specific[casted.subType];
9910
+ visitNodeSpecific(startObject, nodeCallBacks, nodeSpecificCallBacks) {
9911
+ const visitWrapper = (curObject) => {
9912
+ let ogTransform;
9913
+ const casted = curObject;
9914
+ if (casted.type && casted.subType) {
9915
+ const specific = nodeSpecificCallBacks[casted.type];
9916
+ if (specific) {
9917
+ ogTransform = specific[casted.subType]?.visitor;
9918
+ }
9919
+ if (!ogTransform) {
9920
+ ogTransform = nodeCallBacks[casted.type]?.visitor;
9921
+ }
9840
9922
  }
9841
- }
9842
- if (!callback && casted.type) {
9843
- callback = nodeCallBacks[curObject.type];
9844
- }
9845
- let shouldIterate = true;
9846
- if (callback) {
9847
- shouldIterate = callback(curObject) ?? true;
9848
- }
9849
- if (shouldIterate) {
9850
- for (const value of Object.values(curObject)) {
9851
- this.safeObjectVisit(value, (obj) => this.visitNodeSpecific(obj, nodeCallBacks, nodeSpecificCallBacks));
9923
+ if (ogTransform) {
9924
+ ogTransform(casted);
9852
9925
  }
9853
- }
9926
+ };
9927
+ const preVisitWrapper = (curObject) => {
9928
+ let ogPreVisit;
9929
+ const casted = curObject;
9930
+ if (casted.type && casted.subType) {
9931
+ const specific = nodeSpecificCallBacks[casted.type];
9932
+ if (specific) {
9933
+ ogPreVisit = specific[casted.subType]?.preVisitor;
9934
+ }
9935
+ if (!ogPreVisit) {
9936
+ ogPreVisit = nodeCallBacks[casted.type]?.preVisitor;
9937
+ }
9938
+ }
9939
+ return ogPreVisit ? ogPreVisit(casted) : curObject;
9940
+ };
9941
+ this.visitObject(startObject, visitWrapper, preVisitWrapper);
9942
+ }
9943
+ traverseSubNodes(currentNode, traverseNode, traverseSubNode) {
9944
+ let didShortCut = false;
9945
+ const recurse = (curNode) => {
9946
+ let traverser;
9947
+ const subObj = traverseSubNode[curNode.type];
9948
+ if (subObj) {
9949
+ traverser = subObj[curNode.subType];
9950
+ }
9951
+ if (!traverser) {
9952
+ traverser = traverseNode[curNode.type];
9953
+ }
9954
+ if (traverser) {
9955
+ const { next, shortcut } = traverser(curNode);
9956
+ didShortCut = shortcut ?? false;
9957
+ if (!didShortCut) {
9958
+ for (const node of next ?? []) {
9959
+ if (didShortCut) {
9960
+ return;
9961
+ }
9962
+ recurse(node);
9963
+ }
9964
+ }
9965
+ }
9966
+ };
9967
+ recurse(currentNode);
9854
9968
  }
9855
9969
  };
9856
9970
 
@@ -11364,12 +11478,12 @@ var CommonIRIs;
11364
11478
  CommonIRIs2["NIL"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#nil";
11365
11479
  CommonIRIs2["TYPE"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
11366
11480
  })(CommonIRIs || (CommonIRIs = {}));
11367
- var TransformerSparql11 = class extends TransformerSubType {
11481
+ var AstTransformer = class extends TransformerSubType {
11368
11482
  };
11369
11483
 
11370
11484
  // ../../packages/rules-sparql-1-1/lib/validation/validators.js
11371
11485
  var F = new AstFactory();
11372
- var transformer = new TransformerSparql11();
11486
+ var transformer = new AstTransformer();
11373
11487
  function getAggregatesOfExpression(expression2) {
11374
11488
  if (F.isExpressionAggregate(expression2)) {
11375
11489
  return [expression2];
@@ -11448,77 +11562,69 @@ function queryIsGood(query2) {
11448
11562
  }
11449
11563
  }
11450
11564
  }
11565
+ function notUndefined(some2) {
11566
+ return some2 !== void 0;
11567
+ }
11451
11568
  function findPatternBoundedVars(iter, boundedVars) {
11452
- if (F.isQuery(iter) || F.isUpdate(iter)) {
11453
- if (F.isQuerySelect(iter) || F.isQueryDescribe(iter)) {
11454
- if (iter.where && iter.variables.some((x) => F.isWildcard(x))) {
11455
- findPatternBoundedVars(iter.where, boundedVars);
11456
- } else {
11457
- for (const v of iter.variables) {
11458
- findPatternBoundedVars(v, boundedVars);
11459
- }
11460
- }
11461
- if (iter.solutionModifiers.group) {
11462
- const grouping = iter.solutionModifiers.group;
11463
- for (const g of grouping.groupings) {
11464
- if ("variable" in g) {
11465
- findPatternBoundedVars(g.variable, boundedVars);
11466
- }
11467
- }
11468
- }
11469
- if (iter.values?.values && iter.values.values.length > 0) {
11470
- const values3 = iter.values.values;
11471
- for (const v of Object.keys(values3[0])) {
11569
+ transformer.traverseSubNodes(iter, {
11570
+ query: (op) => ({ next: [
11571
+ op.solutionModifiers.group
11572
+ ].filter(notUndefined) }),
11573
+ triple: (op) => ({ next: [op.subject, op.predicate, op.object] }),
11574
+ path: (op) => ({ next: op.items }),
11575
+ tripleCollection: (op) => ({ next: [op.identifier, ...op.triples] })
11576
+ }, {
11577
+ query: {
11578
+ select: (op) => ({ next: [
11579
+ ...op.variables.some((x) => F.isWildcard(x)) ? [op.where] : op.variables,
11580
+ op.solutionModifiers.group,
11581
+ op.values
11582
+ ].filter(notUndefined) }),
11583
+ describe: (op) => ({ next: [
11584
+ ...op.variables.some((x) => F.isWildcard(x)) ? [op.where] : op.variables,
11585
+ op.solutionModifiers.group,
11586
+ op.values
11587
+ ].filter(notUndefined) })
11588
+ },
11589
+ solutionModifier: {
11590
+ group: (op) => ({
11591
+ next: op.groupings.filter((g) => "variable" in g).map((x) => x.variable)
11592
+ }),
11593
+ having: (op) => ({ next: op.having }),
11594
+ order: (op) => ({ next: op.orderDefs.map((x) => x.expression) })
11595
+ },
11596
+ pattern: {
11597
+ values: (op) => {
11598
+ for (const v of Object.keys(op.values.at(0) ?? {})) {
11472
11599
  boundedVars.add(v);
11473
11600
  }
11601
+ return {};
11602
+ },
11603
+ bgp: (op) => ({ next: op.triples }),
11604
+ group: (op) => ({ next: op.patterns }),
11605
+ union: (op) => ({ next: op.patterns }),
11606
+ optional: (op) => ({ next: op.patterns }),
11607
+ service: (op) => ({ next: [op.name, ...op.patterns] }),
11608
+ bind: (op) => ({ next: [op.variable] }),
11609
+ graph: (op) => ({ next: [op.name, ...op.patterns] }),
11610
+ minus: (op) => ({ next: op.patterns.slice(0, 1) })
11611
+ },
11612
+ term: {
11613
+ variable: (op) => {
11614
+ boundedVars.add(op.value);
11615
+ return {};
11474
11616
  }
11475
11617
  }
11476
- } else if (F.isTerm(iter)) {
11477
- if (F.isTermVariable(iter)) {
11478
- boundedVars.add(iter.value);
11479
- }
11480
- } else if (F.isTriple(iter)) {
11481
- findPatternBoundedVars(iter.subject, boundedVars);
11482
- findPatternBoundedVars(iter.predicate, boundedVars);
11483
- findPatternBoundedVars(iter.object, boundedVars);
11484
- } else if (F.isPath(iter)) {
11485
- if (!F.isTerm(iter)) {
11486
- for (const item of iter.items) {
11487
- findPatternBoundedVars(item, boundedVars);
11488
- }
11489
- }
11490
- } else if (F.isTripleCollection(iter) || F.isPatternBgp(iter)) {
11491
- for (const triple of iter.triples) {
11492
- findPatternBoundedVars(triple, boundedVars);
11493
- }
11494
- } else if (F.isPatternGroup(iter) || F.isPatternUnion(iter) || F.isPatternOptional(iter) || F.isPatternService(iter)) {
11495
- for (const pattern of iter.patterns) {
11496
- findPatternBoundedVars(pattern, boundedVars);
11497
- }
11498
- if (F.isPatternService(iter)) {
11499
- findPatternBoundedVars(iter.name, boundedVars);
11500
- }
11501
- } else if (F.isPatternBind(iter)) {
11502
- findPatternBoundedVars(iter.variable, boundedVars);
11503
- } else if (F.isPatternValues(iter)) {
11504
- for (const variable of Object.keys(iter.values.at(0) ?? {})) {
11505
- boundedVars.add(variable);
11506
- }
11507
- } else if (F.isPatternGraph(iter)) {
11508
- findPatternBoundedVars(iter.name, boundedVars);
11509
- for (const pattern of iter.patterns) {
11510
- findPatternBoundedVars(pattern, boundedVars);
11511
- }
11512
- }
11618
+ });
11513
11619
  }
11514
11620
  function checkNote13(patterns) {
11515
11621
  for (const [index, pattern] of patterns.entries()) {
11516
11622
  if (F.isPatternBind(pattern) && index > 0 && F.isPatternBgp(patterns[index - 1])) {
11517
11623
  const bgp = patterns[index - 1];
11518
11624
  const variables = [];
11519
- transformer.visitNodeSpecific(bgp, {}, { term: { variable: (var_2) => {
11625
+ transformer.visitNodeSpecific(bgp, {}, { term: { variable: { visitor: (var_2) => {
11520
11626
  variables.push(var_2);
11521
- } } });
11627
+ } } } });
11522
11628
  if (variables.some((var_2) => var_2.value === pattern.variable.value)) {
11523
11629
  throw new Error(`Variable used to bind is already bound (?${pattern.variable.value})`);
11524
11630
  }
@@ -11544,12 +11650,12 @@ function updateNoReuseBlankNodeLabels(updateQuery) {
11544
11650
  const operation = update2.operation;
11545
11651
  if (operation.subType === "insertdata") {
11546
11652
  const blankNodesHere = /* @__PURE__ */ new Set();
11547
- transformer.visitNodeSpecific(operation, {}, { term: { blankNode: (blankNode2) => {
11653
+ transformer.visitNodeSpecific(operation, {}, { term: { blankNode: { visitor: (blankNode2) => {
11548
11654
  blankNodesHere.add(blankNode2.label);
11549
11655
  if (blankLabelsUsedInInsertData.has(blankNode2.label)) {
11550
11656
  throw new Error("Detected reuse blank node across different INSERT DATA clauses");
11551
11657
  }
11552
- } } });
11658
+ } } } });
11553
11659
  for (const blankNode2 of blankNodesHere) {
11554
11660
  blankLabelsUsedInInsertData.add(blankNode2);
11555
11661
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@traqula/parser-sparql-1-1-adjust",
3
3
  "type": "module",
4
- "version": "0.0.16",
4
+ "version": "0.0.17",
5
5
  "description": "SPARQL 1.1 + ADJUST parser",
6
6
  "lsd:module": true,
7
7
  "license": "MIT",
@@ -51,16 +51,16 @@
51
51
  "spec:earl": "yarn spec:earl:query && yarn spec:earl:update"
52
52
  },
53
53
  "dependencies": {
54
- "@traqula/core": "^0.0.16",
55
- "@traqula/parser-sparql-1-1": "^0.0.16",
56
- "@traqula/rules-sparql-1-1": "^0.0.16",
57
- "@traqula/rules-sparql-1-1-adjust": "^0.0.16"
54
+ "@traqula/core": "^0.0.17",
55
+ "@traqula/parser-sparql-1-1": "^0.0.17",
56
+ "@traqula/rules-sparql-1-1": "^0.0.17",
57
+ "@traqula/rules-sparql-1-1-adjust": "^0.0.17"
58
58
  },
59
59
  "devDependencies": {
60
60
  "@rdfjs/types": "^2.0.0",
61
- "@traqula/test-utils": "^0.0.16",
61
+ "@traqula/test-utils": "^0.0.17",
62
62
  "rdf-data-factory": "^2.0.1",
63
63
  "rdf-test-suite": "^2.0.0"
64
64
  },
65
- "gitHead": "2de4ee608a2337bed63ecc5cf336b1a57c39e45f"
65
+ "gitHead": "e4c1ef096da34d42ff86ac01d9affd428754c0b1"
66
66
  }