cf-parser.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (c) 2023 Lain 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, const char *val1, const char *val2,
  18. const char *val3)
  19. {
  20. uint32_t row, col;
  21. lexer_getstroffset(&p->cur_token->lex->base_lexer, p->cur_token->unmerged_str.array, &row, &col);
  22. if (!val1 && !val2 && !val3) {
  23. error_data_add(&p->error_list, p->cur_token->lex->file, row, col, error, level);
  24. } else {
  25. struct dstr formatted;
  26. dstr_init(&formatted);
  27. dstr_safe_printf(&formatted, error, val1, val2, val3, NULL);
  28. error_data_add(&p->error_list, p->cur_token->lex->file, row, col, formatted.array, level);
  29. dstr_free(&formatted);
  30. }
  31. }
  32. bool cf_pass_pair(struct cf_parser *p, char in, char out)
  33. {
  34. if (p->cur_token->type != CFTOKEN_OTHER || *p->cur_token->str.array != in)
  35. return p->cur_token->type != CFTOKEN_NONE;
  36. p->cur_token++;
  37. while (p->cur_token->type != CFTOKEN_NONE) {
  38. if (*p->cur_token->str.array == in) {
  39. if (!cf_pass_pair(p, in, out))
  40. break;
  41. continue;
  42. } else if (*p->cur_token->str.array == out) {
  43. p->cur_token++;
  44. return true;
  45. }
  46. p->cur_token++;
  47. }
  48. return false;
  49. }