2 #include "command_graph.h"
3 #include "command_parse.h"
4 #include "command_match.h"
7 #define GRAMMAR_STR "CLI grammar sandbox\n"
9 struct graph_node
* nodegraph
;
13 "grammar parse .COMMAND",
15 "command to pass to new parser\n")
17 char* command
= argv_concat(argv
, argc
, 0);
18 struct cmd_element
*cmd
= malloc(sizeof(struct cmd_element
));
19 cmd
->string
= command
;
22 cmd
->tokens
= vector_init(VECTOR_MIN_SIZE
);
23 parse_command_format(nodegraph
, cmd
);
27 DEFUN (grammar_test_doc
,
29 "grammar test docstring",
31 "Test function for docstring\n"
34 struct cmd_element
*cmd
= malloc(sizeof(struct cmd_element
));
35 cmd
->string
= "test docstring <example|selector follow> (1-255) end VARIABLE [OPTION|set lol] . VARARG";
36 cmd
->doc
= "Test stuff\n"
49 cmd
->tokens
= vector_init(VECTOR_MIN_SIZE
);
50 parse_command_format(nodegraph
, cmd
);
54 DEFUN (grammar_test_show
,
55 grammar_test_show_cmd
,
58 "print current accumulated DFA\n")
61 fprintf(stderr
, "!nodegraph\n");
63 walk_graph(nodegraph
, 0);
67 DEFUN (grammar_test_complete
,
68 grammar_test_complete_cmd
,
69 "grammar complete .COMMAND",
71 "attempt to complete input on DFA\n"
72 "command to complete")
74 const char* command
= argv_concat(argv
, argc
, 0);
75 struct list
*result
= match_command_complete (nodegraph
, command
);
77 if (result
->count
== 0) // invalid command
78 fprintf(stderr
, "%% Unknown command\n");
81 fprintf(stderr
, "%% Matched full input, possible completions:\n");
82 char* desc
= malloc(30);
83 struct listnode
*node
;
84 struct graph_node
*cnode
;
85 // print possible next hops, if any
86 for (ALL_LIST_ELEMENTS_RO(result
,node
,cnode
)) {
87 if (cnode
->type
== END_GN
)
88 fprintf(stderr
, "<cr> %p\n", cnode
->element
->func
);
90 fprintf(stderr
, "%-30s%s\n", describe_node(cnode
, desc
, 30), cnode
->doc
);
99 DEFUN (grammar_test_match
,
100 grammar_test_match_cmd
,
101 "grammar match .COMMAND",
103 "attempt to match input on DFA\n"
106 const char *line
= argv_concat(argv
, argc
, 0);
108 struct list
*argvv
= NULL
;
109 struct cmd_element
*element
= NULL
;
110 enum matcher_rv result
= match_command (nodegraph
, line
, &argvv
, &element
);
113 fprintf(stderr
, "Matched: %s\n", element
->string
);
115 struct graph_node
*gn
;
116 for (ALL_LIST_ELEMENTS_RO(argvv
,ln
,gn
))
117 fprintf(stderr
, "%s -- %s\n", gn
->text
, gn
->arg
);
121 case MATCHER_NO_MATCH
:
122 fprintf(stderr
, "%% Unknown command\n");
124 case MATCHER_INCOMPLETE
:
125 fprintf(stderr
, "%% Incomplete command\n");
127 case MATCHER_AMBIGUOUS
:
128 fprintf(stderr
, "%% Ambiguous command\n");
131 fprintf(stderr
, "%% Unknown error\n");
140 void grammar_sandbox_init(void);
141 void grammar_sandbox_init() {
142 fprintf(stderr
, "reinitializing graph\n");
143 nodegraph
= new_node(START_GN
);
144 install_element (ENABLE_NODE
, &grammar_test_cmd
);
145 install_element (ENABLE_NODE
, &grammar_test_show_cmd
);
146 install_element (ENABLE_NODE
, &grammar_test_match_cmd
);
147 install_element (ENABLE_NODE
, &grammar_test_complete_cmd
);
148 install_element (ENABLE_NODE
, &grammar_test_doc_cmd
);