summaryrefslogtreecommitdiff
path: root/lib/command_parse.y
blob: e7efb0d5a82887d1dce0d7d12ae95950db173ab9 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
%{
#include "cmdtree.h"

extern int yylex(void);
extern void yyerror(const char *);
extern int cmd_parse_format(const char *, const char *);
extern void set_buffer_string(const char *);

// compile with debugging facilities
#define YYDEBUG 1
%}

%union{
  int integer;
  char *string;
  struct graph_node *node;
}

%{
// last top-level node
struct graph_node *topnode,         // command root node
                  *currnode;        // current node

struct graph_node *optnode_start,   // start node for option set
                  *optnode_end,     // end node for option set
                  *optnode_el;      // start node for an option set element

struct graph_node *selnode_start,   // start node for selector set
                  *selnode_end,     // end node for selector set
                  *selnode_el;      // start node for an selector set element
%}

%token <string>  WORD
%token <string>  IPV4
%token <string>  IPV4_PREFIX
%token <string>  IPV6
%token <string>  IPV6_PREFIX
%token <string>  VARIABLE
%token <string>  RANGE
%token <integer> NUMBER

%type <node> start
%type <node> sentence_root
%type <node> literal_token
%type <node> placeholder_token
%type <node> option_token
%type <node> selector_token
%type <node> option
%type <node> selector
%type <node> selector_token_seq
%type <node> option_token_seq

%defines "command_parse.h"
%output "command_parse.c"

/* grammar proper */
%%

start: sentence_root
       cmd_token_seq;

sentence_root: WORD
  {
    currnode = new_node(WORD_GN);
    currnode->is_root = 1;
    add_node(topnode, currnode);
  };

/* valid top level tokens */
cmd_token:
  placeholder_token
  { currnode = add_node(currnode, $1); }
| literal_token
  { currnode = add_node(currnode, $1); }
| selector
  {
    add_node(currnode, selnode_start);
    currnode = selnode_end;
  }
| option
  {
    add_node(currnode, optnode_start);
    currnode = optnode_end;
  }
;

cmd_token_seq:
  %empty
| cmd_token_seq cmd_token
;

placeholder_token:
  IPV4                                  {$$ = new_node(IPV4_GN);}
| IPV4_PREFIX                           {$$ = new_node(IPV4_PREFIX_GN);}
| IPV6                                  {$$ = new_node(IPV6_GN);}
| IPV6_PREFIX                           {$$ = new_node(IPV6_PREFIX_GN);}
| VARIABLE                              {$$ = new_node(VARIABLE_GN);}
| RANGE                                 {$$ = new_node(RANGE_GN);}
;

literal_token:
  WORD                                  {$$ = new_node(WORD_GN);}
| NUMBER                                {$$ = new_node(NUMBER_GN);}
;

/* <selector|token> productions */
selector:
  '<' selector_part '|'
      selector_element '>'
{
  $$ = new_node(SELECTOR_GN);
  // attach subtree here
};

selector_part:
  selector_part '|' selector_element
| selector_element
;

selector_element:
  WORD selector_token_seq;

selector_token_seq:
  %empty                                {$$ = NULL;}
| selector_token_seq selector_token
{
  currnode = add_node(currnode, $2);
}
;

selector_token:
  literal_token
| placeholder_token
| option
;

/* [option|set] productions */
option: '[' option_part ']'
{
  $$ = new_node(OPTION_GN);
  // attach subtree here
};

option_part:
  option_part '|' option_token_seq
| option_token_seq
;

option_token_seq:
  option_token_seq option_token
| option_token
{
  printf("Matched singular option token in sequence, type: %d\n", $1->type);
}
;

option_token:
  literal_token
{
  // optnode_el points to root of option element
  if (optnode_el == NULL) {
    optnode_el = $1;
    currnode = $1;
  }
  else
    add_node(currnode, $1);
}
| placeholder_token
{
  // optnode_el points to root of option element
  if (optnode_el == NULL) {
    optnode_el = $1;
    currnode = $1;
  }
  else
    add_node(currnode, $1);
}
;

%%

int
main (void)
{
  yydebug = 1;
  const char* input = "show [random conf NAME] thing";
  printf("Parsing:\n\t%s\n", input);
  return cmd_parse_format(input, "description");
}

void yyerror(char const *message) {
  printf("Grammar error: %s\n", message);
  exit(EXIT_FAILURE);
}

int
cmd_parse_format(const char *string, const char *desc)
{
  // make flex read from a string
  set_buffer_string(string);
  // initialize the start node of this command dfa
  topnode = new_node(NUL_GN);
  // parse command into DFA
  yyparse();
  // topnode points to command DFA
  return 0;
}