wast.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007
  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 "wast.h"
  25. #include <stdio.h>
  26. /* set to 1 to get parse debug info */
  27. static int debug = 0;
  28. static int linenr = 1;
  29. static const char *dbg_str[] = { "Evaluating rule", "Matched rule", "Abandoning rule" };
  30. #define PCC_DEBUG(event, rule, level, pos, buffer, length) \
  31. if (debug) fprintf(stdout, "%*s%s %s @%d [%.*s]\n", (int)(level * 2), "", dbg_str[event], rule, (int)pos, (int)length, buffer); fflush(stdout)
  32. /* NOTE: To guarantee the output order, stderr, which can lead a race condition with stdout, is not used. */
  33. #ifndef PCC_BUFFERSIZE
  34. #define PCC_BUFFERSIZE 256
  35. #endif /* !PCC_BUFFERSIZE */
  36. #ifndef PCC_ARRAYSIZE
  37. #define PCC_ARRAYSIZE 2
  38. #endif /* !PCC_ARRAYSIZE */
  39. #define VOID_VALUE (~(size_t)0)
  40. typedef enum pcc_bool_tag {
  41. PCC_FALSE = 0,
  42. PCC_TRUE
  43. } pcc_bool_t;
  44. typedef struct pcc_char_array_tag {
  45. char *buf;
  46. size_t max;
  47. size_t len;
  48. } pcc_char_array_t;
  49. typedef struct pcc_range_tag {
  50. size_t start;
  51. size_t end;
  52. } pcc_range_t;
  53. typedef int pcc_value_t;
  54. typedef void *pcc_auxil_t;
  55. typedef struct pcc_value_table_tag {
  56. pcc_value_t *buf;
  57. size_t max;
  58. size_t len;
  59. } pcc_value_table_t;
  60. typedef struct pcc_value_refer_table_tag {
  61. pcc_value_t **buf;
  62. size_t max;
  63. size_t len;
  64. } pcc_value_refer_table_t;
  65. typedef struct pcc_capture_tag {
  66. pcc_range_t range;
  67. char *string; /* mutable */
  68. } pcc_capture_t;
  69. typedef struct pcc_capture_table_tag {
  70. pcc_capture_t *buf;
  71. size_t max;
  72. size_t len;
  73. } pcc_capture_table_t;
  74. typedef struct pcc_capture_const_table_tag {
  75. const pcc_capture_t **buf;
  76. size_t max;
  77. size_t len;
  78. } pcc_capture_const_table_t;
  79. typedef struct pcc_thunk_tag pcc_thunk_t;
  80. typedef struct pcc_thunk_array_tag pcc_thunk_array_t;
  81. typedef void (*pcc_action_t)(wast_context_t *, pcc_thunk_t *, pcc_value_t *);
  82. typedef enum pcc_thunk_type_tag {
  83. PCC_THUNK_LEAF,
  84. PCC_THUNK_NODE
  85. } pcc_thunk_type_t;
  86. typedef struct pcc_thunk_leaf_tag {
  87. pcc_value_refer_table_t values;
  88. pcc_capture_const_table_t capts;
  89. pcc_capture_t capt0;
  90. pcc_action_t action;
  91. } pcc_thunk_leaf_t;
  92. typedef struct pcc_thunk_node_tag {
  93. const pcc_thunk_array_t *thunks; /* just a reference */
  94. pcc_value_t *value; /* just a reference */
  95. } pcc_thunk_node_t;
  96. typedef union pcc_thunk_data_tag {
  97. pcc_thunk_leaf_t leaf;
  98. pcc_thunk_node_t node;
  99. } pcc_thunk_data_t;
  100. struct pcc_thunk_tag {
  101. pcc_thunk_type_t type;
  102. pcc_thunk_data_t data;
  103. };
  104. struct pcc_thunk_array_tag {
  105. pcc_thunk_t **buf;
  106. size_t max;
  107. size_t len;
  108. };
  109. typedef struct pcc_thunk_chunk_tag {
  110. pcc_value_table_t values;
  111. pcc_capture_table_t capts;
  112. pcc_thunk_array_t thunks;
  113. size_t pos; /* the starting position in the character buffer */
  114. } pcc_thunk_chunk_t;
  115. typedef struct pcc_lr_entry_tag pcc_lr_entry_t;
  116. typedef enum pcc_lr_answer_type_tag {
  117. PCC_LR_ANSWER_LR,
  118. PCC_LR_ANSWER_CHUNK
  119. } pcc_lr_answer_type_t;
  120. typedef union pcc_lr_answer_data_tag {
  121. pcc_lr_entry_t *lr;
  122. pcc_thunk_chunk_t *chunk;
  123. } pcc_lr_answer_data_t;
  124. typedef struct pcc_lr_answer_tag pcc_lr_answer_t;
  125. struct pcc_lr_answer_tag {
  126. pcc_lr_answer_type_t type;
  127. pcc_lr_answer_data_t data;
  128. size_t pos; /* the absolute position in the input */
  129. pcc_lr_answer_t *hold;
  130. };
  131. typedef pcc_thunk_chunk_t *(*pcc_rule_t)(wast_context_t *);
  132. typedef struct pcc_rule_set_tag {
  133. pcc_rule_t *buf;
  134. size_t max;
  135. size_t len;
  136. } pcc_rule_set_t;
  137. typedef struct pcc_lr_head_tag pcc_lr_head_t;
  138. struct pcc_lr_head_tag {
  139. pcc_rule_t rule;
  140. pcc_rule_set_t invol;
  141. pcc_rule_set_t eval;
  142. pcc_lr_head_t *hold;
  143. };
  144. typedef struct pcc_lr_memo_tag {
  145. pcc_rule_t rule;
  146. pcc_lr_answer_t *answer;
  147. } pcc_lr_memo_t;
  148. typedef struct pcc_lr_memo_map_tag {
  149. pcc_lr_memo_t *buf;
  150. size_t max;
  151. size_t len;
  152. } pcc_lr_memo_map_t;
  153. typedef struct pcc_lr_table_entry_tag {
  154. pcc_lr_head_t *head; /* just a reference */
  155. pcc_lr_memo_map_t memos;
  156. pcc_lr_answer_t *hold_a;
  157. pcc_lr_head_t *hold_h;
  158. } pcc_lr_table_entry_t;
  159. typedef struct pcc_lr_table_tag {
  160. pcc_lr_table_entry_t **buf;
  161. size_t max;
  162. size_t len;
  163. } pcc_lr_table_t;
  164. struct pcc_lr_entry_tag {
  165. pcc_rule_t rule;
  166. pcc_thunk_chunk_t *seed; /* just a reference */
  167. pcc_lr_head_t *head; /* just a reference */
  168. };
  169. typedef struct pcc_lr_stack_tag {
  170. pcc_lr_entry_t **buf;
  171. size_t max;
  172. size_t len;
  173. } pcc_lr_stack_t;
  174. struct wast_context_tag {
  175. size_t pos; /* the position in the input of the first character currently buffered */
  176. size_t cur; /* the current parsing position in the character buffer */
  177. size_t level;
  178. pcc_char_array_t buffer;
  179. pcc_lr_table_t lrtable;
  180. pcc_lr_stack_t lrstack;
  181. pcc_auxil_t auxil;
  182. };
  183. #ifndef PCC_ERROR
  184. #define PCC_ERROR(auxil) pcc_error()
  185. static void pcc_error(void) {
  186. fprintf(stderr, "Syntax error\n");
  187. exit(1);
  188. }
  189. #endif /* !PCC_ERROR */
  190. #ifndef PCC_GETCHAR
  191. #define PCC_GETCHAR(auxil) getchar()
  192. #endif /* !PCC_GETCHAR */
  193. #ifndef PCC_MALLOC
  194. #define PCC_MALLOC(auxil, size) pcc_malloc_e(size)
  195. static void *pcc_malloc_e(size_t size) {
  196. void *const p = malloc(size);
  197. if (p == NULL) {
  198. fprintf(stderr, "Out of memory\n");
  199. exit(1);
  200. }
  201. return p;
  202. }
  203. #endif /* !PCC_MALLOC */
  204. #ifndef PCC_REALLOC
  205. #define PCC_REALLOC(auxil, ptr, size) pcc_realloc_e(ptr, size)
  206. static void *pcc_realloc_e(void *ptr, size_t size) {
  207. void *const p = realloc(ptr, size);
  208. if (p == NULL) {
  209. fprintf(stderr, "Out of memory\n");
  210. exit(1);
  211. }
  212. return p;
  213. }
  214. #endif /* !PCC_REALLOC */
  215. #ifndef PCC_FREE
  216. #define PCC_FREE(auxil, ptr) free(ptr)
  217. #endif /* !PCC_FREE */
  218. #ifndef PCC_DEBUG
  219. #define PCC_DEBUG(event, rule, level, pos, buffer, length) ((void)0)
  220. #endif /* !PCC_DEBUG */
  221. static char *pcc_strndup_e(pcc_auxil_t auxil, const char *str, size_t len) {
  222. const size_t m = strnlen(str, len);
  223. char *const s = (char *)PCC_MALLOC(auxil, m + 1);
  224. if (auxil) { }
  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. if (auxil) { }
  233. array->buf = (char *)PCC_MALLOC(auxil, array->max);
  234. }
  235. static void pcc_char_array__add(pcc_auxil_t auxil, pcc_char_array_t *array, char ch) {
  236. if (array->max <= array->len) {
  237. const size_t n = array->len + 1;
  238. size_t m = array->max;
  239. if (m == 0) m = 1;
  240. while (m < n && m != 0) m <<= 1;
  241. if (m == 0) m = n;
  242. if (auxil) { }
  243. array->buf = (char *)PCC_REALLOC(auxil, array->buf, m);
  244. array->max = m;
  245. }
  246. array->buf[array->len++] = ch;
  247. }
  248. static void pcc_char_array__term(pcc_auxil_t auxil, pcc_char_array_t *array) {
  249. PCC_FREE(auxil, array->buf);
  250. }
  251. static void pcc_value_table__init(pcc_auxil_t auxil, pcc_value_table_t *table, size_t max) {
  252. table->len = 0;
  253. table->max = max;
  254. table->buf = (pcc_value_t *)PCC_MALLOC(auxil, sizeof(pcc_value_t) * table->max);
  255. if (auxil) { }
  256. return;
  257. }
  258. static void pcc_value_table__resize(pcc_auxil_t auxil, pcc_value_table_t *table, size_t len) {
  259. if (table->max < len) {
  260. size_t m = table->max;
  261. if (m == 0) m = 1;
  262. while (m < len && m != 0) m <<= 1;
  263. if (m == 0) m = len;
  264. table->buf = (pcc_value_t *)PCC_REALLOC(auxil, table->buf, sizeof(pcc_value_t) * m);
  265. table->max = m;
  266. }
  267. table->len = len;
  268. }
  269. static void pcc_value_table__term(pcc_auxil_t auxil, pcc_value_table_t *table) {
  270. PCC_FREE(auxil, table->buf);
  271. }
  272. static void pcc_value_refer_table__init(pcc_auxil_t auxil, pcc_value_refer_table_t *table, size_t max) {
  273. table->len = 0;
  274. table->max = max;
  275. table->buf = (pcc_value_t **)PCC_MALLOC(auxil, sizeof(pcc_value_t *) * table->max);
  276. }
  277. static void pcc_value_refer_table__resize(pcc_auxil_t auxil, pcc_value_refer_table_t *table, size_t len) {
  278. size_t i;
  279. if (table->max < len) {
  280. size_t m = table->max;
  281. if (m == 0) m = 1;
  282. while (m < len && m != 0) m <<= 1;
  283. if (m == 0) m = len;
  284. table->buf = (pcc_value_t **)PCC_REALLOC(auxil, table->buf, sizeof(pcc_value_t *) * m);
  285. table->max = m;
  286. }
  287. for (i = table->len; i < len; i++) table->buf[i] = NULL;
  288. table->len = len;
  289. }
  290. static void pcc_value_refer_table__term(pcc_auxil_t auxil, pcc_value_refer_table_t *table) {
  291. PCC_FREE(auxil, table->buf);
  292. }
  293. static void pcc_capture_table__init(pcc_auxil_t auxil, pcc_capture_table_t *table, size_t max) {
  294. table->len = 0;
  295. table->max = max;
  296. table->buf = (pcc_capture_t *)PCC_MALLOC(auxil, sizeof(pcc_capture_t) * table->max);
  297. }
  298. static void pcc_capture_table__resize(pcc_auxil_t auxil, pcc_capture_table_t *table, size_t len) {
  299. size_t i;
  300. for (i = len; i < table->len; i++) PCC_FREE(auxil, table->buf[i].string);
  301. if (table->max < len) {
  302. size_t m = table->max;
  303. if (m == 0) m = 1;
  304. while (m < len && m != 0) m <<= 1;
  305. if (m == 0) m = len;
  306. table->buf = (pcc_capture_t *)PCC_REALLOC(auxil, table->buf, sizeof(pcc_capture_t) * m);
  307. table->max = m;
  308. }
  309. for (i = table->len; i < len; i++) {
  310. table->buf[i].range.start = 0;
  311. table->buf[i].range.end = 0;
  312. table->buf[i].string = NULL;
  313. }
  314. table->len = len;
  315. }
  316. static void pcc_capture_table__term(pcc_auxil_t auxil, pcc_capture_table_t *table) {
  317. while (table->len > 0) {
  318. table->len--;
  319. PCC_FREE(auxil, table->buf[table->len].string);
  320. }
  321. PCC_FREE(auxil, table->buf);
  322. }
  323. static void pcc_capture_const_table__init(pcc_auxil_t auxil, pcc_capture_const_table_t *table, size_t max) {
  324. table->len = 0;
  325. table->max = max;
  326. table->buf = (const pcc_capture_t **)PCC_MALLOC(auxil, sizeof(const pcc_capture_t *) * table->max);
  327. }
  328. static void pcc_capture_const_table__resize(pcc_auxil_t auxil, pcc_capture_const_table_t *table, size_t len) {
  329. size_t i;
  330. if (table->max < len) {
  331. size_t m = table->max;
  332. if (m == 0) m = 1;
  333. while (m < len && m != 0) m <<= 1;
  334. if (m == 0) m = len;
  335. table->buf = (const pcc_capture_t **)PCC_REALLOC(auxil, (pcc_capture_t **)table->buf, sizeof(const pcc_capture_t *) * m);
  336. table->max = m;
  337. }
  338. for (i = table->len; i < len; i++) table->buf[i] = NULL;
  339. table->len = len;
  340. }
  341. static void pcc_capture_const_table__term(pcc_auxil_t auxil, pcc_capture_const_table_t *table) {
  342. PCC_FREE(auxil, table->buf);
  343. }
  344. static pcc_thunk_t *pcc_thunk__create_leaf(pcc_auxil_t auxil, pcc_action_t action, size_t valuec, size_t captc) {
  345. pcc_thunk_t *const thunk = (pcc_thunk_t *)PCC_MALLOC(auxil, sizeof(pcc_thunk_t));
  346. thunk->type = PCC_THUNK_LEAF;
  347. pcc_value_refer_table__init(auxil, &thunk->data.leaf.values, valuec);
  348. pcc_value_refer_table__resize(auxil, &thunk->data.leaf.values, valuec);
  349. pcc_capture_const_table__init(auxil, &thunk->data.leaf.capts, captc);
  350. pcc_capture_const_table__resize(auxil, &thunk->data.leaf.capts, captc);
  351. thunk->data.leaf.capt0.range.start = 0;
  352. thunk->data.leaf.capt0.range.end = 0;
  353. thunk->data.leaf.capt0.string = NULL;
  354. thunk->data.leaf.action = action;
  355. return thunk;
  356. }
  357. static pcc_thunk_t *pcc_thunk__create_node(pcc_auxil_t auxil, const pcc_thunk_array_t *thunks, pcc_value_t *value) {
  358. pcc_thunk_t *const thunk = (pcc_thunk_t *)PCC_MALLOC(auxil, sizeof(pcc_thunk_t));
  359. thunk->type = PCC_THUNK_NODE;
  360. thunk->data.node.thunks = thunks;
  361. thunk->data.node.value = value;
  362. return thunk;
  363. }
  364. static void pcc_thunk__destroy(pcc_auxil_t auxil, pcc_thunk_t *thunk) {
  365. if (thunk == NULL) return;
  366. switch (thunk->type) {
  367. case PCC_THUNK_LEAF:
  368. PCC_FREE(auxil, thunk->data.leaf.capt0.string);
  369. pcc_capture_const_table__term(auxil, &thunk->data.leaf.capts);
  370. pcc_value_refer_table__term(auxil, &thunk->data.leaf.values);
  371. break;
  372. case PCC_THUNK_NODE:
  373. break;
  374. default: /* unknown */
  375. break;
  376. }
  377. PCC_FREE(auxil, thunk);
  378. }
  379. static void pcc_thunk_array__init(pcc_auxil_t auxil, pcc_thunk_array_t *array, size_t max) {
  380. array->len = 0;
  381. array->max = max;
  382. array->buf = (pcc_thunk_t **)PCC_MALLOC(auxil, sizeof(pcc_thunk_t *) * array->max);
  383. }
  384. static void pcc_thunk_array__add(pcc_auxil_t auxil, pcc_thunk_array_t *array, pcc_thunk_t *thunk) {
  385. if (array->max <= array->len) {
  386. const size_t n = array->len + 1;
  387. size_t m = array->max;
  388. if (m == 0) m = 1;
  389. while (m < n && m != 0) m <<= 1;
  390. if (m == 0) m = n;
  391. array->buf = (pcc_thunk_t **)PCC_REALLOC(auxil, array->buf, sizeof(pcc_thunk_t *) * m);
  392. array->max = m;
  393. }
  394. array->buf[array->len++] = thunk;
  395. }
  396. static void pcc_thunk_array__revert(pcc_auxil_t auxil, pcc_thunk_array_t *array, size_t len) {
  397. while (array->len > len) {
  398. array->len--;
  399. pcc_thunk__destroy(auxil, array->buf[array->len]);
  400. }
  401. }
  402. static void pcc_thunk_array__term(pcc_auxil_t auxil, pcc_thunk_array_t *array) {
  403. while (array->len > 0) {
  404. array->len--;
  405. pcc_thunk__destroy(auxil, array->buf[array->len]);
  406. }
  407. PCC_FREE(auxil, array->buf);
  408. }
  409. static pcc_thunk_chunk_t *pcc_thunk_chunk__create(pcc_auxil_t auxil) {
  410. pcc_thunk_chunk_t *const chunk = (pcc_thunk_chunk_t *)PCC_MALLOC(auxil, sizeof(pcc_thunk_chunk_t));
  411. pcc_value_table__init(auxil, &chunk->values, PCC_ARRAYSIZE);
  412. pcc_capture_table__init(auxil, &chunk->capts, PCC_ARRAYSIZE);
  413. pcc_thunk_array__init(auxil, &chunk->thunks, PCC_ARRAYSIZE);
  414. chunk->pos = 0;
  415. return chunk;
  416. }
  417. static void pcc_thunk_chunk__destroy(pcc_auxil_t auxil, pcc_thunk_chunk_t *chunk) {
  418. if (chunk == NULL) return;
  419. pcc_thunk_array__term(auxil, &chunk->thunks);
  420. pcc_capture_table__term(auxil, &chunk->capts);
  421. pcc_value_table__term(auxil, &chunk->values);
  422. PCC_FREE(auxil, chunk);
  423. }
  424. static void pcc_rule_set__init(pcc_auxil_t auxil, pcc_rule_set_t *set, size_t max) {
  425. set->len = 0;
  426. set->max = max;
  427. set->buf = (pcc_rule_t *)PCC_MALLOC(auxil, sizeof(pcc_rule_t) * set->max);
  428. }
  429. static size_t pcc_rule_set__index(pcc_auxil_t auxil, const pcc_rule_set_t *set, pcc_rule_t rule) {
  430. size_t i;
  431. for (i = 0; i < set->len; i++) {
  432. if (set->buf[i] == rule) return i;
  433. }
  434. return VOID_VALUE;
  435. }
  436. static pcc_bool_t pcc_rule_set__add(pcc_auxil_t auxil, pcc_rule_set_t *set, pcc_rule_t rule) {
  437. const size_t i = pcc_rule_set__index(auxil, set, rule);
  438. if (i != VOID_VALUE) return PCC_FALSE;
  439. if (set->max <= set->len) {
  440. const size_t n = set->len + 1;
  441. size_t m = set->max;
  442. if (m == 0) m = 1;
  443. while (m < n && m != 0) m <<= 1;
  444. if (m == 0) m = n;
  445. set->buf = (pcc_rule_t *)PCC_REALLOC(auxil, set->buf, sizeof(pcc_rule_t) * m);
  446. set->max = m;
  447. }
  448. set->buf[set->len++] = rule;
  449. return PCC_TRUE;
  450. }
  451. static pcc_bool_t pcc_rule_set__remove(pcc_auxil_t auxil, pcc_rule_set_t *set, pcc_rule_t rule) {
  452. const size_t i = pcc_rule_set__index(auxil, set, rule);
  453. if (i == VOID_VALUE) return PCC_FALSE;
  454. memmove(set->buf + i, set->buf + (i + 1), sizeof(pcc_rule_t) * (set->len - (i + 1)));
  455. return PCC_TRUE;
  456. }
  457. static void pcc_rule_set__clear(pcc_auxil_t auxil, pcc_rule_set_t *set) {
  458. set->len = 0;
  459. }
  460. static void pcc_rule_set__copy(pcc_auxil_t auxil, pcc_rule_set_t *set, const pcc_rule_set_t *src) {
  461. size_t i;
  462. pcc_rule_set__clear(auxil, set);
  463. for (i = 0; i < src->len; i++) {
  464. pcc_rule_set__add(auxil, set, src->buf[i]);
  465. }
  466. }
  467. static void pcc_rule_set__term(pcc_auxil_t auxil, pcc_rule_set_t *set) {
  468. PCC_FREE(auxil, set->buf);
  469. }
  470. static pcc_lr_head_t *pcc_lr_head__create(pcc_auxil_t auxil, pcc_rule_t rule) {
  471. pcc_lr_head_t *const head = (pcc_lr_head_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_head_t));
  472. head->rule = rule;
  473. pcc_rule_set__init(auxil, &head->invol, PCC_ARRAYSIZE);
  474. pcc_rule_set__init(auxil, &head->eval, PCC_ARRAYSIZE);
  475. head->hold = NULL;
  476. return head;
  477. }
  478. static void pcc_lr_head__destroy(pcc_auxil_t auxil, pcc_lr_head_t *head) {
  479. if (head == NULL) return;
  480. pcc_lr_head__destroy(auxil, head->hold);
  481. pcc_rule_set__term(auxil, &head->eval);
  482. pcc_rule_set__term(auxil, &head->invol);
  483. PCC_FREE(auxil, head);
  484. }
  485. static void pcc_lr_entry__destroy(pcc_auxil_t auxil, pcc_lr_entry_t *lr);
  486. static pcc_lr_answer_t *pcc_lr_answer__create(pcc_auxil_t auxil, pcc_lr_answer_type_t type, size_t pos) {
  487. pcc_lr_answer_t *answer = (pcc_lr_answer_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_answer_t));
  488. answer->type = type;
  489. answer->pos = pos;
  490. answer->hold = NULL;
  491. switch (answer->type) {
  492. case PCC_LR_ANSWER_LR:
  493. answer->data.lr = NULL;
  494. break;
  495. case PCC_LR_ANSWER_CHUNK:
  496. answer->data.chunk = NULL;
  497. break;
  498. default: /* unknown */
  499. PCC_FREE(auxil, answer);
  500. answer = NULL;
  501. }
  502. return answer;
  503. }
  504. static void pcc_lr_answer__set_chunk(pcc_auxil_t auxil, pcc_lr_answer_t *answer, pcc_thunk_chunk_t *chunk) {
  505. pcc_lr_answer_t *const a = pcc_lr_answer__create(auxil, answer->type, answer->pos);
  506. switch (answer->type) {
  507. case PCC_LR_ANSWER_LR:
  508. a->data.lr = answer->data.lr;
  509. break;
  510. case PCC_LR_ANSWER_CHUNK:
  511. a->data.chunk = answer->data.chunk;
  512. break;
  513. default: /* unknown */
  514. break;
  515. }
  516. a->hold = answer->hold;
  517. answer->hold = a;
  518. answer->type = PCC_LR_ANSWER_CHUNK;
  519. answer->data.chunk = chunk;
  520. }
  521. static void pcc_lr_answer__destroy(pcc_auxil_t auxil, pcc_lr_answer_t *answer) {
  522. if (answer == NULL) return;
  523. pcc_lr_answer__destroy(auxil, answer->hold);
  524. switch (answer->type) {
  525. case PCC_LR_ANSWER_LR:
  526. pcc_lr_entry__destroy(auxil, answer->data.lr);
  527. break;
  528. case PCC_LR_ANSWER_CHUNK:
  529. pcc_thunk_chunk__destroy(auxil, answer->data.chunk);
  530. break;
  531. default: /* unknown */
  532. break;
  533. }
  534. PCC_FREE(auxil, answer);
  535. }
  536. static void pcc_lr_memo_map__init(pcc_auxil_t auxil, pcc_lr_memo_map_t *map, size_t max) {
  537. map->len = 0;
  538. map->max = max;
  539. map->buf = (pcc_lr_memo_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_memo_t) * map->max);
  540. }
  541. static size_t pcc_lr_memo_map__index(pcc_auxil_t auxil, pcc_lr_memo_map_t *map, pcc_rule_t rule) {
  542. size_t i;
  543. for (i = 0; i < map->len; i++) {
  544. if (map->buf[i].rule == rule) return i;
  545. }
  546. return VOID_VALUE;
  547. }
  548. 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) {
  549. const size_t i = pcc_lr_memo_map__index(auxil, map, rule);
  550. if (i != VOID_VALUE) {
  551. pcc_lr_answer__destroy(auxil, map->buf[i].answer);
  552. map->buf[i].answer = answer;
  553. }
  554. else {
  555. if (map->max <= map->len) {
  556. const size_t n = map->len + 1;
  557. size_t m = map->max;
  558. if (m == 0) m = 1;
  559. while (m < n && m != 0) m <<= 1;
  560. if (m == 0) m = n;
  561. map->buf = (pcc_lr_memo_t *)PCC_REALLOC(auxil, map->buf, sizeof(pcc_lr_memo_t) * m);
  562. map->max = m;
  563. }
  564. map->buf[map->len].rule = rule;
  565. map->buf[map->len].answer = answer;
  566. map->len++;
  567. }
  568. }
  569. static pcc_lr_answer_t *pcc_lr_memo_map__get(pcc_auxil_t auxil, pcc_lr_memo_map_t *map, pcc_rule_t rule) {
  570. const size_t i = pcc_lr_memo_map__index(auxil, map, rule);
  571. return (i != VOID_VALUE) ? map->buf[i].answer : NULL;
  572. }
  573. static void pcc_lr_memo_map__term(pcc_auxil_t auxil, pcc_lr_memo_map_t *map) {
  574. while (map->len > 0) {
  575. map->len--;
  576. pcc_lr_answer__destroy(auxil, map->buf[map->len].answer);
  577. }
  578. PCC_FREE(auxil, map->buf);
  579. }
  580. static pcc_lr_table_entry_t *pcc_lr_table_entry__create(pcc_auxil_t auxil) {
  581. pcc_lr_table_entry_t *const entry = (pcc_lr_table_entry_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_table_entry_t));
  582. entry->head = NULL;
  583. pcc_lr_memo_map__init(auxil, &entry->memos, PCC_ARRAYSIZE);
  584. entry->hold_a = NULL;
  585. entry->hold_h = NULL;
  586. return entry;
  587. }
  588. static void pcc_lr_table_entry__destroy(pcc_auxil_t auxil, pcc_lr_table_entry_t *entry) {
  589. if (entry == NULL) return;
  590. pcc_lr_head__destroy(auxil, entry->hold_h);
  591. pcc_lr_answer__destroy(auxil, entry->hold_a);
  592. pcc_lr_memo_map__term(auxil, &entry->memos);
  593. PCC_FREE(auxil, entry);
  594. }
  595. static void pcc_lr_table__init(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t max) {
  596. table->len = 0;
  597. table->max = max;
  598. table->buf = (pcc_lr_table_entry_t **)PCC_MALLOC(auxil, sizeof(pcc_lr_table_entry_t *) * table->max);
  599. }
  600. static void pcc_lr_table__resize(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t len) {
  601. size_t i;
  602. for (i = len; i < table->len; i++) pcc_lr_table_entry__destroy(auxil, table->buf[i]);
  603. if (table->max < len) {
  604. size_t m = table->max;
  605. if (m == 0) m = 1;
  606. while (m < len && m != 0) m <<= 1;
  607. if (m == 0) m = len;
  608. table->buf = (pcc_lr_table_entry_t **)PCC_REALLOC(auxil, table->buf, sizeof(pcc_lr_table_entry_t *) * m);
  609. table->max = m;
  610. }
  611. for (i = table->len; i < len; i++) table->buf[i] = NULL;
  612. table->len = len;
  613. }
  614. static void pcc_lr_table__set_head(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index, pcc_lr_head_t *head) {
  615. if (index >= table->len) pcc_lr_table__resize(auxil, table, index + 1);
  616. if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(auxil);
  617. table->buf[index]->head = head;
  618. }
  619. static void pcc_lr_table__hold_head(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index, pcc_lr_head_t *head) {
  620. if (index >= table->len) pcc_lr_table__resize(auxil, table, index + 1);
  621. if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(auxil);
  622. head->hold = table->buf[index]->hold_h;
  623. table->buf[index]->hold_h = head;
  624. }
  625. 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) {
  626. if (index >= table->len) pcc_lr_table__resize(auxil, table, index + 1);
  627. if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(auxil);
  628. pcc_lr_memo_map__put(auxil, &table->buf[index]->memos, rule, answer);
  629. }
  630. static void pcc_lr_table__hold_answer(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index, pcc_lr_answer_t *answer) {
  631. if (index >= table->len) pcc_lr_table__resize(auxil, table, index + 1);
  632. if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(auxil);
  633. answer->hold = table->buf[index]->hold_a;
  634. table->buf[index]->hold_a = answer;
  635. }
  636. static pcc_lr_head_t *pcc_lr_table__get_head(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index) {
  637. if (index >= table->len || table->buf[index] == NULL) return NULL;
  638. return table->buf[index]->head;
  639. }
  640. 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) {
  641. if (index >= table->len || table->buf[index] == NULL) return NULL;
  642. return pcc_lr_memo_map__get(auxil, &table->buf[index]->memos, rule);
  643. }
  644. static void pcc_lr_table__shift(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t count) {
  645. size_t i;
  646. if (count > table->len) count = table->len;
  647. for (i = 0; i < count; i++) pcc_lr_table_entry__destroy(auxil, table->buf[i]);
  648. memmove(table->buf, table->buf + count, sizeof(pcc_lr_table_entry_t *) * (table->len - count));
  649. table->len -= count;
  650. }
  651. static void pcc_lr_table__term(pcc_auxil_t auxil, pcc_lr_table_t *table) {
  652. while (table->len > 0) {
  653. table->len--;
  654. pcc_lr_table_entry__destroy(auxil, table->buf[table->len]);
  655. }
  656. PCC_FREE(auxil, table->buf);
  657. }
  658. static pcc_lr_entry_t *pcc_lr_entry__create(pcc_auxil_t auxil, pcc_rule_t rule) {
  659. pcc_lr_entry_t *const lr = (pcc_lr_entry_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_entry_t));
  660. lr->rule = rule;
  661. lr->seed = NULL;
  662. lr->head = NULL;
  663. return lr;
  664. }
  665. static void pcc_lr_entry__destroy(pcc_auxil_t auxil, pcc_lr_entry_t *lr) {
  666. PCC_FREE(auxil, lr);
  667. }
  668. static void pcc_lr_stack__init(pcc_auxil_t auxil, pcc_lr_stack_t *stack, size_t max) {
  669. stack->len = 0;
  670. stack->max = max;
  671. stack->buf = (pcc_lr_entry_t **)PCC_MALLOC(auxil, sizeof(pcc_lr_entry_t *) * stack->max);
  672. }
  673. static void pcc_lr_stack__push(pcc_auxil_t auxil, pcc_lr_stack_t *stack, pcc_lr_entry_t *lr) {
  674. if (stack->max <= stack->len) {
  675. const size_t n = stack->len + 1;
  676. size_t m = stack->max;
  677. if (m == 0) m = 1;
  678. while (m < n && m != 0) m <<= 1;
  679. if (m == 0) m = n;
  680. stack->buf = (pcc_lr_entry_t **)PCC_REALLOC(auxil, stack->buf, sizeof(pcc_lr_entry_t *) * m);
  681. stack->max = m;
  682. }
  683. stack->buf[stack->len++] = lr;
  684. }
  685. static pcc_lr_entry_t *pcc_lr_stack__pop(pcc_auxil_t auxil, pcc_lr_stack_t *stack) {
  686. return stack->buf[--stack->len];
  687. }
  688. static void pcc_lr_stack__term(pcc_auxil_t auxil, pcc_lr_stack_t *stack) {
  689. PCC_FREE(auxil, stack->buf);
  690. }
  691. static wast_context_t *pcc_context__create(pcc_auxil_t auxil) {
  692. wast_context_t *const ctx = (wast_context_t *)PCC_MALLOC(auxil, sizeof(wast_context_t));
  693. ctx->pos = 0;
  694. ctx->cur = 0;
  695. ctx->level = 0;
  696. pcc_char_array__init(auxil, &ctx->buffer, PCC_BUFFERSIZE);
  697. pcc_lr_table__init(auxil, &ctx->lrtable, PCC_BUFFERSIZE);
  698. pcc_lr_stack__init(auxil, &ctx->lrstack, PCC_ARRAYSIZE);
  699. ctx->auxil = auxil;
  700. return ctx;
  701. }
  702. static void pcc_context__destroy(wast_context_t *ctx) {
  703. if (ctx == NULL) return;
  704. pcc_lr_stack__term(ctx->auxil, &ctx->lrstack);
  705. pcc_lr_table__term(ctx->auxil, &ctx->lrtable);
  706. pcc_char_array__term(ctx->auxil, &ctx->buffer);
  707. PCC_FREE(ctx->auxil, ctx);
  708. }
  709. static size_t pcc_refill_buffer(wast_context_t *ctx, size_t num) {
  710. if (ctx->buffer.len >= ctx->cur + num) return ctx->buffer.len - ctx->cur;
  711. while (ctx->buffer.len < ctx->cur + num) {
  712. const int c = PCC_GETCHAR(ctx->auxil);
  713. if (c == EOF) break;
  714. pcc_char_array__add(ctx->auxil, &ctx->buffer, (char)c);
  715. }
  716. return ctx->buffer.len - ctx->cur;
  717. }
  718. static void pcc_commit_buffer(wast_context_t *ctx) {
  719. memmove(ctx->buffer.buf, ctx->buffer.buf + ctx->cur, ctx->buffer.len - ctx->cur);
  720. ctx->buffer.len -= ctx->cur;
  721. ctx->pos += ctx->cur;
  722. pcc_lr_table__shift(ctx->auxil, &ctx->lrtable, ctx->cur);
  723. ctx->cur = 0;
  724. }
  725. static const char *pcc_get_capture_string(wast_context_t *ctx, const pcc_capture_t *capt) {
  726. if (capt->string == NULL)
  727. ((pcc_capture_t *)capt)->string =
  728. pcc_strndup_e(ctx->auxil, ctx->buffer.buf + capt->range.start, capt->range.end - capt->range.start);
  729. return capt->string;
  730. }
  731. static size_t pcc_get_char_as_utf32(wast_context_t *ctx, int *out) { /* with checking UTF-8 validity */
  732. int c, u;
  733. size_t n;
  734. if (pcc_refill_buffer(ctx, 1) < 1) return 0;
  735. c = (int)(unsigned char)ctx->buffer.buf[ctx->cur];
  736. n = (c < 0x80) ? 1 :
  737. ((c & 0xe0) == 0xc0) ? 2 :
  738. ((c & 0xf0) == 0xe0) ? 3 :
  739. ((c & 0xf8) == 0xf0) ? 4 : 0;
  740. if (n < 1) return 0;
  741. if (pcc_refill_buffer(ctx, n) < n) return 0;
  742. switch (n) {
  743. case 1:
  744. u = c;
  745. break;
  746. case 2:
  747. u = c & 0x1f;
  748. c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 1];
  749. if ((c & 0xc0) != 0x80) return 0;
  750. u <<= 6; u |= c & 0x3f;
  751. if (u < 0x80) return 0;
  752. break;
  753. case 3:
  754. u = c & 0x0f;
  755. c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 1];
  756. if ((c & 0xc0) != 0x80) return 0;
  757. u <<= 6; u |= c & 0x3f;
  758. c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 2];
  759. if ((c & 0xc0) != 0x80) return 0;
  760. u <<= 6; u |= c & 0x3f;
  761. if (u < 0x800) return 0;
  762. break;
  763. case 4:
  764. u = c & 0x07;
  765. c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 1];
  766. if ((c & 0xc0) != 0x80) return 0;
  767. u <<= 6; u |= c & 0x3f;
  768. c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 2];
  769. if ((c & 0xc0) != 0x80) return 0;
  770. u <<= 6; u |= c & 0x3f;
  771. c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 3];
  772. if ((c & 0xc0) != 0x80) return 0;
  773. u <<= 6; u |= c & 0x3f;
  774. if (u < 0x10000 || u > 0x10ffff) return 0;
  775. break;
  776. default:
  777. return 0;
  778. }
  779. if (out) *out = u;
  780. return n;
  781. }
  782. static pcc_bool_t pcc_apply_rule(wast_context_t *ctx, pcc_rule_t rule, pcc_thunk_array_t *thunks, pcc_value_t *value) {
  783. static pcc_value_t null;
  784. pcc_thunk_chunk_t *c = NULL;
  785. const size_t p = ctx->pos + ctx->cur;
  786. pcc_bool_t b = PCC_TRUE;
  787. pcc_lr_answer_t *a = pcc_lr_table__get_answer(ctx->auxil, &ctx->lrtable, p, rule);
  788. pcc_lr_head_t *h = pcc_lr_table__get_head(ctx->auxil, &ctx->lrtable, p);
  789. if (h != NULL) {
  790. if (a == NULL && rule != h->rule && pcc_rule_set__index(ctx->auxil, &h->invol, rule) == VOID_VALUE) {
  791. b = PCC_FALSE;
  792. c = NULL;
  793. }
  794. else if (pcc_rule_set__remove(ctx->auxil, &h->eval, rule)) {
  795. b = PCC_FALSE;
  796. c = rule(ctx);
  797. a = pcc_lr_answer__create(ctx->auxil, PCC_LR_ANSWER_CHUNK, ctx->pos + ctx->cur);
  798. a->data.chunk = c;
  799. pcc_lr_table__hold_answer(ctx->auxil, &ctx->lrtable, p, a);
  800. }
  801. }
  802. if (b) {
  803. if (a != NULL) {
  804. ctx->cur = a->pos - ctx->pos;
  805. switch (a->type) {
  806. case PCC_LR_ANSWER_LR:
  807. if (a->data.lr->head == NULL) {
  808. a->data.lr->head = pcc_lr_head__create(ctx->auxil, rule);
  809. pcc_lr_table__hold_head(ctx->auxil, &ctx->lrtable, p, a->data.lr->head);
  810. }
  811. {
  812. size_t i = ctx->lrstack.len;
  813. while (i > 0) {
  814. i--;
  815. if (ctx->lrstack.buf[i]->head == a->data.lr->head) break;
  816. ctx->lrstack.buf[i]->head = a->data.lr->head;
  817. pcc_rule_set__add(ctx->auxil, &a->data.lr->head->invol, ctx->lrstack.buf[i]->rule);
  818. }
  819. }
  820. c = a->data.lr->seed;
  821. break;
  822. case PCC_LR_ANSWER_CHUNK:
  823. c = a->data.chunk;
  824. break;
  825. default: /* unknown */
  826. break;
  827. }
  828. }
  829. else {
  830. pcc_lr_entry_t *const e = pcc_lr_entry__create(ctx->auxil, rule);
  831. pcc_lr_stack__push(ctx->auxil, &ctx->lrstack, e);
  832. a = pcc_lr_answer__create(ctx->auxil, PCC_LR_ANSWER_LR, p);
  833. a->data.lr = e;
  834. pcc_lr_table__set_answer(ctx->auxil, &ctx->lrtable, p, rule, a);
  835. c = rule(ctx);
  836. pcc_lr_stack__pop(ctx->auxil, &ctx->lrstack);
  837. a->pos = ctx->pos + ctx->cur;
  838. if (e->head == NULL) {
  839. pcc_lr_answer__set_chunk(ctx->auxil, a, c);
  840. }
  841. else {
  842. e->seed = c;
  843. h = a->data.lr->head;
  844. if (h->rule != rule) {
  845. c = a->data.lr->seed;
  846. a = pcc_lr_answer__create(ctx->auxil, PCC_LR_ANSWER_CHUNK, ctx->pos + ctx->cur);
  847. a->data.chunk = c;
  848. pcc_lr_table__hold_answer(ctx->auxil, &ctx->lrtable, p, a);
  849. }
  850. else {
  851. pcc_lr_answer__set_chunk(ctx->auxil, a, a->data.lr->seed);
  852. if (a->data.chunk == NULL) {
  853. c = NULL;
  854. }
  855. else {
  856. pcc_lr_table__set_head(ctx->auxil, &ctx->lrtable, p, h);
  857. for (;;) {
  858. ctx->cur = p - ctx->pos;
  859. pcc_rule_set__copy(ctx->auxil, &h->eval, &h->invol);
  860. c = rule(ctx);
  861. if (c == NULL || ctx->pos + ctx->cur <= a->pos) break;
  862. pcc_lr_answer__set_chunk(ctx->auxil, a, c);
  863. a->pos = ctx->pos + ctx->cur;
  864. }
  865. pcc_thunk_chunk__destroy(ctx->auxil, c);
  866. pcc_lr_table__set_head(ctx->auxil, &ctx->lrtable, p, NULL);
  867. ctx->cur = a->pos - ctx->pos;
  868. c = a->data.chunk;
  869. }
  870. }
  871. }
  872. }
  873. }
  874. if (c == NULL) return PCC_FALSE;
  875. if (value == NULL) value = &null;
  876. memset(value, 0, sizeof(pcc_value_t)); /* in case */
  877. pcc_thunk_array__add(ctx->auxil, thunks, pcc_thunk__create_node(ctx->auxil, &c->thunks, value));
  878. return PCC_TRUE;
  879. }
  880. static void pcc_do_action(wast_context_t *ctx, const pcc_thunk_array_t *thunks, pcc_value_t *value) {
  881. size_t i;
  882. for (i = 0; i < thunks->len; i++) {
  883. pcc_thunk_t *const thunk = thunks->buf[i];
  884. switch (thunk->type) {
  885. case PCC_THUNK_LEAF:
  886. thunk->data.leaf.action(ctx, thunk, value);
  887. break;
  888. case PCC_THUNK_NODE:
  889. pcc_do_action(ctx, thunk->data.node.thunks, thunk->data.node.value);
  890. break;
  891. default: /* unknown */
  892. break;
  893. }
  894. }
  895. }
  896. static void pcc_action_moduledef_0(wast_context_t *__pcc_ctx, pcc_thunk_t *__pcc_in, pcc_value_t *__pcc_out) {
  897. #define auxil (__pcc_ctx->auxil)
  898. #define __ (*__pcc_out)
  899. #define _0 pcc_get_capture_string(__pcc_ctx, &__pcc_in->data.leaf.capt0)
  900. #define _0s ((const size_t)__pcc_in->data.leaf.capt0.range.start)
  901. #define _0e ((const size_t)__pcc_in->data.leaf.capt0.range.end)
  902. printf("(module\n");
  903. #undef _0e
  904. #undef _0s
  905. #undef _0
  906. #undef __
  907. #undef auxil
  908. }
  909. static void pcc_action_moduledef2_0(wast_context_t *__pcc_ctx, pcc_thunk_t *__pcc_in, pcc_value_t *__pcc_out) {
  910. #define auxil (__pcc_ctx->auxil)
  911. #define __ (*__pcc_out)
  912. #define _0 pcc_get_capture_string(__pcc_ctx, &__pcc_in->data.leaf.capt0)
  913. #define _0s ((const size_t)__pcc_in->data.leaf.capt0.range.start)
  914. #define _0e ((const size_t)__pcc_in->data.leaf.capt0.range.end)
  915. printf(")\n");
  916. #undef _0e
  917. #undef _0s
  918. #undef _0
  919. #undef __
  920. #undef auxil
  921. }
  922. static void pcc_action_string_0(wast_context_t *__pcc_ctx, pcc_thunk_t *__pcc_in, pcc_value_t *__pcc_out) {
  923. #define auxil (__pcc_ctx->auxil)
  924. #define __ (*__pcc_out)
  925. #define _0 pcc_get_capture_string(__pcc_ctx, &__pcc_in->data.leaf.capt0)
  926. #define _0s ((const size_t)__pcc_in->data.leaf.capt0.range.start)
  927. #define _0e ((const size_t)__pcc_in->data.leaf.capt0.range.end)
  928. #define _1 pcc_get_capture_string(__pcc_ctx, __pcc_in->data.leaf.capts.buf[0])
  929. #define _1s ((const size_t)(__pcc_ctx->pos + __pcc_in->data.leaf.capts.buf[0]->range.start))
  930. #define _1e ((const size_t)(__pcc_ctx->pos + __pcc_in->data.leaf.capts.buf[0]->range.end))
  931. printf (" '%s'\n",_0);
  932. #undef _1e
  933. #undef _1s
  934. #undef _1
  935. #undef _0e
  936. #undef _0s
  937. #undef _0
  938. #undef __
  939. #undef auxil
  940. }
  941. static void pcc_action_symbol_0(wast_context_t *__pcc_ctx, pcc_thunk_t *__pcc_in, pcc_value_t *__pcc_out) {
  942. #define auxil (__pcc_ctx->auxil)
  943. #define __ (*__pcc_out)
  944. #define _0 pcc_get_capture_string(__pcc_ctx, &__pcc_in->data.leaf.capt0)
  945. #define _0s ((const size_t)__pcc_in->data.leaf.capt0.range.start)
  946. #define _0e ((const size_t)__pcc_in->data.leaf.capt0.range.end)
  947. printf (" '%s'\n",_0);
  948. #undef _0e
  949. #undef _0s
  950. #undef _0
  951. #undef __
  952. #undef auxil
  953. }
  954. static void pcc_action_endofline_0(wast_context_t *__pcc_ctx, pcc_thunk_t *__pcc_in, pcc_value_t *__pcc_out) {
  955. #define auxil (__pcc_ctx->auxil)
  956. #define __ (*__pcc_out)
  957. #define _0 pcc_get_capture_string(__pcc_ctx, &__pcc_in->data.leaf.capt0)
  958. #define _0s ((const size_t)__pcc_in->data.leaf.capt0.range.start)
  959. #define _0e ((const size_t)__pcc_in->data.leaf.capt0.range.end)
  960. linenr++;
  961. #undef _0e
  962. #undef _0s
  963. #undef _0
  964. #undef __
  965. #undef auxil
  966. }
  967. static pcc_thunk_chunk_t *pcc_evaluate_rule_file(wast_context_t *ctx);
  968. static pcc_thunk_chunk_t *pcc_evaluate_rule_lines(wast_context_t *ctx);
  969. static pcc_thunk_chunk_t *pcc_evaluate_rule_line(wast_context_t *ctx);
  970. static pcc_thunk_chunk_t *pcc_evaluate_rule_moduledef(wast_context_t *ctx);
  971. static pcc_thunk_chunk_t *pcc_evaluate_rule_moduledef2(wast_context_t *ctx);
  972. static pcc_thunk_chunk_t *pcc_evaluate_rule_string(wast_context_t *ctx);
  973. static pcc_thunk_chunk_t *pcc_evaluate_rule_char(wast_context_t *ctx);
  974. static pcc_thunk_chunk_t *pcc_evaluate_rule_symsp(wast_context_t *ctx);
  975. static pcc_thunk_chunk_t *pcc_evaluate_rule_symbol(wast_context_t *ctx);
  976. static pcc_thunk_chunk_t *pcc_evaluate_rule_moduledata(wast_context_t *ctx);
  977. static pcc_thunk_chunk_t *pcc_evaluate_rule_expression(wast_context_t *ctx);
  978. static pcc_thunk_chunk_t *pcc_evaluate_rule_atom(wast_context_t *ctx);
  979. static pcc_thunk_chunk_t *pcc_evaluate_rule_list(wast_context_t *ctx);
  980. static pcc_thunk_chunk_t *pcc_evaluate_rule_liststart(wast_context_t *ctx);
  981. static pcc_thunk_chunk_t *pcc_evaluate_rule_listend(wast_context_t *ctx);
  982. static pcc_thunk_chunk_t *pcc_evaluate_rule__(wast_context_t *ctx);
  983. static pcc_thunk_chunk_t *pcc_evaluate_rule_comment(wast_context_t *ctx);
  984. static pcc_thunk_chunk_t *pcc_evaluate_rule_space(wast_context_t *ctx);
  985. static pcc_thunk_chunk_t *pcc_evaluate_rule_endofline(wast_context_t *ctx);
  986. static pcc_thunk_chunk_t *pcc_evaluate_rule_file(wast_context_t *ctx) {
  987. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  988. chunk->pos = ctx->cur;
  989. PCC_DEBUG(PCC_DBG_EVALUATE, "file", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  990. ctx->level++;
  991. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  992. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  993. {
  994. int i;
  995. for (i = 0;; i++) {
  996. const size_t p = ctx->cur;
  997. const size_t n = chunk->thunks.len;
  998. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_lines, &chunk->thunks, NULL)) goto L0001;
  999. if (ctx->cur == p) break;
  1000. continue;
  1001. L0001:;
  1002. ctx->cur = p;
  1003. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1004. break;
  1005. }
  1006. }
  1007. {
  1008. const size_t p = ctx->cur;
  1009. const size_t n = chunk->thunks.len;
  1010. {
  1011. int u;
  1012. const size_t n = pcc_get_char_as_utf32(ctx, &u);
  1013. if (n == 0) goto L0002;
  1014. ctx->cur += n;
  1015. }
  1016. ctx->cur = p;
  1017. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1018. goto L0000;
  1019. L0002:;
  1020. ctx->cur = p;
  1021. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1022. }
  1023. ctx->level--;
  1024. PCC_DEBUG(PCC_DBG_MATCH, "file", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1025. return chunk;
  1026. L0000:;
  1027. ctx->level--;
  1028. PCC_DEBUG(PCC_DBG_NOMATCH, "file", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1029. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1030. return NULL;
  1031. }
  1032. static pcc_thunk_chunk_t *pcc_evaluate_rule_lines(wast_context_t *ctx) {
  1033. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1034. chunk->pos = ctx->cur;
  1035. PCC_DEBUG(PCC_DBG_EVALUATE, "lines", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1036. ctx->level++;
  1037. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1038. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1039. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_line, &chunk->thunks, NULL)) goto L0000;
  1040. ctx->level--;
  1041. PCC_DEBUG(PCC_DBG_MATCH, "lines", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1042. return chunk;
  1043. L0000:;
  1044. ctx->level--;
  1045. PCC_DEBUG(PCC_DBG_NOMATCH, "lines", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1046. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1047. return NULL;
  1048. }
  1049. static pcc_thunk_chunk_t *pcc_evaluate_rule_line(wast_context_t *ctx) {
  1050. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1051. chunk->pos = ctx->cur;
  1052. PCC_DEBUG(PCC_DBG_EVALUATE, "line", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1053. ctx->level++;
  1054. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1055. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1056. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_moduledef, &chunk->thunks, NULL)) goto L0000;
  1057. {
  1058. int i;
  1059. for (i = 0;; i++) {
  1060. const size_t p = ctx->cur;
  1061. const size_t n = chunk->thunks.len;
  1062. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_endofline, &chunk->thunks, NULL)) goto L0001;
  1063. if (ctx->cur == p) break;
  1064. continue;
  1065. L0001:;
  1066. ctx->cur = p;
  1067. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1068. break;
  1069. }
  1070. }
  1071. ctx->level--;
  1072. PCC_DEBUG(PCC_DBG_MATCH, "line", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1073. return chunk;
  1074. L0000:;
  1075. ctx->level--;
  1076. PCC_DEBUG(PCC_DBG_NOMATCH, "line", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1077. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1078. return NULL;
  1079. }
  1080. static pcc_thunk_chunk_t *pcc_evaluate_rule_moduledef(wast_context_t *ctx) {
  1081. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1082. chunk->pos = ctx->cur;
  1083. PCC_DEBUG(PCC_DBG_EVALUATE, "moduledef", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1084. ctx->level++;
  1085. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1086. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1087. if (
  1088. pcc_refill_buffer(ctx, 1) < 1 ||
  1089. ctx->buffer.buf[ctx->cur] != '('
  1090. ) goto L0000;
  1091. ctx->cur++;
  1092. if (!pcc_apply_rule(ctx, pcc_evaluate_rule__, &chunk->thunks, NULL)) goto L0000;
  1093. if (
  1094. pcc_refill_buffer(ctx, 6) < 6 ||
  1095. (ctx->buffer.buf + ctx->cur)[0] != 'm' ||
  1096. (ctx->buffer.buf + ctx->cur)[1] != 'o' ||
  1097. (ctx->buffer.buf + ctx->cur)[2] != 'd' ||
  1098. (ctx->buffer.buf + ctx->cur)[3] != 'u' ||
  1099. (ctx->buffer.buf + ctx->cur)[4] != 'l' ||
  1100. (ctx->buffer.buf + ctx->cur)[5] != 'e'
  1101. ) goto L0000;
  1102. ctx->cur += 6;
  1103. if (!pcc_apply_rule(ctx, pcc_evaluate_rule__, &chunk->thunks, NULL)) goto L0000;
  1104. {
  1105. pcc_thunk_t *const thunk = pcc_thunk__create_leaf(ctx->auxil, pcc_action_moduledef_0, 0, 0);
  1106. thunk->data.leaf.capt0.range.start = chunk->pos;
  1107. thunk->data.leaf.capt0.range.end = ctx->cur;
  1108. pcc_thunk_array__add(ctx->auxil, &chunk->thunks, thunk);
  1109. }
  1110. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_moduledef2, &chunk->thunks, NULL)) goto L0000;
  1111. if (!pcc_apply_rule(ctx, pcc_evaluate_rule__, &chunk->thunks, NULL)) goto L0000;
  1112. ctx->level--;
  1113. PCC_DEBUG(PCC_DBG_MATCH, "moduledef", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1114. return chunk;
  1115. L0000:;
  1116. ctx->level--;
  1117. PCC_DEBUG(PCC_DBG_NOMATCH, "moduledef", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1118. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1119. return NULL;
  1120. }
  1121. static pcc_thunk_chunk_t *pcc_evaluate_rule_moduledef2(wast_context_t *ctx) {
  1122. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1123. chunk->pos = ctx->cur;
  1124. PCC_DEBUG(PCC_DBG_EVALUATE, "moduledef2", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1125. ctx->level++;
  1126. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1127. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1128. {
  1129. int i;
  1130. for (i = 0;; i++) {
  1131. const size_t p = ctx->cur;
  1132. const size_t n = chunk->thunks.len;
  1133. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_moduledata, &chunk->thunks, NULL)) goto L0001;
  1134. if (ctx->cur == p) break;
  1135. continue;
  1136. L0001:;
  1137. ctx->cur = p;
  1138. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1139. break;
  1140. }
  1141. }
  1142. if (!pcc_apply_rule(ctx, pcc_evaluate_rule__, &chunk->thunks, NULL)) goto L0000;
  1143. if (
  1144. pcc_refill_buffer(ctx, 1) < 1 ||
  1145. ctx->buffer.buf[ctx->cur] != ')'
  1146. ) goto L0000;
  1147. ctx->cur++;
  1148. if (!pcc_apply_rule(ctx, pcc_evaluate_rule__, &chunk->thunks, NULL)) goto L0000;
  1149. {
  1150. pcc_thunk_t *const thunk = pcc_thunk__create_leaf(ctx->auxil, pcc_action_moduledef2_0, 0, 0);
  1151. thunk->data.leaf.capt0.range.start = chunk->pos;
  1152. thunk->data.leaf.capt0.range.end = ctx->cur;
  1153. pcc_thunk_array__add(ctx->auxil, &chunk->thunks, thunk);
  1154. }
  1155. ctx->level--;
  1156. PCC_DEBUG(PCC_DBG_MATCH, "moduledef2", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1157. return chunk;
  1158. L0000:;
  1159. ctx->level--;
  1160. PCC_DEBUG(PCC_DBG_NOMATCH, "moduledef2", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1161. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1162. return NULL;
  1163. }
  1164. static pcc_thunk_chunk_t *pcc_evaluate_rule_string(wast_context_t *ctx) {
  1165. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1166. chunk->pos = ctx->cur;
  1167. PCC_DEBUG(PCC_DBG_EVALUATE, "string", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1168. ctx->level++;
  1169. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1170. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 1);
  1171. if (
  1172. pcc_refill_buffer(ctx, 1) < 1 ||
  1173. ctx->buffer.buf[ctx->cur] != '\"'
  1174. ) goto L0000;
  1175. ctx->cur++;
  1176. {
  1177. const size_t p = ctx->cur;
  1178. size_t q;
  1179. {
  1180. int i;
  1181. for (i = 0;; i++) {
  1182. const size_t p = ctx->cur;
  1183. const size_t n = chunk->thunks.len;
  1184. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_char, &chunk->thunks, NULL)) goto L0001;
  1185. if (ctx->cur == p) break;
  1186. continue;
  1187. L0001:;
  1188. ctx->cur = p;
  1189. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1190. break;
  1191. }
  1192. }
  1193. q = ctx->cur;
  1194. chunk->capts.buf[0].range.start = p;
  1195. chunk->capts.buf[0].range.end = q;
  1196. }
  1197. if (
  1198. pcc_refill_buffer(ctx, 1) < 1 ||
  1199. ctx->buffer.buf[ctx->cur] != '\"'
  1200. ) goto L0000;
  1201. ctx->cur++;
  1202. {
  1203. pcc_thunk_t *const thunk = pcc_thunk__create_leaf(ctx->auxil, pcc_action_string_0, 0, 1);
  1204. thunk->data.leaf.capts.buf[0] = &(chunk->capts.buf[0]);
  1205. thunk->data.leaf.capt0.range.start = chunk->pos;
  1206. thunk->data.leaf.capt0.range.end = ctx->cur;
  1207. pcc_thunk_array__add(ctx->auxil, &chunk->thunks, thunk);
  1208. }
  1209. ctx->level--;
  1210. PCC_DEBUG(PCC_DBG_MATCH, "string", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1211. return chunk;
  1212. L0000:;
  1213. ctx->level--;
  1214. PCC_DEBUG(PCC_DBG_NOMATCH, "string", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1215. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1216. return NULL;
  1217. }
  1218. static pcc_thunk_chunk_t *pcc_evaluate_rule_char(wast_context_t *ctx) {
  1219. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1220. chunk->pos = ctx->cur;
  1221. PCC_DEBUG(PCC_DBG_EVALUATE, "char", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1222. ctx->level++;
  1223. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1224. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1225. {
  1226. const size_t p = ctx->cur;
  1227. const size_t n = chunk->thunks.len;
  1228. if (
  1229. pcc_refill_buffer(ctx, 1) < 1 ||
  1230. ctx->buffer.buf[ctx->cur] != '\\'
  1231. ) goto L0002;
  1232. ctx->cur++;
  1233. if (
  1234. pcc_refill_buffer(ctx, 1) < 1 ||
  1235. ctx->buffer.buf[ctx->cur] != '\"'
  1236. ) goto L0002;
  1237. ctx->cur++;
  1238. goto L0001;
  1239. L0002:;
  1240. ctx->cur = p;
  1241. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1242. if (
  1243. pcc_refill_buffer(ctx, 1) < 1 ||
  1244. ctx->buffer.buf[ctx->cur] != '\\'
  1245. ) goto L0003;
  1246. ctx->cur++;
  1247. if (
  1248. pcc_refill_buffer(ctx, 1) < 1 ||
  1249. ctx->buffer.buf[ctx->cur] != '\\'
  1250. ) goto L0003;
  1251. ctx->cur++;
  1252. goto L0001;
  1253. L0003:;
  1254. ctx->cur = p;
  1255. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1256. if (
  1257. pcc_refill_buffer(ctx, 1) < 1 ||
  1258. ctx->buffer.buf[ctx->cur] != '\\'
  1259. ) goto L0004;
  1260. ctx->cur++;
  1261. if (
  1262. pcc_refill_buffer(ctx, 1) < 1 ||
  1263. ctx->buffer.buf[ctx->cur] != 'b'
  1264. ) goto L0004;
  1265. ctx->cur++;
  1266. goto L0001;
  1267. L0004:;
  1268. ctx->cur = p;
  1269. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1270. if (
  1271. pcc_refill_buffer(ctx, 1) < 1 ||
  1272. ctx->buffer.buf[ctx->cur] != '\\'
  1273. ) goto L0005;
  1274. ctx->cur++;
  1275. if (
  1276. pcc_refill_buffer(ctx, 1) < 1 ||
  1277. ctx->buffer.buf[ctx->cur] != 'f'
  1278. ) goto L0005;
  1279. ctx->cur++;
  1280. goto L0001;
  1281. L0005:;
  1282. ctx->cur = p;
  1283. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1284. if (
  1285. pcc_refill_buffer(ctx, 1) < 1 ||
  1286. ctx->buffer.buf[ctx->cur] != '\\'
  1287. ) goto L0006;
  1288. ctx->cur++;
  1289. if (
  1290. pcc_refill_buffer(ctx, 1) < 1 ||
  1291. ctx->buffer.buf[ctx->cur] != 'n'
  1292. ) goto L0006;
  1293. ctx->cur++;
  1294. goto L0001;
  1295. L0006:;
  1296. ctx->cur = p;
  1297. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1298. if (
  1299. pcc_refill_buffer(ctx, 1) < 1 ||
  1300. ctx->buffer.buf[ctx->cur] != '\\'
  1301. ) goto L0007;
  1302. ctx->cur++;
  1303. if (
  1304. pcc_refill_buffer(ctx, 1) < 1 ||
  1305. ctx->buffer.buf[ctx->cur] != 'r'
  1306. ) goto L0007;
  1307. ctx->cur++;
  1308. goto L0001;
  1309. L0007:;
  1310. ctx->cur = p;
  1311. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1312. if (
  1313. pcc_refill_buffer(ctx, 1) < 1 ||
  1314. ctx->buffer.buf[ctx->cur] != '\\'
  1315. ) goto L0008;
  1316. ctx->cur++;
  1317. if (
  1318. pcc_refill_buffer(ctx, 1) < 1 ||
  1319. ctx->buffer.buf[ctx->cur] != 't'
  1320. ) goto L0008;
  1321. ctx->cur++;
  1322. goto L0001;
  1323. L0008:;
  1324. ctx->cur = p;
  1325. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1326. {
  1327. const size_t p = ctx->cur;
  1328. const size_t n = chunk->thunks.len;
  1329. if (
  1330. pcc_refill_buffer(ctx, 1) < 1 ||
  1331. ctx->buffer.buf[ctx->cur] != '\"'
  1332. ) goto L0010;
  1333. ctx->cur++;
  1334. ctx->cur = p;
  1335. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1336. goto L0009;
  1337. L0010:;
  1338. ctx->cur = p;
  1339. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1340. }
  1341. {
  1342. int u;
  1343. const size_t n = pcc_get_char_as_utf32(ctx, &u);
  1344. if (n == 0) goto L0009;
  1345. ctx->cur += n;
  1346. }
  1347. goto L0001;
  1348. L0009:;
  1349. ctx->cur = p;
  1350. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1351. goto L0000;
  1352. L0001:;
  1353. }
  1354. ctx->level--;
  1355. PCC_DEBUG(PCC_DBG_MATCH, "char", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1356. return chunk;
  1357. L0000:;
  1358. ctx->level--;
  1359. PCC_DEBUG(PCC_DBG_NOMATCH, "char", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1360. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1361. return NULL;
  1362. }
  1363. static pcc_thunk_chunk_t *pcc_evaluate_rule_symsp(wast_context_t *ctx) {
  1364. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1365. chunk->pos = ctx->cur;
  1366. PCC_DEBUG(PCC_DBG_EVALUATE, "symsp", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1367. ctx->level++;
  1368. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1369. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1370. {
  1371. const size_t p = ctx->cur;
  1372. const size_t n = chunk->thunks.len;
  1373. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_space, &chunk->thunks, NULL)) goto L0002;
  1374. goto L0001;
  1375. L0002:;
  1376. ctx->cur = p;
  1377. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1378. if (
  1379. pcc_refill_buffer(ctx, 1) < 1 ||
  1380. ctx->buffer.buf[ctx->cur] != ')'
  1381. ) goto L0003;
  1382. ctx->cur++;
  1383. goto L0001;
  1384. L0003:;
  1385. ctx->cur = p;
  1386. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1387. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_endofline, &chunk->thunks, NULL)) goto L0004;
  1388. goto L0001;
  1389. L0004:;
  1390. ctx->cur = p;
  1391. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1392. goto L0000;
  1393. L0001:;
  1394. }
  1395. ctx->level--;
  1396. PCC_DEBUG(PCC_DBG_MATCH, "symsp", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1397. return chunk;
  1398. L0000:;
  1399. ctx->level--;
  1400. PCC_DEBUG(PCC_DBG_NOMATCH, "symsp", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1401. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1402. return NULL;
  1403. }
  1404. static pcc_thunk_chunk_t *pcc_evaluate_rule_symbol(wast_context_t *ctx) {
  1405. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1406. chunk->pos = ctx->cur;
  1407. PCC_DEBUG(PCC_DBG_EVALUATE, "symbol", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1408. ctx->level++;
  1409. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1410. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1411. {
  1412. int i;
  1413. for (i = 0;; i++) {
  1414. const size_t p = ctx->cur;
  1415. const size_t n = chunk->thunks.len;
  1416. {
  1417. const size_t p = ctx->cur;
  1418. const size_t n = chunk->thunks.len;
  1419. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_symsp, &chunk->thunks, NULL)) goto L0002;
  1420. ctx->cur = p;
  1421. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1422. goto L0001;
  1423. L0002:;
  1424. ctx->cur = p;
  1425. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1426. }
  1427. {
  1428. int u;
  1429. const size_t n = pcc_get_char_as_utf32(ctx, &u);
  1430. if (n == 0) goto L0001;
  1431. ctx->cur += n;
  1432. }
  1433. if (ctx->cur == p) break;
  1434. continue;
  1435. L0001:;
  1436. ctx->cur = p;
  1437. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1438. break;
  1439. }
  1440. }
  1441. {
  1442. pcc_thunk_t *const thunk = pcc_thunk__create_leaf(ctx->auxil, pcc_action_symbol_0, 0, 0);
  1443. thunk->data.leaf.capt0.range.start = chunk->pos;
  1444. thunk->data.leaf.capt0.range.end = ctx->cur;
  1445. pcc_thunk_array__add(ctx->auxil, &chunk->thunks, thunk);
  1446. }
  1447. ctx->level--;
  1448. PCC_DEBUG(PCC_DBG_MATCH, "symbol", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1449. return chunk;
  1450. }
  1451. static pcc_thunk_chunk_t *pcc_evaluate_rule_moduledata(wast_context_t *ctx) {
  1452. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1453. chunk->pos = ctx->cur;
  1454. PCC_DEBUG(PCC_DBG_EVALUATE, "moduledata", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1455. ctx->level++;
  1456. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1457. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1458. if (!pcc_apply_rule(ctx, pcc_evaluate_rule__, &chunk->thunks, NULL)) goto L0000;
  1459. {
  1460. int i;
  1461. for (i = 0;; i++) {
  1462. const size_t p = ctx->cur;
  1463. const size_t n = chunk->thunks.len;
  1464. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_expression, &chunk->thunks, NULL)) goto L0001;
  1465. if (ctx->cur == p) break;
  1466. continue;
  1467. L0001:;
  1468. ctx->cur = p;
  1469. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1470. break;
  1471. }
  1472. }
  1473. if (!pcc_apply_rule(ctx, pcc_evaluate_rule__, &chunk->thunks, NULL)) goto L0000;
  1474. ctx->level--;
  1475. PCC_DEBUG(PCC_DBG_MATCH, "moduledata", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1476. return chunk;
  1477. L0000:;
  1478. ctx->level--;
  1479. PCC_DEBUG(PCC_DBG_NOMATCH, "moduledata", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1480. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1481. return NULL;
  1482. }
  1483. static pcc_thunk_chunk_t *pcc_evaluate_rule_expression(wast_context_t *ctx) {
  1484. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1485. chunk->pos = ctx->cur;
  1486. PCC_DEBUG(PCC_DBG_EVALUATE, "expression", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1487. ctx->level++;
  1488. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1489. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1490. if (!pcc_apply_rule(ctx, pcc_evaluate_rule__, &chunk->thunks, NULL)) goto L0000;
  1491. {
  1492. const size_t p = ctx->cur;
  1493. const size_t n = chunk->thunks.len;
  1494. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_list, &chunk->thunks, NULL)) goto L0002;
  1495. goto L0001;
  1496. L0002:;
  1497. ctx->cur = p;
  1498. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1499. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_atom, &chunk->thunks, NULL)) goto L0003;
  1500. goto L0001;
  1501. L0003:;
  1502. ctx->cur = p;
  1503. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1504. goto L0000;
  1505. L0001:;
  1506. }
  1507. if (!pcc_apply_rule(ctx, pcc_evaluate_rule__, &chunk->thunks, NULL)) goto L0000;
  1508. ctx->level--;
  1509. PCC_DEBUG(PCC_DBG_MATCH, "expression", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1510. return chunk;
  1511. L0000:;
  1512. ctx->level--;
  1513. PCC_DEBUG(PCC_DBG_NOMATCH, "expression", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1514. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1515. return NULL;
  1516. }
  1517. static pcc_thunk_chunk_t *pcc_evaluate_rule_atom(wast_context_t *ctx) {
  1518. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1519. chunk->pos = ctx->cur;
  1520. PCC_DEBUG(PCC_DBG_EVALUATE, "atom", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1521. ctx->level++;
  1522. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1523. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1524. {
  1525. const size_t p = ctx->cur;
  1526. const size_t n = chunk->thunks.len;
  1527. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_string, &chunk->thunks, NULL)) goto L0002;
  1528. goto L0001;
  1529. L0002:;
  1530. ctx->cur = p;
  1531. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1532. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_symbol, &chunk->thunks, NULL)) goto L0003;
  1533. goto L0001;
  1534. L0003:;
  1535. ctx->cur = p;
  1536. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1537. goto L0000;
  1538. L0001:;
  1539. }
  1540. if (!pcc_apply_rule(ctx, pcc_evaluate_rule__, &chunk->thunks, NULL)) goto L0000;
  1541. ctx->level--;
  1542. PCC_DEBUG(PCC_DBG_MATCH, "atom", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1543. return chunk;
  1544. L0000:;
  1545. ctx->level--;
  1546. PCC_DEBUG(PCC_DBG_NOMATCH, "atom", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1547. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1548. return NULL;
  1549. }
  1550. static pcc_thunk_chunk_t *pcc_evaluate_rule_list(wast_context_t *ctx) {
  1551. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1552. chunk->pos = ctx->cur;
  1553. PCC_DEBUG(PCC_DBG_EVALUATE, "list", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1554. ctx->level++;
  1555. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1556. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1557. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_liststart, &chunk->thunks, NULL)) goto L0000;
  1558. {
  1559. int i;
  1560. for (i = 0;; i++) {
  1561. const size_t p = ctx->cur;
  1562. const size_t n = chunk->thunks.len;
  1563. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_expression, &chunk->thunks, NULL)) goto L0001;
  1564. if (ctx->cur == p) break;
  1565. continue;
  1566. L0001:;
  1567. ctx->cur = p;
  1568. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1569. break;
  1570. }
  1571. }
  1572. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_listend, &chunk->thunks, NULL)) goto L0000;
  1573. if (!pcc_apply_rule(ctx, pcc_evaluate_rule__, &chunk->thunks, NULL)) goto L0000;
  1574. ctx->level--;
  1575. PCC_DEBUG(PCC_DBG_MATCH, "list", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1576. return chunk;
  1577. L0000:;
  1578. ctx->level--;
  1579. PCC_DEBUG(PCC_DBG_NOMATCH, "list", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1580. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1581. return NULL;
  1582. }
  1583. static pcc_thunk_chunk_t *pcc_evaluate_rule_liststart(wast_context_t *ctx) {
  1584. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1585. chunk->pos = ctx->cur;
  1586. PCC_DEBUG(PCC_DBG_EVALUATE, "liststart", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1587. ctx->level++;
  1588. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1589. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1590. if (
  1591. pcc_refill_buffer(ctx, 1) < 1 ||
  1592. ctx->buffer.buf[ctx->cur] != '('
  1593. ) goto L0000;
  1594. ctx->cur++;
  1595. ctx->level--;
  1596. PCC_DEBUG(PCC_DBG_MATCH, "liststart", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1597. return chunk;
  1598. L0000:;
  1599. ctx->level--;
  1600. PCC_DEBUG(PCC_DBG_NOMATCH, "liststart", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1601. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1602. return NULL;
  1603. }
  1604. static pcc_thunk_chunk_t *pcc_evaluate_rule_listend(wast_context_t *ctx) {
  1605. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1606. chunk->pos = ctx->cur;
  1607. PCC_DEBUG(PCC_DBG_EVALUATE, "listend", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1608. ctx->level++;
  1609. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1610. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1611. if (
  1612. pcc_refill_buffer(ctx, 1) < 1 ||
  1613. ctx->buffer.buf[ctx->cur] != ')'
  1614. ) goto L0000;
  1615. ctx->cur++;
  1616. ctx->level--;
  1617. PCC_DEBUG(PCC_DBG_MATCH, "listend", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1618. return chunk;
  1619. L0000:;
  1620. ctx->level--;
  1621. PCC_DEBUG(PCC_DBG_NOMATCH, "listend", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1622. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1623. return NULL;
  1624. }
  1625. static pcc_thunk_chunk_t *pcc_evaluate_rule__(wast_context_t *ctx) {
  1626. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1627. chunk->pos = ctx->cur;
  1628. PCC_DEBUG(PCC_DBG_EVALUATE, "_", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1629. ctx->level++;
  1630. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1631. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1632. {
  1633. int i;
  1634. for (i = 0;; i++) {
  1635. const size_t p = ctx->cur;
  1636. const size_t n = chunk->thunks.len;
  1637. {
  1638. const size_t p = ctx->cur;
  1639. const size_t n = chunk->thunks.len;
  1640. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_space, &chunk->thunks, NULL)) goto L0003;
  1641. goto L0002;
  1642. L0003:;
  1643. ctx->cur = p;
  1644. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1645. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_endofline, &chunk->thunks, NULL)) goto L0004;
  1646. goto L0002;
  1647. L0004:;
  1648. ctx->cur = p;
  1649. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1650. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_comment, &chunk->thunks, NULL)) goto L0005;
  1651. goto L0002;
  1652. L0005:;
  1653. ctx->cur = p;
  1654. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1655. goto L0001;
  1656. L0002:;
  1657. }
  1658. if (ctx->cur == p) break;
  1659. continue;
  1660. L0001:;
  1661. ctx->cur = p;
  1662. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1663. break;
  1664. }
  1665. }
  1666. ctx->level--;
  1667. PCC_DEBUG(PCC_DBG_MATCH, "_", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1668. return chunk;
  1669. }
  1670. static pcc_thunk_chunk_t *pcc_evaluate_rule_comment(wast_context_t *ctx) {
  1671. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1672. chunk->pos = ctx->cur;
  1673. PCC_DEBUG(PCC_DBG_EVALUATE, "comment", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1674. ctx->level++;
  1675. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1676. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1677. if (
  1678. pcc_refill_buffer(ctx, 2) < 2 ||
  1679. (ctx->buffer.buf + ctx->cur)[0] != ';' ||
  1680. (ctx->buffer.buf + ctx->cur)[1] != ';'
  1681. ) goto L0000;
  1682. ctx->cur += 2;
  1683. {
  1684. int i;
  1685. for (i = 0;; i++) {
  1686. const size_t p = ctx->cur;
  1687. const size_t n = chunk->thunks.len;
  1688. {
  1689. const size_t p = ctx->cur;
  1690. const size_t n = chunk->thunks.len;
  1691. if (!pcc_apply_rule(ctx, pcc_evaluate_rule_endofline, &chunk->thunks, NULL)) goto L0002;
  1692. ctx->cur = p;
  1693. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1694. goto L0001;
  1695. L0002:;
  1696. ctx->cur = p;
  1697. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1698. }
  1699. {
  1700. int u;
  1701. const size_t n = pcc_get_char_as_utf32(ctx, &u);
  1702. if (n == 0) goto L0001;
  1703. ctx->cur += n;
  1704. }
  1705. if (ctx->cur == p) break;
  1706. continue;
  1707. L0001:;
  1708. ctx->cur = p;
  1709. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1710. break;
  1711. }
  1712. }
  1713. ctx->level--;
  1714. PCC_DEBUG(PCC_DBG_MATCH, "comment", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1715. return chunk;
  1716. L0000:;
  1717. ctx->level--;
  1718. PCC_DEBUG(PCC_DBG_NOMATCH, "comment", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1719. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1720. return NULL;
  1721. }
  1722. static pcc_thunk_chunk_t *pcc_evaluate_rule_space(wast_context_t *ctx) {
  1723. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1724. chunk->pos = ctx->cur;
  1725. PCC_DEBUG(PCC_DBG_EVALUATE, "space", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1726. ctx->level++;
  1727. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1728. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1729. {
  1730. const size_t p = ctx->cur;
  1731. const size_t n = chunk->thunks.len;
  1732. if (
  1733. pcc_refill_buffer(ctx, 1) < 1 ||
  1734. ctx->buffer.buf[ctx->cur] != ' '
  1735. ) goto L0002;
  1736. ctx->cur++;
  1737. goto L0001;
  1738. L0002:;
  1739. ctx->cur = p;
  1740. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1741. if (
  1742. pcc_refill_buffer(ctx, 1) < 1 ||
  1743. ctx->buffer.buf[ctx->cur] != '\t'
  1744. ) goto L0003;
  1745. ctx->cur++;
  1746. goto L0001;
  1747. L0003:;
  1748. ctx->cur = p;
  1749. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1750. goto L0000;
  1751. L0001:;
  1752. }
  1753. ctx->level--;
  1754. PCC_DEBUG(PCC_DBG_MATCH, "space", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1755. return chunk;
  1756. L0000:;
  1757. ctx->level--;
  1758. PCC_DEBUG(PCC_DBG_NOMATCH, "space", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1759. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1760. return NULL;
  1761. }
  1762. static pcc_thunk_chunk_t *pcc_evaluate_rule_endofline(wast_context_t *ctx) {
  1763. pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);
  1764. chunk->pos = ctx->cur;
  1765. PCC_DEBUG(PCC_DBG_EVALUATE, "endofline", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
  1766. ctx->level++;
  1767. pcc_value_table__resize(ctx->auxil, &chunk->values, 0);
  1768. pcc_capture_table__resize(ctx->auxil, &chunk->capts, 0);
  1769. {
  1770. const size_t p = ctx->cur;
  1771. const size_t n = chunk->thunks.len;
  1772. if (
  1773. pcc_refill_buffer(ctx, 2) < 2 ||
  1774. (ctx->buffer.buf + ctx->cur)[0] != '\r' ||
  1775. (ctx->buffer.buf + ctx->cur)[1] != '\n'
  1776. ) goto L0002;
  1777. ctx->cur += 2;
  1778. goto L0001;
  1779. L0002:;
  1780. ctx->cur = p;
  1781. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1782. if (
  1783. pcc_refill_buffer(ctx, 1) < 1 ||
  1784. ctx->buffer.buf[ctx->cur] != '\n'
  1785. ) goto L0003;
  1786. ctx->cur++;
  1787. goto L0001;
  1788. L0003:;
  1789. ctx->cur = p;
  1790. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1791. if (
  1792. pcc_refill_buffer(ctx, 1) < 1 ||
  1793. ctx->buffer.buf[ctx->cur] != '\r'
  1794. ) goto L0004;
  1795. ctx->cur++;
  1796. goto L0001;
  1797. L0004:;
  1798. ctx->cur = p;
  1799. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1800. if (
  1801. pcc_refill_buffer(ctx, 2) < 2 ||
  1802. (ctx->buffer.buf + ctx->cur)[0] != '\n' ||
  1803. (ctx->buffer.buf + ctx->cur)[1] != '\r'
  1804. ) goto L0005;
  1805. ctx->cur += 2;
  1806. goto L0001;
  1807. L0005:;
  1808. ctx->cur = p;
  1809. pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
  1810. goto L0000;
  1811. L0001:;
  1812. }
  1813. {
  1814. pcc_thunk_t *const thunk = pcc_thunk__create_leaf(ctx->auxil, pcc_action_endofline_0, 0, 0);
  1815. thunk->data.leaf.capt0.range.start = chunk->pos;
  1816. thunk->data.leaf.capt0.range.end = ctx->cur;
  1817. pcc_thunk_array__add(ctx->auxil, &chunk->thunks, thunk);
  1818. }
  1819. ctx->level--;
  1820. PCC_DEBUG(PCC_DBG_MATCH, "endofline", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1821. return chunk;
  1822. L0000:;
  1823. ctx->level--;
  1824. PCC_DEBUG(PCC_DBG_NOMATCH, "endofline", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
  1825. pcc_thunk_chunk__destroy(ctx->auxil, chunk);
  1826. return NULL;
  1827. }
  1828. wast_context_t *wast_create(void *auxil) {
  1829. return pcc_context__create(auxil);
  1830. }
  1831. int wast_parse(wast_context_t *ctx, int *ret) {
  1832. pcc_thunk_array_t thunks;
  1833. pcc_thunk_array__init(ctx->auxil, &thunks, PCC_ARRAYSIZE);
  1834. if (pcc_apply_rule(ctx, pcc_evaluate_rule_file, &thunks, ret))
  1835. pcc_do_action(ctx, &thunks, ret);
  1836. else
  1837. PCC_ERROR(ctx->auxil);
  1838. pcc_commit_buffer(ctx);
  1839. pcc_thunk_array__term(ctx->auxil, &thunks);
  1840. return pcc_refill_buffer(ctx, 1) >= 1;
  1841. }
  1842. void wast_destroy(wast_context_t *ctx) {
  1843. pcc_context__destroy(ctx);
  1844. }
  1845. int main() {
  1846. wast_context_t *ctx = wast_create(NULL);
  1847. while (wast_parse(ctx, NULL)){;}
  1848. wast_destroy(ctx);
  1849. printf ("parsed %d lines\n", linenr);
  1850. return 0;
  1851. }