Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #158 - handle identifier list containing an indexing expression #159

Merged
11 commits merged into from
Aug 1, 2017
41 changes: 28 additions & 13 deletions src/dparse/ast.d
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public:
/** */ void visit(const FunctionLiteralExpression functionLiteralExpression) { functionLiteralExpression.accept(this); }
/** */ void visit(const GotoStatement gotoStatement) { gotoStatement.accept(this); }
/** */ void visit(const IdentifierChain identifierChain) { identifierChain.accept(this); }
/** */ void visit(const IdentifierList identifierList) { identifierList.accept(this); }
/** */ void visit(const DeclaratorIdentifierList identifierList) { identifierList.accept(this); }
/** */ void visit(const IdentifierOrTemplateChain identifierOrTemplateChain) { identifierOrTemplateChain.accept(this); }
/** */ void visit(const IdentifierOrTemplateInstance identifierOrTemplateInstance) { identifierOrTemplateInstance.accept(this); }
/** */ void visit(const IdentityExpression identityExpression) { identityExpression.accept(this); }
Expand Down Expand Up @@ -322,6 +322,7 @@ public:
/** */ void visit(const TraitsExpression traitsExpression) { traitsExpression.accept(this); }
/** */ void visit(const TryStatement tryStatement) { tryStatement.accept(this); }
/** */ void visit(const Type type) { type.accept(this); }
/** */ void visit(const TypeIdentifierChain typeIdentChain) { typeIdentChain.accept(this); }
/** */ void visit(const Type2 type2) { type2.accept(this); }
/** */ void visit(const TypeSpecialization typeSpecialization) { typeSpecialization.accept(this); }
/** */ void visit(const TypeSuffix typeSuffix) { typeSuffix.accept(this); }
Expand Down Expand Up @@ -442,13 +443,13 @@ final class AliasDeclaration : ASTNode
public:
override void accept(ASTVisitor visitor) const
{
mixin (visitIfNotNull!(storageClasses, type, identifierList, initializers,
parameters, memberFunctionAttributes));
mixin (visitIfNotNull!(storageClasses, type, declaratorIdentifierList,
initializers, parameters, memberFunctionAttributes));
}
mixin OpEquals;
/** */ StorageClass[] storageClasses;
/** */ Type type;
/** */ IdentifierList identifierList;
/** */ DeclaratorIdentifierList declaratorIdentifierList;
/** */ AliasInitializer[] initializers;
/** */ string comment;
/** */ Parameters parameters;
Expand Down Expand Up @@ -1401,6 +1402,18 @@ public:
mixin OpEquals;
}

///
final class DeclaratorIdentifierList : ASTNode
{
public:
override void accept(ASTVisitor visitor) const
{
mixin (visitIfNotNull!(identifiers));
}
/** */ Token[] identifiers;
mixin OpEquals;
}

///
final class DefaultStatement : ASTNode
{
Expand Down Expand Up @@ -1793,14 +1806,18 @@ public:
}

///
final class IdentifierList : ASTNode
final class TypeIdentifierChain : ASTNode
{
public:
override void accept(ASTVisitor visitor) const
{
mixin (visitIfNotNull!(identifiers));
mixin (visitIfNotNull!(identifierOrTemplateInstance, typeIdentifierChain,
indexer));
}
/** */ Token[] identifiers;
/** */ bool dot;
/** */ IdentifierOrTemplateInstance identifierOrTemplateInstance;
/** */ TypeIdentifierChain typeIdentifierChain ;
/** */ ExpressionNode indexer;
mixin OpEquals;
}

Expand Down Expand Up @@ -2101,11 +2118,11 @@ final class LinkageAttribute : ASTNode
public:
override void accept(ASTVisitor visitor) const
{
mixin (visitIfNotNull!(identifier, identifierChain));
mixin (visitIfNotNull!(identifier, typeIdentifierChain));
}
/** */ Token identifier;
/** */ bool hasPlusPlus;
/** */ IdentifierChain identifierChain;
/** */ TypeIdentifierChain typeIdentifierChain;
/** */ IdType classOrStruct;
mixin OpEquals;
}
Expand Down Expand Up @@ -3135,15 +3152,13 @@ final class Type2 : ASTNode
public:
override void accept(ASTVisitor visitor) const
{
mixin (visitIfNotNull!(symbol, typeofExpression,
identifierOrTemplateChain, type, vector));
mixin (visitIfNotNull!(typeofExpression, typeIdentifierChain, type, vector));
}

/** */ IdType builtinType;
/** */ alias superOrThis = builtinType;
/** */ Symbol symbol;
/** */ TypeofExpression typeofExpression;
/** */ IdentifierOrTemplateChain identifierOrTemplateChain;
/** */ TypeIdentifierChain typeIdentifierChain;
/** */ IdType typeConstructor;
/** */ Type type;
/** */ Vector vector;
Expand Down
98 changes: 83 additions & 15 deletions src/dparse/formatter.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import std.typetuple:TypeTuple;

import dparse.ast;
import dparse.lexer;
version (unittest)
{
import dparse.parser;
import dparse.rollback_allocator;
import std.array : Appender;
import std.algorithm : canFind;
}

//debug = verbose;

Expand Down Expand Up @@ -121,7 +128,10 @@ class Formatter(Sink)
format(type);
space();
}
format(identifierList);
if (declaratorIdentifierList)
{
format(declaratorIdentifierList);
}
}
put(";");
}
Expand Down Expand Up @@ -1551,16 +1561,43 @@ class Formatter(Sink)
}
}

void format(const IdentifierList identifierList)
void format(const DeclaratorIdentifierList declaratorIdentifierList)
{
debug(verbose) writeln("IdentifierList");
foreach(count, ident; identifierList.identifiers)
debug(verbose) writeln("DeclaratorIdentifierList");

foreach(count, ident; declaratorIdentifierList.identifiers)
{
if (count) put(", ");
if (count)
put(", ");
put(ident.text);
}
}

void format(const TypeIdentifierChain typeIdentifierChain)
{
debug(verbose) writeln("TypeIdentifierChain");

if (typeIdentifierChain.dot)
{
put(".");
}
if (typeIdentifierChain.identifierOrTemplateInstance)
{
format(typeIdentifierChain.identifierOrTemplateInstance);
}
if (typeIdentifierChain.indexer)
{
put("[");
format(typeIdentifierChain.indexer);
put("]");
}
if (typeIdentifierChain.typeIdentifierChain)
{
put(".");
format(typeIdentifierChain.typeIdentifierChain);
}
}

void format(const IdentifierOrTemplateChain identifierOrTemplateChain)
{
debug(verbose) writeln("IdentifierOrTemplateChain");
Expand Down Expand Up @@ -1970,10 +2007,10 @@ class Formatter(Sink)
if (linkageAttribute.hasPlusPlus)
{
put("++");
if (linkageAttribute.identifierChain && linkageAttribute.identifierChain.identifiers.length > 0)
if (linkageAttribute.typeIdentifierChain)
{
put(", ");
format(linkageAttribute.identifierChain);
format(linkageAttribute.typeIdentifierChain);
}
else if (linkageAttribute.classOrStruct == tok!"class")
put(", class");
Expand Down Expand Up @@ -3187,24 +3224,23 @@ class Formatter(Sink)

/**
IdType builtinType;
Symbol symbol;
TypeofExpression typeofExpression;
IdentifierOrTemplateChain identifierOrTemplateChain;
IdentifierList identifierList;
IdType typeConstructor;
Type type;
**/

if (type2.symbol !is null)
if (type2.typeIdentifierChain !is null)
{
format(type2.symbol);
format(type2.typeIdentifierChain);
}
else if (type2.typeofExpression !is null)
{
format(type2.typeofExpression);
if (type2.identifierOrTemplateChain)
if (type2.typeIdentifierChain)
{
put(".");
format(type2.identifierOrTemplateChain);
format(type2.typeIdentifierChain);
}
return;
}
Expand All @@ -3218,10 +3254,10 @@ class Formatter(Sink)
else
{
put(tokenRep(type2.builtinType));
if (type2.identifierOrTemplateChain !is null)
if (type2.typeIdentifierChain)
{
put(".");
format(type2.identifierOrTemplateChain);
format(type2.typeIdentifierChain);
}
}
}
Expand Down Expand Up @@ -3867,3 +3903,35 @@ protected:
"postblit"
];
}

version (unittest)
void testFormatNode(Node)(string sourceCode)
Copy link
Author

@ghost ghost Jul 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use this new function to fix #135 and also problems in harbored-mod (which uses dparse formatter apparently...)

{
Appender!string fmt;
ubyte[] code = cast(ubyte[]) sourceCode;

class CatchInterestingStuff : ASTVisitor
{
alias visit = ASTVisitor.visit;
override void visit(const(Node) stuff)
{
stuff.accept(this);
format(&fmt, stuff);
assert(fmt.data.canFind(code), fmt.data);
writeln(fmt.data);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotta remove this writeln.

}
}

LexerConfig config;
StringCache cache = StringCache(32);
RollbackAllocator rba;
auto toks = getTokensForParser(code, config, &cache);
Module mod = parseModule(toks, "stdin", &rba);
(new CatchInterestingStuff).visit(mod);
}

unittest
{
testFormatNode!(VariableDeclaration)("T[0].Y y;");
testFormatNode!(VariableDeclaration)(`.T!"oof" toof;`);
}
Loading