parse_config.c 7.42 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <moberg_config_parser.h>
#include <moberg_driver.h>

#define token moberg_config_parser_token
#define token_kind moberg_config_parser_token_kind 
#define acceptsym moberg_config_parser_acceptsym
Anders Blomdell's avatar
Anders Blomdell committed
14
#define peeksym moberg_config_parser_peeksym
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

typedef struct moberg_config_parser_token token_t;
typedef struct moberg_config_parser_ident ident_t;

typedef struct moberg_config_parser_context {
  char *buf;      /* Pointer to data to be parsed */
  const char *p;  /* current parse location */
  token_t token;
} context_t;

static const void nextsym_ident_or_keyword(context_t *c)
{
  c->token.u.ident.length = 1;
  c->token.u.ident.value = c->p;
  c->p++;
  while (*c->p) {
    switch (*c->p) {
      case 'a'...'z':
      case 'A'...'Z':
      case '0'...'9':
      case '_':
        c->p++;
        c->token.u.ident.length++;
        break;
      default:
        goto out;
        break;
    }
  }
out: ;
  const char *v = c->token.u.ident.value;
  int l = c->token.u.ident.length;
  if (strncmp("config", v, l) == 0) {
    c->token.kind = tok_CONFIG;
  } else if (strncmp("map", v, l) == 0) {
    c->token.kind = tok_MAP;
Anders Blomdell's avatar
Anders Blomdell committed
51
  } else if (strncmp("analog_in", v, l) == 0) {
52
    c->token.kind = tok_ANALOGIN;
Anders Blomdell's avatar
Anders Blomdell committed
53
  } else if (strncmp("analog_out", v, l) == 0) {
54
    c->token.kind = tok_ANALOGOUT;
Anders Blomdell's avatar
Anders Blomdell committed
55
  } else if (strncmp("digital_in", v, l) == 0) {
56
    c->token.kind = tok_DIGITALIN;
Anders Blomdell's avatar
Anders Blomdell committed
57
  } else if (strncmp("digital_out", v, l) == 0) {
58
    c->token.kind = tok_DIGITALOUT;
Anders Blomdell's avatar
Anders Blomdell committed
59
  } else if (strncmp("encoder_in", v, l) == 0) {
60
61
62
63
    c->token.kind = tok_ENCODERIN;
  } else {
    c->token.kind = tok_IDENT;
  }
Anders Blomdell's avatar
Anders Blomdell committed
64
  printf("IDENT: %.*s %d\n", l, v, c->token.kind);
65
66
67
68
}

static const void nextsym_integer(context_t *c)
{
Anders Blomdell's avatar
Anders Blomdell committed
69
  c->token.kind = tok_INTEGER;
70
71
72
73
74
75
76
77
78
  c->token.u.integer.value = 0;
  while (*c->p && '0' <= *c->p && *c->p <= '9') {
    c->token.u.integer.value *= 10;
    c->token.u.integer.value += *c->p - '0';
    c->p++;
  }
  printf("INTEGER: %d\n", c->token.u.integer.value);
}

Anders Blomdell's avatar
Anders Blomdell committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
static const void nextsym_string(context_t *c)
{
  printf("STTRING");
  c->token.kind = tok_STRING;
  c->p++;
  c->token.u.string.value = c->p;
  c->token.u.string.length = 0;
  while (*c->p && *c->p != '"') {
    if (*c->p == '\\') {
      c->token.u.string.length++;
      c->p++;
    }
    if (*c->p) {
      c->token.u.string.length++;
      c->p++;
    }
  }
  c->p++;
  printf("STRING: %.*s\n", c->token.u.string.length, c->token.u.string.value);
}

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
static int nextsym(context_t *c)
{
  c->token.kind = tok_none;
  while (c->p && *c->p && c->token.kind == tok_none) {
    if (c->p[0] == '/' && c->p[1] == '*') {
      printf("Skipping COMMENT\n");
      /* Skip comment */
      c->p += 2;
      while (*c->p && (c->p[0] != '*' || c->p[1] != '/')) {
        c->p++;
      }
      c->p += 2;
      continue;
    }
    switch (*c->p) {
      case ' ':
      case '\t':
      case '\n':
      case '\r':
        /* Skip whitespace */
        c->p++;
        break;
      case '{':
        c->token.kind = tok_LBRACE;
        c->p++;
        break;
      case '}':
        c->token.kind = tok_RBRACE;
        c->p++;
        break;
      case '[':
        c->token.kind = tok_LBRACKET;
        c->p++;
        break;
      case ']':
        c->token.kind = tok_RBRACKET;
        c->p++;
        break;
      case '=':
        c->token.kind = tok_EQUAL;
        c->p++;
        break;
      case ':':
        c->token.kind = tok_COLON;
        c->p++;
        break;
      case ';':
        c->token.kind = tok_SEMICOLON;
        c->p++;
        break;
Anders Blomdell's avatar
Anders Blomdell committed
150
151
152
      case '"':
        nextsym_string(c);
        break;
153
154
155
156
157
158
159
160
161
162
163
164
165
166
      case 'a'...'z':
      case 'A'...'Z':
      case '_':
        nextsym_ident_or_keyword(c);
        break;
      case '0'...'9':
        nextsym_integer(c);
        break;
      default:
        printf("UNEXPECTED %c\n\n", *c->p);
        c->p++;
        break;
    }
  }
Anders Blomdell's avatar
Anders Blomdell committed
167
  printf("TOKEN %d %c\n\n", c->token.kind, c->token.kind<255?c->token.kind:' ');
168
169
170
171
172
173
174
175
176
177
178
179
  if (c->token.kind != tok_none) {
    return 1;
  } else {
    return 0;
  }
}

int moberg_config_parser_acceptsym(context_t *c,
                                   enum token_kind kind,
                                   token_t *token)
{
  if (c->token.kind == kind) {
Anders Blomdell's avatar
Anders Blomdell committed
180
    printf("ACCEPT %d", c->token.kind);
181
182
183
184
185
186
    if (token) {
      *token = c->token;
    }
    nextsym(c);
    return 1;
  }
Anders Blomdell's avatar
Anders Blomdell committed
187
  printf("REJECT %d (%d)", kind, c->token.kind);
188
189
190
  return 0;
}

Anders Blomdell's avatar
Anders Blomdell committed
191
192
193
194
195
196
197
198
199
int moberg_config_parser_peeksym(context_t *c,
				 token_t *token)
{
  if (token) {
    *token = c->token;
  }
  return *c->p != 0;
}

200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
static int parse_map_range(context_t *c)
{
  token_t low, high;
  if (! acceptsym(c, tok_LBRACKET, NULL)) { goto err; }
  if (! acceptsym(c, tok_INTEGER, &low)) { goto err; }
  if (acceptsym(c, tok_COLON, NULL)) {
    if (! acceptsym(c, tok_INTEGER, &high)) { goto err; }
  } else {
    high = low;
  }
  if (! acceptsym(c, tok_RBRACKET, NULL)) { goto err; }
  return 1;
err:
  printf("OOPS");
  return 0;
}

static int parse_map(context_t *c,
                     struct moberg_driver *driver)
{
Anders Blomdell's avatar
Anders Blomdell committed
220
  printf("parsemap");
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
  struct token t;
  if (acceptsym(c, tok_ANALOGIN, &t) ||
      acceptsym(c, tok_ANALOGOUT, &t) ||
      acceptsym(c, tok_DIGITALIN, &t) ||
      acceptsym(c, tok_DIGITALOUT, &t) ||
      acceptsym(c, tok_ENCODERIN, &t)) {
    if (! parse_map_range(c)) { goto err; }
    if (! acceptsym(c, tok_EQUAL, NULL)) { goto err; }
    driver->module.parse_map(c, t.kind);
    if (! parse_map_range(c)) { goto err; }
    if (! acceptsym(c, tok_SEMICOLON, NULL)) { goto err; }
    switch (t.kind) {
      case tok_ANALOGIN:
        
      case tok_ANALOGOUT:
        
      case tok_DIGITALIN:

      case tok_DIGITALOUT:
        
      case tok_ENCODERIN:
        break;
      default:
        goto err;
    }
  } else {
    goto err;
  }
  return 1;
err:    
  return 0;
}

static int parse_device(context_t *c,
                        struct moberg_driver *driver)
{
  if (! acceptsym(c, tok_LBRACE, NULL)) { goto err; }
  for (;;) {
Anders Blomdell's avatar
Anders Blomdell committed
259
260
261
    struct token t;
    peeksym(c, &t);
    printf("PEEK %d", t.kind);
262
263
264
265
    if (acceptsym(c, tok_CONFIG, NULL)) {
      driver->module.parse_config(c);
    } else if (acceptsym(c, tok_MAP, NULL)) {
      parse_map(c, driver);
Anders Blomdell's avatar
Anders Blomdell committed
266
267
    } else if (acceptsym(c, tok_RBRACE, NULL)) {
      break;
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
    } else {
      goto err;
    }
  }
  return 1;
err:
  return 0;
}

static int parse_driver(context_t *c,
                        ident_t name)
{
  struct moberg_driver *driver = moberg_open_driver(name);
  if (! driver) {
    printf("Driver not found\n");
    goto err;
  } else {
    int OK = parse_device(c, driver);
    moberg_close_driver(driver);
    if (! OK) { goto err; } 
  }
  return 1;
err:
  return 0;
}

static int parse_config(context_t *c)
{
  for (;;) {
    struct token t;
Anders Blomdell's avatar
Anders Blomdell committed
298
299
    peeksym(c, &t);
    printf("PEEK %d", t.kind);
300
301
302
303
304
305
306
307
    if (acceptsym(c, tok_IDENT, &t)) {
      printf("DRIVER=%.*s\n", t.u.ident.length, t.u.ident.value);
      if (! parse_driver(c, t.u.ident)) { goto err; }
    }
  }
  return 1;
err:
  printf("Failed!!");
Anders Blomdell's avatar
Anders Blomdell committed
308
  exit (1);
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
  return 0;
}

static char *read_fd(int fd)
{
  char *result = malloc(10000); /* HACK */
  int pos = read(fd, result, 10000);
  result[pos] = 0;
  return result;
}

int main(int argc, char *argv[])
{
  context_t context;
  int fd = open(argv[1], O_RDONLY);
  context.buf = read_fd(fd);
  context.p = context.buf;
  close(fd);
  printf("%s\n", context.buf);
  nextsym(&context);
  parse_config(&context);
  free(context.buf);
}