fix parsing
This commit is contained in:
@@ -22,6 +22,7 @@ import {
|
||||
NamedTypeUsage,
|
||||
newNever,
|
||||
Operation,
|
||||
Path,
|
||||
ReturnStatement,
|
||||
Statement,
|
||||
StructField,
|
||||
@@ -31,7 +32,6 @@ import {
|
||||
TraitTypeDeclaration,
|
||||
TypeDeclaration,
|
||||
TypeUsage,
|
||||
VariableUsage,
|
||||
} from "./ast";
|
||||
import { boringGrammar } from "./grammar";
|
||||
|
||||
@@ -44,44 +44,60 @@ function nextUnknown() {
|
||||
|
||||
export const semantics = boringGrammar.createSemantics();
|
||||
semantics.addOperation<any>("toAST", {
|
||||
LiteralInt(a): LiteralInt {
|
||||
LiteralInt(a): Expression {
|
||||
return {
|
||||
expressionType: "LiteralInt",
|
||||
value: this.sourceString,
|
||||
type: {
|
||||
typeUsage: "NamedTypeUsage",
|
||||
name: { text: "i64", spanStart: 0, spanEnd: 0 },
|
||||
statementType: "Expression",
|
||||
subExpression: {
|
||||
expressionType: "LiteralInt",
|
||||
value: this.sourceString,
|
||||
type: {
|
||||
typeUsage: "NamedTypeUsage",
|
||||
name: { text: "i64", spanStart: 0, spanEnd: 0 },
|
||||
},
|
||||
},
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
};
|
||||
},
|
||||
LiteralFloat(_1, _2, _3): LiteralFloat {
|
||||
LiteralFloat(_1, _2, _3): Expression {
|
||||
return {
|
||||
expressionType: "LiteralFloat",
|
||||
value: this.sourceString,
|
||||
type: {
|
||||
typeUsage: "NamedTypeUsage",
|
||||
name: { text: "f64", spanStart: 0, spanEnd: 0 },
|
||||
statementType: "Expression",
|
||||
subExpression: {
|
||||
expressionType: "LiteralFloat",
|
||||
value: this.sourceString,
|
||||
type: {
|
||||
typeUsage: "NamedTypeUsage",
|
||||
name: { text: "f64", spanStart: 0, spanEnd: 0 },
|
||||
},
|
||||
},
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
};
|
||||
},
|
||||
LiteralBool(_): LiteralBool {
|
||||
LiteralBool(_): Expression {
|
||||
return {
|
||||
expressionType: "LiteralBool",
|
||||
value: this.sourceString,
|
||||
type: {
|
||||
typeUsage: "NamedTypeUsage",
|
||||
name: { text: "bool", spanStart: 0, spanEnd: 0 },
|
||||
statementType: "Expression",
|
||||
subExpression: {
|
||||
expressionType: "LiteralBool",
|
||||
value: this.sourceString,
|
||||
type: {
|
||||
typeUsage: "NamedTypeUsage",
|
||||
name: { text: "bool", spanStart: 0, spanEnd: 0 },
|
||||
},
|
||||
},
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
};
|
||||
},
|
||||
LiteralString(_1, text, _3): LiteralString {
|
||||
LiteralString(_1, text, _3): Expression {
|
||||
return {
|
||||
expressionType: "LiteralString",
|
||||
value: text.sourceString,
|
||||
type: {
|
||||
typeUsage: "NamedTypeUsage",
|
||||
name: { text: "String", spanStart: 0, spanEnd: 0 },
|
||||
statementType: "Expression",
|
||||
subExpression: {
|
||||
expressionType: "LiteralString",
|
||||
value: text.sourceString,
|
||||
type: {
|
||||
typeUsage: "NamedTypeUsage",
|
||||
name: { text: "String", spanStart: 0, spanEnd: 0 },
|
||||
},
|
||||
},
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
};
|
||||
},
|
||||
LiteralStructField(identifier, _2, expression): StructField {
|
||||
@@ -90,12 +106,16 @@ semantics.addOperation<any>("toAST", {
|
||||
expression: expression.toAST(),
|
||||
};
|
||||
},
|
||||
LiteralStruct(identifier, _2, fields, _4): LiteralStruct {
|
||||
LiteralStruct(identifier, _2, fields, _4): Expression {
|
||||
return {
|
||||
expressionType: "LiteralStruct",
|
||||
name: identifier.toAST(),
|
||||
fields: fields.asIteration().children.map((c) => c.toAST()),
|
||||
type: { typeUsage: "NamedTypeUsage", name: identifier.toAST() },
|
||||
statementType: "Expression",
|
||||
subExpression: {
|
||||
expressionType: "LiteralStruct",
|
||||
name: identifier.toAST(),
|
||||
fields: fields.asIteration().children.map((c) => c.toAST()),
|
||||
type: { typeUsage: "NamedTypeUsage", name: identifier.toAST() },
|
||||
},
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
};
|
||||
},
|
||||
identifier(_1, _2): Identifier {
|
||||
@@ -105,95 +125,183 @@ semantics.addOperation<any>("toAST", {
|
||||
spanEnd: this.source.endIdx,
|
||||
};
|
||||
},
|
||||
FunctionCall(expression, _2, args, _4): FunctionCall {
|
||||
const resolvedArgs = args.asIteration().children.map((c) => c.toAST());
|
||||
return {
|
||||
expressionType: "FunctionCall",
|
||||
source: expression.toAST(),
|
||||
arguments: resolvedArgs,
|
||||
type: {
|
||||
typeUsage: "UnknownTypeUsage",
|
||||
name: nextUnknown(),
|
||||
},
|
||||
};
|
||||
PrimaryExpression(literal): Expression {
|
||||
return literal.toAST();
|
||||
},
|
||||
StructGetter(expression, _2, identifier): StructGetter {
|
||||
return {
|
||||
expressionType: "StructGetter",
|
||||
source: expression.toAST(),
|
||||
attribute: identifier.toAST(),
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
};
|
||||
},
|
||||
VariableUsage(identifier): VariableUsage {
|
||||
return {
|
||||
expressionType: "VariableUsage",
|
||||
name: identifier.toAST(),
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
};
|
||||
},
|
||||
IfExpression(_1, _2, expression, _4, block, _6, elseBlock): IfExpression {
|
||||
const eb = elseBlock.toAST();
|
||||
return {
|
||||
expressionType: "IfExpression",
|
||||
condition: expression.toAST(),
|
||||
block: block.toAST(),
|
||||
else: eb.length > 0 ? eb[0] : null,
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
};
|
||||
},
|
||||
Term(term): Expression {
|
||||
return term.toAST();
|
||||
},
|
||||
Term_parens(_1, term, _3): Expression {
|
||||
return term.toAST();
|
||||
},
|
||||
Factor(factor): Expression {
|
||||
return factor.toAST();
|
||||
},
|
||||
Expression(expression): Expression {
|
||||
PrimaryExpression_path(path): Expression {
|
||||
return {
|
||||
statementType: "Expression",
|
||||
subExpression: expression.toAST(),
|
||||
subExpression: path.toAST(),
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
};
|
||||
},
|
||||
Expression_plus(expression, _2, factor): Operation {
|
||||
PrimaryExpression_parens(_1, term, _3): Expression {
|
||||
return term.toAST();
|
||||
},
|
||||
StructExpression(expression): Expression {
|
||||
return expression.toAST();
|
||||
},
|
||||
MemberExpression(expression): Expression {
|
||||
return expression.toAST();
|
||||
},
|
||||
MemberExpression_structGetter(expression, _2, identifier): Expression {
|
||||
return {
|
||||
expressionType: "Operation",
|
||||
left: expression.toAST(),
|
||||
op: "+",
|
||||
right: factor.toAST(),
|
||||
statementType: "Expression",
|
||||
subExpression: {
|
||||
expressionType: "StructGetter",
|
||||
source: expression.toAST(),
|
||||
attribute: identifier.toAST(),
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
},
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
};
|
||||
},
|
||||
Expression_minus(expression, _2, factor): Operation {
|
||||
CallExpression(expression): Expression {
|
||||
return expression.toAST();
|
||||
},
|
||||
CallExpression_structGetter(expression, _2, identifier): Expression {
|
||||
return {
|
||||
expressionType: "Operation",
|
||||
left: expression.toAST(),
|
||||
op: "-",
|
||||
right: factor.toAST(),
|
||||
statementType: "Expression",
|
||||
subExpression: {
|
||||
expressionType: "StructGetter",
|
||||
source: expression.toAST(),
|
||||
attribute: identifier.toAST(),
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
},
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
};
|
||||
},
|
||||
Factor_mult(factor, _2, term): Operation {
|
||||
CallExpression_functionCall(expression, _2, args, _4): Expression {
|
||||
const resolvedArgs = args.asIteration().children.map((c) => c.toAST());
|
||||
return {
|
||||
expressionType: "Operation",
|
||||
left: factor.toAST(),
|
||||
op: "*",
|
||||
right: term.toAST(),
|
||||
statementType: "Expression",
|
||||
subExpression: {
|
||||
expressionType: "FunctionCall",
|
||||
source: expression.toAST(),
|
||||
arguments: resolvedArgs,
|
||||
type: {
|
||||
typeUsage: "UnknownTypeUsage",
|
||||
name: nextUnknown(),
|
||||
},
|
||||
},
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
};
|
||||
},
|
||||
Factor_div(factor, _2, term): Operation {
|
||||
CallExpression_memberFunctionCall(expression, _2, args, _4): Expression {
|
||||
const resolvedArgs = args.asIteration().children.map((c) => c.toAST());
|
||||
return {
|
||||
expressionType: "Operation",
|
||||
left: factor.toAST(),
|
||||
op: "/",
|
||||
right: term.toAST(),
|
||||
statementType: "Expression",
|
||||
subExpression: {
|
||||
expressionType: "FunctionCall",
|
||||
source: expression.toAST(),
|
||||
arguments: resolvedArgs,
|
||||
type: {
|
||||
typeUsage: "UnknownTypeUsage",
|
||||
name: nextUnknown(),
|
||||
},
|
||||
},
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
};
|
||||
},
|
||||
StructGetter(expression, _2, identifier): Expression {
|
||||
return {
|
||||
statementType: "Expression",
|
||||
subExpression: {
|
||||
expressionType: "StructGetter",
|
||||
source: expression.toAST(),
|
||||
attribute: identifier.toAST(),
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
},
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
};
|
||||
},
|
||||
IfExpression(_1, _2, expression, _4, block, _6, elseBlock): Expression {
|
||||
const eb = elseBlock.toAST();
|
||||
return {
|
||||
statementType: "Expression",
|
||||
subExpression: {
|
||||
expressionType: "IfExpression",
|
||||
condition: expression.toAST(),
|
||||
block: block.toAST(),
|
||||
else: eb.length > 0 ? eb[0] : null,
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
},
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
};
|
||||
},
|
||||
Path_base(identifier): Path {
|
||||
return {
|
||||
expressionType: "Path",
|
||||
value: { type: "Identifier", name: identifier.toAST() },
|
||||
};
|
||||
},
|
||||
Path_nested(basePath, _2, attrIdent): Path {
|
||||
return {
|
||||
expressionType: "Path",
|
||||
value: { type: "Nested", parent: basePath.toAST(), name: attrIdent.toAST() },
|
||||
};
|
||||
},
|
||||
MultExpression(expression): Expression {
|
||||
return expression.toAST();
|
||||
},
|
||||
MultExpression_mult(factor, _2, term): Expression {
|
||||
return {
|
||||
statementType: "Expression",
|
||||
subExpression: {
|
||||
expressionType: "Operation",
|
||||
left: factor.toAST(),
|
||||
op: "*",
|
||||
right: term.toAST(),
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
},
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
};
|
||||
},
|
||||
MultExpression_div(factor, _2, term): Expression {
|
||||
return {
|
||||
statementType: "Expression",
|
||||
subExpression: {
|
||||
expressionType: "Operation",
|
||||
left: factor.toAST(),
|
||||
op: "/",
|
||||
right: term.toAST(),
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
},
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
};
|
||||
},
|
||||
AddExpression(expression): Expression {
|
||||
return expression.toAST();
|
||||
},
|
||||
AddExpression_plus(expression, _2, factor): Expression {
|
||||
return {
|
||||
statementType: "Expression",
|
||||
subExpression: {
|
||||
expressionType: "Operation",
|
||||
left: expression.toAST(),
|
||||
op: "+",
|
||||
right: factor.toAST(),
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
},
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
};
|
||||
},
|
||||
AddExpression_minus(expression, _2, factor): Expression {
|
||||
return {
|
||||
statementType: "Expression",
|
||||
subExpression: {
|
||||
expressionType: "Operation",
|
||||
left: expression.toAST(),
|
||||
op: "-",
|
||||
right: factor.toAST(),
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
},
|
||||
type: { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
};
|
||||
},
|
||||
Expression(expression): Expression {
|
||||
return expression.toAST();
|
||||
},
|
||||
Statement(statement): Statement {
|
||||
return statement.toAST();
|
||||
},
|
||||
@@ -212,10 +320,17 @@ semantics.addOperation<any>("toAST", {
|
||||
type: tu.length > 0 ? tu[0] : { typeUsage: "UnknownTypeUsage", name: nextUnknown() },
|
||||
};
|
||||
},
|
||||
AssignmentStatement(variable, _2, expression, _4): AssignmentStatement {
|
||||
AssignmentStatement_identifier(variable, _2, expression, _4): AssignmentStatement {
|
||||
return {
|
||||
statementType: "AssignmentStatement",
|
||||
source: variable.toAST(),
|
||||
source: { type: "Identifier", name: variable.toAST() },
|
||||
expression: expression.toAST(),
|
||||
};
|
||||
},
|
||||
AssignmentStatement_getter(variable, _2, expression, _4): AssignmentStatement {
|
||||
return {
|
||||
statementType: "AssignmentStatement",
|
||||
source: { type: "StructGetter", source: variable.toAST() },
|
||||
expression: expression.toAST(),
|
||||
};
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user