Skip to content

Commit 4f33c4a

Browse files
committed
WIP: member resolution
1 parent 0d02709 commit 4f33c4a

File tree

10 files changed

+390
-110
lines changed

10 files changed

+390
-110
lines changed

packages/schema/src/language-server/generated/ast.ts

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function isAbstractDeclaration(item: unknown): item is AbstractDeclaratio
1515
return reflection.isInstance(item, AbstractDeclaration);
1616
}
1717

18-
export type Expression = ArrayExpr | BinaryExpr | InvocationExpr | LiteralExpr | ReferenceExpr | UnaryExpr;
18+
export type Expression = ArrayExpr | BinaryExpr | InvocationExpr | LiteralExpr | MemberAccessExpr | ReferenceExpr | UnaryExpr;
1919

2020
export const Expression = 'Expression';
2121

@@ -25,7 +25,7 @@ export function isExpression(item: unknown): item is Expression {
2525

2626
export type QualifiedName = string;
2727

28-
export type ReferenceTarget = DataModelField | EnumField | Function | FunctionParam;
28+
export type ReferenceTarget = DataModelField | EnumField | FunctionParam;
2929

3030
export const ReferenceTarget = 'ReferenceTarget';
3131

@@ -42,7 +42,7 @@ export function isTypeDeclaration(item: unknown): item is TypeDeclaration {
4242
}
4343

4444
export interface ArrayExpr extends AstNode {
45-
readonly $container: ArrayExpr | BinaryExpr | DataModelAttribute | DataModelFieldAttribute | DataSourceField | Function | InvocationExpr | UnaryExpr;
45+
readonly $container: ArrayExpr | BinaryExpr | DataModelAttribute | DataModelFieldAttribute | DataSourceField | Function | InvocationExpr | MemberAccessExpr | UnaryExpr;
4646
items: Array<Expression>
4747
}
4848

@@ -64,7 +64,7 @@ export function isAttribute(item: unknown): item is Attribute {
6464
}
6565

6666
export interface BinaryExpr extends AstNode {
67-
readonly $container: ArrayExpr | BinaryExpr | DataModelAttribute | DataModelFieldAttribute | DataSourceField | Function | InvocationExpr | UnaryExpr;
67+
readonly $container: ArrayExpr | BinaryExpr | DataModelAttribute | DataModelFieldAttribute | DataSourceField | Function | InvocationExpr | MemberAccessExpr | UnaryExpr;
6868
left: Expression
6969
operator: '!=' | '&&' | '*' | '+' | '-' | '/' | '<' | '<=' | '==' | '>' | '>=' | '||'
7070
right: Expression
@@ -131,7 +131,7 @@ export interface DataModelFieldType extends AstNode {
131131
array: boolean
132132
optional: boolean
133133
reference?: Reference<TypeDeclaration>
134-
type?: 'Boolean' | 'DateTime' | 'Float' | 'Int' | 'JSON' | 'String'
134+
type?: 'Boolean' | 'DateTime' | 'Int' | 'JSON' | 'String'
135135
}
136136

137137
export const DataModelFieldType = 'DataModelFieldType';
@@ -155,7 +155,7 @@ export function isDataSource(item: unknown): item is DataSource {
155155
export interface DataSourceField extends AstNode {
156156
readonly $container: DataSource;
157157
name: string
158-
value: Expression
158+
value: InvocationExpr | LiteralExpr
159159
}
160160

161161
export const DataSourceField = 'DataSourceField';
@@ -216,7 +216,7 @@ export interface FunctionParamType extends AstNode {
216216
readonly $container: FunctionParam;
217217
array: boolean
218218
reference?: Reference<TypeDeclaration>
219-
type?: 'Boolean' | 'DateTime' | 'Float' | 'Int' | 'JSON' | 'String'
219+
type?: 'Boolean' | 'DateTime' | 'Int' | 'JSON' | 'String'
220220
}
221221

222222
export const FunctionParamType = 'FunctionParamType';
@@ -226,9 +226,9 @@ export function isFunctionParamType(item: unknown): item is FunctionParamType {
226226
}
227227

228228
export interface InvocationExpr extends AstNode {
229-
readonly $container: ArrayExpr | BinaryExpr | DataModelAttribute | DataModelFieldAttribute | DataSourceField | Function | InvocationExpr | UnaryExpr;
229+
readonly $container: ArrayExpr | BinaryExpr | DataModelAttribute | DataModelFieldAttribute | DataSourceField | Function | InvocationExpr | MemberAccessExpr | UnaryExpr;
230230
args: Array<Expression>
231-
function: string
231+
function: Reference<Function>
232232
}
233233

234234
export const InvocationExpr = 'InvocationExpr';
@@ -238,7 +238,7 @@ export function isInvocationExpr(item: unknown): item is InvocationExpr {
238238
}
239239

240240
export interface LiteralExpr extends AstNode {
241-
readonly $container: ArrayExpr | BinaryExpr | DataModelAttribute | DataModelFieldAttribute | DataSourceField | Function | InvocationExpr | UnaryExpr;
241+
readonly $container: ArrayExpr | BinaryExpr | DataModelAttribute | DataModelFieldAttribute | DataSourceField | Function | InvocationExpr | MemberAccessExpr | UnaryExpr;
242242
value: boolean | number | string
243243
}
244244

@@ -248,6 +248,18 @@ export function isLiteralExpr(item: unknown): item is LiteralExpr {
248248
return reflection.isInstance(item, LiteralExpr);
249249
}
250250

251+
export interface MemberAccessExpr extends AstNode {
252+
readonly $container: ArrayExpr | BinaryExpr | DataModelAttribute | DataModelFieldAttribute | DataSourceField | Function | InvocationExpr | MemberAccessExpr | UnaryExpr;
253+
member: Reference<DataModelField>
254+
operand: Expression
255+
}
256+
257+
export const MemberAccessExpr = 'MemberAccessExpr';
258+
259+
export function isMemberAccessExpr(item: unknown): item is MemberAccessExpr {
260+
return reflection.isInstance(item, MemberAccessExpr);
261+
}
262+
251263
export interface Model extends AstNode {
252264
declarations: Array<AbstractDeclaration>
253265
}
@@ -259,7 +271,7 @@ export function isModel(item: unknown): item is Model {
259271
}
260272

261273
export interface ReferenceExpr extends AstNode {
262-
readonly $container: ArrayExpr | BinaryExpr | DataModelAttribute | DataModelFieldAttribute | DataSourceField | Function | InvocationExpr | UnaryExpr;
274+
readonly $container: ArrayExpr | BinaryExpr | DataModelAttribute | DataModelFieldAttribute | DataSourceField | Function | InvocationExpr | MemberAccessExpr | UnaryExpr;
263275
target: Reference<ReferenceTarget>
264276
}
265277

@@ -270,7 +282,7 @@ export function isReferenceExpr(item: unknown): item is ReferenceExpr {
270282
}
271283

272284
export interface UnaryExpr extends AstNode {
273-
readonly $container: ArrayExpr | BinaryExpr | DataModelAttribute | DataModelFieldAttribute | DataSourceField | Function | InvocationExpr | UnaryExpr;
285+
readonly $container: ArrayExpr | BinaryExpr | DataModelAttribute | DataModelFieldAttribute | DataSourceField | Function | InvocationExpr | MemberAccessExpr | UnaryExpr;
274286
arg: Expression
275287
operator: '!' | '+' | '-'
276288
}
@@ -281,14 +293,14 @@ export function isUnaryExpr(item: unknown): item is UnaryExpr {
281293
return reflection.isInstance(item, UnaryExpr);
282294
}
283295

284-
export type ZModelAstType = 'AbstractDeclaration' | 'ArrayExpr' | 'Attribute' | 'BinaryExpr' | 'DataModel' | 'DataModelAttribute' | 'DataModelField' | 'DataModelFieldAttribute' | 'DataModelFieldType' | 'DataSource' | 'DataSourceField' | 'Enum' | 'EnumField' | 'Expression' | 'Function' | 'FunctionParam' | 'FunctionParamType' | 'InvocationExpr' | 'LiteralExpr' | 'Model' | 'ReferenceExpr' | 'ReferenceTarget' | 'TypeDeclaration' | 'UnaryExpr';
296+
export type ZModelAstType = 'AbstractDeclaration' | 'ArrayExpr' | 'Attribute' | 'BinaryExpr' | 'DataModel' | 'DataModelAttribute' | 'DataModelField' | 'DataModelFieldAttribute' | 'DataModelFieldType' | 'DataSource' | 'DataSourceField' | 'Enum' | 'EnumField' | 'Expression' | 'Function' | 'FunctionParam' | 'FunctionParamType' | 'InvocationExpr' | 'LiteralExpr' | 'MemberAccessExpr' | 'Model' | 'ReferenceExpr' | 'ReferenceTarget' | 'TypeDeclaration' | 'UnaryExpr';
285297

286-
export type ZModelAstReference = 'DataModelAttribute:decl' | 'DataModelFieldAttribute:decl' | 'DataModelFieldType:reference' | 'FunctionParamType:reference' | 'ReferenceExpr:target';
298+
export type ZModelAstReference = 'DataModelAttribute:decl' | 'DataModelFieldAttribute:decl' | 'DataModelFieldType:reference' | 'FunctionParamType:reference' | 'InvocationExpr:function' | 'MemberAccessExpr:member' | 'ReferenceExpr:target';
287299

288300
export class ZModelAstReflection implements AstReflection {
289301

290302
getAllTypes(): string[] {
291-
return ['AbstractDeclaration', 'ArrayExpr', 'Attribute', 'BinaryExpr', 'DataModel', 'DataModelAttribute', 'DataModelField', 'DataModelFieldAttribute', 'DataModelFieldType', 'DataSource', 'DataSourceField', 'Enum', 'EnumField', 'Expression', 'Function', 'FunctionParam', 'FunctionParamType', 'InvocationExpr', 'LiteralExpr', 'Model', 'ReferenceExpr', 'ReferenceTarget', 'TypeDeclaration', 'UnaryExpr'];
303+
return ['AbstractDeclaration', 'ArrayExpr', 'Attribute', 'BinaryExpr', 'DataModel', 'DataModelAttribute', 'DataModelField', 'DataModelFieldAttribute', 'DataModelFieldType', 'DataSource', 'DataSourceField', 'Enum', 'EnumField', 'Expression', 'Function', 'FunctionParam', 'FunctionParamType', 'InvocationExpr', 'LiteralExpr', 'MemberAccessExpr', 'Model', 'ReferenceExpr', 'ReferenceTarget', 'TypeDeclaration', 'UnaryExpr'];
292304
}
293305

294306
isInstance(node: unknown, type: string): boolean {
@@ -304,12 +316,14 @@ export class ZModelAstReflection implements AstReflection {
304316
case BinaryExpr:
305317
case InvocationExpr:
306318
case LiteralExpr:
319+
case MemberAccessExpr:
307320
case ReferenceExpr:
308321
case UnaryExpr: {
309322
return this.isSubtype(Expression, supertype);
310323
}
311324
case Attribute:
312-
case DataSource: {
325+
case DataSource:
326+
case Function: {
313327
return this.isSubtype(AbstractDeclaration, supertype);
314328
}
315329
case DataModel:
@@ -321,9 +335,6 @@ export class ZModelAstReflection implements AstReflection {
321335
case FunctionParam: {
322336
return this.isSubtype(ReferenceTarget, supertype);
323337
}
324-
case Function: {
325-
return this.isSubtype(AbstractDeclaration, supertype) || this.isSubtype(ReferenceTarget, supertype);
326-
}
327338
default: {
328339
return false;
329340
}
@@ -344,6 +355,12 @@ export class ZModelAstReflection implements AstReflection {
344355
case 'FunctionParamType:reference': {
345356
return TypeDeclaration;
346357
}
358+
case 'InvocationExpr:function': {
359+
return Function;
360+
}
361+
case 'MemberAccessExpr:member': {
362+
return DataModelField;
363+
}
347364
case 'ReferenceExpr:target': {
348365
return ReferenceTarget;
349366
}

packages/schema/src/language-server/generated/grammar.ts

Lines changed: 89 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,23 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ||(loadedZModelG
163163
"feature": "value",
164164
"operator": "=",
165165
"terminal": {
166-
"$type": "RuleCall",
167-
"rule": {
168-
"$refText": "Expression"
169-
},
170-
"arguments": []
166+
"$type": "Alternatives",
167+
"elements": [
168+
{
169+
"$type": "RuleCall",
170+
"rule": {
171+
"$refText": "LiteralExpr"
172+
},
173+
"arguments": []
174+
},
175+
{
176+
"$type": "RuleCall",
177+
"rule": {
178+
"$refText": "InvocationExpr"
179+
},
180+
"arguments": []
181+
}
182+
]
171183
}
172184
}
173185
]
@@ -216,7 +228,7 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ||(loadedZModelG
216228
{
217229
"$type": "RuleCall",
218230
"rule": {
219-
"$refText": "NUMBER"
231+
"$refText": "INT"
220232
},
221233
"arguments": []
222234
},
@@ -315,7 +327,7 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ||(loadedZModelG
315327
"terminal": {
316328
"$type": "RuleCall",
317329
"rule": {
318-
"$refText": "QualifiedName"
330+
"$refText": "ID"
319331
},
320332
"arguments": []
321333
},
@@ -340,11 +352,11 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ||(loadedZModelG
340352
"feature": "function",
341353
"operator": "=",
342354
"terminal": {
343-
"$type": "RuleCall",
344-
"rule": {
345-
"$refText": "ID"
355+
"$type": "CrossReference",
356+
"type": {
357+
"$refText": "Function"
346358
},
347-
"arguments": []
359+
"deprecatedSyntax": false
348360
}
349361
},
350362
{
@@ -423,7 +435,7 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ||(loadedZModelG
423435
},
424436
{
425437
"$type": "ParserRule",
426-
"name": "MultDivExpr",
438+
"name": "MemberAccessExpr",
427439
"inferredType": {
428440
"$type": "InferredType",
429441
"name": "Expression"
@@ -438,6 +450,68 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ||(loadedZModelG
438450
},
439451
"arguments": []
440452
},
453+
{
454+
"$type": "Group",
455+
"elements": [
456+
{
457+
"$type": "Action",
458+
"inferredType": {
459+
"$type": "InferredType",
460+
"name": "MemberAccessExpr"
461+
},
462+
"feature": "operand",
463+
"operator": "="
464+
},
465+
{
466+
"$type": "Group",
467+
"elements": [
468+
{
469+
"$type": "Keyword",
470+
"value": "."
471+
},
472+
{
473+
"$type": "Assignment",
474+
"feature": "member",
475+
"operator": "=",
476+
"terminal": {
477+
"$type": "CrossReference",
478+
"type": {
479+
"$refText": "DataModelField"
480+
},
481+
"deprecatedSyntax": false
482+
}
483+
}
484+
]
485+
}
486+
],
487+
"cardinality": "*"
488+
}
489+
]
490+
},
491+
"definesHiddenTokens": false,
492+
"entry": false,
493+
"fragment": false,
494+
"hiddenTokens": [],
495+
"parameters": [],
496+
"wildcard": false
497+
},
498+
{
499+
"$type": "ParserRule",
500+
"name": "MultDivExpr",
501+
"inferredType": {
502+
"$type": "InferredType",
503+
"name": "Expression"
504+
},
505+
"alternatives": {
506+
"$type": "Group",
507+
"elements": [
508+
{
509+
"$type": "RuleCall",
510+
"rule": {
511+
"$refText": "MemberAccessExpr"
512+
},
513+
"arguments": []
514+
},
441515
{
442516
"$type": "Group",
443517
"elements": [
@@ -475,7 +549,7 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ||(loadedZModelG
475549
"terminal": {
476550
"$type": "RuleCall",
477551
"rule": {
478-
"$refText": "PrimaryExpr"
552+
"$refText": "MemberAccessExpr"
479553
},
480554
"arguments": []
481555
}
@@ -1530,10 +1604,6 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ||(loadedZModelG
15301604
"$type": "Keyword",
15311605
"value": "Int"
15321606
},
1533-
{
1534-
"$type": "Keyword",
1535-
"value": "Float"
1536-
},
15371607
{
15381608
"$type": "Keyword",
15391609
"value": "DateTime"
@@ -1637,14 +1707,14 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ||(loadedZModelG
16371707
},
16381708
{
16391709
"$type": "TerminalRule",
1640-
"name": "NUMBER",
1710+
"name": "INT",
16411711
"type": {
16421712
"$type": "ReturnType",
16431713
"name": "number"
16441714
},
16451715
"terminal": {
16461716
"$type": "RegexToken",
1647-
"regex": "[+-]?[0-9]+(\\\\.[0-9]+)?"
1717+
"regex": "[+-]?[0-9]+"
16481718
},
16491719
"fragment": false,
16501720
"hidden": false
@@ -1682,14 +1752,6 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ||(loadedZModelG
16821752
"isArray": false,
16831753
"isRef": false
16841754
},
1685-
{
1686-
"$type": "AtomType",
1687-
"refType": {
1688-
"$refText": "Function"
1689-
},
1690-
"isArray": false,
1691-
"isRef": false
1692-
},
16931755
{
16941756
"$type": "AtomType",
16951757
"refType": {

packages/schema/src/language-server/stdlib.zmodel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
function env()
12
function auth() {}
2-
function some(collection, predicate) {}
3+
function some() {}
34

45
attribute id() {}
56
attribute default() {}

0 commit comments

Comments
 (0)