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_show
,
28 grammar_test_show_cmd
,
31 "print current accumulated DFA\n")
34 fprintf(stderr
, "!nodegraph\n");
35 fprintf(stderr
, "trying to print nodegraph->type\n");
36 fprintf(stderr
, "%d\n", nodegraph
->type
);
37 walk_graph(nodegraph
, 0);
41 DEFUN (grammar_test_complete
,
42 grammar_test_complete_cmd
,
43 "grammar complete .COMMAND",
45 "attempt to complete input on DFA\n"
46 "command to complete")
48 const char* command
= argv_concat(argv
, argc
, 0);
49 struct list
*result
= match_command_complete (nodegraph
, command
);
51 if (result
->count
== 0) // invalid command
52 fprintf(stderr
, "%% Unknown command\n");
55 fprintf(stderr
, "%% Matched full input, possible completions:\n");
56 char* desc
= malloc(50);
57 struct listnode
*node
;
58 struct graph_node
*cnode
;
59 // print possible next hops, if any
60 for (ALL_LIST_ELEMENTS_RO(result
,node
,cnode
)) {
61 if (cnode
->type
== END_GN
)
62 fprintf(stderr
, "<cr> %p\n", cnode
->element
->func
);
64 fprintf(stderr
, "%s\n", describe_node(cnode
, desc
, 50));
73 DEFUN (grammar_test_match
,
74 grammar_test_match_cmd
,
75 "grammar match .COMMAND",
77 "attempt to match input on DFA\n"
80 struct list
*argvv
= NULL
;
81 const char *command
= argv_concat(argv
, argc
, 0);
82 struct cmd_element
*element
= match_command (nodegraph
, command
, &argvv
);
85 fprintf(stderr
, "Matched: %s\n", element
->string
);
87 struct graph_node
*gn
;
88 for (ALL_LIST_ELEMENTS_RO(argvv
,ln
,gn
))
89 fprintf(stderr
, "%s -- %s\n", gn
->text
, gn
->arg
);
92 fprintf(stderr
, "Returned NULL\n");
100 void grammar_sandbox_init(void);
101 void grammar_sandbox_init() {
102 fprintf(stderr
, "reinitializing graph\n");
103 nodegraph
= new_node(START_GN
);
104 install_element (ENABLE_NODE
, &grammar_test_cmd
);
105 install_element (ENABLE_NODE
, &grammar_test_show_cmd
);
106 install_element (ENABLE_NODE
, &grammar_test_match_cmd
);
107 install_element (ENABLE_NODE
, &grammar_test_complete_cmd
);