Coverage Report

Created: 2017-11-23 03:11

/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
61.3k
{
72
61.3k
  struct isl_token *tok = isl_alloc_type(ctx, struct isl_token);
73
61.3k
  if (!tok)
74
0
    return NULL;
75
61.3k
  tok->line = line;
76
61.3k
  tok->col = col;
77
61.3k
  tok->on_new_line = on_new_line;
78
61.3k
  tok->is_keyword = 0;
79
61.3k
  tok->u.s = NULL;
80
61.3k
  return tok;
81
61.3k
}
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
768
{
107
768
  if (!tok)
108
0
    return NULL;
109
768
  if (!tok->u.s)
110
768
    
isl_die0
(ctx, isl_error_invalid,
111
768
      "token does not have a string representation",
112
768
      return NULL);
113
768
114
768
  return strdup(tok->u.s);
115
768
}
116
117
void isl_token_free(struct isl_token *tok)
118
68.6k
{
119
68.6k
  if (!tok)
120
7.27k
    return;
121
61.3k
  if (tok->type == ISL_TOKEN_VALUE)
122
61.3k
    
isl_int_clear10.0k
(tok->u.v);
123
61.3k
  else 
if (51.3k
tok->type == ISL_TOKEN_MAP51.3k
)
124
163
    isl_map_free(tok->u.map);
125
51.1k
  else if (tok->type == ISL_TOKEN_AFF)
126
179
    isl_pw_aff_free(tok->u.pwaff);
127
51.0k
  else
128
51.0k
    free(tok->u.s);
129
68.6k
  free(tok);
130
68.6k
}
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
2.89k
{
172
2.89k
  int i;
173
2.89k
  isl_stream *s = isl_calloc_type(ctx, struct isl_stream);
174
2.89k
  if (!s)
175
0
    return NULL;
176
2.89k
  s->ctx = ctx;
177
2.89k
  isl_ctx_ref(s->ctx);
178
2.89k
  s->file = NULL;
179
2.89k
  s->str = NULL;
180
2.89k
  s->len = 0;
181
2.89k
  s->line = 1;
182
2.89k
  s->col = 1;
183
2.89k
  s->eof = 0;
184
2.89k
  s->last_line = 0;
185
2.89k
  s->c = -1;
186
2.89k
  s->n_un = 0;
187
17.3k
  for (i = 0; i < 5; 
++i14.4k
)
188
14.4k
    s->tokens[i] = NULL;
189
2.89k
  s->n_token = 0;
190
2.89k
  s->keywords = NULL;
191
2.89k
  s->size = 256;
192
2.89k
  s->buffer = isl_alloc_array(ctx, char, s->size);
193
2.89k
  if (!s->buffer)
194
0
    goto error;
195
2.89k
  return s;
196
0
error:
197
0
  isl_stream_free(s);
198
0
  return NULL;
199
2.89k
}
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
2.78k
{
212
2.78k
  isl_stream *s;
213
2.78k
  if (!str)
214
0
    return NULL;
215
2.78k
  s = isl_stream_new(ctx);
216
2.78k
  if (!s)
217
0
    return NULL;
218
2.78k
  s->str = str;
219
2.78k
  return s;
220
2.78k
}
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
183k
{
227
183k
  int c;
228
183k
  if (s->eof)
229
471
    return -1;
230
183k
  if (s->n_un)
231
50.1k
    return s->c = s->un[--s->n_un];
232
132k
  if (s->file)
233
10.6k
    c = fgetc(s->file);
234
122k
  else {
235
122k
    c = *s->str++;
236
122k
    if (c == '\0')
237
340
      c = -1;
238
122k
  }
239
132k
  if (c == -1)
240
340
    s->eof = 1;
241
132k
  else if (c == '\n') {
242
613
    s->line++;
243
613
    s->col = 1;
244
613
  } else
245
132k
    s->col++;
246
183k
  s->c = c;
247
183k
  return c;
248
183k
}
249
250
static void isl_stream_ungetc(__isl_keep isl_stream *s, int c)
251
50.1k
{
252
50.1k
  isl_assert(s->ctx, s->n_un < 5, return);
253
50.1k
  s->un[s->n_un++] = c;
254
50.1k
  s->c = -1;
255
50.1k
}
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
183k
{
263
183k
  int c;
264
183k
265
183k
  do {
266
183k
    s->start_line = s->line;
267
183k
    s->start_col = s->col;
268
183k
    c = stream_getc(s);
269
183k
    if (c != '\\')
270
183k
      return c;
271
0
    c = stream_getc(s);
272
0
  } while (c == '\n');
273
183k
274
183k
  isl_stream_ungetc(s, c);
275
0
276
0
  return '\\';
277
183k
}
278
279
static int isl_stream_push_char(__isl_keep isl_stream *s, int c)
280
80.9k
{
281
80.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
80.9k
  s->buffer[s->len++] = c;
290
80.9k
  return 0;
291
80.9k
}
292
293
void isl_stream_push_token(__isl_keep isl_stream *s, struct isl_token *tok)
294
194k
{
295
194k
  isl_assert(s->ctx, s->n_token < 5, return);
296
194k
  s->tokens[s->n_token++] = tok;
297
194k
}
298
299
static enum isl_token_type check_keywords(__isl_keep isl_stream *s)
300
17.7k
{
301
17.7k
  struct isl_hash_table_entry *entry;
302
17.7k
  struct isl_keyword *keyword;
303
17.7k
  uint32_t name_hash;
304
17.7k
305
17.7k
  if (!strcasecmp(s->buffer, "exists"))
306
131
    return ISL_TOKEN_EXISTS;
307
17.6k
  if (!strcasecmp(s->buffer, "and"))
308
1.96k
    return ISL_TOKEN_AND;
309
15.6k
  if (!strcasecmp(s->buffer, "or"))
310
151
    return ISL_TOKEN_OR;
311
15.5k
  if (!strcasecmp(s->buffer, "implies"))
312
1
    return ISL_TOKEN_IMPLIES;
313
15.5k
  if (!strcasecmp(s->buffer, "not"))
314
1
    return ISL_TOKEN_NOT;
315
15.5k
  if (!strcasecmp(s->buffer, "infty"))
316
83
    return ISL_TOKEN_INFTY;
317
15.4k
  if (!strcasecmp(s->buffer, "infinity"))
318
0
    return ISL_TOKEN_INFTY;
319
15.4k
  if (!strcasecmp(s->buffer, "NaN"))
320
78
    return ISL_TOKEN_NAN;
321
15.3k
  if (!strcasecmp(s->buffer, "min"))
322
5
    return ISL_TOKEN_MIN;
323
15.3k
  if (!strcasecmp(s->buffer, "max"))
324
1
    return ISL_TOKEN_MAX;
325
15.3k
  if (!strcasecmp(s->buffer, "rat"))
326
22
    return ISL_TOKEN_RAT;
327
15.3k
  if (!strcasecmp(s->buffer, "true"))
328
1
    return ISL_TOKEN_TRUE;
329
15.3k
  if (!strcasecmp(s->buffer, "false"))
330
7
    return ISL_TOKEN_FALSE;
331
15.3k
  if (!strcasecmp(s->buffer, "ceild"))
332
0
    return ISL_TOKEN_CEILD;
333
15.3k
  if (!strcasecmp(s->buffer, "floord"))
334
1
    return ISL_TOKEN_FLOORD;
335
15.3k
  if (!strcasecmp(s->buffer, "mod"))
336
58
    return ISL_TOKEN_MOD;
337
15.2k
  if (!strcasecmp(s->buffer, "ceil"))
338
0
    return ISL_TOKEN_CEIL;
339
15.2k
  if (!strcasecmp(s->buffer, "floor"))
340
142
    return ISL_TOKEN_FLOOR;
341
15.1k
342
15.1k
  if (!s->keywords)
343
15.1k
    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
14
    /* 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
256k
{
369
256k
  int c;
370
256k
  struct isl_token *tok = NULL;
371
256k
  int line, col;
372
256k
  int old_line = s->last_line;
373
256k
374
256k
  if (s->n_token) {
375
194k
    if (same_line && 
s->tokens[s->n_token - 1]->on_new_line105
)
376
0
      return NULL;
377
194k
    return s->tokens[--s->n_token];
378
194k
  }
379
61.7k
380
61.7k
  if (same_line && 
s->c == '\n'210
)
381
0
    return NULL;
382
61.7k
383
61.7k
  s->len = 0;
384
61.7k
385
61.7k
  /* skip spaces and comment lines */
386
100k
  while ((c = isl_stream_getc(s)) != -1) {
387
100k
    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
100k
    } else if (!isspace(c) || 
(39.4k
same_line39.4k
&&
c == '\n'212
))
394
61.3k
      break;
395
100k
  }
396
61.7k
397
61.7k
  line = s->start_line;
398
61.7k
  col = s->start_col;
399
61.7k
400
61.7k
  if (c == -1 || 
(61.3k
same_line61.3k
&&
c == '\n'210
))
401
568
    return NULL;
402
61.1k
  s->last_line = line;
403
61.1k
404
61.1k
  if (c == '(' ||
405
61.1k
      
c == ')'60.4k
||
406
61.1k
      
c == '+'59.7k
||
407
61.1k
      
c == '*'58.3k
||
408
61.1k
      
c == '%'58.2k
||
409
61.1k
      
c == '?'58.2k
||
410
61.1k
      
c == '^'58.2k
||
411
61.1k
      
c == '@'58.1k
||
412
61.1k
      
c == '$'58.1k
||
413
61.1k
      
c == ','58.1k
||
414
61.1k
      
c == '.'55.1k
||
415
61.1k
      
c == ';'55.1k
||
416
61.1k
      
c == '['54.4k
||
417
61.1k
      
c == ']'48.5k
||
418
61.1k
      
c == '{'42.6k
||
419
61.1k
      
c == '}'40.1k
) {
420
23.4k
    tok = isl_token_new(s->ctx, line, col, old_line != line);
421
23.4k
    if (!tok)
422
0
      return NULL;
423
23.4k
    tok->type = (enum isl_token_type)c;
424
23.4k
    return tok;
425
23.4k
  }
426
37.6k
  if (c == '-') {
427
4.68k
    int c;
428
4.68k
    if ((c = isl_stream_getc(s)) == '>') {
429
2.73k
      tok = isl_token_new(s->ctx, line, col, old_line != line);
430
2.73k
      if (!tok)
431
0
        return NULL;
432
2.73k
      tok->u.s = strdup("->");
433
2.73k
      tok->type = ISL_TOKEN_TO;
434
2.73k
      return tok;
435
2.73k
    }
436
1.95k
    if (c != -1)
437
1.95k
      isl_stream_ungetc(s, c);
438
1.95k
    if (!isdigit(c)) {
439
669
      tok = isl_token_new(s->ctx, line, col, old_line != line);
440
669
      if (!tok)
441
0
        return NULL;
442
669
      tok->type = (enum isl_token_type) '-';
443
669
      return tok;
444
669
    }
445
4.68k
  }
446
34.2k
  if (c == '-' || 
isdigit(c)32.9k
) {
447
10.0k
    int minus = c == '-';
448
10.0k
    tok = isl_token_new(s->ctx, line, col, old_line != line);
449
10.0k
    if (!tok)
450
0
      return NULL;
451
10.0k
    tok->type = ISL_TOKEN_VALUE;
452
10.0k
    isl_int_init(tok->u.v);
453
10.0k
    if (isl_stream_push_char(s, c))
454
0
      goto error;
455
14.7k
    
while (10.0k
(c = isl_stream_getc(s)) != -1 &&
isdigit(c)14.5k
)
456
10.0k
      
if (4.73k
isl_stream_push_char(s, c)4.73k
)
457
0
        goto error;
458
10.0k
    if (c != -1)
459
9.80k
      isl_stream_ungetc(s, c);
460
10.0k
    isl_stream_push_char(s, '\0');
461
10.0k
    isl_int_read(tok->u.v, s->buffer);
462
10.0k
    if (minus && 
isl_int_is_zero1.28k
(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.0k
    return tok;
472
24.2k
  }
473
24.2k
  if (isalpha(c) || 
c == '_'6.51k
) {
474
17.7k
    tok = isl_token_new(s->ctx, line, col, old_line != line);
475
17.7k
    if (!tok)
476
0
      return NULL;
477
17.7k
    isl_stream_push_char(s, c);
478
38.3k
    while ((c = isl_stream_getc(s)) != -1 &&
479
38.3k
        
(38.2k
isalnum(c)38.2k
||
c == '_'18.9k
))
480
20.6k
      isl_stream_push_char(s, c);
481
17.7k
    if (c != -1)
482
17.6k
      isl_stream_ungetc(s, c);
483
17.8k
    while ((c = isl_stream_getc(s)) != -1 && 
c == '\''17.7k
)
484
17.7k
      
isl_stream_push_char(s, c)94
;
485
17.7k
    if (c != -1)
486
17.6k
      isl_stream_ungetc(s, c);
487
17.7k
    isl_stream_push_char(s, '\0');
488
17.7k
    tok->type = check_keywords(s);
489
17.7k
    if (tok->type != ISL_TOKEN_IDENT)
490
2.64k
      tok->is_keyword = 1;
491
17.7k
    tok->u.s = strdup(s->buffer);
492
17.7k
    if (!tok->u.s)
493
0
      goto error;
494
17.7k
    return tok;
495
17.7k
  }
496
6.51k
  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
6.51k
  if (c == '=') {
513
632
    int c;
514
632
    tok = isl_token_new(s->ctx, line, col, old_line != line);
515
632
    if (!tok)
516
0
      return NULL;
517
632
    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
632
    if (c != -1)
523
632
      isl_stream_ungetc(s, c);
524
632
    tok->type = (enum isl_token_type) '=';
525
632
    return tok;
526
632
  }
527
5.87k
  if (c == ':') {
528
1.60k
    int c;
529
1.60k
    tok = isl_token_new(s->ctx, line, col, old_line != line);
530
1.60k
    if (!tok)
531
0
      return NULL;
532
1.60k
    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
1.60k
    if (c != -1)
538
1.60k
      isl_stream_ungetc(s, c);
539
1.60k
    tok->type = (enum isl_token_type) ':';
540
1.60k
    return tok;
541
1.60k
  }
542
4.27k
  if (c == '>') {
543
1.18k
    int c;
544
1.18k
    tok = isl_token_new(s->ctx, line, col, old_line != line);
545
1.18k
    if (!tok)
546
0
      return NULL;
547
1.18k
    if ((c = isl_stream_getc(s)) == '=') {
548
1.13k
      tok->u.s = strdup(">=");
549
1.13k
      tok->type = ISL_TOKEN_GE;
550
1.13k
      return tok;
551
1.13k
    } else 
if (55
c == '>'55
) {
552
2
      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
1
      tok->u.s = strdup(">>");
558
1
      tok->type = ISL_TOKEN_LEX_GT;
559
53
    } else {
560
53
      tok->u.s = strdup(">");
561
53
      tok->type = ISL_TOKEN_GT;
562
53
    }
563
1.18k
    
if (54
c != -154
)
564
54
      isl_stream_ungetc(s, c);
565
54
    return tok;
566
3.08k
  }
567
3.08k
  if (c == '<') {
568
2.59k
    int c;
569
2.59k
    tok = isl_token_new(s->ctx, line, col, old_line != line);
570
2.59k
    if (!tok)
571
0
      return NULL;
572
2.59k
    if ((c = isl_stream_getc(s)) == '=') {
573
2.24k
      tok->u.s = strdup("<=");
574
2.24k
      tok->type = ISL_TOKEN_LE;
575
2.24k
      return tok;
576
2.24k
    } else 
if (352
c == '<'352
) {
577
2
      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
1
      tok->u.s = strdup("<<");
583
1
      tok->type = ISL_TOKEN_LEX_LT;
584
350
    } else {
585
350
      tok->u.s = strdup("<");
586
350
      tok->type = ISL_TOKEN_LT;
587
350
    }
588
2.59k
    
if (351
c != -1351
)
589
351
      isl_stream_ungetc(s, c);
590
351
    return tok;
591
492
  }
592
492
  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
388
  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
387
  if (c == '/') {
617
375
    tok = isl_token_new(s->ctx, line, col, old_line != line);
618
375
    if (!tok)
619
0
      return NULL;
620
375
    if ((c = isl_stream_getc(s)) != '\\' && c != -1) {
621
375
      tok->type = (enum isl_token_type) '/';
622
375
      isl_stream_ungetc(s, c);
623
375
    } else {
624
0
      tok->u.s = strdup("/\\");
625
0
      tok->type = ISL_TOKEN_AND;
626
0
    }
627
375
    return tok;
628
375
  }
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
256k
}
668
669
struct isl_token *isl_stream_next_token(__isl_keep isl_stream *s)
670
256k
{
671
256k
  return next_token(s, 0);
672
256k
}
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
87.4k
{
681
87.4k
  struct isl_token *tok;
682
87.4k
683
87.4k
  tok = isl_stream_next_token(s);
684
87.4k
  if (!tok)
685
343
    return 0;
686
87.0k
  if (tok->type == type) {
687
3.46k
    isl_token_free(tok);
688
3.46k
    return 1;
689
3.46k
  }
690
83.6k
  isl_stream_push_token(s, tok);
691
83.6k
  return 0;
692
83.6k
}
693
694
int isl_stream_next_token_is(__isl_keep isl_stream *s, int type)
695
36.7k
{
696
36.7k
  struct isl_token *tok;
697
36.7k
  int r;
698
36.7k
699
36.7k
  tok = isl_stream_next_token(s);
700
36.7k
  if (!tok)
701
15
    return 0;
702
36.7k
  r = tok->type == type;
703
36.7k
  isl_stream_push_token(s, tok);
704
36.7k
  return r;
705
36.7k
}
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
13.5k
{
725
13.5k
  struct isl_token *tok;
726
13.5k
727
13.5k
  tok = isl_stream_next_token(s);
728
13.5k
  if (!tok)
729
0
    return -1;
730
13.5k
  if (tok->type == type) {
731
13.5k
    isl_token_free(tok);
732
13.5k
    return 0;
733
13.5k
  }
734
2
  isl_stream_error(s, tok, "expecting other token");
735
2
  isl_stream_push_token(s, tok);
736
2
  return -1;
737
2
}
738
739
int isl_stream_is_empty(__isl_keep isl_stream *s)
740
0
{
741
0
  struct isl_token *tok;
742
0
743
0
  tok = isl_stream_next_token(s);
744
0
745
0
  if (!tok)
746
0
    return 1;
747
0
748
0
  isl_stream_push_token(s, tok);
749
0
  return 0;
750
0
}
751
752
static isl_stat free_keyword(void **p, void *user)
753
0
{
754
0
  struct isl_keyword *keyword = *p;
755
0
756
0
  free(keyword->name);
757
0
  free(keyword);
758
0
759
0
  return isl_stat_ok;
760
0
}
761
762
void isl_stream_flush_tokens(__isl_keep isl_stream *s)
763
0
{
764
0
  int i;
765
0
766
0
  if (!s)
767
0
    return;
768
0
  for (i = 0; i < s->n_token; ++i)
769
0
    isl_token_free(s->tokens[i]);
770
0
  s->n_token = 0;
771
0
}
772
773
isl_ctx *isl_stream_get_ctx(__isl_keep isl_stream *s)
774
0
{
775
0
  return s ? s->ctx : NULL;
776
0
}
777
778
void isl_stream_free(__isl_take isl_stream *s)
779
2.89k
{
780
2.89k
  if (!s)
781
0
    return;
782
2.89k
  free(s->buffer);
783
2.89k
  if (s->n_token != 0) {
784
3
    struct isl_token *tok = isl_stream_next_token(s);
785
3
    isl_stream_error(s, tok, "unexpected token");
786
3
    isl_token_free(tok);
787
3
  }
788
2.89k
  if (s->keywords) {
789
0
    isl_hash_table_foreach(s->ctx, s->keywords, &free_keyword, NULL);
790
0
    isl_hash_table_free(s->ctx, s->keywords);
791
0
  }
792
2.89k
  free(s->yaml_state);
793
2.89k
  free(s->yaml_indent);
794
2.89k
  isl_ctx_deref(s->ctx);
795
2.89k
  free(s);
796
2.89k
}
797
798
/* Push "state" onto the stack of currently active YAML elements.
799
 * The caller is responsible for setting the corresponding indentation.
800
 * Return 0 on success and -1 on failure.
801
 */
802
static int push_state(__isl_keep isl_stream *s, enum isl_yaml_state state)
803
0
{
804
0
  if (s->yaml_size < s->yaml_depth + 1) {
805
0
    int *indent;
806
0
    enum isl_yaml_state *state;
807
0
808
0
    state = isl_realloc_array(s->ctx, s->yaml_state,
809
0
          enum isl_yaml_state, s->yaml_depth + 1);
810
0
    if (!state)
811
0
      return -1;
812
0
    s->yaml_state = state;
813
0
814
0
    indent = isl_realloc_array(s->ctx, s->yaml_indent,
815
0
          int, s->yaml_depth + 1);
816
0
    if (!indent)
817
0
      return -1;
818
0
    s->yaml_indent = indent;
819
0
820
0
    s->yaml_size = s->yaml_depth + 1;
821
0
  }
822
0
823
0
  s->yaml_state[s->yaml_depth] = state;
824
0
  s->yaml_depth++;
825
0
826
0
  return 0;
827
0
}
828
829
/* Remove the innermost active YAML element from the stack.
830
 * Return 0 on success and -1 on failure.
831
 */
832
static int pop_state(__isl_keep isl_stream *s)
833
0
{
834
0
  if (!s)
835
0
    return -1;
836
0
  if (s->yaml_depth < 1)
837
0
    isl_die(isl_stream_get_ctx(s), isl_error_invalid,
838
0
      "not in YAML construct", return -1);
839
0
840
0
  s->yaml_depth--;
841
0
842
0
  return 0;
843
0
}
844
845
/* Set the state of the innermost active YAML element to "state".
846
 * Return 0 on success and -1 on failure.
847
 */
848
static int update_state(__isl_keep isl_stream *s, enum isl_yaml_state state)
849
0
{
850
0
  if (!s)
851
0
    return -1;
852
0
  if (s->yaml_depth < 1)
853
0
    isl_die(isl_stream_get_ctx(s), isl_error_invalid,
854
0
      "not in YAML construct", return -1);
855
0
856
0
  s->yaml_state[s->yaml_depth - 1] = state;
857
0
858
0
  return 0;
859
0
}
860
861
/* Return the state of the innermost active YAML element.
862
 * Return isl_yaml_none if we are not inside any YAML element.
863
 */
864
static enum isl_yaml_state current_state(__isl_keep isl_stream *s)
865
0
{
866
0
  if (!s)
867
0
    return isl_yaml_none;
868
0
  if (s->yaml_depth < 1)
869
0
    return isl_yaml_none;
870
0
  return s->yaml_state[s->yaml_depth - 1];
871
0
}
872
873
/* Set the indentation of the innermost active YAML element to "indent".
874
 * If "indent" is equal to ISL_YAML_INDENT_FLOW, then this means
875
 * that the current elemient is in flow format.
876
 */
877
static int set_yaml_indent(__isl_keep isl_stream *s, int indent)
878
0
{
879
0
  if (s->yaml_depth < 1)
880
0
    isl_die(s->ctx, isl_error_internal,
881
0
      "not in YAML element", return -1);
882
0
883
0
  s->yaml_indent[s->yaml_depth - 1] = indent;
884
0
885
0
  return 0;
886
0
}
887
888
/* Return the indentation of the innermost active YAML element
889
 * of -1 on error.
890
 */
891
static int get_yaml_indent(__isl_keep isl_stream *s)
892
0
{
893
0
  if (s->yaml_depth < 1)
894
0
    isl_die(s->ctx, isl_error_internal,
895
0
      "not in YAML element", return -1);
896
0
897
0
  return s->yaml_indent[s->yaml_depth - 1];
898
0
}
899
900
/* Move to the next state at the innermost level.
901
 * Return 1 if successful.
902
 * Return 0 if we are at the end of the innermost level.
903
 * Return -1 on error.
904
 *
905
 * If we are in state isl_yaml_mapping_key_start, then we have just
906
 * started a mapping and we are expecting a key.  If the mapping started
907
 * with a '{', then we check if the next token is a '}'.  If so,
908
 * then the mapping is empty and there is no next state at this level.
909
 * Otherwise, we assume that there is at least one key (the one from
910
 * which we derived the indentation in isl_stream_yaml_read_start_mapping.
911
 *
912
 * If we are in state isl_yaml_mapping_key, then the we expect a colon
913
 * followed by a value, so there is always a next state unless
914
 * some error occurs.
915
 *
916
 * If we are in state isl_yaml_mapping_val, then there may or may
917
 * not be a subsequent key in the same mapping.
918
 * In flow format, the next key is preceded by a comma.
919
 * In block format, the next key has the same indentation as the first key.
920
 * If the first token has a smaller indentation, then we have reached
921
 * the end of the current mapping.
922
 *
923
 * If we are in state isl_yaml_sequence_start, then we have just
924
 * started a sequence.  If the sequence started with a '[',
925
 * then we check if the next token is a ']'.  If so, then the sequence
926
 * is empty and there is no next state at this level.
927
 * Otherwise, we assume that there is at least one element in the sequence
928
 * (the one from which we derived the indentation in
929
 * isl_stream_yaml_read_start_sequence.
930
 *
931
 * If we are in state isl_yaml_sequence, then there may or may
932
 * not be a subsequent element in the same sequence.
933
 * In flow format, the next element is preceded by a comma.
934
 * In block format, the next element is introduced by a dash with
935
 * the same indentation as that of the first element.
936
 * If the first token is not a dash or if it has a smaller indentation,
937
 * then we have reached the end of the current sequence.
938
 */
939
int isl_stream_yaml_next(__isl_keep isl_stream *s)
940
0
{
941
0
  struct isl_token *tok;
942
0
  enum isl_yaml_state state;
943
0
  int indent;
944
0
945
0
  state = current_state(s);
946
0
  if (state == isl_yaml_none)
947
0
    isl_die(s->ctx, isl_error_invalid,
948
0
      "not in YAML element", return -1);
949
0
  switch (state) {
950
0
  case isl_yaml_mapping_key_start:
951
0
    if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW &&
952
0
        isl_stream_next_token_is(s, '}'))
953
0
      return 0;
954
0
    if (update_state(s, isl_yaml_mapping_key) < 0)
955
0
      return -1;
956
0
    return 1;
957
0
  case isl_yaml_mapping_key:
958
0
    tok = isl_stream_next_token(s);
959
0
    if (!tok) {
960
0
      if (s->eof)
961
0
        isl_stream_error(s, NULL, "unexpected EOF");
962
0
      return -1;
963
0
    }
964
0
    if (tok->type == ':') {
965
0
      isl_token_free(tok);
966
0
      if (update_state(s, isl_yaml_mapping_val) < 0)
967
0
        return -1;
968
0
      return 1;
969
0
    }
970
0
    isl_stream_error(s, tok, "expecting ':'");
971
0
    isl_stream_push_token(s, tok);
972
0
    return -1;
973
0
  case isl_yaml_mapping_val:
974
0
    if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW) {
975
0
      if (!isl_stream_eat_if_available(s, ','))
976
0
        return 0;
977
0
      if (update_state(s, isl_yaml_mapping_key) < 0)
978
0
        return -1;
979
0
      return 1;
980
0
    }
981
0
    tok = isl_stream_next_token(s);
982
0
    if (!tok)
983
0
      return 0;
984
0
    indent = tok->col - 1;
985
0
    isl_stream_push_token(s, tok);
986
0
    if (indent < get_yaml_indent(s))
987
0
      return 0;
988
0
    if (update_state(s, isl_yaml_mapping_key) < 0)
989
0
      return -1;
990
0
    return 1;
991
0
  case isl_yaml_sequence_start:
992
0
    if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW) {
993
0
      if (isl_stream_next_token_is(s, ']'))
994
0
        return 0;
995
0
      if (update_state(s, isl_yaml_sequence) < 0)
996
0
        return -1;
997
0
      return 1;
998
0
    }
999
0
    tok = isl_stream_next_token(s);
1000
0
    if (!tok) {
1001
0
      if (s->eof)
1002
0
        isl_stream_error(s, NULL, "unexpected EOF");
1003
0
      return -1;
1004
0
    }
1005
0
    if (tok->type == '-') {
1006
0
      isl_token_free(tok);
1007
0
      if (update_state(s, isl_yaml_sequence) < 0)
1008
0
        return -1;
1009
0
      return 1;
1010
0
    }
1011
0
    isl_stream_error(s, tok, "expecting '-'");
1012
0
    isl_stream_push_token(s, tok);
1013
0
    return 0;
1014
0
  case isl_yaml_sequence:
1015
0
    if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW)
1016
0
      return isl_stream_eat_if_available(s, ',');
1017
0
    tok = isl_stream_next_token(s);
1018
0
    if (!tok)
1019
0
      return 0;
1020
0
    indent = tok->col - 1;
1021
0
    if (indent < get_yaml_indent(s) || tok->type != '-') {
1022
0
      isl_stream_push_token(s, tok);
1023
0
      return 0;
1024
0
    }
1025
0
    isl_token_free(tok);
1026
0
    return 1;
1027
0
  default:
1028
0
    isl_die(s->ctx, isl_error_internal,
1029
0
      "unexpected state", return 0);
1030
0
  }
1031
0
}
1032
1033
/* Start reading a YAML mapping.
1034
 * Return 0 on success and -1 on error.
1035
 *
1036
 * If the first token on the stream is a '{' then we remove this token
1037
 * from the stream and keep track of the fact that the mapping
1038
 * is given in flow format.
1039
 * Otherwise, we assume the first token is the first key of the mapping and
1040
 * keep track of its indentation, but keep the token on the stream.
1041
 * In both cases, the next token we expect is the first key of the mapping.
1042
 */
1043
int isl_stream_yaml_read_start_mapping(__isl_keep isl_stream *s)
1044
0
{
1045
0
  struct isl_token *tok;
1046
0
  int indent;
1047
0
1048
0
  if (push_state(s, isl_yaml_mapping_key_start) < 0)
1049
0
    return -1;
1050
0
1051
0
  tok = isl_stream_next_token(s);
1052
0
  if (!tok) {
1053
0
    if (s->eof)
1054
0
      isl_stream_error(s, NULL, "unexpected EOF");
1055
0
    return -1;
1056
0
  }
1057
0
  if (isl_token_get_type(tok) == '{') {
1058
0
    isl_token_free(tok);
1059
0
    return set_yaml_indent(s, ISL_YAML_INDENT_FLOW);
1060
0
  }
1061
0
  indent = tok->col - 1;
1062
0
  isl_stream_push_token(s, tok);
1063
0
1064
0
  return set_yaml_indent(s, indent);
1065
0
}
1066
1067
/* Finish reading a YAML mapping.
1068
 * Return 0 on success and -1 on error.
1069
 *
1070
 * If the mapping started with a '{', then we expect a '}' to close
1071
 * the mapping.
1072
 * Otherwise, we double-check that the next token (if any)
1073
 * has a smaller indentation than that of the current mapping.
1074
 */
1075
int isl_stream_yaml_read_end_mapping(__isl_keep isl_stream *s)
1076
0
{
1077
0
  struct isl_token *tok;
1078
0
  int indent;
1079
0
1080
0
  if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW) {
1081
0
    if (isl_stream_eat(s, '}') < 0)
1082
0
      return -1;
1083
0
    return pop_state(s);
1084
0
  }
1085
0
1086
0
  tok = isl_stream_next_token(s);
1087
0
  if (!tok)
1088
0
    return pop_state(s);
1089
0
1090
0
  indent = tok->col - 1;
1091
0
  isl_stream_push_token(s, tok);
1092
0
1093
0
  if (indent >= get_yaml_indent(s))
1094
0
    isl_die(isl_stream_get_ctx(s), isl_error_invalid,
1095
0
      "mapping not finished", return -1);
1096
0
1097
0
  return pop_state(s);
1098
0
}
1099
1100
/* Start reading a YAML sequence.
1101
 * Return 0 on success and -1 on error.
1102
 *
1103
 * If the first token on the stream is a '[' then we remove this token
1104
 * from the stream and keep track of the fact that the sequence
1105
 * is given in flow format.
1106
 * Otherwise, we assume the first token is the dash that introduces
1107
 * the first element of the sequence and keep track of its indentation,
1108
 * but keep the token on the stream.
1109
 * In both cases, the next token we expect is the first element
1110
 * of the sequence.
1111
 */
1112
int isl_stream_yaml_read_start_sequence(__isl_keep isl_stream *s)
1113
0
{
1114
0
  struct isl_token *tok;
1115
0
  int indent;
1116
0
1117
0
  if (push_state(s, isl_yaml_sequence_start) < 0)
1118
0
    return -1;
1119
0
1120
0
  tok = isl_stream_next_token(s);
1121
0
  if (!tok) {
1122
0
    if (s->eof)
1123
0
      isl_stream_error(s, NULL, "unexpected EOF");
1124
0
    return -1;
1125
0
  }
1126
0
  if (isl_token_get_type(tok) == '[') {
1127
0
    isl_token_free(tok);
1128
0
    return set_yaml_indent(s, ISL_YAML_INDENT_FLOW);
1129
0
  }
1130
0
  indent = tok->col - 1;
1131
0
  isl_stream_push_token(s, tok);
1132
0
1133
0
  return set_yaml_indent(s, indent);
1134
0
}
1135
1136
/* Finish reading a YAML sequence.
1137
 * Return 0 on success and -1 on error.
1138
 *
1139
 * If the sequence started with a '[', then we expect a ']' to close
1140
 * the sequence.
1141
 * Otherwise, we double-check that the next token (if any)
1142
 * is not a dash or that it has a smaller indentation than
1143
 * that of the current sequence.
1144
 */
1145
int isl_stream_yaml_read_end_sequence(__isl_keep isl_stream *s)
1146
0
{
1147
0
  struct isl_token *tok;
1148
0
  int indent;
1149
0
  int dash;
1150
0
1151
0
  if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW) {
1152
0
    if (isl_stream_eat(s, ']') < 0)
1153
0
      return -1;
1154
0
    return pop_state(s);
1155
0
  }
1156
0
1157
0
  tok = isl_stream_next_token(s);
1158
0
  if (!tok)
1159
0
    return pop_state(s);
1160
0
1161
0
  indent = tok->col - 1;
1162
0
  dash = tok->type == '-';
1163
0
  isl_stream_push_token(s, tok);
1164
0
1165
0
  if (indent >= get_yaml_indent(s) && dash)
1166
0
    isl_die(isl_stream_get_ctx(s), isl_error_invalid,
1167
0
      "sequence not finished", return -1);
1168
0
1169
0
  return pop_state(s);
1170
0
}