cf-parser.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. #pragma once
  17. #include "cf-lexer.h"
  18. /*
  19. * C-family parser
  20. *
  21. * Handles preprocessing/lexing/errors when parsing a file, and provides a
  22. * set of parsing functions to be able to go through all the resulting tokens
  23. * more easily.
  24. */
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. #define PARSE_SUCCESS 0
  29. #define PARSE_CONTINUE -1
  30. #define PARSE_BREAK -2
  31. #define PARSE_UNEXPECTED_CONTINUE -3
  32. #define PARSE_UNEXPECTED_BREAK -4
  33. #define PARSE_EOF -5
  34. struct cf_parser {
  35. struct cf_lexer lex;
  36. struct cf_preprocessor pp;
  37. struct error_data error_list;
  38. struct cf_token *cur_token;
  39. };
  40. static inline void cf_parser_init(struct cf_parser *parser)
  41. {
  42. cf_lexer_init(&parser->lex);
  43. cf_preprocessor_init(&parser->pp);
  44. error_data_init(&parser->error_list);
  45. parser->cur_token = NULL;
  46. }
  47. static inline void cf_parser_free(struct cf_parser *parser)
  48. {
  49. cf_lexer_free(&parser->lex);
  50. cf_preprocessor_free(&parser->pp);
  51. error_data_free(&parser->error_list);
  52. parser->cur_token = NULL;
  53. }
  54. static inline bool cf_parser_parse(struct cf_parser *parser, const char *str, const char *file)
  55. {
  56. if (!cf_lexer_lex(&parser->lex, str, file))
  57. return false;
  58. if (!cf_preprocess(&parser->pp, &parser->lex, &parser->error_list))
  59. return false;
  60. parser->cur_token = cf_preprocessor_get_tokens(&parser->pp);
  61. return true;
  62. }
  63. EXPORT void cf_adderror(struct cf_parser *parser, const char *error, int level, const char *val1, const char *val2,
  64. const char *val3);
  65. static inline void cf_adderror_expecting(struct cf_parser *p, const char *expected)
  66. {
  67. cf_adderror(p, "Expected '$1'", LEX_ERROR, expected, NULL, NULL);
  68. }
  69. static inline void cf_adderror_unexpected_eof(struct cf_parser *p)
  70. {
  71. cf_adderror(p, "Unexpected EOF", LEX_ERROR, NULL, NULL, NULL);
  72. }
  73. static inline void cf_adderror_syntax_error(struct cf_parser *p)
  74. {
  75. cf_adderror(p, "Syntax error", LEX_ERROR, NULL, NULL, NULL);
  76. }
  77. static inline bool cf_next_token(struct cf_parser *p)
  78. {
  79. if (p->cur_token->type != CFTOKEN_SPACETAB && p->cur_token->type != CFTOKEN_NEWLINE &&
  80. p->cur_token->type != CFTOKEN_NONE)
  81. p->cur_token++;
  82. while (p->cur_token->type == CFTOKEN_SPACETAB || p->cur_token->type == CFTOKEN_NEWLINE)
  83. p->cur_token++;
  84. return p->cur_token->type != CFTOKEN_NONE;
  85. }
  86. static inline bool cf_next_valid_token(struct cf_parser *p)
  87. {
  88. if (!cf_next_token(p)) {
  89. cf_adderror_unexpected_eof(p);
  90. return false;
  91. }
  92. return true;
  93. }
  94. EXPORT bool cf_pass_pair(struct cf_parser *p, char in, char out);
  95. static inline bool cf_go_to_token(struct cf_parser *p, const char *str1, const char *str2)
  96. {
  97. while (cf_next_token(p)) {
  98. if (strref_cmp(&p->cur_token->str, str1) == 0) {
  99. return true;
  100. } else if (str2 && strref_cmp(&p->cur_token->str, str2) == 0) {
  101. return true;
  102. } else if (*p->cur_token->str.array == '{') {
  103. if (!cf_pass_pair(p, '{', '}'))
  104. break;
  105. }
  106. }
  107. return false;
  108. }
  109. static inline bool cf_go_to_valid_token(struct cf_parser *p, const char *str1, const char *str2)
  110. {
  111. if (!cf_go_to_token(p, str1, str2)) {
  112. cf_adderror_unexpected_eof(p);
  113. return false;
  114. }
  115. return true;
  116. }
  117. static inline bool cf_go_to_token_type(struct cf_parser *p, enum cf_token_type type)
  118. {
  119. while (p->cur_token->type != CFTOKEN_NONE && p->cur_token->type != type)
  120. p->cur_token++;
  121. return p->cur_token->type != CFTOKEN_NONE;
  122. }
  123. static inline int cf_token_should_be(struct cf_parser *p, const char *str, const char *goto1, const char *goto2)
  124. {
  125. if (strref_cmp(&p->cur_token->str, str) == 0)
  126. return PARSE_SUCCESS;
  127. if (goto1) {
  128. if (!cf_go_to_token(p, goto1, goto2))
  129. return PARSE_EOF;
  130. }
  131. cf_adderror_expecting(p, str);
  132. return PARSE_CONTINUE;
  133. }
  134. static inline int cf_next_token_should_be(struct cf_parser *p, const char *str, const char *goto1, const char *goto2)
  135. {
  136. if (!cf_next_token(p)) {
  137. cf_adderror_unexpected_eof(p);
  138. return PARSE_EOF;
  139. } else if (strref_cmp(&p->cur_token->str, str) == 0) {
  140. return PARSE_SUCCESS;
  141. }
  142. if (goto1) {
  143. if (!cf_go_to_token(p, goto1, goto2))
  144. return PARSE_EOF;
  145. }
  146. cf_adderror_expecting(p, str);
  147. return PARSE_CONTINUE;
  148. }
  149. static inline bool cf_peek_token(struct cf_parser *p, struct cf_token *peek)
  150. {
  151. struct cf_token *cur_token = p->cur_token;
  152. bool success = cf_next_token(p);
  153. *peek = *p->cur_token;
  154. p->cur_token = cur_token;
  155. return success;
  156. }
  157. static inline bool cf_peek_valid_token(struct cf_parser *p, struct cf_token *peek)
  158. {
  159. bool success = cf_peek_token(p, peek);
  160. if (!success)
  161. cf_adderror_unexpected_eof(p);
  162. return success;
  163. }
  164. static inline bool cf_token_is(struct cf_parser *p, const char *val)
  165. {
  166. return strref_cmp(&p->cur_token->str, val) == 0;
  167. }
  168. static inline int cf_token_is_type(struct cf_parser *p, enum cf_token_type type, const char *type_expected,
  169. const char *goto_token)
  170. {
  171. if (p->cur_token->type != type) {
  172. cf_adderror_expecting(p, type_expected);
  173. if (goto_token) {
  174. if (!cf_go_to_valid_token(p, goto_token, NULL))
  175. return PARSE_EOF;
  176. }
  177. return PARSE_CONTINUE;
  178. }
  179. return PARSE_SUCCESS;
  180. }
  181. static inline void cf_copy_token(struct cf_parser *p, char **dst)
  182. {
  183. *dst = bstrdup_n(p->cur_token->str.array, p->cur_token->str.len);
  184. }
  185. static inline int cf_get_name(struct cf_parser *p, char **dst, const char *name, const char *goto_token)
  186. {
  187. int errcode;
  188. errcode = cf_token_is_type(p, CFTOKEN_NAME, name, goto_token);
  189. if (errcode != PARSE_SUCCESS)
  190. return errcode;
  191. *dst = bstrdup_n(p->cur_token->str.array, p->cur_token->str.len);
  192. return PARSE_SUCCESS;
  193. }
  194. static inline int cf_next_name(struct cf_parser *p, char **dst, const char *name, const char *goto_token)
  195. {
  196. if (!cf_next_valid_token(p))
  197. return PARSE_EOF;
  198. return cf_get_name(p, dst, name, goto_token);
  199. }
  200. static inline int cf_next_token_copy(struct cf_parser *p, char **dst)
  201. {
  202. if (!cf_next_valid_token(p))
  203. return PARSE_EOF;
  204. cf_copy_token(p, dst);
  205. return PARSE_SUCCESS;
  206. }
  207. static inline int cf_get_name_ref(struct cf_parser *p, struct strref *dst, const char *name, const char *goto_token)
  208. {
  209. int errcode;
  210. errcode = cf_token_is_type(p, CFTOKEN_NAME, name, goto_token);
  211. if (errcode != PARSE_SUCCESS)
  212. return errcode;
  213. strref_copy(dst, &p->cur_token->str);
  214. return PARSE_SUCCESS;
  215. }
  216. static inline int cf_next_name_ref(struct cf_parser *p, struct strref *dst, const char *name, const char *goto_token)
  217. {
  218. if (!cf_next_valid_token(p))
  219. return PARSE_EOF;
  220. return cf_get_name_ref(p, dst, name, goto_token);
  221. }
  222. #ifdef __cplusplus
  223. }
  224. #endif