lexer.h 6.5 KB

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