Skip to content

Commit

Permalink
Merge pull request #169 from snowball-lang/167-generate-macros
Browse files Browse the repository at this point in the history
Added macros support for documentation generator
  • Loading branch information
mauro-balades authored Dec 24, 2023
2 parents 6727b90 + f8e7cea commit 14a24c1
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/Debug/snowball",
"args": ["docs"],
"args": ["test", "-O0"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
Expand Down
15 changes: 15 additions & 0 deletions src/ast/syntax/exprs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,20 @@ std::string Macro::arguementTypeToString(Macro::ArguementType t) {
UNREACHABLE
}

std::string Macro::arguementTypeToSyntax(ArguementType type) {
switch (type) {
case ArguementType::CONSTANT: return "const";
case ArguementType::EXPRESSION: return "expr";
case ArguementType::STATEMENT: return "stmt";
case ArguementType::CONSTANT_STRING: return "const[str]";
case ArguementType::CONSTANT_NUMBER: return "const[num]";
case ArguementType::CONSTANT_CHAR: return "const[chr]";
case ArguementType::TYPE: return "type";
default: E<BUG>(FMT("Unknown arguement type '%i'!", type));
}

UNREACHABLE
}

} // namespace Syntax
} // namespace snowball
4 changes: 4 additions & 0 deletions src/ast/syntax/nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,10 @@ struct Macro : public AcceptorExtend<Macro, Node>, public AcceptorExtend<Macro,
/// @param type Type to convert
/// @return String representation of the type
static std::string arguementTypeToString(ArguementType type);
/// @brief Convert an arguement type to a syntax equivalent
/// @param type Type to convert
/// @return String representation of the type
static std::string arguementTypeToSyntax(ArguementType type);
// Set an acceptance call
ACCEPT()
};
Expand Down
14 changes: 13 additions & 1 deletion src/visitors/documentation/DocGen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace Syntax {
using Operator = services::OperatorService;

SN_DOCGEN_VISIT(Block) {}
SN_DOCGEN_VISIT(Macro) {}

SN_DOCGEN_VISIT(Expression::ConstantValue) {}
SN_DOCGEN_VISIT(Expression::FunctionCall) {}
Expand Down Expand Up @@ -100,6 +99,19 @@ SN_DOCGEN_VISIT(Statement::FunctionDef) {
result.pages.push_back(newPage);
}

SN_DOCGEN_VISIT(Macro) {
auto rawName = p_node->getName();
auto [name, path] = getFullName(rawName);

auto newPage = DocumentationPage {
.name = name,
.path = path + "-macro.html",
.type = DocumentationPage::Type::Function,
};
docgen::createMacroPage(p_node, context, newPage);
result.pages.push_back(newPage);
}

void DocGen::createModulePage(std::vector<Syntax::Node*> nodes) {
auto newPage = DocumentationPage {
.name = context.currentModule,
Expand Down
63 changes: 63 additions & 0 deletions src/visitors/documentation/DocTemplates.cc
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,69 @@ void createModulePage(std::vector<Syntax::Node*> nodes, DocGenContext context, D
page.html = getPageTemplate(context, page.name, body);
}

void createMacroPage(Macro* node, DocGenContext context, DocumentationPage& page) {
std::string body = "";
body += "<h1>";

auto pathParts = page.path;
auto nameParts = utils::list2vec(utils::split(page.name, "::"));
int i = 0;
for (auto _:pathParts) {
std::string url = "";
int j = 0;
for (auto part : pathParts) {
if (j == i) {
url += part.string();
if (utils::endsWith(part.string(), ".html"))
url = url.substr(0, url.size() - 5);
break;
} else {
url += part.string() + "/";
}
j++;
}
body += "<a style=\"color:#78d3fc;\" href=\"" + url + ".html\">" + nameParts[i] + "</a>::";
i++;
}
body = body.substr(0, body.size() - 2) + ";"; // remove the last "::"
body += "</h1><hr/><div><pre><code class=\"language-snowball\">";

body += "macro " + node->getName() + "(";
for (auto& param : node->getArgs()) {
body += std::get<0>(param);
auto argType = std::get<1>(param);
body += ": " + Macro::arguementTypeToSyntax(argType);
if (std::get<2>(param) != nullptr) {
body += " = ...";
}
body += ", ";
}
if (node->getArgs().size() > 0)
body = body.substr(0, body.size() - 2);
if (node->isMacroStatement())
body += ") {\n ...\n}";
else body += ") = ...";

body += "</code></pre></div><br><div class=\"doc\">";

if (auto docString = node->getComment()) {
auto values = docString->getValues();
if (values.count("brief")) {
body += "<h1 style=\"color:#78d3fc;\">Brief Description for <quote>" + node->getName() + "</quote></h1>";
body += "<p>" + sanitize(values["brief"]) + "</p>";
}
body += "<p>" + sanitize(docString->getBody()) + "</p>";

for (auto& [tag, value] : values) {
if (tag == "brief") continue;
}
}

body += "<br/><br/></div>";

page.html = getPageTemplate(context, page.name, body);
}

} // namespace docgen
} // namespace Syntax
} // namespace snowball
9 changes: 9 additions & 0 deletions src/visitors/documentation/DocTemplates.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ void createTypePage(Statement::DefinedTypeDef* node, DocGenContext context, Docu
* @ingroup docgen
*/
void createModulePage(std::vector<Syntax::Node*> nodes, DocGenContext context, DocumentationPage& page);
/**
* @brief Creates a documentation page for a macro.
*
* @param node The macro node.
* @param context The documentation generator context.
* @return DocumentationPage The documentation page.
* @ingroup docgen
*/
void createMacroPage(Macro* node, DocGenContext context, DocumentationPage& page);

} // namespace docgen
} // namespace Syntax
Expand Down

0 comments on commit 14a24c1

Please sign in to comment.