cf-parser.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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_gettokens(&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", LEVEL_ERROR, expected, NULL, NULL);
  71. }
  72. static inline void cf_adderror_unexpected_eof(struct cf_parser *p)
  73. {
  74. cf_adderror(p, "Unexpected end of file", LEVEL_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", LEVEL_ERROR,
  80. NULL, NULL, NULL);
  81. }
  82. static inline bool 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 next_valid_token(struct cf_parser *p)
  94. {
  95. if (!next_token(p)) {
  96. cf_adderror_unexpected_eof(p);
  97. return false;
  98. }
  99. return true;
  100. }
  101. EXPORT bool pass_pair(struct cf_parser *p, char in, char out);
  102. static inline bool go_to_token(struct cf_parser *p,
  103. const char *str1, const char *str2)
  104. {
  105. while (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 (!pass_pair(p, '{', '}'))
  112. break;
  113. }
  114. }
  115. return false;
  116. }
  117. static inline bool go_to_valid_token(struct cf_parser *p,
  118. const char *str1, const char *str2)
  119. {
  120. if (!go_to_token(p, str1, str2)) {
  121. cf_adderror_unexpected_eof(p);
  122. return false;
  123. }
  124. return true;
  125. }
  126. static inline bool 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 next_token_should_be(struct cf_parser *p,
  135. const char *str, const char *goto1, const char *goto2)
  136. {
  137. if (!next_token(p)) {
  138. cf_adderror_unexpected_eof(p);
  139. return PARSE_EOF;
  140. } else if (strref_cmp(&p->cur_token->str, str) == 0) {
  141. return PARSE_SUCCESS;
  142. }
  143. if (goto1)
  144. go_to_token(p, goto1, goto2);
  145. cf_adderror_expecting(p, str);
  146. return PARSE_CONTINUE;
  147. }
  148. static inline bool peek_token(struct cf_parser *p, struct cf_token *peek)
  149. {
  150. struct cf_token *cur_token = p->cur_token;
  151. bool success = next_token(p);
  152. *peek = *p->cur_token;
  153. p->cur_token = cur_token;
  154. return success;
  155. }
  156. static inline bool peek_valid_token(struct cf_parser *p,
  157. struct cf_token *peek)
  158. {
  159. bool success = peek_token(p, peek);
  160. if (!success)
  161. cf_adderror_unexpected_eof(p);
  162. return success;
  163. }
  164. static inline bool 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 token_is_type(struct cf_parser *p,
  169. enum cf_token_type type, const char *type_expected,
  170. const char *goto_token)
  171. {
  172. if (p->cur_token->type != type) {
  173. cf_adderror_expecting(p, type_expected);
  174. if (goto_token) {
  175. if (!go_to_valid_token(p, goto_token, NULL))
  176. return PARSE_EOF;
  177. }
  178. return PARSE_CONTINUE;
  179. }
  180. return PARSE_SUCCESS;
  181. }
  182. static inline void copy_token(struct cf_parser *p, char **dst)
  183. {
  184. *dst = bstrdup_n(p->cur_token->str.array, p->cur_token->str.len);
  185. }
  186. static inline int get_name(struct cf_parser *p, char **dst,
  187. const char *name, const char *goto_token)
  188. {
  189. int errcode;
  190. errcode = token_is_type(p, CFTOKEN_NAME, name, goto_token);
  191. if (errcode != PARSE_SUCCESS)
  192. return errcode;
  193. *dst = bstrdup_n(p->cur_token->str.array, p->cur_token->str.len);
  194. return PARSE_SUCCESS;
  195. }
  196. static inline int next_name(struct cf_parser *p, char **dst,
  197. const char *name, const char *goto_token)
  198. {
  199. if (!next_valid_token(p))
  200. return PARSE_EOF;
  201. return get_name(p, dst, name, goto_token);
  202. }
  203. static inline int get_name_ref(struct cf_parser *p, struct strref *dst,
  204. const char *name, const char *goto_token)
  205. {
  206. int errcode;
  207. errcode = token_is_type(p, CFTOKEN_NAME, name, goto_token);
  208. if (errcode != PARSE_SUCCESS)
  209. return errcode;
  210. strref_copy(dst, &p->cur_token->str);
  211. return PARSE_SUCCESS;
  212. }
  213. static inline int next_name_ref(struct cf_parser *p, struct strref *dst,
  214. const char *name, const char *goto_token)
  215. {
  216. if (!next_valid_token(p))
  217. return PARSE_EOF;
  218. return get_name_ref(p, dst, name, goto_token);
  219. }
  220. #ifdef __cplusplus
  221. }
  222. #endif