Skip to content

Commit

Permalink
Added support for typed constants in the doc extractor.
Browse files Browse the repository at this point in the history
The type can either be given explicitly between the "constant" keyword and
the identifier, or it can be determined from the constant expression if it's
specified and is a plain literal.

Note: This makes the parsing of constant declarations somewhat more strict,
which can cause errors in existing AutoDoc markup.

This change makes the constant declarations in the GSSAPI module correct.
  • Loading branch information
marstj committed Jan 26, 2011
1 parent 1456100 commit 01dc2a8
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/modules/Tools.pmod/AutoDoc.pmod/PikeObjects.pmod
Original file line number Diff line number Diff line change
Expand Up @@ -821,11 +821,15 @@ class Constant {
//!
constant objtype = "constant";

//! The type of the constant, if known.
Type type;

//! Typedef @[Type] if it is a typedef.
Type typedefType = 0;

string xml() {
return standardStart() + standardTags()
+ (type ? xmltag ("type", type->xml()) : "")
+ (typedefType ? xmltag("typevalue", typedefType->xml()) : "")
+ standardEnd();
}
Expand Down
40 changes: 40 additions & 0 deletions lib/modules/Tools.pmod/AutoDoc.pmod/PikeParser.pike
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,33 @@ void|string parseLiteral() {
return 0;
}
Type literalType (string literal)
//! Returns the type of a literal. Currently only recognizes the top
//! level type. Currently does not thoroughly check that the literal
//! is syntactically valid.
{
if (sizeof (literal))
switch (literal[0]) {
case '\'': return IntType(); // Character constant.
case '"': return StringType();
case '(':
if (sizeof (literal) > 1)
switch (literal[1]) {
case '{': return ArrayType();
case '[': return MappingType();
case '<': return MultisetType();
}
break;
default:
if (sscanf (literal, "%*D%*c") == 1)
return IntType();
if (sscanf (literal, "%*f%*c") == 1)
return FloatType();
}
// Unrecognized format. Add an option to trig a parse error instead?
return 0;
}

//! Expect a literal constant.
//!
//! @seealso
Expand Down Expand Up @@ -731,9 +758,22 @@ PikeObject|array(PikeObject) parseDecl(mapping|void args) {
c->position = position;
c->modifiers = modifiers;
readToken();
int save_pos = tokenPtr;
mixed err = catch (c->type = parseOrType());
if (err && (!objectp (err) || !err->is_pike_parse_error))
throw (err);
if (err || (<"=", ";", EOF>)[peekToken()]) {
c->type = 0;
tokenPtr = save_pos;
}
c->name = eatIdentifier();
if (peekToken() == "=") {
eat("=");
if (string l = parseLiteral())
// It's intentional that literalType doesn't return too
// specific types for integers, i.e. it's int instead of e.g.
// int(4711..4711).
c->type = literalType (l);
// TODO: parse the expression ???
// added parsing only of types...
// a constant value will just be ignored.
Expand Down
9 changes: 9 additions & 0 deletions refdoc/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ Description: Declare a Pike entity and state that the current
2. All @decl's in the block are methods with the same
name as that method. (look at the last example
below)

The rest of the line after @decl should follow real pike
syntax, but the ending semicolon is optional. There are
some differences though, notably it allows a type to be
specified for constants.

Arguments: <declaration>
Where:
<declaration> is a valid Pike declaration. A trailing
Expand All @@ -101,6 +107,9 @@ Examples:
//! This is how to document a "polymorph" function.
float|int cube(float|int x) { /* body */ }

//! @decl constant int bitmask
//! A constant which has a public type but private value.

_______________________________________________________________________________

Keyword: @endclass
Expand Down
2 changes: 2 additions & 0 deletions refdoc/presentation/make_html.pike
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,8 @@ string parse_not_doc(Node n) {
case "constant":
if(const++) ret += "<br />\n";
ret += "<tt>constant ";
if (Node type = c->get_first_element ("type"))
ret += parse_type (get_first_element (type), "constant") + " ";
ret += c->get_attributes()->class_path;
ret += "<font color='#F000F0'>" + c->get_attributes()->name + "</font>";
cc = c->get_first_element("typevalue");
Expand Down
3 changes: 2 additions & 1 deletion refdoc/xml.txt
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ entities:
</class>

<constant>
Only has a name. The element is empty (or has a <source-position> child.)
Has a name attribute. Contains optional <type> and
<source-position> child elements.

<enum>
Works as a container. Has a <doc> child element with the documentation of
Expand Down

0 comments on commit 01dc2a8

Please sign in to comment.