summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--compiler/cpp/src/main.cc47
-rw-r--r--compiler/cpp/src/parse/t_const.h2
-rw-r--r--compiler/cpp/src/thrifty.yy61
-rwxr-xr-xtest/DocTest.thrift8
4 files changed, 80 insertions, 38 deletions
diff --git a/compiler/cpp/src/main.cc b/compiler/cpp/src/main.cc
index ed38b0747..76676f384 100644
--- a/compiler/cpp/src/main.cc
+++ b/compiler/cpp/src/main.cc
@@ -433,10 +433,11 @@ static bool dump_docs = false;
/**
* Dumps docstrings to stdout
- * Only works for typedefs and whole program
+ * Only works for top-level definitions and the whole program doc
+ * (i.e., not enum constants, struct fields, or functions.
*/
void dump_docstrings(t_program* program) {
- string progdoc = g_program->get_doc();
+ string progdoc = program->get_doc();
if (!progdoc.empty()) {
printf("Whole program doc:\n%s\n", progdoc.c_str());
}
@@ -445,7 +446,47 @@ void dump_docstrings(t_program* program) {
for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
t_typedef* td = *t_iter;
if (td->has_doc()) {
- printf("%s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
+ printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
+ }
+ }
+ const vector<t_enum*>& enums = program->get_enums();
+ vector<t_enum*>::const_iterator e_iter;
+ for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
+ t_enum* en = *e_iter;
+ if (en->has_doc()) {
+ printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
+ }
+ }
+ const vector<t_const*>& consts = program->get_consts();
+ vector<t_const*>::const_iterator c_iter;
+ for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
+ t_const* co = *c_iter;
+ if (co->has_doc()) {
+ printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
+ }
+ }
+ const vector<t_struct*>& structs = program->get_structs();
+ vector<t_struct*>::const_iterator s_iter;
+ for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
+ t_struct* st = *s_iter;
+ if (st->has_doc()) {
+ printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
+ }
+ }
+ const vector<t_struct*>& xceptions = program->get_xceptions();
+ vector<t_struct*>::const_iterator x_iter;
+ for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
+ t_struct* xn = *x_iter;
+ if (xn->has_doc()) {
+ printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
+ }
+ }
+ const vector<t_service*>& services = program->get_services();
+ vector<t_service*>::const_iterator v_iter;
+ for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
+ t_service* sv = *v_iter;
+ if (sv->has_doc()) {
+ printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
}
}
}
diff --git a/compiler/cpp/src/parse/t_const.h b/compiler/cpp/src/parse/t_const.h
index 9cae45bac..dadcfd2ef 100644
--- a/compiler/cpp/src/parse/t_const.h
+++ b/compiler/cpp/src/parse/t_const.h
@@ -18,7 +18,7 @@
*
* @author Mark Slee <mcslee@facebook.com>
*/
-class t_const {
+class t_const : public t_doc {
public:
t_const(t_type* type, std::string name, t_const_value* value) :
type_(type),
diff --git a/compiler/cpp/src/thrifty.yy b/compiler/cpp/src/thrifty.yy
index 1ad0c35fc..5f7e3b995 100644
--- a/compiler/cpp/src/thrifty.yy
+++ b/compiler/cpp/src/thrifty.yy
@@ -37,6 +37,7 @@ int y_field_val = -1;
int iconst;
double dconst;
bool tbool;
+ t_doc* tdoc;
t_type* ttype;
t_base_type* tbase;
t_typedef* ttypedef;
@@ -48,7 +49,7 @@ int y_field_val = -1;
t_service* tservice;
t_function* tfunction;
t_field* tfield;
- char* tdoc;
+ char* dtext;
}
/**
@@ -56,7 +57,7 @@ int y_field_val = -1;
*/
%token<id> tok_identifier
%token<id> tok_literal
-%token<tdoc> tok_doctext
+%token<dtext> tok_doctext
/**
* Constant values
@@ -130,6 +131,7 @@ int y_field_val = -1;
%type<ttype> SetType
%type<ttype> ListType
+%type<tdoc> Definition
%type<ttype> TypeDefinition
%type<ttypedef> Typedef
@@ -173,7 +175,7 @@ int y_field_val = -1;
%type<tstruct> XsdAttributes
%type<id> CppType
-%type<tdoc> DocTextOptional
+%type<dtext> DocTextOptional
%%
@@ -273,9 +275,12 @@ Include:
}
DefinitionList:
- DefinitionList Definition
+ DefinitionList DocTextOptional Definition
{
pdebug("DefinitionList -> DefinitionList Definition");
+ if ($2 != NULL && $3 != NULL) {
+ $3->set_doc($2);
+ }
}
|
{
@@ -289,6 +294,7 @@ Definition:
if (g_parse_mode == PROGRAM) {
g_program->add_const($1);
}
+ $$ = $1;
}
| TypeDefinition
{
@@ -299,6 +305,7 @@ Definition:
g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
}
}
+ $$ = $1;
}
| Service
{
@@ -310,6 +317,7 @@ Definition:
}
g_program->add_service($1);
}
+ $$ = $1;
}
TypeDefinition:
@@ -350,14 +358,11 @@ TypeDefinition:
}
Typedef:
- DocTextOptional tok_typedef DefinitionType tok_identifier
+ tok_typedef DefinitionType tok_identifier
{
pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier");
- t_typedef *td = new t_typedef(g_program, $3, $4);
+ t_typedef *td = new t_typedef(g_program, $2, $3);
$$ = td;
- if ($1 != NULL) {
- td->set_doc($1);
- }
}
DocTextOptional:
@@ -380,14 +385,11 @@ CommaOrSemicolonOptional:
{}
Enum:
- DocTextOptional tok_enum tok_identifier '{' EnumDefList '}'
+ tok_enum tok_identifier '{' EnumDefList '}'
{
pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
- $$ = $5;
- $$->set_name($3);
- if ($1 != NULL) {
- $$->set_doc($1);
- }
+ $$ = $4;
+ $$->set_name($2);
}
EnumDefList:
@@ -432,13 +434,10 @@ EnumDef:
}
Senum:
- DocTextOptional tok_senum tok_identifier '{' SenumDefList '}'
+ tok_senum tok_identifier '{' SenumDefList '}'
{
pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
- $$ = new t_typedef(g_program, $5, $3);
- if ($1 != NULL) {
- $$->set_doc($1);
- }
+ $$ = new t_typedef(g_program, $4, $2);
}
SenumDefList:
@@ -565,15 +564,12 @@ ConstMapContents:
}
Struct:
- DocTextOptional tok_struct tok_identifier XsdAll '{' FieldList '}'
+ tok_struct tok_identifier XsdAll '{' FieldList '}'
{
pdebug("Struct -> tok_struct tok_identifier { FieldList }");
- $6->set_name($3);
- if ($1 != NULL) {
- $6->set_doc($1);
- }
- $6->set_xsd_all($4);
- $$ = $6;
+ $5->set_name($2);
+ $5->set_xsd_all($3);
+ $$ = $5;
y_field_val = -1;
}
@@ -628,15 +624,12 @@ Xception:
}
Service:
- DocTextOptional tok_service tok_identifier Extends '{' FunctionList '}'
+ tok_service tok_identifier Extends '{' FunctionList '}'
{
pdebug("Service -> tok_service tok_identifier { FunctionList }");
- $$ = $6;
- $$->set_name($3);
- $$->set_extends($4);
- if ($1 != NULL) {
- $$->set_doc($1);
- }
+ $$ = $5;
+ $$->set_name($2);
+ $$->set_extends($3);
}
Extends:
diff --git a/test/DocTest.thrift b/test/DocTest.thrift
index 361bdded6..e202b0eb5 100755
--- a/test/DocTest.thrift
+++ b/test/DocTest.thrift
@@ -45,6 +45,14 @@ struct Xtruct
11: i64 i64_thing
}
+/**
+ * You can document constants now too. Yeehaw!
+ */
+const i32 INT32CONSTANT = 9853
+const i16 INT16CONSTANT = 1616
+/** Everyone get in on the docu-action! */
+const map<string,string> MAPCONSTANT = {'hello':'world', 'goodnight':'moon'}
+
struct Xtruct2
{
1: byte byte_thing,