added generics ast
This commit is contained in:
@@ -8,6 +8,9 @@ import {
|
||||
FunctionCall,
|
||||
FunctionDeclaration,
|
||||
FunctionTypeUsage,
|
||||
GenericDeclaration,
|
||||
GenericParameter,
|
||||
GenericUsage,
|
||||
Identifier,
|
||||
IfExpression,
|
||||
Impl,
|
||||
@@ -44,6 +47,29 @@ function nextUnknown() {
|
||||
|
||||
export const semantics = boringGrammar.createSemantics();
|
||||
semantics.addOperation<any>("toAST", {
|
||||
GenericUsage(_1, typeUsage, _3): GenericUsage {
|
||||
return {
|
||||
genericUsage: "Known",
|
||||
parameters: typeUsage.asIteration().children.map((c) => c.toAST()),
|
||||
};
|
||||
},
|
||||
GenericParameter_conditions(identifier, _2, typeUsage): GenericParameter {
|
||||
return {
|
||||
name: identifier.toAST(),
|
||||
bounds: typeUsage.asIteration().children.map((c) => c.toAST()),
|
||||
};
|
||||
},
|
||||
GenericParameter(identifier): GenericParameter {
|
||||
return {
|
||||
name: identifier.toAST(),
|
||||
bounds: [],
|
||||
};
|
||||
},
|
||||
GenericDeclaration(_1, parameters, _2): GenericDeclaration {
|
||||
return {
|
||||
parameters: parameters.asIteration().children.map((c) => c.toAST()),
|
||||
};
|
||||
},
|
||||
LiteralInt(a): Expression {
|
||||
return {
|
||||
statementType: "Expression",
|
||||
@@ -52,6 +78,7 @@ semantics.addOperation<any>("toAST", {
|
||||
value: this.sourceString,
|
||||
type: {
|
||||
typeUsage: "NamedTypeUsage",
|
||||
typeParameters: { genericUsage: "Known", parameters: [] },
|
||||
name: { text: "i64", spanStart: 0, spanEnd: 0 },
|
||||
},
|
||||
},
|
||||
@@ -66,6 +93,7 @@ semantics.addOperation<any>("toAST", {
|
||||
value: this.sourceString,
|
||||
type: {
|
||||
typeUsage: "NamedTypeUsage",
|
||||
typeParameters: { genericUsage: "Known", parameters: [] },
|
||||
name: { text: "f64", spanStart: 0, spanEnd: 0 },
|
||||
},
|
||||
},
|
||||
@@ -80,6 +108,7 @@ semantics.addOperation<any>("toAST", {
|
||||
value: this.sourceString,
|
||||
type: {
|
||||
typeUsage: "NamedTypeUsage",
|
||||
typeParameters: { genericUsage: "Known", parameters: [] },
|
||||
name: { text: "bool", spanStart: 0, spanEnd: 0 },
|
||||
},
|
||||
},
|
||||
@@ -94,6 +123,7 @@ semantics.addOperation<any>("toAST", {
|
||||
value: text.sourceString,
|
||||
type: {
|
||||
typeUsage: "NamedTypeUsage",
|
||||
typeParameters: { genericUsage: "Known", parameters: [] },
|
||||
name: { text: "String", spanStart: 0, spanEnd: 0 },
|
||||
},
|
||||
},
|
||||
@@ -106,16 +136,25 @@ semantics.addOperation<any>("toAST", {
|
||||
expression: expression.toAST(),
|
||||
};
|
||||
},
|
||||
LiteralStruct(identifier, _2, fields, _4): Expression {
|
||||
LiteralStruct(identifier, genericUsage, _2, fields, _4): Expression {
|
||||
const gu = genericUsage.toAST();
|
||||
return {
|
||||
statementType: "Expression",
|
||||
subExpression: {
|
||||
expressionType: "LiteralStruct",
|
||||
name: identifier.toAST(),
|
||||
typeParameters: gu.length ? gu[0] : { genericUsage: "Unknown" },
|
||||
fields: fields.asIteration().children.map((c) => c.toAST()),
|
||||
type: { typeUsage: "NamedTypeUsage", name: identifier.toAST() },
|
||||
type: {
|
||||
typeUsage: "NamedTypeUsage",
|
||||
typeParameters: gu.length ? gu[0] : { genericUsage: "Unknown" },
|
||||
name: identifier.toAST(),
|
||||
},
|
||||
},
|
||||
type: {
|
||||
typeUsage: "UnknownTypeUsage",
|
||||
name: nextUnknown(),
|
||||
},
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
};
|
||||
},
|
||||
identifier(_1, _2): Identifier {
|
||||
@@ -144,13 +183,15 @@ semantics.addOperation<any>("toAST", {
|
||||
MemberExpression(expression): Expression {
|
||||
return expression.toAST();
|
||||
},
|
||||
MemberExpression_structGetter(expression, _2, identifier): Expression {
|
||||
MemberExpression_structGetter(expression, _2, identifier, genericUsage): Expression {
|
||||
const gu = genericUsage.toAST();
|
||||
return {
|
||||
statementType: "Expression",
|
||||
subExpression: {
|
||||
expressionType: "StructGetter",
|
||||
source: expression.toAST(),
|
||||
attribute: identifier.toAST(),
|
||||
typeParameters: gu.length ? gu[0] : { genericUsage: "Unknown" },
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
},
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
@@ -159,13 +200,15 @@ semantics.addOperation<any>("toAST", {
|
||||
CallExpression(expression): Expression {
|
||||
return expression.toAST();
|
||||
},
|
||||
CallExpression_structGetter(expression, _2, identifier): Expression {
|
||||
CallExpression_structGetter(expression, _2, identifier, genericUsage): Expression {
|
||||
const gu = genericUsage.toAST();
|
||||
return {
|
||||
statementType: "Expression",
|
||||
subExpression: {
|
||||
expressionType: "StructGetter",
|
||||
source: expression.toAST(),
|
||||
attribute: identifier.toAST(),
|
||||
typeParameters: gu.length ? gu[0] : { genericUsage: "Unknown" },
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
},
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
@@ -210,6 +253,7 @@ semantics.addOperation<any>("toAST", {
|
||||
expressionType: "StructGetter",
|
||||
source: expression.toAST(),
|
||||
attribute: identifier.toAST(),
|
||||
typeParameters: { genericUsage: "Unknown" },
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
},
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
@@ -229,15 +273,19 @@ semantics.addOperation<any>("toAST", {
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
};
|
||||
},
|
||||
Path_base(identifier): Path {
|
||||
Path_base(identifier, genericUsage): Path {
|
||||
const gu = genericUsage.toAST();
|
||||
return {
|
||||
expressionType: "Path",
|
||||
typeParameters: gu.length ? gu[0] : { genericUsage: "Unknown" },
|
||||
value: { type: "Identifier", name: identifier.toAST() },
|
||||
};
|
||||
},
|
||||
Path_nested(basePath, _2, attrIdent): Path {
|
||||
Path_nested(basePath, _2, attrIdent, genericUsage): Path {
|
||||
const gu = genericUsage.toAST();
|
||||
return {
|
||||
expressionType: "Path",
|
||||
typeParameters: gu.length ? gu[0] : { genericUsage: "Unknown" },
|
||||
value: { type: "Nested", parent: basePath.toAST(), name: attrIdent.toAST() },
|
||||
};
|
||||
},
|
||||
@@ -357,9 +405,11 @@ semantics.addOperation<any>("toAST", {
|
||||
}
|
||||
return block;
|
||||
},
|
||||
NamedTypeUsage(name): NamedTypeUsage {
|
||||
NamedTypeUsage(name, genericUsage): NamedTypeUsage {
|
||||
const gu = genericUsage.toAST();
|
||||
return {
|
||||
typeUsage: "NamedTypeUsage",
|
||||
typeParameters: gu.length ? gu[0] : { genericUsage: "Unknown" },
|
||||
name: name.toAST(),
|
||||
};
|
||||
},
|
||||
@@ -379,9 +429,20 @@ semantics.addOperation<any>("toAST", {
|
||||
type: typeUsage.toAST(),
|
||||
};
|
||||
},
|
||||
FunctionDeclaration(_1, identifier, _3, args, _4, _5, returnType): FunctionDeclaration {
|
||||
FunctionDeclaration(
|
||||
_1,
|
||||
identifier,
|
||||
genericDeclaration,
|
||||
_4,
|
||||
args,
|
||||
_5,
|
||||
_6,
|
||||
returnType,
|
||||
): FunctionDeclaration {
|
||||
const gd = genericDeclaration.toAST();
|
||||
return {
|
||||
name: identifier.toAST(),
|
||||
generic: gd.length ? gd[0] : { parameters: [] },
|
||||
arguments: args.asIteration().children.map((c) => c.toAST()),
|
||||
returnType: returnType.toAST(),
|
||||
};
|
||||
@@ -399,32 +460,54 @@ semantics.addOperation<any>("toAST", {
|
||||
type: typeUsage.toAST(),
|
||||
};
|
||||
},
|
||||
StructTypeDeclaration(_1, identifier, _3, _4, fields, _6): StructTypeDeclaration {
|
||||
StructTypeDeclaration(
|
||||
_1,
|
||||
identifier,
|
||||
genericDeclaration,
|
||||
_4,
|
||||
_5,
|
||||
fields,
|
||||
_7,
|
||||
): StructTypeDeclaration {
|
||||
const gd = genericDeclaration.toAST();
|
||||
return {
|
||||
moduleItem: "StructTypeDeclaration",
|
||||
typeDeclaration: "StructTypeDeclaration",
|
||||
name: identifier.toAST(),
|
||||
generic: gd.length ? gd[0] : { parameters: [] },
|
||||
fields: fields.asIteration().children.map((c) => c.toAST()),
|
||||
};
|
||||
},
|
||||
TraitMethod(declaration, _2): FunctionDeclaration {
|
||||
return declaration.toAST();
|
||||
},
|
||||
TraitTypeDeclaration(_1, identifier, _3, _4, methods, _5): TraitTypeDeclaration {
|
||||
TraitTypeDeclaration(
|
||||
_1,
|
||||
identifier,
|
||||
genericDeclaration,
|
||||
_4,
|
||||
_5,
|
||||
methods,
|
||||
_7,
|
||||
): TraitTypeDeclaration {
|
||||
const gd = genericDeclaration.toAST();
|
||||
return {
|
||||
moduleItem: "TraitTypeDeclaration",
|
||||
typeDeclaration: "TraitTypeDeclaration",
|
||||
name: identifier.toAST(),
|
||||
generic: gd.length ? gd[0] : { parameters: [] },
|
||||
functions: methods.asIteration().children.map((c) => c.toAST()),
|
||||
};
|
||||
},
|
||||
TypeDeclaration(declaration): TypeDeclaration {
|
||||
return declaration.toAST();
|
||||
},
|
||||
Impl(_1, trait, _3, struct, _4, methods, _5): Impl {
|
||||
Impl(_1, genericDeclaration, trait, _4, struct, _6, methods, _8): Impl {
|
||||
const tr = trait.toAST();
|
||||
const gd = genericDeclaration.toAST();
|
||||
return {
|
||||
moduleItem: "Impl",
|
||||
generic: gd.length ? gd[0] : { parameters: [] },
|
||||
struct: struct.toAST(),
|
||||
trait: tr.length > 0 ? tr[0] : null,
|
||||
functions: methods.asIteration().children.map((c) => c.toAST()),
|
||||
|
||||
Reference in New Issue
Block a user