Skip to content

Add support for arbitrary expressions as default values in documentation #1323

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

Closed
wants to merge 4 commits into from
Closed
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
6 changes: 6 additions & 0 deletions src/libponyc/ast/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,12 @@ const char* ast_get_print(ast_t* ast)
return token_print(ast->t);
}

char* ast_string_escape(ast_t* ast)
{
pony_assert(ast != NULL && ast_id(ast) == TK_STRING);
return token_print_escaped(ast->t);
}

const char* ast_name(ast_t* ast)
{
pony_assert(ast != NULL);
Expand Down
1 change: 1 addition & 0 deletions src/libponyc/ast/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ void ast_resetpass(ast_t *ast, uint32_t flag);
const char* ast_get_print(ast_t* ast);
const char* ast_name(ast_t* ast);
const char* ast_nice_name(ast_t* ast);
char* ast_string_escape(ast_t* ast);
size_t ast_name_len(ast_t* ast);
void ast_set_name(ast_t* ast, const char* name);
double ast_float(ast_t* ast);
Expand Down
106 changes: 82 additions & 24 deletions src/libponyc/pass/docgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,82 @@ static void doc_type_params(docgen_t* docgen, docgen_opt_t* docgen_opt,
fprintf(docgen->type_file, "]");
}

// Write the expression to the current type file.
void doc_expression(docgen_t* docgen, docgen_opt_t* docgen_opt, ast_t* ast)
{
FILE* fp = docgen->type_file;
ast_t* child = ast_child(ast);
token_id token = ast_id(ast);

switch(token)
{
case TK_CALL:
case TK_CHAIN:
case TK_DOT:
case TK_SEQ:
while(child != NULL)
{
doc_expression(docgen, docgen_opt, child);
child = ast_sibling(child);
if (child != NULL) {
if (token == TK_CHAIN)
fprintf(fp, ".>");

if (token == TK_DOT)
fprintf(fp, ".");

if (token == TK_SEQ)
fprintf(fp, "; ");
}
}
if (token == TK_CALL)
fprintf(fp, "()");
Copy link
Contributor

Choose a reason for hiding this comment

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

Where are call arguments handled?

break;

case TK_REFERENCE:
doc_expression(docgen, docgen_opt, child);
break;

case TK_TYPE:
case TK_INTERFACE:
case TK_TRAIT:
case TK_PRIMITIVE:
case TK_STRUCT:
case TK_CLASS:
case TK_ACTOR:
doc_type(docgen, docgen_opt, ast, false, false);
break;

case TK_NONE:
break;

case TK_STRING:
{
char* escaped = ast_string_escape(ast);
fprintf(fp, "\"%s\"", escaped);
ponyint_pool_free_size(strlen(escaped) + 1, escaped);
break;
}

case TK_RECOVER:
fprintf(fp, "recover");
while(child != NULL)
{
if (ast_id(child) != TK_NONE) {
Copy link
Member

Choose a reason for hiding this comment

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

this seems like the wrong way to tackle this.

there could be other types later that we would be special casing for. that said, i dont immediately have an idea for a better way to address.

fprintf(fp, " ");
doc_expression(docgen, docgen_opt, child);
}
child = ast_sibling(child);
}
fprintf(fp, " end");
break;

default:
fprintf(fp, "%s", ast_get_print(ast));
break;
}
}

// Write the given list of parameters to the current type file, with
// surrounding (). If the given list is empty () is still written.
static void code_block_doc_params(docgen_t* docgen, docgen_opt_t* docgen_opt,
Expand Down Expand Up @@ -647,18 +723,9 @@ static void code_block_doc_params(docgen_t* docgen, docgen_opt_t* docgen_opt,
doc_type(docgen, docgen_opt, type, false, true);

// if we have a default value, add it to the documentation
if(ast_id(def_val) != TK_NONE)
{
switch(ast_id(def_val))
{
case TK_STRING:
fprintf(docgen->type_file, "= \"%s\"", ast_get_print(def_val));
break;

default:
fprintf(docgen->type_file, " = %s", ast_get_print(def_val));
break;
}
if(ast_id(def_val) != TK_NONE) {
fprintf(docgen->type_file, " = ");
doc_expression(docgen, docgen_opt, def_val);
}
}

Expand Down Expand Up @@ -689,18 +756,9 @@ static void list_doc_params(docgen_t* docgen, docgen_opt_t* docgen_opt,
doc_type(docgen, docgen_opt, type, true, true);

// if we have a default value, add it to the documentation
if(ast_id(def_val) != TK_NONE)
{
switch(ast_id(def_val))
{
case TK_STRING:
fprintf(docgen->type_file, "= \"%s\"", ast_get_print(def_val));
break;

default:
fprintf(docgen->type_file, " = %s", ast_get_print(def_val));
break;
}
if(ast_id(def_val) != TK_NONE) {
fprintf(docgen->type_file, " = ");
doc_expression(docgen, docgen_opt, def_val);
}

fprintf(docgen->type_file, "\n");
Expand Down