isl_tab_basic_map_partial_lexopt: copy integer divisions to context
[isl.git] / extract_key.c
blob28fbdfdd30967be75a00b380a579442c9b8748b2
1 /*
2 * Copyright 2013 Ecole Normale Superieure
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
8 */
10 #include <string.h>
12 /* Extract a mapping key from the token "tok".
13 * Return KEY_ERROR on error, i.e., if "tok" does not
14 * correspond to any known key.
16 static KEY KEY_EXTRACT(__isl_keep isl_stream *s, struct isl_token *tok)
18 isl_bool has_string;
19 char *name;
20 KEY key;
21 isl_ctx *ctx;
23 has_string = isl_token_has_str(tok);
24 if (has_string < 0)
25 return KEY_ERROR;
26 if (!has_string) {
27 isl_stream_error(s, tok, "expecting key");
28 return KEY_ERROR;
31 ctx = isl_stream_get_ctx(s);
32 name = isl_token_get_str(ctx, tok);
33 if (!name)
34 return KEY_ERROR;
36 for (key = 0; key < KEY_END; ++key) {
37 if (KEY_STR[key] && !strcmp(name, KEY_STR[key]))
38 break;
40 free(name);
42 if (key >= KEY_END)
43 isl_die(ctx, isl_error_invalid, "unknown key",
44 return KEY_ERROR);
45 return key;
48 /* Read a key from "s" and return the corresponding enum.
49 * Return KEY_ERROR on error, i.e., if the first token
50 * on the stream does not correspond to any known key.
52 static KEY KEY_GET(__isl_keep isl_stream *s)
54 struct isl_token *tok;
55 KEY key;
57 tok = isl_stream_next_token(s);
58 key = KEY_EXTRACT(s, tok);
59 isl_token_free(tok);
61 return key;