summaryrefslogtreecommitdiff
path: root/lib/grammar_sandbox.c
blob: 24f882c89f57e27c7883fe780df9a4588539ea76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "command.h"
#include "command_graph.h"
#include "command_parse.h"
#include "command_match.h"
#include "linklist.h"

#define GRAMMAR_STR "CLI grammar sandbox\n"

struct graph_node * nodegraph;

DEFUN (grammar_test,
       grammar_test_cmd,
       "grammar parse .COMMAND",
       GRAMMAR_STR
       "command to pass to new parser\n")
{

  const char* command = argv_concat(argv, argc, 0);
  cmd_parse_format(command, "lol", nodegraph);
  walk_graph(nodegraph, 0);

  return CMD_SUCCESS;
}

DEFUN (grammar_test_show,
       grammar_test_show_cmd,
       "grammar tree",
       GRAMMAR_STR
       "print current accumulated DFA\n")
{
  walk_graph(nodegraph, 0);
  return CMD_SUCCESS;
}

DEFUN (grammar_test_match,
       grammar_test_match_cmd,
       "grammar match .COMMAND",
       GRAMMAR_STR
       "attempt to match input on DFA\n"
       "command to match")
{
  const char* command = argv_concat(argv, argc, 0);
  struct list *result = match_command(nodegraph, FILTER_STRICT, command);

  if (result->count == 0) // invalid command
    fprintf(stderr, "%% Unknown command\n");
  else
  {
    fprintf(stderr, "%% Matched full input, possible completions:\n");
    char* desc = malloc(50);
    struct listnode *node;
    struct graph_node *cnode;
    // print possible next hops, if any
    for (ALL_LIST_ELEMENTS_RO(result,node,cnode)) {
      if (cnode->type == END_GN)
        fprintf(stderr, "<cr>");
      else
        fprintf(stderr, "%s\n", describe_node(cnode, desc, 50));
    }
    free(desc);
  }
  list_free(result);

  return CMD_SUCCESS;
}


void grammar_sandbox_init(void);
void grammar_sandbox_init() {
  fprintf(stderr, "reinitializing graph\n");
  nodegraph = new_node(NUL_GN);
  install_element (ENABLE_NODE, &grammar_test_cmd);
  install_element (ENABLE_NODE, &grammar_test_show_cmd);
  install_element (ENABLE_NODE, &grammar_test_match_cmd);
}