lexer.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 "c99defs.h"
  18. #include "dstr.h"
  19. #include "darray.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /* ------------------------------------------------------------------------- */
  24. /* string reference (string segment within an already existing array) */
  25. struct strref {
  26. const char *array;
  27. size_t len;
  28. };
  29. static inline void strref_clear(struct strref *dst)
  30. {
  31. dst->array = NULL;
  32. dst->len = 0;
  33. }
  34. static inline void strref_set(struct strref *dst, const char *array, size_t len)
  35. {
  36. dst->array = array;
  37. dst->len = len;
  38. }
  39. static inline void strref_copy(struct strref *dst, const struct strref *src)
  40. {
  41. dst->array = src->array;
  42. dst->len = src->len;
  43. }
  44. static inline void strref_add(struct strref *dst, const struct strref *t)
  45. {
  46. if (!dst->array)
  47. strref_copy(dst, t);
  48. else
  49. dst->len += t->len;
  50. }
  51. static inline bool strref_isempty(const struct strref *str)
  52. {
  53. if (!str->array || !str->len)
  54. return true;
  55. if (!*str->array)
  56. return true;
  57. return false;
  58. }
  59. EXPORT int strref_cmp(const struct strref *str1, const char *str2);
  60. EXPORT int strref_cmpi(const struct strref *str1, const char *str2);
  61. EXPORT int strref_cmp_strref(const struct strref *str1,
  62. const struct strref *str2);
  63. EXPORT int strref_cmpi_strref(const struct strref *str1,
  64. const struct strref *str2);
  65. /* ------------------------------------------------------------------------- */
  66. EXPORT bool valid_int_str(const char *str, size_t n);
  67. EXPORT bool valid_float_str(const char *str, size_t n);
  68. static inline bool valid_int_strref(const struct strref *str)
  69. {
  70. return valid_int_str(str->array, str->len);
  71. }
  72. static inline bool valid_float_strref(const struct strref *str)
  73. {
  74. return valid_float_str(str->array, str->len);
  75. }
  76. static inline bool is_whitespace(char ch)
  77. {
  78. return ch == ' ' || ch == '\r' || ch == '\t' || ch == '\n';
  79. }
  80. static inline bool is_newline(char ch)
  81. {
  82. return ch == '\r' || ch == '\n';
  83. }
  84. static inline bool is_space_or_tab(const char ch)
  85. {
  86. return ch == ' ' || ch == '\t';
  87. }
  88. static inline bool is_newline_pair(char ch1, char ch2)
  89. {
  90. return (ch1 == '\r' && ch2 == '\n') ||
  91. (ch1 == '\n' && ch2 == '\r');
  92. }
  93. static inline int newline_size(const char *array)
  94. {
  95. if (strncmp(array, "\r\n", 2) == 0 || strncmp(array, "\n\r", 2) == 0)
  96. return 2;
  97. else if (*array == '\r' || *array == '\n')
  98. return 1;
  99. return 0;
  100. }
  101. /* ------------------------------------------------------------------------- */
  102. /*
  103. * A "base" token is one of four things:
  104. * 1.) A sequence of alpha characters
  105. * 2.) A sequence of numeric characters
  106. * 3.) A single whitespace character if whitespace is not ignored
  107. * 4.) A single character that does not fall into the above 3 categories
  108. */
  109. enum base_token_type {
  110. BASETOKEN_NONE,
  111. BASETOKEN_ALPHA,
  112. BASETOKEN_DIGIT,
  113. BASETOKEN_WHITESPACE,
  114. BASETOKEN_OTHER,
  115. };
  116. struct base_token {
  117. struct strref text;
  118. enum base_token_type type;
  119. bool passed_whitespace;
  120. };
  121. static inline void base_token_clear(struct base_token *t)
  122. {
  123. memset(t, 0, sizeof(struct base_token));
  124. }
  125. static inline void base_token_copy(struct base_token *dst,
  126. struct base_token *src)
  127. {
  128. memcpy(dst, src, sizeof(struct base_token));
  129. }
  130. /* ------------------------------------------------------------------------- */
  131. #define LEVEL_ERROR 0
  132. #define LEVEL_WARNING 1
  133. struct error_item {
  134. char *error;
  135. const char *file;
  136. uint32_t row, column;
  137. int level;
  138. };
  139. static inline void error_item_init(struct error_item *ei)
  140. {
  141. memset(ei, 0, sizeof(struct error_item));
  142. }
  143. static inline void error_item_free(struct error_item *ei)
  144. {
  145. bfree(ei->error);
  146. error_item_init(ei);
  147. }
  148. static inline void error_item_array_free(struct error_item *array, size_t num)
  149. {
  150. size_t i;
  151. for (i = 0; i < num; i++)
  152. error_item_free(array+i);
  153. }
  154. /* ------------------------------------------------------------------------- */
  155. struct error_data {
  156. DARRAY(struct error_item) errors;
  157. };
  158. static inline void error_data_init(struct error_data *data)
  159. {
  160. da_init(data->errors);
  161. }
  162. static inline void error_data_free(struct error_data *data)
  163. {
  164. error_item_array_free(data->errors.array, data->errors.num);
  165. da_free(data->errors);
  166. }
  167. static inline const struct error_item *error_data_item(struct error_data *ed,
  168. size_t idx)
  169. {
  170. return ed->errors.array+idx;
  171. }
  172. EXPORT char *error_data_buildstring(struct error_data *ed);
  173. EXPORT void error_data_add(struct error_data *ed, const char *file,
  174. uint32_t row, uint32_t column, const char *msg, int level);
  175. static inline size_t error_data_type_count(struct error_data *ed,
  176. int type)
  177. {
  178. size_t count = 0, i;
  179. for (i = 0; i < ed->errors.num; i++) {
  180. if (ed->errors.array[i].level == type)
  181. count++;
  182. }
  183. return count;
  184. }
  185. static inline bool error_data_has_errors(struct error_data *ed)
  186. {
  187. size_t i;
  188. for (i = 0; i < ed->errors.num; i++)
  189. if (ed->errors.array[i].level == LEVEL_ERROR)
  190. return true;
  191. return false;
  192. }
  193. /* ------------------------------------------------------------------------- */
  194. struct lexer {
  195. char *text;
  196. const char *offset;
  197. };
  198. static inline void lexer_init(struct lexer *lex)
  199. {
  200. memset(lex, 0, sizeof(struct lexer));
  201. }
  202. static inline void lexer_free(struct lexer *lex)
  203. {
  204. bfree(lex->text);
  205. lexer_init(lex);
  206. }
  207. static inline void lexer_start(struct lexer *lex, const char *text)
  208. {
  209. lexer_free(lex);
  210. lex->text = bstrdup(text);
  211. lex->offset = lex->text;
  212. }
  213. static inline void lexer_start_move(struct lexer *lex, char *text)
  214. {
  215. lexer_free(lex);
  216. lex->text = text;
  217. lex->offset = lex->text;
  218. }
  219. static inline void lexer_reset(struct lexer *lex)
  220. {
  221. lex->offset = lex->text;
  222. }
  223. enum ignore_whitespace {
  224. PARSE_WHITESPACE,
  225. IGNORE_WHITESPACE
  226. };
  227. EXPORT bool lexer_getbasetoken(struct lexer *lex, struct base_token *t,
  228. enum ignore_whitespace iws);
  229. EXPORT void lexer_getstroffset(const struct lexer *lex, const char *str,
  230. uint32_t *row, uint32_t *col);
  231. #ifdef __cplusplus
  232. }
  233. #endif