Skip to content

Commit f71f373

Browse files
committed
Automatically generate the grammar overview
1 parent e3a49b3 commit f71f373

File tree

2 files changed

+61
-1851
lines changed

2 files changed

+61
-1851
lines changed

ddoc_preprocessor.d

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ All unknown options are passed to the compiler.
4949
args = args[0..pos].chain("-".only, args[pos..$].dropOne).array;
5050

5151
// transform and extend the ddoc page
52+
text = genGrammar(text);
5253
text = genHeader(text);
5354

5455
// inject custom, "dynamic" macros
@@ -194,3 +195,61 @@ auto genHeader(string fileText)
194195
return updateDdocTag(fileText, ddocKey, newContent);
195196
}
196197

198+
// parse the menu from the Ddoc file
199+
auto specTocEntries()
200+
{
201+
alias Entry = Tuple!(string, "name", string, "title", string, "fileName");
202+
Entry[] entries;
203+
204+
static immutable specDir = __FILE_FULL_PATH__.dirName.buildNormalizedPath("spec");
205+
static immutable mainFile = specDir.buildPath("./spec.ddoc");
206+
207+
auto specText = mainFile.readText;
208+
if (!specText.findSkip("SUBMENU2"))
209+
writeln("Menu file has an invalid format.");
210+
foreach (line; specText.splitter("\n"))
211+
{
212+
enum ddocEntryStart = "$(ROOT_DIR)spec/";
213+
if (line.find!(not!isWhite).startsWith(ddocEntryStart))
214+
{
215+
auto ps = line.splitter(ddocEntryStart).dropOne.front.splitter(",");
216+
auto name = ps.front.stripExtension.withExtension(".dd").to!string;
217+
auto fileName = specDir.buildPath(name);
218+
auto title = ps.dropOne.front.idup.strip;
219+
entries ~= Entry(name, title, fileName);
220+
}
221+
}
222+
return entries;
223+
}
224+
225+
// Automatically generate spec/grammar
226+
auto genGrammar(string fileText)
227+
{
228+
import std.uni : toLower;
229+
230+
enum ddocKey = "$(GRAMMAR_SUMMARY";
231+
auto newContent = ddocKey ~ "\n";
232+
233+
if (fileText.canFind(ddocKey))
234+
{
235+
foreach (i, entry; specTocEntries)
236+
{
237+
if (entry.fileName.endsWith("grammar.dd", "lex.dd", "simd.dd"))
238+
continue;
239+
240+
enum grammarKey = "$(GRAMMAR";
241+
auto text = entry.fileName.readText.find(grammarKey);
242+
if (text.length)
243+
newContent ~= "$(H2 $(LNAME2 %s, %s))\n".format(entry.title.toLower, entry.title);
244+
for (; text.length; text = text[ grammarKey.length .. $].find(grammarKey))
245+
{
246+
newContent ~= grammarKey;
247+
newContent ~= text.drop(grammarKey.length).untilClosingParentheses.to!string;
248+
newContent ~= ")\n";
249+
}
250+
}
251+
return updateDdocTag(fileText, ddocKey, newContent);
252+
}
253+
return fileText;
254+
}
255+

0 commit comments

Comments
 (0)