vector children; // this node's children
struct graph_node * end; // pointer to end for SELECTOR_GN & OPTION_GN
- char* text; // for WORD_GN and VARIABLE_GN
+ char *text; // original format text
+ char *doc; // docstring for this node
long long value; // for NUMBER_GN
long long min, max; // for RANGE_GN
node_exists(struct graph_node *, struct graph_node *);
struct graph_node *
node_replace(struct graph_node *, struct graph_node *);
+char *
+doc_next(void);
#define DECIMAL_STRLEN_MAX 20
*selnode_end; // end node for selector set
struct cmd_element *command; // command we're parsing
+char *docstring;
%}
%token <string> WORD
{
struct graph_node *root = new_node(WORD_GN);
root->text = XSTRDUP(MTYPE_CMD_TOKENS, $1);
+ root->doc = doc_next();
if ((currnode = node_replace(startnode, root)) != root)
free (root);
{
$$ = new_node(IPV4_GN);
$$->text = XSTRDUP(MTYPE_CMD_TOKENS, $1);
+ $$->doc = doc_next();
free ($1);
}
| IPV4_PREFIX
{
$$ = new_node(IPV4_PREFIX_GN);
$$->text = XSTRDUP(MTYPE_CMD_TOKENS, $1);
+ $$->doc = doc_next();
free ($1);
}
| IPV6
{
$$ = new_node(IPV6_GN);
$$->text = XSTRDUP(MTYPE_CMD_TOKENS, $1);
+ $$->doc = doc_next();
free ($1);
}
| IPV6_PREFIX
{
$$ = new_node(IPV6_PREFIX_GN);
$$->text = XSTRDUP(MTYPE_CMD_TOKENS, $1);
+ $$->doc = doc_next();
free ($1);
}
| VARIABLE
{
$$ = new_node(VARIABLE_GN);
$$->text = XSTRDUP(MTYPE_CMD_TOKENS, $1);
+ $$->doc = doc_next();
free ($1);
}
| RANGE
{
$$ = new_node(RANGE_GN);
$$->text = XSTRDUP(MTYPE_CMD_TOKENS, $1);
+ $$->doc = doc_next();
// get the numbers out
yylval.string++;
{
$$ = new_node(WORD_GN);
$$->text = XSTRDUP(MTYPE_CMD_TOKENS, $1);
+ $$->doc = doc_next();
free ($1);
}
| NUMBER
{
$$ = new_node(NUMBER_GN);
$$->value = yylval.integer;
+ $$->text = XCALLOC(MTYPE_CMD_TOKENS, DECIMAL_STRLEN_MAX+1);
+ snprintf($$->text, DECIMAL_STRLEN_MAX, "%lld", $$->value);
+ $$->doc = doc_next();
}
;
// trace parser
yydebug = 0;
- // command string
+ // command element
command = cmd;
+ // copy docstring and keep a pointer to the copy
+ char *doc = docstring = cmd->doc ? XSTRDUP(MTYPE_TMP, cmd->doc) : NULL;
// make flex read from a string
set_buffer_string(command->string);
// parse command into DFA
yyparse();
+ // cleanup
+ free (doc);
+ doc = NULL;
// startnode points to command DFA
return startnode;
}
return existing;
}
+
+char *
+doc_next()
+{
+ char *piece = NULL;
+ if (!docstring || !(piece = strsep(&docstring, "\n")))
+ return NULL;
+ return XSTRDUP(MTYPE_CMD_TOKENS, piece);
+}
return CMD_SUCCESS;
}
+DEFUN (grammar_test_doc,
+ grammar_test_doc_cmd,
+ "grammar test docstring",
+ GRAMMAR_STR
+ "Test function for docstring\n"
+ "Command end\n")
+{
+ struct cmd_element *cmd = malloc(sizeof(struct cmd_element));
+ cmd->string = "test docstring <example|selector follow> (1-255) end VARIABLE [OPTION|set lol] . VARARG";
+ cmd->doc = "Test stuff\n"
+ "docstring thing\n"
+ "first example\n"
+ "second example\n"
+ "follow\n"
+ "random range\n"
+ "end thingy\n"
+ "variable\n"
+ "optional variable\n"
+ "optional set\n"
+ "optional lol\n"
+ "vararg!\n";
+ cmd->func = NULL;
+ cmd->tokens = vector_init(VECTOR_MIN_SIZE);
+ parse_command_format(nodegraph, cmd);
+ return CMD_SUCCESS;
+}
+
DEFUN (grammar_test_show,
grammar_test_show_cmd,
"grammar tree",
else
{
fprintf(stderr, "%% Matched full input, possible completions:\n");
- char* desc = malloc(50);
+ char* desc = malloc(30);
struct listnode *node;
struct graph_node *cnode;
// print possible next hops, if any
if (cnode->type == END_GN)
fprintf(stderr, "<cr> %p\n", cnode->element->func);
else
- fprintf(stderr, "%s\n", describe_node(cnode, desc, 50));
+ fprintf(stderr, "%-30s%s\n", describe_node(cnode, desc, 30), cnode->doc);
}
free(desc);
}
install_element (ENABLE_NODE, &grammar_test_show_cmd);
install_element (ENABLE_NODE, &grammar_test_match_cmd);
install_element (ENABLE_NODE, &grammar_test_complete_cmd);
+ install_element (ENABLE_NODE, &grammar_test_doc_cmd);
}