Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
271 changes: 140 additions & 131 deletions examples/arithmetics/src/language-server/generated/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,32 @@ export type ArithmeticsTokenNames = ArithmeticsTerminalNames | ArithmeticsKeywor

export type AbstractDefinition = DeclaredParameter | Definition;

export const AbstractDefinition = 'AbstractDefinition';
export const AbstractDefinition = {
$type: 'AbstractDefinition'
} as const;

export function isAbstractDefinition(item: unknown): item is AbstractDefinition {
return reflection.isInstance(item, AbstractDefinition);
return reflection.isInstance(item, AbstractDefinition.$type);
}

export type Expression = BinaryExpression | FunctionCall | NumberLiteral;

export const Expression = 'Expression';
export const Expression = {
$type: 'Expression'
} as const;

export function isExpression(item: unknown): item is Expression {
return reflection.isInstance(item, Expression);
return reflection.isInstance(item, Expression.$type);
}

export type Statement = Definition | Evaluation;

export const Statement = 'Statement';
export const Statement = {
$type: 'Statement'
} as const;

export function isStatement(item: unknown): item is Statement {
return reflection.isInstance(item, Statement);
return reflection.isInstance(item, Statement.$type);
}

export interface BinaryExpression extends langium.AstNode {
Expand All @@ -65,10 +71,15 @@ export interface BinaryExpression extends langium.AstNode {
right: Expression;
}

export const BinaryExpression = 'BinaryExpression';
export const BinaryExpression = {
$type: 'BinaryExpression',
left: 'left',
operator: 'operator',
right: 'right'
} as const;

export function isBinaryExpression(item: unknown): item is BinaryExpression {
return reflection.isInstance(item, BinaryExpression);
return reflection.isInstance(item, BinaryExpression.$type);
}

export interface DeclaredParameter extends langium.AstNode {
Expand All @@ -77,10 +88,13 @@ export interface DeclaredParameter extends langium.AstNode {
name: string;
}

export const DeclaredParameter = 'DeclaredParameter';
export const DeclaredParameter = {
$type: 'DeclaredParameter',
name: 'name'
} as const;

export function isDeclaredParameter(item: unknown): item is DeclaredParameter {
return reflection.isInstance(item, DeclaredParameter);
return reflection.isInstance(item, DeclaredParameter.$type);
}

export interface Definition extends langium.AstNode {
Expand All @@ -91,10 +105,15 @@ export interface Definition extends langium.AstNode {
name: string;
}

export const Definition = 'Definition';
export const Definition = {
$type: 'Definition',
args: 'args',
expr: 'expr',
name: 'name'
} as const;

export function isDefinition(item: unknown): item is Definition {
return reflection.isInstance(item, Definition);
return reflection.isInstance(item, Definition.$type);
}

export interface Evaluation extends langium.AstNode {
Expand All @@ -103,10 +122,13 @@ export interface Evaluation extends langium.AstNode {
expression: Expression;
}

export const Evaluation = 'Evaluation';
export const Evaluation = {
$type: 'Evaluation',
expression: 'expression'
} as const;

export function isEvaluation(item: unknown): item is Evaluation {
return reflection.isInstance(item, Evaluation);
return reflection.isInstance(item, Evaluation.$type);
}

export interface FunctionCall extends langium.AstNode {
Expand All @@ -116,10 +138,14 @@ export interface FunctionCall extends langium.AstNode {
func: langium.Reference<AbstractDefinition>;
}

export const FunctionCall = 'FunctionCall';
export const FunctionCall = {
$type: 'FunctionCall',
args: 'args',
func: 'func'
} as const;

export function isFunctionCall(item: unknown): item is FunctionCall {
return reflection.isInstance(item, FunctionCall);
return reflection.isInstance(item, FunctionCall.$type);
}

export interface Module extends langium.AstNode {
Expand All @@ -128,10 +154,14 @@ export interface Module extends langium.AstNode {
statements: Array<Statement>;
}

export const Module = 'Module';
export const Module = {
$type: 'Module',
name: 'name',
statements: 'statements'
} as const;

export function isModule(item: unknown): item is Module {
return reflection.isInstance(item, Module);
return reflection.isInstance(item, Module.$type);
}

export interface NumberLiteral extends langium.AstNode {
Expand All @@ -140,10 +170,13 @@ export interface NumberLiteral extends langium.AstNode {
value: number;
}

export const NumberLiteral = 'NumberLiteral';
export const NumberLiteral = {
$type: 'NumberLiteral',
value: 'value'
} as const;

export function isNumberLiteral(item: unknown): item is NumberLiteral {
return reflection.isInstance(item, NumberLiteral);
return reflection.isInstance(item, NumberLiteral.$type);
}

export type ArithmeticsAstType = {
Expand All @@ -160,117 +193,93 @@ export type ArithmeticsAstType = {
}

export class ArithmeticsAstReflection extends langium.AbstractAstReflection {

getAllTypes(): string[] {
return [AbstractDefinition, BinaryExpression, DeclaredParameter, Definition, Evaluation, Expression, FunctionCall, Module, NumberLiteral, Statement];
}

protected override computeIsSubtype(subtype: string, supertype: string): boolean {
switch (subtype) {
case BinaryExpression:
case FunctionCall:
case NumberLiteral: {
return this.isSubtype(Expression, supertype);
}
case DeclaredParameter: {
return this.isSubtype(AbstractDefinition, supertype);
}
case Definition: {
return this.isSubtype(AbstractDefinition, supertype) || this.isSubtype(Statement, supertype);
}
case Evaluation: {
return this.isSubtype(Statement, supertype);
}
default: {
return false;
}
}
}

getReferenceType(refInfo: langium.ReferenceInfo): string {
const referenceId = `${refInfo.container.$type}:${refInfo.property}`;
switch (referenceId) {
case 'FunctionCall:func': {
return AbstractDefinition;
}
default: {
throw new Error(`${referenceId} is not a valid reference id.`);
}
}
}

getTypeMetaData(type: string): langium.TypeMetaData {
switch (type) {
case BinaryExpression: {
return {
name: BinaryExpression,
properties: [
{ name: 'left' },
{ name: 'operator' },
{ name: 'right' }
]
};
}
case DeclaredParameter: {
return {
name: DeclaredParameter,
properties: [
{ name: 'name' }
]
};
}
case Definition: {
return {
name: Definition,
properties: [
{ name: 'args', defaultValue: [] },
{ name: 'expr' },
{ name: 'name' }
]
};
}
case Evaluation: {
return {
name: Evaluation,
properties: [
{ name: 'expression' }
]
};
}
case FunctionCall: {
return {
name: FunctionCall,
properties: [
{ name: 'args', defaultValue: [] },
{ name: 'func' }
]
};
}
case Module: {
return {
name: Module,
properties: [
{ name: 'name' },
{ name: 'statements', defaultValue: [] }
]
};
}
case NumberLiteral: {
return {
name: NumberLiteral,
properties: [
{ name: 'value' }
]
};
}
default: {
return {
name: type,
properties: []
};
}
}
}
override readonly types = {
BinaryExpression: {
name: BinaryExpression.$type,
properties: {
left: {
name: 'left'
},
operator: {
name: 'operator'
},
right: {
name: 'right'
}
},
superTypes: ['Expression']
},
DeclaredParameter: {
name: DeclaredParameter.$type,
properties: {
name: {
name: 'name'
}
},
superTypes: ['AbstractDefinition']
},
Definition: {
name: Definition.$type,
properties: {
args: {
name: 'args',
defaultValue: []
},
expr: {
name: 'expr'
},
name: {
name: 'name'
}
},
superTypes: ['AbstractDefinition', 'Statement']
},
Evaluation: {
name: Evaluation.$type,
properties: {
expression: {
name: 'expression'
}
},
superTypes: ['Statement']
},
FunctionCall: {
name: FunctionCall.$type,
properties: {
args: {
name: 'args',
defaultValue: []
},
func: {
name: 'func',
referenceType: 'AbstractDefinition'
}
},
superTypes: ['Expression']
},
Module: {
name: Module.$type,
properties: {
name: {
name: 'name'
},
statements: {
name: 'statements',
defaultValue: []
}
},
superTypes: []
},
NumberLiteral: {
name: NumberLiteral.$type,
properties: {
value: {
name: 'value'
}
},
superTypes: ['Expression']
},
} as const satisfies langium.AstMetaData
}

export const reflection = new ArithmeticsAstReflection();
Loading