cf-parser.h 6.9 KB

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