@atscript/core 0.1.98 → 0.1.99
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/dist/index.cjs +1662 -1237
- package/dist/index.d.ts +117 -9
- package/dist/index.mjs +1655 -1238
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -400,6 +400,9 @@ var Token = class Token {
|
|
|
400
400
|
...replace
|
|
401
401
|
});
|
|
402
402
|
}
|
|
403
|
+
/** The underlying lexical token (for re-iterating its children). */ get lexicalToken() {
|
|
404
|
+
return this._data;
|
|
405
|
+
}
|
|
403
406
|
get text() {
|
|
404
407
|
return this._data.text || "";
|
|
405
408
|
}
|
|
@@ -423,6 +426,9 @@ var Token = class Token {
|
|
|
423
426
|
get isAnnotation() {
|
|
424
427
|
return this._data.type === "annotation";
|
|
425
428
|
}
|
|
429
|
+
/** The parsed tree of a backtick argument — whichever of the three it was parsed as. */ get backtickNode() {
|
|
430
|
+
return this.queryNode ?? this.exprNode ?? this.orderNode;
|
|
431
|
+
}
|
|
426
432
|
constructor(_data) {
|
|
427
433
|
_define_property$13(this, "_data", void 0);
|
|
428
434
|
/**
|
|
@@ -457,6 +463,12 @@ var Token = class Token {
|
|
|
457
463
|
/**
|
|
458
464
|
* Parsed query expression tree (for query-type annotation arguments)
|
|
459
465
|
*/ _define_property$13(this, "queryNode", void 0);
|
|
466
|
+
/**
|
|
467
|
+
* Parsed arithmetic expression (for expr-type annotation arguments)
|
|
468
|
+
*/ _define_property$13(this, "exprNode", void 0);
|
|
469
|
+
/**
|
|
470
|
+
* Parsed ordering (for order-type annotation arguments)
|
|
471
|
+
*/ _define_property$13(this, "orderNode", void 0);
|
|
460
472
|
this._data = _data;
|
|
461
473
|
}
|
|
462
474
|
};
|
|
@@ -815,15 +827,20 @@ function _define_property$9(obj, key, value) {
|
|
|
815
827
|
}
|
|
816
828
|
var SemanticQueryNode = class extends SemanticNode {
|
|
817
829
|
registerAtDocument(doc) {
|
|
818
|
-
this.
|
|
830
|
+
for (const ref of this.fieldRefs()) ref.queryArgToken = this.sourceToken;
|
|
819
831
|
this.expression.registerAtDocument(doc);
|
|
820
832
|
}
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
833
|
+
/** Every field ref of the predicate (both sides of comparisons), in source order. */ fieldRefs() {
|
|
834
|
+
const refs = [];
|
|
835
|
+
const walk = (expr) => {
|
|
836
|
+
if ("left" in expr) {
|
|
837
|
+
refs.push(expr.left);
|
|
838
|
+
const right = expr.right;
|
|
839
|
+
if (right && "fieldRef" in right) refs.push(right);
|
|
840
|
+
} else if ("operands" in expr) for (const operand of expr.operands) walk(operand);
|
|
841
|
+
};
|
|
842
|
+
walk(this.expression);
|
|
843
|
+
return refs;
|
|
827
844
|
}
|
|
828
845
|
constructor() {
|
|
829
846
|
super("query"), _define_property$9(this, "expression", void 0), _define_property$9(this, "sourceToken", void 0);
|
|
@@ -878,6 +895,74 @@ var SemanticQueryValueListNode = class extends SemanticNode {
|
|
|
878
895
|
super("query-value-list"), _define_property$9(this, "values", void 0);
|
|
879
896
|
}
|
|
880
897
|
};
|
|
898
|
+
/** Set `queryArgToken` on every field-ref leaf and register the leaves at the document. */ function registerFieldRefs(refs, sourceToken, doc) {
|
|
899
|
+
for (const ref of refs) {
|
|
900
|
+
ref.queryArgToken = sourceToken;
|
|
901
|
+
ref.registerAtDocument(doc);
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
/** Root of a parsed `expr` argument (closed arithmetic over field refs). */ var SemanticExprNode = class extends SemanticNode {
|
|
905
|
+
/** Every field-ref leaf of the expression, in source order. */ fieldRefs() {
|
|
906
|
+
const refs = [];
|
|
907
|
+
collectExprFieldRefs(this.expression, refs);
|
|
908
|
+
return refs;
|
|
909
|
+
}
|
|
910
|
+
registerAtDocument(doc) {
|
|
911
|
+
registerFieldRefs(this.fieldRefs(), this.sourceToken, doc);
|
|
912
|
+
}
|
|
913
|
+
constructor() {
|
|
914
|
+
super("query-expr"), _define_property$9(this, "expression", void 0), _define_property$9(this, "sourceToken", void 0);
|
|
915
|
+
}
|
|
916
|
+
};
|
|
917
|
+
function collectExprFieldRefs(node, out) {
|
|
918
|
+
switch (node.entity) {
|
|
919
|
+
case "query-field-ref":
|
|
920
|
+
out.push(node);
|
|
921
|
+
break;
|
|
922
|
+
case "query-expr-binary":
|
|
923
|
+
collectExprFieldRefs(node.left, out);
|
|
924
|
+
collectExprFieldRefs(node.right, out);
|
|
925
|
+
break;
|
|
926
|
+
case "query-expr-unary":
|
|
927
|
+
collectExprFieldRefs(node.operand, out);
|
|
928
|
+
break;
|
|
929
|
+
case "query-expr-call":
|
|
930
|
+
for (const arg of node.args) collectExprFieldRefs(arg, out);
|
|
931
|
+
break;
|
|
932
|
+
default:
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
var SemanticExprBinaryNode = class extends SemanticNode {
|
|
936
|
+
constructor() {
|
|
937
|
+
super("query-expr-binary"), _define_property$9(this, "op", void 0), _define_property$9(this, "left", void 0), _define_property$9(this, "right", void 0);
|
|
938
|
+
}
|
|
939
|
+
};
|
|
940
|
+
var SemanticExprUnaryNode = class extends SemanticNode {
|
|
941
|
+
constructor() {
|
|
942
|
+
super("query-expr-unary"), _define_property$9(this, "op", "neg"), _define_property$9(this, "operand", void 0);
|
|
943
|
+
}
|
|
944
|
+
};
|
|
945
|
+
var SemanticExprCallNode = class extends SemanticNode {
|
|
946
|
+
constructor() {
|
|
947
|
+
super("query-expr-call"), _define_property$9(this, "fn", "coalesce"), _define_property$9(this, "args", void 0);
|
|
948
|
+
}
|
|
949
|
+
};
|
|
950
|
+
var SemanticExprNumberNode = class extends SemanticNode {
|
|
951
|
+
constructor() {
|
|
952
|
+
super("query-expr-number"), _define_property$9(this, "valueToken", void 0), _define_property$9(this, "value", void 0);
|
|
953
|
+
}
|
|
954
|
+
};
|
|
955
|
+
/** Root of a parsed `order` argument: `key (asc|desc)?, …`. */ var SemanticOrderNode = class extends SemanticNode {
|
|
956
|
+
/** Every order key, in source order. */ fieldRefs() {
|
|
957
|
+
return this.items.map((i) => i.ref);
|
|
958
|
+
}
|
|
959
|
+
registerAtDocument(doc) {
|
|
960
|
+
registerFieldRefs(this.fieldRefs(), this.sourceToken, doc);
|
|
961
|
+
}
|
|
962
|
+
constructor() {
|
|
963
|
+
super("query-order"), _define_property$9(this, "items", void 0), _define_property$9(this, "sourceToken", void 0);
|
|
964
|
+
}
|
|
965
|
+
};
|
|
881
966
|
|
|
882
967
|
//#endregion
|
|
883
968
|
//#region packages/core/src/parser/nodes/index.ts
|
|
@@ -953,6 +1038,14 @@ function _define_property$8(obj, key, value) {
|
|
|
953
1038
|
else obj[key] = value;
|
|
954
1039
|
return obj;
|
|
955
1040
|
}
|
|
1041
|
+
/** Whether an annotation argument type is written in backticks (`query`, `expr`, `order`). */ function isBacktickArgType(type) {
|
|
1042
|
+
return type === "query" || type === "expr" || type === "order";
|
|
1043
|
+
}
|
|
1044
|
+
const BACKTICK_TYPE_MESSAGES = {
|
|
1045
|
+
query: "query expression expected (use backticks).",
|
|
1046
|
+
expr: "expression expected (use backticks).",
|
|
1047
|
+
order: "order list expected (use backticks)."
|
|
1048
|
+
};
|
|
956
1049
|
var AnnotationSpec = class {
|
|
957
1050
|
get arguments() {
|
|
958
1051
|
if (!this.config.argument) return [];
|
|
@@ -963,7 +1056,7 @@ var AnnotationSpec = class {
|
|
|
963
1056
|
return this.arguments.map((arg, index) => {
|
|
964
1057
|
const placeholderIndex = index + 1;
|
|
965
1058
|
const defaultValue = this.getDefaultValueForType(arg.name, arg.type);
|
|
966
|
-
const quote = arg.type === "string" ? `'` : arg.type
|
|
1059
|
+
const quote = arg.type === "string" ? `'` : isBacktickArgType(arg.type) ? "`" : "";
|
|
967
1060
|
return `${quote}\${${placeholderIndex}:${defaultValue}}${quote}`;
|
|
968
1061
|
}).join(", ");
|
|
969
1062
|
}
|
|
@@ -973,7 +1066,9 @@ var AnnotationSpec = class {
|
|
|
973
1066
|
case "number": return tokenType === "number" ? void 0 : "number expected.";
|
|
974
1067
|
case "boolean": return tokenType === "identifier" ? void 0 : "boolean expected.";
|
|
975
1068
|
case "ref": return tokenType === "identifier" ? void 0 : "type reference expected.";
|
|
976
|
-
case "query":
|
|
1069
|
+
case "query":
|
|
1070
|
+
case "expr":
|
|
1071
|
+
case "order": return tokenType === "query" ? void 0 : BACKTICK_TYPE_MESSAGES[type];
|
|
977
1072
|
default: return `unknown type "${type}".`;
|
|
978
1073
|
}
|
|
979
1074
|
}
|
|
@@ -1095,6 +1190,8 @@ var AnnotationSpec = class {
|
|
|
1095
1190
|
case "boolean": return "true";
|
|
1096
1191
|
case "ref": return "TypeName";
|
|
1097
1192
|
case "query": return "field = value";
|
|
1193
|
+
case "expr": return "field + 1";
|
|
1194
|
+
case "order": return "field asc";
|
|
1098
1195
|
default: return "";
|
|
1099
1196
|
}
|
|
1100
1197
|
}
|
|
@@ -1417,421 +1514,30 @@ async function loadConfig(configPath, forceFormat) {
|
|
|
1417
1514
|
}
|
|
1418
1515
|
|
|
1419
1516
|
//#endregion
|
|
1420
|
-
//#region packages/core/src/
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
*/ function getLegacyDbQueryScope(queryArgToken) {
|
|
1431
|
-
const owner = queryArgToken.parentNode;
|
|
1432
|
-
if (!owner) return;
|
|
1433
|
-
switch (queryArgToken.annotationRef?.text.slice(1)) {
|
|
1434
|
-
case "db.view.filter": return getViewFilterScope(owner);
|
|
1435
|
-
case "db.view.joins": return getViewJoinsScope(owner, queryArgToken);
|
|
1436
|
-
case "db.rel.filter": return getRelFilterScope(owner);
|
|
1437
|
-
default: return;
|
|
1438
|
-
}
|
|
1517
|
+
//#region packages/core/src/parser/iterator.ts
|
|
1518
|
+
function _define_property$7(obj, key, value) {
|
|
1519
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
1520
|
+
value,
|
|
1521
|
+
enumerable: true,
|
|
1522
|
+
configurable: true,
|
|
1523
|
+
writable: true
|
|
1524
|
+
});
|
|
1525
|
+
else obj[key] = value;
|
|
1526
|
+
return obj;
|
|
1439
1527
|
}
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
const allowedTypes = [entryTypeName];
|
|
1444
|
-
const joinsAnnotations = owner.annotations?.filter((a) => a.name === "db.view.joins");
|
|
1445
|
-
if (joinsAnnotations) {
|
|
1446
|
-
for (const join of joinsAnnotations) if (join.args[0]) allowedTypes.push(join.args[0].text);
|
|
1528
|
+
var NodeIterator = class NodeIterator {
|
|
1529
|
+
get index() {
|
|
1530
|
+
return this.i;
|
|
1447
1531
|
}
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
unqualifiedTarget: entryTypeName
|
|
1451
|
-
};
|
|
1452
|
-
}
|
|
1453
|
-
function getViewJoinsScope(owner, queryArgToken) {
|
|
1454
|
-
const entryTypeName = (owner.annotations?.filter((a) => a.name === "db.view.for"))?.[0]?.args[0]?.text;
|
|
1455
|
-
if (!entryTypeName) return;
|
|
1456
|
-
const joinTargetName = (owner.annotations?.find((a) => a.name === "db.view.joins" && a.args.includes(queryArgToken)))?.args[0]?.text;
|
|
1457
|
-
if (!joinTargetName) return;
|
|
1458
|
-
return {
|
|
1459
|
-
allowedTypes: [joinTargetName, entryTypeName],
|
|
1460
|
-
unqualifiedTarget: entryTypeName
|
|
1461
|
-
};
|
|
1462
|
-
}
|
|
1463
|
-
function getRelFilterScope(owner) {
|
|
1464
|
-
let def = owner.getDefinition();
|
|
1465
|
-
if (isArray(def)) def = def?.getDefinition();
|
|
1466
|
-
if (!isRef(def)) return;
|
|
1467
|
-
const targetTypeName = def.id;
|
|
1468
|
-
const allowedTypes = [targetTypeName];
|
|
1469
|
-
const viaAnnotation = owner.annotations?.find((a) => a.name === "db.rel.via");
|
|
1470
|
-
if (viaAnnotation?.args[0]) allowedTypes.push(viaAnnotation.args[0].text);
|
|
1471
|
-
return {
|
|
1472
|
-
allowedTypes,
|
|
1473
|
-
unqualifiedTarget: targetTypeName
|
|
1474
|
-
};
|
|
1475
|
-
}
|
|
1476
|
-
|
|
1477
|
-
//#endregion
|
|
1478
|
-
//#region packages/core/src/lsp/field-refs.ts
|
|
1479
|
-
function getArgSpec(argToken, doc) {
|
|
1480
|
-
const annotationRef = argToken.annotationRef;
|
|
1481
|
-
if (!annotationRef || typeof argToken.index !== "number") return;
|
|
1482
|
-
return doc.resolveAnnotation(annotationRef.text.slice(1))?.arguments[argToken.index];
|
|
1483
|
-
}
|
|
1484
|
-
/**
|
|
1485
|
-
* Field scope of an annotation argument: the answer of the argument spec's `fieldScope`
|
|
1486
|
-
* hook when it declares one, otherwise the deprecated built-in `@db.*` query rules.
|
|
1487
|
-
*/ function getQueryScope(argToken, doc) {
|
|
1488
|
-
const argSpec = getArgSpec(argToken, doc);
|
|
1489
|
-
return argSpec?.fieldScope ? argSpec.fieldScope(argToken, doc) : getLegacyDbQueryScope(argToken);
|
|
1490
|
-
}
|
|
1491
|
-
/**
|
|
1492
|
-
* Type a field-path `string` argument names a field of: the `unqualifiedTarget` of the
|
|
1493
|
-
* argument spec's `fieldScope`. `undefined` for any other token.
|
|
1494
|
-
*/ function getFieldPathTarget(argToken, doc) {
|
|
1495
|
-
if (argToken.type !== "text") return;
|
|
1496
|
-
const argSpec = getArgSpec(argToken, doc);
|
|
1497
|
-
if (argSpec?.type !== "string" || !argSpec.fieldScope) return;
|
|
1498
|
-
return argSpec.fieldScope(argToken, doc)?.unqualifiedTarget ?? void 0;
|
|
1499
|
-
}
|
|
1500
|
-
/**
|
|
1501
|
-
* Resolve a (possibly dotted) field path of a type to the property it names and the
|
|
1502
|
-
* document that declares that property.
|
|
1503
|
-
*/ function resolveFieldPath(doc, typeName, segment) {
|
|
1504
|
-
const chain = segment.path.split(".");
|
|
1505
|
-
const [name] = chain.splice(-1);
|
|
1506
|
-
const parent = doc.unwindType(typeName, chain);
|
|
1507
|
-
if (!parent || !(isStructure(parent.def) || isInterface(parent.def))) return;
|
|
1508
|
-
const prop = parent.def.props.get(name);
|
|
1509
|
-
return prop && {
|
|
1510
|
-
typeName,
|
|
1511
|
-
range: segment.range,
|
|
1512
|
-
prop,
|
|
1513
|
-
doc: parent.doc
|
|
1514
|
-
};
|
|
1515
|
-
}
|
|
1516
|
-
/**
|
|
1517
|
-
* Segments of a field-path string argument (`'address.city'` → `address`, `city`), with
|
|
1518
|
-
* ranges that exclude the quotes. A string spanning several lines is a single segment.
|
|
1519
|
-
*/ function fieldPathSegments(argToken) {
|
|
1520
|
-
const { text, range } = argToken;
|
|
1521
|
-
if (range.start.line !== range.end.line) return [{
|
|
1522
|
-
path: text,
|
|
1523
|
-
range
|
|
1524
|
-
}];
|
|
1525
|
-
const line = range.start.line;
|
|
1526
|
-
const contentStart = range.start.character + 1;
|
|
1527
|
-
const segments = [];
|
|
1528
|
-
let start = 0;
|
|
1529
|
-
for (const part of text.split(".")) {
|
|
1530
|
-
const end = start + part.length;
|
|
1531
|
-
segments.push({
|
|
1532
|
-
path: text.slice(0, end),
|
|
1533
|
-
range: {
|
|
1534
|
-
start: {
|
|
1535
|
-
line,
|
|
1536
|
-
character: contentStart + start
|
|
1537
|
-
},
|
|
1538
|
-
end: {
|
|
1539
|
-
line,
|
|
1540
|
-
character: contentStart + end
|
|
1541
|
-
}
|
|
1542
|
-
}
|
|
1543
|
-
});
|
|
1544
|
-
start = end + 1;
|
|
1532
|
+
get lastNode() {
|
|
1533
|
+
return this.nodes[this.nodes.length - 1];
|
|
1545
1534
|
}
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
/** The segment under `character`, or the last segment when `character` is omitted. */ function fieldPathSegmentAt(argToken, character) {
|
|
1549
|
-
const segments = fieldPathSegments(argToken);
|
|
1550
|
-
return (character === void 0 ? void 0 : segments.find((s) => s.range.end.character >= character)) ?? segments[segments.length - 1];
|
|
1551
|
-
}
|
|
1552
|
-
/**
|
|
1553
|
-
* Resolve a field reference token to the property it names:
|
|
1554
|
-
* - a field ref inside a `query` argument (`amount`, `Customer.name` in backticks);
|
|
1555
|
-
* - a `string` argument whose spec declares `fieldScope`: a (dotted) field path of the
|
|
1556
|
-
* scope's `unqualifiedTarget`. With `character` (a cursor column), the path resolves
|
|
1557
|
-
* only up to the segment under the cursor — `'address.city'` with the cursor on
|
|
1558
|
-
* `address` resolves `address` — and `range` is that segment's range.
|
|
1559
|
-
*
|
|
1560
|
-
* Returns `undefined` for any other token, or when the scope or the path does not resolve.
|
|
1561
|
-
*/ function resolveFieldRefAt(token, doc, character) {
|
|
1562
|
-
if (isQueryFieldRef(token.parentNode)) {
|
|
1563
|
-
const { fieldRef, typeRef, queryArgToken } = token.parentNode;
|
|
1564
|
-
const scope = token === fieldRef && queryArgToken ? getQueryScope(queryArgToken, doc) : void 0;
|
|
1565
|
-
const typeName = scope && (typeRef?.text ?? scope.unqualifiedTarget);
|
|
1566
|
-
return typeName ? resolveFieldPath(doc, typeName, {
|
|
1567
|
-
path: token.text,
|
|
1568
|
-
range: token.range
|
|
1569
|
-
}) : void 0;
|
|
1535
|
+
unexpectedEOB() {
|
|
1536
|
+
this.issues.unexpectedEOB = true;
|
|
1570
1537
|
}
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
/**
|
|
1575
|
-
* Resolve the segments named `fieldName` of a field-path `string` argument
|
|
1576
|
-
* (`'address.city'` has the segments `address` and `city`). Supports find-references.
|
|
1577
|
-
*/ function resolveFieldPathArgSegments(argToken, doc, fieldName) {
|
|
1578
|
-
const segments = fieldPathSegments(argToken).filter((s) => s.path === fieldName || s.path.endsWith(`.${fieldName}`));
|
|
1579
|
-
const typeName = segments.length > 0 ? getFieldPathTarget(argToken, doc) : void 0;
|
|
1580
|
-
if (!typeName) return [];
|
|
1581
|
-
const targets = [];
|
|
1582
|
-
for (const segment of segments) {
|
|
1583
|
-
const target = resolveFieldPath(doc, typeName, segment);
|
|
1584
|
-
if (target) targets.push(target);
|
|
1585
|
-
}
|
|
1586
|
-
return targets;
|
|
1587
|
-
}
|
|
1588
|
-
/**
|
|
1589
|
-
* Resolve a query field ref node to a property definition.
|
|
1590
|
-
*/ function resolveQueryFieldRef(fieldRefNode, doc, scope) {
|
|
1591
|
-
const typeName = fieldRefNode.typeRef?.text ?? scope.unqualifiedTarget;
|
|
1592
|
-
const { fieldRef } = fieldRefNode;
|
|
1593
|
-
const resolved = typeName ? resolveFieldPath(doc, typeName, {
|
|
1594
|
-
path: fieldRef.text,
|
|
1595
|
-
range: fieldRef.range
|
|
1596
|
-
}) : void 0;
|
|
1597
|
-
return resolved && {
|
|
1598
|
-
targetUri: resolved.doc.id,
|
|
1599
|
-
doc: resolved.doc,
|
|
1600
|
-
prop: resolved.prop
|
|
1601
|
-
};
|
|
1602
|
-
}
|
|
1603
|
-
/**
|
|
1604
|
-
* Get fields available for a type (resolving extends/intersections).
|
|
1605
|
-
* With `chain`, returns the fields of the nested object that field path leads to.
|
|
1606
|
-
*/ function getFieldsForType(doc, typeName, chain = []) {
|
|
1607
|
-
const unwound = doc.unwindType(typeName, chain);
|
|
1608
|
-
if (!unwound?.def) return [];
|
|
1609
|
-
let def = unwound.doc.mergeIntersection(unwound.def);
|
|
1610
|
-
if (isInterface(def)) def = unwound.doc.resolveInterfaceExtends(def) || def.getDefinition() || def;
|
|
1611
|
-
if (isStructure(def) || isInterface(def)) return Array.from(def.props.values());
|
|
1612
|
-
return [];
|
|
1613
|
-
}
|
|
1614
|
-
/**
|
|
1615
|
-
* Completion scope of a field-path `string` argument (spec declares `fieldScope`) at the
|
|
1616
|
-
* cursor column `character`: the fields of the level being typed — `'address.ci'` offers
|
|
1617
|
-
* the fields of `address` (`chain` = `['address']`).
|
|
1618
|
-
*/ function getFieldPathCompletionScope(argToken, doc, character) {
|
|
1619
|
-
const typeName = getFieldPathTarget(argToken, doc);
|
|
1620
|
-
if (!typeName) return;
|
|
1621
|
-
const chain = fieldPathSegmentAt(argToken, character).path.split(".").slice(0, -1);
|
|
1622
|
-
return {
|
|
1623
|
-
typeName,
|
|
1624
|
-
chain,
|
|
1625
|
-
fields: getFieldsForType(doc, typeName, chain)
|
|
1626
|
-
};
|
|
1627
|
-
}
|
|
1628
|
-
/**
|
|
1629
|
-
* Get completion scope data for a query context.
|
|
1630
|
-
*/ function getQueryCompletionScope(queryArgToken, doc) {
|
|
1631
|
-
const scope = getQueryScope(queryArgToken, doc);
|
|
1632
|
-
if (!scope) return;
|
|
1633
|
-
return {
|
|
1634
|
-
typeNames: scope.allowedTypes,
|
|
1635
|
-
unqualifiedTarget: scope.unqualifiedTarget,
|
|
1636
|
-
getFields: (typeName) => getFieldsForType(doc, typeName)
|
|
1637
|
-
};
|
|
1638
|
-
}
|
|
1639
|
-
const SYMBOLIC_OPS$1 = new Set([
|
|
1640
|
-
"=",
|
|
1641
|
-
"!=",
|
|
1642
|
-
">",
|
|
1643
|
-
">=",
|
|
1644
|
-
"<",
|
|
1645
|
-
"<="
|
|
1646
|
-
]);
|
|
1647
|
-
const KEYWORD_OPS = new Set([
|
|
1648
|
-
"in",
|
|
1649
|
-
"matches",
|
|
1650
|
-
"exists"
|
|
1651
|
-
]);
|
|
1652
|
-
const LOGICAL_KEYWORDS = new Set([
|
|
1653
|
-
"and",
|
|
1654
|
-
"or",
|
|
1655
|
-
"not"
|
|
1656
|
-
]);
|
|
1657
|
-
const VALUE_KEYWORDS$1 = new Set([
|
|
1658
|
-
"true",
|
|
1659
|
-
"false",
|
|
1660
|
-
"null",
|
|
1661
|
-
"undefined"
|
|
1662
|
-
]);
|
|
1663
|
-
/**
|
|
1664
|
-
* Analyze text inside backticks up to cursor position
|
|
1665
|
-
* to determine what kind of completions to offer.
|
|
1666
|
-
* Uses lightweight text scanning, not full AST parsing,
|
|
1667
|
-
* so it works on incomplete/mid-typing input.
|
|
1668
|
-
*/ function analyzeQueryCursorContext(textBeforeCursor) {
|
|
1669
|
-
const trimmed = textBeforeCursor.trimEnd();
|
|
1670
|
-
const hasTrailingSpace = textBeforeCursor.length > trimmed.length;
|
|
1671
|
-
if (trimmed.length === 0) return { type: "field-start" };
|
|
1672
|
-
if (trimmed.endsWith(".")) {
|
|
1673
|
-
const before = trimmed.slice(0, -1).trimEnd();
|
|
1674
|
-
const match = /(\w+)$/u.exec(before);
|
|
1675
|
-
if (match) return {
|
|
1676
|
-
type: "after-dot",
|
|
1677
|
-
typeName: match[1]
|
|
1678
|
-
};
|
|
1679
|
-
return { type: "field-start" };
|
|
1680
|
-
}
|
|
1681
|
-
const tokens = tokenizeQueryText(trimmed);
|
|
1682
|
-
if (tokens.length === 0) return { type: "field-start" };
|
|
1683
|
-
const last = tokens[tokens.length - 1];
|
|
1684
|
-
if (LOGICAL_KEYWORDS.has(last) || last === "(") return { type: "field-start" };
|
|
1685
|
-
if (SYMBOLIC_OPS$1.has(last)) return { type: "after-operator" };
|
|
1686
|
-
if (last === "in" || last === "matches") return { type: "after-operator" };
|
|
1687
|
-
if (last === "exists") return { type: "after-comparison" };
|
|
1688
|
-
if (VALUE_KEYWORDS$1.has(last)) return { type: "after-comparison" };
|
|
1689
|
-
if (/^['"]/.test(last) || /^\d/.test(last)) return { type: "after-comparison" };
|
|
1690
|
-
if (tokens.length >= 2) {
|
|
1691
|
-
const prev = tokens[tokens.length - 2];
|
|
1692
|
-
if (SYMBOLIC_OPS$1.has(prev) || KEYWORD_OPS.has(prev)) return { type: "after-comparison" };
|
|
1693
|
-
}
|
|
1694
|
-
if (/^\w+$/u.test(last)) {
|
|
1695
|
-
if (!hasTrailingSpace) return { type: "field-start" };
|
|
1696
|
-
return { type: "after-field" };
|
|
1697
|
-
}
|
|
1698
|
-
return { type: "field-start" };
|
|
1699
|
-
}
|
|
1700
|
-
/**
|
|
1701
|
-
* Simple tokenizer for query text — splits into identifiers, operators, and punctuation.
|
|
1702
|
-
* Does NOT need to handle all edge cases; just enough for cursor context analysis.
|
|
1703
|
-
*/ function tokenizeQueryText(text) {
|
|
1704
|
-
const tokens = [];
|
|
1705
|
-
let i = 0;
|
|
1706
|
-
while (i < text.length) {
|
|
1707
|
-
if (/\s/.test(text[i])) {
|
|
1708
|
-
i++;
|
|
1709
|
-
continue;
|
|
1710
|
-
}
|
|
1711
|
-
if (text[i] === "'" || text[i] === "\"") {
|
|
1712
|
-
const quote = text[i];
|
|
1713
|
-
let j = i + 1;
|
|
1714
|
-
while (j < text.length && text[j] !== quote) {
|
|
1715
|
-
if (text[j] === "\\") j++;
|
|
1716
|
-
j++;
|
|
1717
|
-
}
|
|
1718
|
-
tokens.push(text.slice(i, j + 1));
|
|
1719
|
-
i = j + 1;
|
|
1720
|
-
continue;
|
|
1721
|
-
}
|
|
1722
|
-
if (i + 1 < text.length) {
|
|
1723
|
-
const two = text.slice(i, i + 2);
|
|
1724
|
-
if (two === "!=" || two === ">=" || two === "<=") {
|
|
1725
|
-
tokens.push(two);
|
|
1726
|
-
i += 2;
|
|
1727
|
-
continue;
|
|
1728
|
-
}
|
|
1729
|
-
}
|
|
1730
|
-
if ("=><(),".includes(text[i])) {
|
|
1731
|
-
tokens.push(text[i]);
|
|
1732
|
-
i++;
|
|
1733
|
-
continue;
|
|
1734
|
-
}
|
|
1735
|
-
if (text[i] === ".") {
|
|
1736
|
-
tokens.push(".");
|
|
1737
|
-
i++;
|
|
1738
|
-
continue;
|
|
1739
|
-
}
|
|
1740
|
-
if (/[\w]/u.test(text[i])) {
|
|
1741
|
-
let j = i;
|
|
1742
|
-
while (j < text.length && /[\w]/u.test(text[j])) j++;
|
|
1743
|
-
tokens.push(text.slice(i, j));
|
|
1744
|
-
i = j;
|
|
1745
|
-
continue;
|
|
1746
|
-
}
|
|
1747
|
-
i++;
|
|
1748
|
-
}
|
|
1749
|
-
return tokens;
|
|
1750
|
-
}
|
|
1751
|
-
|
|
1752
|
-
//#endregion
|
|
1753
|
-
//#region packages/core/src/parser/id-registry.ts
|
|
1754
|
-
function _define_property$7(obj, key, value) {
|
|
1755
|
-
if (key in obj) Object.defineProperty(obj, key, {
|
|
1756
|
-
value,
|
|
1757
|
-
enumerable: true,
|
|
1758
|
-
configurable: true,
|
|
1759
|
-
writable: true
|
|
1760
|
-
});
|
|
1761
|
-
else obj[key] = value;
|
|
1762
|
-
return obj;
|
|
1763
|
-
}
|
|
1764
|
-
var IdRegistry = class {
|
|
1765
|
-
clear() {
|
|
1766
|
-
this.definitions.clear();
|
|
1767
|
-
this.duplicates.clear();
|
|
1768
|
-
this.forbidden.clear();
|
|
1769
|
-
}
|
|
1770
|
-
registerDefinition(token) {
|
|
1771
|
-
if (!token) return;
|
|
1772
|
-
if (this.reserved.has(token.text)) this.forbidden.add(token);
|
|
1773
|
-
else if (this.definitions.has(token.text)) this.duplicates.add(token);
|
|
1774
|
-
else this.definitions.set(token.text, token);
|
|
1775
|
-
}
|
|
1776
|
-
isDefined(t) {
|
|
1777
|
-
const text = typeof t === "string" ? t : t.text;
|
|
1778
|
-
return this.definitions.has(text) || this.globalTypes.has(text);
|
|
1779
|
-
}
|
|
1780
|
-
getErrors() {
|
|
1781
|
-
return [...Array.from(this.duplicates, (t) => ({
|
|
1782
|
-
severity: 1,
|
|
1783
|
-
message: `Duplicate identifier "${t.text}"`,
|
|
1784
|
-
range: t.range
|
|
1785
|
-
})), ...Array.from(this.forbidden, (t) => ({
|
|
1786
|
-
severity: 1,
|
|
1787
|
-
message: `Reserved keyword "${t.text}"`,
|
|
1788
|
-
range: t.range
|
|
1789
|
-
}))];
|
|
1790
|
-
}
|
|
1791
|
-
constructor(globalTypes = []) {
|
|
1792
|
-
_define_property$7(this, "reserved", void 0);
|
|
1793
|
-
_define_property$7(this, "globalTypes", void 0);
|
|
1794
|
-
_define_property$7(this, "definitions", /* @__PURE__ */ new Map());
|
|
1795
|
-
_define_property$7(this, "duplicates", /* @__PURE__ */ new Set());
|
|
1796
|
-
_define_property$7(this, "forbidden", /* @__PURE__ */ new Set());
|
|
1797
|
-
this.reserved = new Set([
|
|
1798
|
-
"interface",
|
|
1799
|
-
"type",
|
|
1800
|
-
"import",
|
|
1801
|
-
"from",
|
|
1802
|
-
"export",
|
|
1803
|
-
"annotate",
|
|
1804
|
-
"as"
|
|
1805
|
-
].concat(globalTypes));
|
|
1806
|
-
this.globalTypes = new Set(globalTypes);
|
|
1807
|
-
}
|
|
1808
|
-
};
|
|
1809
|
-
|
|
1810
|
-
//#endregion
|
|
1811
|
-
//#region packages/core/src/parser/iterator.ts
|
|
1812
|
-
function _define_property$6(obj, key, value) {
|
|
1813
|
-
if (key in obj) Object.defineProperty(obj, key, {
|
|
1814
|
-
value,
|
|
1815
|
-
enumerable: true,
|
|
1816
|
-
configurable: true,
|
|
1817
|
-
writable: true
|
|
1818
|
-
});
|
|
1819
|
-
else obj[key] = value;
|
|
1820
|
-
return obj;
|
|
1821
|
-
}
|
|
1822
|
-
var NodeIterator = class NodeIterator {
|
|
1823
|
-
get index() {
|
|
1824
|
-
return this.i;
|
|
1825
|
-
}
|
|
1826
|
-
get lastNode() {
|
|
1827
|
-
return this.nodes[this.nodes.length - 1];
|
|
1828
|
-
}
|
|
1829
|
-
unexpectedEOB() {
|
|
1830
|
-
this.issues.unexpectedEOB = true;
|
|
1831
|
-
}
|
|
1832
|
-
unfork(fork) {
|
|
1833
|
-
this.i = fork.index;
|
|
1834
|
-
this.update();
|
|
1538
|
+
unfork(fork) {
|
|
1539
|
+
this.i = fork.index;
|
|
1540
|
+
this.update();
|
|
1835
1541
|
}
|
|
1836
1542
|
accepted() {
|
|
1837
1543
|
if (this.$ && this.$.accepted !== false) {
|
|
@@ -1922,13 +1628,13 @@ var NodeIterator = class NodeIterator {
|
|
|
1922
1628
|
})).concat(this.messages);
|
|
1923
1629
|
}
|
|
1924
1630
|
constructor(nodes, messages = [], badNodes = /* @__PURE__ */ new Map(), issues = {}, parent, i = -1) {
|
|
1925
|
-
_define_property$
|
|
1926
|
-
_define_property$
|
|
1927
|
-
_define_property$
|
|
1928
|
-
_define_property$
|
|
1929
|
-
_define_property$
|
|
1930
|
-
_define_property$
|
|
1931
|
-
_define_property$
|
|
1631
|
+
_define_property$7(this, "nodes", void 0);
|
|
1632
|
+
_define_property$7(this, "messages", void 0);
|
|
1633
|
+
_define_property$7(this, "badNodes", void 0);
|
|
1634
|
+
_define_property$7(this, "issues", void 0);
|
|
1635
|
+
_define_property$7(this, "parent", void 0);
|
|
1636
|
+
_define_property$7(this, "i", void 0);
|
|
1637
|
+
_define_property$7(this, "$", void 0);
|
|
1932
1638
|
this.nodes = nodes;
|
|
1933
1639
|
this.messages = messages;
|
|
1934
1640
|
this.badNodes = badNodes;
|
|
@@ -1939,891 +1645,1560 @@ var NodeIterator = class NodeIterator {
|
|
|
1939
1645
|
};
|
|
1940
1646
|
|
|
1941
1647
|
//#endregion
|
|
1942
|
-
//#region packages/core/src/
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1648
|
+
//#region packages/core/src/parser/pipes/query.pipe.ts
|
|
1649
|
+
const SYMBOLIC_OPS = new Set([
|
|
1650
|
+
"=",
|
|
1651
|
+
"!=",
|
|
1652
|
+
">",
|
|
1653
|
+
">=",
|
|
1654
|
+
"<",
|
|
1655
|
+
"<="
|
|
1656
|
+
]);
|
|
1657
|
+
const VALUE_KEYWORDS = new Set([
|
|
1658
|
+
"true",
|
|
1659
|
+
"false",
|
|
1660
|
+
"null",
|
|
1661
|
+
"undefined"
|
|
1662
|
+
]);
|
|
1663
|
+
const ZERO_RANGE = {
|
|
1664
|
+
start: {
|
|
1665
|
+
line: 0,
|
|
1666
|
+
character: 0
|
|
1956
1667
|
},
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1668
|
+
end: {
|
|
1669
|
+
line: 0,
|
|
1670
|
+
character: 0
|
|
1671
|
+
}
|
|
1672
|
+
};
|
|
1962
1673
|
/**
|
|
1963
|
-
*
|
|
1964
|
-
*
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1674
|
+
* Parse a backtick-delimited query expression.
|
|
1675
|
+
*
|
|
1676
|
+
* `ni` iterates the query token's children and carries that token as its
|
|
1677
|
+
* `parent` — the range to blame when a diagnostic has no token of its own —
|
|
1678
|
+
* so fork it from the annotation iterator while the query token is current.
|
|
1679
|
+
* Returns a SemanticQueryNode or undefined if the content is empty.
|
|
1680
|
+
*/ function parseQueryExpression(ni, sourceToken) {
|
|
1681
|
+
if (!ni.$) return;
|
|
1682
|
+
const expr = parseOrExpr(ni);
|
|
1683
|
+
if (!expr) return;
|
|
1684
|
+
if (ni.$) pushError(ni, `Unexpected token in query expression: "${ni.$.text}"`);
|
|
1685
|
+
const node = new SemanticQueryNode();
|
|
1686
|
+
node.expression = expr;
|
|
1687
|
+
node.sourceToken = sourceToken;
|
|
1688
|
+
return node;
|
|
1689
|
+
}
|
|
1690
|
+
function parseOrExpr(ni) {
|
|
1691
|
+
const left = parseAndExpr(ni);
|
|
1692
|
+
if (!left) return;
|
|
1693
|
+
const operands = [left];
|
|
1694
|
+
while (ni.$?.type === "identifier" && ni.$.text === "or") {
|
|
1695
|
+
ni.move();
|
|
1696
|
+
const right = parseAndExpr(ni);
|
|
1697
|
+
if (!right) {
|
|
1698
|
+
pushError(ni, "Expected expression after \"or\"");
|
|
1699
|
+
break;
|
|
1982
1700
|
}
|
|
1983
|
-
|
|
1701
|
+
operands.push(right);
|
|
1702
|
+
}
|
|
1703
|
+
if (operands.length === 1) return operands[0];
|
|
1704
|
+
const node = new SemanticQueryLogicalNode();
|
|
1705
|
+
node.operator = "or";
|
|
1706
|
+
node.operands = operands;
|
|
1707
|
+
return node;
|
|
1984
1708
|
}
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
start: {
|
|
1998
|
-
token: "//",
|
|
1999
|
-
omit: true
|
|
2000
|
-
},
|
|
2001
|
-
end: {
|
|
2002
|
-
token: /$/mu,
|
|
2003
|
-
omit: true
|
|
2004
|
-
},
|
|
2005
|
-
eofClose: true,
|
|
2006
|
-
data: {
|
|
2007
|
-
type: "comment",
|
|
2008
|
-
text: ""
|
|
2009
|
-
},
|
|
2010
|
-
mapContent: "text"
|
|
2011
|
-
});
|
|
2012
|
-
const block$1 = new _prostojs_parser.Node({
|
|
2013
|
-
name: "block-comment",
|
|
2014
|
-
start: {
|
|
2015
|
-
token: "/*",
|
|
2016
|
-
omit: true
|
|
2017
|
-
},
|
|
2018
|
-
end: {
|
|
2019
|
-
token: "*/",
|
|
2020
|
-
omit: true
|
|
2021
|
-
},
|
|
2022
|
-
eofClose: true,
|
|
2023
|
-
data: {
|
|
2024
|
-
type: "comment",
|
|
2025
|
-
text: ""
|
|
2026
|
-
},
|
|
2027
|
-
mapContent: "text"
|
|
2028
|
-
});
|
|
2029
|
-
/**
|
|
2030
|
-
* Block and inline comments
|
|
2031
|
-
*/ const commentNodes = {
|
|
2032
|
-
inline,
|
|
2033
|
-
block: block$1,
|
|
2034
|
-
all: [inline, block$1]
|
|
2035
|
-
};
|
|
2036
|
-
|
|
2037
|
-
//#endregion
|
|
2038
|
-
//#region packages/core/src/tokenizer/tokens/identifier.token.ts
|
|
2039
|
-
/**
|
|
2040
|
-
* Identifier node
|
|
2041
|
-
*/ const IdentifierToken = new _prostojs_parser.Node({
|
|
2042
|
-
name: "identifier",
|
|
2043
|
-
start: { token: RegExp("[\\p{ID_Start}$_][\\p{ID_Continue}$]*", "u") },
|
|
2044
|
-
end: {
|
|
2045
|
-
token: RegExp("[^\\p{ID_Continue}$]", "u"),
|
|
2046
|
-
eject: true
|
|
2047
|
-
},
|
|
2048
|
-
eofClose: true,
|
|
2049
|
-
data: {
|
|
2050
|
-
type: "identifier",
|
|
2051
|
-
text: ""
|
|
2052
|
-
},
|
|
2053
|
-
mapContent: "text"
|
|
2054
|
-
});
|
|
2055
|
-
|
|
2056
|
-
//#endregion
|
|
2057
|
-
//#region packages/core/src/tokenizer/tokens/number.token.ts
|
|
2058
|
-
/**
|
|
2059
|
-
* Number node
|
|
2060
|
-
*/ const NumberToken = new _prostojs_parser.Node({
|
|
2061
|
-
name: "number",
|
|
2062
|
-
start: { token: /[-+]?(?:\d*\.\d+|\d+)(?:[eE][-+]?\d+)?/u },
|
|
2063
|
-
end: {
|
|
2064
|
-
token: /[^\d]/u,
|
|
2065
|
-
eject: true
|
|
2066
|
-
},
|
|
2067
|
-
eofClose: true,
|
|
2068
|
-
data: {
|
|
2069
|
-
type: "number",
|
|
2070
|
-
text: ""
|
|
2071
|
-
},
|
|
2072
|
-
mapContent: "text"
|
|
2073
|
-
});
|
|
2074
|
-
|
|
2075
|
-
//#endregion
|
|
2076
|
-
//#region packages/core/src/tokenizer/tokens/punctuation.token.ts
|
|
2077
|
-
/**
|
|
2078
|
-
* Punctuation node
|
|
2079
|
-
*/ const PunctuationToken = new _prostojs_parser.Node({
|
|
2080
|
-
name: "punctuation",
|
|
2081
|
-
start: {
|
|
2082
|
-
token: RegExp("(?<text>[\\n!&+,./:;=?|])", "u"),
|
|
2083
|
-
omit: true
|
|
2084
|
-
},
|
|
2085
|
-
end: {
|
|
2086
|
-
token: "",
|
|
2087
|
-
omit: true
|
|
2088
|
-
},
|
|
2089
|
-
eofClose: true,
|
|
2090
|
-
data: {
|
|
2091
|
-
type: "punctuation",
|
|
2092
|
-
text: ""
|
|
1709
|
+
function parseAndExpr(ni) {
|
|
1710
|
+
const left = parseUnaryExpr(ni);
|
|
1711
|
+
if (!left) return;
|
|
1712
|
+
const operands = [left];
|
|
1713
|
+
while (ni.$?.type === "identifier" && ni.$.text === "and") {
|
|
1714
|
+
ni.move();
|
|
1715
|
+
const right = parseUnaryExpr(ni);
|
|
1716
|
+
if (!right) {
|
|
1717
|
+
pushError(ni, "Expected expression after \"and\"");
|
|
1718
|
+
break;
|
|
1719
|
+
}
|
|
1720
|
+
operands.push(right);
|
|
2093
1721
|
}
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
//#endregion
|
|
2113
|
-
//#region packages/core/src/tokenizer/tokens/query-operator.token.ts
|
|
2114
|
-
/**
|
|
2115
|
-
* Query operator token — matches symbolic comparison operators
|
|
2116
|
-
* inside backtick-delimited query expressions.
|
|
2117
|
-
*
|
|
2118
|
-
* Multi-character operators (!=, >=, <=) are matched as single tokens.
|
|
2119
|
-
* Only recognized inside QueryToken — not in root or BlockToken.
|
|
2120
|
-
*/ const QueryOperatorToken = new _prostojs_parser.Node({
|
|
2121
|
-
name: "punctuation",
|
|
2122
|
-
start: {
|
|
2123
|
-
token: RegExp("(?<text>!=|>=|<=|[=><])", "u"),
|
|
2124
|
-
omit: true
|
|
2125
|
-
},
|
|
2126
|
-
end: {
|
|
2127
|
-
token: "",
|
|
2128
|
-
omit: true
|
|
2129
|
-
},
|
|
2130
|
-
eofClose: true,
|
|
2131
|
-
data: {
|
|
2132
|
-
type: "punctuation",
|
|
2133
|
-
text: ""
|
|
1722
|
+
if (operands.length === 1) return operands[0];
|
|
1723
|
+
const node = new SemanticQueryLogicalNode();
|
|
1724
|
+
node.operator = "and";
|
|
1725
|
+
node.operands = operands;
|
|
1726
|
+
return node;
|
|
1727
|
+
}
|
|
1728
|
+
function parseUnaryExpr(ni) {
|
|
1729
|
+
if (ni.$?.type === "identifier" && ni.$.text === "not") {
|
|
1730
|
+
ni.move();
|
|
1731
|
+
const operand = parseUnaryExpr(ni);
|
|
1732
|
+
if (!operand) {
|
|
1733
|
+
pushError(ni, "Expected expression after \"not\"");
|
|
1734
|
+
return;
|
|
1735
|
+
}
|
|
1736
|
+
const node = new SemanticQueryLogicalNode();
|
|
1737
|
+
node.operator = "not";
|
|
1738
|
+
node.operands = [operand];
|
|
1739
|
+
return node;
|
|
2134
1740
|
}
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
token: /`/u,
|
|
2146
|
-
omit: true
|
|
2147
|
-
},
|
|
2148
|
-
end: {
|
|
2149
|
-
token: /`/u,
|
|
2150
|
-
omit: true
|
|
2151
|
-
},
|
|
2152
|
-
skip: /\s/u,
|
|
2153
|
-
eofClose: true,
|
|
2154
|
-
data: {
|
|
2155
|
-
type: "query",
|
|
2156
|
-
text: ""
|
|
1741
|
+
if (ni.$?.type === "block" && ni.$.text === "(") {
|
|
1742
|
+
const subNi = ni.fork(ni.$.children || []);
|
|
1743
|
+
ni.move();
|
|
1744
|
+
if (!subNi.$) {
|
|
1745
|
+
pushError(subNi, "Empty parenthesized expression");
|
|
1746
|
+
return;
|
|
1747
|
+
}
|
|
1748
|
+
const expr = parseOrExpr(subNi);
|
|
1749
|
+
if (subNi.$) pushError(subNi, `Unexpected token in parenthesized expression: "${subNi.$.text}"`);
|
|
1750
|
+
return expr;
|
|
2157
1751
|
}
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
//#endregion
|
|
2161
|
-
//#region packages/core/src/tokenizer/tokens/regexp.token.ts
|
|
2162
|
-
const REGEXP_LITERAL_RE = /\/(?![/*])(?:\\.|\[.*?]|[^/\\\n\r[])*\/[dgimsuy]*/;
|
|
2163
|
-
/**
|
|
2164
|
-
* RegExp literal – `/pattern/flags`
|
|
2165
|
-
* • Starts with `/` but *not* `//` or `/*`
|
|
2166
|
-
* • Ends with `/` followed by zero-or-more valid flags
|
|
2167
|
-
*/ const RegExpToken = new _prostojs_parser.Node({
|
|
2168
|
-
name: "regexp",
|
|
2169
|
-
start: {
|
|
2170
|
-
token: REGEXP_LITERAL_RE,
|
|
2171
|
-
omit: true
|
|
2172
|
-
},
|
|
2173
|
-
end: {
|
|
2174
|
-
token: "",
|
|
2175
|
-
omit: true
|
|
2176
|
-
},
|
|
2177
|
-
eofClose: true,
|
|
2178
|
-
data: {
|
|
2179
|
-
type: "regexp",
|
|
2180
|
-
text: ""
|
|
2181
|
-
}
|
|
2182
|
-
}).onOpen((node, match) => {
|
|
2183
|
-
node.data.text = match?.text ?? "";
|
|
2184
|
-
});
|
|
2185
|
-
|
|
2186
|
-
//#endregion
|
|
2187
|
-
//#region packages/core/src/tokenizer/tokens/text.node.ts
|
|
2188
|
-
/**
|
|
2189
|
-
* Text node single or doublequoted
|
|
2190
|
-
*/ const TextToken = new _prostojs_parser.Node({
|
|
2191
|
-
name: "text",
|
|
2192
|
-
start: {
|
|
2193
|
-
token: RegExp("(?<quote>[\"'])", "u"),
|
|
2194
|
-
omit: true
|
|
2195
|
-
},
|
|
2196
|
-
end: {
|
|
2197
|
-
token: (ctx) => new RegExp(`(?<end>${ctx.node.data.quote || ""}|\\n)`),
|
|
2198
|
-
omit: true,
|
|
2199
|
-
backslash: true
|
|
2200
|
-
},
|
|
2201
|
-
eofClose: true,
|
|
2202
|
-
data: {
|
|
2203
|
-
type: "text",
|
|
2204
|
-
quote: "",
|
|
2205
|
-
end: "",
|
|
2206
|
-
text: ""
|
|
2207
|
-
},
|
|
2208
|
-
mapContent: "text"
|
|
2209
|
-
}).onClose((node) => {
|
|
2210
|
-
node.data.multiline = node.data.end === "\n";
|
|
2211
|
-
});
|
|
2212
|
-
|
|
2213
|
-
//#endregion
|
|
2214
|
-
//#region packages/core/src/tokenizer/tokens/index.ts
|
|
2215
|
-
const tokens = {
|
|
2216
|
-
aIdentifier: AIdentifierToken,
|
|
2217
|
-
punctuation: PunctuationToken,
|
|
2218
|
-
comments: commentNodes.all,
|
|
2219
|
-
inlineComment: commentNodes.inline,
|
|
2220
|
-
blockComment: commentNodes.block,
|
|
2221
|
-
block: BlockToken,
|
|
2222
|
-
identifier: IdentifierToken,
|
|
2223
|
-
number: NumberToken,
|
|
2224
|
-
text: TextToken,
|
|
2225
|
-
regexp: RegExpToken,
|
|
2226
|
-
query: QueryToken,
|
|
2227
|
-
queryBlock: QueryBlockToken,
|
|
2228
|
-
root: void 0
|
|
2229
|
-
};
|
|
2230
|
-
const root = new _prostojs_parser.Node({
|
|
2231
|
-
name: "root",
|
|
2232
|
-
skip: /\s/u,
|
|
2233
|
-
eofClose: true,
|
|
2234
|
-
recognizes: [
|
|
2235
|
-
...commentNodes.all,
|
|
2236
|
-
BlockToken,
|
|
2237
|
-
QueryToken,
|
|
2238
|
-
AIdentifierToken,
|
|
2239
|
-
IdentifierToken,
|
|
2240
|
-
TextToken,
|
|
2241
|
-
NumberToken,
|
|
2242
|
-
PunctuationToken
|
|
2243
|
-
]
|
|
2244
|
-
});
|
|
2245
|
-
tokens.root = root;
|
|
2246
|
-
BlockToken.recognize(RegExpToken, ...commentNodes.all, BlockToken, QueryToken, AIdentifierToken, IdentifierToken, TextToken, NumberToken, PunctuationToken);
|
|
2247
|
-
QueryToken.recognize(RegExpToken, QueryOperatorToken, QueryBlockToken, BlockToken, IdentifierToken, TextToken, NumberToken, PunctuationToken);
|
|
2248
|
-
QueryBlockToken.recognize(RegExpToken, QueryOperatorToken, QueryBlockToken, IdentifierToken, TextToken, NumberToken, PunctuationToken);
|
|
2249
|
-
/** 1-based parser position → 0-based VSCode position, shifted by `offset` characters. */ function toPos(p, offset = 0) {
|
|
2250
|
-
return {
|
|
2251
|
-
line: p.line - 1,
|
|
2252
|
-
character: p.column - 1 + offset
|
|
2253
|
-
};
|
|
1752
|
+
return parseComparison(ni);
|
|
2254
1753
|
}
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
if (typeof item === "string") {
|
|
2266
|
-
const prev = content[index - 1];
|
|
2267
|
-
const next = content[index + 1];
|
|
2268
|
-
const start = typeof prev === "object" ? prev.end : node.start;
|
|
2269
|
-
const end = typeof next === "object" ? next.start : node.end;
|
|
2270
|
-
return {
|
|
2271
|
-
type: "unknown",
|
|
2272
|
-
text: item,
|
|
2273
|
-
getRange: () => ({
|
|
2274
|
-
start: toPos(start),
|
|
2275
|
-
end: toPos(end)
|
|
2276
|
-
})
|
|
2277
|
-
};
|
|
1754
|
+
function parseComparison(ni) {
|
|
1755
|
+
const left = parseFieldRef(ni);
|
|
1756
|
+
if (!left) return;
|
|
1757
|
+
if (ni.$?.type === "punctuation" && SYMBOLIC_OPS.has(ni.$.text)) {
|
|
1758
|
+
const opText = ni.$.text;
|
|
1759
|
+
ni.move();
|
|
1760
|
+
const right = parseValueOrFieldRef(ni);
|
|
1761
|
+
if (!right) {
|
|
1762
|
+
pushError(ni, `Expected value or field reference after "${opText}"`);
|
|
1763
|
+
return;
|
|
2278
1764
|
}
|
|
2279
|
-
const
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
1765
|
+
const node = new SemanticQueryComparisonNode();
|
|
1766
|
+
node.left = left;
|
|
1767
|
+
node.operator = opText;
|
|
1768
|
+
node.right = right;
|
|
1769
|
+
return node;
|
|
1770
|
+
}
|
|
1771
|
+
if (ni.$?.type === "identifier") {
|
|
1772
|
+
const opText = ni.$.text;
|
|
1773
|
+
if (opText === "in") {
|
|
1774
|
+
ni.move();
|
|
1775
|
+
const right = parseValueList(ni);
|
|
1776
|
+
if (!right) return;
|
|
1777
|
+
const node = new SemanticQueryComparisonNode();
|
|
1778
|
+
node.left = left;
|
|
1779
|
+
node.operator = "in";
|
|
1780
|
+
node.right = right;
|
|
1781
|
+
return node;
|
|
1782
|
+
}
|
|
1783
|
+
if (opText === "matches") {
|
|
1784
|
+
ni.move();
|
|
1785
|
+
const right = parseValueOrFieldRef(ni);
|
|
1786
|
+
if (!right) {
|
|
1787
|
+
pushError(ni, "Expected value after \"matches\"");
|
|
1788
|
+
return;
|
|
1789
|
+
}
|
|
1790
|
+
const node = new SemanticQueryComparisonNode();
|
|
1791
|
+
node.left = left;
|
|
1792
|
+
node.operator = "matches";
|
|
1793
|
+
node.right = right;
|
|
1794
|
+
return node;
|
|
1795
|
+
}
|
|
1796
|
+
if (opText === "exists") {
|
|
1797
|
+
ni.move();
|
|
1798
|
+
const node = new SemanticQueryComparisonNode();
|
|
1799
|
+
node.left = left;
|
|
1800
|
+
node.operator = "exists";
|
|
1801
|
+
return node;
|
|
1802
|
+
}
|
|
1803
|
+
if (opText === "not") return parseNotComparison(ni, left);
|
|
1804
|
+
pushError(ni, `Unknown operator "${opText}"`);
|
|
1805
|
+
return;
|
|
1806
|
+
}
|
|
1807
|
+
pushError(ni, "Expected operator after field reference");
|
|
2287
1808
|
}
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
1809
|
+
function parseNotComparison(ni, left) {
|
|
1810
|
+
ni.move();
|
|
1811
|
+
if (ni.$?.type === "identifier" && ni.$.text === "in") {
|
|
1812
|
+
ni.move();
|
|
1813
|
+
const right = parseValueList(ni);
|
|
1814
|
+
if (!right) return;
|
|
1815
|
+
const node = new SemanticQueryComparisonNode();
|
|
1816
|
+
node.left = left;
|
|
1817
|
+
node.operator = "not in";
|
|
1818
|
+
node.right = right;
|
|
1819
|
+
return node;
|
|
1820
|
+
}
|
|
1821
|
+
if (ni.$?.type === "identifier" && ni.$.text === "exists") {
|
|
1822
|
+
ni.move();
|
|
1823
|
+
const node = new SemanticQueryComparisonNode();
|
|
1824
|
+
node.left = left;
|
|
1825
|
+
node.operator = "not exists";
|
|
1826
|
+
return node;
|
|
1827
|
+
}
|
|
1828
|
+
pushError(ni, "Expected \"in\" or \"exists\" after \"not\"");
|
|
2295
1829
|
}
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
const
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
debug: false,
|
|
2318
|
-
eob: false,
|
|
2319
|
-
suppressEobError: false,
|
|
2320
|
-
lookBehind: false
|
|
2321
|
-
};
|
|
2322
|
-
return {
|
|
2323
|
-
expect: [{
|
|
2324
|
-
node: name,
|
|
2325
|
-
text
|
|
2326
|
-
}],
|
|
2327
|
-
handler(ni, target) {
|
|
2328
|
-
if (opts.debug) debugger;
|
|
2329
|
-
let firstRun = true;
|
|
2330
|
-
while (firstRun || opts.wrapMultiple) {
|
|
2331
|
-
firstRun = false;
|
|
2332
|
-
if (!ni.$) {
|
|
2333
|
-
const ok = opts.eob || opts.optional;
|
|
2334
|
-
if (!ok && !opts.suppressEobError) ni.unexpectedEOB();
|
|
2335
|
-
return ok;
|
|
2336
|
-
}
|
|
2337
|
-
let matched = ni.satisfies(...opts.expect);
|
|
2338
|
-
if (!matched && opts.lookBehind) {
|
|
2339
|
-
matched = ni.fork().move(-1).satisfies(...opts.expect);
|
|
2340
|
-
if (matched) ni.move(-1);
|
|
2341
|
-
}
|
|
2342
|
-
if (matched) {
|
|
2343
|
-
if (opts.empty && ni.$.children?.length) {
|
|
2344
|
-
ni.unexpected(false, `Expected empty block`);
|
|
2345
|
-
return opts.optional;
|
|
2346
|
-
}
|
|
2347
|
-
if (opts.contains) {
|
|
2348
|
-
if (!ni.$.children?.length) {
|
|
2349
|
-
ni.unexpected(false, `Expected ${opts.contains.map((v) => v.text).join(", ")}`);
|
|
2350
|
-
return opts.optional;
|
|
2351
|
-
}
|
|
2352
|
-
if (ni.$.children[0].type !== opts.contains[0].node || ni.$.children[0].text !== opts.contains[0].text) {
|
|
2353
|
-
ni.unexpected(false, `Expected ${opts.contains.map((v) => v.text).join(", ")}`);
|
|
2354
|
-
return opts.optional;
|
|
2355
|
-
}
|
|
2356
|
-
}
|
|
2357
|
-
if (opts.saveAs) target.node.saveToken(new Token(ni.$), opts.saveAs);
|
|
2358
|
-
ni.accepted();
|
|
2359
|
-
if (opts.wrapper) {
|
|
2360
|
-
const wrapped = target.node;
|
|
2361
|
-
target.node = opts.wrapper().wrap(wrapped, new Token(ni.$));
|
|
2362
|
-
}
|
|
2363
|
-
ni.move();
|
|
2364
|
-
if (opts.skip) ni.skip(opts.skip);
|
|
2365
|
-
if (opts.wrapMultiple) continue;
|
|
2366
|
-
else return true;
|
|
2367
|
-
} else {
|
|
2368
|
-
if (!opts.optional) ni.unexpected();
|
|
2369
|
-
return opts.optional;
|
|
1830
|
+
function parseFieldRef(ni) {
|
|
1831
|
+
if (ni.$?.type !== "identifier") {
|
|
1832
|
+
pushError(ni, "Expected field reference");
|
|
1833
|
+
return;
|
|
1834
|
+
}
|
|
1835
|
+
const firstToken = new Token(ni.$);
|
|
1836
|
+
ni.move();
|
|
1837
|
+
if (ni.$?.type === "punctuation" && ni.$.text === ".") {
|
|
1838
|
+
ni.move();
|
|
1839
|
+
if (ni.$?.type === "identifier") {
|
|
1840
|
+
const secondToken = new Token(ni.$);
|
|
1841
|
+
ni.move();
|
|
1842
|
+
let fieldText = secondToken.text;
|
|
1843
|
+
while (ni.$?.type === "punctuation" && ni.$.text === ".") {
|
|
1844
|
+
ni.move();
|
|
1845
|
+
if (ni.$?.type === "identifier") {
|
|
1846
|
+
fieldText += `.${ni.$.text}`;
|
|
1847
|
+
ni.move();
|
|
1848
|
+
} else {
|
|
1849
|
+
pushError(ni, "Expected identifier after \".\"");
|
|
1850
|
+
break;
|
|
2370
1851
|
}
|
|
2371
1852
|
}
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
return this;
|
|
2377
|
-
},
|
|
2378
|
-
wrap(sn, multiple = false) {
|
|
2379
|
-
opts.wrapper = sn;
|
|
2380
|
-
opts.wrapMultiple = multiple;
|
|
2381
|
-
return this;
|
|
2382
|
-
},
|
|
2383
|
-
optional() {
|
|
2384
|
-
opts.optional = true;
|
|
2385
|
-
return this;
|
|
2386
|
-
},
|
|
2387
|
-
empty() {
|
|
2388
|
-
opts.empty = true;
|
|
2389
|
-
return this;
|
|
2390
|
-
},
|
|
2391
|
-
contains(t) {
|
|
2392
|
-
opts.contains = t.expect;
|
|
2393
|
-
return this;
|
|
2394
|
-
},
|
|
2395
|
-
skip(...p) {
|
|
2396
|
-
opts.skip = p;
|
|
2397
|
-
return this;
|
|
2398
|
-
},
|
|
2399
|
-
or(t) {
|
|
2400
|
-
opts.expect.push(...t.expect);
|
|
2401
|
-
return this;
|
|
2402
|
-
},
|
|
2403
|
-
orEob() {
|
|
2404
|
-
opts.eob = true;
|
|
2405
|
-
return this;
|
|
2406
|
-
},
|
|
2407
|
-
suppressEobError() {
|
|
2408
|
-
opts.suppressEobError = true;
|
|
2409
|
-
return this;
|
|
2410
|
-
},
|
|
2411
|
-
global() {
|
|
2412
|
-
opts.isGlobal = true;
|
|
2413
|
-
return this;
|
|
2414
|
-
},
|
|
2415
|
-
lookBehind() {
|
|
2416
|
-
opts.lookBehind = true;
|
|
2417
|
-
return this;
|
|
2418
|
-
},
|
|
2419
|
-
debug() {
|
|
2420
|
-
opts.debug = true;
|
|
2421
|
-
return this;
|
|
1853
|
+
const node = new SemanticQueryFieldRefNode();
|
|
1854
|
+
node.typeRef = firstToken;
|
|
1855
|
+
node.fieldRef = fieldText === secondToken.text ? secondToken : secondToken.clone({ text: fieldText });
|
|
1856
|
+
return node;
|
|
2422
1857
|
}
|
|
2423
|
-
|
|
1858
|
+
pushError(ni, "Expected identifier after \".\"");
|
|
1859
|
+
const node = new SemanticQueryFieldRefNode();
|
|
1860
|
+
node.fieldRef = firstToken;
|
|
1861
|
+
return node;
|
|
1862
|
+
}
|
|
1863
|
+
const node = new SemanticQueryFieldRefNode();
|
|
1864
|
+
node.fieldRef = firstToken;
|
|
1865
|
+
return node;
|
|
2424
1866
|
}
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
case "interface": return new $n.SemanticInterfaceNode();
|
|
2434
|
-
case "array": return new $n.SemanticArrayNode();
|
|
2435
|
-
case "const": return new $n.SemanticConstNode();
|
|
2436
|
-
case "group": return new $n.SemanticGroup();
|
|
2437
|
-
case "prop": return new $n.SemanticPropNode();
|
|
2438
|
-
case "type": return new $n.SemanticTypeNode();
|
|
2439
|
-
case "ref": return new $n.SemanticRefNode();
|
|
2440
|
-
case "structure": return new $n.SemanticStructureNode();
|
|
2441
|
-
case "tuple": return new $n.SemanticTupleNode();
|
|
2442
|
-
case "import": return new $n.SemanticImportNode();
|
|
2443
|
-
case "annotate": return new $n.SemanticAnnotateNode();
|
|
2444
|
-
case "primitive": throw new Error("Can't create pipe for primitive node");
|
|
2445
|
-
default: throw new Error(`Can't create pipe for ${entity} node`);
|
|
2446
|
-
}
|
|
2447
|
-
},
|
|
2448
|
-
pipe,
|
|
2449
|
-
toString: () => entity,
|
|
2450
|
-
skipTokens: [],
|
|
2451
|
-
stopCondition: void 0,
|
|
2452
|
-
skip(...s) {
|
|
2453
|
-
this.skipTokens.push(...s);
|
|
2454
|
-
return this;
|
|
2455
|
-
},
|
|
2456
|
-
stop(sc) {
|
|
2457
|
-
this.stopCondition = sc;
|
|
2458
|
-
return this;
|
|
2459
|
-
},
|
|
2460
|
-
t(node, skip = []) {
|
|
2461
|
-
this.pipe.push($token(node).saveAs("identifier").skip(...skip));
|
|
2462
|
-
return this;
|
|
2463
|
-
}
|
|
2464
|
-
};
|
|
1867
|
+
function parseValueOrFieldRef(ni) {
|
|
1868
|
+
if (!ni.$) return;
|
|
1869
|
+
if (ni.$.type === "text" || ni.$.type === "number" || ni.$.type === "regexp") return parseValue(ni);
|
|
1870
|
+
if (ni.$.type === "identifier") {
|
|
1871
|
+
if (VALUE_KEYWORDS.has(ni.$.text)) return parseValue(ni);
|
|
1872
|
+
return parseFieldRef(ni);
|
|
1873
|
+
}
|
|
1874
|
+
pushError(ni, "Expected value or field reference");
|
|
2465
1875
|
}
|
|
2466
|
-
function
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
if (!fork.$) {
|
|
2476
|
-
ni.unfork(fork);
|
|
2477
|
-
break;
|
|
2478
|
-
}
|
|
2479
|
-
matched = true;
|
|
2480
|
-
for (const { handler } of pipe) if (!handler(fork, target)) {
|
|
2481
|
-
matched = false;
|
|
2482
|
-
break;
|
|
2483
|
-
}
|
|
2484
|
-
if (fork.index > depth) depth = fork.index;
|
|
2485
|
-
if (matched) {
|
|
2486
|
-
nodes.push(target.node);
|
|
2487
|
-
ni.unfork(fork);
|
|
2488
|
-
if (stopCondition && stopCondition(fork)) return nodes;
|
|
2489
|
-
ni.move(-1);
|
|
2490
|
-
break;
|
|
2491
|
-
}
|
|
2492
|
-
}
|
|
2493
|
-
if (singlePass) return nodes;
|
|
2494
|
-
if (!matched) {
|
|
2495
|
-
ni.shouldHaveError(depth);
|
|
2496
|
-
ni.move(depth - ni.index);
|
|
2497
|
-
}
|
|
1876
|
+
function parseValue(ni) {
|
|
1877
|
+
if (!ni.$) {
|
|
1878
|
+
pushError(ni, "Expected value");
|
|
1879
|
+
return;
|
|
1880
|
+
}
|
|
1881
|
+
const token = ni.$;
|
|
1882
|
+
if (token.type === "text" || token.type === "number" || token.type === "regexp" || token.type === "identifier" && VALUE_KEYWORDS.has(token.text)) {
|
|
1883
|
+
const node = new SemanticQueryValueNode();
|
|
1884
|
+
node.valueToken = new Token(token);
|
|
2498
1885
|
ni.move();
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
1886
|
+
return node;
|
|
1887
|
+
}
|
|
1888
|
+
pushError(ni, `Unexpected token "${token.text}" where value expected`);
|
|
1889
|
+
}
|
|
1890
|
+
function parseValueList(ni) {
|
|
1891
|
+
if (ni.$?.type === "block" && ni.$.text === "(") {
|
|
1892
|
+
const subNi = ni.fork(ni.$.children || []);
|
|
1893
|
+
ni.move();
|
|
1894
|
+
if (!subNi.$) {
|
|
1895
|
+
pushError(subNi, "Empty value list in \"in\" expression");
|
|
1896
|
+
return;
|
|
1897
|
+
}
|
|
1898
|
+
const values = [];
|
|
1899
|
+
const first = parseValue(subNi);
|
|
1900
|
+
if (first) values.push(first);
|
|
1901
|
+
while (subNi.$?.type === "punctuation" && subNi.$.text === ",") {
|
|
1902
|
+
subNi.move();
|
|
1903
|
+
const v = parseValue(subNi);
|
|
1904
|
+
if (v) values.push(v);
|
|
2502
1905
|
}
|
|
1906
|
+
if (subNi.$) pushError(subNi, `Unexpected token in value list: "${subNi.$.text}"`);
|
|
1907
|
+
const node = new SemanticQueryValueListNode();
|
|
1908
|
+
node.values = values;
|
|
1909
|
+
return node;
|
|
2503
1910
|
}
|
|
2504
|
-
|
|
1911
|
+
pushError(ni, "Expected parenthesized value list after \"in\"");
|
|
2505
1912
|
}
|
|
2506
|
-
|
|
2507
|
-
|
|
1913
|
+
/**
|
|
1914
|
+
* Range to blame for a diagnostic: the current token, else the enclosing
|
|
1915
|
+
* query token / "(...)" block the iterator was forked from.
|
|
1916
|
+
*/ function rangeOf(ni) {
|
|
1917
|
+
return ni.$?.getRange?.() ?? ni.parent?.getRange?.() ?? ZERO_RANGE;
|
|
1918
|
+
}
|
|
1919
|
+
function pushError(ni, message) {
|
|
1920
|
+
ni.messages.push({
|
|
1921
|
+
severity: 1,
|
|
1922
|
+
message,
|
|
1923
|
+
range: rangeOf(ni)
|
|
1924
|
+
});
|
|
2508
1925
|
}
|
|
2509
1926
|
|
|
2510
1927
|
//#endregion
|
|
2511
|
-
//#region packages/core/src/parser/
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
|
|
2525
|
-
|
|
2526
|
-
continue;
|
|
2527
|
-
}
|
|
2528
|
-
const prev = currentPass[i - 1]?.text;
|
|
2529
|
-
const next = currentPass[i + 1]?.text;
|
|
2530
|
-
if (prev !== p && next !== p) {
|
|
2531
|
-
temporaryResult.push(node);
|
|
2532
|
-
continue;
|
|
2533
|
-
}
|
|
2534
|
-
if (prev === p || next === p) {
|
|
2535
|
-
groupped.push(node);
|
|
2536
|
-
if (next !== p) {
|
|
2537
|
-
temporaryResult.push(new $n.SemanticGroup(groupped, p));
|
|
2538
|
-
groupped = [];
|
|
2539
|
-
}
|
|
2540
|
-
}
|
|
2541
|
-
}
|
|
2542
|
-
currentPass = temporaryResult;
|
|
2543
|
-
temporaryResult = [];
|
|
1928
|
+
//#region packages/core/src/parser/pipes/expr.pipe.ts
|
|
1929
|
+
/**
|
|
1930
|
+
* Parse a backtick annotation argument as the given kind and attach the tree to
|
|
1931
|
+
* the token (`queryNode`, `exprNode` or `orderNode`). Returns the diagnostics.
|
|
1932
|
+
*/ function parseBacktickArg(argToken, kind) {
|
|
1933
|
+
const messages = [];
|
|
1934
|
+
const ni = new NodeIterator(argToken.children, messages, /* @__PURE__ */ new Map(), {}, argToken.lexicalToken, 0).update();
|
|
1935
|
+
switch (kind) {
|
|
1936
|
+
case "expr":
|
|
1937
|
+
argToken.exprNode = parseExprExpression(ni, argToken);
|
|
1938
|
+
break;
|
|
1939
|
+
case "order":
|
|
1940
|
+
argToken.orderNode = parseOrderList(ni, argToken);
|
|
1941
|
+
break;
|
|
1942
|
+
default: argToken.queryNode = parseQueryExpression(ni, argToken);
|
|
2544
1943
|
}
|
|
2545
|
-
|
|
2546
|
-
return output.length === 1 ? output.first : output;
|
|
1944
|
+
return messages;
|
|
2547
1945
|
}
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
"
|
|
2556
|
-
"
|
|
2557
|
-
"<="
|
|
2558
|
-
]);
|
|
2559
|
-
const VALUE_KEYWORDS = new Set([
|
|
2560
|
-
"true",
|
|
2561
|
-
"false",
|
|
2562
|
-
"null",
|
|
2563
|
-
"undefined"
|
|
2564
|
-
]);
|
|
2565
|
-
const ZERO_RANGE = {
|
|
2566
|
-
start: {
|
|
2567
|
-
line: 0,
|
|
2568
|
-
character: 0
|
|
2569
|
-
},
|
|
2570
|
-
end: {
|
|
2571
|
-
line: 0,
|
|
2572
|
-
character: 0
|
|
2573
|
-
}
|
|
1946
|
+
/** Vocabulary of `expr` / `order` arguments: arithmetic operators, functions, order directions. */ const BACKTICK_ARG_VOCABULARY = {
|
|
1947
|
+
arithmeticOps: [
|
|
1948
|
+
"+",
|
|
1949
|
+
"-",
|
|
1950
|
+
"*",
|
|
1951
|
+
"/"
|
|
1952
|
+
],
|
|
1953
|
+
functions: ["coalesce"],
|
|
1954
|
+
orderDirections: ["asc", "desc"]
|
|
2574
1955
|
};
|
|
1956
|
+
const ORDER_DIRECTIONS = new Set(BACKTICK_ARG_VOCABULARY.orderDirections);
|
|
2575
1957
|
/**
|
|
2576
|
-
* Parse a backtick
|
|
1958
|
+
* Parse a backtick arithmetic expression:
|
|
2577
1959
|
*
|
|
2578
|
-
*
|
|
2579
|
-
*
|
|
2580
|
-
*
|
|
2581
|
-
*
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
1960
|
+
* ```
|
|
1961
|
+
* expr := additive
|
|
1962
|
+
* additive := mult (('+' | '-') mult)*
|
|
1963
|
+
* mult := unary (('*' | '/') unary)*
|
|
1964
|
+
* unary := '-' unary | primary
|
|
1965
|
+
* primary := NUMBER | fieldRef | 'coalesce' '(' expr (',' expr)+ ')' | '(' expr ')'
|
|
1966
|
+
* ```
|
|
1967
|
+
*
|
|
1968
|
+
* A signed number in binary-operator position (`a -5`, `a+1`) is split into the
|
|
1969
|
+
* operator and an unsigned operand. Returns `undefined` when the content is empty.
|
|
1970
|
+
*/ function parseExprExpression(ni, sourceToken) {
|
|
1971
|
+
if (!ni.$) {
|
|
1972
|
+
pushError(ni, "Empty expression");
|
|
1973
|
+
return;
|
|
1974
|
+
}
|
|
1975
|
+
const expr = parseAdditive(ni);
|
|
2585
1976
|
if (!expr) return;
|
|
2586
|
-
if (ni.$) pushError(ni, `Unexpected token in
|
|
2587
|
-
const node = new
|
|
1977
|
+
if (ni.$) pushError(ni, `Unexpected token in expression: "${ni.$.text}"`);
|
|
1978
|
+
const node = new SemanticExprNode();
|
|
2588
1979
|
node.expression = expr;
|
|
2589
1980
|
node.sourceToken = sourceToken;
|
|
2590
1981
|
return node;
|
|
2591
1982
|
}
|
|
2592
|
-
function
|
|
2593
|
-
|
|
1983
|
+
function isPunct(t, chars) {
|
|
1984
|
+
return t?.type === "punctuation" && t.text.length === 1 && chars.includes(t.text);
|
|
1985
|
+
}
|
|
1986
|
+
function isParenBlock(t) {
|
|
1987
|
+
return t?.type === "block" && t.text === "(";
|
|
1988
|
+
}
|
|
1989
|
+
/** A number token whose text carries a sign (`-5`, `+1`). */ function signedNumber(t) {
|
|
1990
|
+
if (t?.type !== "number") return;
|
|
1991
|
+
const c = t.text.charAt(0);
|
|
1992
|
+
return c === "-" || c === "+" ? c : void 0;
|
|
1993
|
+
}
|
|
1994
|
+
function binary(op, left, right) {
|
|
1995
|
+
const node = new SemanticExprBinaryNode();
|
|
1996
|
+
node.op = op;
|
|
1997
|
+
node.left = left;
|
|
1998
|
+
node.right = right;
|
|
1999
|
+
return node;
|
|
2000
|
+
}
|
|
2001
|
+
function parseAdditive(ni) {
|
|
2002
|
+
let left = parseMult(ni);
|
|
2594
2003
|
if (!left) return;
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
ni
|
|
2598
|
-
|
|
2004
|
+
for (;;) {
|
|
2005
|
+
const punct = isPunct(ni.$, "+-");
|
|
2006
|
+
const op = punct ? ni.$?.text : signedNumber(ni.$);
|
|
2007
|
+
if (!op) break;
|
|
2008
|
+
if (punct) ni.move();
|
|
2009
|
+
const right = parseMult(ni, !punct);
|
|
2599
2010
|
if (!right) {
|
|
2600
|
-
pushError(ni,
|
|
2601
|
-
|
|
2011
|
+
pushError(ni, `Expected operand after "${op}"`);
|
|
2012
|
+
return;
|
|
2602
2013
|
}
|
|
2603
|
-
|
|
2014
|
+
left = binary(op, left, right);
|
|
2604
2015
|
}
|
|
2605
|
-
|
|
2606
|
-
const node = new SemanticQueryLogicalNode();
|
|
2607
|
-
node.operator = "or";
|
|
2608
|
-
node.operands = operands;
|
|
2609
|
-
return node;
|
|
2016
|
+
return left;
|
|
2610
2017
|
}
|
|
2611
|
-
function
|
|
2612
|
-
|
|
2018
|
+
function parseMult(ni, unsignFirst = false) {
|
|
2019
|
+
let left = parseUnary(ni, unsignFirst);
|
|
2613
2020
|
if (!left) return;
|
|
2614
|
-
|
|
2615
|
-
|
|
2021
|
+
for (let t = ni.$; isPunct(t, "*/"); t = ni.$) {
|
|
2022
|
+
const op = t.text;
|
|
2616
2023
|
ni.move();
|
|
2617
|
-
const right =
|
|
2024
|
+
const right = parseUnary(ni);
|
|
2618
2025
|
if (!right) {
|
|
2619
|
-
pushError(ni,
|
|
2620
|
-
|
|
2026
|
+
pushError(ni, `Expected operand after "${op}"`);
|
|
2027
|
+
return;
|
|
2621
2028
|
}
|
|
2622
|
-
|
|
2029
|
+
left = binary(op, left, right);
|
|
2623
2030
|
}
|
|
2624
|
-
|
|
2625
|
-
const node = new SemanticQueryLogicalNode();
|
|
2626
|
-
node.operator = "and";
|
|
2627
|
-
node.operands = operands;
|
|
2628
|
-
return node;
|
|
2031
|
+
return left;
|
|
2629
2032
|
}
|
|
2630
|
-
function
|
|
2631
|
-
if (
|
|
2033
|
+
function parseUnary(ni, unsign = false) {
|
|
2034
|
+
if (!unsign && isPunct(ni.$, "-")) {
|
|
2632
2035
|
ni.move();
|
|
2633
|
-
const operand =
|
|
2036
|
+
const operand = parseUnary(ni);
|
|
2634
2037
|
if (!operand) {
|
|
2635
|
-
pushError(ni, "Expected
|
|
2038
|
+
pushError(ni, "Expected operand after \"-\"");
|
|
2636
2039
|
return;
|
|
2637
2040
|
}
|
|
2638
|
-
const node = new
|
|
2639
|
-
node.
|
|
2640
|
-
node.operands = [operand];
|
|
2041
|
+
const node = new SemanticExprUnaryNode();
|
|
2042
|
+
node.operand = operand;
|
|
2641
2043
|
return node;
|
|
2642
2044
|
}
|
|
2643
|
-
|
|
2644
|
-
|
|
2045
|
+
return parsePrimary(ni, unsign);
|
|
2046
|
+
}
|
|
2047
|
+
function parsePrimary(ni, unsign = false) {
|
|
2048
|
+
const t = ni.$;
|
|
2049
|
+
if (!t) {
|
|
2050
|
+
pushError(ni, "Expected operand");
|
|
2051
|
+
return;
|
|
2052
|
+
}
|
|
2053
|
+
if (t.type === "number") return parseNumber(ni, t, unsign);
|
|
2054
|
+
if (isParenBlock(t)) {
|
|
2055
|
+
const subNi = ni.fork(t.children || []);
|
|
2645
2056
|
ni.move();
|
|
2646
2057
|
if (!subNi.$) {
|
|
2647
2058
|
pushError(subNi, "Empty parenthesized expression");
|
|
2648
2059
|
return;
|
|
2649
2060
|
}
|
|
2650
|
-
const expr =
|
|
2651
|
-
if (subNi.$) pushError(subNi, `Unexpected token in parenthesized expression: "${subNi.$.text}"`);
|
|
2061
|
+
const expr = parseAdditive(subNi);
|
|
2062
|
+
if (expr && subNi.$) pushError(subNi, `Unexpected token in parenthesized expression: "${subNi.$.text}"`);
|
|
2652
2063
|
return expr;
|
|
2653
2064
|
}
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
|
|
2657
|
-
const left = parseFieldRef(ni);
|
|
2658
|
-
if (!left) return;
|
|
2659
|
-
if (ni.$?.type === "punctuation" && SYMBOLIC_OPS.has(ni.$.text)) {
|
|
2660
|
-
const opText = ni.$.text;
|
|
2661
|
-
ni.move();
|
|
2662
|
-
const right = parseValueOrFieldRef(ni);
|
|
2663
|
-
if (!right) {
|
|
2664
|
-
pushError(ni, `Expected value or field reference after "${opText}"`);
|
|
2065
|
+
if (t.type === "identifier") {
|
|
2066
|
+
if (VALUE_KEYWORDS.has(t.text)) {
|
|
2067
|
+
pushError(ni, `"${t.text}" is not allowed in an arithmetic expression`);
|
|
2665
2068
|
return;
|
|
2666
2069
|
}
|
|
2667
|
-
|
|
2668
|
-
node.left = left;
|
|
2669
|
-
node.operator = opText;
|
|
2670
|
-
node.right = right;
|
|
2671
|
-
return node;
|
|
2672
|
-
}
|
|
2673
|
-
if (ni.$?.type === "identifier") {
|
|
2674
|
-
const opText = ni.$.text;
|
|
2675
|
-
if (opText === "in") {
|
|
2676
|
-
ni.move();
|
|
2677
|
-
const right = parseValueList(ni);
|
|
2678
|
-
if (!right) return;
|
|
2679
|
-
const node = new SemanticQueryComparisonNode();
|
|
2680
|
-
node.left = left;
|
|
2681
|
-
node.operator = "in";
|
|
2682
|
-
node.right = right;
|
|
2683
|
-
return node;
|
|
2684
|
-
}
|
|
2685
|
-
if (opText === "matches") {
|
|
2686
|
-
ni.move();
|
|
2687
|
-
const right = parseValueOrFieldRef(ni);
|
|
2688
|
-
if (!right) {
|
|
2689
|
-
pushError(ni, "Expected value after \"matches\"");
|
|
2690
|
-
return;
|
|
2691
|
-
}
|
|
2692
|
-
const node = new SemanticQueryComparisonNode();
|
|
2693
|
-
node.left = left;
|
|
2694
|
-
node.operator = "matches";
|
|
2695
|
-
node.right = right;
|
|
2696
|
-
return node;
|
|
2697
|
-
}
|
|
2698
|
-
if (opText === "exists") {
|
|
2070
|
+
if (t.text === "coalesce" && isParenBlock(ni.next().$)) {
|
|
2699
2071
|
ni.move();
|
|
2700
|
-
|
|
2701
|
-
node.left = left;
|
|
2702
|
-
node.operator = "exists";
|
|
2703
|
-
return node;
|
|
2072
|
+
return parseCoalesce(ni);
|
|
2704
2073
|
}
|
|
2705
|
-
|
|
2706
|
-
|
|
2074
|
+
return parseFieldRef(ni);
|
|
2075
|
+
}
|
|
2076
|
+
pushError(ni, `Unexpected token in expression: "${t.text}"`);
|
|
2077
|
+
}
|
|
2078
|
+
/** Why a number literal cannot be used in an expression (or `undefined` when it can). */ function numberLiteralProblem(text, value) {
|
|
2079
|
+
if (!Number.isFinite(value)) return "is out of range";
|
|
2080
|
+
if (value === 0 && /[1-9]/.test(text.replace(/[eE].*$/, ""))) return "underflows to 0";
|
|
2081
|
+
if (Math.abs(value) > Number.MAX_SAFE_INTEGER) return "exceeds the safe integer range (±2^53 - 1)";
|
|
2082
|
+
}
|
|
2083
|
+
function parseNumber(ni, lexical, unsign) {
|
|
2084
|
+
const token = new Token(lexical);
|
|
2085
|
+
const text = unsign ? token.text.slice(1) : token.text;
|
|
2086
|
+
const value = Number(text);
|
|
2087
|
+
const problem = numberLiteralProblem(text, value);
|
|
2088
|
+
if (problem) {
|
|
2089
|
+
pushError(ni, `Number literal "${token.text}" ${problem}`);
|
|
2090
|
+
ni.move();
|
|
2707
2091
|
return;
|
|
2708
2092
|
}
|
|
2709
|
-
|
|
2093
|
+
ni.move();
|
|
2094
|
+
const node = new SemanticExprNumberNode();
|
|
2095
|
+
node.valueToken = token;
|
|
2096
|
+
node.value = value;
|
|
2097
|
+
return node;
|
|
2710
2098
|
}
|
|
2711
|
-
function
|
|
2099
|
+
function parseCoalesce(ni) {
|
|
2100
|
+
const subNi = ni.fork(ni.$?.children || []);
|
|
2712
2101
|
ni.move();
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
const
|
|
2716
|
-
if (!
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
2102
|
+
const args = [];
|
|
2103
|
+
if (subNi.$) for (;;) {
|
|
2104
|
+
const arg = parseAdditive(subNi);
|
|
2105
|
+
if (!arg) return;
|
|
2106
|
+
args.push(arg);
|
|
2107
|
+
if (!isPunct(subNi.$, ",")) break;
|
|
2108
|
+
subNi.move();
|
|
2109
|
+
}
|
|
2110
|
+
if (subNi.$) {
|
|
2111
|
+
pushError(subNi, `Unexpected token in coalesce(): "${subNi.$.text}"`);
|
|
2112
|
+
return;
|
|
2722
2113
|
}
|
|
2723
|
-
if (
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
node.left = left;
|
|
2727
|
-
node.operator = "not exists";
|
|
2728
|
-
return node;
|
|
2114
|
+
if (args.length < 2) {
|
|
2115
|
+
pushError(subNi, "coalesce() requires at least 2 arguments");
|
|
2116
|
+
return;
|
|
2729
2117
|
}
|
|
2730
|
-
|
|
2118
|
+
const node = new SemanticExprCallNode();
|
|
2119
|
+
node.args = args;
|
|
2120
|
+
return node;
|
|
2731
2121
|
}
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2122
|
+
/**
|
|
2123
|
+
* Parse a backtick ordering: `key (asc|desc)? (, key (asc|desc)?)*`.
|
|
2124
|
+
* Returns `undefined` when the content is empty or a key does not parse.
|
|
2125
|
+
*/ function parseOrderList(ni, sourceToken) {
|
|
2126
|
+
if (!ni.$) {
|
|
2127
|
+
pushError(ni, "Empty order list");
|
|
2735
2128
|
return;
|
|
2736
2129
|
}
|
|
2737
|
-
const
|
|
2738
|
-
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
|
|
2742
|
-
|
|
2130
|
+
const items = [];
|
|
2131
|
+
for (;;) {
|
|
2132
|
+
const ref = parseFieldRef(ni);
|
|
2133
|
+
if (!ref) return;
|
|
2134
|
+
const item = {
|
|
2135
|
+
ref,
|
|
2136
|
+
desc: false
|
|
2137
|
+
};
|
|
2138
|
+
if (ni.$?.type === "identifier" && ORDER_DIRECTIONS.has(ni.$.text)) {
|
|
2139
|
+
item.desc = ni.$.text === "desc";
|
|
2140
|
+
item.dirToken = new Token(ni.$);
|
|
2743
2141
|
ni.move();
|
|
2744
|
-
|
|
2745
|
-
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
|
|
2142
|
+
}
|
|
2143
|
+
items.push(item);
|
|
2144
|
+
if (!ni.$) break;
|
|
2145
|
+
if (isPunct(ni.$, ",")) {
|
|
2146
|
+
ni.move();
|
|
2147
|
+
continue;
|
|
2148
|
+
}
|
|
2149
|
+
pushError(ni, `Unexpected token in order list: "${ni.$.text}" (expected "," or "asc" / "desc")`);
|
|
2150
|
+
return;
|
|
2151
|
+
}
|
|
2152
|
+
const node = new SemanticOrderNode();
|
|
2153
|
+
node.items = items;
|
|
2154
|
+
node.sourceToken = sourceToken;
|
|
2155
|
+
return node;
|
|
2156
|
+
}
|
|
2157
|
+
|
|
2158
|
+
//#endregion
|
|
2159
|
+
//#region packages/core/src/lsp/legacy-db-query-scope.ts
|
|
2160
|
+
/**
|
|
2161
|
+
* @deprecated Built-in editor scopes for the `@db.view.filter`, `@db.view.joins` and
|
|
2162
|
+
* `@db.rel.filter` query arguments. They are a fallback for DB plugin versions whose
|
|
2163
|
+
* argument specs do not declare a `fieldScope` hook yet — the DB plugin owns these
|
|
2164
|
+
* rules now. Scheduled for removal in `@atscript/core` 0.2.0 (next minor).
|
|
2165
|
+
*/
|
|
2166
|
+
/**
|
|
2167
|
+
* @deprecated Query scope of a built-in `@db.*` query argument (see the module note);
|
|
2168
|
+
* `undefined` for any other annotation.
|
|
2169
|
+
*/ function getLegacyDbQueryScope(queryArgToken) {
|
|
2170
|
+
const owner = queryArgToken.parentNode;
|
|
2171
|
+
if (!owner) return;
|
|
2172
|
+
switch (queryArgToken.annotationRef?.text.slice(1)) {
|
|
2173
|
+
case "db.view.filter": return getViewFilterScope(owner);
|
|
2174
|
+
case "db.view.joins": return getViewJoinsScope(owner, queryArgToken);
|
|
2175
|
+
case "db.rel.filter": return getRelFilterScope(owner);
|
|
2176
|
+
default: return;
|
|
2177
|
+
}
|
|
2178
|
+
}
|
|
2179
|
+
function getViewFilterScope(owner) {
|
|
2180
|
+
const entryTypeName = (owner.annotations?.filter((a) => a.name === "db.view.for"))?.[0]?.args[0]?.text;
|
|
2181
|
+
if (!entryTypeName) return;
|
|
2182
|
+
const allowedTypes = [entryTypeName];
|
|
2183
|
+
const joinsAnnotations = owner.annotations?.filter((a) => a.name === "db.view.joins");
|
|
2184
|
+
if (joinsAnnotations) {
|
|
2185
|
+
for (const join of joinsAnnotations) if (join.args[0]) allowedTypes.push(join.args[0].text);
|
|
2186
|
+
}
|
|
2187
|
+
return {
|
|
2188
|
+
allowedTypes,
|
|
2189
|
+
unqualifiedTarget: entryTypeName
|
|
2190
|
+
};
|
|
2191
|
+
}
|
|
2192
|
+
function getViewJoinsScope(owner, queryArgToken) {
|
|
2193
|
+
const entryTypeName = (owner.annotations?.filter((a) => a.name === "db.view.for"))?.[0]?.args[0]?.text;
|
|
2194
|
+
if (!entryTypeName) return;
|
|
2195
|
+
const joinTargetName = (owner.annotations?.find((a) => a.name === "db.view.joins" && a.args.includes(queryArgToken)))?.args[0]?.text;
|
|
2196
|
+
if (!joinTargetName) return;
|
|
2197
|
+
return {
|
|
2198
|
+
allowedTypes: [joinTargetName, entryTypeName],
|
|
2199
|
+
unqualifiedTarget: entryTypeName
|
|
2200
|
+
};
|
|
2201
|
+
}
|
|
2202
|
+
function getRelFilterScope(owner) {
|
|
2203
|
+
let def = owner.getDefinition();
|
|
2204
|
+
if (isArray(def)) def = def?.getDefinition();
|
|
2205
|
+
if (!isRef(def)) return;
|
|
2206
|
+
const targetTypeName = def.id;
|
|
2207
|
+
const allowedTypes = [targetTypeName];
|
|
2208
|
+
const viaAnnotation = owner.annotations?.find((a) => a.name === "db.rel.via");
|
|
2209
|
+
if (viaAnnotation?.args[0]) allowedTypes.push(viaAnnotation.args[0].text);
|
|
2210
|
+
return {
|
|
2211
|
+
allowedTypes,
|
|
2212
|
+
unqualifiedTarget: targetTypeName
|
|
2213
|
+
};
|
|
2214
|
+
}
|
|
2215
|
+
|
|
2216
|
+
//#endregion
|
|
2217
|
+
//#region packages/core/src/lsp/field-refs.ts
|
|
2218
|
+
function getArgSpec(argToken, doc) {
|
|
2219
|
+
const annotationRef = argToken.annotationRef;
|
|
2220
|
+
if (!annotationRef || typeof argToken.index !== "number") return;
|
|
2221
|
+
return doc.resolveAnnotation(annotationRef.text.slice(1))?.arguments[argToken.index];
|
|
2222
|
+
}
|
|
2223
|
+
/**
|
|
2224
|
+
* Field scope of an annotation argument: the answer of the argument spec's `fieldScope`
|
|
2225
|
+
* hook when it declares one, otherwise the deprecated built-in `@db.*` query rules.
|
|
2226
|
+
*/ function getQueryScope(argToken, doc) {
|
|
2227
|
+
const argSpec = getArgSpec(argToken, doc);
|
|
2228
|
+
return argSpec?.fieldScope ? argSpec.fieldScope(argToken, doc) : getLegacyDbQueryScope(argToken);
|
|
2229
|
+
}
|
|
2230
|
+
/**
|
|
2231
|
+
* Type a field-path `string` argument names a field of: the `unqualifiedTarget` of the
|
|
2232
|
+
* argument spec's `fieldScope`. `undefined` for any other token.
|
|
2233
|
+
*/ function getFieldPathTarget(argToken, doc) {
|
|
2234
|
+
if (argToken.type !== "text") return;
|
|
2235
|
+
const argSpec = getArgSpec(argToken, doc);
|
|
2236
|
+
if (argSpec?.type !== "string" || !argSpec.fieldScope) return;
|
|
2237
|
+
return argSpec.fieldScope(argToken, doc)?.unqualifiedTarget ?? void 0;
|
|
2238
|
+
}
|
|
2239
|
+
/**
|
|
2240
|
+
* Resolve a (possibly dotted) field path of a type to the property it names and the
|
|
2241
|
+
* document that declares that property.
|
|
2242
|
+
*/ function resolveFieldPath(doc, typeName, segment) {
|
|
2243
|
+
const chain = segment.path.split(".");
|
|
2244
|
+
const [name] = chain.splice(-1);
|
|
2245
|
+
const parent = doc.unwindType(typeName, chain);
|
|
2246
|
+
if (!parent || !(isStructure(parent.def) || isInterface(parent.def))) return;
|
|
2247
|
+
const prop = parent.def.props.get(name);
|
|
2248
|
+
return prop && {
|
|
2249
|
+
typeName,
|
|
2250
|
+
range: segment.range,
|
|
2251
|
+
prop,
|
|
2252
|
+
doc: parent.doc
|
|
2253
|
+
};
|
|
2254
|
+
}
|
|
2255
|
+
/**
|
|
2256
|
+
* Segments of a field-path string argument (`'address.city'` → `address`, `city`), with
|
|
2257
|
+
* ranges that exclude the quotes. A string spanning several lines is a single segment.
|
|
2258
|
+
*/ function fieldPathSegments(argToken) {
|
|
2259
|
+
const { text, range } = argToken;
|
|
2260
|
+
if (range.start.line !== range.end.line) return [{
|
|
2261
|
+
path: text,
|
|
2262
|
+
range
|
|
2263
|
+
}];
|
|
2264
|
+
const line = range.start.line;
|
|
2265
|
+
const contentStart = range.start.character + 1;
|
|
2266
|
+
const segments = [];
|
|
2267
|
+
let start = 0;
|
|
2268
|
+
for (const part of text.split(".")) {
|
|
2269
|
+
const end = start + part.length;
|
|
2270
|
+
segments.push({
|
|
2271
|
+
path: text.slice(0, end),
|
|
2272
|
+
range: {
|
|
2273
|
+
start: {
|
|
2274
|
+
line,
|
|
2275
|
+
character: contentStart + start
|
|
2276
|
+
},
|
|
2277
|
+
end: {
|
|
2278
|
+
line,
|
|
2279
|
+
character: contentStart + end
|
|
2280
|
+
}
|
|
2281
|
+
}
|
|
2282
|
+
});
|
|
2283
|
+
start = end + 1;
|
|
2284
|
+
}
|
|
2285
|
+
return segments;
|
|
2286
|
+
}
|
|
2287
|
+
/** The segment under `character`, or the last segment when `character` is omitted. */ function fieldPathSegmentAt(argToken, character) {
|
|
2288
|
+
const segments = fieldPathSegments(argToken);
|
|
2289
|
+
return (character === void 0 ? void 0 : segments.find((s) => s.range.end.character >= character)) ?? segments[segments.length - 1];
|
|
2290
|
+
}
|
|
2291
|
+
/**
|
|
2292
|
+
* Resolve a field reference token to the property it names:
|
|
2293
|
+
* - a field ref inside a `query` argument (`amount`, `Customer.name` in backticks);
|
|
2294
|
+
* - a `string` argument whose spec declares `fieldScope`: a (dotted) field path of the
|
|
2295
|
+
* scope's `unqualifiedTarget`. With `character` (a cursor column), the path resolves
|
|
2296
|
+
* only up to the segment under the cursor — `'address.city'` with the cursor on
|
|
2297
|
+
* `address` resolves `address` — and `range` is that segment's range.
|
|
2298
|
+
*
|
|
2299
|
+
* Returns `undefined` for any other token, or when the scope or the path does not resolve.
|
|
2300
|
+
*/ function resolveFieldRefAt(token, doc, character) {
|
|
2301
|
+
if (isQueryFieldRef(token.parentNode)) {
|
|
2302
|
+
const { fieldRef, typeRef, queryArgToken } = token.parentNode;
|
|
2303
|
+
const scope = token === fieldRef && queryArgToken ? getQueryScope(queryArgToken, doc) : void 0;
|
|
2304
|
+
const typeName = scope && (typeRef?.text ?? scope.unqualifiedTarget);
|
|
2305
|
+
return typeName ? resolveFieldPath(doc, typeName, {
|
|
2306
|
+
path: token.text,
|
|
2307
|
+
range: token.range
|
|
2308
|
+
}) : void 0;
|
|
2309
|
+
}
|
|
2310
|
+
const typeName = getFieldPathTarget(token, doc);
|
|
2311
|
+
return typeName ? resolveFieldPath(doc, typeName, fieldPathSegmentAt(token, character)) : void 0;
|
|
2312
|
+
}
|
|
2313
|
+
/**
|
|
2314
|
+
* Resolve the segments named `fieldName` of a field-path `string` argument
|
|
2315
|
+
* (`'address.city'` has the segments `address` and `city`). Supports find-references.
|
|
2316
|
+
*/ function resolveFieldPathArgSegments(argToken, doc, fieldName) {
|
|
2317
|
+
const segments = fieldPathSegments(argToken).filter((s) => s.path === fieldName || s.path.endsWith(`.${fieldName}`));
|
|
2318
|
+
const typeName = segments.length > 0 ? getFieldPathTarget(argToken, doc) : void 0;
|
|
2319
|
+
if (!typeName) return [];
|
|
2320
|
+
const targets = [];
|
|
2321
|
+
for (const segment of segments) {
|
|
2322
|
+
const target = resolveFieldPath(doc, typeName, segment);
|
|
2323
|
+
if (target) targets.push(target);
|
|
2324
|
+
}
|
|
2325
|
+
return targets;
|
|
2326
|
+
}
|
|
2327
|
+
/**
|
|
2328
|
+
* Resolve a query field ref node to a property definition.
|
|
2329
|
+
*/ function resolveQueryFieldRef(fieldRefNode, doc, scope) {
|
|
2330
|
+
const typeName = fieldRefNode.typeRef?.text ?? scope.unqualifiedTarget;
|
|
2331
|
+
const { fieldRef } = fieldRefNode;
|
|
2332
|
+
const resolved = typeName ? resolveFieldPath(doc, typeName, {
|
|
2333
|
+
path: fieldRef.text,
|
|
2334
|
+
range: fieldRef.range
|
|
2335
|
+
}) : void 0;
|
|
2336
|
+
return resolved && {
|
|
2337
|
+
targetUri: resolved.doc.id,
|
|
2338
|
+
doc: resolved.doc,
|
|
2339
|
+
prop: resolved.prop
|
|
2340
|
+
};
|
|
2341
|
+
}
|
|
2342
|
+
/**
|
|
2343
|
+
* Get fields available for a type (resolving extends/intersections).
|
|
2344
|
+
* With `chain`, returns the fields of the nested object that field path leads to.
|
|
2345
|
+
*/ function getFieldsForType(doc, typeName, chain = []) {
|
|
2346
|
+
const unwound = doc.unwindType(typeName, chain);
|
|
2347
|
+
if (!unwound?.def) return [];
|
|
2348
|
+
let def = unwound.doc.mergeIntersection(unwound.def);
|
|
2349
|
+
if (isInterface(def)) def = unwound.doc.resolveInterfaceExtends(def) || def.getDefinition() || def;
|
|
2350
|
+
if (isStructure(def) || isInterface(def)) return Array.from(def.props.values());
|
|
2351
|
+
return [];
|
|
2352
|
+
}
|
|
2353
|
+
/**
|
|
2354
|
+
* Completion scope of a field-path `string` argument (spec declares `fieldScope`) at the
|
|
2355
|
+
* cursor column `character`: the fields of the level being typed — `'address.ci'` offers
|
|
2356
|
+
* the fields of `address` (`chain` = `['address']`).
|
|
2357
|
+
*/ function getFieldPathCompletionScope(argToken, doc, character) {
|
|
2358
|
+
const typeName = getFieldPathTarget(argToken, doc);
|
|
2359
|
+
if (!typeName) return;
|
|
2360
|
+
const chain = fieldPathSegmentAt(argToken, character).path.split(".").slice(0, -1);
|
|
2361
|
+
return {
|
|
2362
|
+
typeName,
|
|
2363
|
+
chain,
|
|
2364
|
+
fields: getFieldsForType(doc, typeName, chain)
|
|
2365
|
+
};
|
|
2366
|
+
}
|
|
2367
|
+
/**
|
|
2368
|
+
* Get completion scope data for a query context.
|
|
2369
|
+
*/ function getQueryCompletionScope(queryArgToken, doc) {
|
|
2370
|
+
const scope = getQueryScope(queryArgToken, doc);
|
|
2371
|
+
if (!scope) return;
|
|
2372
|
+
return {
|
|
2373
|
+
typeNames: scope.allowedTypes,
|
|
2374
|
+
unqualifiedTarget: scope.unqualifiedTarget,
|
|
2375
|
+
getFields: (typeName) => getFieldsForType(doc, typeName)
|
|
2376
|
+
};
|
|
2377
|
+
}
|
|
2378
|
+
const KEYWORD_OPS = new Set([
|
|
2379
|
+
"in",
|
|
2380
|
+
"matches",
|
|
2381
|
+
"exists"
|
|
2382
|
+
]);
|
|
2383
|
+
const LOGICAL_KEYWORDS = new Set([
|
|
2384
|
+
"and",
|
|
2385
|
+
"or",
|
|
2386
|
+
"not"
|
|
2387
|
+
]);
|
|
2388
|
+
/**
|
|
2389
|
+
* Analyze text inside backticks up to cursor position
|
|
2390
|
+
* to determine what kind of completions to offer.
|
|
2391
|
+
* Uses lightweight text scanning, not full AST parsing,
|
|
2392
|
+
* so it works on incomplete/mid-typing input.
|
|
2393
|
+
*
|
|
2394
|
+
* `mode` selects the grammar of the argument (default `query`).
|
|
2395
|
+
*/ function analyzeQueryCursorContext(textBeforeCursor, mode = "query") {
|
|
2396
|
+
const trimmed = textBeforeCursor.trimEnd();
|
|
2397
|
+
const hasTrailingSpace = textBeforeCursor.length > trimmed.length;
|
|
2398
|
+
if (trimmed.length === 0) return { type: "field-start" };
|
|
2399
|
+
if (trimmed.endsWith(".")) {
|
|
2400
|
+
const before = trimmed.slice(0, -1).trimEnd();
|
|
2401
|
+
const match = /(\w+)$/u.exec(before);
|
|
2402
|
+
if (match) return {
|
|
2403
|
+
type: "after-dot",
|
|
2404
|
+
typeName: match[1]
|
|
2405
|
+
};
|
|
2406
|
+
return { type: "field-start" };
|
|
2407
|
+
}
|
|
2408
|
+
if (mode !== "query") return analyzeArgCursorContext(trimmed, hasTrailingSpace, mode);
|
|
2409
|
+
const tokens = tokenizeQueryText(trimmed);
|
|
2410
|
+
if (tokens.length === 0) return { type: "field-start" };
|
|
2411
|
+
const last = tokens[tokens.length - 1];
|
|
2412
|
+
if (LOGICAL_KEYWORDS.has(last) || last === "(") return { type: "field-start" };
|
|
2413
|
+
if (SYMBOLIC_OPS.has(last)) return { type: "after-operator" };
|
|
2414
|
+
if (last === "in" || last === "matches") return { type: "after-operator" };
|
|
2415
|
+
if (last === "exists") return { type: "after-comparison" };
|
|
2416
|
+
if (VALUE_KEYWORDS.has(last)) return { type: "after-comparison" };
|
|
2417
|
+
if (/^['"]/.test(last) || /^\d/.test(last)) return { type: "after-comparison" };
|
|
2418
|
+
if (tokens.length >= 2) {
|
|
2419
|
+
const prev = tokens[tokens.length - 2];
|
|
2420
|
+
if (SYMBOLIC_OPS.has(prev) || KEYWORD_OPS.has(prev)) return { type: "after-comparison" };
|
|
2421
|
+
}
|
|
2422
|
+
if (/^\w+$/u.test(last)) {
|
|
2423
|
+
if (!hasTrailingSpace) return { type: "field-start" };
|
|
2424
|
+
return { type: "after-field" };
|
|
2425
|
+
}
|
|
2426
|
+
return { type: "field-start" };
|
|
2427
|
+
}
|
|
2428
|
+
const ARITHMETIC_OPS = new Set(BACKTICK_ARG_VOCABULARY.arithmeticOps);
|
|
2429
|
+
/** An order key (an identifier other than `asc` / `desc`). */ function isOrderKey(token) {
|
|
2430
|
+
return token !== void 0 && /^\w+$/u.test(token) && !ORDER_DIRECTIONS.has(token);
|
|
2431
|
+
}
|
|
2432
|
+
/**
|
|
2433
|
+
* Cursor context inside an `expr` or `order` argument — `trimmed` is the
|
|
2434
|
+
* non-empty text before the cursor that does not end with `.`.
|
|
2435
|
+
*/ function analyzeArgCursorContext(trimmed, hasTrailingSpace, mode) {
|
|
2436
|
+
const tokens = tokenizeQueryText(trimmed, true);
|
|
2437
|
+
const last = tokens[tokens.length - 1];
|
|
2438
|
+
if (last === void 0 || last === "," || last === "(" || ARITHMETIC_OPS.has(last)) return { type: "field-start" };
|
|
2439
|
+
if (mode === "order") {
|
|
2440
|
+
if (!/^\w+$/u.test(last)) return { type: "after-order-direction" };
|
|
2441
|
+
if (!hasTrailingSpace) return isOrderKey(tokens[tokens.length - 2]) ? { type: "after-order-key" } : { type: "field-start" };
|
|
2442
|
+
return isOrderKey(last) ? { type: "after-order-key" } : { type: "after-order-direction" };
|
|
2443
|
+
}
|
|
2444
|
+
if (/^[a-z_$]\w*$/iu.test(last) && !hasTrailingSpace) return { type: "field-start" };
|
|
2445
|
+
return { type: "after-operand" };
|
|
2446
|
+
}
|
|
2447
|
+
/**
|
|
2448
|
+
* Simple tokenizer for query text — splits into identifiers, operators, and punctuation.
|
|
2449
|
+
* Does NOT need to handle all edge cases; just enough for cursor context analysis.
|
|
2450
|
+
* With `arithmetic`, `+ - * /` are tokens too (otherwise they are skipped).
|
|
2451
|
+
*/ function tokenizeQueryText(text, arithmetic = false) {
|
|
2452
|
+
const tokens = [];
|
|
2453
|
+
let i = 0;
|
|
2454
|
+
while (i < text.length) {
|
|
2455
|
+
if (/\s/.test(text[i])) {
|
|
2456
|
+
i++;
|
|
2457
|
+
continue;
|
|
2458
|
+
}
|
|
2459
|
+
if (text[i] === "'" || text[i] === "\"") {
|
|
2460
|
+
const quote = text[i];
|
|
2461
|
+
let j = i + 1;
|
|
2462
|
+
while (j < text.length && text[j] !== quote) {
|
|
2463
|
+
if (text[j] === "\\") j++;
|
|
2464
|
+
j++;
|
|
2465
|
+
}
|
|
2466
|
+
tokens.push(text.slice(i, j + 1));
|
|
2467
|
+
i = j + 1;
|
|
2468
|
+
continue;
|
|
2469
|
+
}
|
|
2470
|
+
if (i + 1 < text.length) {
|
|
2471
|
+
const two = text.slice(i, i + 2);
|
|
2472
|
+
if (two === "!=" || two === ">=" || two === "<=") {
|
|
2473
|
+
tokens.push(two);
|
|
2474
|
+
i += 2;
|
|
2475
|
+
continue;
|
|
2476
|
+
}
|
|
2477
|
+
}
|
|
2478
|
+
if ("=><(),".includes(text[i]) || arithmetic && ARITHMETIC_OPS.has(text[i])) {
|
|
2479
|
+
tokens.push(text[i]);
|
|
2480
|
+
i++;
|
|
2481
|
+
continue;
|
|
2482
|
+
}
|
|
2483
|
+
if (text[i] === ".") {
|
|
2484
|
+
tokens.push(".");
|
|
2485
|
+
i++;
|
|
2486
|
+
continue;
|
|
2487
|
+
}
|
|
2488
|
+
if (/[\w]/u.test(text[i])) {
|
|
2489
|
+
let j = i;
|
|
2490
|
+
while (j < text.length && /[\w]/u.test(text[j])) j++;
|
|
2491
|
+
tokens.push(text.slice(i, j));
|
|
2492
|
+
i = j;
|
|
2493
|
+
continue;
|
|
2494
|
+
}
|
|
2495
|
+
i++;
|
|
2496
|
+
}
|
|
2497
|
+
return tokens;
|
|
2498
|
+
}
|
|
2499
|
+
|
|
2500
|
+
//#endregion
|
|
2501
|
+
//#region packages/core/src/parser/id-registry.ts
|
|
2502
|
+
function _define_property$6(obj, key, value) {
|
|
2503
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
2504
|
+
value,
|
|
2505
|
+
enumerable: true,
|
|
2506
|
+
configurable: true,
|
|
2507
|
+
writable: true
|
|
2508
|
+
});
|
|
2509
|
+
else obj[key] = value;
|
|
2510
|
+
return obj;
|
|
2511
|
+
}
|
|
2512
|
+
var IdRegistry = class {
|
|
2513
|
+
clear() {
|
|
2514
|
+
this.definitions.clear();
|
|
2515
|
+
this.duplicates.clear();
|
|
2516
|
+
this.forbidden.clear();
|
|
2517
|
+
}
|
|
2518
|
+
registerDefinition(token) {
|
|
2519
|
+
if (!token) return;
|
|
2520
|
+
if (this.reserved.has(token.text)) this.forbidden.add(token);
|
|
2521
|
+
else if (this.definitions.has(token.text)) this.duplicates.add(token);
|
|
2522
|
+
else this.definitions.set(token.text, token);
|
|
2523
|
+
}
|
|
2524
|
+
isDefined(t) {
|
|
2525
|
+
const text = typeof t === "string" ? t : t.text;
|
|
2526
|
+
return this.definitions.has(text) || this.globalTypes.has(text);
|
|
2527
|
+
}
|
|
2528
|
+
getErrors() {
|
|
2529
|
+
return [...Array.from(this.duplicates, (t) => ({
|
|
2530
|
+
severity: 1,
|
|
2531
|
+
message: `Duplicate identifier "${t.text}"`,
|
|
2532
|
+
range: t.range
|
|
2533
|
+
})), ...Array.from(this.forbidden, (t) => ({
|
|
2534
|
+
severity: 1,
|
|
2535
|
+
message: `Reserved keyword "${t.text}"`,
|
|
2536
|
+
range: t.range
|
|
2537
|
+
}))];
|
|
2538
|
+
}
|
|
2539
|
+
constructor(globalTypes = []) {
|
|
2540
|
+
_define_property$6(this, "reserved", void 0);
|
|
2541
|
+
_define_property$6(this, "globalTypes", void 0);
|
|
2542
|
+
_define_property$6(this, "definitions", /* @__PURE__ */ new Map());
|
|
2543
|
+
_define_property$6(this, "duplicates", /* @__PURE__ */ new Set());
|
|
2544
|
+
_define_property$6(this, "forbidden", /* @__PURE__ */ new Set());
|
|
2545
|
+
this.reserved = new Set([
|
|
2546
|
+
"interface",
|
|
2547
|
+
"type",
|
|
2548
|
+
"import",
|
|
2549
|
+
"from",
|
|
2550
|
+
"export",
|
|
2551
|
+
"annotate",
|
|
2552
|
+
"as"
|
|
2553
|
+
].concat(globalTypes));
|
|
2554
|
+
this.globalTypes = new Set(globalTypes);
|
|
2555
|
+
}
|
|
2556
|
+
};
|
|
2557
|
+
|
|
2558
|
+
//#endregion
|
|
2559
|
+
//#region packages/core/src/tokenizer/tokens/a-identifier.token.ts
|
|
2560
|
+
/**
|
|
2561
|
+
* Annotation (@) Identifier node
|
|
2562
|
+
*/ const AIdentifierToken = new _prostojs_parser.Node({
|
|
2563
|
+
name: "annotation",
|
|
2564
|
+
start: { token: RegExp("@[\\p{ID_Continue}$.]*", "u") },
|
|
2565
|
+
end: {
|
|
2566
|
+
token: RegExp("[^\\p{ID_Continue}$.]", "u"),
|
|
2567
|
+
eject: true
|
|
2568
|
+
},
|
|
2569
|
+
eofClose: true,
|
|
2570
|
+
data: {
|
|
2571
|
+
type: "annotation",
|
|
2572
|
+
text: ""
|
|
2573
|
+
},
|
|
2574
|
+
mapContent: "text"
|
|
2575
|
+
});
|
|
2576
|
+
|
|
2577
|
+
//#endregion
|
|
2578
|
+
//#region packages/core/src/tokenizer/tokens/block.token.ts
|
|
2579
|
+
/**
|
|
2580
|
+
* Builds a block node that opens on any key of `pairs` and closes on the
|
|
2581
|
+
* matching value; the opening character is kept as the token's `text`.
|
|
2582
|
+
*/ function createBlockToken(pairs) {
|
|
2583
|
+
const opening = Object.keys(pairs).map((c) => c.replace(/[\\^$.*+?()[\]{}|/-]/gu, "\\$&")).join("");
|
|
2584
|
+
return new _prostojs_parser.Node({
|
|
2585
|
+
name: "block",
|
|
2586
|
+
start: {
|
|
2587
|
+
token: new RegExp(`(?<text>[${opening}])`, "u"),
|
|
2588
|
+
omit: true
|
|
2589
|
+
},
|
|
2590
|
+
end: {
|
|
2591
|
+
token: (ctx) => pairs[ctx.node.data.text] || "",
|
|
2592
|
+
omit: true
|
|
2593
|
+
},
|
|
2594
|
+
skip: /\s/u,
|
|
2595
|
+
eofClose: true,
|
|
2596
|
+
data: {
|
|
2597
|
+
type: "block",
|
|
2598
|
+
text: ""
|
|
2599
|
+
}
|
|
2600
|
+
});
|
|
2601
|
+
}
|
|
2602
|
+
/**
|
|
2603
|
+
* Block node
|
|
2604
|
+
*/ const BlockToken = createBlockToken({
|
|
2605
|
+
"{": "}",
|
|
2606
|
+
"(": ")",
|
|
2607
|
+
"[": "]"
|
|
2608
|
+
});
|
|
2609
|
+
|
|
2610
|
+
//#endregion
|
|
2611
|
+
//#region packages/core/src/tokenizer/tokens/comment.token.ts
|
|
2612
|
+
const inline = new _prostojs_parser.Node({
|
|
2613
|
+
name: "inline-comment",
|
|
2614
|
+
start: {
|
|
2615
|
+
token: "//",
|
|
2616
|
+
omit: true
|
|
2617
|
+
},
|
|
2618
|
+
end: {
|
|
2619
|
+
token: /$/mu,
|
|
2620
|
+
omit: true
|
|
2621
|
+
},
|
|
2622
|
+
eofClose: true,
|
|
2623
|
+
data: {
|
|
2624
|
+
type: "comment",
|
|
2625
|
+
text: ""
|
|
2626
|
+
},
|
|
2627
|
+
mapContent: "text"
|
|
2628
|
+
});
|
|
2629
|
+
const block$1 = new _prostojs_parser.Node({
|
|
2630
|
+
name: "block-comment",
|
|
2631
|
+
start: {
|
|
2632
|
+
token: "/*",
|
|
2633
|
+
omit: true
|
|
2634
|
+
},
|
|
2635
|
+
end: {
|
|
2636
|
+
token: "*/",
|
|
2637
|
+
omit: true
|
|
2638
|
+
},
|
|
2639
|
+
eofClose: true,
|
|
2640
|
+
data: {
|
|
2641
|
+
type: "comment",
|
|
2642
|
+
text: ""
|
|
2643
|
+
},
|
|
2644
|
+
mapContent: "text"
|
|
2645
|
+
});
|
|
2646
|
+
/**
|
|
2647
|
+
* Block and inline comments
|
|
2648
|
+
*/ const commentNodes = {
|
|
2649
|
+
inline,
|
|
2650
|
+
block: block$1,
|
|
2651
|
+
all: [inline, block$1]
|
|
2652
|
+
};
|
|
2653
|
+
|
|
2654
|
+
//#endregion
|
|
2655
|
+
//#region packages/core/src/tokenizer/tokens/identifier.token.ts
|
|
2656
|
+
/**
|
|
2657
|
+
* Identifier node
|
|
2658
|
+
*/ const IdentifierToken = new _prostojs_parser.Node({
|
|
2659
|
+
name: "identifier",
|
|
2660
|
+
start: { token: RegExp("[\\p{ID_Start}$_][\\p{ID_Continue}$]*", "u") },
|
|
2661
|
+
end: {
|
|
2662
|
+
token: RegExp("[^\\p{ID_Continue}$]", "u"),
|
|
2663
|
+
eject: true
|
|
2664
|
+
},
|
|
2665
|
+
eofClose: true,
|
|
2666
|
+
data: {
|
|
2667
|
+
type: "identifier",
|
|
2668
|
+
text: ""
|
|
2669
|
+
},
|
|
2670
|
+
mapContent: "text"
|
|
2671
|
+
});
|
|
2672
|
+
|
|
2673
|
+
//#endregion
|
|
2674
|
+
//#region packages/core/src/tokenizer/tokens/number.token.ts
|
|
2675
|
+
/**
|
|
2676
|
+
* Number node
|
|
2677
|
+
*/ const NumberToken = new _prostojs_parser.Node({
|
|
2678
|
+
name: "number",
|
|
2679
|
+
start: { token: /[-+]?(?:\d*\.\d+|\d+)(?:[eE][-+]?\d+)?/u },
|
|
2680
|
+
end: {
|
|
2681
|
+
token: /[^\d]/u,
|
|
2682
|
+
eject: true
|
|
2683
|
+
},
|
|
2684
|
+
eofClose: true,
|
|
2685
|
+
data: {
|
|
2686
|
+
type: "number",
|
|
2687
|
+
text: ""
|
|
2688
|
+
},
|
|
2689
|
+
mapContent: "text"
|
|
2690
|
+
});
|
|
2691
|
+
|
|
2692
|
+
//#endregion
|
|
2693
|
+
//#region packages/core/src/tokenizer/tokens/punctuation.token.ts
|
|
2694
|
+
/**
|
|
2695
|
+
* Punctuation node
|
|
2696
|
+
*/ const PunctuationToken = new _prostojs_parser.Node({
|
|
2697
|
+
name: "punctuation",
|
|
2698
|
+
start: {
|
|
2699
|
+
token: RegExp("(?<text>[\\n!&+,./:;=?|])", "u"),
|
|
2700
|
+
omit: true
|
|
2701
|
+
},
|
|
2702
|
+
end: {
|
|
2703
|
+
token: "",
|
|
2704
|
+
omit: true
|
|
2705
|
+
},
|
|
2706
|
+
eofClose: true,
|
|
2707
|
+
data: {
|
|
2708
|
+
type: "punctuation",
|
|
2709
|
+
text: ""
|
|
2710
|
+
}
|
|
2711
|
+
});
|
|
2712
|
+
|
|
2713
|
+
//#endregion
|
|
2714
|
+
//#region packages/core/src/tokenizer/tokens/query-arith.token.ts
|
|
2715
|
+
/**
|
|
2716
|
+
* Arithmetic operator token — `*` and `-` inside backtick expressions
|
|
2717
|
+
* (`+` and `/` are already generic punctuation).
|
|
2718
|
+
*
|
|
2719
|
+
* Registered after `NumberToken`, so a sign written adjacent to a number
|
|
2720
|
+
* (`-5`, `+1`) still lexes as a signed number; the expression parser splits a
|
|
2721
|
+
* signed number found in binary-operator position (`a -5` → `a - 5`).
|
|
2722
|
+
* Only recognized inside queries — not in root or generic blocks.
|
|
2723
|
+
*/ const QueryArithmeticToken = new _prostojs_parser.Node({
|
|
2724
|
+
name: "punctuation",
|
|
2725
|
+
start: {
|
|
2726
|
+
token: RegExp("(?<text>[*-])", "u"),
|
|
2727
|
+
omit: true
|
|
2728
|
+
},
|
|
2729
|
+
end: {
|
|
2730
|
+
token: "",
|
|
2731
|
+
omit: true
|
|
2732
|
+
},
|
|
2733
|
+
eofClose: true,
|
|
2734
|
+
data: {
|
|
2735
|
+
type: "punctuation",
|
|
2736
|
+
text: ""
|
|
2737
|
+
}
|
|
2738
|
+
});
|
|
2739
|
+
|
|
2740
|
+
//#endregion
|
|
2741
|
+
//#region packages/core/src/tokenizer/tokens/query-block.token.ts
|
|
2742
|
+
/**
|
|
2743
|
+
* Query block node — parenthesized sub-expressions inside backtick queries.
|
|
2744
|
+
*
|
|
2745
|
+
* Same definition as `BlockToken` narrowed to `(`, so it produces the same
|
|
2746
|
+
* token shape (`type: 'block'`, `text: '('`) and every consumer keeps working,
|
|
2747
|
+
* but it recognizes `QueryOperatorToken` inside (see `tokens/index.ts`), which
|
|
2748
|
+
* makes `!=`, `>=`, `<=`, `>`, `<` work within parentheses — the generic block
|
|
2749
|
+
* only knows `PunctuationToken`, whose charset has no `<` / `>`.
|
|
2750
|
+
*
|
|
2751
|
+
* Only registered inside `QueryToken` (and itself, for nested parens) —
|
|
2752
|
+
* the generic `BlockToken` is left untouched so tokenization of `(`/`[`/`{`
|
|
2753
|
+
* outside queries does not change.
|
|
2754
|
+
*/ const QueryBlockToken = createBlockToken({ "(": ")" });
|
|
2755
|
+
|
|
2756
|
+
//#endregion
|
|
2757
|
+
//#region packages/core/src/tokenizer/tokens/query-operator.token.ts
|
|
2758
|
+
/**
|
|
2759
|
+
* Query operator token — matches symbolic comparison operators
|
|
2760
|
+
* inside backtick-delimited query expressions.
|
|
2761
|
+
*
|
|
2762
|
+
* Multi-character operators (!=, >=, <=) are matched as single tokens.
|
|
2763
|
+
* Only recognized inside QueryToken — not in root or BlockToken.
|
|
2764
|
+
*/ const QueryOperatorToken = new _prostojs_parser.Node({
|
|
2765
|
+
name: "punctuation",
|
|
2766
|
+
start: {
|
|
2767
|
+
token: RegExp("(?<text>!=|>=|<=|[=><])", "u"),
|
|
2768
|
+
omit: true
|
|
2769
|
+
},
|
|
2770
|
+
end: {
|
|
2771
|
+
token: "",
|
|
2772
|
+
omit: true
|
|
2773
|
+
},
|
|
2774
|
+
eofClose: true,
|
|
2775
|
+
data: {
|
|
2776
|
+
type: "punctuation",
|
|
2777
|
+
text: ""
|
|
2778
|
+
}
|
|
2779
|
+
});
|
|
2780
|
+
|
|
2781
|
+
//#endregion
|
|
2782
|
+
//#region packages/core/src/tokenizer/tokens/query.token.ts
|
|
2783
|
+
/**
|
|
2784
|
+
* Query node — backtick-delimited filter expressions
|
|
2785
|
+
* Content inside backticks is tokenized into children.
|
|
2786
|
+
*/ const QueryToken = new _prostojs_parser.Node({
|
|
2787
|
+
name: "query",
|
|
2788
|
+
start: {
|
|
2789
|
+
token: /`/u,
|
|
2790
|
+
omit: true
|
|
2791
|
+
},
|
|
2792
|
+
end: {
|
|
2793
|
+
token: /`/u,
|
|
2794
|
+
omit: true
|
|
2795
|
+
},
|
|
2796
|
+
skip: /\s/u,
|
|
2797
|
+
eofClose: true,
|
|
2798
|
+
data: {
|
|
2799
|
+
type: "query",
|
|
2800
|
+
text: ""
|
|
2801
|
+
}
|
|
2802
|
+
});
|
|
2803
|
+
|
|
2804
|
+
//#endregion
|
|
2805
|
+
//#region packages/core/src/tokenizer/tokens/regexp.token.ts
|
|
2806
|
+
const REGEXP_LITERAL_RE = /\/(?![/*])(?:\\.|\[.*?]|[^/\\\n\r[])*\/[dgimsuy]*/;
|
|
2807
|
+
/**
|
|
2808
|
+
* Regexp literal inside a backtick expression: only directly after the
|
|
2809
|
+
* `matches` keyword (the one operator that takes a regexp), so `/` elsewhere
|
|
2810
|
+
* stays the division operator (`a / b / c` is not the regexp `/ b /`).
|
|
2811
|
+
* The lookbehind works because the parser scans the full source from the
|
|
2812
|
+
* current position.
|
|
2813
|
+
*/ const QUERY_REGEXP_LITERAL_RE = new RegExp(`(?<=\\bmatches\\s*)${REGEXP_LITERAL_RE.source}`);
|
|
2814
|
+
/** A regexp-literal token node starting at `start`. */ function regExpNode(start) {
|
|
2815
|
+
return new _prostojs_parser.Node({
|
|
2816
|
+
name: "regexp",
|
|
2817
|
+
start: {
|
|
2818
|
+
token: start,
|
|
2819
|
+
omit: true
|
|
2820
|
+
},
|
|
2821
|
+
end: {
|
|
2822
|
+
token: "",
|
|
2823
|
+
omit: true
|
|
2824
|
+
},
|
|
2825
|
+
eofClose: true,
|
|
2826
|
+
data: {
|
|
2827
|
+
type: "regexp",
|
|
2828
|
+
text: ""
|
|
2829
|
+
}
|
|
2830
|
+
}).onOpen((node, match) => {
|
|
2831
|
+
node.data.text = match?.text ?? "";
|
|
2832
|
+
});
|
|
2833
|
+
}
|
|
2834
|
+
/**
|
|
2835
|
+
* RegExp literal – `/pattern/flags`
|
|
2836
|
+
* • Starts with `/` but *not* `//` or `/*`
|
|
2837
|
+
* • Ends with `/` followed by zero-or-more valid flags
|
|
2838
|
+
*/ const RegExpToken = regExpNode(REGEXP_LITERAL_RE);
|
|
2839
|
+
/** RegExp literal inside backtick expressions — recognized only after `matches`. */ const QueryRegExpToken = regExpNode(QUERY_REGEXP_LITERAL_RE);
|
|
2840
|
+
|
|
2841
|
+
//#endregion
|
|
2842
|
+
//#region packages/core/src/tokenizer/tokens/text.node.ts
|
|
2843
|
+
/**
|
|
2844
|
+
* Text node single or doublequoted
|
|
2845
|
+
*/ const TextToken = new _prostojs_parser.Node({
|
|
2846
|
+
name: "text",
|
|
2847
|
+
start: {
|
|
2848
|
+
token: RegExp("(?<quote>[\"'])", "u"),
|
|
2849
|
+
omit: true
|
|
2850
|
+
},
|
|
2851
|
+
end: {
|
|
2852
|
+
token: (ctx) => new RegExp(`(?<end>${ctx.node.data.quote || ""}|\\n)`),
|
|
2853
|
+
omit: true,
|
|
2854
|
+
backslash: true
|
|
2855
|
+
},
|
|
2856
|
+
eofClose: true,
|
|
2857
|
+
data: {
|
|
2858
|
+
type: "text",
|
|
2859
|
+
quote: "",
|
|
2860
|
+
end: "",
|
|
2861
|
+
text: ""
|
|
2862
|
+
},
|
|
2863
|
+
mapContent: "text"
|
|
2864
|
+
}).onClose((node) => {
|
|
2865
|
+
node.data.multiline = node.data.end === "\n";
|
|
2866
|
+
});
|
|
2867
|
+
|
|
2868
|
+
//#endregion
|
|
2869
|
+
//#region packages/core/src/tokenizer/tokens/index.ts
|
|
2870
|
+
const tokens = {
|
|
2871
|
+
aIdentifier: AIdentifierToken,
|
|
2872
|
+
punctuation: PunctuationToken,
|
|
2873
|
+
comments: commentNodes.all,
|
|
2874
|
+
inlineComment: commentNodes.inline,
|
|
2875
|
+
blockComment: commentNodes.block,
|
|
2876
|
+
block: BlockToken,
|
|
2877
|
+
identifier: IdentifierToken,
|
|
2878
|
+
number: NumberToken,
|
|
2879
|
+
text: TextToken,
|
|
2880
|
+
regexp: RegExpToken,
|
|
2881
|
+
query: QueryToken,
|
|
2882
|
+
queryBlock: QueryBlockToken,
|
|
2883
|
+
root: void 0
|
|
2884
|
+
};
|
|
2885
|
+
const root = new _prostojs_parser.Node({
|
|
2886
|
+
name: "root",
|
|
2887
|
+
skip: /\s/u,
|
|
2888
|
+
eofClose: true,
|
|
2889
|
+
recognizes: [
|
|
2890
|
+
...commentNodes.all,
|
|
2891
|
+
BlockToken,
|
|
2892
|
+
QueryToken,
|
|
2893
|
+
AIdentifierToken,
|
|
2894
|
+
IdentifierToken,
|
|
2895
|
+
TextToken,
|
|
2896
|
+
NumberToken,
|
|
2897
|
+
PunctuationToken
|
|
2898
|
+
]
|
|
2899
|
+
});
|
|
2900
|
+
tokens.root = root;
|
|
2901
|
+
BlockToken.recognize(RegExpToken, ...commentNodes.all, BlockToken, QueryToken, AIdentifierToken, IdentifierToken, TextToken, NumberToken, PunctuationToken);
|
|
2902
|
+
QueryToken.recognize(QueryRegExpToken, QueryOperatorToken, QueryBlockToken, BlockToken, IdentifierToken, TextToken, NumberToken, QueryArithmeticToken, PunctuationToken);
|
|
2903
|
+
QueryBlockToken.recognize(QueryRegExpToken, QueryOperatorToken, QueryBlockToken, IdentifierToken, TextToken, NumberToken, QueryArithmeticToken, PunctuationToken);
|
|
2904
|
+
/** 1-based parser position → 0-based VSCode position, shifted by `offset` characters. */ function toPos(p, offset = 0) {
|
|
2905
|
+
return {
|
|
2906
|
+
line: p.line - 1,
|
|
2907
|
+
character: p.column - 1 + offset
|
|
2908
|
+
};
|
|
2909
|
+
}
|
|
2910
|
+
/**
|
|
2911
|
+
* Recursively extract TLexicalToken[] from a ParsedNode tree.
|
|
2912
|
+
* Replaces the v0.5 mapContent('children', callback) + global loop.
|
|
2913
|
+
*
|
|
2914
|
+
* v0.6 Position has { offset, line, column }.
|
|
2915
|
+
* line is 1-based, column is 1-based.
|
|
2916
|
+
* VSCode expects 0-based line, 0-based character.
|
|
2917
|
+
*/ function extractTokens(node) {
|
|
2918
|
+
const content = node.content;
|
|
2919
|
+
return content.map((item, index) => {
|
|
2920
|
+
if (typeof item === "string") {
|
|
2921
|
+
const prev = content[index - 1];
|
|
2922
|
+
const next = content[index + 1];
|
|
2923
|
+
const start = typeof prev === "object" ? prev.end : node.start;
|
|
2924
|
+
const end = typeof next === "object" ? next.start : node.end;
|
|
2925
|
+
return {
|
|
2926
|
+
type: "unknown",
|
|
2927
|
+
text: item,
|
|
2928
|
+
getRange: () => ({
|
|
2929
|
+
start: toPos(start),
|
|
2930
|
+
end: toPos(end)
|
|
2931
|
+
})
|
|
2932
|
+
};
|
|
2933
|
+
}
|
|
2934
|
+
const data = item.data;
|
|
2935
|
+
data.getRange = () => ({
|
|
2936
|
+
start: toPos(item.start, data.startOffset),
|
|
2937
|
+
end: toPos(item.end, data.endOffset)
|
|
2938
|
+
});
|
|
2939
|
+
data.children = extractTokens(item);
|
|
2940
|
+
return data;
|
|
2941
|
+
});
|
|
2942
|
+
}
|
|
2943
|
+
|
|
2944
|
+
//#endregion
|
|
2945
|
+
//#region packages/core/src/tokenizer/index.ts
|
|
2946
|
+
function tokenize(source, debug = false) {
|
|
2947
|
+
const result = root.parse(source);
|
|
2948
|
+
if (debug) console.log((0, _prostojs_parser.printTree)(result));
|
|
2949
|
+
return extractTokens(result);
|
|
2950
|
+
}
|
|
2951
|
+
|
|
2952
|
+
//#endregion
|
|
2953
|
+
//#region packages/core/src/parser/pipes/tokens.pipe.ts
|
|
2954
|
+
const identifier = (...text) => $token("identifier", text.length > 0 ? text : void 0);
|
|
2955
|
+
const text = (...text) => $token("text", text.length > 0 ? text : void 0);
|
|
2956
|
+
const block = (...text) => $token("block", text.length > 0 ? text.map((v) => v[0]) : void 0);
|
|
2957
|
+
const pun = (...text) => $token("punctuation", text.length > 0 ? text : void 0);
|
|
2958
|
+
function $token(name, text) {
|
|
2959
|
+
const opts = {
|
|
2960
|
+
optional: false,
|
|
2961
|
+
saveAs: void 0,
|
|
2962
|
+
skip: void 0,
|
|
2963
|
+
expect: [{
|
|
2964
|
+
node: name,
|
|
2965
|
+
text
|
|
2966
|
+
}],
|
|
2967
|
+
contains: void 0,
|
|
2968
|
+
isGlobal: false,
|
|
2969
|
+
empty: false,
|
|
2970
|
+
wrapper: void 0,
|
|
2971
|
+
wrapMultiple: false,
|
|
2972
|
+
debug: false,
|
|
2973
|
+
eob: false,
|
|
2974
|
+
suppressEobError: false,
|
|
2975
|
+
lookBehind: false
|
|
2976
|
+
};
|
|
2977
|
+
return {
|
|
2978
|
+
expect: [{
|
|
2979
|
+
node: name,
|
|
2980
|
+
text
|
|
2981
|
+
}],
|
|
2982
|
+
handler(ni, target) {
|
|
2983
|
+
if (opts.debug) debugger;
|
|
2984
|
+
let firstRun = true;
|
|
2985
|
+
while (firstRun || opts.wrapMultiple) {
|
|
2986
|
+
firstRun = false;
|
|
2987
|
+
if (!ni.$) {
|
|
2988
|
+
const ok = opts.eob || opts.optional;
|
|
2989
|
+
if (!ok && !opts.suppressEobError) ni.unexpectedEOB();
|
|
2990
|
+
return ok;
|
|
2991
|
+
}
|
|
2992
|
+
let matched = ni.satisfies(...opts.expect);
|
|
2993
|
+
if (!matched && opts.lookBehind) {
|
|
2994
|
+
matched = ni.fork().move(-1).satisfies(...opts.expect);
|
|
2995
|
+
if (matched) ni.move(-1);
|
|
2996
|
+
}
|
|
2997
|
+
if (matched) {
|
|
2998
|
+
if (opts.empty && ni.$.children?.length) {
|
|
2999
|
+
ni.unexpected(false, `Expected empty block`);
|
|
3000
|
+
return opts.optional;
|
|
3001
|
+
}
|
|
3002
|
+
if (opts.contains) {
|
|
3003
|
+
if (!ni.$.children?.length) {
|
|
3004
|
+
ni.unexpected(false, `Expected ${opts.contains.map((v) => v.text).join(", ")}`);
|
|
3005
|
+
return opts.optional;
|
|
3006
|
+
}
|
|
3007
|
+
if (ni.$.children[0].type !== opts.contains[0].node || ni.$.children[0].text !== opts.contains[0].text) {
|
|
3008
|
+
ni.unexpected(false, `Expected ${opts.contains.map((v) => v.text).join(", ")}`);
|
|
3009
|
+
return opts.optional;
|
|
3010
|
+
}
|
|
3011
|
+
}
|
|
3012
|
+
if (opts.saveAs) target.node.saveToken(new Token(ni.$), opts.saveAs);
|
|
3013
|
+
ni.accepted();
|
|
3014
|
+
if (opts.wrapper) {
|
|
3015
|
+
const wrapped = target.node;
|
|
3016
|
+
target.node = opts.wrapper().wrap(wrapped, new Token(ni.$));
|
|
3017
|
+
}
|
|
2749
3018
|
ni.move();
|
|
3019
|
+
if (opts.skip) ni.skip(opts.skip);
|
|
3020
|
+
if (opts.wrapMultiple) continue;
|
|
3021
|
+
else return true;
|
|
2750
3022
|
} else {
|
|
2751
|
-
|
|
2752
|
-
|
|
3023
|
+
if (!opts.optional) ni.unexpected();
|
|
3024
|
+
return opts.optional;
|
|
2753
3025
|
}
|
|
2754
3026
|
}
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
|
|
3027
|
+
return true;
|
|
3028
|
+
},
|
|
3029
|
+
saveAs(v) {
|
|
3030
|
+
opts.saveAs = v;
|
|
3031
|
+
return this;
|
|
3032
|
+
},
|
|
3033
|
+
wrap(sn, multiple = false) {
|
|
3034
|
+
opts.wrapper = sn;
|
|
3035
|
+
opts.wrapMultiple = multiple;
|
|
3036
|
+
return this;
|
|
3037
|
+
},
|
|
3038
|
+
optional() {
|
|
3039
|
+
opts.optional = true;
|
|
3040
|
+
return this;
|
|
3041
|
+
},
|
|
3042
|
+
empty() {
|
|
3043
|
+
opts.empty = true;
|
|
3044
|
+
return this;
|
|
3045
|
+
},
|
|
3046
|
+
contains(t) {
|
|
3047
|
+
opts.contains = t.expect;
|
|
3048
|
+
return this;
|
|
3049
|
+
},
|
|
3050
|
+
skip(...p) {
|
|
3051
|
+
opts.skip = p;
|
|
3052
|
+
return this;
|
|
3053
|
+
},
|
|
3054
|
+
or(t) {
|
|
3055
|
+
opts.expect.push(...t.expect);
|
|
3056
|
+
return this;
|
|
3057
|
+
},
|
|
3058
|
+
orEob() {
|
|
3059
|
+
opts.eob = true;
|
|
3060
|
+
return this;
|
|
3061
|
+
},
|
|
3062
|
+
suppressEobError() {
|
|
3063
|
+
opts.suppressEobError = true;
|
|
3064
|
+
return this;
|
|
3065
|
+
},
|
|
3066
|
+
global() {
|
|
3067
|
+
opts.isGlobal = true;
|
|
3068
|
+
return this;
|
|
3069
|
+
},
|
|
3070
|
+
lookBehind() {
|
|
3071
|
+
opts.lookBehind = true;
|
|
3072
|
+
return this;
|
|
3073
|
+
},
|
|
3074
|
+
debug() {
|
|
3075
|
+
opts.debug = true;
|
|
3076
|
+
return this;
|
|
2759
3077
|
}
|
|
2760
|
-
|
|
2761
|
-
const node = new SemanticQueryFieldRefNode();
|
|
2762
|
-
node.fieldRef = firstToken;
|
|
2763
|
-
return node;
|
|
2764
|
-
}
|
|
2765
|
-
const node = new SemanticQueryFieldRefNode();
|
|
2766
|
-
node.fieldRef = firstToken;
|
|
2767
|
-
return node;
|
|
2768
|
-
}
|
|
2769
|
-
function parseValueOrFieldRef(ni) {
|
|
2770
|
-
if (!ni.$) return;
|
|
2771
|
-
if (ni.$.type === "text" || ni.$.type === "number" || ni.$.type === "regexp") return parseValue(ni);
|
|
2772
|
-
if (ni.$.type === "identifier") {
|
|
2773
|
-
if (VALUE_KEYWORDS.has(ni.$.text)) return parseValue(ni);
|
|
2774
|
-
return parseFieldRef(ni);
|
|
2775
|
-
}
|
|
2776
|
-
pushError(ni, "Expected value or field reference");
|
|
3078
|
+
};
|
|
2777
3079
|
}
|
|
2778
|
-
|
|
2779
|
-
|
|
2780
|
-
|
|
2781
|
-
|
|
2782
|
-
}
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
|
|
2786
|
-
|
|
2787
|
-
|
|
2788
|
-
|
|
2789
|
-
|
|
2790
|
-
|
|
3080
|
+
|
|
3081
|
+
//#endregion
|
|
3082
|
+
//#region packages/core/src/parser/pipes/core.pipe.ts
|
|
3083
|
+
function $pipe(entity, pipe = []) {
|
|
3084
|
+
pipe.forEach((p, i) => Object.assign(p, { toString: () => `${entity}.${i}` }));
|
|
3085
|
+
return {
|
|
3086
|
+
targetFactory: () => {
|
|
3087
|
+
switch (entity) {
|
|
3088
|
+
case "interface": return new $n.SemanticInterfaceNode();
|
|
3089
|
+
case "array": return new $n.SemanticArrayNode();
|
|
3090
|
+
case "const": return new $n.SemanticConstNode();
|
|
3091
|
+
case "group": return new $n.SemanticGroup();
|
|
3092
|
+
case "prop": return new $n.SemanticPropNode();
|
|
3093
|
+
case "type": return new $n.SemanticTypeNode();
|
|
3094
|
+
case "ref": return new $n.SemanticRefNode();
|
|
3095
|
+
case "structure": return new $n.SemanticStructureNode();
|
|
3096
|
+
case "tuple": return new $n.SemanticTupleNode();
|
|
3097
|
+
case "import": return new $n.SemanticImportNode();
|
|
3098
|
+
case "annotate": return new $n.SemanticAnnotateNode();
|
|
3099
|
+
case "primitive": throw new Error("Can't create pipe for primitive node");
|
|
3100
|
+
default: throw new Error(`Can't create pipe for ${entity} node`);
|
|
3101
|
+
}
|
|
3102
|
+
},
|
|
3103
|
+
pipe,
|
|
3104
|
+
toString: () => entity,
|
|
3105
|
+
skipTokens: [],
|
|
3106
|
+
stopCondition: void 0,
|
|
3107
|
+
skip(...s) {
|
|
3108
|
+
this.skipTokens.push(...s);
|
|
3109
|
+
return this;
|
|
3110
|
+
},
|
|
3111
|
+
stop(sc) {
|
|
3112
|
+
this.stopCondition = sc;
|
|
3113
|
+
return this;
|
|
3114
|
+
},
|
|
3115
|
+
t(node, skip = []) {
|
|
3116
|
+
this.pipe.push($token(node).saveAs("identifier").skip(...skip));
|
|
3117
|
+
return this;
|
|
3118
|
+
}
|
|
3119
|
+
};
|
|
2791
3120
|
}
|
|
2792
|
-
function
|
|
2793
|
-
|
|
2794
|
-
|
|
2795
|
-
|
|
2796
|
-
|
|
2797
|
-
|
|
2798
|
-
|
|
3121
|
+
function runPipes(pipes, ni, singlePass = false) {
|
|
3122
|
+
const nodes = [];
|
|
3123
|
+
while (ni.$) {
|
|
3124
|
+
let matched = true;
|
|
3125
|
+
let depth = ni.index;
|
|
3126
|
+
for (const { targetFactory, pipe, skipTokens: skip, stopCondition } of pipes) {
|
|
3127
|
+
const target = { node: targetFactory() };
|
|
3128
|
+
const fork = ni.fork();
|
|
3129
|
+
fork.skip(skip);
|
|
3130
|
+
if (!fork.$) {
|
|
3131
|
+
ni.unfork(fork);
|
|
3132
|
+
break;
|
|
3133
|
+
}
|
|
3134
|
+
matched = true;
|
|
3135
|
+
for (const { handler } of pipe) if (!handler(fork, target)) {
|
|
3136
|
+
matched = false;
|
|
3137
|
+
break;
|
|
3138
|
+
}
|
|
3139
|
+
if (fork.index > depth) depth = fork.index;
|
|
3140
|
+
if (matched) {
|
|
3141
|
+
nodes.push(target.node);
|
|
3142
|
+
ni.unfork(fork);
|
|
3143
|
+
if (stopCondition && stopCondition(fork)) return nodes;
|
|
3144
|
+
ni.move(-1);
|
|
3145
|
+
break;
|
|
3146
|
+
}
|
|
2799
3147
|
}
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
|
|
3148
|
+
if (singlePass) return nodes;
|
|
3149
|
+
if (!matched) {
|
|
3150
|
+
ni.shouldHaveError(depth);
|
|
3151
|
+
ni.move(depth - ni.index);
|
|
3152
|
+
}
|
|
3153
|
+
ni.move();
|
|
3154
|
+
if (matched) {} else {
|
|
3155
|
+
ni.confirmIssues();
|
|
3156
|
+
ni.skipUntil([";", "\n"]);
|
|
2807
3157
|
}
|
|
2808
|
-
if (subNi.$) pushError(subNi, `Unexpected token in value list: "${subNi.$.text}"`);
|
|
2809
|
-
const node = new SemanticQueryValueListNode();
|
|
2810
|
-
node.values = values;
|
|
2811
|
-
return node;
|
|
2812
3158
|
}
|
|
2813
|
-
|
|
3159
|
+
return nodes;
|
|
2814
3160
|
}
|
|
2815
|
-
|
|
2816
|
-
|
|
2817
|
-
* query token / "(...)" block the iterator was forked from.
|
|
2818
|
-
*/ function rangeOf(ni) {
|
|
2819
|
-
return ni.$?.getRange?.() ?? ni.parent?.getRange?.() ?? ZERO_RANGE;
|
|
3161
|
+
function runPipesOnce(pipes, ni) {
|
|
3162
|
+
return runPipes(pipes, ni, true)[0];
|
|
2820
3163
|
}
|
|
2821
|
-
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
|
|
2826
|
-
|
|
3164
|
+
|
|
3165
|
+
//#endregion
|
|
3166
|
+
//#region packages/core/src/parser/grouping.ts
|
|
3167
|
+
function groupByPriority(nodes, priority) {
|
|
3168
|
+
if (nodes.length === 1) return nodes[0];
|
|
3169
|
+
if (nodes.length === 0) return;
|
|
3170
|
+
if (nodes.length % 2 === 0) throw new Error(`Invalid number of nodes ${nodes.length}. Odd number expected`);
|
|
3171
|
+
let currentPass = nodes;
|
|
3172
|
+
let temporaryResult = [];
|
|
3173
|
+
let groupped = [];
|
|
3174
|
+
for (const p of priority) {
|
|
3175
|
+
for (const [i, node] of currentPass.entries()) {
|
|
3176
|
+
const isOperator = i % 2 === 1;
|
|
3177
|
+
if (!isOperator && node instanceof Token) throw new Error(`Unexpected token ${node.toString()} at ${i}`);
|
|
3178
|
+
else if (isOperator && !(node instanceof Token)) throw new Error(`Unexpected token ${isGroup(node) ? node.op : node.token.toString()} at ${i}`);
|
|
3179
|
+
if (isOperator) {
|
|
3180
|
+
if (node.text !== p) temporaryResult.push(node);
|
|
3181
|
+
continue;
|
|
3182
|
+
}
|
|
3183
|
+
const prev = currentPass[i - 1]?.text;
|
|
3184
|
+
const next = currentPass[i + 1]?.text;
|
|
3185
|
+
if (prev !== p && next !== p) {
|
|
3186
|
+
temporaryResult.push(node);
|
|
3187
|
+
continue;
|
|
3188
|
+
}
|
|
3189
|
+
if (prev === p || next === p) {
|
|
3190
|
+
groupped.push(node);
|
|
3191
|
+
if (next !== p) {
|
|
3192
|
+
temporaryResult.push(new $n.SemanticGroup(groupped, p));
|
|
3193
|
+
groupped = [];
|
|
3194
|
+
}
|
|
3195
|
+
}
|
|
3196
|
+
}
|
|
3197
|
+
currentPass = temporaryResult;
|
|
3198
|
+
temporaryResult = [];
|
|
3199
|
+
}
|
|
3200
|
+
const output = currentPass[0];
|
|
3201
|
+
return output.length === 1 ? output.first : output;
|
|
2827
3202
|
}
|
|
2828
3203
|
|
|
2829
3204
|
//#endregion
|
|
@@ -2945,10 +3320,6 @@ function annotations() {
|
|
|
2945
3320
|
}
|
|
2946
3321
|
while (ni.satisfies(...opts.argument)) {
|
|
2947
3322
|
let argToken = new Token(ni.$);
|
|
2948
|
-
if (argToken.type === "query") {
|
|
2949
|
-
const queryNode = parseQueryExpression(ni.fork(argToken.children), argToken);
|
|
2950
|
-
if (queryNode) argToken.queryNode = queryNode;
|
|
2951
|
-
}
|
|
2952
3323
|
ni.accepted();
|
|
2953
3324
|
ni.move();
|
|
2954
3325
|
if (argToken.type === "identifier" && ![
|
|
@@ -3529,7 +3900,8 @@ var AtscriptDoc = class {
|
|
|
3529
3900
|
update(text, debug = false) {
|
|
3530
3901
|
this._text = text;
|
|
3531
3902
|
this.cleanup();
|
|
3532
|
-
const
|
|
3903
|
+
const rawTokens = tokenize(text, debug);
|
|
3904
|
+
const ni = new NodeIterator(rawTokens, []).move();
|
|
3533
3905
|
this.nodes = runPipes([
|
|
3534
3906
|
pipes.importPipe,
|
|
3535
3907
|
pipes.type,
|
|
@@ -3539,6 +3911,43 @@ var AtscriptDoc = class {
|
|
|
3539
3911
|
if (debug) console.log(this.nodes.map((n) => n.toString()).join("\n"));
|
|
3540
3912
|
this.semanticMessages = ni.getErrors();
|
|
3541
3913
|
this.registerNodes(this.nodes);
|
|
3914
|
+
this.parseUnregisteredBacktickArgs(rawTokens);
|
|
3915
|
+
}
|
|
3916
|
+
/**
|
|
3917
|
+
* Backtick arguments of annotations that never get registered (dangling at
|
|
3918
|
+
* EOF, or followed by a syntax error) are still parsed for diagnostics — by
|
|
3919
|
+
* the argument spec of the preceding annotation when it resolves, else as a
|
|
3920
|
+
* query predicate.
|
|
3921
|
+
*/ parseUnregisteredBacktickArgs(tokens) {
|
|
3922
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
3923
|
+
const t = tokens[i];
|
|
3924
|
+
if (t.type === "block" && t.children) {
|
|
3925
|
+
this.parseUnregisteredBacktickArgs(t.children);
|
|
3926
|
+
continue;
|
|
3927
|
+
}
|
|
3928
|
+
if (t.type !== "query" || this.parsedBacktickArgs.has(t)) continue;
|
|
3929
|
+
let argIndex = 0;
|
|
3930
|
+
let annotation;
|
|
3931
|
+
for (let j = i - 1; j >= 0; j--) {
|
|
3932
|
+
const p = tokens[j];
|
|
3933
|
+
if (p.type === "annotation") {
|
|
3934
|
+
annotation = p;
|
|
3935
|
+
break;
|
|
3936
|
+
}
|
|
3937
|
+
if (p.type === "block" || p.type === "punctuation" && (p.text === "\n" || p.text === ";")) break;
|
|
3938
|
+
if (p.type === "punctuation" && p.text === ",") argIndex++;
|
|
3939
|
+
}
|
|
3940
|
+
if (!annotation) continue;
|
|
3941
|
+
this.parseBacktickArgAs(new Token(t), this.resolveAnnotation(annotation.text.slice(1))?.arguments[argIndex]?.type);
|
|
3942
|
+
}
|
|
3943
|
+
}
|
|
3944
|
+
/**
|
|
3945
|
+
* Parse a backtick argument by its spec type: `expr` / `order` get their own
|
|
3946
|
+
* grammar; anything else (unknown annotation, position beyond the spec) parses
|
|
3947
|
+
* as a query predicate.
|
|
3948
|
+
*/ parseBacktickArgAs(arg, specType) {
|
|
3949
|
+
this.parsedBacktickArgs.add(arg.lexicalToken);
|
|
3950
|
+
this.registerMessages(parseBacktickArg(arg, isBacktickArgType(specType) ? specType : "query"));
|
|
3542
3951
|
}
|
|
3543
3952
|
cleanup() {
|
|
3544
3953
|
this.exports.clear();
|
|
@@ -3556,6 +3965,7 @@ var AtscriptDoc = class {
|
|
|
3556
3965
|
this.blocksIndex = new BlocksIndex();
|
|
3557
3966
|
this.resolvedAnnotations = [];
|
|
3558
3967
|
this.annotations = [];
|
|
3968
|
+
this.parsedBacktickArgs = /* @__PURE__ */ new WeakSet();
|
|
3559
3969
|
}
|
|
3560
3970
|
registerNodes(nodes) {
|
|
3561
3971
|
for (const node of nodes) {
|
|
@@ -3574,11 +3984,14 @@ var AtscriptDoc = class {
|
|
|
3574
3984
|
this.tokensIndex.add(mainToken);
|
|
3575
3985
|
args?.forEach((a) => this.tokensIndex.add(a));
|
|
3576
3986
|
const annotationSpec = this.resolveAnnotation(mainToken.text.slice(1));
|
|
3987
|
+
const specArgs = annotationSpec?.arguments ?? [];
|
|
3988
|
+
args?.forEach((a, i) => {
|
|
3989
|
+
if (a.type === "query") this.parseBacktickArgAs(a, specArgs[i]?.type);
|
|
3990
|
+
});
|
|
3577
3991
|
if (annotationSpec) {
|
|
3578
3992
|
this.registerMessages(annotationSpec.validate(mainToken, args || [], this));
|
|
3579
3993
|
annotationSpec.modify(mainToken, args || [], this);
|
|
3580
3994
|
this.resolvedAnnotations.push(mainToken);
|
|
3581
|
-
const specArgs = annotationSpec.arguments;
|
|
3582
3995
|
for (let i = 0; i < (args?.length ?? 0); i++) {
|
|
3583
3996
|
const argSpec = specArgs[i];
|
|
3584
3997
|
if (argSpec?.type === "ref" && args?.[i]) {
|
|
@@ -3593,7 +4006,7 @@ var AtscriptDoc = class {
|
|
|
3593
4006
|
this.referred.push(refToken);
|
|
3594
4007
|
}
|
|
3595
4008
|
}
|
|
3596
|
-
if (argSpec?.type
|
|
4009
|
+
if (isBacktickArgType(argSpec?.type)) args?.[i]?.backtickNode?.registerAtDocument(this);
|
|
3597
4010
|
if (argSpec?.type === "string" && argSpec.fieldScope && args?.[i]) this.fieldPathArgs.push(args[i]);
|
|
3598
4011
|
}
|
|
3599
4012
|
} else {
|
|
@@ -4519,6 +4932,7 @@ var AtscriptDoc = class {
|
|
|
4519
4932
|
_define_property$3(this, "nodes", void 0);
|
|
4520
4933
|
_define_property$3(this, "_text", void 0);
|
|
4521
4934
|
_define_property$3(this, "_annotateNodesByTarget", void 0);
|
|
4935
|
+
/** Lexical backtick tokens already parsed by `registerAnnotation`. */ _define_property$3(this, "parsedBacktickArgs", void 0);
|
|
4522
4936
|
_define_property$3(this, "resolvedAnnotations", void 0);
|
|
4523
4937
|
_define_property$3(this, "annotations", void 0);
|
|
4524
4938
|
_define_property$3(this, "_nextTypeId", void 0);
|
|
@@ -4543,6 +4957,7 @@ var AtscriptDoc = class {
|
|
|
4543
4957
|
this._annotationSpecCache = /* @__PURE__ */ new Map();
|
|
4544
4958
|
this.nodes = [];
|
|
4545
4959
|
this._text = "";
|
|
4960
|
+
this.parsedBacktickArgs = /* @__PURE__ */ new WeakSet();
|
|
4546
4961
|
this.resolvedAnnotations = [];
|
|
4547
4962
|
this.annotations = [];
|
|
4548
4963
|
this._nextTypeId = 0;
|
|
@@ -5267,7 +5682,9 @@ var AtscriptRepo = class {
|
|
|
5267
5682
|
async getUsedAnnotations() {
|
|
5268
5683
|
const ANNOTATION_TYPE_MAP = {
|
|
5269
5684
|
ref: "import(\"@atscript/typescript/utils\").AtscriptRef",
|
|
5270
|
-
query: "import(\"@atscript/typescript/utils\").AtscriptQueryNode"
|
|
5685
|
+
query: "import(\"@atscript/typescript/utils\").AtscriptQueryNode",
|
|
5686
|
+
expr: "import(\"@atscript/typescript/utils\").AtscriptExprNode",
|
|
5687
|
+
order: "import(\"@atscript/typescript/utils\").AtscriptOrderItem[]"
|
|
5271
5688
|
};
|
|
5272
5689
|
const mapArgType = (t) => ANNOTATION_TYPE_MAP[t] || t;
|
|
5273
5690
|
const manager = await this.getSharedPluginManager();
|
|
@@ -5931,6 +6348,7 @@ exports.$n = $n;
|
|
|
5931
6348
|
exports.AnnotationSpec = AnnotationSpec;
|
|
5932
6349
|
exports.AtscriptDoc = AtscriptDoc;
|
|
5933
6350
|
exports.AtscriptRepo = AtscriptRepo;
|
|
6351
|
+
exports.BACKTICK_ARG_VOCABULARY = BACKTICK_ARG_VOCABULARY;
|
|
5934
6352
|
exports.BuildRepo = BuildRepo;
|
|
5935
6353
|
exports.DB_ENTITY_ANNOTATIONS = DB_ENTITY_ANNOTATIONS;
|
|
5936
6354
|
exports.DEFAULT_FORMAT = DEFAULT_FORMAT;
|
|
@@ -5938,10 +6356,16 @@ exports.PluginManager = PluginManager;
|
|
|
5938
6356
|
exports.SemanticAnnotateNode = SemanticAnnotateNode;
|
|
5939
6357
|
exports.SemanticArrayNode = SemanticArrayNode;
|
|
5940
6358
|
exports.SemanticConstNode = SemanticConstNode;
|
|
6359
|
+
exports.SemanticExprBinaryNode = SemanticExprBinaryNode;
|
|
6360
|
+
exports.SemanticExprCallNode = SemanticExprCallNode;
|
|
6361
|
+
exports.SemanticExprNode = SemanticExprNode;
|
|
6362
|
+
exports.SemanticExprNumberNode = SemanticExprNumberNode;
|
|
6363
|
+
exports.SemanticExprUnaryNode = SemanticExprUnaryNode;
|
|
5941
6364
|
exports.SemanticGroup = SemanticGroup;
|
|
5942
6365
|
exports.SemanticImportNode = SemanticImportNode;
|
|
5943
6366
|
exports.SemanticInterfaceNode = SemanticInterfaceNode;
|
|
5944
6367
|
exports.SemanticNode = SemanticNode;
|
|
6368
|
+
exports.SemanticOrderNode = SemanticOrderNode;
|
|
5945
6369
|
exports.SemanticPrimitiveNode = SemanticPrimitiveNode;
|
|
5946
6370
|
exports.SemanticPropNode = SemanticPropNode;
|
|
5947
6371
|
exports.SemanticQueryComparisonNode = SemanticQueryComparisonNode;
|
|
@@ -5971,6 +6395,7 @@ exports.hasNavPropAnnotation = hasNavPropAnnotation;
|
|
|
5971
6395
|
exports.isAnnotate = isAnnotate;
|
|
5972
6396
|
exports.isAnnotationSpec = isAnnotationSpec;
|
|
5973
6397
|
exports.isArray = isArray;
|
|
6398
|
+
exports.isBacktickArgType = isBacktickArgType;
|
|
5974
6399
|
exports.isBareId = isBareId;
|
|
5975
6400
|
exports.isBareSpecifier = isBareSpecifier;
|
|
5976
6401
|
exports.isConst = isConst;
|