NCDConfigTokenizer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /**
  2. * @file NCDConfigTokenizer.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <string.h>
  30. #include <stddef.h>
  31. #include <stdlib.h>
  32. #include <misc/debug.h>
  33. #include <misc/string_begins_with.h>
  34. #include <misc/balloc.h>
  35. #include <misc/expstring.h>
  36. #include <misc/parse_number.h>
  37. #include <base/BLog.h>
  38. #include <ncd/NCDConfigTokenizer.h>
  39. #include <generated/blog_channel_NCDConfigTokenizer.h>
  40. static int is_name_char (char c)
  41. {
  42. return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_');
  43. }
  44. static int is_name_first_char (char c)
  45. {
  46. return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_');
  47. }
  48. static int is_space_char (char c)
  49. {
  50. return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
  51. }
  52. static int string_equals (char *str, int str_len, char *needle)
  53. {
  54. return (str_len == strlen(needle) && !memcmp(str, needle, str_len));
  55. }
  56. void NCDConfigTokenizer_Tokenize (MemRef the_str, NCDConfigTokenizer_output output, void *user)
  57. {
  58. char const *str = the_str.ptr;
  59. size_t left = the_str.len;
  60. size_t line = 1;
  61. size_t line_char = 1;
  62. while (left > 0) {
  63. size_t l;
  64. int error = 0;
  65. int token;
  66. void *token_val = NULL;
  67. size_t token_len = 0;
  68. if (*str == '#') {
  69. l = 1;
  70. while (l < left && str[l] != '\n') {
  71. l++;
  72. }
  73. token = 0;
  74. }
  75. else if (l = data_begins_with(str, left, "{")) {
  76. token = NCD_TOKEN_CURLY_OPEN;
  77. }
  78. else if (l = data_begins_with(str, left, "}")) {
  79. token = NCD_TOKEN_CURLY_CLOSE;
  80. }
  81. else if (l = data_begins_with(str, left, "(")) {
  82. token = NCD_TOKEN_ROUND_OPEN;
  83. }
  84. else if (l = data_begins_with(str, left, ")")) {
  85. token = NCD_TOKEN_ROUND_CLOSE;
  86. }
  87. else if (l = data_begins_with(str, left, ";")) {
  88. token = NCD_TOKEN_SEMICOLON;
  89. }
  90. else if (l = data_begins_with(str, left, ".")) {
  91. token = NCD_TOKEN_DOT;
  92. }
  93. else if (l = data_begins_with(str, left, ",")) {
  94. token = NCD_TOKEN_COMMA;
  95. }
  96. else if (l = data_begins_with(str, left, ":")) {
  97. token = NCD_TOKEN_COLON;
  98. }
  99. else if (l = data_begins_with(str, left, "[")) {
  100. token = NCD_TOKEN_BRACKET_OPEN;
  101. }
  102. else if (l = data_begins_with(str, left, "]")) {
  103. token = NCD_TOKEN_BRACKET_CLOSE;
  104. }
  105. else if (l = data_begins_with(str, left, "@")) {
  106. token = NCD_TOKEN_AT;
  107. }
  108. else if (l = data_begins_with(str, left, "^")) {
  109. token = NCD_TOKEN_CARET;
  110. }
  111. else if (l = data_begins_with(str, left, "->")) {
  112. token = NCD_TOKEN_ARROW;
  113. }
  114. else if (l = data_begins_with(str, left, "If")) {
  115. token = NCD_TOKEN_IF;
  116. }
  117. else if (l = data_begins_with(str, left, "Elif")) {
  118. token = NCD_TOKEN_ELIF;
  119. }
  120. else if (l = data_begins_with(str, left, "elif")) {
  121. token = NCD_TOKEN_ELIF;
  122. }
  123. else if (l = data_begins_with(str, left, "Else")) {
  124. token = NCD_TOKEN_ELSE;
  125. }
  126. else if (l = data_begins_with(str, left, "else")) {
  127. token = NCD_TOKEN_ELSE;
  128. }
  129. else if (l = data_begins_with(str, left, "Foreach")) {
  130. token = NCD_TOKEN_FOREACH;
  131. }
  132. else if (l = data_begins_with(str, left, "As")) {
  133. token = NCD_TOKEN_AS;
  134. }
  135. else if (l = data_begins_with(str, left, "Block")) {
  136. token = NCD_TOKEN_BLOCK;
  137. }
  138. else if (l = data_begins_with(str, left, "Do")) {
  139. token = NCD_TOKEN_DO;
  140. }
  141. else if (l = data_begins_with(str, left, "include_guard")) {
  142. token = NCD_TOKEN_INCLUDE_GUARD;
  143. }
  144. else if (l = data_begins_with(str, left, "include")) {
  145. token = NCD_TOKEN_INCLUDE;
  146. }
  147. else if (is_name_first_char(*str)) {
  148. l = 1;
  149. while (l < left && is_name_char(str[l])) {
  150. l++;
  151. }
  152. // allocate buffer
  153. bsize_t bufsize = bsize_add(bsize_fromsize(l), bsize_fromint(1));
  154. char *buf;
  155. if (bufsize.is_overflow || !(buf = malloc(bufsize.value))) {
  156. BLog(BLOG_ERROR, "malloc failed");
  157. error = 1;
  158. goto out;
  159. }
  160. // copy and terminate
  161. memcpy(buf, str, l);
  162. buf[l] = '\0';
  163. if (!strcmp(buf, "process")) {
  164. token = NCD_TOKEN_PROCESS;
  165. free(buf);
  166. }
  167. else if (!strcmp(buf, "template")) {
  168. token = NCD_TOKEN_TEMPLATE;
  169. free(buf);
  170. }
  171. else {
  172. token = NCD_TOKEN_NAME;
  173. token_val = buf;
  174. token_len = l;
  175. }
  176. }
  177. else if (*str == '"') do {
  178. // init string
  179. ExpString estr;
  180. if (!ExpString_Init(&estr)) {
  181. BLog(BLOG_ERROR, "ExpString_Init failed");
  182. goto string_fail0;
  183. }
  184. // skip start quote
  185. l = 1;
  186. // decode string
  187. while (l < left) {
  188. uint8_t dec_ch;
  189. // get character
  190. if (str[l] == '\\') {
  191. if (left - l < 2) {
  192. BLog(BLOG_ERROR, "escape character found in string but nothing follows");
  193. goto string_fail1;
  194. }
  195. size_t extra = 0;
  196. switch (str[l + 1]) {
  197. case '\'':
  198. case '\"':
  199. case '\\':
  200. case '\?':
  201. dec_ch = str[l + 1]; break;
  202. case 'a':
  203. dec_ch = '\a'; break;
  204. case 'b':
  205. dec_ch = '\b'; break;
  206. case 'f':
  207. dec_ch = '\f'; break;
  208. case 'n':
  209. dec_ch = '\n'; break;
  210. case 'r':
  211. dec_ch = '\r'; break;
  212. case 't':
  213. dec_ch = '\t'; break;
  214. case 'v':
  215. dec_ch = '\v'; break;
  216. case '0':
  217. dec_ch = 0; break;
  218. case 'x': {
  219. if (left - l < 4) {
  220. BLog(BLOG_ERROR, "hexadecimal escape found in string but too little characters follow");
  221. goto string_fail1;
  222. }
  223. uintmax_t hex_val;
  224. if (!parse_unsigned_hex_integer(MemRef_Make(&str[l + 2], 2), &hex_val)) {
  225. BLog(BLOG_ERROR, "hexadecimal escape found in string but two hex characters don't follow");
  226. goto string_fail1;
  227. }
  228. dec_ch = hex_val;
  229. extra = 2;
  230. } break;
  231. default:
  232. BLog(BLOG_ERROR, "bad escape sequence in string");
  233. goto string_fail1;
  234. }
  235. l += 2 + extra;
  236. }
  237. else if (str[l] == '"') {
  238. break;
  239. }
  240. else {
  241. dec_ch = str[l];
  242. l++;
  243. }
  244. // append character to string
  245. if (!ExpString_AppendByte(&estr, dec_ch)) {
  246. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  247. goto string_fail1;
  248. }
  249. }
  250. // make sure ending quote was found
  251. if (l == left) {
  252. BLog(BLOG_ERROR, "missing ending quote for string");
  253. goto string_fail1;
  254. }
  255. // skip ending quote
  256. l++;
  257. token = NCD_TOKEN_STRING;
  258. token_val = ExpString_Get(&estr);
  259. token_len = ExpString_Length(&estr);
  260. break;
  261. string_fail1:
  262. ExpString_Free(&estr);
  263. string_fail0:
  264. error = 1;
  265. } while (0);
  266. else if (is_space_char(*str)) {
  267. token = 0;
  268. l = 1;
  269. }
  270. else {
  271. BLog(BLOG_ERROR, "unrecognized character");
  272. error = 1;
  273. }
  274. out:
  275. // report error
  276. if (error) {
  277. output(user, NCD_ERROR, NULL, 0, line, line_char);
  278. return;
  279. }
  280. // output token
  281. if (token) {
  282. if (!output(user, token, token_val, token_len, line, line_char)) {
  283. return;
  284. }
  285. }
  286. // update line/char counters
  287. for (size_t i = 0; i < l; i++) {
  288. if (str[i] == '\n') {
  289. line++;
  290. line_char = 1;
  291. } else {
  292. line_char++;
  293. }
  294. }
  295. str += l;
  296. left -= l;
  297. }
  298. output(user, NCD_EOF, NULL, 0, line, line_char);
  299. }