Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added doc comments to function arguments in func defs that use formals.
  • Loading branch information
moretea committed Jun 26, 2016
1 parent d121a81 commit 9be41e4
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/libexpr/nixexpr.cc
Expand Up @@ -434,5 +434,12 @@ size_t SymbolTable::totalSize() const
return n;
}

Formal::~Formal() {
if (this->docComment != 0) {
// Free the C string.
free(this->docComment);
this->docComment = 0;
}
}

}
5 changes: 4 additions & 1 deletion src/libexpr/nixexpr.hh
Expand Up @@ -212,7 +212,10 @@ struct Formal
{
Symbol name;
Expr * def;
Formal(const Symbol & name, Expr * def) : name(name), def(def) { };
char *docComment;
Formal(const Symbol & name, Expr * def, char * docComment) : name(name), def(def), docComment(docComment) { };

~Formal();
};

struct Formals
Expand Down
9 changes: 7 additions & 2 deletions src/libexpr/parser.y
Expand Up @@ -250,6 +250,7 @@ void yyerror(YYLTYPE * loc, yyscan_t scanner, ParseData * data, const char * err
const char * id; // !!! -> Symbol
char * path;
char * uri;
char *doc_comment;
std::vector<nix::AttrName> * attrNames;
std::vector<nix::Expr *> * string_parts;
}
Expand Down Expand Up @@ -509,8 +510,12 @@ formals
;

formal
: ID { $$ = new Formal(data->symbols.create($1), 0); }
| ID '?' expr { $$ = new Formal(data->symbols.create($1), $3); }
: ID { $$ = new Formal(data->symbols.create($1), 0, 0); }
| ID '?' expr { $$ = new Formal(data->symbols.create($1), $3, 0); }
| DOC_COMMENT ID { $$ = new Formal(data->symbols.create($2), 0, $1); }
| ID DOC_COMMENT { $$ = new Formal(data->symbols.create($1), 0, $2); }
| DOC_COMMENT ID '?' expr { $$ = new Formal(data->symbols.create($2), $4, $1); }
| ID '?' expr DOC_COMMENT { $$ = new Formal(data->symbols.create($1), $3, $4); }
;

%%
Expand Down
24 changes: 24 additions & 0 deletions src/libexpr/primops.cc
Expand Up @@ -1196,6 +1196,29 @@ static void prim_functionArgs(EvalState & state, const Pos & pos, Value * * args
v.attrs->sort();
}

static void prim_functionArgDocs(EvalState & state, const Pos & pos, Value * * args, Value & v)
{
state.forceValue(*args[0]);
if (args[0]->type != tLambda)
throw TypeError(format("‘functionArgDocs’ requires a function, at %1%") % pos);

if (!args[0]->lambda.fun->matchAttrs) {
state.mkAttrs(v, 0);
return;
}

state.mkAttrs(v, args[0]->lambda.fun->formals->formals.size());

for (auto & i : args[0]->lambda.fun->formals->formals) {
if (i.docComment != NULL)
mkString(*state.allocAttr(v, i.name), i.docComment);
else
mkNull(*state.allocAttr(v,i.name));
}

v.attrs->sort();
}


/*************************************************************
* Lists
Expand Down Expand Up @@ -1835,6 +1858,7 @@ void EvalState::createBaseEnv()
addPrimOp("__intersectAttrs", 2, prim_intersectAttrs);
addPrimOp("__catAttrs", 2, prim_catAttrs);
addPrimOp("__functionArgs", 1, prim_functionArgs);
addPrimOp("__functionArgDocs", 1, prim_functionArgDocs);

// Lists
addPrimOp("__isList", 1, prim_isList);
Expand Down
1 change: 1 addition & 0 deletions tests/lang/eval-okay-functionargs-docs.exp
@@ -0,0 +1 @@
{ a = "aa"; b = "bb"; }
1 change: 1 addition & 0 deletions tests/lang/eval-okay-functionargs-docs.nix
@@ -0,0 +1 @@
builtins.functionArgDocs ({a /*! aa */, /*! bb */ b}: null)

0 comments on commit 9be41e4

Please sign in to comment.