gml.c 74 KB

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