seq.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  1. /* A packrat parser generated by PackCC 1.5.0 */
  2. #ifdef _MSC_VER
  3. #undef _CRT_SECURE_NO_WARNINGS
  4. #define _CRT_SECURE_NO_WARNINGS
  5. #endif /* _MSC_VER */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #ifndef _MSC_VER
  10. #if defined __GNUC__ && defined _WIN32 /* MinGW */
  11. #ifndef PCC_USE_SYSTEM_STRNLEN
  12. #define strnlen(str, maxlen) pcc_strnlen(str, maxlen)
  13. static size_t pcc_strnlen(const char *str, size_t maxlen) {
  14. size_t i;
  15. for (i = 0; i < maxlen && str[i]; i++);
  16. return i;
  17. }
  18. #endif /* !PCC_USE_SYSTEM_STRNLEN */
  19. #endif /* defined __GNUC__ && defined _WIN32 */
  20. #endif /* !_MSC_VER */
  21. #define PCC_DBG_EVALUATE 0
  22. #define PCC_DBG_MATCH 1
  23. #define PCC_DBG_NOMATCH 2
  24. #include "seq.h"
  25. #include <stdio.h>
  26. int nlines= 1;
  27. #if 1
  28. static const char *dbg_str[] = { "Evaluating rule", "Matched rule", "Abandoning rule" };
  29. #define PCC_DEBUG(event, rule, level, pos, buffer, length) \
  30. fprintf(stdout, "%*s%s %s @%d [%.*s]\n", level * 2, "", dbg_str[event], rule, pos, length, buffer)
  31. /* NOTE: To guarantee the output order, stderr, which can lead a race condition with stdout, is not used. */
  32. fflush(stdout);
  33. #endif
  34. #ifndef PCC_BUFFERSIZE
  35. #define PCC_BUFFERSIZE 256
  36. #endif /* !PCC_BUFFERSIZE */
  37. #ifndef PCC_ARRAYSIZE
  38. #define PCC_ARRAYSIZE 2
  39. #endif /* !PCC_ARRAYSIZE */
  40. #define VOID_VALUE (~(size_t)0)
  41. typedef enum pcc_bool_tag {
  42. PCC_FALSE = 0,
  43. PCC_TRUE
  44. } pcc_bool_t;
  45. typedef struct pcc_char_array_tag {
  46. char *buf;
  47. size_t max;
  48. size_t len;
  49. } pcc_char_array_t;
  50. typedef struct pcc_range_tag {
  51. size_t start;
  52. size_t end;
  53. } pcc_range_t;
  54. typedef int pcc_value_t;
  55. typedef void *pcc_auxil_t;
  56. typedef struct pcc_value_table_tag {
  57. pcc_value_t *buf;
  58. size_t max;
  59. size_t len;
  60. } pcc_value_table_t;
  61. typedef struct pcc_value_refer_table_tag {
  62. pcc_value_t **buf;
  63. size_t max;
  64. size_t len;
  65. } pcc_value_refer_table_t;
  66. typedef struct pcc_capture_tag {
  67. pcc_range_t range;
  68. char *string; /* mutable */
  69. } pcc_capture_t;
  70. typedef struct pcc_capture_table_tag {
  71. pcc_capture_t *buf;
  72. size_t max;
  73. size_t len;
  74. } pcc_capture_table_t;
  75. typedef struct pcc_capture_const_table_tag {
  76. const pcc_capture_t **buf;
  77. size_t max;
  78. size_t len;
  79. } pcc_capture_const_table_t;
  80. typedef struct pcc_thunk_tag pcc_thunk_t;
  81. typedef struct pcc_thunk_array_tag pcc_thunk_array_t;
  82. typedef void (*pcc_action_t)(seq_context_t *, pcc_thunk_t *, pcc_value_t *);
  83. typedef enum pcc_thunk_type_tag {
  84. PCC_THUNK_LEAF,
  85. PCC_THUNK_NODE
  86. } pcc_thunk_type_t;
  87. typedef struct pcc_thunk_leaf_tag {
  88. pcc_value_refer_table_t values;
  89. pcc_capture_const_table_t capts;
  90. pcc_capture_t capt0;
  91. pcc_action_t action;
  92. } pcc_thunk_leaf_t;
  93. typedef struct pcc_thunk_node_tag {
  94. const pcc_thunk_array_t *thunks; /* just a reference */
  95. pcc_value_t *value; /* just a reference */
  96. } pcc_thunk_node_t;
  97. typedef union pcc_thunk_data_tag {
  98. pcc_thunk_leaf_t leaf;
  99. pcc_thunk_node_t node;
  100. } pcc_thunk_data_t;
  101. struct pcc_thunk_tag {
  102. pcc_thunk_type_t type;
  103. pcc_thunk_data_t data;
  104. };
  105. struct pcc_thunk_array_tag {
  106. pcc_thunk_t **buf;
  107. size_t max;
  108. size_t len;
  109. };
  110. typedef struct pcc_thunk_chunk_tag {
  111. pcc_value_table_t values;
  112. pcc_capture_table_t capts;
  113. pcc_thunk_array_t thunks;
  114. size_t pos; /* the starting position in the character buffer */
  115. } pcc_thunk_chunk_t;
  116. typedef struct pcc_lr_entry_tag pcc_lr_entry_t;
  117. typedef enum pcc_lr_answer_type_tag {
  118. PCC_LR_ANSWER_LR,
  119. PCC_LR_ANSWER_CHUNK
  120. } pcc_lr_answer_type_t;
  121. typedef union pcc_lr_answer_data_tag {
  122. pcc_lr_entry_t *lr;
  123. pcc_thunk_chunk_t *chunk;
  124. } pcc_lr_answer_data_t;
  125. typedef struct pcc_lr_answer_tag pcc_lr_answer_t;
  126. struct pcc_lr_answer_tag {
  127. pcc_lr_answer_type_t type;
  128. pcc_lr_answer_data_t data;
  129. size_t pos; /* the absolute position in the input */
  130. pcc_lr_answer_t *hold;
  131. };
  132. typedef pcc_thunk_chunk_t *(*pcc_rule_t)(seq_context_t *);
  133. typedef struct pcc_rule_set_tag {
  134. pcc_rule_t *buf;
  135. size_t max;
  136. size_t len;
  137. } pcc_rule_set_t;
  138. typedef struct pcc_lr_head_tag pcc_lr_head_t;
  139. struct pcc_lr_head_tag {
  140. pcc_rule_t rule;
  141. pcc_rule_set_t invol;
  142. pcc_rule_set_t eval;
  143. pcc_lr_head_t *hold;
  144. };
  145. typedef struct pcc_lr_memo_tag {
  146. pcc_rule_t rule;
  147. pcc_lr_answer_t *answer;
  148. } pcc_lr_memo_t;
  149. typedef struct pcc_lr_memo_map_tag {
  150. pcc_lr_memo_t *buf;
  151. size_t max;
  152. size_t len;
  153. } pcc_lr_memo_map_t;
  154. typedef struct pcc_lr_table_entry_tag {
  155. pcc_lr_head_t *head; /* just a reference */
  156. pcc_lr_memo_map_t memos;
  157. pcc_lr_answer_t *hold_a;
  158. pcc_lr_head_t *hold_h;
  159. } pcc_lr_table_entry_t;
  160. typedef struct pcc_lr_table_tag {
  161. pcc_lr_table_entry_t **buf;
  162. size_t max;
  163. size_t len;
  164. } pcc_lr_table_t;
  165. struct pcc_lr_entry_tag {
  166. pcc_rule_t rule;
  167. pcc_thunk_chunk_t *seed; /* just a reference */
  168. pcc_lr_head_t *head; /* just a reference */
  169. };
  170. typedef struct pcc_lr_stack_tag {
  171. pcc_lr_entry_t **buf;
  172. size_t max;
  173. size_t len;
  174. } pcc_lr_stack_t;
  175. struct seq_context_tag {
  176. size_t pos; /* the position in the input of the first character currently buffered */
  177. size_t cur; /* the current parsing position in the character buffer */
  178. size_t level;
  179. pcc_char_array_t buffer;
  180. pcc_lr_table_t lrtable;
  181. pcc_lr_stack_t lrstack;
  182. pcc_auxil_t auxil;
  183. };
  184. #ifndef PCC_ERROR
  185. #define PCC_ERROR(auxil) pcc_error()
  186. static void pcc_error(void) {
  187. fprintf(stderr, "Syntax error\n");
  188. exit(1);
  189. }
  190. #endif /* !PCC_ERROR */
  191. #ifndef PCC_GETCHAR
  192. #define PCC_GETCHAR(auxil) getchar()
  193. #endif /* !PCC_GETCHAR */
  194. #ifndef PCC_MALLOC
  195. #define PCC_MALLOC(auxil, size) pcc_malloc_e(size)
  196. static void *pcc_malloc_e(size_t size) {
  197. void *const p = malloc(size);
  198. if (p == NULL) {
  199. fprintf(stderr, "Out of memory\n");
  200. exit(1);
  201. }
  202. return p;
  203. }
  204. #endif /* !PCC_MALLOC */
  205. #ifndef PCC_REALLOC
  206. #define PCC_REALLOC(auxil, ptr, size) pcc_realloc_e(ptr, size)
  207. static void *pcc_realloc_e(void *ptr, size_t size) {
  208. void *const p = realloc(ptr, size);
  209. if (p == NULL) {
  210. fprintf(stderr, "Out of memory\n");
  211. exit(1);
  212. }
  213. return p;
  214. }
  215. #endif /* !PCC_REALLOC */
  216. #ifndef PCC_FREE
  217. #define PCC_FREE(auxil, ptr) free(ptr)
  218. #endif /* !PCC_FREE */
  219. #ifndef PCC_DEBUG
  220. #define PCC_DEBUG(event, rule, level, pos, buffer, length) ((void)0)
  221. #endif /* !PCC_DEBUG */
  222. static char *pcc_strndup_e(pcc_auxil_t auxil, const char *str, size_t len) {
  223. const size_t m = strnlen(str, len);
  224. char *const s = (char *)PCC_MALLOC(auxil, m + 1);
  225. memcpy(s, str, m);
  226. s[m] = '\0';
  227. return s;
  228. }
  229. static void pcc_char_array__init(pcc_auxil_t auxil, pcc_char_array_t *array, size_t max) {
  230. array->len = 0;
  231. array->max = max;
  232. array->buf = (char *)PCC_MALLOC(auxil, array->max);
  233. }
  234. static void pcc_char_array__add(pcc_auxil_t auxil, pcc_char_array_t *array, char ch) {
  235. if (array->max <= array->len) {
  236. const size_t n = array->len + 1;
  237. size_t m = array->max;
  238. if (m == 0) m = 1;
  239. while (m < n && m != 0) m <<= 1;
  240. if (m == 0) m = n;
  241. array->buf = (char *)PCC_REALLOC(auxil, array->buf, m);
  242. array->max = m;
  243. }
  244. array->buf[array->len++] = ch;
  245. }
  246. static void pcc_char_array__term(pcc_auxil_t auxil, pcc_char_array_t *array) {
  247. PCC_FREE(auxil, array->buf);
  248. }
  249. static void pcc_value_table__init(pcc_auxil_t auxil, pcc_value_table_t *table, size_t max) {
  250. table->len = 0;
  251. table->max = max;
  252. table->buf = (pcc_value_t *)PCC_MALLOC(auxil, sizeof(pcc_value_t) * table->max);
  253. }
  254. static void pcc_value_table__resize(pcc_auxil_t auxil, pcc_value_table_t *table, size_t len) {
  255. if (table->max < len) {
  256. size_t m = table->max;
  257. if (m == 0) m = 1;
  258. while (m < len && m != 0) m <<= 1;
  259. if (m == 0) m = len;
  260. table->buf = (pcc_value_t *)PCC_REALLOC(auxil, table->buf, sizeof(pcc_value_t) * m);
  261. table->max = m;
  262. }
  263. table->len = len;
  264. }
  265. static void pcc_value_table__term(pcc_auxil_t auxil, pcc_value_table_t *table) {
  266. PCC_FREE(auxil, table->buf);
  267. }
  268. static void pcc_value_refer_table__init(pcc_auxil_t auxil, pcc_value_refer_table_t *table, size_t max) {
  269. table->len = 0;
  270. table->max = max;
  271. table->buf = (pcc_value_t **)PCC_MALLOC(auxil, sizeof(pcc_value_t *) * table->max);
  272. }
  273. static void pcc_value_refer_table__resize(pcc_auxil_t auxil, pcc_value_refer_table_t *table, size_t len) {
  274. size_t i;
  275. if (table->max < len) {
  276. size_t m = table->max;
  277. if (m == 0) m = 1;
  278. while (m < len && m != 0) m <<= 1;
  279. if (m == 0) m = len;
  280. table->buf = (pcc_value_t **)PCC_REALLOC(auxil, table->buf, sizeof(pcc_value_t *) * m);
  281. table->max = m;
  282. }
  283. for (i = table->len; i < len; i++) table->buf[i] = NULL;
  284. table->len = len;
  285. }
  286. static void pcc_value_refer_table__term(pcc_auxil_t auxil, pcc_value_refer_table_t *table) {
  287. PCC_FREE(auxil, table->buf);
  288. }
  289. static void pcc_capture_table__init(pcc_auxil_t auxil, pcc_capture_table_t *table, size_t max) {
  290. table->len = 0;
  291. table->max = max;
  292. table->buf = (pcc_capture_t *)PCC_MALLOC(auxil, sizeof(pcc_capture_t) * table->max);
  293. }
  294. static void pcc_capture_table__resize(pcc_auxil_t auxil, pcc_capture_table_t *table, size_t len) {
  295. size_t i;
  296. for (i = len; i < table->len; i++) PCC_FREE(auxil, table->buf[i].string);
  297. if (table->max < len) {
  298. size_t m = table->max;
  299. if (m == 0) m = 1;
  300. while (m < len && m != 0) m <<= 1;
  301. if (m == 0) m = len;
  302. table->buf = (pcc_capture_t *)PCC_REALLOC(auxil, table->buf, sizeof(pcc_capture_t) * m);
  303. table->max = m;
  304. }
  305. for (i = table->len; i < len; i++) {
  306. table->buf[i].range.start = 0;
  307. table->buf[i].range.end = 0;
  308. table->buf[i].string = NULL;
  309. }
  310. table->len = len;
  311. }
  312. static void pcc_capture_table__term(pcc_auxil_t auxil, pcc_capture_table_t *table) {
  313. while (table->len > 0) {
  314. table->len--;
  315. PCC_FREE(auxil, table->buf[table->len].string);
  316. }
  317. PCC_FREE(auxil, table->buf);
  318. }
  319. static void pcc_capture_const_table__init(pcc_auxil_t auxil, pcc_capture_const_table_t *table, size_t max) {
  320. table->len = 0;
  321. table->max = max;
  322. table->buf = (const pcc_capture_t **)PCC_MALLOC(auxil, sizeof(const pcc_capture_t *) * table->max);
  323. }
  324. static void pcc_capture_const_table__resize(pcc_auxil_t auxil, pcc_capture_const_table_t *table, size_t len) {
  325. size_t i;
  326. if (table->max < len) {
  327. size_t m = table->max;
  328. if (m == 0) m = 1;
  329. while (m < len && m != 0) m <<= 1;
  330. if (m == 0) m = len;
  331. table->buf = (const pcc_capture_t **)PCC_REALLOC(auxil, (pcc_capture_t **)table->buf, sizeof(const pcc_capture_t *) * m);
  332. table->max = m;
  333. }
  334. for (i = table->len; i < len; i++) table->buf[i] = NULL;
  335. table->len = len;
  336. }
  337. static void pcc_capture_const_table__term(pcc_auxil_t auxil, pcc_capture_const_table_t *table) {
  338. PCC_FREE(auxil, table->buf);
  339. }
  340. static pcc_thunk_t *pcc_thunk__create_leaf(pcc_auxil_t auxil, pcc_action_t action, size_t valuec, size_t captc) {
  341. pcc_thunk_t *const thunk = (pcc_thunk_t *)PCC_MALLOC(auxil, sizeof(pcc_thunk_t));
  342. thunk->type = PCC_THUNK_LEAF;
  343. pcc_value_refer_table__init(auxil, &thunk->data.leaf.values, valuec);
  344. pcc_value_refer_table__resize(auxil, &thunk->data.leaf.values, valuec);
  345. pcc_capture_const_table__init(auxil, &thunk->data.leaf.capts, captc);
  346. pcc_capture_const_table__resize(auxil, &thunk->data.leaf.capts, captc);
  347. thunk->data.leaf.capt0.range.start = 0;
  348. thunk->data.leaf.capt0.range.end = 0;
  349. thunk->data.leaf.capt0.string = NULL;
  350. thunk->data.leaf.action = action;
  351. return thunk;
  352. }
  353. static pcc_thunk_t *pcc_thunk__create_node(pcc_auxil_t auxil, const pcc_thunk_array_t *thunks, pcc_value_t *value) {
  354. pcc_thunk_t *const thunk = (pcc_thunk_t *)PCC_MALLOC(auxil, sizeof(pcc_thunk_t));
  355. thunk->type = PCC_THUNK_NODE;
  356. thunk->data.node.thunks = thunks;
  357. thunk->data.node.value = value;
  358. return thunk;
  359. }
  360. static void pcc_thunk__destroy(pcc_auxil_t auxil, pcc_thunk_t *thunk) {
  361. if (thunk == NULL) return;
  362. switch (thunk->type) {
  363. case PCC_THUNK_LEAF:
  364. PCC_FREE(auxil, thunk->data.leaf.capt0.string);
  365. pcc_capture_const_table__term(auxil, &thunk->data.leaf.capts);
  366. pcc_value_refer_table__term(auxil, &thunk->data.leaf.values);
  367. break;
  368. case PCC_THUNK_NODE:
  369. break;
  370. default: /* unknown */
  371. break;
  372. }
  373. PCC_FREE(auxil, thunk);
  374. }
  375. static void pcc_thunk_array__init(pcc_auxil_t auxil, pcc_thunk_array_t *array, size_t max) {
  376. array->len = 0;
  377. array->max = max;
  378. array->buf = (pcc_thunk_t **)PCC_MALLOC(auxil, sizeof(pcc_thunk_t *) * array->max);
  379. }
  380. static void pcc_thunk_array__add(pcc_auxil_t auxil, pcc_thunk_array_t *array, pcc_thunk_t *thunk) {
  381. if (array->max <= array->len) {
  382. const size_t n = array->len + 1;
  383. size_t m = array->max;
  384. if (m == 0) m = 1;
  385. while (m < n && m != 0) m <<= 1;
  386. if (m == 0) m = n;
  387. array->buf = (pcc_thunk_t **)PCC_REALLOC(auxil, array->buf, sizeof(pcc_thunk_t *) * m);
  388. array->max = m;
  389. }
  390. array->buf[array->len++] = thunk;
  391. }
  392. static void pcc_thunk_array__revert(pcc_auxil_t auxil, pcc_thunk_array_t *array, size_t len) {
  393. while (array->len > len) {
  394. array->len--;
  395. pcc_thunk__destroy(auxil, array->buf[array->len]);
  396. }
  397. }
  398. static void pcc_thunk_array__term(pcc_auxil_t auxil, pcc_thunk_array_t *array) {
  399. while (array->len > 0) {
  400. array->len--;
  401. pcc_thunk__destroy(auxil, array->buf[array->len]);
  402. }
  403. PCC_FREE(auxil, array->buf);
  404. }
  405. static pcc_thunk_chunk_t *pcc_thunk_chunk__create(pcc_auxil_t auxil) {
  406. pcc_thunk_chunk_t *const chunk = (pcc_thunk_chunk_t *)PCC_MALLOC(auxil, sizeof(pcc_thunk_chunk_t));
  407. pcc_value_table__init(auxil, &chunk->values, PCC_ARRAYSIZE);
  408. pcc_capture_table__init(auxil, &chunk->capts, PCC_ARRAYSIZE);
  409. pcc_thunk_array__init(auxil, &chunk->thunks, PCC_ARRAYSIZE);
  410. chunk->pos = 0;
  411. return chunk;
  412. }
  413. static void pcc_thunk_chunk__destroy(pcc_auxil_t auxil, pcc_thunk_chunk_t *chunk) {
  414. if (chunk == NULL) return;
  415. pcc_thunk_array__term(auxil, &chunk->thunks);
  416. pcc_capture_table__term(auxil, &chunk->capts);
  417. pcc_value_table__term(auxil, &chunk->values);
  418. PCC_FREE(auxil, chunk);
  419. }
  420. static void pcc_rule_set__init(pcc_auxil_t auxil, pcc_rule_set_t *set, size_t max) {
  421. set->len = 0;
  422. set->max = max;
  423. set->buf = (pcc_rule_t *)PCC_MALLOC(auxil, sizeof(pcc_rule_t) * set->max);
  424. }
  425. static size_t pcc_rule_set__index(pcc_auxil_t auxil, const pcc_rule_set_t *set, pcc_rule_t rule) {
  426. size_t i;
  427. for (i = 0; i < set->len; i++) {
  428. if (set->buf[i] == rule) return i;
  429. }
  430. return VOID_VALUE;
  431. }
  432. static pcc_bool_t pcc_rule_set__add(pcc_auxil_t auxil, pcc_rule_set_t *set, pcc_rule_t rule) {
  433. const size_t i = pcc_rule_set__index(auxil, set, rule);
  434. if (i != VOID_VALUE) return PCC_FALSE;
  435. if (set->max <= set->len) {
  436. const size_t n = set->len + 1;
  437. size_t m = set->max;
  438. if (m == 0) m = 1;
  439. while (m < n && m != 0) m <<= 1;
  440. if (m == 0) m = n;
  441. set->buf = (pcc_rule_t *)PCC_REALLOC(auxil, set->buf, sizeof(pcc_rule_t) * m);
  442. set->max = m;
  443. }
  444. set->buf[set->len++] = rule;
  445. return PCC_TRUE;
  446. }
  447. static pcc_bool_t pcc_rule_set__remove(pcc_auxil_t auxil, pcc_rule_set_t *set, pcc_rule_t rule) {
  448. const size_t i = pcc_rule_set__index(auxil, set, rule);
  449. if (i == VOID_VALUE) return PCC_FALSE;
  450. memmove(set->buf + i, set->buf + (i + 1), sizeof(pcc_rule_t) * (set->len - (i + 1)));
  451. return PCC_TRUE;
  452. }
  453. static void pcc_rule_set__clear(pcc_auxil_t auxil, pcc_rule_set_t *set) {
  454. set->len = 0;
  455. }
  456. static void pcc_rule_set__copy(pcc_auxil_t auxil, pcc_rule_set_t *set, const pcc_rule_set_t *src) {
  457. size_t i;
  458. pcc_rule_set__clear(auxil, set);
  459. for (i = 0; i < src->len; i++) {
  460. pcc_rule_set__add(auxil, set, src->buf[i]);
  461. }
  462. }
  463. static void pcc_rule_set__term(pcc_auxil_t auxil, pcc_rule_set_t *set) {
  464. PCC_FREE(auxil, set->buf);
  465. }
  466. static pcc_lr_head_t *pcc_lr_head__create(pcc_auxil_t auxil, pcc_rule_t rule) {
  467. pcc_lr_head_t *const head = (pcc_lr_head_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_head_t));
  468. head->rule = rule;
  469. pcc_rule_set__init(auxil, &head->invol, PCC_ARRAYSIZE);
  470. pcc_rule_set__init(auxil, &head->eval, PCC_ARRAYSIZE);
  471. head->hold = NULL;
  472. return head;
  473. }
  474. static void pcc_lr_head__destroy(pcc_auxil_t auxil, pcc_lr_head_t *head) {
  475. if (head == NULL) return;
  476. pcc_lr_head__destroy(auxil, head->hold);
  477. pcc_rule_set__term(auxil, &head->eval);
  478. pcc_rule_set__term(auxil, &head->invol);
  479. PCC_FREE(auxil, head);
  480. }
  481. static void pcc_lr_entry__destroy(pcc_auxil_t auxil, pcc_lr_entry_t *lr);
  482. static pcc_lr_answer_t *pcc_lr_answer__create(pcc_auxil_t auxil, pcc_lr_answer_type_t type, size_t pos) {
  483. pcc_lr_answer_t *answer = (pcc_lr_answer_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_answer_t));
  484. answer->type = type;
  485. answer->pos = pos;
  486. answer->hold = NULL;
  487. switch (answer->type) {
  488. case PCC_LR_ANSWER_LR:
  489. answer->data.lr = NULL;
  490. break;
  491. case PCC_LR_ANSWER_CHUNK:
  492. answer->data.chunk = NULL;
  493. break;
  494. default: /* unknown */
  495. PCC_FREE(auxil, answer);
  496. answer = NULL;
  497. }
  498. return answer;
  499. }
  500. static void pcc_lr_answer__set_chunk(pcc_auxil_t auxil, pcc_lr_answer_t *answer, pcc_thunk_chunk_t *chunk) {
  501. pcc_lr_answer_t *const a = pcc_lr_answer__create(auxil, answer->type, answer->pos);
  502. switch (answer->type) {
  503. case PCC_LR_ANSWER_LR:
  504. a->data.lr = answer->data.lr;
  505. break;
  506. case PCC_LR_ANSWER_CHUNK:
  507. a->data.chunk = answer->data.chunk;
  508. break;
  509. default: /* unknown */
  510. break;
  511. }
  512. a->hold = answer->hold;
  513. answer->hold = a;
  514. answer->type = PCC_LR_ANSWER_CHUNK;
  515. answer->data.chunk = chunk;
  516. }
  517. static void pcc_lr_answer__destroy(pcc_auxil_t auxil, pcc_lr_answer_t *answer) {
  518. if (answer == NULL) return;
  519. pcc_lr_answer__destroy(auxil, answer->hold);
  520. switch (answer->type) {
  521. case PCC_LR_ANSWER_LR:
  522. pcc_lr_entry__destroy(auxil, answer->data.lr);
  523. break;
  524. case PCC_LR_ANSWER_CHUNK:
  525. pcc_thunk_chunk__destroy(auxil, answer->data.chunk);
  526. break;
  527. default: /* unknown */
  528. break;
  529. }
  530. PCC_FREE(auxil, answer);
  531. }
  532. static void pcc_lr_memo_map__init(pcc_auxil_t auxil, pcc_lr_memo_map_t *map, size_t max) {
  533. map->len = 0;
  534. map->max = max;
  535. map->buf = (pcc_lr_memo_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_memo_t) * map->max);
  536. }
  537. static size_t pcc_lr_memo_map__index(pcc_auxil_t auxil, pcc_lr_memo_map_t *map, pcc_rule_t rule) {
  538. size_t i;
  539. for (i = 0; i < map->len; i++) {
  540. if (map->buf[i].rule == rule) return i;
  541. }
  542. return VOID_VALUE;
  543. }
  544. static void pcc_lr_memo_map__put(pcc_auxil_t auxil, pcc_lr_memo_map_t *map, pcc_rule_t rule, pcc_lr_answer_t *answer) {
  545. const size_t i = pcc_lr_memo_map__index(auxil, map, rule);
  546. if (i != VOID_VALUE) {
  547. pcc_lr_answer__destroy(auxil, map->buf[i].answer);
  548. map->buf[i].answer = answer;
  549. }
  550. else {
  551. if (map->max <= map->len) {
  552. const size_t n = map->len + 1;
  553. size_t m = map->max;
  554. if (m == 0) m = 1;
  555. while (m < n && m != 0) m <<= 1;
  556. if (m == 0) m = n;
  557. map->buf = (pcc_lr_memo_t *)PCC_REALLOC(auxil, map->buf, sizeof(pcc_lr_memo_t) * m);
  558. map->max = m;
  559. }
  560. map->buf[map->len].rule = rule;
  561. map->buf[map->len].answer = answer;
  562. map->len++;
  563. }
  564. }
  565. static pcc_lr_answer_t *pcc_lr_memo_map__get(pcc_auxil_t auxil, pcc_lr_memo_map_t *map, pcc_rule_t rule) {
  566. const size_t i = pcc_lr_memo_map__index(auxil, map, rule);
  567. return (i != VOID_VALUE) ? map->buf[i].answer : NULL;
  568. }
  569. static void pcc_lr_memo_map__term(pcc_auxil_t auxil, pcc_lr_memo_map_t *map) {
  570. while (map->len > 0) {
  571. map->len--;
  572. pcc_lr_answer__destroy(auxil, map->buf[map->len].answer);
  573. }
  574. PCC_FREE(auxil, map->buf);
  575. }
  576. static pcc_lr_table_entry_t *pcc_lr_table_entry__create(pcc_auxil_t auxil) {
  577. pcc_lr_table_entry_t *const entry = (pcc_lr_table_entry_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_table_entry_t));
  578. entry->head = NULL;
  579. pcc_lr_memo_map__init(auxil, &entry->memos, PCC_ARRAYSIZE);
  580. entry->hold_a = NULL;
  581. entry->hold_h = NULL;
  582. return entry;
  583. }
  584. static void pcc_lr_table_entry__destroy(pcc_auxil_t auxil, pcc_lr_table_entry_t *entry) {
  585. if (entry == NULL) return;
  586. pcc_lr_head__destroy(auxil, entry->hold_h);
  587. pcc_lr_answer__destroy(auxil, entry->hold_a);
  588. pcc_lr_memo_map__term(auxil, &entry->memos);
  589. PCC_FREE(auxil, entry);
  590. }
  591. static void pcc_lr_table__init(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t max) {
  592. table->len = 0;
  593. table->max = max;
  594. table->buf = (pcc_lr_table_entry_t **)PCC_MALLOC(auxil, sizeof(pcc_lr_table_entry_t *) * table->max);
  595. }
  596. static void pcc_lr_table__resize(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t len) {
  597. size_t i;
  598. for (i = len; i < table->len; i++) pcc_lr_table_entry__destroy(auxil, table->buf[i]);
  599. if (table->max < len) {
  600. size_t m = table->max;
  601. if (m == 0) m = 1;
  602. while (m < len && m != 0) m <<= 1;
  603. if (m == 0) m = len;
  604. table->buf = (pcc_lr_table_entry_t **)PCC_REALLOC(auxil, table->buf, sizeof(pcc_lr_table_entry_t *) * m);
  605. table->max = m;
  606. }
  607. for (i = table->len; i < len; i++) table->buf[i] = NULL;
  608. table->len = len;
  609. }
  610. static void pcc_lr_table__set_head(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index, pcc_lr_head_t *head) {
  611. if (index >= table->len) pcc_lr_table__resize(auxil, table, index + 1);
  612. if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(auxil);
  613. table->buf[index]->head = head;
  614. }
  615. static void pcc_lr_table__hold_head(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index, pcc_lr_head_t *head) {
  616. if (index >= table->len) pcc_lr_table__resize(auxil, table, index + 1);
  617. if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(auxil);
  618. head->hold = table->buf[index]->hold_h;
  619. table->buf[index]->hold_h = head;
  620. }
  621. static void pcc_lr_table__set_answer(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index, pcc_rule_t rule, pcc_lr_answer_t *answer) {
  622. if (index >= table->len) pcc_lr_table__resize(auxil, table, index + 1);
  623. if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(auxil);
  624. pcc_lr_memo_map__put(auxil, &table->buf[index]->memos, rule, answer);
  625. }
  626. static void pcc_lr_table__hold_answer(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index, pcc_lr_answer_t *answer) {
  627. if (index >= table->len) pcc_lr_table__resize(auxil, table, index + 1);
  628. if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(auxil);
  629. answer->hold = table->buf[index]->hold_a;
  630. table->buf[index]->hold_a = answer;
  631. }
  632. static pcc_lr_head_t *pcc_lr_table__get_head(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index) {
  633. if (index >= table->len || table->buf[index] == NULL) return NULL;
  634. return table->buf[index]->head;
  635. }
  636. static pcc_lr_answer_t *pcc_lr_table__get_answer(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index, pcc_rule_t rule) {
  637. if (index >= table->len || table->buf[index] == NULL) return NULL;
  638. return pcc_lr_memo_map__get(auxil, &table->buf[index]->memos, rule);
  639. }
  640. static void pcc_lr_table__shift(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t count) {
  641. size_t i;
  642. if (count > table->len) count = table->len;
  643. for (i = 0; i < count; i++) pcc_lr_table_entry__destroy(auxil, table->buf[i]);
  644. memmove(table->buf, table->buf + count, sizeof(pcc_lr_table_entry_t *) * (table->len - count));
  645. table->len -= count;
  646. }
  647. static void pcc_lr_table__term(pcc_auxil_t auxil, pcc_lr_table_t *table) {
  648. while (table->len > 0) {
  649. table->len--;
  650. pcc_lr_table_entry__destroy(auxil, table->buf[table->len]);
  651. }
  652. PCC_FREE(auxil, table->buf);
  653. }
  654. static pcc_lr_entry_t *pcc_lr_entry__create(pcc_auxil_t auxil, pcc_rule_t rule) {
  655. pcc_lr_entry_t *const lr = (pcc_lr_entry_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_entry_t));
  656. lr->rule = rule;
  657. lr->seed = NULL;
  658. lr->head = NULL;
  659. return lr;
  660. }
  661. static void pcc_lr_entry__destroy(pcc_auxil_t auxil, pcc_lr_entry_t *lr) {
  662. PCC_FREE(auxil, lr);
  663. }
  664. static void pcc_lr_stack__init(pcc_auxil_t auxil, pcc_lr_stack_t *stack, size_t max) {
  665. stack->len = 0;
  666. stack->max = max;
  667. stack->buf = (pcc_lr_entry_t **)PCC_MALLOC(auxil, sizeof(pcc_lr_entry_t *) * stack->max);
  668. }
  669. static void pcc_lr_stack__push(pcc_auxil_t auxil, pcc_lr_stack_t *stack, pcc_lr_entry_t *lr) {
  670. if (stack->max <= stack->len) {
  671. const size_t n = stack->len + 1;
  672. size_t m = stack->max;
  673. if (m == 0) m = 1;
  674. while (m < n && m != 0) m <<= 1;
  675. if (m == 0) m = n;
  676. stack->buf = (pcc_lr_entry_t **)PCC_REALLOC(auxil, stack->buf, sizeof(pcc_lr_entry_t *) * m);
  677. stack->max = m;
  678. }
  679. stack->buf[stack->len++] = lr;
  680. }
  681. static pcc_lr_entry_t *pcc_lr_stack__pop(pcc_auxil_t auxil, pcc_lr_stack_t *stack) {
  682. return stack->buf[--stack->len];
  683. }
  684. static void pcc_lr_stack__term(pcc_auxil_t auxil, pcc_lr_stack_t *stack) {
  685. PCC_FREE(auxil, stack->buf);
  686. }
  687. static seq_context_t *pcc_context__create(pcc_auxil_t auxil) {
  688. seq_context_t *const ctx = (seq_context_t *)PCC_MALLOC(auxil, sizeof(seq_context_t));
  689. ctx->pos = 0;
  690. ctx->cur = 0;
  691. ctx->level = 0;
  692. pcc_char_array__init(auxil, &ctx->buffer, PCC_BUFFERSIZE);
  693. pcc_lr_table__init(auxil, &ctx->lrtable, PCC_BUFFERSIZE);
  694. pcc_lr_stack__init(auxil, &ctx->lrstack, PCC_ARRAYSIZE);
  695. ctx->auxil = auxil;
  696. return ctx;
  697. }
  698. static void pcc_context__destroy(seq_context_t *ctx) {
  699. if (ctx == NULL) return;
  700. pcc_lr_stack__term(ctx->auxil, &ctx->lrstack);
  701. pcc_lr_table__term(ctx->auxil, &ctx->lrtable);
  702. pcc_char_array__term(ctx->auxil, &ctx->buffer);
  703. PCC_FREE(ctx->auxil, ctx);
  704. }
  705. static size_t pcc_refill_buffer(seq_context_t *ctx, size_t num) {
  706. if (ctx->buffer.len >= ctx->cur + num) return ctx->buffer.len - ctx->cur;
  707. while (ctx->buffer.len < ctx->cur + num) {
  708. const int c = PCC_GETCHAR(ctx->auxil);
  709. if (c == EOF) break;
  710. pcc_char_array__add(ctx->auxil, &ctx->buffer, (char)c);
  711. }
  712. return ctx->buffer.len - ctx->cur;
  713. }
  714. static void pcc_commit_buffer(seq_context_t *ctx) {
  715. memmove(ctx->buffer.buf, ctx->buffer.buf + ctx->cur, ctx->buffer.len - ctx->cur);
  716. ctx->buffer.len -= ctx->cur;
  717. ctx->pos += ctx->cur;
  718. pcc_lr_table__shift(ctx->auxil, &ctx->lrtable, ctx->cur);
  719. ctx->cur = 0;
  720. }
  721. static const char *pcc_get_capture_string(seq_context_t *ctx, const pcc_capture_t *capt) {
  722. if (capt->string == NULL)
  723. ((pcc_capture_t *)capt)->string =
  724. pcc_strndup_e(ctx->auxil, ctx->buffer.buf + capt->range.start, capt->range.end - capt->range.start);
  725. return capt->string;
  726. }
  727. static size_t pcc_get_char_as_utf32(seq_context_t *ctx, int *out) { /* with checking UTF-8 validity */
  728. int c, u;
  729. size_t n;
  730. if (pcc_refill_buffer(ctx, 1) < 1) return 0;
  731. c = (int)(unsigned char)ctx->buffer.buf[ctx->cur];
  732. n = (c < 0x80) ? 1 :
  733. ((c & 0xe0) == 0xc0) ? 2 :
  734. ((c & 0xf0) == 0xe0) ? 3 :
  735. ((c & 0xf8) == 0xf0) ? 4 : 0;
  736. if (n < 1) return 0;
  737. if (pcc_refill_buffer(ctx, n) < n) return 0;
  738. switch (n) {
  739. case 1:
  740. u = c;
  741. break;
  742. case 2:
  743. u = c & 0x1f;
  744. c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 1];
  745. if ((c & 0xc0) != 0x80) return 0;
  746. u <<= 6; u |= c & 0x3f;
  747. if (u < 0x80) return 0;
  748. break;
  749. case 3:
  750. u = c & 0x0f;
  751. c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 1];
  752. if ((c & 0xc0) != 0x80) return 0;
  753. u <<= 6; u |= c & 0x3f;
  754. c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 2];
  755. if ((c & 0xc0) != 0x80) return 0;
  756. u <<= 6; u |= c & 0x3f;
  757. if (u < 0x800) return 0;
  758. break;
  759. case 4:
  760. u = c & 0x07;
  761. c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 1];
  762. if ((c & 0xc0) != 0x80) return 0;
  763. u <<= 6; u |= c & 0x3f;
  764. c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 2];
  765. if ((c & 0xc0) != 0x80) return 0;
  766. u <<= 6; u |= c & 0x3f;
  767. c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 3];
  768. if ((c & 0xc0) != 0x80) return 0;
  769. u <<= 6; u |= c & 0x3f;
  770. if (u < 0x10000 || u > 0x10ffff) return 0;
  771. break;
  772. default:
  773. return 0;
  774. }
  775. if (out) *out = u;
  776. return n;
  777. }
  778. static pcc_bool_t pcc_apply_rule(seq_context_t *ctx, pcc_rule_t rule, pcc_thunk_array_t *thunks, pcc_value_t *value) {
  779. static pcc_value_t null;
  780. pcc_thunk_chunk_t *c = NULL;
  781. const size_t p = ctx->pos + ctx->cur;
  782. pcc_bool_t b = PCC_TRUE;
  783. pcc_lr_answer_t *a = pcc_lr_table__get_answer(ctx->auxil, &ctx->lrtable, p, rule);
  784. pcc_lr_head_t *h = pcc_lr_table__get_head(ctx->auxil, &ctx->lrtable, p);
  785. if (h != NULL) {
  786. if (a == NULL && rule != h->rule && pcc_rule_set__index(ctx->auxil, &h->invol, rule) == VOID_VALUE) {
  787. b = PCC_FALSE;
  788. c = NULL;
  789. }
  790. else if (pcc_rule_set__remove(ctx->auxil, &h->eval, rule)) {
  791. b = PCC_FALSE;
  792. c = rule(ctx);
  793. a = pcc_lr_answer__create(ctx->auxil, PCC_LR_ANSWER_CHUNK, ctx->pos + ctx->cur);
  794. a->data.chunk = c;
  795. pcc_lr_table__hold_answer(ctx->auxil, &ctx->lrtable, p, a);
  796. }
  797. }
  798. if (b) {
  799. if (a != NULL) {
  800. ctx->cur = a->pos - ctx->pos;
  801. switch (a->type) {
  802. case PCC_LR_ANSWER_LR:
  803. if (a->data.lr->head == NULL) {
  804. a->data.lr->head = pcc_lr_head__create(ctx->auxil, rule);
  805. pcc_lr_table__hold_head(ctx->auxil, &ctx->lrtable, p, a->data.lr->head);
  806. }
  807. {
  808. size_t i = ctx->lrstack.len;
  809. while (i > 0) {
  810. i--;
  811. if (ctx->lrstack.buf[i]->head == a->data.lr->head) break;
  812. ctx->lrstack.buf[i]->head = a->data.lr->head;
  813. pcc_rule_set__add(ctx->auxil, &a->data.lr->head->invol, ctx->lrstack.buf[i]->rule);
  814. }
  815. }
  816. c = a->data.lr->seed;
  817. break;
  818. case PCC_LR_ANSWER_CHUNK:
  819. c = a->data.chunk;
  820. break;
  821. default: /* unknown */
  822. break;
  823. }
  824. }
  825. else {
  826. pcc_lr_entry_t *const e = pcc_lr_entry__create(ctx->auxil, rule);
  827. pcc_lr_stack__push(ctx->auxil, &ctx->lrstack, e);
  828. a = pcc_lr_answer__create(ctx->auxil, PCC_LR_ANSWER_LR, p);
  829. a->data.lr = e;
  830. pcc_lr_table__set_answer(ctx->auxil, &ctx->lrtable, p, rule, a);
  831. c = rule(ctx);
  832. pcc_lr_stack__pop(ctx->auxil, &ctx->lrstack);
  833. a->pos = ctx->pos + ctx->cur;
  834. if (e->head == NULL) {
  835. pcc_lr_answer__set_chunk(ctx->auxil, a, c);
  836. }
  837. else {
  838. e->seed = c;
  839. h = a->data.lr->head;
  840. if (h->rule != rule) {
  841. c = a->data.lr->seed;
  842. a = pcc_lr_answer__create(ctx->auxil, PCC_LR_ANSWER_CHUNK, ctx->pos + ctx->cur);
  843. a->data.chunk = c;
  844. pcc_lr_table__hold_answer(ctx->auxil, &ctx->lrtable, p, a);
  845. }
  846. else {
  847. pcc_lr_answer__set_chunk(ctx->auxil, a, a->data.lr->seed);
  848. if (a->data.chunk == NULL) {
  849. c = NULL;
  850. }
  851. else {
  852. pcc_lr_table__set_head(ctx->auxil, &ctx->lrtable, p, h);
  853. for (;;) {
  854. ctx->cur = p - ctx->pos;
  855. pcc_rule_set__copy(ctx->auxil, &h->eval, &h->invol);
  856. c = rule(ctx);
  857. if (c == NULL || ctx->pos + ctx->cur <= a->pos) break;
  858. pcc_lr_answer__set_chunk(ctx->auxil, a, c);
  859. a->pos = ctx->pos + ctx->cur;
  860. }
  861. pcc_thunk_chunk__destroy(ctx->auxil, c);
  862. pcc_lr_table__set_head(ctx->auxil, &ctx->lrtable, p, NULL);
  863. ctx->cur = a->pos - ctx->pos;
  864. c = a->data.chunk;
  865. }
  866. }
  867. }
  868. }
  869. }
  870. if (c == NULL) return PCC_FALSE;
  871. if (value == NULL) value = &null;
  872. memset(value, 0, sizeof(pcc_value_t)); /* in case */
  873. pcc_thunk_array__add(ctx->auxil, thunks, pcc_thunk__create_node(ctx->auxil, &c->thunks, value));
  874. return PCC_TRUE;
  875. }
  876. static void pcc_do_action(seq_context_t *ctx, const pcc_thunk_array_t *thunks, pcc_value_t *value) {
  877. size_t i;
  878. for (i = 0; i < thunks->len; i++) {
  879. pcc_thunk_t *const thunk = thunks->buf[i];
  880. switch (thunk->type) {
  881. case PCC_THUNK_LEAF:
  882. thunk->data.leaf.action(ctx, thunk, value);
  883. break;
  884. case PCC_THUNK_NODE:
  885. pcc_do_action(ctx, thunk->data.node.thunks, thunk->data.node.value);
  886. break;
  887. default: /* unknown */
  888. break;
  889. }
  890. }
  891. }
  892. static void pcc_action_id_0(seq_context_t *__pcc_ctx, pcc_thunk_t *__pcc_in, pcc_value_t *__pcc_out) {
  893. #define auxil (__pcc_ctx->auxil)
  894. #define __ (*__pcc_out)
  895. #define _0 pcc_get_capture_string(__pcc_ctx, &__pcc_in->data.leaf.capt0)
  896. #define _0s ((const size_t)__pcc_in->data.leaf.capt0.range.start)
  897. #define _0e ((const size_t)__pcc_in->data.leaf.capt0.range.end)
  898. #define _1 pcc_get_capture_string(__pcc_ctx, __pcc_in->data.leaf.capts.buf[0])
  899. #define _1s ((const size_t)(__pcc_ctx->pos + __pcc_in->data.leaf.capts.buf[0]->range.start))
  900. #define _1e ((const size_t)(__pcc_ctx->pos + __pcc_in->data.leaf.capts.buf[0]->range.end))
  901. printf("ID=%s\n",_0);
  902. #undef _1e
  903. #undef _1s
  904. #undef _1
  905. #undef _0e
  906. #undef _0s
  907. #undef _0
  908. #undef __
  909. #undef auxil
  910. }
  911. static void pcc_action_endofline_0(seq_context_t *__pcc_ctx, pcc_thunk_t *__pcc_in, pcc_value_t *__pcc_out) {
  912. #define auxil (__pcc_ctx->auxil)
  913. #define __ (*__pcc_out)
  914. #define _0 pcc_get_capture_string(__pcc_ctx, &__pcc_in->data.leaf.capt0)
  915. #define _0s ((const size_t)__pcc_in->data.leaf.capt0.range.start)
  916. #define _0e ((const size_t)__pcc_in->data.leaf.capt0.range.end)
  917. nlines++;
  918. #undef _0e
  919. #undef _0s
  920. #undef _0
  921. #undef __
  922. #undef auxil
  923. }
  924. static pcc_thunk_chunk_t *pcc_evaluate_rule_list(seq_context_t *ctx);
  925. static pcc_thunk_chunk_t *pcc_evaluate_rule_some_digits(seq_context_t *ctx);
  926. static pcc_thunk_chunk_t *pcc_evaluate_rule_list2(seq_context_t *ctx);
  927. static pcc_thunk_chunk_t *pcc_evaluate_rule_pair(seq_context_t *ctx);
  928. static pcc_thunk_chunk_t *pcc_evaluate_rule_digit(seq_context_t *ctx);
  929. static pcc_thunk_chunk_t *pcc_evaluate_rule_id(seq_context_t *ctx);
  930. static pcc_thunk_chunk_t *pcc_evaluate_rule__(seq_context_t *ctx);
  931. static pcc_thunk_chunk_t *pcc_evaluate_rule_space(seq_context_t *ctx);
  932. static pcc_thunk_chunk_t *pcc_evaluate_rule_comment(seq_context_t *ctx);
  933. static pcc_thunk_chunk_t *pcc_evaluate_rule_endofline(seq_context_t *ctx);
  934. static pcc_thunk_chunk_t *pcc_evaluate_rule_list(seq_context_t *ctx) {
  935. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  936. chunk->pos = ctx->cur;
  937. PCC_DEBUG(PCC_DBG_EVALUATE, "list", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  938. ctx->level++;
  939. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  940. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  941. if (
  942. pcc_refill_buffer(ctx, 1) < 1 ||
  943. ctx->buffer.buf[ctx->cur] != '['
  944. ) goto L0000;
  945. ctx->cur++;
  946. if (!pcc_apply_rule(ctx, pcc_evaluate_rule__, &chunk->thunks, NULL)) goto L0000;
  947. {
  948. int i;
  949. for (i = 0;; i++) {
  950. const size_t p = ctx->cur;
  951. const size_t n = chunk->thunks.len;
  952. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_some_digits, &chunk->thunks, NULL)) goto L0001;
  953. if (ctx->cur == p) break;
  954. continue;
  955. L0001:;
  956. ctx->cur = p;
  957. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  958. break;
  959. }
  960. }
  961. if (!pcc_apply_rule(ctx, pcc_evaluate_rule__, &chunk->thunks, NULL)) goto L0000;
  962. if (
  963. pcc_refill_buffer(ctx, 1) < 1 ||
  964. ctx->buffer.buf[ctx->cur] != ']'
  965. ) goto L0000;
  966. ctx->cur++;
  967. if (!pcc_apply_rule(ctx, pcc_evaluate_rule__, &chunk->thunks, NULL)) goto L0000;
  968. ctx->level--;
  969. PCC_DEBUG(PCC_DBG_MATCH, "list", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  970. return chunk;
  971. L0000:;
  972. ctx->level--;
  973. PCC_DEBUG(PCC_DBG_NOMATCH, "list", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  974. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  975. return NULL;
  976. }
  977. static pcc_thunk_chunk_t *pcc_evaluate_rule_some_digits(seq_context_t *ctx) {
  978. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  979. chunk->pos = ctx->cur;
  980. PCC_DEBUG(PCC_DBG_EVALUATE, "some_digits", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  981. ctx->level++;
  982. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  983. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  984. {
  985. const size_t p = ctx->cur;
  986. const size_t n = chunk->thunks.len;
  987. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_id, &chunk->thunks, NULL)) goto L0002;
  988. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_list2, &chunk->thunks, NULL)) goto L0002;
  989. goto L0001;
  990. L0002:;
  991. ctx->cur = p;
  992. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  993. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_pair, &chunk->thunks, NULL)) goto L0003;
  994. goto L0001;
  995. L0003:;
  996. ctx->cur = p;
  997. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  998. goto L0000;
  999. L0001:;
  1000. }
  1001. ctx->level--;
  1002. PCC_DEBUG(PCC_DBG_MATCH, "some_digits", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1003. return chunk;
  1004. L0000:;
  1005. ctx->level--;
  1006. PCC_DEBUG(PCC_DBG_NOMATCH, "some_digits", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1007. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1008. return NULL;
  1009. }
  1010. static pcc_thunk_chunk_t *pcc_evaluate_rule_list2(seq_context_t *ctx) {
  1011. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1012. chunk->pos = ctx->cur;
  1013. PCC_DEBUG(PCC_DBG_EVALUATE, "list2", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1014. ctx->level++;
  1015. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1016. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1017. if (
  1018. pcc_refill_buffer(ctx, 1) < 1 ||
  1019. ctx->buffer.buf[ctx->cur] != '['
  1020. ) goto L0000;
  1021. ctx->cur++;
  1022. if (!pcc_apply_rule(ctx, pcc_evaluate_rule__, &chunk->thunks, NULL)) goto L0000;
  1023. {
  1024. int i;
  1025. for (i = 0;; i++) {
  1026. const size_t p = ctx->cur;
  1027. const size_t n = chunk->thunks.len;
  1028. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_some_digits, &chunk->thunks, NULL)) goto L0001;
  1029. if (ctx->cur == p) break;
  1030. continue;
  1031. L0001:;
  1032. ctx->cur = p;
  1033. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1034. break;
  1035. }
  1036. }
  1037. if (!pcc_apply_rule(ctx, pcc_evaluate_rule__, &chunk->thunks, NULL)) goto L0000;
  1038. if (
  1039. pcc_refill_buffer(ctx, 1) < 1 ||
  1040. ctx->buffer.buf[ctx->cur] != ']'
  1041. ) goto L0000;
  1042. ctx->cur++;
  1043. if (!pcc_apply_rule(ctx, pcc_evaluate_rule__, &chunk->thunks, NULL)) goto L0000;
  1044. ctx->level--;
  1045. PCC_DEBUG(PCC_DBG_MATCH, "list2", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1046. return chunk;
  1047. L0000:;
  1048. ctx->level--;
  1049. PCC_DEBUG(PCC_DBG_NOMATCH, "list2", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1050. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1051. return NULL;
  1052. }
  1053. static pcc_thunk_chunk_t *pcc_evaluate_rule_pair(seq_context_t *ctx) {
  1054. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1055. chunk->pos = ctx->cur;
  1056. PCC_DEBUG(PCC_DBG_EVALUATE, "pair", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1057. ctx->level++;
  1058. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1059. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1060. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_id, &chunk->thunks, NULL)) goto L0000;
  1061. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_digit, &chunk->thunks, NULL)) goto L0000;
  1062. ctx->level--;
  1063. PCC_DEBUG(PCC_DBG_MATCH, "pair", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1064. return chunk;
  1065. L0000:;
  1066. ctx->level--;
  1067. PCC_DEBUG(PCC_DBG_NOMATCH, "pair", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1068. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1069. return NULL;
  1070. }
  1071. static pcc_thunk_chunk_t *pcc_evaluate_rule_digit(seq_context_t *ctx) {
  1072. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1073. chunk->pos = ctx->cur;
  1074. PCC_DEBUG(PCC_DBG_EVALUATE, "digit", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1075. ctx->level++;
  1076. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1077. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1078. {
  1079. const size_t p0 = ctx->cur;
  1080. const size_t n0 = chunk->thunks.len;
  1081. int i;
  1082. for (i = 0;; i++) {
  1083. const size_t p = ctx->cur;
  1084. const size_t n = chunk->thunks.len;
  1085. {
  1086. int u;
  1087. const size_t n = pcc_get_char_as_utf32(ctx, &u);
  1088. if (n == 0) goto L0001;
  1089. if (!(
  1090. (u >= 0x000030 && u <= 0x000039)
  1091. )) goto L0001;
  1092. ctx->cur += n;
  1093. }
  1094. if (ctx->cur == p) break;
  1095. continue;
  1096. L0001:;
  1097. ctx->cur = p;
  1098. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1099. break;
  1100. }
  1101. if (i < 1) {
  1102. ctx->cur = p0;
  1103. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n0);
  1104. goto L0000;
  1105. }
  1106. }
  1107. if (!pcc_apply_rule(ctx, pcc_evaluate_rule__, &chunk->thunks, NULL)) goto L0000;
  1108. ctx->level--;
  1109. PCC_DEBUG(PCC_DBG_MATCH, "digit", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1110. return chunk;
  1111. L0000:;
  1112. ctx->level--;
  1113. PCC_DEBUG(PCC_DBG_NOMATCH, "digit", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1114. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1115. return NULL;
  1116. }
  1117. static pcc_thunk_chunk_t *pcc_evaluate_rule_id(seq_context_t *ctx) {
  1118. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1119. chunk->pos = ctx->cur;
  1120. PCC_DEBUG(PCC_DBG_EVALUATE, "id", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1121. ctx->level++;
  1122. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1123. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 1);
  1124. {
  1125. const size_t p = ctx->cur;
  1126. size_t q;
  1127. {
  1128. const size_t p0 = ctx->cur;
  1129. const size_t n0 = chunk->thunks.len;
  1130. int i;
  1131. for (i = 0;; i++) {
  1132. const size_t p = ctx->cur;
  1133. const size_t n = chunk->thunks.len;
  1134. {
  1135. int u;
  1136. const size_t n = pcc_get_char_as_utf32(ctx, &u);
  1137. if (n == 0) goto L0001;
  1138. if (!(
  1139. (u >= 0x000061 && u <= 0x00007a) ||
  1140. (u >= 0x000041 && u <= 0x00005a) ||
  1141. u == 0x00005f
  1142. )) goto L0001;
  1143. ctx->cur += n;
  1144. }
  1145. if (ctx->cur == p) break;
  1146. continue;
  1147. L0001:;
  1148. ctx->cur = p;
  1149. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1150. break;
  1151. }
  1152. if (i < 1) {
  1153. ctx->cur = p0;
  1154. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n0);
  1155. goto L0000;
  1156. }
  1157. }
  1158. {
  1159. int i;
  1160. for (i = 0;; i++) {
  1161. const size_t p = ctx->cur;
  1162. const size_t n = chunk->thunks.len;
  1163. {
  1164. int u;
  1165. const size_t n = pcc_get_char_as_utf32(ctx, &u);
  1166. if (n == 0) goto L0002;
  1167. if (!(
  1168. (u >= 0x000061 && u <= 0x00007a) ||
  1169. (u >= 0x000041 && u <= 0x00005a) ||
  1170. u == 0x00005f ||
  1171. (u >= 0x000030 && u <= 0x000039)
  1172. )) goto L0002;
  1173. ctx->cur += n;
  1174. }
  1175. if (ctx->cur == p) break;
  1176. continue;
  1177. L0002:;
  1178. ctx->cur = p;
  1179. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1180. break;
  1181. }
  1182. }
  1183. q = ctx->cur;
  1184. chunk->capts.buf[0].range.start = p;
  1185. chunk->capts.buf[0].range.end = q;
  1186. }
  1187. if (!pcc_apply_rule(ctx, pcc_evaluate_rule__, &chunk->thunks, NULL)) goto L0000;
  1188. {
  1189. pcc_thunk_t *const thunk = pcc_thunk__create_leaf(ctx->auxil, pcc_action_id_0, 0, 1);
  1190. thunk->data.leaf.capts.buf[0] = &(chunk->capts.buf[0]);
  1191. thunk->data.leaf.capt0.range.start = chunk->pos;
  1192. thunk->data.leaf.capt0.range.end = ctx->cur;
  1193. pcc_thunk_array__add(ctx->auxil, &chunk->thunks, thunk);
  1194. }
  1195. ctx->level--;
  1196. PCC_DEBUG(PCC_DBG_MATCH, "id", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1197. return chunk;
  1198. L0000:;
  1199. ctx->level--;
  1200. PCC_DEBUG(PCC_DBG_NOMATCH, "id", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1201. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1202. return NULL;
  1203. }
  1204. static pcc_thunk_chunk_t *pcc_evaluate_rule__(seq_context_t *ctx) {
  1205. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1206. chunk->pos = ctx->cur;
  1207. PCC_DEBUG(PCC_DBG_EVALUATE, "_", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1208. ctx->level++;
  1209. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1210. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1211. {
  1212. int i;
  1213. for (i = 0;; i++) {
  1214. const size_t p = ctx->cur;
  1215. const size_t n = chunk->thunks.len;
  1216. {
  1217. const size_t p = ctx->cur;
  1218. const size_t n = chunk->thunks.len;
  1219. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_space, &chunk->thunks, NULL)) goto L0003;
  1220. goto L0002;
  1221. L0003:;
  1222. ctx->cur = p;
  1223. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1224. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_comment, &chunk->thunks, NULL)) goto L0004;
  1225. goto L0002;
  1226. L0004:;
  1227. ctx->cur = p;
  1228. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1229. goto L0001;
  1230. L0002:;
  1231. }
  1232. if (ctx->cur == p) break;
  1233. continue;
  1234. L0001:;
  1235. ctx->cur = p;
  1236. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1237. break;
  1238. }
  1239. }
  1240. ctx->level--;
  1241. PCC_DEBUG(PCC_DBG_MATCH, "_", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1242. return chunk;
  1243. }
  1244. static pcc_thunk_chunk_t *pcc_evaluate_rule_space(seq_context_t *ctx) {
  1245. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1246. chunk->pos = ctx->cur;
  1247. PCC_DEBUG(PCC_DBG_EVALUATE, "space", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1248. ctx->level++;
  1249. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1250. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1251. {
  1252. const size_t p = ctx->cur;
  1253. const size_t n = chunk->thunks.len;
  1254. if (
  1255. pcc_refill_buffer(ctx, 1) < 1 ||
  1256. ctx->buffer.buf[ctx->cur] != ' '
  1257. ) goto L0002;
  1258. ctx->cur++;
  1259. goto L0001;
  1260. L0002:;
  1261. ctx->cur = p;
  1262. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1263. if (
  1264. pcc_refill_buffer(ctx, 1) < 1 ||
  1265. ctx->buffer.buf[ctx->cur] != '\t'
  1266. ) goto L0003;
  1267. ctx->cur++;
  1268. goto L0001;
  1269. L0003:;
  1270. ctx->cur = p;
  1271. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1272. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_endofline, &chunk->thunks, NULL)) goto L0004;
  1273. goto L0001;
  1274. L0004:;
  1275. ctx->cur = p;
  1276. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1277. goto L0000;
  1278. L0001:;
  1279. }
  1280. ctx->level--;
  1281. PCC_DEBUG(PCC_DBG_MATCH, "space", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1282. return chunk;
  1283. L0000:;
  1284. ctx->level--;
  1285. PCC_DEBUG(PCC_DBG_NOMATCH, "space", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1286. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1287. return NULL;
  1288. }
  1289. static pcc_thunk_chunk_t *pcc_evaluate_rule_comment(seq_context_t *ctx) {
  1290. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1291. chunk->pos = ctx->cur;
  1292. PCC_DEBUG(PCC_DBG_EVALUATE, "comment", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1293. ctx->level++;
  1294. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1295. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1296. if (
  1297. pcc_refill_buffer(ctx, 1) < 1 ||
  1298. ctx->buffer.buf[ctx->cur] != '#'
  1299. ) goto L0000;
  1300. ctx->cur++;
  1301. {
  1302. int i;
  1303. for (i = 0;; i++) {
  1304. const size_t p = ctx->cur;
  1305. const size_t n = chunk->thunks.len;
  1306. {
  1307. const size_t p = ctx->cur;
  1308. const size_t n = chunk->thunks.len;
  1309. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_endofline, &chunk->thunks, NULL)) goto L0002;
  1310. ctx->cur = p;
  1311. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1312. goto L0001;
  1313. L0002:;
  1314. ctx->cur = p;
  1315. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1316. }
  1317. {
  1318. int u;
  1319. const size_t n = pcc_get_char_as_utf32(ctx, &u);
  1320. if (n == 0) goto L0001;
  1321. ctx->cur += n;
  1322. }
  1323. if (ctx->cur == p) break;
  1324. continue;
  1325. L0001:;
  1326. ctx->cur = p;
  1327. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1328. break;
  1329. }
  1330. }
  1331. ctx->level--;
  1332. PCC_DEBUG(PCC_DBG_MATCH, "comment", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1333. return chunk;
  1334. L0000:;
  1335. ctx->level--;
  1336. PCC_DEBUG(PCC_DBG_NOMATCH, "comment", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1337. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1338. return NULL;
  1339. }
  1340. static pcc_thunk_chunk_t *pcc_evaluate_rule_endofline(seq_context_t *ctx) {
  1341. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1342. chunk->pos = ctx->cur;
  1343. PCC_DEBUG(PCC_DBG_EVALUATE, "endofline", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1344. ctx->level++;
  1345. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1346. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1347. {
  1348. const size_t p = ctx->cur;
  1349. const size_t n = chunk->thunks.len;
  1350. if (
  1351. pcc_refill_buffer(ctx, 2) < 2 ||
  1352. (ctx->buffer.buf + ctx->cur)[0] != '\r' ||
  1353. (ctx->buffer.buf + ctx->cur)[1] != '\n'
  1354. ) goto L0002;
  1355. ctx->cur += 2;
  1356. goto L0001;
  1357. L0002:;
  1358. ctx->cur = p;
  1359. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1360. if (
  1361. pcc_refill_buffer(ctx, 1) < 1 ||
  1362. ctx->buffer.buf[ctx->cur] != '\n'
  1363. ) goto L0003;
  1364. ctx->cur++;
  1365. goto L0001;
  1366. L0003:;
  1367. ctx->cur = p;
  1368. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1369. if (
  1370. pcc_refill_buffer(ctx, 1) < 1 ||
  1371. ctx->buffer.buf[ctx->cur] != '\r'
  1372. ) goto L0004;
  1373. ctx->cur++;
  1374. goto L0001;
  1375. L0004:;
  1376. ctx->cur = p;
  1377. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1378. if (
  1379. pcc_refill_buffer(ctx, 2) < 2 ||
  1380. (ctx->buffer.buf + ctx->cur)[0] != '\n' ||
  1381. (ctx->buffer.buf + ctx->cur)[1] != '\r'
  1382. ) goto L0005;
  1383. ctx->cur += 2;
  1384. goto L0001;
  1385. L0005:;
  1386. ctx->cur = p;
  1387. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1388. goto L0000;
  1389. L0001:;
  1390. }
  1391. {
  1392. pcc_thunk_t *const thunk = pcc_thunk__create_leaf(ctx->auxil, pcc_action_endofline_0, 0, 0);
  1393. thunk->data.leaf.capt0.range.start = chunk->pos;
  1394. thunk->data.leaf.capt0.range.end = ctx->cur;
  1395. pcc_thunk_array__add(ctx->auxil, &chunk->thunks, thunk);
  1396. }
  1397. ctx->level--;
  1398. PCC_DEBUG(PCC_DBG_MATCH, "endofline", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1399. return chunk;
  1400. L0000:;
  1401. ctx->level--;
  1402. PCC_DEBUG(PCC_DBG_NOMATCH, "endofline", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1403. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1404. return NULL;
  1405. }
  1406. seq_context_t *seq_create(void *auxil) {
  1407. return pcc_context__create(auxil);
  1408. }
  1409. int seq_parse(seq_context_t *ctx, int *ret) {
  1410. pcc_thunk_array_t thunks;
  1411. pcc_thunk_array__init(ctx->auxil, &thunks, PCC_ARRAYSIZE);
  1412. if (pcc_apply_rule(ctx, pcc_evaluate_rule_list, &thunks, ret))
  1413. pcc_do_action(ctx, &thunks, ret);
  1414. else
  1415. PCC_ERROR(ctx->auxil);
  1416. pcc_commit_buffer(ctx);
  1417. pcc_thunk_array__term(ctx->auxil, &thunks);
  1418. return pcc_refill_buffer(ctx, 1) >= 1;
  1419. }
  1420. void seq_destroy(seq_context_t *ctx) {
  1421. pcc_context__destroy(ctx);
  1422. }
  1423. int main() {
  1424. seq_context_t *ctx = seq_create(NULL);
  1425. while (seq_parse(ctx, NULL)){;}
  1426. seq_destroy(ctx);
  1427. return 0;
  1428. }