Skip to content

Commit 507845b

Browse files
committed
grammar and type generation support
1 parent 28b130f commit 507845b

File tree

13 files changed

+694
-158
lines changed

13 files changed

+694
-158
lines changed

packages/language/src/generated/ast.ts

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const ZModelTerminals = {
2020
SL_COMMENT: /\/\/[^\n\r]*/,
2121
};
2222

23-
export type AbstractDeclaration = Attribute | DataModel | DataSource | Enum | FunctionDecl | GeneratorDecl | Plugin;
23+
export type AbstractDeclaration = Attribute | DataModel | DataSource | Enum | FunctionDecl | GeneratorDecl | Plugin | TypeDef;
2424

2525
export const AbstractDeclaration = 'AbstractDeclaration';
2626

@@ -78,10 +78,10 @@ export function isReferenceTarget(item: unknown): item is ReferenceTarget {
7878
return reflection.isInstance(item, ReferenceTarget);
7979
}
8080

81-
export type RegularID = 'abstract' | 'attribute' | 'datasource' | 'enum' | 'import' | 'in' | 'model' | 'plugin' | 'view' | string;
81+
export type RegularID = 'abstract' | 'attribute' | 'datasource' | 'enum' | 'import' | 'in' | 'model' | 'plugin' | 'type' | 'view' | string;
8282

8383
export function isRegularID(item: unknown): item is RegularID {
84-
return item === 'model' || item === 'enum' || item === 'attribute' || item === 'datasource' || item === 'plugin' || item === 'abstract' || item === 'in' || item === 'view' || item === 'import' || (typeof item === 'string' && (/[_a-zA-Z][\w_]*/.test(item)));
84+
return item === 'model' || item === 'enum' || item === 'attribute' || item === 'datasource' || item === 'plugin' || item === 'abstract' || item === 'in' || item === 'view' || item === 'import' || item === 'type' || (typeof item === 'string' && (/[_a-zA-Z][\w_]*/.test(item)));
8585
}
8686

8787
export type RegularIDWithTypeNames = 'Any' | 'BigInt' | 'Boolean' | 'Bytes' | 'DateTime' | 'Decimal' | 'Float' | 'Int' | 'Json' | 'Null' | 'Object' | 'String' | 'Unsupported' | RegularID;
@@ -90,7 +90,7 @@ export function isRegularIDWithTypeNames(item: unknown): item is RegularIDWithTy
9090
return isRegularID(item) || item === 'String' || item === 'Boolean' || item === 'Int' || item === 'BigInt' || item === 'Float' || item === 'Decimal' || item === 'DateTime' || item === 'Json' || item === 'Bytes' || item === 'Null' || item === 'Object' || item === 'Any' || item === 'Unsupported';
9191
}
9292

93-
export type TypeDeclaration = DataModel | Enum;
93+
export type TypeDeclaration = DataModel | Enum | TypeDef;
9494

9595
export const TypeDeclaration = 'TypeDeclaration';
9696

@@ -290,7 +290,7 @@ export function isDataModelAttribute(item: unknown): item is DataModelAttribute
290290
}
291291

292292
export interface DataModelField extends AstNode {
293-
readonly $container: DataModel | Enum | FunctionDecl;
293+
readonly $container: DataModel | Enum | FunctionDecl | TypeDef;
294294
readonly $type: 'DataModelField';
295295
attributes: Array<DataModelFieldAttribute>
296296
comments: Array<string>
@@ -362,7 +362,7 @@ export function isEnum(item: unknown): item is Enum {
362362
}
363363

364364
export interface EnumField extends AstNode {
365-
readonly $container: DataModel | Enum | FunctionDecl;
365+
readonly $container: DataModel | Enum | FunctionDecl | TypeDef;
366366
readonly $type: 'EnumField';
367367
attributes: Array<DataModelFieldAttribute>
368368
comments: Array<string>
@@ -405,7 +405,7 @@ export function isFunctionDecl(item: unknown): item is FunctionDecl {
405405
}
406406

407407
export interface FunctionParam extends AstNode {
408-
readonly $container: DataModel | Enum | FunctionDecl;
408+
readonly $container: DataModel | Enum | FunctionDecl | TypeDef;
409409
readonly $type: 'FunctionParam';
410410
name: RegularID
411411
optional: boolean
@@ -620,6 +620,20 @@ export function isThisExpr(item: unknown): item is ThisExpr {
620620
return reflection.isInstance(item, ThisExpr);
621621
}
622622

623+
export interface TypeDef extends AstNode {
624+
readonly $container: Model;
625+
readonly $type: 'TypeDef';
626+
comments: Array<string>
627+
fields: Array<DataModelField>
628+
name: RegularID
629+
}
630+
631+
export const TypeDef = 'TypeDef';
632+
633+
export function isTypeDef(item: unknown): item is TypeDef {
634+
return reflection.isInstance(item, TypeDef);
635+
}
636+
623637
export interface UnaryExpr extends AstNode {
624638
readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | ReferenceArg | UnaryExpr | UnsupportedFieldType;
625639
readonly $type: 'UnaryExpr';
@@ -691,14 +705,15 @@ export type ZModelAstType = {
691705
StringLiteral: StringLiteral
692706
ThisExpr: ThisExpr
693707
TypeDeclaration: TypeDeclaration
708+
TypeDef: TypeDef
694709
UnaryExpr: UnaryExpr
695710
UnsupportedFieldType: UnsupportedFieldType
696711
}
697712

698713
export class ZModelAstReflection extends AbstractAstReflection {
699714

700715
getAllTypes(): string[] {
701-
return ['AbstractDeclaration', 'Argument', 'ArrayExpr', 'Attribute', 'AttributeArg', 'AttributeParam', 'AttributeParamType', 'BinaryExpr', 'BooleanLiteral', 'ConfigArrayExpr', 'ConfigExpr', 'ConfigField', 'ConfigInvocationArg', 'ConfigInvocationExpr', 'DataModel', 'DataModelAttribute', 'DataModelField', 'DataModelFieldAttribute', 'DataModelFieldType', 'DataSource', 'Enum', 'EnumField', 'Expression', 'FieldInitializer', 'FunctionDecl', 'FunctionParam', 'FunctionParamType', 'GeneratorDecl', 'InternalAttribute', 'InvocationExpr', 'LiteralExpr', 'MemberAccessExpr', 'Model', 'ModelImport', 'NullExpr', 'NumberLiteral', 'ObjectExpr', 'Plugin', 'PluginField', 'ReferenceArg', 'ReferenceExpr', 'ReferenceTarget', 'StringLiteral', 'ThisExpr', 'TypeDeclaration', 'UnaryExpr', 'UnsupportedFieldType'];
716+
return ['AbstractDeclaration', 'Argument', 'ArrayExpr', 'Attribute', 'AttributeArg', 'AttributeParam', 'AttributeParamType', 'BinaryExpr', 'BooleanLiteral', 'ConfigArrayExpr', 'ConfigExpr', 'ConfigField', 'ConfigInvocationArg', 'ConfigInvocationExpr', 'DataModel', 'DataModelAttribute', 'DataModelField', 'DataModelFieldAttribute', 'DataModelFieldType', 'DataSource', 'Enum', 'EnumField', 'Expression', 'FieldInitializer', 'FunctionDecl', 'FunctionParam', 'FunctionParamType', 'GeneratorDecl', 'InternalAttribute', 'InvocationExpr', 'LiteralExpr', 'MemberAccessExpr', 'Model', 'ModelImport', 'NullExpr', 'NumberLiteral', 'ObjectExpr', 'Plugin', 'PluginField', 'ReferenceArg', 'ReferenceExpr', 'ReferenceTarget', 'StringLiteral', 'ThisExpr', 'TypeDeclaration', 'TypeDef', 'UnaryExpr', 'UnsupportedFieldType'];
702717
}
703718

704719
protected override computeIsSubtype(subtype: string, supertype: string): boolean {
@@ -729,7 +744,8 @@ export class ZModelAstReflection extends AbstractAstReflection {
729744
return this.isSubtype(ConfigExpr, supertype);
730745
}
731746
case DataModel:
732-
case Enum: {
747+
case Enum:
748+
case TypeDef: {
733749
return this.isSubtype(AbstractDeclaration, supertype) || this.isSubtype(TypeDeclaration, supertype);
734750
}
735751
case DataModelField:
@@ -989,6 +1005,15 @@ export class ZModelAstReflection extends AbstractAstReflection {
9891005
]
9901006
};
9911007
}
1008+
case 'TypeDef': {
1009+
return {
1010+
name: 'TypeDef',
1011+
mandatory: [
1012+
{ name: 'comments', type: 'array' },
1013+
{ name: 'fields', type: 'array' }
1014+
]
1015+
};
1016+
}
9921017
default: {
9931018
return {
9941019
name: type,

0 commit comments

Comments
 (0)