Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions ddoc_preprocessor.d
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ All unknown options are passed to the compiler.
args = args[0..pos].chain("-".only, args[pos..$].dropOne).array;

// transform and extend the ddoc page
text = genGrammar(text);
text = genHeader(text);

// inject custom, "dynamic" macros
Expand Down Expand Up @@ -194,3 +195,61 @@ auto genHeader(string fileText)
return updateDdocTag(fileText, ddocKey, newContent);
}

// parse the menu from the Ddoc file
auto specTocEntries()
{
alias Entry = Tuple!(string, "name", string, "title", string, "fileName");
Entry[] entries;

static immutable specDir = __FILE_FULL_PATH__.dirName.buildNormalizedPath("spec");
static immutable mainFile = specDir.buildPath("./spec.ddoc");

auto specText = mainFile.readText;
if (!specText.findSkip("SUBMENU2"))
writeln("Menu file has an invalid format.");
foreach (line; specText.splitter("\n"))
{
enum ddocEntryStart = "$(ROOT_DIR)spec/";
if (line.find!(not!isWhite).startsWith(ddocEntryStart))
{
auto ps = line.splitter(ddocEntryStart).dropOne.front.splitter(",");
auto name = ps.front.stripExtension.withExtension(".dd").to!string;
auto fileName = specDir.buildPath(name);
auto title = ps.dropOne.front.idup.strip;
Copy link
Contributor Author

@wilzbach wilzbach Jan 28, 2018

Choose a reason for hiding this comment

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

I would love to have shift - front and popFront in one step.

entries ~= Entry(name, title, fileName);
}
}
return entries;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This function is copied from the footer generation. I plan to move the footer generation to this script too, see e.g. #2061 (but that isn't a top priority as the footer already exists and we don't add new pages frequently).


// Automatically generate spec/grammar
auto genGrammar(string fileText)
{
import std.uni : toLower;

enum ddocKey = "$(GRAMMAR_SUMMARY";
auto newContent = ddocKey ~ "\n";

if (fileText.canFind(ddocKey))
{
foreach (i, entry; specTocEntries)
{
if (entry.fileName.endsWith("grammar.dd", "lex.dd", "simd.dd"))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

  • sim.dd defines this:
$(GRAMMAR
$(I typeNN)
)
  • lex.dd is excluded in the current spec

image

continue;

enum grammarKey = "$(GRAMMAR";
auto text = entry.fileName.readText.find(grammarKey);
if (text.length)
newContent ~= "$(H2 $(LNAME2 %s, %s))\n".format(entry.title.toLower, entry.title);
for (; text.length; text = text[ grammarKey.length .. $].find(grammarKey))
{
newContent ~= grammarKey;
newContent ~= text.drop(grammarKey.length).untilClosingParentheses.to!string;
newContent ~= ")\n";
}
}
return updateDdocTag(fileText, ddocKey, newContent);
}
return fileText;
}

18 changes: 7 additions & 11 deletions spec/declaration.dd
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ $(GNAME AltDeclaratorSuffixes):

$(GNAME AltDeclaratorSuffix):
$(D [ ])
$(D [) $(VEXPRESSION) $(D ])
$(D [) $($(GLINK2 expression, AssignExpression)) $(D ])
$(D [) $(GLINK Type) $(D ])

$(GNAME AltFuncDeclaratorSuffix):
Expand Down Expand Up @@ -135,8 +135,8 @@ $(GNAME BasicType2):
$(GNAME BasicType2X):
$(D *)
$(D [ ])
$(D [) $(VEXPRESSION) $(D ])
$(D [) $(VEXPRESSION) .. $(VEXPRESSION) $(D ])
$(D [) $($(GLINK2 expression, AssignExpression)) $(D ])
$(D [) $($(GLINK2 expression, AssignExpression)) .. $($(GLINK2 expression, AssignExpression)) $(D ])
$(D [) $(GLINK Type) $(D ])
$(D delegate) $(GLINK2 function, Parameters) $(GLINK2 function, MemberFunctionAttributes)$(OPT)
$(D function) $(GLINK2 function, Parameters) $(GLINK2 function, FunctionAttributes)$(OPT)
Expand All @@ -146,7 +146,7 @@ $(GNAME IdentifierList):
$(I Identifier) $(D .) $(I IdentifierList)
$(GLINK2 template, TemplateInstance)
$(GLINK2 template, TemplateInstance) $(D .) $(I IdentifierList)
$(I Identifier) $(D [) $(ASSIGNEXPRESSION) $(D].) $(I IdentifierList)
$(I Identifier) $(D [) $($(GLINK2 expression, AssignExpression)) $(D].) $(I IdentifierList)
)

$(GRAMMAR
Expand Down Expand Up @@ -189,7 +189,7 @@ $(GNAME NonVoidInitializer):
$(GLINK StructInitializer)

$(GNAME ExpInitializer):
$(ASSIGNEXPRESSION)
$($(GLINK2 expression, AssignExpression))

$(GNAME ArrayInitializer):
$(D [) $(GLINK ArrayMemberInitializations)$(OPT) $(D ])
Expand All @@ -201,7 +201,7 @@ $(GNAME ArrayMemberInitializations):

$(GNAME ArrayMemberInitialization):
$(GLINK NonVoidInitializer)
$(ASSIGNEXPRESSION) $(D :) $(GLINK NonVoidInitializer)
$($(GLINK2 expression, AssignExpression)) $(D :) $(GLINK NonVoidInitializer)

$(GNAME StructInitializer):
$(D {) $(GLINK StructMemberInitializers)$(OPT) $(D })
Expand Down Expand Up @@ -501,7 +501,7 @@ $(H2 $(LNAME2 typeof, $(D typeof)))

$(GRAMMAR
$(GNAME Typeof):
$(D typeof $(LPAREN)) $(EXPRESSION) $(D $(RPAREN))
$(D typeof $(LPAREN)) $(GLINK2 expression, Expression) $(D $(RPAREN))
$(D typeof $(LPAREN)) $(D return) $(D $(RPAREN))
)

Expand Down Expand Up @@ -748,7 +748,3 @@ $(SPEC_SUBNAV_PREV_NEXT module, Modules, type, Types)
Macros:
CHAPTER=5
TITLE=Declarations
ASSIGNEXPRESSION=$(GLINK2 expression, AssignExpression)
EXPRESSION=$(GLINK2 expression, Expression)
VEXPRESSION=$(ASSIGNEXPRESSION)
_=
Loading