Coverage Report

Created: 2018-06-19 22:08

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/External/isl/isl_stream.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2008-2009 Katholieke Universiteit Leuven
3
 *
4
 * Use of this software is governed by the MIT license
5
 *
6
 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7
 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8
 */
9
10
#include <ctype.h>
11
#include <string.h>
12
#include <isl/ctx.h>
13
#include <isl_stream_private.h>
14
#include <isl/map.h>
15
#include <isl/aff.h>
16
#include <isl_val_private.h>
17
18
struct isl_keyword {
19
  char      *name;
20
  enum isl_token_type type;
21
};
22
23
static int same_name(const void *entry, const void *val)
24
0
{
25
0
  const struct isl_keyword *keyword = (const struct isl_keyword *)entry;
26
0
27
0
  return !strcmp(keyword->name, val);
28
0
}
29
30
enum isl_token_type isl_stream_register_keyword(__isl_keep isl_stream *s,
31
  const char *name)
32
0
{
33
0
  struct isl_hash_table_entry *entry;
34
0
  struct isl_keyword *keyword;
35
0
  uint32_t name_hash;
36
0
37
0
  if (!s->keywords) {
38
0
    s->keywords = isl_hash_table_alloc(s->ctx, 10);
39
0
    if (!s->keywords)
40
0
      return ISL_TOKEN_ERROR;
41
0
    s->next_type = ISL_TOKEN_LAST;
42
0
  }
43
0
44
0
  name_hash = isl_hash_string(isl_hash_init(), name);
45
0
46
0
  entry = isl_hash_table_find(s->ctx, s->keywords, name_hash,
47
0
          same_name, name, 1);
48
0
  if (!entry)
49
0
    return ISL_TOKEN_ERROR;
50
0
  if (entry->data) {
51
0
    keyword = entry->data;
52
0
    return keyword->type;
53
0
  }
54
0
55
0
  keyword = isl_calloc_type(s->ctx, struct isl_keyword);
56
0
  if (!keyword)
57
0
    return ISL_TOKEN_ERROR;
58
0
  keyword->type = s->next_type++;
59
0
  keyword->name = strdup(name);
60
0
  if (!keyword->name) {
61
0
    free(keyword);
62
0
    return ISL_TOKEN_ERROR;
63
0
  }
64
0
  entry->data = keyword;
65
0
66
0
  return keyword->type;
67
0
}
68
69
struct isl_token *isl_token_new(isl_ctx *ctx,
70
  int line, int col, unsigned on_new_line)
71
73.4k
{
72
73.4k
  struct isl_token *tok = isl_alloc_type(ctx, struct isl_token);
73
73.4k
  if (!tok)
74
0
    return NULL;
75
73.4k
  tok->line = line;
76
73.4k
  tok->col = col;
77
73.4k
  tok->on_new_line = on_new_line;
78
73.4k
  tok->is_keyword = 0;
79
73.4k
  tok->u.s = NULL;
80
73.4k
  return tok;
81
73.4k
}
82
83
/* Return the type of "tok".
84
 */
85
int isl_token_get_type(struct isl_token *tok)
86
0
{
87
0
  return tok ? tok->type : ISL_TOKEN_ERROR;
88
0
}
89
90
/* Given a token of type ISL_TOKEN_VALUE, return the value it represents.
91
 */
92
__isl_give isl_val *isl_token_get_val(isl_ctx *ctx, struct isl_token *tok)
93
0
{
94
0
  if (!tok)
95
0
    return NULL;
96
0
  if (tok->type != ISL_TOKEN_VALUE)
97
0
    isl_die(ctx, isl_error_invalid, "not a value token",
98
0
      return NULL);
99
0
100
0
  return isl_val_int_from_isl_int(ctx, tok->u.v);
101
0
}
102
103
/* Given a token with a string representation, return a copy of this string.
104
 */
105
__isl_give char *isl_token_get_str(isl_ctx *ctx, struct isl_token *tok)
106
878
{
107
878
  if (!tok)
108
0
    return NULL;
109
878
  if (!tok->u.s)
110
878
    
isl_die0
(ctx, isl_error_invalid,
111
878
      "token does not have a string representation",
112
878
      return NULL);
113
878
114
878
  return strdup(tok->u.s);
115
878
}
116
117
void isl_token_free(struct isl_token *tok)
118
81.9k
{
119
81.9k
  if (!tok)
120
8.49k
    return;
121
73.4k
  if (tok->type == ISL_TOKEN_VALUE)
122
73.4k
    
isl_int_clear10.7k
(tok->u.v);
123
73.4k
  else 
if (62.6k
tok->type == ISL_TOKEN_MAP62.6k
)
124
167
    isl_map_free(tok->u.map);
125
62.5k
  else if (tok->type == ISL_TOKEN_AFF)
126
278
    isl_pw_aff_free(tok->u.pwaff);
127
62.2k
  else
128
62.2k
    free(tok->u.s);
129
73.4k
  free(tok);
130
73.4k
}
131
132
void isl_stream_error(__isl_keep isl_stream *s, struct isl_token *tok,
133
  char *msg)
134
6
{
135
6
  int line = tok ? tok->line : 
s->line0
;
136
6
  int col = tok ? tok->col : 
s->col0
;
137
6
  fprintf(stderr, "syntax error (%d, %d): %s\n", line, col, msg);
138
6
  if (tok) {
139
6
    if (tok->type < 256)
140
2
      fprintf(stderr, "got '%c'\n", tok->type);
141
4
    else if (tok->type == ISL_TOKEN_IDENT)
142
0
      fprintf(stderr, "got ident '%s'\n", tok->u.s);
143
4
    else if (tok->is_keyword)
144
0
      fprintf(stderr, "got keyword '%s'\n", tok->u.s);
145
4
    else if (tok->type == ISL_TOKEN_VALUE) {
146
0
      fprintf(stderr, "got value '");
147
0
      isl_int_print(stderr, tok->u.v, 0);
148
0
      fprintf(stderr, "'\n");
149
4
    } else if (tok->type == ISL_TOKEN_MAP) {
150
0
      isl_printer *p;
151
0
      fprintf(stderr, "got map '");
152
0
      p = isl_printer_to_file(s->ctx, stderr);
153
0
      p = isl_printer_print_map(p, tok->u.map);
154
0
      isl_printer_free(p);
155
0
      fprintf(stderr, "'\n");
156
4
    } else if (tok->type == ISL_TOKEN_AFF) {
157
0
      isl_printer *p;
158
0
      fprintf(stderr, "got affine expression '");
159
0
      p = isl_printer_to_file(s->ctx, stderr);
160
0
      p = isl_printer_print_pw_aff(p, tok->u.pwaff);
161
0
      isl_printer_free(p);
162
0
      fprintf(stderr, "'\n");
163
4
    } else if (tok->u.s)
164
4
      fprintf(stderr, "got token '%s'\n", tok->u.s);
165
0
    else
166
0
      fprintf(stderr, "got token type %d\n", tok->type);
167
6
  }
168
6
}
169
170
static __isl_give isl_stream* isl_stream_new(struct isl_ctx *ctx)
171
3.55k
{
172
3.55k
  int i;
173
3.55k
  isl_stream *s = isl_calloc_type(ctx, struct isl_stream);
174
3.55k
  if (!s)
175
0
    return NULL;
176
3.55k
  s->ctx = ctx;
177
3.55k
  isl_ctx_ref(s->ctx);
178
3.55k
  s->file = NULL;
179
3.55k
  s->str = NULL;
180
3.55k
  s->len = 0;
181
3.55k
  s->line = 1;
182
3.55k
  s->col = 1;
183
3.55k
  s->eof = 0;
184
3.55k
  s->last_line = 0;
185
3.55k
  s->c = -1;
186
3.55k
  s->n_un = 0;
187
21.3k
  for (i = 0; i < 5; 
++i17.7k
)
188
17.7k
    s->tokens[i] = NULL;
189
3.55k
  s->n_token = 0;
190
3.55k
  s->keywords = NULL;
191
3.55k
  s->size = 256;
192
3.55k
  s->buffer = isl_alloc_array(ctx, char, s->size);
193
3.55k
  if (!s->buffer)
194
0
    goto error;
195
3.55k
  return s;
196
0
error:
197
0
  isl_stream_free(s);
198
0
  return NULL;
199
3.55k
}
200
201
__isl_give isl_stream* isl_stream_new_file(struct isl_ctx *ctx, FILE *file)
202
112
{
203
112
  isl_stream *s = isl_stream_new(ctx);
204
112
  if (!s)
205
0
    return NULL;
206
112
  s->file = file;
207
112
  return s;
208
112
}
209
210
__isl_give isl_stream* isl_stream_new_str(struct isl_ctx *ctx, const char *str)
211
3.44k
{
212
3.44k
  isl_stream *s;
213
3.44k
  if (!str)
214
0
    return NULL;
215
3.44k
  s = isl_stream_new(ctx);
216
3.44k
  if (!s)
217
0
    return NULL;
218
3.44k
  s->str = str;
219
3.44k
  return s;
220
3.44k
}
221
222
/* Read a character from the stream and advance s->line and s->col
223
 * to point to the next character.
224
 */
225
static int stream_getc(__isl_keep isl_stream *s)
226
211k
{
227
211k
  int c;
228
211k
  if (s->eof)
229
512
    return -1;
230
210k
  if (s->n_un)
231
58.3k
    return s->c = s->un[--s->n_un];
232
152k
  if (s->file)
233
10.6k
    c = fgetc(s->file);
234
141k
  else {
235
141k
    c = *s->str++;
236
141k
    if (c == '\0')
237
454
      c = -1;
238
141k
  }
239
152k
  if (c == -1)
240
454
    s->eof = 1;
241
151k
  else if (c == '\n') {
242
613
    s->line++;
243
613
    s->col = 1;
244
613
  } else
245
151k
    s->col++;
246
152k
  s->c = c;
247
152k
  return c;
248
152k
}
249
250
static void isl_stream_ungetc(__isl_keep isl_stream *s, int c)
251
58.3k
{
252
58.3k
  isl_assert(s->ctx, s->n_un < 5, return);
253
58.3k
  s->un[s->n_un++] = c;
254
58.3k
  s->c = -1;
255
58.3k
}
256
257
/* Read a character from the stream, skipping pairs of '\\' and '\n'.
258
 * Set s->start_line and s->start_col to the line and column
259
 * of the returned character.
260
 */
261
static int isl_stream_getc(__isl_keep isl_stream *s)
262
211k
{
263
211k
  int c;
264
211k
265
211k
  do {
266
211k
    s->start_line = s->line;
267
211k
    s->start_col = s->col;
268
211k
    c = stream_getc(s);
269
211k
    if (c != '\\')
270
211k
      return c;
271
0
    c = stream_getc(s);
272
0
  } while (c == '\n');
273
211k
274
211k
  isl_stream_ungetc(s, c);
275
0
276
0
  return '\\';
277
211k
}
278
279
static int isl_stream_push_char(__isl_keep isl_stream *s, int c)
280
89.9k
{
281
89.9k
  if (s->len >= s->size) {
282
0
    char *buffer;
283
0
    s->size = (3*s->size)/2;
284
0
    buffer = isl_realloc_array(s->ctx, s->buffer, char, s->size);
285
0
    if (!buffer)
286
0
      return -1;
287
0
    s->buffer = buffer;
288
0
  }
289
89.9k
  s->buffer[s->len++] = c;
290
89.9k
  return 0;
291
89.9k
}
292
293
void isl_stream_push_token(__isl_keep isl_stream *s, struct isl_token *tok)
294
231k
{
295
231k
  isl_assert(s->ctx, s->n_token < 5, return);
296
231k
  s->tokens[s->n_token++] = tok;
297
231k
}
298
299
static enum isl_token_type check_keywords(__isl_keep isl_stream *s)
300
21.1k
{
301
21.1k
  struct isl_hash_table_entry *entry;
302
21.1k
  struct isl_keyword *keyword;
303
21.1k
  uint32_t name_hash;
304
21.1k
305
21.1k
  if (!strcasecmp(s->buffer, "exists"))
306
134
    return ISL_TOKEN_EXISTS;
307
20.9k
  if (!strcasecmp(s->buffer, "and"))
308
2.02k
    return ISL_TOKEN_AND;
309
18.9k
  if (!strcasecmp(s->buffer, "or"))
310
156
    return ISL_TOKEN_OR;
311
18.8k
  if (!strcasecmp(s->buffer, "implies"))
312
1
    return ISL_TOKEN_IMPLIES;
313
18.8k
  if (!strcasecmp(s->buffer, "not"))
314
1
    return ISL_TOKEN_NOT;
315
18.8k
  if (!strcasecmp(s->buffer, "infty"))
316
83
    return ISL_TOKEN_INFTY;
317
18.7k
  if (!strcasecmp(s->buffer, "infinity"))
318
0
    return ISL_TOKEN_INFTY;
319
18.7k
  if (!strcasecmp(s->buffer, "NaN"))
320
82
    return ISL_TOKEN_NAN;
321
18.6k
  if (!strcasecmp(s->buffer, "min"))
322
5
    return ISL_TOKEN_MIN;
323
18.6k
  if (!strcasecmp(s->buffer, "max"))
324
1
    return ISL_TOKEN_MAX;
325
18.6k
  if (!strcasecmp(s->buffer, "rat"))
326
22
    return ISL_TOKEN_RAT;
327
18.6k
  if (!strcasecmp(s->buffer, "true"))
328
1
    return ISL_TOKEN_TRUE;
329
18.6k
  if (!strcasecmp(s->buffer, "false"))
330
12
    return ISL_TOKEN_FALSE;
331
18.5k
  if (!strcasecmp(s->buffer, "ceild"))
332
0
    return ISL_TOKEN_CEILD;
333
18.5k
  if (!strcasecmp(s->buffer, "floord"))
334
1
    return ISL_TOKEN_FLOORD;
335
18.5k
  if (!strcasecmp(s->buffer, "mod"))
336
81
    return ISL_TOKEN_MOD;
337
18.5k
  if (!strcasecmp(s->buffer, "ceil"))
338
0
    return ISL_TOKEN_CEIL;
339
18.5k
  if (!strcasecmp(s->buffer, "floor"))
340
181
    return ISL_TOKEN_FLOOR;
341
18.3k
342
18.3k
  if (!s->keywords)
343
18.3k
    return ISL_TOKEN_IDENT;
344
0
345
0
  name_hash = isl_hash_string(isl_hash_init(), s->buffer);
346
0
  entry = isl_hash_table_find(s->ctx, s->keywords, name_hash, same_name,
347
0
          s->buffer, 0);
348
0
  if (entry) {
349
0
    keyword = entry->data;
350
0
    return keyword->type;
351
0
  }
352
0
353
0
  return ISL_TOKEN_IDENT;
354
0
}
355
356
int isl_stream_skip_line(__isl_keep isl_stream *s)
357
14
{
358
14
  int c;
359
14
360
478
  while ((c = isl_stream_getc(s)) != -1 && c != '\n')
361
464
    /* nothing */
362
464
    ;
363
14
364
14
  return c == -1 ? 
-10
: 0;
365
14
}
366
367
static struct isl_token *next_token(__isl_keep isl_stream *s, int same_line)
368
304k
{
369
304k
  int c;
370
304k
  struct isl_token *tok = NULL;
371
304k
  int line, col;
372
304k
  int old_line = s->last_line;
373
304k
374
304k
  if (s->n_token) {
375
231k
    if (same_line && 
s->tokens[s->n_token - 1]->on_new_line105
)
376
0
      return NULL;
377
231k
    return s->tokens[--s->n_token];
378
231k
  }
379
73.8k
380
73.8k
  if (same_line && 
s->c == '\n'210
)
381
0
    return NULL;
382
73.8k
383
73.8k
  s->len = 0;
384
73.8k
385
73.8k
  /* skip spaces and comment lines */
386
118k
  while ((c = isl_stream_getc(s)) != -1) {
387
118k
    if (c == '#') {
388
14
      if (isl_stream_skip_line(s) < 0)
389
0
        break;
390
14
      c = '\n';
391
14
      if (same_line)
392
0
        break;
393
118k
    } else if (!isspace(c) || 
(44.8k
same_line44.8k
&&
c == '\n'212
))
394
73.3k
      break;
395
118k
  }
396
73.8k
397
73.8k
  line = s->start_line;
398
73.8k
  col = s->start_col;
399
73.8k
400
73.8k
  if (c == -1 || 
(73.3k
same_line73.3k
&&
c == '\n'210
))
401
696
    return NULL;
402
73.1k
  s->last_line = line;
403
73.1k
404
73.1k
  if (c == '(' ||
405
73.1k
      
c == ')'72.1k
||
406
73.1k
      
c == '+'71.1k
||
407
73.1k
      
c == '*'69.6k
||
408
73.1k
      
c == '%'69.4k
||
409
73.1k
      
c == '?'69.4k
||
410
73.1k
      
c == '^'69.4k
||
411
73.1k
      
c == '@'69.4k
||
412
73.1k
      
c == '$'69.4k
||
413
73.1k
      
c == ','69.4k
||
414
73.1k
      
c == '.'65.9k
||
415
73.1k
      
c == ';'65.9k
||
416
73.1k
      
c == '['65.1k
||
417
73.1k
      
c == ']'57.4k
||
418
73.1k
      
c == '{'49.8k
||
419
73.1k
      
c == '}'46.7k
) {
420
29.5k
    tok = isl_token_new(s->ctx, line, col, old_line != line);
421
29.5k
    if (!tok)
422
0
      return NULL;
423
29.5k
    tok->type = (enum isl_token_type)c;
424
29.5k
    return tok;
425
29.5k
  }
426
43.6k
  if (c == '-') {
427
5.45k
    int c;
428
5.45k
    if ((c = isl_stream_getc(s)) == '>') {
429
3.43k
      tok = isl_token_new(s->ctx, line, col, old_line != line);
430
3.43k
      if (!tok)
431
0
        return NULL;
432
3.43k
      tok->u.s = strdup("->");
433
3.43k
      tok->type = ISL_TOKEN_TO;
434
3.43k
      return tok;
435
3.43k
    }
436
2.01k
    if (c != -1)
437
2.01k
      isl_stream_ungetc(s, c);
438
2.01k
    if (!isdigit(c)) {
439
722
      tok = isl_token_new(s->ctx, line, col, old_line != line);
440
722
      if (!tok)
441
0
        return NULL;
442
722
      tok->type = (enum isl_token_type) '-';
443
722
      return tok;
444
722
    }
445
2.01k
  }
446
39.4k
  if (c == '-' || 
isdigit(c)38.1k
) {
447
10.7k
    int minus = c == '-';
448
10.7k
    tok = isl_token_new(s->ctx, line, col, old_line != line);
449
10.7k
    if (!tok)
450
0
      return NULL;
451
10.7k
    tok->type = ISL_TOKEN_VALUE;
452
10.7k
    isl_int_init(tok->u.v);
453
10.7k
    if (isl_stream_push_char(s, c))
454
0
      goto error;
455
15.5k
    
while (10.7k
(c = isl_stream_getc(s)) != -1 &&
isdigit(c)15.3k
)
456
4.81k
      if (isl_stream_push_char(s, c))
457
0
        goto error;
458
10.7k
    if (c != -1)
459
10.5k
      isl_stream_ungetc(s, c);
460
10.7k
    isl_stream_push_char(s, '\0');
461
10.7k
    isl_int_read(tok->u.v, s->buffer);
462
10.7k
    if (minus && 
isl_int_is_zero1.29k
(tok->u.v)) {
463
1
      tok->col++;
464
1
      tok->on_new_line = 0;
465
1
      isl_stream_push_token(s, tok);
466
1
      tok = isl_token_new(s->ctx, line, col, old_line != line);
467
1
      if (!tok)
468
0
        return NULL;
469
1
      tok->type = (enum isl_token_type) '-';
470
1
    }
471
10.7k
    return tok;
472
28.7k
  }
473
28.7k
  if (isalpha(c) || 
c == '_'7.62k
) {
474
21.1k
    tok = isl_token_new(s->ctx, line, col, old_line != line);
475
21.1k
    if (!tok)
476
0
      return NULL;
477
21.1k
    isl_stream_push_char(s, c);
478
42.3k
    while ((c = isl_stream_getc(s)) != -1 &&
479
42.3k
        
(42.2k
isalnum(c)42.2k
||
c == '_'22.3k
))
480
21.2k
      isl_stream_push_char(s, c);
481
21.1k
    if (c != -1)
482
20.9k
      isl_stream_ungetc(s, c);
483
21.2k
    while ((c = isl_stream_getc(s)) != -1 && 
c == '\''21.1k
)
484
155
      isl_stream_push_char(s, c);
485
21.1k
    if (c != -1)
486
20.9k
      isl_stream_ungetc(s, c);
487
21.1k
    isl_stream_push_char(s, '\0');
488
21.1k
    tok->type = check_keywords(s);
489
21.1k
    if (tok->type != ISL_TOKEN_IDENT)
490
2.78k
      tok->is_keyword = 1;
491
21.1k
    tok->u.s = strdup(s->buffer);
492
21.1k
    if (!tok->u.s)
493
0
      goto error;
494
21.1k
    return tok;
495
21.1k
  }
496
7.62k
  if (c == '"') {
497
0
    tok = isl_token_new(s->ctx, line, col, old_line != line);
498
0
    if (!tok)
499
0
      return NULL;
500
0
    tok->type = ISL_TOKEN_STRING;
501
0
    tok->u.s = NULL;
502
0
    while ((c = isl_stream_getc(s)) != -1 && c != '"' && c != '\n')
503
0
      isl_stream_push_char(s, c);
504
0
    if (c != '"') {
505
0
      isl_stream_error(s, NULL, "unterminated string");
506
0
      goto error;
507
0
    }
508
0
    isl_stream_push_char(s, '\0');
509
0
    tok->u.s = strdup(s->buffer);
510
0
    return tok;
511
0
  }
512
7.62k
  if (c == '=') {
513
680
    int c;
514
680
    tok = isl_token_new(s->ctx, line, col, old_line != line);
515
680
    if (!tok)
516
0
      return NULL;
517
680
    if ((c = isl_stream_getc(s)) == '=') {
518
0
      tok->u.s = strdup("==");
519
0
      tok->type = ISL_TOKEN_EQ_EQ;
520
0
      return tok;
521
0
    }
522
680
    if (c != -1)
523
680
      isl_stream_ungetc(s, c);
524
680
    tok->type = (enum isl_token_type) '=';
525
680
    return tok;
526
680
  }
527
6.94k
  if (c == ':') {
528
2.16k
    int c;
529
2.16k
    tok = isl_token_new(s->ctx, line, col, old_line != line);
530
2.16k
    if (!tok)
531
0
      return NULL;
532
2.16k
    if ((c = isl_stream_getc(s)) == '=') {
533
0
      tok->u.s = strdup(":=");
534
0
      tok->type = ISL_TOKEN_DEF;
535
0
      return tok;
536
0
    }
537
2.16k
    if (c != -1)
538
2.16k
      isl_stream_ungetc(s, c);
539
2.16k
    tok->type = (enum isl_token_type) ':';
540
2.16k
    return tok;
541
2.16k
  }
542
4.78k
  if (c == '>') {
543
1.47k
    int c;
544
1.47k
    tok = isl_token_new(s->ctx, line, col, old_line != line);
545
1.47k
    if (!tok)
546
0
      return NULL;
547
1.47k
    if ((c = isl_stream_getc(s)) == '=') {
548
1.34k
      tok->u.s = strdup(">=");
549
1.34k
      tok->type = ISL_TOKEN_GE;
550
1.34k
      return tok;
551
1.34k
    } else 
if (128
c == '>'128
) {
552
3
      if ((c = isl_stream_getc(s)) == '=') {
553
1
        tok->u.s = strdup(">>=");
554
1
        tok->type = ISL_TOKEN_LEX_GE;
555
1
        return tok;
556
1
      }
557
2
      tok->u.s = strdup(">>");
558
2
      tok->type = ISL_TOKEN_LEX_GT;
559
125
    } else {
560
125
      tok->u.s = strdup(">");
561
125
      tok->type = ISL_TOKEN_GT;
562
125
    }
563
1.47k
    
if (127
c != -1127
)
564
127
      isl_stream_ungetc(s, c);
565
127
    return tok;
566
3.31k
  }
567
3.31k
  if (c == '<') {
568
2.77k
    int c;
569
2.77k
    tok = isl_token_new(s->ctx, line, col, old_line != line);
570
2.77k
    if (!tok)
571
0
      return NULL;
572
2.77k
    if ((c = isl_stream_getc(s)) == '=') {
573
2.38k
      tok->u.s = strdup("<=");
574
2.38k
      tok->type = ISL_TOKEN_LE;
575
2.38k
      return tok;
576
2.38k
    } else 
if (394
c == '<'394
) {
577
3
      if ((c = isl_stream_getc(s)) == '=') {
578
1
        tok->u.s = strdup("<<=");
579
1
        tok->type = ISL_TOKEN_LEX_LE;
580
1
        return tok;
581
1
      }
582
2
      tok->u.s = strdup("<<");
583
2
      tok->type = ISL_TOKEN_LEX_LT;
584
391
    } else {
585
391
      tok->u.s = strdup("<");
586
391
      tok->type = ISL_TOKEN_LT;
587
391
    }
588
2.77k
    
if (393
c != -1393
)
589
393
      isl_stream_ungetc(s, c);
590
393
    return tok;
591
537
  }
592
537
  if (c == '&') {
593
104
    tok = isl_token_new(s->ctx, line, col, old_line != line);
594
104
    if (!tok)
595
0
      return NULL;
596
104
    tok->type = ISL_TOKEN_AND;
597
104
    if ((c = isl_stream_getc(s)) != '&' && 
c != -197
) {
598
97
      tok->u.s = strdup("&");
599
97
      isl_stream_ungetc(s, c);
600
97
    } else
601
7
      tok->u.s = strdup("&&");
602
104
    return tok;
603
104
  }
604
433
  if (c == '|') {
605
1
    tok = isl_token_new(s->ctx, line, col, old_line != line);
606
1
    if (!tok)
607
0
      return NULL;
608
1
    tok->type = ISL_TOKEN_OR;
609
1
    if ((c = isl_stream_getc(s)) != '|' && 
c != -10
) {
610
0
      tok->u.s = strdup("|");
611
0
      isl_stream_ungetc(s, c);
612
0
    } else
613
1
      tok->u.s = strdup("||");
614
1
    return tok;
615
1
  }
616
432
  if (c == '/') {
617
420
    tok = isl_token_new(s->ctx, line, col, old_line != line);
618
420
    if (!tok)
619
0
      return NULL;
620
420
    if ((c = isl_stream_getc(s)) != '\\' && c != -1) {
621
420
      tok->type = (enum isl_token_type) '/';
622
420
      isl_stream_ungetc(s, c);
623
420
    } else {
624
0
      tok->u.s = strdup("/\\");
625
0
      tok->type = ISL_TOKEN_AND;
626
0
    }
627
420
    return tok;
628
420
  }
629
12
  if (c == '\\') {
630
0
    tok = isl_token_new(s->ctx, line, col, old_line != line);
631
0
    if (!tok)
632
0
      return NULL;
633
0
    if ((c = isl_stream_getc(s)) != '/' && c != -1) {
634
0
      tok->type = (enum isl_token_type) '\\';
635
0
      isl_stream_ungetc(s, c);
636
0
    } else {
637
0
      tok->u.s = strdup("\\/");
638
0
      tok->type = ISL_TOKEN_OR;
639
0
    }
640
0
    return tok;
641
0
  }
642
12
  if (c == '!') {
643
12
    tok = isl_token_new(s->ctx, line, col, old_line != line);
644
12
    if (!tok)
645
0
      return NULL;
646
12
    if ((c = isl_stream_getc(s)) == '=') {
647
12
      tok->u.s = strdup("!=");
648
12
      tok->type = ISL_TOKEN_NE;
649
12
      return tok;
650
12
    } else {
651
0
      tok->type = ISL_TOKEN_NOT;
652
0
      tok->u.s = strdup("!");
653
0
    }
654
12
    
if (0
c != -10
)
655
0
      isl_stream_ungetc(s, c);
656
0
    return tok;
657
0
  }
658
0
659
0
  tok = isl_token_new(s->ctx, line, col, old_line != line);
660
0
  if (!tok)
661
0
    return NULL;
662
0
  tok->type = ISL_TOKEN_UNKNOWN;
663
0
  return tok;
664
0
error:
665
0
  isl_token_free(tok);
666
0
  return NULL;
667
0
}
668
669
struct isl_token *isl_stream_next_token(__isl_keep isl_stream *s)
670
304k
{
671
304k
  return next_token(s, 0);
672
304k
}
673
674
struct isl_token *isl_stream_next_token_on_same_line(__isl_keep isl_stream *s)
675
315
{
676
315
  return next_token(s, 1);
677
315
}
678
679
int isl_stream_eat_if_available(__isl_keep isl_stream *s, int type)
680
103k
{
681
103k
  struct isl_token *tok;
682
103k
683
103k
  tok = isl_stream_next_token(s);
684
103k
  if (!tok)
685
380
    return 0;
686
103k
  if (tok->type == type) {
687
5.07k
    isl_token_free(tok);
688
5.07k
    return 1;
689
5.07k
  }
690
98.0k
  isl_stream_push_token(s, tok);
691
98.0k
  return 0;
692
98.0k
}
693
694
int isl_stream_next_token_is(__isl_keep isl_stream *s, int type)
695
46.0k
{
696
46.0k
  struct isl_token *tok;
697
46.0k
  int r;
698
46.0k
699
46.0k
  tok = isl_stream_next_token(s);
700
46.0k
  if (!tok)
701
104
    return 0;
702
45.9k
  r = tok->type == type;
703
45.9k
  isl_stream_push_token(s, tok);
704
45.9k
  return r;
705
45.9k
}
706
707
char *isl_stream_read_ident_if_available(__isl_keep isl_stream *s)
708
0
{
709
0
  struct isl_token *tok;
710
0
711
0
  tok = isl_stream_next_token(s);
712
0
  if (!tok)
713
0
    return NULL;
714
0
  if (tok->type == ISL_TOKEN_IDENT) {
715
0
    char *ident = strdup(tok->u.s);
716
0
    isl_token_free(tok);
717
0
    return ident;
718
0
  }
719
0
  isl_stream_push_token(s, tok);
720
0
  return NULL;
721
0
}
722
723
int isl_stream_eat(__isl_keep isl_stream *s, int type)
724
18.6k
{
725
18.6k
  struct isl_token *tok;
726
18.6k
727
18.6k
  tok = isl_stream_next_token(s);
728
18.6k
  if (!tok) {
729
0
    if (s->eof)
730
0
      isl_stream_error(s, NULL, "unexpected EOF");
731
0
    return -1;
732
0
  }
733
18.6k
  if (tok->type == type) {
734
18.6k
    isl_token_free(tok);
735
18.6k
    return 0;
736
18.6k
  }
737
2
  isl_stream_error(s, tok, "expecting other token");
738
2
  isl_stream_push_token(s, tok);
739
2
  return -1;
740
2
}
741
742
int isl_stream_is_empty(__isl_keep isl_stream *s)
743
0
{
744
0
  struct isl_token *tok;
745
0
746
0
  tok = isl_stream_next_token(s);
747
0
748
0
  if (!tok)
749
0
    return 1;
750
0
751
0
  isl_stream_push_token(s, tok);
752
0
  return 0;
753
0
}
754
755
static isl_stat free_keyword(void **p, void *user)
756
0
{
757
0
  struct isl_keyword *keyword = *p;
758
0
759
0
  free(keyword->name);
760
0
  free(keyword);
761
0
762
0
  return isl_stat_ok;
763
0
}
764
765
void isl_stream_flush_tokens(__isl_keep isl_stream *s)
766
0
{
767
0
  int i;
768
0
769
0
  if (!s)
770
0
    return;
771
0
  for (i = 0; i < s->n_token; ++i)
772
0
    isl_token_free(s->tokens[i]);
773
0
  s->n_token = 0;
774
0
}
775
776
isl_ctx *isl_stream_get_ctx(__isl_keep isl_stream *s)
777
0
{
778
0
  return s ? s->ctx : NULL;
779
0
}
780
781
void isl_stream_free(__isl_take isl_stream *s)
782
3.55k
{
783
3.55k
  if (!s)
784
0
    return;
785
3.55k
  free(s->buffer);
786
3.55k
  if (s->n_token != 0) {
787
3
    struct isl_token *tok = isl_stream_next_token(s);
788
3
    isl_stream_error(s, tok, "unexpected token");
789
3
    isl_token_free(tok);
790
3
  }
791
3.55k
  if (s->keywords) {
792
0
    isl_hash_table_foreach(s->ctx, s->keywords, &free_keyword, NULL);
793
0
    isl_hash_table_free(s->ctx, s->keywords);
794
0
  }
795
3.55k
  free(s->yaml_state);
796
3.55k
  free(s->yaml_indent);
797
3.55k
  isl_ctx_deref(s->ctx);
798
3.55k
  free(s);
799
3.55k
}
800
801
/* Push "state" onto the stack of currently active YAML elements.
802
 * The caller is responsible for setting the corresponding indentation.
803
 * Return 0 on success and -1 on failure.
804
 */
805
static int push_state(__isl_keep isl_stream *s, enum isl_yaml_state state)
806
0
{
807
0
  if (s->yaml_size < s->yaml_depth + 1) {
808
0
    int *indent;
809
0
    enum isl_yaml_state *state;
810
0
811
0
    state = isl_realloc_array(s->ctx, s->yaml_state,
812
0
          enum isl_yaml_state, s->yaml_depth + 1);
813
0
    if (!state)
814
0
      return -1;
815
0
    s->yaml_state = state;
816
0
817
0
    indent = isl_realloc_array(s->ctx, s->yaml_indent,
818
0
          int, s->yaml_depth + 1);
819
0
    if (!indent)
820
0
      return -1;
821
0
    s->yaml_indent = indent;
822
0
823
0
    s->yaml_size = s->yaml_depth + 1;
824
0
  }
825
0
826
0
  s->yaml_state[s->yaml_depth] = state;
827
0
  s->yaml_depth++;
828
0
829
0
  return 0;
830
0
}
831
832
/* Remove the innermost active YAML element from the stack.
833
 * Return 0 on success and -1 on failure.
834
 */
835
static int pop_state(__isl_keep isl_stream *s)
836
0
{
837
0
  if (!s)
838
0
    return -1;
839
0
  if (s->yaml_depth < 1)
840
0
    isl_die(isl_stream_get_ctx(s), isl_error_invalid,
841
0
      "not in YAML construct", return -1);
842
0
843
0
  s->yaml_depth--;
844
0
845
0
  return 0;
846
0
}
847
848
/* Set the state of the innermost active YAML element to "state".
849
 * Return 0 on success and -1 on failure.
850
 */
851
static int update_state(__isl_keep isl_stream *s, enum isl_yaml_state state)
852
0
{
853
0
  if (!s)
854
0
    return -1;
855
0
  if (s->yaml_depth < 1)
856
0
    isl_die(isl_stream_get_ctx(s), isl_error_invalid,
857
0
      "not in YAML construct", return -1);
858
0
859
0
  s->yaml_state[s->yaml_depth - 1] = state;
860
0
861
0
  return 0;
862
0
}
863
864
/* Return the state of the innermost active YAML element.
865
 * Return isl_yaml_none if we are not inside any YAML element.
866
 */
867
static enum isl_yaml_state current_state(__isl_keep isl_stream *s)
868
0
{
869
0
  if (!s)
870
0
    return isl_yaml_none;
871
0
  if (s->yaml_depth < 1)
872
0
    return isl_yaml_none;
873
0
  return s->yaml_state[s->yaml_depth - 1];
874
0
}
875
876
/* Set the indentation of the innermost active YAML element to "indent".
877
 * If "indent" is equal to ISL_YAML_INDENT_FLOW, then this means
878
 * that the current elemient is in flow format.
879
 */
880
static int set_yaml_indent(__isl_keep isl_stream *s, int indent)
881
0
{
882
0
  if (s->yaml_depth < 1)
883
0
    isl_die(s->ctx, isl_error_internal,
884
0
      "not in YAML element", return -1);
885
0
886
0
  s->yaml_indent[s->yaml_depth - 1] = indent;
887
0
888
0
  return 0;
889
0
}
890
891
/* Return the indentation of the innermost active YAML element
892
 * of -1 on error.
893
 */
894
static int get_yaml_indent(__isl_keep isl_stream *s)
895
0
{
896
0
  if (s->yaml_depth < 1)
897
0
    isl_die(s->ctx, isl_error_internal,
898
0
      "not in YAML element", return -1);
899
0
900
0
  return s->yaml_indent[s->yaml_depth - 1];
901
0
}
902
903
/* Move to the next state at the innermost level.
904
 * Return 1 if successful.
905
 * Return 0 if we are at the end of the innermost level.
906
 * Return -1 on error.
907
 *
908
 * If we are in state isl_yaml_mapping_key_start, then we have just
909
 * started a mapping and we are expecting a key.  If the mapping started
910
 * with a '{', then we check if the next token is a '}'.  If so,
911
 * then the mapping is empty and there is no next state at this level.
912
 * Otherwise, we assume that there is at least one key (the one from
913
 * which we derived the indentation in isl_stream_yaml_read_start_mapping.
914
 *
915
 * If we are in state isl_yaml_mapping_key, then the we expect a colon
916
 * followed by a value, so there is always a next state unless
917
 * some error occurs.
918
 *
919
 * If we are in state isl_yaml_mapping_val, then there may or may
920
 * not be a subsequent key in the same mapping.
921
 * In flow format, the next key is preceded by a comma.
922
 * In block format, the next key has the same indentation as the first key.
923
 * If the first token has a smaller indentation, then we have reached
924
 * the end of the current mapping.
925
 *
926
 * If we are in state isl_yaml_sequence_start, then we have just
927
 * started a sequence.  If the sequence started with a '[',
928
 * then we check if the next token is a ']'.  If so, then the sequence
929
 * is empty and there is no next state at this level.
930
 * Otherwise, we assume that there is at least one element in the sequence
931
 * (the one from which we derived the indentation in
932
 * isl_stream_yaml_read_start_sequence.
933
 *
934
 * If we are in state isl_yaml_sequence, then there may or may
935
 * not be a subsequent element in the same sequence.
936
 * In flow format, the next element is preceded by a comma.
937
 * In block format, the next element is introduced by a dash with
938
 * the same indentation as that of the first element.
939
 * If the first token is not a dash or if it has a smaller indentation,
940
 * then we have reached the end of the current sequence.
941
 */
942
int isl_stream_yaml_next(__isl_keep isl_stream *s)
943
0
{
944
0
  struct isl_token *tok;
945
0
  enum isl_yaml_state state;
946
0
  int indent;
947
0
948
0
  state = current_state(s);
949
0
  if (state == isl_yaml_none)
950
0
    isl_die(s->ctx, isl_error_invalid,
951
0
      "not in YAML element", return -1);
952
0
  switch (state) {
953
0
  case isl_yaml_mapping_key_start:
954
0
    if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW &&
955
0
        isl_stream_next_token_is(s, '}'))
956
0
      return 0;
957
0
    if (update_state(s, isl_yaml_mapping_key) < 0)
958
0
      return -1;
959
0
    return 1;
960
0
  case isl_yaml_mapping_key:
961
0
    tok = isl_stream_next_token(s);
962
0
    if (!tok) {
963
0
      if (s->eof)
964
0
        isl_stream_error(s, NULL, "unexpected EOF");
965
0
      return -1;
966
0
    }
967
0
    if (tok->type == ':') {
968
0
      isl_token_free(tok);
969
0
      if (update_state(s, isl_yaml_mapping_val) < 0)
970
0
        return -1;
971
0
      return 1;
972
0
    }
973
0
    isl_stream_error(s, tok, "expecting ':'");
974
0
    isl_stream_push_token(s, tok);
975
0
    return -1;
976
0
  case isl_yaml_mapping_val:
977
0
    if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW) {
978
0
      if (!isl_stream_eat_if_available(s, ','))
979
0
        return 0;
980
0
      if (update_state(s, isl_yaml_mapping_key) < 0)
981
0
        return -1;
982
0
      return 1;
983
0
    }
984
0
    tok = isl_stream_next_token(s);
985
0
    if (!tok)
986
0
      return 0;
987
0
    indent = tok->col - 1;
988
0
    isl_stream_push_token(s, tok);
989
0
    if (indent < get_yaml_indent(s))
990
0
      return 0;
991
0
    if (update_state(s, isl_yaml_mapping_key) < 0)
992
0
      return -1;
993
0
    return 1;
994
0
  case isl_yaml_sequence_start:
995
0
    if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW) {
996
0
      if (isl_stream_next_token_is(s, ']'))
997
0
        return 0;
998
0
      if (update_state(s, isl_yaml_sequence) < 0)
999
0
        return -1;
1000
0
      return 1;
1001
0
    }
1002
0
    tok = isl_stream_next_token(s);
1003
0
    if (!tok) {
1004
0
      if (s->eof)
1005
0
        isl_stream_error(s, NULL, "unexpected EOF");
1006
0
      return -1;
1007
0
    }
1008
0
    if (tok->type == '-') {
1009
0
      isl_token_free(tok);
1010
0
      if (update_state(s, isl_yaml_sequence) < 0)
1011
0
        return -1;
1012
0
      return 1;
1013
0
    }
1014
0
    isl_stream_error(s, tok, "expecting '-'");
1015
0
    isl_stream_push_token(s, tok);
1016
0
    return 0;
1017
0
  case isl_yaml_sequence:
1018
0
    if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW)
1019
0
      return isl_stream_eat_if_available(s, ',');
1020
0
    tok = isl_stream_next_token(s);
1021
0
    if (!tok)
1022
0
      return 0;
1023
0
    indent = tok->col - 1;
1024
0
    if (indent < get_yaml_indent(s) || tok->type != '-') {
1025
0
      isl_stream_push_token(s, tok);
1026
0
      return 0;
1027
0
    }
1028
0
    isl_token_free(tok);
1029
0
    return 1;
1030
0
  default:
1031
0
    isl_die(s->ctx, isl_error_internal,
1032
0
      "unexpected state", return 0);
1033
0
  }
1034
0
}
1035
1036
/* Start reading a YAML mapping.
1037
 * Return 0 on success and -1 on error.
1038
 *
1039
 * If the first token on the stream is a '{' then we remove this token
1040
 * from the stream and keep track of the fact that the mapping
1041
 * is given in flow format.
1042
 * Otherwise, we assume the first token is the first key of the mapping and
1043
 * keep track of its indentation, but keep the token on the stream.
1044
 * In both cases, the next token we expect is the first key of the mapping.
1045
 */
1046
int isl_stream_yaml_read_start_mapping(__isl_keep isl_stream *s)
1047
0
{
1048
0
  struct isl_token *tok;
1049
0
  int indent;
1050
0
1051
0
  if (push_state(s, isl_yaml_mapping_key_start) < 0)
1052
0
    return -1;
1053
0
1054
0
  tok = isl_stream_next_token(s);
1055
0
  if (!tok) {
1056
0
    if (s->eof)
1057
0
      isl_stream_error(s, NULL, "unexpected EOF");
1058
0
    return -1;
1059
0
  }
1060
0
  if (isl_token_get_type(tok) == '{') {
1061
0
    isl_token_free(tok);
1062
0
    return set_yaml_indent(s, ISL_YAML_INDENT_FLOW);
1063
0
  }
1064
0
  indent = tok->col - 1;
1065
0
  isl_stream_push_token(s, tok);
1066
0
1067
0
  return set_yaml_indent(s, indent);
1068
0
}
1069
1070
/* Finish reading a YAML mapping.
1071
 * Return 0 on success and -1 on error.
1072
 *
1073
 * If the mapping started with a '{', then we expect a '}' to close
1074
 * the mapping.
1075
 * Otherwise, we double-check that the next token (if any)
1076
 * has a smaller indentation than that of the current mapping.
1077
 */
1078
int isl_stream_yaml_read_end_mapping(__isl_keep isl_stream *s)
1079
0
{
1080
0
  struct isl_token *tok;
1081
0
  int indent;
1082
0
1083
0
  if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW) {
1084
0
    if (isl_stream_eat(s, '}') < 0)
1085
0
      return -1;
1086
0
    return pop_state(s);
1087
0
  }
1088
0
1089
0
  tok = isl_stream_next_token(s);
1090
0
  if (!tok)
1091
0
    return pop_state(s);
1092
0
1093
0
  indent = tok->col - 1;
1094
0
  isl_stream_push_token(s, tok);
1095
0
1096
0
  if (indent >= get_yaml_indent(s))
1097
0
    isl_die(isl_stream_get_ctx(s), isl_error_invalid,
1098
0
      "mapping not finished", return -1);
1099
0
1100
0
  return pop_state(s);
1101
0
}
1102
1103
/* Start reading a YAML sequence.
1104
 * Return 0 on success and -1 on error.
1105
 *
1106
 * If the first token on the stream is a '[' then we remove this token
1107
 * from the stream and keep track of the fact that the sequence
1108
 * is given in flow format.
1109
 * Otherwise, we assume the first token is the dash that introduces
1110
 * the first element of the sequence and keep track of its indentation,
1111
 * but keep the token on the stream.
1112
 * In both cases, the next token we expect is the first element
1113
 * of the sequence.
1114
 */
1115
int isl_stream_yaml_read_start_sequence(__isl_keep isl_stream *s)
1116
0
{
1117
0
  struct isl_token *tok;
1118
0
  int indent;
1119
0
1120
0
  if (push_state(s, isl_yaml_sequence_start) < 0)
1121
0
    return -1;
1122
0
1123
0
  tok = isl_stream_next_token(s);
1124
0
  if (!tok) {
1125
0
    if (s->eof)
1126
0
      isl_stream_error(s, NULL, "unexpected EOF");
1127
0
    return -1;
1128
0
  }
1129
0
  if (isl_token_get_type(tok) == '[') {
1130
0
    isl_token_free(tok);
1131
0
    return set_yaml_indent(s, ISL_YAML_INDENT_FLOW);
1132
0
  }
1133
0
  indent = tok->col - 1;
1134
0
  isl_stream_push_token(s, tok);
1135
0
1136
0
  return set_yaml_indent(s, indent);
1137
0
}
1138
1139
/* Finish reading a YAML sequence.
1140
 * Return 0 on success and -1 on error.
1141
 *
1142
 * If the sequence started with a '[', then we expect a ']' to close
1143
 * the sequence.
1144
 * Otherwise, we double-check that the next token (if any)
1145
 * is not a dash or that it has a smaller indentation than
1146
 * that of the current sequence.
1147
 */
1148
int isl_stream_yaml_read_end_sequence(__isl_keep isl_stream *s)
1149
0
{
1150
0
  struct isl_token *tok;
1151
0
  int indent;
1152
0
  int dash;
1153
0
1154
0
  if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW) {
1155
0
    if (isl_stream_eat(s, ']') < 0)
1156
0
      return -1;
1157
0
    return pop_state(s);
1158
0
  }
1159
0
1160
0
  tok = isl_stream_next_token(s);
1161
0
  if (!tok)
1162
0
    return pop_state(s);
1163
0
1164
0
  indent = tok->col - 1;
1165
0
  dash = tok->type == '-';
1166
0
  isl_stream_push_token(s, tok);
1167
0
1168
0
  if (indent >= get_yaml_indent(s) && dash)
1169
0
    isl_die(isl_stream_get_ctx(s), isl_error_invalid,
1170
0
      "sequence not finished", return -1);
1171
0
1172
0
  return pop_state(s);
1173
0
}