extract_key.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright 2013 Ecole Normale Superieure
  3. *
  4. * Use of this software is governed by the MIT license
  5. *
  6. * Written by Sven Verdoolaege,
  7. * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
  8. */
  9. #include <string.h>
  10. /* Extract a mapping key from the token "tok".
  11. * Return KEY_ERROR on error, i.e., if "tok" does not
  12. * correspond to any known key.
  13. */
  14. static KEY extract_key(__isl_keep isl_stream *s, struct isl_token *tok)
  15. {
  16. int type;
  17. char *name;
  18. KEY key;
  19. isl_ctx *ctx;
  20. if (!tok)
  21. return KEY_ERROR;
  22. type = isl_token_get_type(tok);
  23. if (type != ISL_TOKEN_IDENT && type != ISL_TOKEN_STRING) {
  24. isl_stream_error(s, tok, "expecting key");
  25. return KEY_ERROR;
  26. }
  27. ctx = isl_stream_get_ctx(s);
  28. name = isl_token_get_str(ctx, tok);
  29. if (!name)
  30. return KEY_ERROR;
  31. for (key = 0; key < KEY_END; ++key) {
  32. if (!strcmp(name, key_str[key]))
  33. break;
  34. }
  35. free(name);
  36. if (key >= KEY_END)
  37. isl_die(ctx, isl_error_invalid, "unknown key",
  38. return KEY_ERROR);
  39. return key;
  40. }
  41. /* Read a key from "s" and return the corresponding enum.
  42. * Return KEY_ERROR on error, i.e., if the first token
  43. * on the stream does not correspond to any known key.
  44. */
  45. static KEY get_key(__isl_keep isl_stream *s)
  46. {
  47. struct isl_token *tok;
  48. KEY key;
  49. tok = isl_stream_next_token(s);
  50. key = extract_key(s, tok);
  51. isl_token_free(tok);
  52. return key;
  53. }