cf-parser.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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,
  55. const char *file)
  56. {
  57. if (!cf_lexer_lex(&parser->lex, str, file))
  58. return false;
  59. if (!cf_preprocess(&parser->pp, &parser->lex, &parser->error_list))
  60. return false;
  61. parser->cur_token = cf_preprocessor_get_tokens(&parser->pp);
  62. return true;
  63. }
  64. EXPORT void cf_adderror(struct cf_parser *parser, const char *error, int level,
  65. const char *val1, const char *val2, const char *val3);
  66. static inline void cf_adderror_expecting(struct cf_parser *p,
  67. const char *expected)
  68. {
  69. cf_adderror(p, "Expected '$1'", LEX_ERROR, expected, NULL, NULL);
  70. }
  71. static inline void cf_adderror_unexpected_eof(struct cf_parser *p)
  72. {
  73. cf_adderror(p, "Unexpected EOF", LEX_ERROR, NULL, NULL, NULL);
  74. }
  75. static inline void cf_adderror_syntax_error(struct cf_parser *p)
  76. {
  77. cf_adderror(p, "Syntax error", LEX_ERROR, NULL, NULL, NULL);
  78. }
  79. static inline bool cf_next_token(struct cf_parser *p)
  80. {
  81. if (p->cur_token->type != CFTOKEN_SPACETAB &&
  82. p->cur_token->type != CFTOKEN_NEWLINE &&
  83. p->cur_token->type != CFTOKEN_NONE)
  84. p->cur_token++;
  85. while (p->cur_token->type == CFTOKEN_SPACETAB ||
  86. p->cur_token->type == CFTOKEN_NEWLINE)
  87. p->cur_token++;
  88. return p->cur_token->type != CFTOKEN_NONE;
  89. }
  90. static inline bool cf_next_valid_token(struct cf_parser *p)
  91. {
  92. if (!cf_next_token(p)) {
  93. cf_adderror_unexpected_eof(p);
  94. return false;
  95. }
  96. return true;
  97. }
  98. EXPORT bool cf_pass_pair(struct cf_parser *p, char in, char out);
  99. static inline bool cf_go_to_token(struct cf_parser *p, const char *str1,
  100. const char *str2)
  101. {
  102. while (cf_next_token(p)) {
  103. if (strref_cmp(&p->cur_token->str, str1) == 0) {
  104. return true;
  105. } else if (str2 && strref_cmp(&p->cur_token->str, str2) == 0) {
  106. return true;
  107. } else if (*p->cur_token->str.array == '{') {
  108. if (!cf_pass_pair(p, '{', '}'))
  109. break;
  110. }
  111. }
  112. return false;
  113. }
  114. static inline bool cf_go_to_valid_token(struct cf_parser *p, const char *str1,
  115. const char *str2)
  116. {
  117. if (!cf_go_to_token(p, str1, str2)) {
  118. cf_adderror_unexpected_eof(p);
  119. return false;
  120. }
  121. return true;
  122. }
  123. static inline bool cf_go_to_token_type(struct cf_parser *p,
  124. enum cf_token_type type)
  125. {
  126. while (p->cur_token->type != CFTOKEN_NONE && p->cur_token->type != type)
  127. p->cur_token++;
  128. return p->cur_token->type != CFTOKEN_NONE;
  129. }
  130. static inline int cf_token_should_be(struct cf_parser *p, const char *str,
  131. const char *goto1, const char *goto2)
  132. {
  133. if (strref_cmp(&p->cur_token->str, str) == 0)
  134. return PARSE_SUCCESS;
  135. if (goto1) {
  136. if (!cf_go_to_token(p, goto1, goto2))
  137. return PARSE_EOF;
  138. }
  139. cf_adderror_expecting(p, str);
  140. return PARSE_CONTINUE;
  141. }
  142. static inline int cf_next_token_should_be(struct cf_parser *p, const char *str,
  143. const char *goto1, const char *goto2)
  144. {
  145. if (!cf_next_token(p)) {
  146. cf_adderror_unexpected_eof(p);
  147. return PARSE_EOF;
  148. } else if (strref_cmp(&p->cur_token->str, str) == 0) {
  149. return PARSE_SUCCESS;
  150. }
  151. if (goto1) {
  152. if (!cf_go_to_token(p, goto1, goto2))
  153. return PARSE_EOF;
  154. }
  155. cf_adderror_expecting(p, str);
  156. return PARSE_CONTINUE;
  157. }
  158. static inline bool cf_peek_token(struct cf_parser *p, struct cf_token *peek)
  159. {
  160. struct cf_token *cur_token = p->cur_token;
  161. bool success = cf_next_token(p);
  162. *peek = *p->cur_token;
  163. p->cur_token = cur_token;
  164. return success;
  165. }
  166. static inline bool cf_peek_valid_token(struct cf_parser *p,
  167. struct cf_token *peek)
  168. {
  169. bool success = cf_peek_token(p, peek);
  170. if (!success)
  171. cf_adderror_unexpected_eof(p);
  172. return success;
  173. }
  174. static inline bool cf_token_is(struct cf_parser *p, const char *val)
  175. {
  176. return strref_cmp(&p->cur_token->str, val) == 0;
  177. }
  178. static inline int cf_token_is_type(struct cf_parser *p, enum cf_token_type type,
  179. const char *type_expected,
  180. const char *goto_token)
  181. {
  182. if (p->cur_token->type != type) {
  183. cf_adderror_expecting(p, type_expected);
  184. if (goto_token) {
  185. if (!cf_go_to_valid_token(p, goto_token, NULL))
  186. return PARSE_EOF;
  187. }
  188. return PARSE_CONTINUE;
  189. }
  190. return PARSE_SUCCESS;
  191. }
  192. static inline void cf_copy_token(struct cf_parser *p, char **dst)
  193. {
  194. *dst = bstrdup_n(p->cur_token->str.array, p->cur_token->str.len);
  195. }
  196. static inline int cf_get_name(struct cf_parser *p, char **dst, const char *name,
  197. const char *goto_token)
  198. {
  199. int errcode;
  200. errcode = cf_token_is_type(p, CFTOKEN_NAME, name, goto_token);
  201. if (errcode != PARSE_SUCCESS)
  202. return errcode;
  203. *dst = bstrdup_n(p->cur_token->str.array, p->cur_token->str.len);
  204. return PARSE_SUCCESS;
  205. }
  206. static inline int cf_next_name(struct cf_parser *p, char **dst,
  207. const char *name, const char *goto_token)
  208. {
  209. if (!cf_next_valid_token(p))
  210. return PARSE_EOF;
  211. return cf_get_name(p, dst, name, goto_token);
  212. }
  213. static inline int cf_next_token_copy(struct cf_parser *p, char **dst)
  214. {
  215. if (!cf_next_valid_token(p))
  216. return PARSE_EOF;
  217. cf_copy_token(p, dst);
  218. return PARSE_SUCCESS;
  219. }
  220. static inline int cf_get_name_ref(struct cf_parser *p, struct strref *dst,
  221. const char *name, const char *goto_token)
  222. {
  223. int errcode;
  224. errcode = cf_token_is_type(p, CFTOKEN_NAME, name, goto_token);
  225. if (errcode != PARSE_SUCCESS)
  226. return errcode;
  227. strref_copy(dst, &p->cur_token->str);
  228. return PARSE_SUCCESS;
  229. }
  230. static inline int cf_next_name_ref(struct cf_parser *p, struct strref *dst,
  231. const char *name, const char *goto_token)
  232. {
  233. if (!cf_next_valid_token(p))
  234. return PARSE_EOF;
  235. return cf_get_name_ref(p, dst, name, goto_token);
  236. }
  237. #ifdef __cplusplus
  238. }
  239. #endif