lexer.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. #include <ctype.h>
  17. #include "lexer.h"
  18. static const char *astrblank = "";
  19. static inline bool strref_is_empty(const struct strref *str)
  20. {
  21. return !str || !str->array || !str->len || !*str->array;
  22. }
  23. int strref_cmp(const struct strref *str1, const char *str2)
  24. {
  25. size_t i = 0;
  26. if (strref_is_empty(str1))
  27. return (!str2 || !*str2) ? 0 : -1;
  28. if (!str2)
  29. str2 = astrblank;
  30. do {
  31. char ch1, ch2;
  32. ch1 = (i < str1->len) ? str1->array[i] : 0;
  33. ch2 = *str2;
  34. if (ch1 < ch2)
  35. return -1;
  36. else if (ch1 > ch2)
  37. return 1;
  38. } while (i++ < str1->len && *str2++);
  39. return 0;
  40. }
  41. int strref_cmpi(const struct strref *str1, const char *str2)
  42. {
  43. size_t i = 0;
  44. if (strref_is_empty(str1))
  45. return (!str2 || !*str2) ? 0 : -1;
  46. if (!str2)
  47. str2 = astrblank;
  48. do {
  49. char ch1, ch2;
  50. ch1 = (i < str1->len) ? (char)toupper(str1->array[i]) : 0;
  51. ch2 = (char)toupper(*str2);
  52. if (ch1 < ch2)
  53. return -1;
  54. else if (ch1 > ch2)
  55. return 1;
  56. } while (i++ < str1->len && *str2++);
  57. return 0;
  58. }
  59. int strref_cmp_strref(const struct strref *str1, const struct strref *str2)
  60. {
  61. size_t i = 0;
  62. if (strref_is_empty(str1))
  63. return strref_is_empty(str2) ? 0 : -1;
  64. if (strref_is_empty(str2))
  65. return -1;
  66. do {
  67. char ch1, ch2;
  68. ch1 = (i < str1->len) ? str1->array[i] : 0;
  69. ch2 = (i < str2->len) ? str2->array[i] : 0;
  70. if (ch1 < ch2)
  71. return -1;
  72. else if (ch1 > ch2)
  73. return 1;
  74. i++;
  75. } while (i <= str1->len && i <= str2->len);
  76. return 0;
  77. }
  78. int strref_cmpi_strref(const struct strref *str1, const struct strref *str2)
  79. {
  80. size_t i = 0;
  81. if (strref_is_empty(str1))
  82. return strref_is_empty(str2) ? 0 : -1;
  83. if (strref_is_empty(str2))
  84. return -1;
  85. do {
  86. char ch1, ch2;
  87. ch1 = (i < str1->len) ? (char)toupper(str1->array[i]) : 0;
  88. ch2 = (i < str2->len) ? (char)toupper(str2->array[i]) : 0;
  89. if (ch1 < ch2)
  90. return -1;
  91. else if (ch1 > ch2)
  92. return 1;
  93. i++;
  94. } while (i <= str1->len && i <= str2->len);
  95. return 0;
  96. }
  97. /* ------------------------------------------------------------------------- */
  98. bool valid_int_str(const char *str, size_t n)
  99. {
  100. bool found_num = false;
  101. if (!str)
  102. return false;
  103. if (!*str)
  104. return false;
  105. if (!n)
  106. n = strlen(str);
  107. if (*str == '-' || *str == '+')
  108. ++str;
  109. do {
  110. if (*str > '9' || *str < '0')
  111. return false;
  112. found_num = true;
  113. } while(*++str && --n);
  114. return found_num;
  115. }
  116. bool valid_float_str(const char *str, size_t n)
  117. {
  118. bool found_num = false;
  119. bool found_exp = false;
  120. bool found_dec = false;
  121. if (!str)
  122. return false;
  123. if (!*str)
  124. return false;
  125. if (!n)
  126. n = strlen(str);
  127. if (*str == '-' || *str == '+')
  128. ++str;
  129. do {
  130. if (*str == '.') {
  131. if (found_dec || found_exp || !found_num)
  132. return false;
  133. found_dec = true;
  134. } else if (*str == 'e') {
  135. if (found_exp || !found_num)
  136. return false;
  137. found_exp = true;
  138. found_num = false;
  139. } else if (*str == '-' || *str == '+') {
  140. if (!found_exp || !found_num)
  141. return false;
  142. } else if (*str > '9' || *str < '0') {
  143. return false;
  144. } else {
  145. found_num = true;
  146. }
  147. } while(*++str && --n);
  148. return found_num;
  149. }
  150. /* ------------------------------------------------------------------------- */
  151. void error_data_add(struct error_data *data, const char *file,
  152. uint32_t row, uint32_t column, const char *msg, int level)
  153. {
  154. struct error_item item;
  155. if (!data)
  156. return;
  157. item.file = file;
  158. item.row = row;
  159. item.column = column;
  160. item.level = level;
  161. item.error = bstrdup(msg);
  162. da_push_back(data->errors, &item);
  163. }
  164. char *error_data_buildstring(struct error_data *ed)
  165. {
  166. struct dstr str;
  167. struct error_item *items = ed->errors.array;
  168. size_t i;
  169. dstr_init(&str);
  170. for (i = 0; i < ed->errors.num; i++) {
  171. struct error_item *item = items+i;
  172. dstr_catf(&str, "%s (%u, %u): %s\n", item->file, item->row,
  173. item->column, item->error);
  174. }
  175. return str.array;
  176. }
  177. /* ------------------------------------------------------------------------- */
  178. static inline enum base_token_type get_char_token_type(const char ch)
  179. {
  180. if (is_whitespace(ch))
  181. return BASETOKEN_WHITESPACE;
  182. else if (ch >= '0' && ch <= '9')
  183. return BASETOKEN_DIGIT;
  184. else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
  185. return BASETOKEN_ALPHA;
  186. return BASETOKEN_OTHER;
  187. }
  188. bool lexer_getbasetoken(struct lexer *lex, struct base_token *token,
  189. enum ignore_whitespace iws)
  190. {
  191. const char *offset = lex->offset;
  192. const char *token_start = NULL;
  193. enum base_token_type type = BASETOKEN_NONE;
  194. bool ignore_whitespace = (iws == IGNORE_WHITESPACE);
  195. if (!offset)
  196. return false;
  197. while (*offset != 0) {
  198. char ch = *(offset++);
  199. enum base_token_type new_type = get_char_token_type(ch);
  200. if (type == BASETOKEN_NONE) {
  201. if (new_type == BASETOKEN_WHITESPACE &&
  202. ignore_whitespace)
  203. continue;
  204. token_start = offset-1;
  205. type = new_type;
  206. if (type != BASETOKEN_DIGIT &&
  207. type != BASETOKEN_ALPHA) {
  208. if (is_newline(ch) &&
  209. is_newline_pair(ch, *offset)) {
  210. offset++;
  211. }
  212. break;
  213. }
  214. } else if (type != new_type) {
  215. offset--;
  216. break;
  217. }
  218. }
  219. lex->offset = offset;
  220. if (token_start && offset > token_start) {
  221. strref_set(&token->text, token_start, offset-token_start);
  222. token->type = type;
  223. return true;
  224. }
  225. return false;
  226. }
  227. void lexer_getstroffset(const struct lexer *lex, const char *str,
  228. uint32_t *row, uint32_t *col)
  229. {
  230. uint32_t cur_col = 1, cur_row = 1;
  231. const char *text = lex->text;
  232. if (!str)
  233. return;
  234. while (text < str) {
  235. if (is_newline(*text)) {
  236. text += newline_size(text)-1;
  237. cur_col = 1;
  238. cur_row++;
  239. } else {
  240. cur_col++;
  241. }
  242. text++;
  243. }
  244. *row = cur_row;
  245. *col = cur_col;
  246. }