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

File diff suppressed because it is too large Load Diff