feat: make it compile with dmd AST

Signed-off-by: Prajwal S N <prajwalnadig21@gmail.com>
This commit is contained in:
Prajwal S N 2023-09-27 23:27:16 +05:30
parent 806e7589c9
commit 6c1c543194
No known key found for this signature in database
GPG Key ID: 60701A603988FAC2
2 changed files with 2451 additions and 2432 deletions

View File

@ -5,8 +5,8 @@
module dfmt.ast_info; module dfmt.ast_info;
import dparse.lexer; import dmd.transitivevisitor;
import dparse.ast; import dmd.tokens;
enum BraceIndentInfoFlags enum BraceIndentInfoFlags
{ {
@ -57,10 +57,8 @@ struct ASTInformation
sort(constructorDestructorLocations); sort(constructorDestructorLocations);
sort(staticConstructorDestructorLocations); sort(staticConstructorDestructorLocations);
sort(sharedStaticConstructorDestructorLocations); sort(sharedStaticConstructorDestructorLocations);
sort!((a,b) => a.endLocation < b.endLocation) sort!((a, b) => a.endLocation < b.endLocation)(indentInfoSortedByEndLocation);
(indentInfoSortedByEndLocation); sort!((a, b) => a.endLocation < b.endLocation)(structInfoSortedByEndLocation);
sort!((a,b) => a.endLocation < b.endLocation)
(structInfoSortedByEndLocation);
sort(ufcsHintLocations); sort(ufcsHintLocations);
ufcsHintLocations = ufcsHintLocations.uniq().array(); ufcsHintLocations = ufcsHintLocations.uniq().array();
sort(ternaryColonLocations); sort(ternaryColonLocations);
@ -146,9 +144,9 @@ struct ASTInformation
} }
/// Collects information from the AST that is useful for the formatter /// Collects information from the AST that is useful for the formatter
final class FormatVisitor : ASTVisitor extern (C++) class FormatVisitor(AST) : ParseTimeTransitiveVisitor!AST
{ {
alias visit = ASTVisitor.visit; alias visit = ParseTimeTransitiveVisitor!AST.visit;
/** /**
* Params: * Params:
@ -159,292 +157,298 @@ final class FormatVisitor : ASTVisitor
this.astInformation = astInformation; this.astInformation = astInformation;
} }
override void visit(const ArrayInitializer arrayInitializer) /* override void visit(const ArrayInitializer arrayInitializer) */
{ /* { */
astInformation.arrayStartLocations ~= arrayInitializer.startLocation; /* astInformation.arrayStartLocations ~= arrayInitializer.startLocation; */
arrayInitializer.accept(this); /* arrayInitializer.accept(this); */
} /* } */
override void visit(const ArrayLiteral arrayLiteral) /* override void visit(const ArrayLiteral arrayLiteral) */
{ /* { */
astInformation.arrayStartLocations ~= arrayLiteral.tokens[0].index; /* astInformation.arrayStartLocations ~= arrayLiteral.tokens[0].index; */
arrayLiteral.accept(this); /* arrayLiteral.accept(this); */
} /* } */
override void visit(const AssocArrayLiteral assocArrayLiteral) /* override void visit(const AssocArrayLiteral assocArrayLiteral) */
{ /* { */
astInformation.arrayStartLocations ~= assocArrayLiteral.tokens[0].index; /* astInformation.arrayStartLocations ~= assocArrayLiteral.tokens[0].index; */
astInformation.assocArrayStartLocations ~= assocArrayLiteral.tokens[0].index; /* astInformation.assocArrayStartLocations ~= assocArrayLiteral.tokens[0].index; */
assocArrayLiteral.accept(this); /* assocArrayLiteral.accept(this); */
} /* } */
override void visit (const SharedStaticConstructor sharedStaticConstructor) /* override void visit(const SharedStaticConstructor sharedStaticConstructor) */
{ /* { */
astInformation.sharedStaticConstructorDestructorLocations ~= sharedStaticConstructor.location; /* astInformation.sharedStaticConstructorDestructorLocations ~= sharedStaticConstructor */
sharedStaticConstructor.accept(this); /* .location; */
} /* sharedStaticConstructor.accept(this); */
/* } */
override void visit (const SharedStaticDestructor sharedStaticDestructor) /* override void visit(const SharedStaticDestructor sharedStaticDestructor) */
{ /* { */
astInformation.sharedStaticConstructorDestructorLocations ~= sharedStaticDestructor.location; /* astInformation.sharedStaticConstructorDestructorLocations ~= sharedStaticDestructor */
sharedStaticDestructor.accept(this); /* .location; */
} /* sharedStaticDestructor.accept(this); */
/* } */
override void visit (const StaticConstructor staticConstructor) /* override void visit(const StaticConstructor staticConstructor) */
{ /* { */
astInformation.staticConstructorDestructorLocations ~= staticConstructor.location; /* astInformation.staticConstructorDestructorLocations ~= staticConstructor.location; */
staticConstructor.accept(this); /* staticConstructor.accept(this); */
} /* } */
override void visit (const StaticDestructor staticDestructor) /* override void visit(const StaticDestructor staticDestructor) */
{ /* { */
astInformation.staticConstructorDestructorLocations ~= staticDestructor.location; /* astInformation.staticConstructorDestructorLocations ~= staticDestructor.location; */
staticDestructor.accept(this); /* staticDestructor.accept(this); */
} /* } */
override void visit (const Constructor constructor) /* override void visit(const Constructor constructor) */
{ /* { */
astInformation.constructorDestructorLocations ~= constructor.location; /* astInformation.constructorDestructorLocations ~= constructor.location; */
constructor.accept(this); /* constructor.accept(this); */
} /* } */
override void visit (const Destructor destructor) /* override void visit(const Destructor destructor) */
{ /* { */
astInformation.constructorDestructorLocations ~= destructor.index; /* astInformation.constructorDestructorLocations ~= destructor.index; */
destructor.accept(this); /* destructor.accept(this); */
} /* } */
override void visit (const FunctionBody functionBody) /* override void visit(const FunctionBody functionBody) */
{ /* { */
if (auto bd = functionBody.specifiedFunctionBody) /* if (auto bd = functionBody.specifiedFunctionBody) */
{ /* { */
if (bd.blockStatement) /* if (bd.blockStatement) */
{ /* { */
astInformation.funBodyLocations ~= bd.blockStatement.startLocation; /* astInformation.funBodyLocations ~= bd.blockStatement.startLocation; */
} /* } */
} /* } */
functionBody.accept(this); /* functionBody.accept(this); */
} /* } */
override void visit(const ConditionalDeclaration dec) /* override void visit(const ConditionalDeclaration dec) */
{ /* { */
if (dec.hasElse) /* if (dec.hasElse) */
{ /* { */
auto condition = dec.compileCondition; /* auto condition = dec.compileCondition; */
if (condition.versionCondition !is null) /* if (condition.versionCondition !is null) */
{ /* { */
astInformation.conditionalWithElseLocations /* astInformation.conditionalWithElseLocations */
~= condition.versionCondition.versionIndex; /* ~= condition.versionCondition.versionIndex; */
} /* } */
else if (condition.debugCondition !is null) /* else if (condition.debugCondition !is null) */
{ /* { */
astInformation.conditionalWithElseLocations ~= condition.debugCondition.debugIndex; /* astInformation.conditionalWithElseLocations ~= condition.debugCondition.debugIndex; */
} /* } */
// Skip "static if" because the formatting for normal "if" handles /* // Skip "static if" because the formatting for normal "if" handles */
// it properly /* // it properly */
} /* } */
dec.accept(this); /* dec.accept(this); */
} /* } */
override void visit(const Constraint constraint) /* override void visit(const Constraint constraint) */
{ /* { */
astInformation.constraintLocations ~= constraint.location; /* astInformation.constraintLocations ~= constraint.location; */
constraint.accept(this); /* constraint.accept(this); */
} /* } */
override void visit(const ConditionalStatement statement) /* override void visit(const ConditionalStatement statement) */
{ /* { */
auto condition = statement.compileCondition; /* auto condition = statement.compileCondition; */
if (condition.versionCondition !is null) /* if (condition.versionCondition !is null) */
{ /* { */
astInformation.conditionalStatementLocations ~= condition.versionCondition.versionIndex; /* astInformation.conditionalStatementLocations ~= condition.versionCondition.versionIndex; */
} /* } */
else if (condition.debugCondition !is null) /* else if (condition.debugCondition !is null) */
{ /* { */
astInformation.conditionalStatementLocations ~= condition.debugCondition.debugIndex; /* astInformation.conditionalStatementLocations ~= condition.debugCondition.debugIndex; */
} /* } */
statement.accept(this); /* statement.accept(this); */
} /* } */
override void visit(const FunctionLiteralExpression funcLit) /* override void visit(const FunctionLiteralExpression funcLit) */
{ /* { */
if (funcLit.specifiedFunctionBody !is null) /* if (funcLit.specifiedFunctionBody !is null) */
{ /* { */
const bs = funcLit.specifiedFunctionBody.blockStatement; /* const bs = funcLit.specifiedFunctionBody.blockStatement; */
astInformation.funLitStartLocations ~= bs.startLocation; /* astInformation.funLitStartLocations ~= bs.startLocation; */
astInformation.funLitEndLocations ~= bs.endLocation; /* astInformation.funLitEndLocations ~= bs.endLocation; */
astInformation.indentInfoSortedByEndLocation ~= /* astInformation.indentInfoSortedByEndLocation ~= */
BraceIndentInfo(bs.startLocation, bs.endLocation); /* BraceIndentInfo(bs.startLocation, bs.endLocation); */
} /* } */
funcLit.accept(this); /* funcLit.accept(this); */
} /* } */
override void visit(const DefaultStatement defaultStatement) /* override void visit(const DefaultStatement defaultStatement) */
{ /* { */
astInformation.caseEndLocations ~= defaultStatement.colonLocation; /* astInformation.caseEndLocations ~= defaultStatement.colonLocation; */
defaultStatement.accept(this); /* defaultStatement.accept(this); */
} /* } */
override void visit(const CaseStatement caseStatement) /* override void visit(const CaseStatement caseStatement) */
{ /* { */
astInformation.caseEndLocations ~= caseStatement.colonLocation; /* astInformation.caseEndLocations ~= caseStatement.colonLocation; */
caseStatement.accept(this); /* caseStatement.accept(this); */
} /* } */
override void visit(const CaseRangeStatement caseRangeStatement) /* override void visit(const CaseRangeStatement caseRangeStatement) */
{ /* { */
astInformation.caseEndLocations ~= caseRangeStatement.colonLocation; /* astInformation.caseEndLocations ~= caseRangeStatement.colonLocation; */
caseRangeStatement.accept(this); /* caseRangeStatement.accept(this); */
} /* } */
override void visit(const SpecifiedFunctionBody specifiedFunctionBody) /* override void visit(const SpecifiedFunctionBody specifiedFunctionBody) */
{ /* { */
if (specifiedFunctionBody.blockStatement !is null) /* if (specifiedFunctionBody.blockStatement !is null) */
astInformation.doubleNewlineLocations ~= specifiedFunctionBody.blockStatement.endLocation; /* astInformation.doubleNewlineLocations ~= specifiedFunctionBody */
specifiedFunctionBody.accept(this); /* .blockStatement.endLocation; */
} /* specifiedFunctionBody.accept(this); */
/* } */
override void visit(const StructInitializer structInitializer) /* override void visit(const StructInitializer structInitializer) */
{ /* { */
astInformation.structInitStartLocations ~= structInitializer.startLocation; /* astInformation.structInitStartLocations ~= structInitializer.startLocation; */
astInformation.structInitEndLocations ~= structInitializer.endLocation; /* astInformation.structInitEndLocations ~= structInitializer.endLocation; */
astInformation.structInfoSortedByEndLocation ~= /* astInformation.structInfoSortedByEndLocation ~= */
StructInitializerInfo(structInitializer.startLocation, structInitializer.endLocation); /* StructInitializerInfo( */
astInformation.indentInfoSortedByEndLocation ~= /* structInitializer.startLocation, structInitializer.endLocation); */
BraceIndentInfo(structInitializer.startLocation, structInitializer.endLocation); /* astInformation.indentInfoSortedByEndLocation ~= */
/* BraceIndentInfo( */
/* structInitializer.startLocation, structInitializer.endLocation); */
structInitializer.accept(this); /* structInitializer.accept(this); */
} /* } */
override void visit(const EnumBody enumBody) /* override void visit(const EnumBody enumBody) */
{ /* { */
astInformation.doubleNewlineLocations ~= enumBody.endLocation; /* astInformation.doubleNewlineLocations ~= enumBody.endLocation; */
enumBody.accept(this); /* enumBody.accept(this); */
} /* } */
override void visit(const Unittest unittest_) /* override void visit(const Unittest unittest_) */
{ /* { */
astInformation.doubleNewlineLocations ~= unittest_.blockStatement.endLocation; /* astInformation.doubleNewlineLocations ~= unittest_.blockStatement.endLocation; */
unittest_.accept(this); /* unittest_.accept(this); */
} /* } */
override void visit(const Invariant invariant_) /* override void visit(const Invariant invariant_) */
{ /* { */
if (invariant_.blockStatement !is null) /* if (invariant_.blockStatement !is null) */
astInformation.doubleNewlineLocations ~= invariant_.blockStatement.endLocation; /* astInformation.doubleNewlineLocations ~= invariant_.blockStatement.endLocation; */
invariant_.accept(this);
}
override void visit(const StructBody structBody) /* invariant_.accept(this); */
{ /* } */
astInformation.aggregateBodyLocations ~= structBody.startLocation;
astInformation.doubleNewlineLocations ~= structBody.endLocation;
structBody.accept(this);
}
override void visit(const TemplateDeclaration templateDeclaration) /* override void visit(const StructBody structBody) */
{ /* { */
astInformation.doubleNewlineLocations ~= templateDeclaration.endLocation; /* astInformation.aggregateBodyLocations ~= structBody.startLocation; */
templateDeclaration.accept(this); /* astInformation.doubleNewlineLocations ~= structBody.endLocation; */
} /* structBody.accept(this); */
/* } */
override void visit(const TypeSuffix typeSuffix) /* override void visit(const TemplateDeclaration templateDeclaration) */
{ /* { */
if (typeSuffix.star.type != tok!"") /* astInformation.doubleNewlineLocations ~= templateDeclaration.endLocation; */
astInformation.spaceAfterLocations ~= typeSuffix.star.index; /* templateDeclaration.accept(this); */
typeSuffix.accept(this); /* } */
}
override void visit(const UnaryExpression unary) /* override void visit(const TypeSuffix typeSuffix) */
{ /* { */
import std.typecons : rebindable; /* if (typeSuffix.star.type != TOK.error) */
/* astInformation.spaceAfterLocations ~= typeSuffix.star.index; */
/* typeSuffix.accept(this); */
/* } */
int chainLength; /* override void visit(const UnaryExpression unary) */
auto u = rebindable(unary); /* { */
while (u !is null) /* import std.typecons : rebindable; */
{
if (u.identifierOrTemplateInstance !is null
&& u.identifierOrTemplateInstance.templateInstance !is null)
chainLength++;
u = u.unaryExpression;
}
if (chainLength > 1)
{
u = unary;
while (u.unaryExpression !is null)
{
astInformation.ufcsHintLocations ~= u.dotLocation;
u = u.unaryExpression;
}
}
if (unary.prefix.type == tok!"~" || unary.prefix.type == tok!"&"
|| unary.prefix.type == tok!"*"
|| unary.prefix.type == tok!"+" || unary.prefix.type == tok!"-")
{
astInformation.unaryLocations ~= unary.prefix.index;
}
unary.accept(this);
}
override void visit(const AttributeDeclaration attributeDeclaration) /* int chainLength; */
{ /* auto u = rebindable(unary); */
astInformation.attributeDeclarationLines ~= attributeDeclaration.line; /* while (u !is null) */
attributeDeclaration.accept(this); /* { */
} /* if (u.identifierOrTemplateInstance !is null */
/* && u.identifierOrTemplateInstance.templateInstance !is null) */
/* chainLength++; */
/* u = u.unaryExpression; */
/* } */
/* if (chainLength > 1) */
/* { */
/* u = unary; */
/* while (u.unaryExpression !is null) */
/* { */
/* astInformation.ufcsHintLocations ~= u.dotLocation; */
/* u = u.unaryExpression; */
/* } */
/* } */
/* if (unary.prefix.type == TOK.tilde || unary.prefix.type == TOK.and */
/* || unary.prefix.type == TOK.mul */
/* || unary.prefix.type == TOK.add || unary.prefix.type == TOK.min) */
/* { */
/* astInformation.unaryLocations ~= unary.prefix.index; */
/* } */
/* unary.accept(this); */
/* } */
override void visit(const FunctionAttribute functionAttribute) /* override void visit(const AttributeDeclaration attributeDeclaration) */
{ /* { */
if (functionAttribute.atAttribute !is null) /* astInformation.attributeDeclarationLines ~= attributeDeclaration.line; */
astInformation.atAttributeStartLocations ~= functionAttribute.atAttribute.startLocation; /* attributeDeclaration.accept(this); */
functionAttribute.accept(this); /* } */
}
override void visit(const MemberFunctionAttribute memberFunctionAttribute) /* override void visit(const FunctionAttribute functionAttribute) */
{ /* { */
if (memberFunctionAttribute.atAttribute !is null) /* if (functionAttribute.atAttribute !is null) */
astInformation.atAttributeStartLocations ~= memberFunctionAttribute.atAttribute.startLocation; /* astInformation.atAttributeStartLocations ~= functionAttribute.atAttribute.startLocation; */
memberFunctionAttribute.accept(this); /* functionAttribute.accept(this); */
} /* } */
override void visit(const Attribute attribute) /* override void visit(const MemberFunctionAttribute memberFunctionAttribute) */
{ /* { */
if (attribute.atAttribute !is null) /* if (memberFunctionAttribute.atAttribute !is null) */
astInformation.atAttributeStartLocations ~= attribute.atAttribute.startLocation; /* astInformation.atAttributeStartLocations ~= memberFunctionAttribute */
attribute.accept(this); /* .atAttribute.startLocation; */
} /* memberFunctionAttribute.accept(this); */
/* } */
override void visit(const StorageClass storageClass) /* override void visit(const Attribute attribute) */
{ /* { */
if (storageClass.atAttribute !is null) /* if (attribute.atAttribute !is null) */
astInformation.atAttributeStartLocations ~= storageClass.atAttribute.startLocation; /* astInformation.atAttributeStartLocations ~= attribute.atAttribute.startLocation; */
storageClass.accept(this); /* attribute.accept(this); */
} /* } */
override void visit(const InContractExpression inContractExpression) /* override void visit(const StorageClass storageClass) */
{ /* { */
astInformation.contractLocations ~= inContractExpression.inTokenLocation; /* if (storageClass.atAttribute !is null) */
inContractExpression.accept(this); /* astInformation.atAttributeStartLocations ~= storageClass.atAttribute.startLocation; */
} /* storageClass.accept(this); */
/* } */
override void visit(const InStatement inStatement) /* override void visit(const InContractExpression inContractExpression) */
{ /* { */
astInformation.contractLocations ~= inStatement.inTokenLocation; /* astInformation.contractLocations ~= inContractExpression.inTokenLocation; */
inStatement.accept(this); /* inContractExpression.accept(this); */
} /* } */
override void visit(const OutContractExpression outContractExpression) /* override void visit(const InStatement inStatement) */
{ /* { */
astInformation.contractLocations ~= outContractExpression.outTokenLocation; /* astInformation.contractLocations ~= inStatement.inTokenLocation; */
outContractExpression.accept(this); /* inStatement.accept(this); */
} /* } */
override void visit(const OutStatement outStatement) /* override void visit(const OutContractExpression outContractExpression) */
{ /* { */
astInformation.contractLocations ~= outStatement.outTokenLocation; /* astInformation.contractLocations ~= outContractExpression.outTokenLocation; */
outStatement.accept(this); /* outContractExpression.accept(this); */
} /* } */
/* override void visit(const OutStatement outStatement) */
/* { */
/* astInformation.contractLocations ~= outStatement.outTokenLocation; */
/* outStatement.accept(this); */
/* } */
override void visit(const TernaryExpression ternaryExpression) override void visit(const TernaryExpression ternaryExpression)
{ {

File diff suppressed because it is too large Load Diff