lexer.h 6.4 KB

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