cf-parser.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2013 Hugh Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "cf-parser.h"
  17. void cf_adderror(struct cf_parser *p, const char *error, int level,
  18. const char *val1, const char *val2, const char *val3)
  19. {
  20. uint32_t row, col;
  21. lexer_getstroffset(&p->cur_token->lex->base_lexer,
  22. p->cur_token->unmerged_str.array, &row, &col);
  23. if (!val1 && !val2 && !val3) {
  24. error_data_add(&p->error_list, p->cur_token->lex->file, row,
  25. col, error, level);
  26. } else {
  27. struct dstr formatted;
  28. dstr_init(&formatted);
  29. dstr_safe_printf(&formatted, error, val1, val2, val3, NULL);
  30. error_data_add(&p->error_list, p->cur_token->lex->file, row,
  31. col, formatted.array, level);
  32. dstr_free(&formatted);
  33. }
  34. }
  35. bool cf_pass_pair(struct cf_parser *p, char in, char out)
  36. {
  37. if (p->cur_token->type != CFTOKEN_OTHER ||
  38. *p->cur_token->str.array != in)
  39. return p->cur_token->type != CFTOKEN_NONE;
  40. p->cur_token++;
  41. while (p->cur_token->type != CFTOKEN_NONE) {
  42. if (*p->cur_token->str.array == in) {
  43. if (!cf_pass_pair(p, in, out))
  44. break;
  45. continue;
  46. } else if (*p->cur_token->str.array == out) {
  47. p->cur_token++;
  48. return true;
  49. }
  50. p->cur_token++;
  51. }
  52. return false;
  53. }