packcc.c 177 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700
  1. /*
  2. * PackCC: a packrat parser generator for C.
  3. *
  4. * Copyright (c) 2014, 2019-2021 Arihiro Yoshida. All rights reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. /*
  25. * The algorithm is based on the paper "Packrat Parsers Can Support Left Recursion"
  26. * authored by A. Warth, J. R. Douglass, and T. Millstein.
  27. *
  28. * The specification is determined by referring to peg/leg developed by Ian Piumarta.
  29. */
  30. /* added few fixex, compiled with std=c99 on GNU/LINUX
  31. * realloc may use too much memory and that can be improved
  32. * these parsers are easier and bigger and slower and use more memory but it includes lexer
  33. * with many parsers this can be usable to replace bison+flex
  34. * now only have to update a similar GNU GPL tool like this.
  35. */
  36. #ifdef _MSC_VER
  37. #define _CRT_SECURE_NO_WARNINGS
  38. #ifdef _DEBUG
  39. #define _CRTDBG_MAP_ALLOC
  40. #include <crtdbg.h>
  41. #endif
  42. #endif
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <stdarg.h>
  46. #include <string.h> /* should have strnlen() */
  47. #include <limits.h>
  48. #include <assert.h>
  49. /* #if 1 */ /* #ifndef _MSC_VER */
  50. /* #if 1 */ /* #if defined __GNUC__ && defined _WIN32 */ /* MinGW */
  51. /* do this always: #ifndef PCC_USE_SYSTEM_STRNLEN */
  52. /* #if 1 */
  53. /* #define strnlen(str, maxlen) pacc_strnlen_(str, maxlen) */
  54. /* static size_t pacc_strnlen_(const char *str, size_t maxlen) { */
  55. /* size_t i; */
  56. /* for (i = 0; i < maxlen && str[i]; i++); */
  57. /* return i; */
  58. /* } */
  59. /* #endif */ /* !PCC_USE_SYSTEM_STRNLEN */
  60. /* #endif */ /* defined __GNUC__ && defined _WIN32 */
  61. /* #endif */ /* !_MSC_VER */
  62. /* for Linux this can go away. maybe make this Linux or posix only? */
  63. #ifdef _MSC_VER
  64. #define snprintf _snprintf
  65. #define unlink _unlink
  66. #else
  67. #include <unistd.h> /* for unlink() */
  68. #endif
  69. #ifndef __has_attribute
  70. #define __attribute__(x)
  71. #endif
  72. /* what is this at this next line? to fix. */
  73. #undef TRUE /* to avoid macro definition conflicts with the system header file of IBM AIX */
  74. #undef FALSE
  75. #define VERSION "1.5.1"
  76. #ifndef BUFFER_INIT_SIZE
  77. #define BUFFER_INIT_SIZE 256
  78. #endif
  79. #ifndef ARRAY_INIT_SIZE
  80. #define ARRAY_INIT_SIZE 2
  81. #endif
  82. #define VOID_VALUE (~(size_t)0)
  83. #ifdef _WIN64 /* 64-bit Windows including MSVC and MinGW-w64 */
  84. #define FMT_LU "%llu"
  85. typedef unsigned long long ulong_t;
  86. /* NOTE: "%llu" and "long long" are not C89-compliant, but they are required to deal with a 64-bit integer value in 64-bit Windows. */
  87. #else
  88. #define FMT_LU "%lu"
  89. typedef unsigned long ulong_t;
  90. #endif
  91. /* FMT_LU and ulong_t are used to print size_t values safely (ex. printf(FMT_LU "\n", (ulong_t)value);) */
  92. /* NOTE: Neither "%z" nor <inttypes.h> is used since PackCC complies with the C89 standard as much as possible. */
  93. typedef enum bool_tag {
  94. FALSE = 0,
  95. TRUE
  96. } bool_t;
  97. typedef struct char_array_tag {
  98. char *buf;
  99. size_t max;
  100. size_t len;
  101. } char_array_t;
  102. typedef enum node_type_tag {
  103. NODE_RULE = 0,
  104. NODE_REFERENCE,
  105. NODE_STRING,
  106. NODE_CHARCLASS,
  107. NODE_QUANTITY,
  108. NODE_PREDICATE,
  109. NODE_SEQUENCE,
  110. NODE_ALTERNATE,
  111. NODE_CAPTURE,
  112. NODE_EXPAND,
  113. NODE_ACTION,
  114. NODE_ERROR
  115. } node_type_t;
  116. typedef struct node_tag node_t;
  117. typedef struct node_array_tag {
  118. node_t **buf;
  119. size_t max;
  120. size_t len;
  121. } node_array_t;
  122. typedef struct node_const_array_tag {
  123. const node_t **buf;
  124. size_t max;
  125. size_t len;
  126. } node_const_array_t;
  127. typedef struct node_hash_table_tag {
  128. const node_t **buf;
  129. size_t max;
  130. size_t mod;
  131. } node_hash_table_t;
  132. typedef struct node_rule_tag {
  133. char *name;
  134. node_t *expr;
  135. int ref; /* mutable */
  136. node_const_array_t vars;
  137. node_const_array_t capts;
  138. node_const_array_t codes;
  139. size_t line;
  140. size_t col;
  141. } node_rule_t;
  142. typedef struct node_reference_tag {
  143. char *var; /* NULL if no variable name */
  144. size_t index;
  145. char *name;
  146. const node_t *rule;
  147. size_t line;
  148. size_t col;
  149. } node_reference_t;
  150. typedef struct node_string_tag {
  151. char *value;
  152. } node_string_t;
  153. typedef struct node_charclass_tag {
  154. char *value; /* NULL means any character */
  155. } node_charclass_t;
  156. typedef struct node_quantity_tag {
  157. int min;
  158. int max;
  159. node_t *expr;
  160. } node_quantity_t;
  161. typedef struct node_predicate_tag {
  162. bool_t neg;
  163. node_t *expr;
  164. } node_predicate_t;
  165. typedef struct node_sequence_tag {
  166. node_array_t nodes;
  167. } node_sequence_t;
  168. typedef struct node_alternate_tag {
  169. node_array_t nodes;
  170. } node_alternate_t;
  171. typedef struct node_capture_tag {
  172. node_t *expr;
  173. size_t index;
  174. } node_capture_t;
  175. typedef struct node_expand_tag {
  176. size_t index;
  177. size_t line;
  178. size_t col;
  179. } node_expand_t;
  180. typedef struct node_action_tag {
  181. char *value;
  182. size_t index;
  183. node_const_array_t vars;
  184. node_const_array_t capts;
  185. } node_action_t;
  186. typedef struct node_error_tag {
  187. node_t *expr;
  188. char *value;
  189. size_t index;
  190. node_const_array_t vars;
  191. node_const_array_t capts;
  192. } node_error_t;
  193. typedef union node_data_tag {
  194. node_rule_t rule;
  195. node_reference_t reference;
  196. node_string_t string;
  197. node_charclass_t charclass;
  198. node_quantity_t quantity;
  199. node_predicate_t predicate;
  200. node_sequence_t sequence;
  201. node_alternate_t alternate;
  202. node_capture_t capture;
  203. node_expand_t expand;
  204. node_action_t action;
  205. node_error_t error;
  206. } node_data_t;
  207. struct node_tag {
  208. node_type_t type;
  209. node_data_t data;
  210. };
  211. typedef enum code_flag_tag {
  212. CODE_FLAG__NONE = 0,
  213. CODE_FLAG__UTF8_CHARCLASS_USED = 1
  214. } code_flag_t;
  215. typedef struct context_tag {
  216. char *iname; /* the path name of the PEG file being parsed */
  217. char *sname; /* the path name of the C source file being generated */
  218. char *hname; /* the path name of the C header file being generated */
  219. FILE *ifile; /* the input stream of the PEG file */
  220. char *hid; /* the macro name for the include guard of the C header file */
  221. char *vtype; /* the type name of the data output by the parsing API function (NULL means the default) */
  222. char *atype; /* the type name of the user-defined data passed to the parser creation API function (NULL means the default) */
  223. char *prefix; /* the prefix of the API function names (NULL means the default) */
  224. bool_t ascii; /* UTF-8 support disabled if true */
  225. bool_t debug; /* debug information is output if true */
  226. code_flag_t flags; /* bitwise flags to control code generation; updated during PEG parsing */
  227. size_t errnum; /* the current number of PEG parsing errors */
  228. size_t linenum; /* the current line number (0-based) */
  229. size_t linepos; /* the beginning position in the PEG file of the current line */
  230. size_t bufpos; /* the position in the PEG file of the first character currently buffered */
  231. size_t bufcur; /* the current parsing position in the character buffer */
  232. char_array_t buffer; /* the character buffer */
  233. node_array_t rules; /* the PEG rules */
  234. node_hash_table_t rulehash; /* the hash table to accelerate access of desired PEG rules */
  235. char_array_t esource; /* the code blocks from %earlysource and %earlycommon directives to be added into the generated source file */
  236. char_array_t eheader; /* the code blocks from %earlyheader and %earlycommon directives to be added into the generated header file */
  237. char_array_t source; /* the code blocks from %source and %common directives to be added into the generated source file */
  238. char_array_t header; /* the code blocks from %header and %common directives to be added into the generated header file */
  239. } context_t;
  240. typedef struct generate_tag {
  241. FILE *stream;
  242. const node_t *rule;
  243. int label;
  244. bool_t ascii;
  245. } generate_t;
  246. typedef enum string_flag_tag {
  247. STRING_FLAG__NONE = 0,
  248. STRING_FLAG__NOTEMPTY = 1,
  249. STRING_FLAG__NOTVOID = 2,
  250. STRING_FLAG__IDENTIFIER = 4
  251. } string_flag_t;
  252. typedef enum code_reach_tag {
  253. CODE_REACH__BOTH = 0,
  254. CODE_REACH__ALWAYS_SUCCEED = 1,
  255. CODE_REACH__ALWAYS_FAIL = -1
  256. } code_reach_t;
  257. static const char *g_cmdname = "packcc"; /* replaced later with actual one */
  258. __attribute__((format(printf, 1, 2)))
  259. static int print_error(const char *format, ...) {
  260. int n;
  261. va_list a;
  262. va_start(a, format);
  263. n = fprintf(stderr, "%s: ", g_cmdname); /* packcc: message ... */
  264. if (n >= 0) {
  265. const int k = vfprintf(stderr, format, a);
  266. if (k < 0) n = k; else n += k;
  267. }
  268. va_end(a);
  269. return n;
  270. }
  271. static FILE *fopen_rb_e(const char *path) {
  272. FILE *const f = fopen(path, "rb");
  273. if (f == NULL) {
  274. print_error("fopen_rb_e(): Cannot open file '%s' to read\n", path);
  275. exit(2);
  276. }
  277. return f;
  278. }
  279. static FILE *fopen_wt_e(const char *path) {
  280. FILE *const f = fopen(path, "wt");
  281. if (f == NULL) {
  282. print_error("fopen_wt_e(): Cannot open file '%s' to write\n", path);
  283. exit(2);
  284. }
  285. return f;
  286. }
  287. static int fclose_e(FILE *stream) {
  288. const int r = fclose(stream);
  289. if (r == EOF) {
  290. print_error("fclose_e(): File closing error\n");
  291. exit(2);
  292. }
  293. return r;
  294. }
  295. static int fgetc_e(FILE *stream) {
  296. const int c = fgetc(stream);
  297. if (c == EOF && ferror(stream)) {
  298. print_error("fgetc_e(): File read error\n");
  299. exit(2);
  300. }
  301. return c;
  302. }
  303. static int fputc_e(int c, FILE *stream) {
  304. const int r = fputc(c, stream);
  305. if (r == EOF) {
  306. print_error("fputc_e(); File write error\n");
  307. exit(2);
  308. }
  309. return r;
  310. }
  311. static int fputs_e(const char *s, FILE *stream) {
  312. const int r = fputs(s, stream);
  313. if (r == EOF) {
  314. print_error("fputs_e(); File write error\n");
  315. exit(2);
  316. }
  317. return r;
  318. }
  319. __attribute__((format(printf, 2, 3)))
  320. static int fprintf_e(FILE *stream, const char *format, ...) {
  321. int n;
  322. va_list a;
  323. va_start(a, format);
  324. n = vfprintf(stream, format, a);
  325. va_end(a);
  326. if (n < 0) {
  327. print_error("fprintf_e(): File write error\n");
  328. exit(2);
  329. }
  330. return n;
  331. }
  332. static void *malloc_e(size_t size) {
  333. void *const p = malloc(size);
  334. if (p == NULL) {
  335. print_error("malloc_e(): Out of memory\n");
  336. exit(3);
  337. }
  338. return p;
  339. }
  340. static void *realloc_e(void *ptr, size_t size) {
  341. void *const p = realloc(ptr, size);
  342. if (p == NULL) {
  343. print_error("realloc_e(): Out of memory\n");
  344. exit(3);
  345. }
  346. return p;
  347. }
  348. static char *strdup_e(const char *str) {
  349. const size_t m = strlen(str);
  350. char *const s = (char *)malloc_e(m + 1);
  351. memmove(s, str, m);
  352. s[m] = '\0';
  353. return s;
  354. }
  355. static char *strndup_e(const char *str, size_t len) {
  356. const size_t m = strnlen(str, len);
  357. char *const s = (char *)malloc_e(m + 1);
  358. memmove(s, str, m);
  359. s[m] = '\0';
  360. return s;
  361. }
  362. static size_t string_to_size_t(const char *str) {
  363. /* #define N (~(size_t)0 / 10)
  364. * #define M (~(size_t)0 - 10 * N)
  365. */
  366. size_t n = 0, i, k;
  367. for (i = 0; str[i]; i++) {
  368. const char c = str[i];
  369. if (c < '0' || c > '9') return VOID_VALUE;
  370. k = (size_t)(c - '0');
  371. /* orig: if (n >= N && k > M) return VOID_VALUE; overflow */
  372. if (n >= (~(size_t)0 / 10) /* N */ && k > (~(size_t)0 - 10 * (~(size_t)0 / 10) /* N */ ) /* M */ ) return VOID_VALUE; /* overflow */
  373. n = k + 10 * n;
  374. }
  375. return n;
  376. /* #undef N
  377. * #undef M
  378. */
  379. }
  380. static size_t find_first_trailing_space(const char *str, size_t start, size_t end, size_t *next) {
  381. size_t j = start, i;
  382. for (i = start; i < end; i++) {
  383. switch (str[i]) {
  384. case ' ':
  385. case '\v':
  386. case '\f':
  387. case '\t':
  388. break;
  389. case '\n':
  390. if (next) *next = i + 1;
  391. return j;
  392. case '\r':
  393. if (i + 1 < end && str[i + 1] == '\n') i++;
  394. if (next) *next = i + 1;
  395. return j;
  396. default:
  397. j = i + 1;
  398. }
  399. }
  400. if (next) *next = end;
  401. return j;
  402. }
  403. static size_t count_indent_spaces(const char *str, size_t start, size_t end, size_t *next) {
  404. size_t n = 0, i;
  405. for (i = start; i < end; i++) {
  406. switch (str[i]) {
  407. case ' ':
  408. case '\v':
  409. case '\f':
  410. n++;
  411. break;
  412. case '\t':
  413. n = (n + 8) & ~7;
  414. break;
  415. default:
  416. if (next) *next = i;
  417. return n;
  418. }
  419. }
  420. if (next) *next = end;
  421. return n;
  422. }
  423. static bool_t is_filled_string(const char *str) {
  424. size_t i;
  425. for (i = 0; str[i]; i++) {
  426. if (
  427. str[i] != ' ' &&
  428. str[i] != '\v' &&
  429. str[i] != '\f' &&
  430. str[i] != '\t' &&
  431. str[i] != '\n' &&
  432. str[i] != '\r'
  433. ) return TRUE;
  434. }
  435. return FALSE;
  436. }
  437. static bool_t is_identifier_string(const char *str) {
  438. size_t i;
  439. if (!(
  440. (str[0] >= 'a' && str[0] <= 'z') ||
  441. (str[0] >= 'A' && str[0] <= 'Z') ||
  442. str[0] == '_'
  443. )) return FALSE;
  444. for (i = 1; str[i]; i++) {
  445. if (!(
  446. (str[i] >= 'a' && str[i] <= 'z') ||
  447. (str[i] >= 'A' && str[i] <= 'Z') ||
  448. (str[i] >= '0' && str[i] <= '9') ||
  449. str[i] == '_'
  450. )) return FALSE;
  451. }
  452. return TRUE;
  453. }
  454. static bool_t is_pointer_type(const char *str) {
  455. const size_t n = strlen(str);
  456. return (n > 0 && str[n - 1] == '*') ? TRUE : FALSE;
  457. }
  458. static bool_t is_valid_utf8_string(const char *str) {
  459. int k = 0, n = 0, u = 0;
  460. size_t i;
  461. for (i = 0; str[i]; i++) {
  462. const int c = (int)(unsigned char)str[i];
  463. switch (k) {
  464. case 0:
  465. if (c >= 0x80) {
  466. if ((c & 0xe0) == 0xc0) {
  467. u = c & 0x1f;
  468. n = k = 1;
  469. }
  470. else if ((c & 0xf0) == 0xe0) {
  471. u = c & 0x0f;
  472. n = k = 2;
  473. }
  474. else if ((c & 0xf8) == 0xf0) {
  475. u = c & 0x07;
  476. n = k = 3;
  477. }
  478. else {
  479. return FALSE;
  480. }
  481. }
  482. break;
  483. case 1:
  484. case 2:
  485. case 3:
  486. if ((c & 0xc0) == 0x80) {
  487. u <<= 6;
  488. u |= c & 0x3f;
  489. k--;
  490. if (k == 0) {
  491. switch (n) {
  492. case 1:
  493. if (u < 0x80) return FALSE;
  494. break;
  495. case 2:
  496. if (u < 0x800) return FALSE;
  497. break;
  498. case 3:
  499. if (u < 0x10000 || u > 0x10ffff) return FALSE;
  500. break;
  501. default:
  502. assert(((void)"unexpected control flow", 0));
  503. return FALSE; /* never reached */
  504. }
  505. u = 0;
  506. n = 0;
  507. }
  508. }
  509. else {
  510. return FALSE;
  511. }
  512. break;
  513. default:
  514. assert(((void)"unexpected control flow", 0));
  515. return FALSE; /* never reached */
  516. }
  517. }
  518. return (k == 0) ? TRUE : FALSE;
  519. }
  520. static size_t utf8_to_utf32(const char *seq, int *out) { /* without checking UTF-8 validity */
  521. const int c = (int)(unsigned char)seq[0];
  522. const size_t n =
  523. (c == 0) ? 0 : (c < 0x80) ? 1 :
  524. ((c & 0xe0) == 0xc0) ? 2 :
  525. ((c & 0xf0) == 0xe0) ? 3 :
  526. ((c & 0xf8) == 0xf0) ? 4 : 1;
  527. int u = 0;
  528. switch (n) {
  529. case 0:
  530. case 1:
  531. u = c;
  532. break;
  533. case 2:
  534. u = ((c & 0x1f) << 6) |
  535. ((int)(unsigned char)seq[1] & 0x3f);
  536. break;
  537. case 3:
  538. u = ((c & 0x0f) << 12) |
  539. (((int)(unsigned char)seq[1] & 0x3f) << 6) |
  540. (seq[1] ? ((int)(unsigned char)seq[2] & 0x3f) : 0);
  541. break;
  542. default:
  543. u = ((c & 0x07) << 18) |
  544. (((int)(unsigned char)seq[1] & 0x3f) << 12) |
  545. (seq[1] ? (((int)(unsigned char)seq[2] & 0x3f) << 6) : 0) |
  546. (seq[2] ? ((int)(unsigned char)seq[3] & 0x3f) : 0);
  547. }
  548. if (out) *out = u;
  549. return n;
  550. }
  551. static bool_t unescape_string(char *str, bool_t cls) { /* cls: TRUE if used for character class matching */
  552. bool_t b = TRUE;
  553. size_t i, j;
  554. for (j = 0, i = 0; str[i]; i++) {
  555. if (str[i] == '\\') {
  556. i++;
  557. switch (str[i]) {
  558. case '\0': str[j++] = '\\'; str[j] = '\0'; return FALSE;
  559. case '\'': str[j++] = '\''; break;
  560. case '\"': str[j++] = '\"'; break;
  561. case '0': str[j++] = '\x00'; break;
  562. case 'a': str[j++] = '\x07'; break;
  563. case 'b': str[j++] = '\x08'; break;
  564. case 'f': str[j++] = '\x0c'; break;
  565. case 'n': str[j++] = '\x0a'; break;
  566. case 'r': str[j++] = '\x0d'; break;
  567. case 't': str[j++] = '\x09'; break;
  568. case 'v': str[j++] = '\x0b'; break;
  569. case 'x':
  570. {
  571. char s = 0, c;
  572. size_t k;
  573. for (k = 0; k < 2; k++) {
  574. char d;
  575. c = str[i + k + 1];
  576. d = (c >= '0' && c <= '9') ? c - '0' :
  577. (c >= 'a' && c <= 'f') ? c - 'a' + 10 :
  578. (c >= 'A' && c <= 'F') ? c - 'A' + 10 : -1;
  579. if (d < 0) break;
  580. s = (s << 4) | d;
  581. }
  582. if (k < 2) {
  583. const size_t l = i + k;
  584. str[j++] = '\\'; str[j++] = 'x';
  585. while (i <= l) str[j++] = str[++i];
  586. if (c == '\0') return FALSE;
  587. b = FALSE;
  588. continue;
  589. }
  590. str[j++] = s;
  591. i += 2;
  592. }
  593. break;
  594. case 'u':
  595. {
  596. int s = 0, t = 0;
  597. char c;
  598. size_t k;
  599. for (k = 0; k < 4; k++) {
  600. char d;
  601. c = str[i + k + 1];
  602. d = (c >= '0' && c <= '9') ? c - '0' :
  603. (c >= 'a' && c <= 'f') ? c - 'a' + 10 :
  604. (c >= 'A' && c <= 'F') ? c - 'A' + 10 : -1;
  605. if (d < 0) break;
  606. s = (s << 4) | d;
  607. }
  608. if (k < 4 || (s & 0xfc00) == 0xdc00) { /* invalid character or invalid surrogate code point */
  609. const size_t l = i + k;
  610. str[j++] = '\\'; str[j++] = 'u';
  611. while (i <= l) str[j++] = str[++i];
  612. if (c == '\0') return FALSE;
  613. b = FALSE;
  614. continue;
  615. }
  616. if ((s & 0xfc00) == 0xd800) { /* surrogate pair */
  617. for (k = 4; k < 10; k++) {
  618. c = str[i + k + 1];
  619. if (k == 4) {
  620. if (c != '\\') break;
  621. }
  622. else if (k == 5) {
  623. if (c != 'u') break;
  624. }
  625. else {
  626. const char d =
  627. (c >= '0' && c <= '9') ? c - '0' :
  628. (c >= 'a' && c <= 'f') ? c - 'a' + 10 :
  629. (c >= 'A' && c <= 'F') ? c - 'A' + 10 : -1;
  630. if (d < 0) break;
  631. t = (t << 4) | d;
  632. }
  633. }
  634. if (k < 10 || (t & 0xfc00) != 0xdc00) { /* invalid character or invalid surrogate code point */
  635. const size_t l = i + 4; /* NOTE: Not i + k to redo with recovery. */
  636. str[j++] = '\\'; str[j++] = 'u';
  637. while (i <= l) str[j++] = str[++i];
  638. b = FALSE;
  639. continue;
  640. }
  641. }
  642. {
  643. const int u = t ? ((((s & 0x03ff) + 0x0040) << 10) | (t & 0x03ff)) : s;
  644. if (u < 0x0080) {
  645. str[j++] = (char)u;
  646. }
  647. else if (u < 0x0800) {
  648. str[j++] = (char)(0xc0 | (u >> 6));
  649. str[j++] = (char)(0x80 | (u & 0x3f));
  650. }
  651. else if (u < 0x010000) {
  652. str[j++] = (char)(0xe0 | (u >> 12));
  653. str[j++] = (char)(0x80 | ((u >> 6) & 0x3f));
  654. str[j++] = (char)(0x80 | (u & 0x3f));
  655. }
  656. else if (u < 0x110000) {
  657. str[j++] = (char)(0xf0 | (u >> 18));
  658. str[j++] = (char)(0x80 | ((u >> 12) & 0x3f));
  659. str[j++] = (char)(0x80 | ((u >> 6) & 0x3f));
  660. str[j++] = (char)(0x80 | (u & 0x3f));
  661. }
  662. else { /* never reached theoretically; in case */
  663. const size_t l = i + 10;
  664. str[j++] = '\\'; str[j++] = 'u';
  665. while (i <= l) str[j++] = str[++i];
  666. b = FALSE;
  667. continue;
  668. }
  669. }
  670. i += t ? 10 : 4;
  671. }
  672. break;
  673. case '\n': break;
  674. case '\r': if (str[i + 1] == '\n') i++; break;
  675. case '\\':
  676. if (cls) str[j++] = '\\'; /* left for character class matching (ex. considering [\^\]\\]) */
  677. str[j++] = '\\';
  678. break;
  679. default: str[j++] = '\\'; str[j++] = str[i];
  680. }
  681. }
  682. else {
  683. str[j++] = str[i];
  684. }
  685. }
  686. str[j] = '\0';
  687. return b;
  688. }
  689. static const char *escape_character(char ch, char (*buf)[5]) {
  690. switch (ch) {
  691. case '\x00': strncpy(*buf, "\\0", 5); break;
  692. case '\x07': strncpy(*buf, "\\a", 5); break;
  693. case '\x08': strncpy(*buf, "\\b", 5); break;
  694. case '\x0c': strncpy(*buf, "\\f", 5); break;
  695. case '\x0a': strncpy(*buf, "\\n", 5); break;
  696. case '\x0d': strncpy(*buf, "\\r", 5); break;
  697. case '\x09': strncpy(*buf, "\\t", 5); break;
  698. case '\x0b': strncpy(*buf, "\\v", 5); break;
  699. case '\\': strncpy(*buf, "\\\\", 5); break;
  700. case '\'': strncpy(*buf, "\\\'", 5); break;
  701. case '\"': strncpy(*buf, "\\\"", 5); break;
  702. default:
  703. if (ch >= '\x20' && ch < '\x7f')
  704. snprintf(*buf, 5, "%c", ch);
  705. else
  706. snprintf(*buf, 5, "\\x%02x", (int)(unsigned char)ch);
  707. }
  708. (*buf)[4] = '\0';
  709. return *buf;
  710. }
  711. static void remove_heading_blank(char *str) {
  712. size_t i, j;
  713. for (i = 0; str[i]; i++) {
  714. if (
  715. str[i] != ' ' &&
  716. str[i] != '\v' &&
  717. str[i] != '\f' &&
  718. str[i] != '\t' &&
  719. str[i] != '\n' &&
  720. str[i] != '\r'
  721. ) break;
  722. }
  723. for (j = 0; str[i]; i++) {
  724. str[j++] = str[i];
  725. }
  726. str[j] = '\0';
  727. }
  728. static void remove_trailing_blank(char *str) {
  729. size_t i, j;
  730. for (j = 0, i = 0; str[i]; i++) {
  731. if (
  732. str[i] != ' ' &&
  733. str[i] != '\v' &&
  734. str[i] != '\f' &&
  735. str[i] != '\t' &&
  736. str[i] != '\n' &&
  737. str[i] != '\r'
  738. ) j = i + 1;
  739. }
  740. str[j] = '\0';
  741. }
  742. static void make_header_identifier(char *str) {
  743. size_t i;
  744. for (i = 0; str[i]; i++) {
  745. str[i] =
  746. ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= '0' && str[i] <= '9')) ? str[i] :
  747. (str[i] >= 'a' && str[i] <= 'z') ? str[i] - 'a' + 'A' : '_';
  748. }
  749. }
  750. static void write_characters(FILE *stream, char ch, size_t len) {
  751. size_t i;
  752. for (i = 0; i < len; i++) fputc_e(ch, stream);
  753. }
  754. static void write_text(FILE *stream, const char *ptr, size_t len) {
  755. size_t i;
  756. for (i = 0; i < len; i++) {
  757. if (ptr[i] == '\r') {
  758. if (i + 1 < len && ptr[i + 1] == '\n') i++;
  759. fputc_e('\n', stream);
  760. }
  761. else {
  762. fputc_e(ptr[i], stream);
  763. }
  764. }
  765. }
  766. static void write_code_block(FILE *stream, const char *ptr, size_t len, size_t indent) {
  767. size_t i, j, k;
  768. j = find_first_trailing_space(ptr, 0, len, &k);
  769. for (i = 0; i < j; i++) {
  770. if (
  771. ptr[i] != ' ' &&
  772. ptr[i] != '\v' &&
  773. ptr[i] != '\f' &&
  774. ptr[i] != '\t'
  775. ) break;
  776. }
  777. if (i < j) {
  778. write_characters(stream, ' ', indent);
  779. write_text(stream, ptr + i, j - i);
  780. fputc_e('\n', stream);
  781. }
  782. if (k < len) {
  783. size_t m = VOID_VALUE;
  784. size_t h;
  785. for (i = k; i < len; i = h) {
  786. j = find_first_trailing_space(ptr, i, len, &h);
  787. if (i < j) {
  788. const size_t l = count_indent_spaces(ptr, i, j, NULL);
  789. if (m == VOID_VALUE || m > l) m = l;
  790. }
  791. }
  792. for (i = k; i < len; i = h) {
  793. j = find_first_trailing_space(ptr, i, len, &h);
  794. if (i < j) {
  795. const size_t l = count_indent_spaces(ptr, i, j, &i);
  796. assert(m != VOID_VALUE); /* m must have a valid value */
  797. assert(l >= m);
  798. write_characters(stream, ' ', l - m + indent);
  799. write_text(stream, ptr + i, j - i);
  800. fputc_e('\n', stream);
  801. }
  802. else if (h < len) {
  803. fputc_e('\n', stream);
  804. }
  805. }
  806. }
  807. }
  808. static const char *extract_filename(const char *path) {
  809. size_t i = strlen(path);
  810. while (i > 0) {
  811. i--;
  812. if (path[i] == '/' || path[i] == '\\' || path[i] == ':') return path + i + 1;
  813. }
  814. return path;
  815. }
  816. static const char *extract_fileext(const char *path) {
  817. const size_t n = strlen(path);
  818. size_t i = n;
  819. while (i > 0) {
  820. i--;
  821. if (path[i] == '/' || path[i] == '\\' || path[i] == ':') break;
  822. if (path[i] == '.') return path + i;
  823. }
  824. return path + n;
  825. }
  826. static char *replace_fileext(const char *path, const char *ext) {
  827. const char *const p = extract_fileext(path);
  828. const size_t m = p - path;
  829. const size_t n = strlen(ext);
  830. char *const s = (char *)malloc_e(m + n + 2);
  831. memmove(s, path, m);
  832. s[m] = '.';
  833. memmove(s + m + 1, ext, n + 1);
  834. return s;
  835. }
  836. static char *add_fileext(const char *path, const char *ext) {
  837. const size_t m = strlen(path);
  838. const size_t n = strlen(ext);
  839. char *const s = (char *)malloc_e(m + n + 2);
  840. memmove(s, path, m);
  841. s[m] = '.';
  842. memmove(s + m + 1, ext, n + 1);
  843. return s;
  844. }
  845. static size_t hash_string(const char *str) {
  846. size_t i, h = 0;
  847. for (i = 0; str[i]; i++) {
  848. h = h * 31 + str[i];
  849. }
  850. return h;
  851. }
  852. static size_t populate_bits(size_t x) {
  853. x |= x >> 1;
  854. x |= x >> 2;
  855. x |= x >> 4;
  856. x |= x >> 8;
  857. x |= x >> 16;
  858. #if (defined __SIZEOF_SIZE_T__ && __SIZEOF_SIZE_T__ == 8) /* gcc or clang */ || defined _WIN64 /* MSVC */
  859. x |= x >> 32;
  860. #endif
  861. return x;
  862. }
  863. static size_t column_number(const context_t *ctx) { /* 0-based */
  864. assert(ctx->bufpos + ctx->bufcur >= ctx->linepos);
  865. return ctx->bufpos + ctx->bufcur - ctx->linepos;
  866. }
  867. static void char_array__init(char_array_t *array, size_t max) {
  868. array->len = 0;
  869. array->max = max;
  870. array->buf = (char *)malloc_e(array->max);
  871. }
  872. static void char_array__add(char_array_t *array, char ch) {
  873. if (array->max <= array->len) {
  874. const size_t n = array->len + 1;
  875. size_t m = array->max;
  876. if (m == 0) m = 1;
  877. while (m < n && m != 0) m <<= 1;
  878. if (m == 0) m = n; /* in case of shift overflow */
  879. array->buf = (char *)realloc_e(array->buf, m);
  880. array->max = m;
  881. }
  882. array->buf[array->len++] = ch;
  883. }
  884. static void char_array__append(char_array_t *array, const char *str, size_t len) {
  885. if (array->max < array->len + len) {
  886. const size_t n = array->len + len;
  887. size_t m = array->max;
  888. if (m == 0) m = 1;
  889. while (m < n && m != 0) m <<= 1;
  890. if (m == 0) m = n; /* in case of shift overflow */
  891. array->buf = (char *)realloc_e(array->buf, m);
  892. array->max = m;
  893. }
  894. memmove(array->buf + array->len, str, len);
  895. array->len += len;
  896. }
  897. static void char_array__term(char_array_t *array) {
  898. free(array->buf);
  899. }
  900. static void node_array__init(node_array_t *array, size_t max) {
  901. array->len = 0;
  902. array->max = max;
  903. array->buf = (node_t **)malloc_e(sizeof(node_t *) * array->max);
  904. }
  905. static void node_array__add(node_array_t *array, node_t *node) {
  906. if (array->max <= array->len) {
  907. const size_t n = array->len + 1;
  908. size_t m = array->max;
  909. if (m == 0) m = 1;
  910. while (m < n && m != 0) m <<= 1;
  911. if (m == 0) m = n; /* in case of shift overflow */
  912. array->buf = (node_t **)realloc_e(array->buf, sizeof(node_t *) * m);
  913. array->max = m;
  914. }
  915. array->buf[array->len++] = node;
  916. }
  917. static void destroy_node(node_t *node);
  918. static void node_array__term(node_array_t *array) {
  919. while (array->len > 0) {
  920. array->len--;
  921. destroy_node(array->buf[array->len]);
  922. }
  923. free(array->buf);
  924. }
  925. static void node_const_array__init(node_const_array_t *array, size_t max) {
  926. array->len = 0;
  927. array->max = max;
  928. array->buf = (const node_t **)malloc_e(sizeof(const node_t *) * array->max);
  929. }
  930. static void node_const_array__add(node_const_array_t *array, const node_t *node) {
  931. if (array->max <= array->len) {
  932. const size_t n = array->len + 1;
  933. size_t m = array->max;
  934. if (m == 0) m = 1;
  935. while (m < n && m != 0) m <<= 1;
  936. if (m == 0) m = n; /* in case of shift overflow */
  937. array->buf = (const node_t **)realloc_e((node_t **)array->buf, sizeof(const node_t *) * m);
  938. array->max = m;
  939. }
  940. array->buf[array->len++] = node;
  941. }
  942. static void node_const_array__clear(node_const_array_t *array) {
  943. array->len = 0;
  944. }
  945. static void node_const_array__copy(node_const_array_t *array, const node_const_array_t *src) {
  946. size_t i;
  947. node_const_array__clear(array);
  948. for (i = 0; i < src->len; i++) {
  949. node_const_array__add(array, src->buf[i]);
  950. }
  951. }
  952. static void node_const_array__term(node_const_array_t *array) {
  953. free((node_t **)array->buf);
  954. }
  955. static context_t *create_context(const char *iname, const char *oname, bool_t ascii, bool_t debug) {
  956. context_t *const ctx = (context_t *)malloc_e(sizeof(context_t));
  957. ctx->iname = strdup_e((iname && iname[0]) ? iname : "-");
  958. ctx->sname = (oname && oname[0]) ? add_fileext(oname, "c") : replace_fileext(ctx->iname, "c");
  959. ctx->hname = (oname && oname[0]) ? add_fileext(oname, "h") : replace_fileext(ctx->iname, "h");
  960. ctx->ifile = (iname && iname[0]) ? fopen_rb_e(ctx->iname) : stdin;
  961. ctx->hid = strdup_e(ctx->hname); make_header_identifier(ctx->hid);
  962. ctx->vtype = NULL;
  963. ctx->atype = NULL;
  964. ctx->prefix = NULL;
  965. ctx->ascii = ascii;
  966. ctx->debug = debug;
  967. ctx->flags = CODE_FLAG__NONE;
  968. ctx->errnum = 0;
  969. ctx->linenum = 0;
  970. ctx->linepos = 0;
  971. ctx->bufpos = 0;
  972. ctx->bufcur = 0;
  973. char_array__init(&ctx->buffer, BUFFER_INIT_SIZE);
  974. node_array__init(&ctx->rules, ARRAY_INIT_SIZE);
  975. ctx->rulehash.mod = 0;
  976. ctx->rulehash.max = 0;
  977. ctx->rulehash.buf = NULL;
  978. char_array__init(&ctx->esource, BUFFER_INIT_SIZE);
  979. char_array__init(&ctx->eheader, BUFFER_INIT_SIZE);
  980. char_array__init(&ctx->source, BUFFER_INIT_SIZE);
  981. char_array__init(&ctx->header, BUFFER_INIT_SIZE);
  982. return ctx;
  983. }
  984. static node_t *create_node(node_type_t type) {
  985. node_t *const node = (node_t *)malloc_e(sizeof(node_t));
  986. node->type = type;
  987. switch (node->type) {
  988. case NODE_RULE:
  989. node->data.rule.name = NULL;
  990. node->data.rule.expr = NULL;
  991. node->data.rule.ref = 0;
  992. node_const_array__init(&node->data.rule.vars, ARRAY_INIT_SIZE);
  993. node_const_array__init(&node->data.rule.capts, ARRAY_INIT_SIZE);
  994. node_const_array__init(&node->data.rule.codes, ARRAY_INIT_SIZE);
  995. node->data.rule.line = VOID_VALUE;
  996. node->data.rule.col = VOID_VALUE;
  997. break;
  998. case NODE_REFERENCE:
  999. node->data.reference.var = NULL;
  1000. node->data.reference.index = VOID_VALUE;
  1001. node->data.reference.name = NULL;
  1002. node->data.reference.rule = NULL;
  1003. node->data.reference.line = VOID_VALUE;
  1004. node->data.reference.col = VOID_VALUE;
  1005. break;
  1006. case NODE_STRING:
  1007. node->data.string.value = NULL;
  1008. break;
  1009. case NODE_CHARCLASS:
  1010. node->data.charclass.value = NULL;
  1011. break;
  1012. case NODE_QUANTITY:
  1013. node->data.quantity.min = node->data.quantity.max = 0;
  1014. node->data.quantity.expr = NULL;
  1015. break;
  1016. case NODE_PREDICATE:
  1017. node->data.predicate.neg = FALSE;
  1018. node->data.predicate.expr = NULL;
  1019. break;
  1020. case NODE_SEQUENCE:
  1021. node_array__init(&node->data.sequence.nodes, ARRAY_INIT_SIZE);
  1022. break;
  1023. case NODE_ALTERNATE:
  1024. node_array__init(&node->data.alternate.nodes, ARRAY_INIT_SIZE);
  1025. break;
  1026. case NODE_CAPTURE:
  1027. node->data.capture.expr = NULL;
  1028. node->data.capture.index = VOID_VALUE;
  1029. break;
  1030. case NODE_EXPAND:
  1031. node->data.expand.index = VOID_VALUE;
  1032. node->data.expand.line = VOID_VALUE;
  1033. node->data.expand.col = VOID_VALUE;
  1034. break;
  1035. case NODE_ACTION:
  1036. node->data.action.value = NULL;
  1037. node->data.action.index = VOID_VALUE;
  1038. node_const_array__init(&node->data.action.vars, ARRAY_INIT_SIZE);
  1039. node_const_array__init(&node->data.action.capts, ARRAY_INIT_SIZE);
  1040. break;
  1041. case NODE_ERROR:
  1042. node->data.error.expr = NULL;
  1043. node->data.error.value = NULL;
  1044. node->data.error.index = VOID_VALUE;
  1045. node_const_array__init(&node->data.error.vars, ARRAY_INIT_SIZE);
  1046. node_const_array__init(&node->data.error.capts, ARRAY_INIT_SIZE);
  1047. break;
  1048. default:
  1049. print_error("Internal error [%d]\n", __LINE__);
  1050. exit(-1);
  1051. }
  1052. return node;
  1053. }
  1054. static void destroy_node(node_t *node) {
  1055. if (node == NULL) return;
  1056. switch (node->type) {
  1057. case NODE_RULE:
  1058. node_const_array__term(&node->data.rule.codes);
  1059. node_const_array__term(&node->data.rule.capts);
  1060. node_const_array__term(&node->data.rule.vars);
  1061. destroy_node(node->data.rule.expr);
  1062. free(node->data.rule.name);
  1063. break;
  1064. case NODE_REFERENCE:
  1065. free(node->data.reference.name);
  1066. free(node->data.reference.var);
  1067. break;
  1068. case NODE_STRING:
  1069. free(node->data.string.value);
  1070. break;
  1071. case NODE_CHARCLASS:
  1072. free(node->data.charclass.value);
  1073. break;
  1074. case NODE_QUANTITY:
  1075. destroy_node(node->data.quantity.expr);
  1076. break;
  1077. case NODE_PREDICATE:
  1078. destroy_node(node->data.predicate.expr);
  1079. break;
  1080. case NODE_SEQUENCE:
  1081. node_array__term(&node->data.sequence.nodes);
  1082. break;
  1083. case NODE_ALTERNATE:
  1084. node_array__term(&node->data.alternate.nodes);
  1085. break;
  1086. case NODE_CAPTURE:
  1087. destroy_node(node->data.capture.expr);
  1088. break;
  1089. case NODE_EXPAND:
  1090. break;
  1091. case NODE_ACTION:
  1092. node_const_array__term(&node->data.action.capts);
  1093. node_const_array__term(&node->data.action.vars);
  1094. free(node->data.action.value);
  1095. break;
  1096. case NODE_ERROR:
  1097. node_const_array__term(&node->data.error.capts);
  1098. node_const_array__term(&node->data.error.vars);
  1099. free(node->data.error.value);
  1100. destroy_node(node->data.error.expr);
  1101. break;
  1102. default:
  1103. print_error("Internal error [%d]\n", __LINE__);
  1104. exit(-1);
  1105. }
  1106. free(node);
  1107. }
  1108. static void destroy_context(context_t *ctx) {
  1109. if (ctx == NULL) return;
  1110. char_array__term(&ctx->header);
  1111. char_array__term(&ctx->source);
  1112. char_array__term(&ctx->eheader);
  1113. char_array__term(&ctx->esource);
  1114. free((node_t **)ctx->rulehash.buf);
  1115. node_array__term(&ctx->rules);
  1116. char_array__term(&ctx->buffer);
  1117. free(ctx->prefix);
  1118. free(ctx->atype);
  1119. free(ctx->vtype);
  1120. free(ctx->hid);
  1121. fclose_e(ctx->ifile);
  1122. free(ctx->hname);
  1123. free(ctx->sname);
  1124. free(ctx->iname);
  1125. free(ctx);
  1126. }
  1127. static void make_rulehash(context_t *ctx) {
  1128. size_t i, j;
  1129. ctx->rulehash.mod = populate_bits(ctx->rules.len * 4);
  1130. ctx->rulehash.max = ctx->rulehash.mod + 1;
  1131. ctx->rulehash.buf = (const node_t **)realloc_e((node_t **)ctx->rulehash.buf, sizeof(const node_t *) * ctx->rulehash.max);
  1132. for (i = 0; i < ctx->rulehash.max; i++) {
  1133. ctx->rulehash.buf[i] = NULL;
  1134. }
  1135. for (i = 0; i < ctx->rules.len; i++) {
  1136. assert(ctx->rules.buf[i]->type == NODE_RULE);
  1137. j = hash_string(ctx->rules.buf[i]->data.rule.name) & ctx->rulehash.mod;
  1138. while (ctx->rulehash.buf[j] != NULL) {
  1139. if (strcmp(ctx->rules.buf[i]->data.rule.name, ctx->rulehash.buf[j]->data.rule.name) == 0) {
  1140. assert(ctx->rules.buf[i]->data.rule.ref == 0);
  1141. assert(ctx->rulehash.buf[j]->data.rule.ref == 0);
  1142. ctx->rules.buf[i]->data.rule.ref = -1;
  1143. goto EXCEPTION;
  1144. }
  1145. j = (j + 1) & ctx->rulehash.mod;
  1146. }
  1147. ctx->rulehash.buf[j] = ctx->rules.buf[i];
  1148. EXCEPTION:;
  1149. }
  1150. }
  1151. static const node_t *lookup_rulehash(const context_t *ctx, const char *name) {
  1152. size_t j = hash_string(name) & ctx->rulehash.mod;
  1153. while (ctx->rulehash.buf[j] != NULL && strcmp(name, ctx->rulehash.buf[j]->data.rule.name) != 0) {
  1154. j = (j + 1) & ctx->rulehash.mod;
  1155. }
  1156. return (ctx->rulehash.buf[j] != NULL) ? ctx->rulehash.buf[j] : NULL;
  1157. }
  1158. static void link_references(context_t *ctx, node_t *node) {
  1159. if (node == NULL) return;
  1160. switch (node->type) {
  1161. case NODE_RULE:
  1162. print_error("Internal error [%d]\n", __LINE__);
  1163. exit(-1);
  1164. case NODE_REFERENCE:
  1165. node->data.reference.rule = lookup_rulehash(ctx, node->data.reference.name);
  1166. if (node->data.reference.rule == NULL) {
  1167. print_error("%s:" FMT_LU ":" FMT_LU ": No definition of rule '%s'\n",
  1168. ctx->iname, (ulong_t)(node->data.reference.line + 1), (ulong_t)(node->data.reference.col + 1),
  1169. node->data.reference.name);
  1170. ctx->errnum++;
  1171. }
  1172. else {
  1173. assert(node->data.reference.rule->type == NODE_RULE);
  1174. ((node_t *)node->data.reference.rule)->data.rule.ref++;
  1175. }
  1176. break;
  1177. case NODE_STRING:
  1178. break;
  1179. case NODE_CHARCLASS:
  1180. break;
  1181. case NODE_QUANTITY:
  1182. link_references(ctx, node->data.quantity.expr);
  1183. break;
  1184. case NODE_PREDICATE:
  1185. link_references(ctx, node->data.predicate.expr);
  1186. break;
  1187. case NODE_SEQUENCE:
  1188. {
  1189. size_t i;
  1190. for (i = 0; i < node->data.sequence.nodes.len; i++) {
  1191. link_references(ctx, node->data.sequence.nodes.buf[i]);
  1192. }
  1193. }
  1194. break;
  1195. case NODE_ALTERNATE:
  1196. {
  1197. size_t i;
  1198. for (i = 0; i < node->data.alternate.nodes.len; i++) {
  1199. link_references(ctx, node->data.alternate.nodes.buf[i]);
  1200. }
  1201. }
  1202. break;
  1203. case NODE_CAPTURE:
  1204. link_references(ctx, node->data.capture.expr);
  1205. break;
  1206. case NODE_EXPAND:
  1207. break;
  1208. case NODE_ACTION:
  1209. break;
  1210. case NODE_ERROR:
  1211. link_references(ctx, node->data.error.expr);
  1212. break;
  1213. default:
  1214. print_error("Internal error [%d]\n", __LINE__);
  1215. exit(-1);
  1216. }
  1217. }
  1218. static void verify_variables(context_t *ctx, node_t *node, node_const_array_t *vars) {
  1219. node_const_array_t a;
  1220. const bool_t b = (vars == NULL) ? TRUE : FALSE;
  1221. if (node == NULL) return;
  1222. if (b) {
  1223. node_const_array__init(&a, ARRAY_INIT_SIZE);
  1224. vars = &a;
  1225. }
  1226. switch (node->type) {
  1227. case NODE_RULE:
  1228. print_error("Internal error [%d]\n", __LINE__);
  1229. exit(-1);
  1230. case NODE_REFERENCE:
  1231. if (node->data.reference.index != VOID_VALUE) {
  1232. size_t i;
  1233. for (i = 0; i < vars->len; i++) {
  1234. assert(vars->buf[i]->type == NODE_REFERENCE);
  1235. if (node->data.reference.index == vars->buf[i]->data.reference.index) break;
  1236. }
  1237. if (i == vars->len) node_const_array__add(vars, node);
  1238. }
  1239. break;
  1240. case NODE_STRING:
  1241. break;
  1242. case NODE_CHARCLASS:
  1243. break;
  1244. case NODE_QUANTITY:
  1245. verify_variables(ctx, node->data.quantity.expr, vars);
  1246. break;
  1247. case NODE_PREDICATE:
  1248. verify_variables(ctx, node->data.predicate.expr, vars);
  1249. break;
  1250. case NODE_SEQUENCE:
  1251. {
  1252. size_t i;
  1253. for (i = 0; i < node->data.sequence.nodes.len; i++) {
  1254. verify_variables(ctx, node->data.sequence.nodes.buf[i], vars);
  1255. }
  1256. }
  1257. break;
  1258. case NODE_ALTERNATE:
  1259. {
  1260. size_t i, j, k, m = vars->len;
  1261. node_const_array_t v;
  1262. node_const_array__init(&v, ARRAY_INIT_SIZE);
  1263. node_const_array__copy(&v, vars);
  1264. for (i = 0; i < node->data.alternate.nodes.len; i++) {
  1265. v.len = m;
  1266. verify_variables(ctx, node->data.alternate.nodes.buf[i], &v);
  1267. for (j = m; j < v.len; j++) {
  1268. for (k = m; k < vars->len; k++) {
  1269. if (v.buf[j]->data.reference.index == vars->buf[k]->data.reference.index) break;
  1270. }
  1271. if (k == vars->len) node_const_array__add(vars, v.buf[j]);
  1272. }
  1273. }
  1274. node_const_array__term(&v);
  1275. }
  1276. break;
  1277. case NODE_CAPTURE:
  1278. verify_variables(ctx, node->data.capture.expr, vars);
  1279. break;
  1280. case NODE_EXPAND:
  1281. break;
  1282. case NODE_ACTION:
  1283. node_const_array__copy(&node->data.action.vars, vars);
  1284. break;
  1285. case NODE_ERROR:
  1286. node_const_array__copy(&node->data.error.vars, vars);
  1287. verify_variables(ctx, node->data.error.expr, vars);
  1288. break;
  1289. default:
  1290. print_error("Internal error [%d]\n", __LINE__);
  1291. exit(-1);
  1292. }
  1293. if (b) {
  1294. node_const_array__term(&a);
  1295. }
  1296. }
  1297. static void verify_captures(context_t *ctx, node_t *node, node_const_array_t *capts) {
  1298. node_const_array_t a;
  1299. const bool_t b = (capts == NULL) ? TRUE : FALSE;
  1300. if (node == NULL) return;
  1301. if (b) {
  1302. node_const_array__init(&a, ARRAY_INIT_SIZE);
  1303. capts = &a;
  1304. }
  1305. switch (node->type) {
  1306. case NODE_RULE:
  1307. print_error("Internal error [%d]\n", __LINE__);
  1308. exit(-1);
  1309. case NODE_REFERENCE:
  1310. break;
  1311. case NODE_STRING:
  1312. break;
  1313. case NODE_CHARCLASS:
  1314. break;
  1315. case NODE_QUANTITY:
  1316. verify_captures(ctx, node->data.quantity.expr, capts);
  1317. break;
  1318. case NODE_PREDICATE:
  1319. verify_captures(ctx, node->data.predicate.expr, capts);
  1320. break;
  1321. case NODE_SEQUENCE:
  1322. {
  1323. size_t i;
  1324. for (i = 0; i < node->data.sequence.nodes.len; i++) {
  1325. verify_captures(ctx, node->data.sequence.nodes.buf[i], capts);
  1326. }
  1327. }
  1328. break;
  1329. case NODE_ALTERNATE:
  1330. {
  1331. size_t i, j, m = capts->len;
  1332. node_const_array_t v;
  1333. node_const_array__init(&v, ARRAY_INIT_SIZE);
  1334. node_const_array__copy(&v, capts);
  1335. for (i = 0; i < node->data.alternate.nodes.len; i++) {
  1336. v.len = m;
  1337. verify_captures(ctx, node->data.alternate.nodes.buf[i], &v);
  1338. for (j = m; j < v.len; j++) {
  1339. node_const_array__add(capts, v.buf[j]);
  1340. }
  1341. }
  1342. node_const_array__term(&v);
  1343. }
  1344. break;
  1345. case NODE_CAPTURE:
  1346. verify_captures(ctx, node->data.capture.expr, capts);
  1347. node_const_array__add(capts, node);
  1348. break;
  1349. case NODE_EXPAND:
  1350. {
  1351. size_t i;
  1352. for (i = 0; i < capts->len; i++) {
  1353. assert(capts->buf[i]->type == NODE_CAPTURE);
  1354. if (node->data.expand.index == capts->buf[i]->data.capture.index) break;
  1355. }
  1356. if (i >= capts->len && node->data.expand.index != VOID_VALUE) {
  1357. print_error("%s:" FMT_LU ":" FMT_LU ": Capture " FMT_LU " not available at this position\n",
  1358. ctx->iname, (ulong_t)(node->data.expand.line + 1), (ulong_t)(node->data.expand.col + 1), (ulong_t)(node->data.expand.index + 1));
  1359. ctx->errnum++;
  1360. }
  1361. }
  1362. break;
  1363. case NODE_ACTION:
  1364. node_const_array__copy(&node->data.action.capts, capts);
  1365. break;
  1366. case NODE_ERROR:
  1367. node_const_array__copy(&node->data.error.capts, capts);
  1368. verify_captures(ctx, node->data.error.expr, capts);
  1369. break;
  1370. default:
  1371. print_error("Internal error [%d]\n", __LINE__);
  1372. exit(-1);
  1373. }
  1374. if (b) {
  1375. node_const_array__term(&a);
  1376. }
  1377. }
  1378. static void dump_escaped(const char *s) {
  1379. char buf[5];
  1380. if (s == NULL) {
  1381. fprintf(stdout, "null");
  1382. return;
  1383. }
  1384. while (*s) {
  1385. escape_character(*s++, &buf);
  1386. fprintf(stdout, "%s", buf);
  1387. }
  1388. }
  1389. static void dump_void_value(size_t value) {
  1390. if (value == VOID_VALUE) {
  1391. fprintf(stdout, "void");
  1392. }
  1393. else {
  1394. fprintf(stdout, FMT_LU, (ulong_t)value);
  1395. }
  1396. }
  1397. static void dump_node(context_t *ctx, const node_t *node, const int indent) {
  1398. if (node == NULL) return;
  1399. switch (node->type) {
  1400. case NODE_RULE:
  1401. fprintf(stdout, "%*sRule(name:'%s', ref:%d, vars.len:" FMT_LU ", capts.len:" FMT_LU ", codes.len:" FMT_LU ") {\n",
  1402. indent, "", node->data.rule.name, node->data.rule.ref,
  1403. (ulong_t)node->data.rule.vars.len, (ulong_t)node->data.rule.capts.len, (ulong_t)node->data.rule.codes.len);
  1404. dump_node(ctx, node->data.rule.expr, indent + 2);
  1405. fprintf(stdout, "%*s}\n", indent, "");
  1406. break;
  1407. case NODE_REFERENCE:
  1408. fprintf(stdout, "%*sReference(var:'%s', index:", indent, "", node->data.reference.var);
  1409. dump_void_value(node->data.reference.index);
  1410. fprintf(stdout, ", name:'%s', rule:'%s')\n", node->data.reference.name,
  1411. (node->data.reference.rule) ? node->data.reference.rule->data.rule.name : NULL);
  1412. break;
  1413. case NODE_STRING:
  1414. fprintf(stdout, "%*sString(value:'", indent, "");
  1415. dump_escaped(node->data.string.value);
  1416. fprintf(stdout, "')\n");
  1417. break;
  1418. case NODE_CHARCLASS:
  1419. fprintf(stdout, "%*sCharclass(value:'", indent, "");
  1420. dump_escaped(node->data.charclass.value);
  1421. fprintf(stdout, "')\n");
  1422. break;
  1423. case NODE_QUANTITY:
  1424. fprintf(stdout, "%*sQuantity(min:%d, max%d) {\n", indent, "", node->data.quantity.min, node->data.quantity.max);
  1425. dump_node(ctx, node->data.quantity.expr, indent + 2);
  1426. fprintf(stdout, "%*s}\n", indent, "");
  1427. break;
  1428. case NODE_PREDICATE:
  1429. fprintf(stdout, "%*sPredicate(neg:%d) {\n", indent, "", node->data.predicate.neg);
  1430. dump_node(ctx, node->data.predicate.expr, indent + 2);
  1431. fprintf(stdout, "%*s}\n", indent, "");
  1432. break;
  1433. case NODE_SEQUENCE:
  1434. fprintf(stdout, "%*sSequence(max:" FMT_LU ", len:" FMT_LU ") {\n",
  1435. indent, "", (ulong_t)node->data.sequence.nodes.max, (ulong_t)node->data.sequence.nodes.len);
  1436. {
  1437. size_t i;
  1438. for (i = 0; i < node->data.sequence.nodes.len; i++) {
  1439. dump_node(ctx, node->data.sequence.nodes.buf[i], indent + 2);
  1440. }
  1441. }
  1442. fprintf(stdout, "%*s}\n", indent, "");
  1443. break;
  1444. case NODE_ALTERNATE:
  1445. fprintf(stdout, "%*sAlternate(max:" FMT_LU ", len:" FMT_LU ") {\n",
  1446. indent, "", (ulong_t)node->data.alternate.nodes.max, (ulong_t)node->data.alternate.nodes.len);
  1447. {
  1448. size_t i;
  1449. for (i = 0; i < node->data.alternate.nodes.len; i++) {
  1450. dump_node(ctx, node->data.alternate.nodes.buf[i], indent + 2);
  1451. }
  1452. }
  1453. fprintf(stdout, "%*s}\n", indent, "");
  1454. break;
  1455. case NODE_CAPTURE:
  1456. fprintf(stdout, "%*sCapture(index:", indent, "");
  1457. dump_void_value(node->data.capture.index);
  1458. fprintf(stdout, ") {\n");
  1459. dump_node(ctx, node->data.capture.expr, indent + 2);
  1460. fprintf(stdout, "%*s}\n", indent, "");
  1461. break;
  1462. case NODE_EXPAND:
  1463. fprintf(stdout, "%*sExpand(index:", indent, "");
  1464. dump_void_value(node->data.expand.index);
  1465. fprintf(stdout, ")\n");
  1466. break;
  1467. case NODE_ACTION:
  1468. fprintf(stdout, "%*sAction(index:", indent, "");
  1469. dump_void_value(node->data.action.index);
  1470. fprintf(stdout, ", value:{");
  1471. dump_escaped(node->data.action.value);
  1472. fprintf(stdout, "}, vars:");
  1473. if (node->data.action.vars.len + node->data.action.capts.len > 0) {
  1474. size_t i;
  1475. fprintf(stdout, "\n");
  1476. for (i = 0; i < node->data.action.vars.len; i++) {
  1477. fprintf(stdout, "%*s'%s'\n", indent + 2, "", node->data.action.vars.buf[i]->data.reference.var);
  1478. }
  1479. for (i = 0; i < node->data.action.capts.len; i++) {
  1480. fprintf(stdout, "%*s$" FMT_LU "\n", indent + 2, "", (ulong_t)(node->data.action.capts.buf[i]->data.capture.index + 1));
  1481. }
  1482. fprintf(stdout, "%*s)\n", indent, "");
  1483. }
  1484. else {
  1485. fprintf(stdout, "none)\n");
  1486. }
  1487. break;
  1488. case NODE_ERROR:
  1489. fprintf(stdout, "%*sError(index:", indent, "");
  1490. dump_void_value(node->data.error.index);
  1491. fprintf(stdout, ", value:{");
  1492. dump_escaped(node->data.error.value);
  1493. fprintf(stdout, "}, vars:\n");
  1494. {
  1495. size_t i;
  1496. for (i = 0; i < node->data.error.vars.len; i++) {
  1497. fprintf(stdout, "%*s'%s'\n", indent + 2, "", node->data.error.vars.buf[i]->data.reference.var);
  1498. }
  1499. for (i = 0; i < node->data.error.capts.len; i++) {
  1500. fprintf(stdout, "%*s$" FMT_LU "\n", indent + 2, "", (ulong_t)(node->data.error.capts.buf[i]->data.capture.index + 1));
  1501. }
  1502. }
  1503. fprintf(stdout, "%*s) {\n", indent, "");
  1504. dump_node(ctx, node->data.error.expr, indent + 2);
  1505. fprintf(stdout, "%*s}\n", indent, "");
  1506. break;
  1507. default:
  1508. print_error("%*sInternal error [%d]\n", indent, "", __LINE__);
  1509. exit(-1);
  1510. }
  1511. }
  1512. static size_t refill_buffer(context_t *ctx, size_t num) {
  1513. if (ctx->buffer.len >= ctx->bufcur + num) return ctx->buffer.len - ctx->bufcur;
  1514. while (ctx->buffer.len < ctx->bufcur + num) {
  1515. const int c = fgetc_e(ctx->ifile);
  1516. if (c == EOF) break;
  1517. char_array__add(&ctx->buffer, (char)c);
  1518. }
  1519. return ctx->buffer.len - ctx->bufcur;
  1520. }
  1521. static void commit_buffer(context_t *ctx) {
  1522. assert(ctx->buffer.len >= ctx->bufcur);
  1523. memmove(ctx->buffer.buf, ctx->buffer.buf + ctx->bufcur, ctx->buffer.len - ctx->bufcur);
  1524. ctx->buffer.len -= ctx->bufcur;
  1525. ctx->bufpos += ctx->bufcur;
  1526. ctx->bufcur = 0;
  1527. }
  1528. static bool_t match_eof(context_t *ctx) {
  1529. return (refill_buffer(ctx, 1) < 1) ? TRUE : FALSE;
  1530. }
  1531. static bool_t match_eol(context_t *ctx) {
  1532. if (refill_buffer(ctx, 1) >= 1) {
  1533. switch (ctx->buffer.buf[ctx->bufcur]) {
  1534. case '\n':
  1535. ctx->bufcur++;
  1536. ctx->linenum++;
  1537. ctx->linepos = ctx->bufpos + ctx->bufcur;
  1538. return TRUE;
  1539. case '\r':
  1540. ctx->bufcur++;
  1541. if (refill_buffer(ctx, 1) >= 1) {
  1542. if (ctx->buffer.buf[ctx->bufcur] == '\n') ctx->bufcur++;
  1543. }
  1544. ctx->linenum++;
  1545. ctx->linepos = ctx->bufpos + ctx->bufcur;
  1546. return TRUE;
  1547. }
  1548. }
  1549. return FALSE;
  1550. }
  1551. static bool_t match_character(context_t *ctx, char ch) {
  1552. if (refill_buffer(ctx, 1) >= 1) {
  1553. if (ctx->buffer.buf[ctx->bufcur] == ch) {
  1554. ctx->bufcur++;
  1555. return TRUE;
  1556. }
  1557. }
  1558. return FALSE;
  1559. }
  1560. static bool_t match_character_range(context_t *ctx, char min, char max) {
  1561. if (refill_buffer(ctx, 1) >= 1) {
  1562. const char c = ctx->buffer.buf[ctx->bufcur];
  1563. if (c >= min && c <= max) {
  1564. ctx->bufcur++;
  1565. return TRUE;
  1566. }
  1567. }
  1568. return FALSE;
  1569. }
  1570. static bool_t match_character_set(context_t *ctx, const char *chs) {
  1571. if (refill_buffer(ctx, 1) >= 1) {
  1572. const char c = ctx->buffer.buf[ctx->bufcur];
  1573. size_t i;
  1574. for (i = 0; chs[i]; i++) {
  1575. if (c == chs[i]) {
  1576. ctx->bufcur++;
  1577. return TRUE;
  1578. }
  1579. }
  1580. }
  1581. return FALSE;
  1582. }
  1583. static bool_t match_character_any(context_t *ctx) {
  1584. if (refill_buffer(ctx, 1) >= 1) {
  1585. ctx->bufcur++;
  1586. return TRUE;
  1587. }
  1588. return FALSE;
  1589. }
  1590. static bool_t match_string(context_t *ctx, const char *str) {
  1591. const size_t n = strlen(str);
  1592. if (refill_buffer(ctx, n) >= n) {
  1593. if (strncmp(ctx->buffer.buf + ctx->bufcur, str, n) == 0) {
  1594. ctx->bufcur += n;
  1595. return TRUE;
  1596. }
  1597. }
  1598. return FALSE;
  1599. }
  1600. static bool_t match_blank(context_t *ctx) {
  1601. return match_character_set(ctx, " \t\v\f");
  1602. }
  1603. static bool_t match_section_line_(context_t *ctx, const char *head) {
  1604. if (match_string(ctx, head)) {
  1605. while (!match_eol(ctx) && !match_eof(ctx)) match_character_any(ctx);
  1606. return TRUE;
  1607. }
  1608. return FALSE;
  1609. }
  1610. static bool_t match_section_line_continuable_(context_t *ctx, const char *head) {
  1611. if (match_string(ctx, head)) {
  1612. while (!match_eof(ctx)) {
  1613. const size_t p = ctx->bufcur;
  1614. if (match_eol(ctx)) {
  1615. if (ctx->buffer.buf[p - 1] != '\\') break;
  1616. }
  1617. else {
  1618. match_character_any(ctx);
  1619. }
  1620. }
  1621. return TRUE;
  1622. }
  1623. return FALSE;
  1624. }
  1625. static bool_t match_section_block_(context_t *ctx, const char *left, const char *right, const char *name) {
  1626. const size_t l = ctx->linenum;
  1627. const size_t m = column_number(ctx);
  1628. if (match_string(ctx, left)) {
  1629. while (!match_string(ctx, right)) {
  1630. if (match_eof(ctx)) {
  1631. print_error("%s:" FMT_LU ":" FMT_LU ": Premature EOF in %s\n", ctx->iname, (ulong_t)(l + 1), (ulong_t)(m + 1), name);
  1632. ctx->errnum++;
  1633. break;
  1634. }
  1635. if (!match_eol(ctx)) match_character_any(ctx);
  1636. }
  1637. return TRUE;
  1638. }
  1639. return FALSE;
  1640. }
  1641. static bool_t match_quotation_(context_t *ctx, const char *left, const char *right, const char *name) {
  1642. const size_t l = ctx->linenum;
  1643. const size_t m = column_number(ctx);
  1644. if (match_string(ctx, left)) {
  1645. while (!match_string(ctx, right)) {
  1646. if (match_eof(ctx)) {
  1647. print_error("%s:" FMT_LU ":" FMT_LU ": Premature EOF in %s\n", ctx->iname, (ulong_t)(l + 1), (ulong_t)(m + 1), name);
  1648. ctx->errnum++;
  1649. break;
  1650. }
  1651. if (match_character(ctx, '\\')) {
  1652. if (!match_eol(ctx)) match_character_any(ctx);
  1653. }
  1654. else {
  1655. if (match_eol(ctx)) {
  1656. print_error("%s:" FMT_LU ":" FMT_LU ": Premature EOL in %s\n", ctx->iname, (ulong_t)(l + 1), (ulong_t)(m + 1), name);
  1657. ctx->errnum++;
  1658. break;
  1659. }
  1660. match_character_any(ctx);
  1661. }
  1662. }
  1663. return TRUE;
  1664. }
  1665. return FALSE;
  1666. }
  1667. static bool_t match_directive_c(context_t *ctx) {
  1668. return match_section_line_continuable_(ctx, "#");
  1669. }
  1670. static bool_t match_comment(context_t *ctx) {
  1671. return match_section_line_(ctx, "#");
  1672. }
  1673. static bool_t match_comment_c(context_t *ctx) {
  1674. return match_section_block_(ctx, "/*", "*/", "C comment");
  1675. }
  1676. static bool_t match_comment_cxx(context_t *ctx) {
  1677. return match_section_line_(ctx, "//");
  1678. }
  1679. static bool_t match_quotation_single(context_t *ctx) {
  1680. return match_quotation_(ctx, "\'", "\'", "single quotation");
  1681. }
  1682. static bool_t match_quotation_double(context_t *ctx) {
  1683. return match_quotation_(ctx, "\"", "\"", "double quotation");
  1684. }
  1685. static bool_t match_character_class(context_t *ctx) {
  1686. return match_quotation_(ctx, "[", "]", "character class");
  1687. }
  1688. static bool_t match_spaces(context_t *ctx) {
  1689. size_t n = 0;
  1690. while (match_blank(ctx) || match_eol(ctx) || match_comment(ctx)) n++;
  1691. return (n > 0) ? TRUE : FALSE;
  1692. }
  1693. static bool_t match_number(context_t *ctx) {
  1694. if (match_character_range(ctx, '0', '9')) {
  1695. while (match_character_range(ctx, '0', '9'));
  1696. return TRUE;
  1697. }
  1698. return FALSE;
  1699. }
  1700. static bool_t match_identifier(context_t *ctx) {
  1701. if (
  1702. match_character_range(ctx, 'a', 'z') ||
  1703. match_character_range(ctx, 'A', 'Z') ||
  1704. match_character(ctx, '_')
  1705. ) {
  1706. while (
  1707. match_character_range(ctx, 'a', 'z') ||
  1708. match_character_range(ctx, 'A', 'Z') ||
  1709. match_character_range(ctx, '0', '9') ||
  1710. match_character(ctx, '_')
  1711. );
  1712. return TRUE;
  1713. }
  1714. return FALSE;
  1715. }
  1716. static bool_t match_code_block(context_t *ctx) {
  1717. const size_t l = ctx->linenum;
  1718. const size_t m = column_number(ctx);
  1719. if (match_character(ctx, '{')) {
  1720. int d = 1;
  1721. for (;;) {
  1722. if (match_eof(ctx)) {
  1723. print_error("%s:" FMT_LU ":" FMT_LU ": Premature EOF in code block\n", ctx->iname, (ulong_t)(l + 1), (ulong_t)(m + 1));
  1724. ctx->errnum++;
  1725. break;
  1726. }
  1727. if (
  1728. match_directive_c(ctx) ||
  1729. match_comment_c(ctx) ||
  1730. match_comment_cxx(ctx) ||
  1731. match_quotation_single(ctx) ||
  1732. match_quotation_double(ctx)
  1733. ) continue;
  1734. if (match_character(ctx, '{')) {
  1735. d++;
  1736. }
  1737. else if (match_character(ctx, '}')) {
  1738. d--;
  1739. if (d == 0) break;
  1740. }
  1741. else {
  1742. if (!match_eol(ctx)) {
  1743. if (match_character(ctx, '$')) {
  1744. ctx->buffer.buf[ctx->bufcur - 1] = '_';
  1745. }
  1746. else {
  1747. match_character_any(ctx);
  1748. }
  1749. }
  1750. }
  1751. }
  1752. return TRUE;
  1753. }
  1754. return FALSE;
  1755. }
  1756. static bool_t match_footer_start(context_t *ctx) {
  1757. return match_string(ctx, "%%");
  1758. }
  1759. static node_t *parse_expression(context_t *ctx, node_t *rule);
  1760. static node_t *parse_primary(context_t *ctx, node_t *rule) {
  1761. const size_t p = ctx->bufcur;
  1762. const size_t l = ctx->linenum;
  1763. const size_t m = column_number(ctx);
  1764. node_t *n_p = NULL;
  1765. if (match_identifier(ctx)) {
  1766. const size_t q = ctx->bufcur;
  1767. size_t r = VOID_VALUE, s = VOID_VALUE;
  1768. match_spaces(ctx);
  1769. if (match_character(ctx, ':')) {
  1770. match_spaces(ctx);
  1771. r = ctx->bufcur;
  1772. if (!match_identifier(ctx)) goto EXCEPTION;
  1773. s = ctx->bufcur;
  1774. match_spaces(ctx);
  1775. }
  1776. if (match_string(ctx, "<-")) goto EXCEPTION;
  1777. n_p = create_node(NODE_REFERENCE);
  1778. if (r == VOID_VALUE) {
  1779. assert(q >= p);
  1780. n_p->data.reference.var = NULL;
  1781. n_p->data.reference.index = VOID_VALUE;
  1782. n_p->data.reference.name = strndup_e(ctx->buffer.buf + p, q - p);
  1783. }
  1784. else {
  1785. assert(s != VOID_VALUE); /* s should have a valid value when r has a valid value */
  1786. assert(q >= p);
  1787. n_p->data.reference.var = strndup_e(ctx->buffer.buf + p, q - p);
  1788. if (n_p->data.reference.var[0] == '_') {
  1789. print_error("%s:" FMT_LU ":" FMT_LU ": Leading underscore in variable name '%s'\n",
  1790. ctx->iname, (ulong_t)(l + 1), (ulong_t)(m + 1), n_p->data.reference.var);
  1791. ctx->errnum++;
  1792. }
  1793. {
  1794. size_t i;
  1795. for (i = 0; i < rule->data.rule.vars.len; i++) {
  1796. assert(rule->data.rule.vars.buf[i]->type == NODE_REFERENCE);
  1797. if (strcmp(n_p->data.reference.var, rule->data.rule.vars.buf[i]->data.reference.var) == 0) break;
  1798. }
  1799. if (i == rule->data.rule.vars.len) node_const_array__add(&rule->data.rule.vars, n_p);
  1800. n_p->data.reference.index = i;
  1801. }
  1802. assert(s >= r);
  1803. n_p->data.reference.name = strndup_e(ctx->buffer.buf + r, s - r);
  1804. }
  1805. n_p->data.reference.line = l;
  1806. n_p->data.reference.col = m;
  1807. }
  1808. else if (match_character(ctx, '(')) {
  1809. match_spaces(ctx);
  1810. n_p = parse_expression(ctx, rule);
  1811. if (n_p == NULL) goto EXCEPTION;
  1812. if (!match_character(ctx, ')')) goto EXCEPTION;
  1813. match_spaces(ctx);
  1814. }
  1815. else if (match_character(ctx, '<')) {
  1816. match_spaces(ctx);
  1817. n_p = create_node(NODE_CAPTURE);
  1818. n_p->data.capture.index = rule->data.rule.capts.len;
  1819. node_const_array__add(&rule->data.rule.capts, n_p);
  1820. n_p->data.capture.expr = parse_expression(ctx, rule);
  1821. if (n_p->data.capture.expr == NULL || !match_character(ctx, '>')) {
  1822. rule->data.rule.capts.len = n_p->data.capture.index;
  1823. goto EXCEPTION;
  1824. }
  1825. match_spaces(ctx);
  1826. }
  1827. else if (match_character(ctx, '$')) {
  1828. size_t p;
  1829. match_spaces(ctx);
  1830. p = ctx->bufcur;
  1831. if (match_number(ctx)) {
  1832. const size_t q = ctx->bufcur;
  1833. char *s;
  1834. match_spaces(ctx);
  1835. n_p = create_node(NODE_EXPAND);
  1836. assert(q >= p);
  1837. s = strndup_e(ctx->buffer.buf + p, q - p);
  1838. n_p->data.expand.index = string_to_size_t(s);
  1839. if (n_p->data.expand.index == VOID_VALUE) {
  1840. print_error("%s:" FMT_LU ":" FMT_LU ": Invalid unsigned number '%s'\n", ctx->iname, (ulong_t)(l + 1), (ulong_t)(m + 1), s);
  1841. ctx->errnum++;
  1842. }
  1843. else if (n_p->data.expand.index == 0) {
  1844. print_error("%s:" FMT_LU ":" FMT_LU ": 0 not allowed\n", ctx->iname, (ulong_t)(l + 1), (ulong_t)(m + 1));
  1845. ctx->errnum++;
  1846. }
  1847. else if (s[0] == '0') {
  1848. print_error("%s:" FMT_LU ":" FMT_LU ": 0-prefixed number not allowed\n", ctx->iname, (ulong_t)(l + 1), (ulong_t)(m + 1));
  1849. ctx->errnum++;
  1850. n_p->data.expand.index = 0;
  1851. }
  1852. free(s);
  1853. if (n_p->data.expand.index > 0 && n_p->data.expand.index != VOID_VALUE) {
  1854. n_p->data.expand.index--;
  1855. n_p->data.expand.line = l;
  1856. n_p->data.expand.col = m;
  1857. }
  1858. }
  1859. else {
  1860. goto EXCEPTION;
  1861. }
  1862. }
  1863. else if (match_character(ctx, '.')) {
  1864. match_spaces(ctx);
  1865. n_p = create_node(NODE_CHARCLASS);
  1866. n_p->data.charclass.value = NULL;
  1867. if (!ctx->ascii) {
  1868. ctx->flags |= CODE_FLAG__UTF8_CHARCLASS_USED;
  1869. }
  1870. }
  1871. else if (match_character_class(ctx)) {
  1872. const size_t q = ctx->bufcur;
  1873. match_spaces(ctx);
  1874. n_p = create_node(NODE_CHARCLASS);
  1875. n_p->data.charclass.value = strndup_e(ctx->buffer.buf + p + 1, q - p - 2);
  1876. if (!unescape_string(n_p->data.charclass.value, TRUE)) {
  1877. print_error("%s:" FMT_LU ":" FMT_LU ": Illegal escape sequence\n", ctx->iname, (ulong_t)(l + 1), (ulong_t)(m + 1));
  1878. ctx->errnum++;
  1879. }
  1880. if (!ctx->ascii && !is_valid_utf8_string(n_p->data.charclass.value)) {
  1881. print_error("%s:" FMT_LU ":" FMT_LU ": Invalid UTF-8 string\n", ctx->iname, (ulong_t)(l + 1), (ulong_t)(m + 1));
  1882. ctx->errnum++;
  1883. }
  1884. if (!ctx->ascii && n_p->data.charclass.value[0] != '\0') {
  1885. ctx->flags |= CODE_FLAG__UTF8_CHARCLASS_USED;
  1886. }
  1887. }
  1888. else if (match_quotation_single(ctx) || match_quotation_double(ctx)) {
  1889. const size_t q = ctx->bufcur;
  1890. match_spaces(ctx);
  1891. n_p = create_node(NODE_STRING);
  1892. n_p->data.string.value = strndup_e(ctx->buffer.buf + p + 1, q - p - 2);
  1893. if (!unescape_string(n_p->data.string.value, FALSE)) {
  1894. print_error("%s:" FMT_LU ":" FMT_LU ": Illegal escape sequence\n", ctx->iname, (ulong_t)(l + 1), (ulong_t)(m + 1));
  1895. ctx->errnum++;
  1896. }
  1897. if (!ctx->ascii && !is_valid_utf8_string(n_p->data.string.value)) {
  1898. print_error("%s:" FMT_LU ":" FMT_LU ": Invalid UTF-8 string\n", ctx->iname, (ulong_t)(l + 1), (ulong_t)(m + 1));
  1899. ctx->errnum++;
  1900. }
  1901. }
  1902. else if (match_code_block(ctx)) {
  1903. const size_t q = ctx->bufcur;
  1904. match_spaces(ctx);
  1905. n_p = create_node(NODE_ACTION);
  1906. n_p->data.action.value = strndup_e(ctx->buffer.buf + p + 1, q - p - 2);
  1907. n_p->data.action.index = rule->data.rule.codes.len;
  1908. node_const_array__add(&rule->data.rule.codes, n_p);
  1909. }
  1910. else {
  1911. goto EXCEPTION;
  1912. }
  1913. return n_p;
  1914. EXCEPTION:;
  1915. destroy_node(n_p);
  1916. ctx->bufcur = p;
  1917. ctx->linenum = l;
  1918. ctx->linepos = ctx->bufpos + p - m;
  1919. return NULL;
  1920. }
  1921. static node_t *parse_term(context_t *ctx, node_t *rule) {
  1922. const size_t p = ctx->bufcur;
  1923. const size_t l = ctx->linenum;
  1924. const size_t m = column_number(ctx);
  1925. node_t *n_p = NULL;
  1926. node_t *n_q = NULL;
  1927. node_t *n_r = NULL;
  1928. node_t *n_t = NULL;
  1929. const char t = match_character(ctx, '&') ? '&' : match_character(ctx, '!') ? '!' : '\0';
  1930. if (t) match_spaces(ctx);
  1931. n_p = parse_primary(ctx, rule);
  1932. if (n_p == NULL) goto EXCEPTION;
  1933. if (match_character(ctx, '*')) {
  1934. match_spaces(ctx);
  1935. n_q = create_node(NODE_QUANTITY);
  1936. n_q->data.quantity.min = 0;
  1937. n_q->data.quantity.max = -1;
  1938. n_q->data.quantity.expr = n_p;
  1939. }
  1940. else if (match_character(ctx, '+')) {
  1941. match_spaces(ctx);
  1942. n_q = create_node(NODE_QUANTITY);
  1943. n_q->data.quantity.min = 1;
  1944. n_q->data.quantity.max = -1;
  1945. n_q->data.quantity.expr = n_p;
  1946. }
  1947. else if (match_character(ctx, '?')) {
  1948. match_spaces(ctx);
  1949. n_q = create_node(NODE_QUANTITY);
  1950. n_q->data.quantity.min = 0;
  1951. n_q->data.quantity.max = 1;
  1952. n_q->data.quantity.expr = n_p;
  1953. }
  1954. else {
  1955. n_q = n_p;
  1956. }
  1957. switch (t) {
  1958. case '&':
  1959. n_r = create_node(NODE_PREDICATE);
  1960. n_r->data.predicate.neg = FALSE;
  1961. n_r->data.predicate.expr = n_q;
  1962. break;
  1963. case '!':
  1964. n_r = create_node(NODE_PREDICATE);
  1965. n_r->data.predicate.neg = TRUE;
  1966. n_r->data.predicate.expr = n_q;
  1967. break;
  1968. default:
  1969. n_r = n_q;
  1970. }
  1971. if (match_character(ctx, '~')) {
  1972. size_t p;
  1973. match_spaces(ctx);
  1974. p = ctx->bufcur;
  1975. if (match_code_block(ctx)) {
  1976. const size_t q = ctx->bufcur;
  1977. match_spaces(ctx);
  1978. n_t = create_node(NODE_ERROR);
  1979. n_t->data.error.expr = n_r;
  1980. n_t->data.error.value = strndup_e(ctx->buffer.buf + p + 1, q - p - 2);
  1981. n_t->data.error.index = rule->data.rule.codes.len;
  1982. node_const_array__add(&rule->data.rule.codes, n_t);
  1983. }
  1984. else {
  1985. goto EXCEPTION;
  1986. }
  1987. }
  1988. else {
  1989. n_t = n_r;
  1990. }
  1991. return n_t;
  1992. EXCEPTION:;
  1993. destroy_node(n_r);
  1994. ctx->bufcur = p;
  1995. ctx->linenum = l;
  1996. ctx->linepos = ctx->bufpos + p - m;
  1997. return NULL;
  1998. }
  1999. static node_t *parse_sequence(context_t *ctx, node_t *rule) {
  2000. const size_t p = ctx->bufcur;
  2001. const size_t l = ctx->linenum;
  2002. const size_t m = column_number(ctx);
  2003. node_array_t *a_t = NULL;
  2004. node_t *n_t = NULL;
  2005. node_t *n_u = NULL;
  2006. node_t *n_s = NULL;
  2007. n_t = parse_term(ctx, rule);
  2008. if (n_t == NULL) goto EXCEPTION;
  2009. n_u = parse_term(ctx, rule);
  2010. if (n_u != NULL) {
  2011. n_s = create_node(NODE_SEQUENCE);
  2012. a_t = &n_s->data.sequence.nodes;
  2013. node_array__add(a_t, n_t);
  2014. node_array__add(a_t, n_u);
  2015. while ((n_t = parse_term(ctx, rule)) != NULL) {
  2016. node_array__add(a_t, n_t);
  2017. }
  2018. }
  2019. else {
  2020. n_s = n_t;
  2021. }
  2022. return n_s;
  2023. EXCEPTION:;
  2024. ctx->bufcur = p;
  2025. ctx->linenum = l;
  2026. ctx->linepos = ctx->bufpos + p - m;
  2027. return NULL;
  2028. }
  2029. static node_t *parse_expression(context_t *ctx, node_t *rule) {
  2030. const size_t p = ctx->bufcur;
  2031. const size_t l = ctx->linenum;
  2032. const size_t m = column_number(ctx);
  2033. size_t q;
  2034. node_array_t *a_s = NULL;
  2035. node_t *n_s = NULL;
  2036. node_t *n_e = NULL;
  2037. n_s = parse_sequence(ctx, rule);
  2038. if (n_s == NULL) goto EXCEPTION;
  2039. q = ctx->bufcur;
  2040. if (match_character(ctx, '/')) {
  2041. ctx->bufcur = q;
  2042. n_e = create_node(NODE_ALTERNATE);
  2043. a_s = &n_e->data.alternate.nodes;
  2044. node_array__add(a_s, n_s);
  2045. while (match_character(ctx, '/')) {
  2046. match_spaces(ctx);
  2047. n_s = parse_sequence(ctx, rule);
  2048. if (n_s == NULL) goto EXCEPTION;
  2049. node_array__add(a_s, n_s);
  2050. }
  2051. }
  2052. else {
  2053. n_e = n_s;
  2054. }
  2055. return n_e;
  2056. EXCEPTION:;
  2057. destroy_node(n_e);
  2058. ctx->bufcur = p;
  2059. ctx->linenum = l;
  2060. ctx->linepos = ctx->bufpos + p - m;
  2061. return NULL;
  2062. }
  2063. static node_t *parse_rule(context_t *ctx) {
  2064. const size_t p = ctx->bufcur;
  2065. const size_t l = ctx->linenum;
  2066. const size_t m = column_number(ctx);
  2067. size_t q;
  2068. node_t *n_r = NULL;
  2069. if (!match_identifier(ctx)) goto EXCEPTION;
  2070. q = ctx->bufcur;
  2071. match_spaces(ctx);
  2072. if (!match_string(ctx, "<-")) goto EXCEPTION;
  2073. match_spaces(ctx);
  2074. n_r = create_node(NODE_RULE);
  2075. n_r->data.rule.expr = parse_expression(ctx, n_r);
  2076. if (n_r->data.rule.expr == NULL) goto EXCEPTION;
  2077. assert(q >= p);
  2078. n_r->data.rule.name = strndup_e(ctx->buffer.buf + p, q - p);
  2079. n_r->data.rule.line = l;
  2080. n_r->data.rule.col = m;
  2081. return n_r;
  2082. EXCEPTION:;
  2083. destroy_node(n_r);
  2084. ctx->bufcur = p;
  2085. ctx->linenum = l;
  2086. ctx->linepos = ctx->bufpos + p - m;
  2087. return NULL;
  2088. }
  2089. static const char *get_value_type(context_t *ctx) {
  2090. return (ctx->vtype && ctx->vtype[0]) ? ctx->vtype : "int";
  2091. }
  2092. static const char *get_auxil_type(context_t *ctx) {
  2093. return (ctx->atype && ctx->atype[0]) ? ctx->atype : "void *";
  2094. }
  2095. static const char *get_prefix(context_t *ctx) {
  2096. return (ctx->prefix && ctx->prefix[0]) ? ctx->prefix : "pcc";
  2097. }
  2098. static void dump_options(context_t *ctx) {
  2099. fprintf(stdout, "value_type: '%s'\n", get_value_type(ctx));
  2100. fprintf(stdout, "auxil_type: '%s'\n", get_auxil_type(ctx));
  2101. fprintf(stdout, "prefix: '%s'\n", get_prefix(ctx));
  2102. }
  2103. static bool_t parse_directive_include_(context_t *ctx, const char *name, char_array_t *output1, char_array_t *output2) {
  2104. const size_t l = ctx->linenum;
  2105. const size_t m = column_number(ctx);
  2106. if (!match_string(ctx, name)) return FALSE;
  2107. match_spaces(ctx);
  2108. {
  2109. const size_t p = ctx->bufcur;
  2110. if (match_code_block(ctx)) {
  2111. const size_t q = ctx->bufcur;
  2112. match_spaces(ctx);
  2113. if (output1 != NULL) {
  2114. char_array__append(output1, ctx->buffer.buf + p + 1, q - p - 2);
  2115. char_array__add(output1, '\n');
  2116. }
  2117. if (output2 != NULL) {
  2118. char_array__append(output2, ctx->buffer.buf + p + 1, q - p - 2);
  2119. char_array__add(output2, '\n');
  2120. }
  2121. }
  2122. else {
  2123. print_error("%s:" FMT_LU ":" FMT_LU ": Illegal %s syntax\n", ctx->iname, (ulong_t)(l + 1), (ulong_t)(m + 1), name);
  2124. ctx->errnum++;
  2125. }
  2126. }
  2127. return TRUE;
  2128. }
  2129. static bool_t parse_directive_string_(context_t *ctx, const char *name, char **output, string_flag_t mode) {
  2130. const size_t l = ctx->linenum;
  2131. const size_t m = column_number(ctx);
  2132. if (!match_string(ctx, name)) return FALSE;
  2133. match_spaces(ctx);
  2134. {
  2135. char *s = NULL;
  2136. const size_t p = ctx->bufcur;
  2137. const size_t lv = ctx->linenum;
  2138. const size_t mv = column_number(ctx);
  2139. size_t q;
  2140. if (match_quotation_single(ctx) || match_quotation_double(ctx)) {
  2141. q = ctx->bufcur;
  2142. match_spaces(ctx);
  2143. s = strndup_e(ctx->buffer.buf + p + 1, q - p - 2);
  2144. if (!unescape_string(s, FALSE)) {
  2145. print_error("%s:" FMT_LU ":" FMT_LU ": Illegal escape sequence\n", ctx->iname, (ulong_t)(lv + 1), (ulong_t)(mv + 1));
  2146. ctx->errnum++;
  2147. }
  2148. }
  2149. else {
  2150. print_error("%s:" FMT_LU ":" FMT_LU ": Illegal %s syntax\n", ctx->iname, (ulong_t)(l + 1), (ulong_t)(m + 1), name);
  2151. ctx->errnum++;
  2152. }
  2153. if (s != NULL) {
  2154. string_flag_t f = STRING_FLAG__NONE;
  2155. bool_t b = TRUE;
  2156. remove_heading_blank(s);
  2157. remove_trailing_blank(s);
  2158. assert((mode & ~7) == 0);
  2159. if ((mode & STRING_FLAG__NOTEMPTY) && !is_filled_string(s)) {
  2160. print_error("%s:" FMT_LU ":" FMT_LU ": Empty string\n", ctx->iname, (ulong_t)(lv + 1), (ulong_t)(mv + 1));
  2161. ctx->errnum++;
  2162. f |= STRING_FLAG__NOTEMPTY;
  2163. }
  2164. if ((mode & STRING_FLAG__NOTVOID) && strcmp(s, "void") == 0) {
  2165. print_error("%s:" FMT_LU ":" FMT_LU ": 'void' not allowed\n", ctx->iname, (ulong_t)(lv + 1), (ulong_t)(mv + 1));
  2166. ctx->errnum++;
  2167. f |= STRING_FLAG__NOTVOID;
  2168. }
  2169. if ((mode & STRING_FLAG__IDENTIFIER) && !is_identifier_string(s)) {
  2170. if (!(f & STRING_FLAG__NOTEMPTY)) {
  2171. print_error("%s:" FMT_LU ":" FMT_LU ": Invalid identifier\n", ctx->iname, (ulong_t)(lv + 1), (ulong_t)(mv + 1));
  2172. ctx->errnum++;
  2173. }
  2174. f |= STRING_FLAG__IDENTIFIER;
  2175. }
  2176. if (*output != NULL) {
  2177. print_error("%s:" FMT_LU ":" FMT_LU ": Multiple %s definition\n", ctx->iname, (ulong_t)(l + 1), (ulong_t)(m + 1), name);
  2178. ctx->errnum++;
  2179. b = FALSE;
  2180. }
  2181. if (f == STRING_FLAG__NONE && b) {
  2182. *output = s;
  2183. }
  2184. else {
  2185. free(s); s = NULL;
  2186. }
  2187. }
  2188. }
  2189. return TRUE;
  2190. }
  2191. static bool_t parse(context_t *ctx) {
  2192. {
  2193. bool_t b = TRUE;
  2194. match_spaces(ctx);
  2195. for (;;) {
  2196. size_t p, l, m;
  2197. if (match_eof(ctx) || match_footer_start(ctx)) break;
  2198. p = ctx->bufcur;
  2199. l = ctx->linenum;
  2200. m = column_number(ctx);
  2201. if (
  2202. parse_directive_include_(ctx, "%earlysource", &ctx->esource, NULL) ||
  2203. parse_directive_include_(ctx, "%earlyheader", &ctx->eheader, NULL) ||
  2204. parse_directive_include_(ctx, "%earlycommon", &ctx->esource, &ctx->eheader) ||
  2205. parse_directive_include_(ctx, "%source", &ctx->source, NULL) ||
  2206. parse_directive_include_(ctx, "%header", &ctx->header, NULL) ||
  2207. parse_directive_include_(ctx, "%common", &ctx->source, &ctx->header) ||
  2208. parse_directive_string_(ctx, "%value", &ctx->vtype, STRING_FLAG__NOTEMPTY | STRING_FLAG__NOTVOID) ||
  2209. parse_directive_string_(ctx, "%auxil", &ctx->atype, STRING_FLAG__NOTEMPTY | STRING_FLAG__NOTVOID) ||
  2210. parse_directive_string_(ctx, "%prefix", &ctx->prefix, STRING_FLAG__NOTEMPTY | STRING_FLAG__IDENTIFIER)
  2211. ) {
  2212. b = TRUE;
  2213. }
  2214. else if (match_character(ctx, '%')) {
  2215. print_error("%s:" FMT_LU ":" FMT_LU ": Invalid directive\n", ctx->iname, (ulong_t)(l + 1), (ulong_t)(m + 1));
  2216. ctx->errnum++;
  2217. match_identifier(ctx);
  2218. match_spaces(ctx);
  2219. b = TRUE;
  2220. }
  2221. else {
  2222. node_t *const n_r = parse_rule(ctx);
  2223. if (n_r == NULL) {
  2224. if (b) {
  2225. print_error("%s:" FMT_LU ":" FMT_LU ": Illegal rule syntax\n", ctx->iname, (ulong_t)(l + 1), (ulong_t)(m + 1));
  2226. ctx->errnum++;
  2227. b = FALSE;
  2228. }
  2229. ctx->linenum = l;
  2230. ctx->linepos = ctx->bufpos + p - m;
  2231. if (!match_identifier(ctx) && !match_spaces(ctx)) match_character_any(ctx);
  2232. continue;
  2233. }
  2234. node_array__add(&ctx->rules, n_r);
  2235. b = TRUE;
  2236. }
  2237. commit_buffer(ctx);
  2238. }
  2239. commit_buffer(ctx);
  2240. }
  2241. {
  2242. size_t i;
  2243. make_rulehash(ctx);
  2244. for (i = 0; i < ctx->rules.len; i++) {
  2245. link_references(ctx, ctx->rules.buf[i]->data.rule.expr);
  2246. }
  2247. for (i = 1; i < ctx->rules.len; i++) {
  2248. if (ctx->rules.buf[i]->data.rule.ref == 0) {
  2249. print_error("%s:" FMT_LU ":" FMT_LU ": Never used rule '%s'\n",
  2250. ctx->iname,
  2251. (ulong_t)(ctx->rules.buf[i]->data.rule.line + 1), (ulong_t)(ctx->rules.buf[i]->data.rule.col + 1),
  2252. ctx->rules.buf[i]->data.rule.name);
  2253. ctx->errnum++;
  2254. }
  2255. else if (ctx->rules.buf[i]->data.rule.ref < 0) {
  2256. print_error("%s:" FMT_LU ":" FMT_LU ": Multiple definition of rule '%s'\n",
  2257. ctx->iname,
  2258. (ulong_t)(ctx->rules.buf[i]->data.rule.line + 1), (ulong_t)(ctx->rules.buf[i]->data.rule.col + 1),
  2259. ctx->rules.buf[i]->data.rule.name);
  2260. ctx->errnum++;
  2261. }
  2262. }
  2263. }
  2264. {
  2265. size_t i;
  2266. for (i = 0; i < ctx->rules.len; i++) {
  2267. verify_variables(ctx, ctx->rules.buf[i]->data.rule.expr, NULL);
  2268. verify_captures(ctx, ctx->rules.buf[i]->data.rule.expr, NULL);
  2269. }
  2270. }
  2271. if (ctx->debug) {
  2272. size_t i;
  2273. for (i = 0; i < ctx->rules.len; i++) {
  2274. dump_node(ctx, ctx->rules.buf[i], 0);
  2275. }
  2276. dump_options(ctx);
  2277. }
  2278. return (ctx->errnum == 0) ? TRUE : FALSE;
  2279. }
  2280. static code_reach_t generate_matching_string_code(generate_t *gen, const char *value, int onfail, size_t indent, bool_t bare) {
  2281. const size_t n = (value != NULL) ? strlen(value) : 0;
  2282. if (bare) {}
  2283. fputs_e("/* generated by generate_matching_string_code() */\n", gen->stream);
  2284. if (n > 0) {
  2285. char s[5];
  2286. if (n > 1) {
  2287. size_t i;
  2288. write_characters(gen->stream, ' ', indent);
  2289. fputs_e("if (\n", gen->stream);
  2290. write_characters(gen->stream, ' ', indent + 4);
  2291. fprintf_e(gen->stream, "pcc_refill_buffer(ctx, " FMT_LU ") < " FMT_LU " ||\n", (ulong_t)n, (ulong_t)n);
  2292. for (i = 0; i < n - 1; i++) {
  2293. write_characters(gen->stream, ' ', indent + 4);
  2294. fprintf_e(gen->stream, "(ctx->buffer.buf + ctx->cur)[" FMT_LU "] != '%s' ||\n", (ulong_t)i, escape_character(value[i], &s));
  2295. }
  2296. write_characters(gen->stream, ' ', indent + 4);
  2297. fprintf_e(gen->stream, "(ctx->buffer.buf + ctx->cur)[" FMT_LU "] != '%s'\n", (ulong_t)i, escape_character(value[i], &s));
  2298. write_characters(gen->stream, ' ', indent);
  2299. fprintf_e(gen->stream, ") goto L%04d;\n", onfail);
  2300. write_characters(gen->stream, ' ', indent);
  2301. fprintf_e(gen->stream, "ctx->cur += " FMT_LU ";\n", (ulong_t)n);
  2302. return CODE_REACH__BOTH;
  2303. }
  2304. else {
  2305. write_characters(gen->stream, ' ', indent);
  2306. fputs_e("if (\n", gen->stream);
  2307. write_characters(gen->stream, ' ', indent + 4);
  2308. fputs_e("pcc_refill_buffer(ctx, 1) < 1 ||\n", gen->stream);
  2309. write_characters(gen->stream, ' ', indent + 4);
  2310. fprintf_e(gen->stream, "ctx->buffer.buf[ctx->cur] != '%s'\n", escape_character(value[0], &s));
  2311. write_characters(gen->stream, ' ', indent);
  2312. fprintf_e(gen->stream, ") goto L%04d;\n", onfail);
  2313. write_characters(gen->stream, ' ', indent);
  2314. fputs_e("ctx->cur++;\n", gen->stream);
  2315. return CODE_REACH__BOTH;
  2316. }
  2317. }
  2318. else {
  2319. /* no code to generate */
  2320. return CODE_REACH__ALWAYS_SUCCEED;
  2321. }
  2322. }
  2323. static code_reach_t generate_matching_charclass_code(generate_t *gen, const char *value, int onfail, size_t indent, bool_t bare) {
  2324. assert(gen->ascii);
  2325. if (value != NULL) {
  2326. const size_t n = strlen(value);
  2327. if (n > 0) {
  2328. char s[5], t[5];
  2329. if (n > 1) {
  2330. const bool_t a = (value[0] == '^') ? TRUE : FALSE;
  2331. size_t i = a ? 1 : 0;
  2332. if (i + 1 == n) { /* fulfilled only if a == TRUE */
  2333. write_characters(gen->stream, ' ', indent);
  2334. fputs_e("if (\n", gen->stream);
  2335. write_characters(gen->stream, ' ', indent + 4);
  2336. fputs_e("pcc_refill_buffer(ctx, 1) < 1 ||\n", gen->stream);
  2337. write_characters(gen->stream, ' ', indent + 4);
  2338. fprintf_e(gen->stream, "ctx->buffer.buf[ctx->cur] == '%s'\n", escape_character(value[i], &s));
  2339. write_characters(gen->stream, ' ', indent);
  2340. fprintf_e(gen->stream, ") goto L%04d;\n", onfail);
  2341. write_characters(gen->stream, ' ', indent);
  2342. fputs_e("ctx->cur++;\n", gen->stream);
  2343. return CODE_REACH__BOTH;
  2344. }
  2345. else {
  2346. if (!bare) {
  2347. write_characters(gen->stream, ' ', indent);
  2348. fputs_e("{\n", gen->stream);
  2349. indent += 4;
  2350. }
  2351. write_characters(gen->stream, ' ', indent);
  2352. fputs_e("char c;\n", gen->stream);
  2353. write_characters(gen->stream, ' ', indent);
  2354. fprintf_e(gen->stream, "if (pcc_refill_buffer(ctx, 1) < 1) goto L%04d;\n", onfail);
  2355. write_characters(gen->stream, ' ', indent);
  2356. fputs_e("c = ctx->buffer.buf[ctx->cur];\n", gen->stream);
  2357. if (i + 3 == n && value[i] != '\\' && value[i + 1] == '-') {
  2358. write_characters(gen->stream, ' ', indent);
  2359. fprintf_e(gen->stream,
  2360. a ? "if (c >= '%s' && c <= '%s') goto L%04d;\n"
  2361. : "if (!(c >= '%s' && c <= '%s')) goto L%04d;\n",
  2362. escape_character(value[i], &s), escape_character(value[i + 2], &t), onfail);
  2363. }
  2364. else {
  2365. write_characters(gen->stream, ' ', indent);
  2366. fputs_e(a ? "if (\n" : "if (!(\n", gen->stream);
  2367. for (; i < n; i++) {
  2368. write_characters(gen->stream, ' ', indent + 4);
  2369. if (value[i] == '\\' && i + 1 < n) i++;
  2370. if (i + 2 < n && value[i + 1] == '-') {
  2371. fprintf_e(gen->stream, "(c >= '%s' && c <= '%s')%s\n",
  2372. escape_character(value[i], &s), escape_character(value[i + 2], &t), (i + 3 == n) ? "" : " ||");
  2373. i += 2;
  2374. }
  2375. else {
  2376. fprintf_e(gen->stream, "c == '%s'%s\n",
  2377. escape_character(value[i], &s), (i + 1 == n) ? "" : " ||");
  2378. }
  2379. }
  2380. write_characters(gen->stream, ' ', indent);
  2381. fprintf_e(gen->stream, a ? ") goto L%04d;\n" : ")) goto L%04d;\n", onfail);
  2382. }
  2383. write_characters(gen->stream, ' ', indent);
  2384. fputs_e("ctx->cur++;\n", gen->stream);
  2385. if (!bare) {
  2386. indent -= 4;
  2387. write_characters(gen->stream, ' ', indent);
  2388. fputs_e("}\n", gen->stream);
  2389. }
  2390. return CODE_REACH__BOTH;
  2391. }
  2392. }
  2393. else {
  2394. write_characters(gen->stream, ' ', indent);
  2395. fputs_e("if (\n", gen->stream);
  2396. write_characters(gen->stream, ' ', indent + 4);
  2397. fputs_e("pcc_refill_buffer(ctx, 1) < 1 ||\n", gen->stream);
  2398. write_characters(gen->stream, ' ', indent + 4);
  2399. fprintf_e(gen->stream, "ctx->buffer.buf[ctx->cur] != '%s'\n", escape_character(value[0], &s));
  2400. write_characters(gen->stream, ' ', indent);
  2401. fprintf_e(gen->stream, ") goto L%04d;\n", onfail);
  2402. write_characters(gen->stream, ' ', indent);
  2403. fputs_e("ctx->cur++;\n", gen->stream);
  2404. return CODE_REACH__BOTH;
  2405. }
  2406. }
  2407. else {
  2408. write_characters(gen->stream, ' ', indent);
  2409. fprintf_e(gen->stream, "goto L%04d;\n", onfail);
  2410. return CODE_REACH__ALWAYS_FAIL;
  2411. }
  2412. }
  2413. else {
  2414. write_characters(gen->stream, ' ', indent);
  2415. fprintf_e(gen->stream, "if (pcc_refill_buffer(ctx, 1) < 1) goto L%04d;\n", onfail);
  2416. write_characters(gen->stream, ' ', indent);
  2417. fputs_e("ctx->cur++;\n", gen->stream);
  2418. return CODE_REACH__BOTH;
  2419. }
  2420. }
  2421. static code_reach_t generate_matching_utf8_charclass_code(generate_t *gen, const char *value, int onfail, size_t indent, bool_t bare) {
  2422. const size_t n = (value != NULL) ? strlen(value) : 0;
  2423. if (value == NULL || n > 0) {
  2424. const bool_t a = (n > 0 && value[0] == '^') ? TRUE : FALSE;
  2425. size_t i = a ? 1 : 0;
  2426. if (!bare) {
  2427. write_characters(gen->stream, ' ', indent);
  2428. fputs_e("{\n", gen->stream);
  2429. indent += 4;
  2430. }
  2431. write_characters(gen->stream, ' ', indent);
  2432. fputs_e("int u;\n", gen->stream);
  2433. write_characters(gen->stream, ' ', indent);
  2434. fputs_e("const size_t n = pcc_get_char_as_utf32(ctx, &u);\n", gen->stream);
  2435. write_characters(gen->stream, ' ', indent);
  2436. fprintf_e(gen->stream, "if (n == 0) goto L%04d;\n", onfail);
  2437. if (value != NULL && !(a && n == 1)) { /* not '.' or '[^]' */
  2438. int u0 = 0;
  2439. bool_t r = FALSE;
  2440. write_characters(gen->stream, ' ', indent);
  2441. fputs_e(a ? "if (\n" : "if (!(\n", gen->stream);
  2442. while (i < n) {
  2443. int u = 0;
  2444. if (value[i] == '\\' && i + 1 < n) i++;
  2445. i += utf8_to_utf32(value + i, &u);
  2446. if (r) { /* character range */
  2447. write_characters(gen->stream, ' ', indent + 4);
  2448. fprintf_e(gen->stream, "(u >= 0x%06x && u <= 0x%06x)%s\n", u0, u, (i < n) ? " ||" : "");
  2449. u0 = 0;
  2450. r = FALSE;
  2451. }
  2452. else if (value[i] != '-') { /* single character */
  2453. write_characters(gen->stream, ' ', indent + 4);
  2454. fprintf_e(gen->stream, "u == 0x%06x%s\n", u, (i < n) ? " ||" : "");
  2455. u0 = 0;
  2456. r = FALSE;
  2457. }
  2458. else {
  2459. assert(value[i] == '-');
  2460. i++;
  2461. u0 = u;
  2462. r = TRUE;
  2463. }
  2464. }
  2465. write_characters(gen->stream, ' ', indent);
  2466. fprintf_e(gen->stream, a ? ") goto L%04d;\n" : ")) goto L%04d;\n", onfail);
  2467. }
  2468. write_characters(gen->stream, ' ', indent);
  2469. fputs_e("ctx->cur += n;\n", gen->stream);
  2470. if (!bare) {
  2471. indent -= 4;
  2472. write_characters(gen->stream, ' ', indent);
  2473. fputs_e("}\n", gen->stream);
  2474. }
  2475. return CODE_REACH__BOTH;
  2476. }
  2477. else {
  2478. write_characters(gen->stream, ' ', indent);
  2479. fprintf_e(gen->stream, "goto L%04d;\n", onfail);
  2480. return CODE_REACH__ALWAYS_FAIL;
  2481. }
  2482. }
  2483. static code_reach_t generate_code(generate_t *gen, const node_t *node, int onfail, size_t indent, bool_t bare);
  2484. static code_reach_t generate_quantifying_code(generate_t *gen, const node_t *expr, int min, int max, int onfail, size_t indent, bool_t bare) {
  2485. fputs_e("/* generated by generate_quantifying_code() */\n", gen->stream);
  2486. if (max > 1 || max < 0) {
  2487. code_reach_t r;
  2488. if (!bare) {
  2489. write_characters(gen->stream, ' ', indent);
  2490. fputs_e("{\n", gen->stream);
  2491. indent += 4;
  2492. }
  2493. if (min > 0) {
  2494. write_characters(gen->stream, ' ', indent);
  2495. fputs_e("const size_t p0 = ctx->cur;\n", gen->stream);
  2496. write_characters(gen->stream, ' ', indent);
  2497. fputs_e("const size_t n0 = chunk->thunks.len;\n", gen->stream);
  2498. }
  2499. write_characters(gen->stream, ' ', indent);
  2500. fputs_e("int i;\n", gen->stream);
  2501. write_characters(gen->stream, ' ', indent);
  2502. if (max < 0)
  2503. fputs_e("for (i = 0;; i++) {\n", gen->stream);
  2504. else
  2505. fprintf_e(gen->stream, "for (i = 0; i < %d; i++) {\n", max);
  2506. write_characters(gen->stream, ' ', indent + 4);
  2507. fputs_e("const size_t p = ctx->cur;\n", gen->stream);
  2508. write_characters(gen->stream, ' ', indent + 4);
  2509. fputs_e("const size_t n = chunk->thunks.len;\n", gen->stream);
  2510. {
  2511. const int l = ++gen->label;
  2512. r = generate_code(gen, expr, l, indent + 4, FALSE);
  2513. write_characters(gen->stream, ' ', indent + 4);
  2514. fputs_e("if (ctx->cur == p) break;\n", gen->stream);
  2515. if (r != CODE_REACH__ALWAYS_SUCCEED) {
  2516. write_characters(gen->stream, ' ', indent + 4);
  2517. fputs_e("continue;\n", gen->stream);
  2518. write_characters(gen->stream, ' ', indent);
  2519. fprintf_e(gen->stream, "L%04d:;\n", l);
  2520. write_characters(gen->stream, ' ', indent + 4);
  2521. fputs_e("ctx->cur = p;\n", gen->stream);
  2522. write_characters(gen->stream, ' ', indent + 4);
  2523. fputs_e("pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);\n", gen->stream);
  2524. write_characters(gen->stream, ' ', indent + 4);
  2525. fputs_e("break;\n", gen->stream);
  2526. }
  2527. }
  2528. write_characters(gen->stream, ' ', indent);
  2529. fputs_e("}\n", gen->stream);
  2530. if (min > 0) {
  2531. write_characters(gen->stream, ' ', indent);
  2532. fprintf_e(gen->stream, "if (i < %d) {\n", min);
  2533. write_characters(gen->stream, ' ', indent + 4);
  2534. fputs_e("ctx->cur = p0;\n", gen->stream);
  2535. write_characters(gen->stream, ' ', indent + 4);
  2536. fputs_e("pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n0);\n", gen->stream);
  2537. write_characters(gen->stream, ' ', indent + 4);
  2538. fprintf_e(gen->stream, "goto L%04d;\n", onfail);
  2539. write_characters(gen->stream, ' ', indent);
  2540. fputs_e("}\n", gen->stream);
  2541. }
  2542. if (!bare) {
  2543. indent -= 4;
  2544. write_characters(gen->stream, ' ', indent);
  2545. fputs_e("}\n", gen->stream);
  2546. }
  2547. return (min > 0) ? ((r == CODE_REACH__ALWAYS_FAIL) ? CODE_REACH__ALWAYS_FAIL : CODE_REACH__BOTH) : CODE_REACH__ALWAYS_SUCCEED;
  2548. }
  2549. else if (max == 1) {
  2550. if (min > 0) {
  2551. return generate_code(gen, expr, onfail, indent, bare);
  2552. }
  2553. else {
  2554. if (!bare) {
  2555. write_characters(gen->stream, ' ', indent);
  2556. fputs_e("{\n", gen->stream);
  2557. indent += 4;
  2558. }
  2559. write_characters(gen->stream, ' ', indent);
  2560. fputs_e("const size_t p = ctx->cur;\n", gen->stream);
  2561. write_characters(gen->stream, ' ', indent);
  2562. fputs_e("const size_t n = chunk->thunks.len;\n", gen->stream);
  2563. {
  2564. const int l = ++gen->label;
  2565. if (generate_code(gen, expr, l, indent, FALSE) != CODE_REACH__ALWAYS_SUCCEED) {
  2566. const int m = ++gen->label;
  2567. write_characters(gen->stream, ' ', indent);
  2568. fprintf_e(gen->stream, "goto L%04d;\n", m);
  2569. if (indent > 4) write_characters(gen->stream, ' ', indent - 4);
  2570. fprintf_e(gen->stream, "L%04d:;\n", l);
  2571. write_characters(gen->stream, ' ', indent);
  2572. fputs_e("ctx->cur = p;\n", gen->stream);
  2573. write_characters(gen->stream, ' ', indent);
  2574. fputs_e("pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);\n", gen->stream);
  2575. if (indent > 4) write_characters(gen->stream, ' ', indent - 4);
  2576. fprintf_e(gen->stream, "L%04d:;\n", m);
  2577. }
  2578. }
  2579. if (!bare) {
  2580. indent -= 4;
  2581. write_characters(gen->stream, ' ', indent);
  2582. fputs_e("}\n", gen->stream);
  2583. }
  2584. return CODE_REACH__ALWAYS_SUCCEED;
  2585. }
  2586. }
  2587. else {
  2588. /* no code to generate */
  2589. return CODE_REACH__ALWAYS_SUCCEED;
  2590. }
  2591. }
  2592. static code_reach_t generate_predicating_code(generate_t *gen, const node_t *expr, bool_t neg, int onfail, size_t indent, bool_t bare) {
  2593. code_reach_t r;
  2594. fputs_e("/* generated by generate_predicating_code() */\n", gen->stream);
  2595. if (!bare) {
  2596. write_characters(gen->stream, ' ', indent);
  2597. fputs_e("{\n", gen->stream);
  2598. indent += 4;
  2599. }
  2600. write_characters(gen->stream, ' ', indent);
  2601. fputs_e("const size_t p = ctx->cur;\n", gen->stream);
  2602. if (neg) {
  2603. const int l = ++gen->label;
  2604. r = generate_code(gen, expr, l, indent, FALSE);
  2605. if (r != CODE_REACH__ALWAYS_FAIL) {
  2606. write_characters(gen->stream, ' ', indent);
  2607. fputs_e("ctx->cur = p;\n", gen->stream);
  2608. write_characters(gen->stream, ' ', indent);
  2609. fprintf_e(gen->stream, "goto L%04d;\n", onfail);
  2610. }
  2611. if (r != CODE_REACH__ALWAYS_SUCCEED) {
  2612. if (indent > 4) write_characters(gen->stream, ' ', indent - 4);
  2613. fprintf_e(gen->stream, "L%04d:;\n", l);
  2614. write_characters(gen->stream, ' ', indent);
  2615. fputs_e("ctx->cur = p;\n", gen->stream);
  2616. }
  2617. switch (r) {
  2618. case CODE_REACH__ALWAYS_SUCCEED: r = CODE_REACH__ALWAYS_FAIL; break;
  2619. case CODE_REACH__ALWAYS_FAIL: r = CODE_REACH__ALWAYS_SUCCEED; break;
  2620. case CODE_REACH__BOTH: break;
  2621. }
  2622. }
  2623. else {
  2624. const int l = ++gen->label;
  2625. const int m = ++gen->label;
  2626. r = generate_code(gen, expr, l, indent, FALSE);
  2627. if (r != CODE_REACH__ALWAYS_FAIL) {
  2628. write_characters(gen->stream, ' ', indent);
  2629. fputs_e("ctx->cur = p;\n", gen->stream);
  2630. }
  2631. if (r == CODE_REACH__BOTH) {
  2632. write_characters(gen->stream, ' ', indent);
  2633. fprintf_e(gen->stream, "goto L%04d;\n", m);
  2634. }
  2635. if (r != CODE_REACH__ALWAYS_SUCCEED) {
  2636. if (indent > 4) write_characters(gen->stream, ' ', indent - 4);
  2637. fprintf_e(gen->stream, "L%04d:;\n", l);
  2638. write_characters(gen->stream, ' ', indent);
  2639. fputs_e("ctx->cur = p;\n", gen->stream);
  2640. write_characters(gen->stream, ' ', indent);
  2641. fprintf_e(gen->stream, "goto L%04d;\n", onfail);
  2642. }
  2643. if (r == CODE_REACH__BOTH) {
  2644. if (indent > 4) write_characters(gen->stream, ' ', indent - 4);
  2645. fprintf_e(gen->stream, "L%04d:;\n", m);
  2646. }
  2647. }
  2648. if (!bare) {
  2649. indent -= 4;
  2650. write_characters(gen->stream, ' ', indent);
  2651. fputs_e("}\n", gen->stream);
  2652. }
  2653. return r;
  2654. }
  2655. static code_reach_t generate_sequential_code(generate_t *gen, const node_array_t *nodes, int onfail, size_t indent, bool_t bare) {
  2656. bool_t b = FALSE;
  2657. size_t i;
  2658. fputs_e(" /* generated by generate_sequential_code() */\n", gen->stream);
  2659. if (bare) {}
  2660. for (i = 0; i < nodes->len; i++) {
  2661. switch (generate_code(gen, nodes->buf[i], onfail, indent, FALSE)) {
  2662. case CODE_REACH__ALWAYS_FAIL:
  2663. if (i + 1 < nodes->len) {
  2664. write_characters(gen->stream, ' ', indent);
  2665. fputs_e("/* unreachable codes omitted */\n", gen->stream);
  2666. }
  2667. return CODE_REACH__ALWAYS_FAIL;
  2668. case CODE_REACH__ALWAYS_SUCCEED:
  2669. break;
  2670. default:
  2671. b = TRUE;
  2672. }
  2673. }
  2674. return b ? CODE_REACH__BOTH : CODE_REACH__ALWAYS_SUCCEED;
  2675. }
  2676. static code_reach_t generate_alternative_code(generate_t *gen, const node_array_t *nodes, int onfail, size_t indent, bool_t bare) {
  2677. bool_t b = FALSE;
  2678. int m = ++gen->label;
  2679. size_t i;
  2680. fputs_e("/* generated by generate_alternative_code() */\n", gen->stream);
  2681. if (!bare) {
  2682. write_characters(gen->stream, ' ', indent);
  2683. fputs_e("{\n", gen->stream);
  2684. indent += 4;
  2685. }
  2686. write_characters(gen->stream, ' ', indent);
  2687. fputs_e("const size_t p = ctx->cur;\n", gen->stream);
  2688. write_characters(gen->stream, ' ', indent);
  2689. fputs_e("const size_t n = chunk->thunks.len;\n", gen->stream);
  2690. for (i = 0; i < nodes->len; i++) {
  2691. const bool_t c = (i + 1 < nodes->len) ? TRUE : FALSE;
  2692. const int l = ++gen->label;
  2693. switch (generate_code(gen, nodes->buf[i], l, indent, FALSE)) {
  2694. case CODE_REACH__ALWAYS_SUCCEED:
  2695. if (c) {
  2696. write_characters(gen->stream, ' ', indent);
  2697. fputs_e("/* unreachable codes omitted */\n", gen->stream);
  2698. }
  2699. if (b) {
  2700. if (indent > 4) write_characters(gen->stream, ' ', indent - 4);
  2701. fprintf_e(gen->stream, "L%04d:;\n", m);
  2702. }
  2703. if (!bare) {
  2704. indent -= 4;
  2705. write_characters(gen->stream, ' ', indent);
  2706. fputs_e("}\n", gen->stream);
  2707. }
  2708. return CODE_REACH__ALWAYS_SUCCEED;
  2709. case CODE_REACH__ALWAYS_FAIL:
  2710. break;
  2711. default:
  2712. b = TRUE;
  2713. write_characters(gen->stream, ' ', indent);
  2714. fprintf_e(gen->stream, "goto L%04d;\n", m);
  2715. }
  2716. if (indent > 4) write_characters(gen->stream, ' ', indent - 4);
  2717. fprintf_e(gen->stream, "L%04d:;\n", l);
  2718. write_characters(gen->stream, ' ', indent);
  2719. fputs_e("ctx->cur = p;\n", gen->stream);
  2720. write_characters(gen->stream, ' ', indent);
  2721. fputs_e("pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);\n", gen->stream);
  2722. if (!c) {
  2723. write_characters(gen->stream, ' ', indent);
  2724. fprintf_e(gen->stream, "goto L%04d;\n", onfail);
  2725. }
  2726. }
  2727. if (b) {
  2728. if (indent > 4) write_characters(gen->stream, ' ', indent - 4);
  2729. fprintf_e(gen->stream, "L%04d:;\n", m);
  2730. }
  2731. if (!bare) {
  2732. indent -= 4;
  2733. write_characters(gen->stream, ' ', indent);
  2734. fputs_e("}\n", gen->stream);
  2735. }
  2736. return b ? CODE_REACH__BOTH : CODE_REACH__ALWAYS_FAIL;
  2737. }
  2738. static code_reach_t generate_capturing_code(generate_t *gen, const node_t *expr, size_t index, int onfail, size_t indent, bool_t bare) {
  2739. code_reach_t r;
  2740. fputs_e("/* generated by generate_capturing_code() */\n", gen->stream);
  2741. if (!bare) {
  2742. write_characters(gen->stream, ' ', indent);
  2743. fputs_e("{\n", gen->stream);
  2744. indent += 4;
  2745. }
  2746. write_characters(gen->stream, ' ', indent);
  2747. fputs_e("const size_t p = ctx->cur;\n", gen->stream);
  2748. write_characters(gen->stream, ' ', indent);
  2749. fputs_e("size_t q;\n", gen->stream);
  2750. r = generate_code(gen, expr, onfail, indent, FALSE);
  2751. write_characters(gen->stream, ' ', indent);
  2752. fputs_e("q = ctx->cur;\n", gen->stream);
  2753. write_characters(gen->stream, ' ', indent);
  2754. fprintf_e(gen->stream, "chunk->capts.buf[" FMT_LU "].range.start = p;\n", (ulong_t)index);
  2755. write_characters(gen->stream, ' ', indent);
  2756. fprintf_e(gen->stream, "chunk->capts.buf[" FMT_LU "].range.end = q;\n", (ulong_t)index);
  2757. if (!bare) {
  2758. indent -= 4;
  2759. write_characters(gen->stream, ' ', indent);
  2760. fputs_e("}\n", gen->stream);
  2761. }
  2762. return r;
  2763. }
  2764. static code_reach_t generate_expanding_code(generate_t *gen, size_t index, int onfail, size_t indent, bool_t bare) {
  2765. fputs_e("/* generated by generate_expanding_code() */\n", gen->stream);
  2766. if (!bare) {
  2767. write_characters(gen->stream, ' ', indent);
  2768. fputs_e("{\n", gen->stream);
  2769. indent += 4;
  2770. }
  2771. write_characters(gen->stream, ' ', indent);
  2772. fprintf_e(gen->stream, "const size_t n = chunk->capts.buf[" FMT_LU "].range.end - chunk->capts.buf[" FMT_LU "].range.start;\n", (ulong_t)index, (ulong_t)index);
  2773. write_characters(gen->stream, ' ', indent);
  2774. fprintf_e(gen->stream, "if (pcc_refill_buffer(ctx, n) < n) goto L%04d;\n", onfail);
  2775. write_characters(gen->stream, ' ', indent);
  2776. fputs_e("if (n > 0) {\n", gen->stream);
  2777. write_characters(gen->stream, ' ', indent + 4);
  2778. fputs_e("const char *const p = ctx->buffer.buf + ctx->cur;\n", gen->stream);
  2779. write_characters(gen->stream, ' ', indent + 4);
  2780. fprintf_e(gen->stream, "const char *const q = ctx->buffer.buf + chunk->capts.buf[" FMT_LU "].range.start;\n", (ulong_t)index);
  2781. write_characters(gen->stream, ' ', indent + 4);
  2782. fputs_e("size_t i;\n", gen->stream);
  2783. write_characters(gen->stream, ' ', indent + 4);
  2784. fputs_e("for (i = 0; i < n; i++) {\n", gen->stream);
  2785. write_characters(gen->stream, ' ', indent + 8);
  2786. fprintf_e(gen->stream, "if (p[i] != q[i]) goto L%04d;\n", onfail);
  2787. write_characters(gen->stream, ' ', indent + 4);
  2788. fputs_e("}\n", gen->stream);
  2789. write_characters(gen->stream, ' ', indent + 4);
  2790. fputs_e("ctx->cur += n;\n", gen->stream);
  2791. write_characters(gen->stream, ' ', indent);
  2792. fputs_e("}\n", gen->stream);
  2793. if (!bare) {
  2794. indent -= 4;
  2795. write_characters(gen->stream, ' ', indent);
  2796. fputs_e("}\n", gen->stream);
  2797. }
  2798. return CODE_REACH__BOTH;
  2799. }
  2800. static code_reach_t generate_thunking_action_code(
  2801. generate_t *gen, size_t index, const node_const_array_t *vars, const node_const_array_t *capts, bool_t error, int onfail, size_t indent, bool_t bare
  2802. ) {
  2803. if (bare) {}
  2804. if (onfail) {}
  2805. fputs_e("/* generated by generate_thunking_action_code() */\n", gen->stream);
  2806. assert(gen->rule->type == NODE_RULE);
  2807. if (!bare) {
  2808. write_characters(gen->stream, ' ', indent);
  2809. fputs_e("{\n", gen->stream);
  2810. indent += 4;
  2811. }
  2812. if (error) {
  2813. write_characters(gen->stream, ' ', indent);
  2814. fputs_e("pcc_value_t null;\n", gen->stream);
  2815. }
  2816. write_characters(gen->stream, ' ', indent);
  2817. fprintf_e(gen->stream, "pcc_thunk_t *const thunk = pcc_thunk__create_leaf(ctx->auxil, pcc_action_%s_" FMT_LU ", " FMT_LU ", " FMT_LU ");\n",
  2818. gen->rule->data.rule.name, (ulong_t)index, (ulong_t)gen->rule->data.rule.vars.len, (ulong_t)gen->rule->data.rule.capts.len);
  2819. {
  2820. size_t i;
  2821. for (i = 0; i < vars->len; i++) {
  2822. assert(vars->buf[i]->type == NODE_REFERENCE);
  2823. write_characters(gen->stream, ' ', indent);
  2824. fprintf_e(gen->stream, "thunk->data.leaf.values.buf[" FMT_LU "] = &(chunk->values.buf[" FMT_LU "]);\n",
  2825. (ulong_t)vars->buf[i]->data.reference.index, (ulong_t)vars->buf[i]->data.reference.index);
  2826. }
  2827. for (i = 0; i < capts->len; i++) {
  2828. assert(capts->buf[i]->type == NODE_CAPTURE);
  2829. write_characters(gen->stream, ' ', indent);
  2830. fprintf_e(gen->stream, "thunk->data.leaf.capts.buf[" FMT_LU "] = &(chunk->capts.buf[" FMT_LU "]);\n",
  2831. (ulong_t)capts->buf[i]->data.capture.index, (ulong_t)capts->buf[i]->data.capture.index);
  2832. }
  2833. write_characters(gen->stream, ' ', indent);
  2834. fputs_e("thunk->data.leaf.capt0.range.start = chunk->pos;\n", gen->stream);
  2835. write_characters(gen->stream, ' ', indent);
  2836. fputs_e("thunk->data.leaf.capt0.range.end = ctx->cur;\n", gen->stream);
  2837. }
  2838. if (error) {
  2839. write_characters(gen->stream, ' ', indent);
  2840. fputs_e("memset(&null, 0, sizeof(pcc_value_t)); /* in case */\n", gen->stream);
  2841. write_characters(gen->stream, ' ', indent);
  2842. fputs_e("thunk->data.leaf.action(ctx, thunk, &null);\n", gen->stream);
  2843. write_characters(gen->stream, ' ', indent);
  2844. fputs_e("pcc_thunk__destroy(ctx->auxil, thunk);\n", gen->stream);
  2845. }
  2846. else {
  2847. write_characters(gen->stream, ' ', indent);
  2848. fputs_e("pcc_thunk_array__add(ctx->auxil, &chunk->thunks, thunk);\n", gen->stream);
  2849. }
  2850. if (!bare) {
  2851. indent -= 4;
  2852. write_characters(gen->stream, ' ', indent);
  2853. fputs_e("}\n", gen->stream);
  2854. }
  2855. return CODE_REACH__ALWAYS_SUCCEED;
  2856. }
  2857. static code_reach_t generate_thunking_error_code(
  2858. generate_t *gen, const node_t *expr, size_t index, const node_const_array_t *vars, const node_const_array_t *capts, int onfail, size_t indent, bool_t bare
  2859. ) {
  2860. code_reach_t r;
  2861. const int l = ++gen->label;
  2862. const int m = ++gen->label;
  2863. assert(gen->rule->type == NODE_RULE);
  2864. fputs_e("/* generated by generate_thunking_error_code() */\n", gen->stream);
  2865. if (!bare) {
  2866. write_characters(gen->stream, ' ', indent);
  2867. fputs_e("{\n", gen->stream);
  2868. indent += 4;
  2869. }
  2870. r = generate_code(gen, expr, l, indent, TRUE);
  2871. write_characters(gen->stream, ' ', indent);
  2872. fprintf_e(gen->stream, "goto L%04d;\n", m);
  2873. if (indent > 4) write_characters(gen->stream, ' ', indent - 4);
  2874. fprintf_e(gen->stream, "L%04d:;\n", l);
  2875. generate_thunking_action_code(gen, index, vars, capts, TRUE, l, indent, FALSE);
  2876. write_characters(gen->stream, ' ', indent);
  2877. fprintf_e(gen->stream, "goto L%04d;\n", onfail);
  2878. if (indent > 4) write_characters(gen->stream, ' ', indent - 4);
  2879. fprintf_e(gen->stream, "L%04d:;\n", m);
  2880. if (!bare) {
  2881. indent -= 4;
  2882. write_characters(gen->stream, ' ', indent);
  2883. fputs_e("}\n", gen->stream);
  2884. }
  2885. return r;
  2886. }
  2887. static code_reach_t generate_code(generate_t *gen, const node_t *node, int onfail, size_t indent, bool_t bare) {
  2888. if (node == NULL) {
  2889. print_error("Internal error [%d]\n", __LINE__);
  2890. exit(-1);
  2891. }
  2892. switch (node->type) {
  2893. case NODE_RULE:
  2894. print_error("Internal error [%d]\n", __LINE__);
  2895. exit(-1);
  2896. case NODE_REFERENCE:
  2897. write_characters(gen->stream, ' ', indent);
  2898. if (node->data.reference.index != VOID_VALUE) {
  2899. fprintf_e(gen->stream, "if (!pcc_apply_rule(ctx, pcc_evaluate_rule_%s, &chunk->thunks, &(chunk->values.buf[" FMT_LU "]))) goto L%04d;\n",
  2900. node->data.reference.name, (ulong_t)node->data.reference.index, onfail);
  2901. }
  2902. else {
  2903. fprintf_e(gen->stream, "if (!pcc_apply_rule(ctx, pcc_evaluate_rule_%s, &chunk->thunks, NULL)) goto L%04d;\n",
  2904. node->data.reference.name, onfail);
  2905. }
  2906. return CODE_REACH__BOTH;
  2907. case NODE_STRING:
  2908. return generate_matching_string_code(gen, node->data.string.value, onfail, indent, bare);
  2909. case NODE_CHARCLASS:
  2910. return gen->ascii ?
  2911. generate_matching_charclass_code(gen, node->data.charclass.value, onfail, indent, bare) :
  2912. generate_matching_utf8_charclass_code(gen, node->data.charclass.value, onfail, indent, bare);
  2913. case NODE_QUANTITY:
  2914. return generate_quantifying_code(gen, node->data.quantity.expr, node->data.quantity.min, node->data.quantity.max, onfail, indent, bare);
  2915. case NODE_PREDICATE:
  2916. return generate_predicating_code(gen, node->data.predicate.expr, node->data.predicate.neg, onfail, indent, bare);
  2917. case NODE_SEQUENCE:
  2918. return generate_sequential_code(gen, &node->data.sequence.nodes, onfail, indent, bare);
  2919. case NODE_ALTERNATE:
  2920. return generate_alternative_code(gen, &node->data.alternate.nodes, onfail, indent, bare);
  2921. case NODE_CAPTURE:
  2922. return generate_capturing_code(gen, node->data.capture.expr, node->data.capture.index, onfail, indent, bare);
  2923. case NODE_EXPAND:
  2924. return generate_expanding_code(gen, node->data.expand.index, onfail, indent, bare);
  2925. case NODE_ACTION:
  2926. return generate_thunking_action_code(
  2927. gen, node->data.action.index, &node->data.action.vars, &node->data.action.capts, FALSE, onfail, indent, bare
  2928. );
  2929. case NODE_ERROR:
  2930. return generate_thunking_error_code(
  2931. gen, node->data.error.expr, node->data.error.index, &node->data.error.vars, &node->data.error.capts, onfail, indent, bare
  2932. );
  2933. default:
  2934. print_error("Internal error [%d]\n", __LINE__);
  2935. exit(-1);
  2936. }
  2937. }
  2938. static bool_t generate(context_t *ctx) {
  2939. const char *const vt = get_value_type(ctx);
  2940. const char *const at = get_auxil_type(ctx);
  2941. const bool_t vp = is_pointer_type(vt);
  2942. const bool_t ap = is_pointer_type(at);
  2943. FILE *const sstream = fopen_wt_e(ctx->sname);
  2944. FILE *const hstream = fopen_wt_e(ctx->hname);
  2945. fprintf_e(sstream, "/* A packrat parser generated by PackCC %s */\n\n", VERSION);
  2946. fprintf_e(hstream, "/* A packrat parser generated by PackCC %s */\n\n", VERSION);
  2947. {
  2948. write_code_block(hstream, ctx->eheader.buf, ctx->eheader.len, 0);
  2949. fprintf_e(
  2950. hstream,
  2951. "#ifndef PCC_INCLUDED_%s\n"
  2952. "#define PCC_INCLUDED_%s\n"
  2953. "\n",
  2954. ctx->hid, ctx->hid
  2955. );
  2956. write_code_block(hstream, ctx->header.buf, ctx->header.len, 0);
  2957. }
  2958. {
  2959. write_code_block(sstream, ctx->esource.buf, ctx->esource.len, 0);
  2960. fputs_e(
  2961. "/* THIS PARSER SOURCE CODE IS GNU GPL FREE SOFTWARE VERSION 3+ */\n"
  2962. "#ifdef _MSC_VER\n"
  2963. "#undef _CRT_SECURE_NO_WARNINGS\n"
  2964. "#define _CRT_SECURE_NO_WARNINGS\n"
  2965. "#endif /* _MSC_VER */\n"
  2966. "#include <stdio.h>\n"
  2967. "#include <stdlib.h>\n"
  2968. "#include <string.h> /* should have strnlen() */\n"
  2969. "\n"
  2970. "/* #ifndef _MSC_VER */\n"
  2971. "/* #if defined __GNUC__ && defined _WIN32 */ /* MinGW */\n"
  2972. "/* #ifndef PCC_USE_SYSTEM_STRNLEN */\n"
  2973. "/* #define strnlen(str, maxlen) pcc_strnlen(str, maxlen) */\n"
  2974. "/* static size_t pacc_strnlen_(const char *str, size_t maxlen) { */\n"
  2975. "/* size_t i; */\n"
  2976. "/* for (i = 0; i < maxlen && str[i]; i++); */\n"
  2977. "/* return i; */\n"
  2978. "/* } */\n"
  2979. "/* #endif */ /* !PCC_USE_SYSTEM_STRNLEN */\n"
  2980. "/* #endif */ /* defined __GNUC__ && defined _WIN32 */\n"
  2981. "/* #endif */ /* !_MSC_VER */\n"
  2982. "\n"
  2983. "#define PCC_DBG_EVALUATE 0\n"
  2984. "#define PCC_DBG_MATCH 1\n"
  2985. "#define PCC_DBG_NOMATCH 2\n"
  2986. "\n",
  2987. sstream
  2988. );
  2989. fprintf_e(
  2990. sstream,
  2991. "#include \"%s\"\n"
  2992. "\n",
  2993. ctx->hname
  2994. );
  2995. write_code_block(sstream, ctx->source.buf, ctx->source.len, 0);
  2996. }
  2997. {
  2998. fputs_e(
  2999. "#ifndef PCC_BUFFERSIZE\n"
  3000. "#define PCC_BUFFERSIZE 256\n"
  3001. "#endif /* !PCC_BUFFERSIZE */\n"
  3002. "\n"
  3003. "#ifndef PCC_ARRAYSIZE\n"
  3004. "#define PCC_ARRAYSIZE 2\n"
  3005. "#endif /* !PCC_ARRAYSIZE */\n"
  3006. "\n"
  3007. "#define VOID_VALUE (~(size_t)0)\n"
  3008. "\n"
  3009. "typedef enum pcc_bool_tag {\n"
  3010. " PCC_FALSE = 0,\n"
  3011. " PCC_TRUE\n"
  3012. "} pcc_bool_t;\n"
  3013. "\n"
  3014. "typedef struct pcc_char_array_tag {\n"
  3015. " char *buf;\n"
  3016. " size_t max;\n"
  3017. " size_t len;\n"
  3018. "} pcc_char_array_t;\n"
  3019. "\n"
  3020. "typedef struct pcc_range_tag {\n"
  3021. " size_t start;\n"
  3022. " size_t end;\n"
  3023. "} pcc_range_t;\n"
  3024. "\n",
  3025. sstream
  3026. );
  3027. fprintf_e(
  3028. sstream,
  3029. "typedef %s%spcc_value_t;\n"
  3030. "\n",
  3031. vt, vp ? "" : " "
  3032. );
  3033. fprintf_e(
  3034. sstream,
  3035. "typedef %s%spcc_auxil_t;\n"
  3036. "\n",
  3037. at, ap ? "" : " "
  3038. );
  3039. fputs_e(
  3040. "typedef struct pcc_value_table_tag {\n"
  3041. " pcc_value_t *buf;\n"
  3042. " size_t max;\n"
  3043. " size_t len;\n"
  3044. "} pcc_value_table_t;\n"
  3045. "\n"
  3046. "typedef struct pcc_value_refer_table_tag {\n"
  3047. " pcc_value_t **buf;\n"
  3048. " size_t max;\n"
  3049. " size_t len;\n"
  3050. "} pcc_value_refer_table_t;\n"
  3051. "\n"
  3052. "typedef struct pcc_capture_tag {\n"
  3053. " pcc_range_t range;\n"
  3054. " char *string; /* mutable */\n"
  3055. "} pcc_capture_t;\n"
  3056. "\n"
  3057. "typedef struct pcc_capture_table_tag {\n"
  3058. " pcc_capture_t *buf;\n"
  3059. " size_t max;\n"
  3060. " size_t len;\n"
  3061. "} pcc_capture_table_t;\n"
  3062. "\n"
  3063. "typedef struct pcc_capture_const_table_tag {\n"
  3064. " const pcc_capture_t **buf;\n"
  3065. " size_t max;\n"
  3066. " size_t len;\n"
  3067. "} pcc_capture_const_table_t;\n"
  3068. "\n"
  3069. "typedef struct pcc_thunk_tag pcc_thunk_t;\n"
  3070. "typedef struct pcc_thunk_array_tag pcc_thunk_array_t;\n"
  3071. "\n",
  3072. sstream
  3073. );
  3074. fprintf_e(
  3075. sstream,
  3076. "typedef void (*pcc_action_t)(%s_context_t *, pcc_thunk_t *, pcc_value_t *);\n"
  3077. "\n",
  3078. get_prefix(ctx)
  3079. );
  3080. fputs_e(
  3081. "typedef enum pcc_thunk_type_tag {\n"
  3082. " PCC_THUNK_LEAF,\n"
  3083. " PCC_THUNK_NODE\n"
  3084. "} pcc_thunk_type_t;\n"
  3085. "\n"
  3086. "typedef struct pcc_thunk_leaf_tag {\n"
  3087. " pcc_value_refer_table_t values;\n"
  3088. " pcc_capture_const_table_t capts;\n"
  3089. " pcc_capture_t capt0;\n"
  3090. " pcc_action_t action;\n"
  3091. "} pcc_thunk_leaf_t;\n"
  3092. "\n"
  3093. "typedef struct pcc_thunk_node_tag {\n"
  3094. " const pcc_thunk_array_t *thunks; /* just a reference */\n"
  3095. " pcc_value_t *value; /* just a reference */\n"
  3096. "} pcc_thunk_node_t;\n"
  3097. "\n"
  3098. "typedef union pcc_thunk_data_tag {\n"
  3099. " pcc_thunk_leaf_t leaf;\n"
  3100. " pcc_thunk_node_t node;\n"
  3101. "} pcc_thunk_data_t;\n"
  3102. "\n"
  3103. "struct pcc_thunk_tag {\n"
  3104. " pcc_thunk_type_t type;\n"
  3105. " pcc_thunk_data_t data;\n"
  3106. "};\n"
  3107. "\n"
  3108. "struct pcc_thunk_array_tag {\n"
  3109. " pcc_thunk_t **buf;\n"
  3110. " size_t max;\n"
  3111. " size_t len;\n"
  3112. "};\n"
  3113. "\n"
  3114. "typedef struct pcc_thunk_chunk_tag {\n"
  3115. " pcc_value_table_t values;\n"
  3116. " pcc_capture_table_t capts;\n"
  3117. " pcc_thunk_array_t thunks;\n"
  3118. " size_t pos; /* the starting position in the character buffer */\n"
  3119. "} pcc_thunk_chunk_t;\n"
  3120. "\n"
  3121. "typedef struct pcc_lr_entry_tag pcc_lr_entry_t;\n"
  3122. "\n"
  3123. "typedef enum pcc_lr_answer_type_tag {\n"
  3124. " PCC_LR_ANSWER_LR,\n"
  3125. " PCC_LR_ANSWER_CHUNK\n"
  3126. "} pcc_lr_answer_type_t;\n"
  3127. "\n"
  3128. "typedef union pcc_lr_answer_data_tag {\n"
  3129. " pcc_lr_entry_t *lr;\n"
  3130. " pcc_thunk_chunk_t *chunk;\n"
  3131. "} pcc_lr_answer_data_t;\n"
  3132. "\n"
  3133. "typedef struct pcc_lr_answer_tag pcc_lr_answer_t;\n"
  3134. "\n"
  3135. "struct pcc_lr_answer_tag {\n"
  3136. " pcc_lr_answer_type_t type;\n"
  3137. " pcc_lr_answer_data_t data;\n"
  3138. " size_t pos; /* the absolute position in the input */\n"
  3139. " pcc_lr_answer_t *hold;\n"
  3140. "};\n"
  3141. "\n",
  3142. sstream
  3143. );
  3144. fprintf_e(
  3145. sstream,
  3146. "typedef pcc_thunk_chunk_t *(*pcc_rule_t)(%s_context_t *);\n"
  3147. "\n",
  3148. get_prefix(ctx)
  3149. );
  3150. fputs_e(
  3151. "typedef struct pcc_rule_set_tag {\n"
  3152. " pcc_rule_t *buf;\n"
  3153. " size_t max;\n"
  3154. " size_t len;\n"
  3155. "} pcc_rule_set_t;\n"
  3156. "\n"
  3157. "typedef struct pcc_lr_head_tag pcc_lr_head_t;\n"
  3158. "\n"
  3159. "struct pcc_lr_head_tag {\n"
  3160. " pcc_rule_t rule;\n"
  3161. " pcc_rule_set_t invol;\n"
  3162. " pcc_rule_set_t eval;\n"
  3163. " pcc_lr_head_t *hold;\n"
  3164. "};\n"
  3165. "\n"
  3166. "typedef struct pcc_lr_memo_tag {\n"
  3167. " pcc_rule_t rule;\n"
  3168. " pcc_lr_answer_t *answer;\n"
  3169. "} pcc_lr_memo_t;\n"
  3170. "\n"
  3171. "typedef struct pcc_lr_memo_map_tag {\n"
  3172. " pcc_lr_memo_t *buf;\n"
  3173. " size_t max;\n"
  3174. " size_t len;\n"
  3175. "} pcc_lr_memo_map_t;\n"
  3176. "\n"
  3177. "typedef struct pcc_lr_table_entry_tag {\n"
  3178. " pcc_lr_head_t *head; /* just a reference */\n"
  3179. " pcc_lr_memo_map_t memos;\n"
  3180. " pcc_lr_answer_t *hold_a;\n"
  3181. " pcc_lr_head_t *hold_h;\n"
  3182. "} pcc_lr_table_entry_t;\n"
  3183. "\n"
  3184. "typedef struct pcc_lr_table_tag {\n"
  3185. " pcc_lr_table_entry_t **buf;\n"
  3186. " size_t max;\n"
  3187. " size_t len;\n"
  3188. "} pcc_lr_table_t;\n"
  3189. "\n"
  3190. "struct pcc_lr_entry_tag {\n"
  3191. " pcc_rule_t rule;\n"
  3192. " pcc_thunk_chunk_t *seed; /* just a reference */\n"
  3193. " pcc_lr_head_t *head; /* just a reference */\n"
  3194. "};\n"
  3195. "\n"
  3196. "typedef struct pcc_lr_stack_tag {\n"
  3197. " pcc_lr_entry_t **buf;\n"
  3198. " size_t max;\n"
  3199. " size_t len;\n"
  3200. "} pcc_lr_stack_t;\n"
  3201. "\n",
  3202. sstream
  3203. );
  3204. fprintf_e(
  3205. sstream,
  3206. "struct %s_context_tag {\n"
  3207. " size_t pos; /* the position in the input of the first character currently buffered */\n"
  3208. " size_t cur; /* the current parsing position in the character buffer */\n"
  3209. " size_t level;\n"
  3210. " pcc_char_array_t buffer;\n"
  3211. " pcc_lr_table_t lrtable;\n"
  3212. " pcc_lr_stack_t lrstack;\n"
  3213. " pcc_auxil_t auxil;\n"
  3214. "};\n"
  3215. "\n",
  3216. get_prefix(ctx)
  3217. );
  3218. fputs_e(
  3219. "#ifndef PCC_ERROR\n"
  3220. "#define PCC_ERROR(auxil) pcc_error()\n"
  3221. "static void pcc_error(void) {\n"
  3222. " fprintf(stderr, \"Syntax error\\n\");\n"
  3223. " exit(1);\n"
  3224. "}\n"
  3225. "#endif /* !PCC_ERROR */\n"
  3226. "\n"
  3227. "#ifndef PCC_GETCHAR\n"
  3228. "#define PCC_GETCHAR(auxil) getchar()\n"
  3229. "#endif /* !PCC_GETCHAR */\n"
  3230. "\n"
  3231. "#ifndef PCC_MALLOC\n"
  3232. "#define PCC_MALLOC(auxil, size) pcc_malloc_e(size)\n"
  3233. "static void *pcc_malloc_e(size_t size) {\n"
  3234. " void *const p = calloc(1,size);\n \n"
  3235. " if (p == NULL) {\n"
  3236. " fprintf(stderr, \"PCC_MALLOC Out of memory\\n\");\n"
  3237. " exit(1);\n"
  3238. " }\n"
  3239. " return p;\n"
  3240. "}\n"
  3241. "#endif /* !PCC_MALLOC */\n"
  3242. "\n"
  3243. "#ifndef PCC_REALLOC\n"
  3244. "#define PCC_REALLOC(auxil, ptr, size) pcc_realloc_e(ptr, size)\n"
  3245. "static void *pcc_realloc_e(void *ptr, size_t size) {\n"
  3246. " void *const p = realloc(ptr, size);\n"
  3247. " if (p == NULL) {\n"
  3248. " fprintf(stderr, \"PCC_REALLOC Out of memory\\n\");\n"
  3249. " exit(1);\n"
  3250. " }\n"
  3251. " return p;\n"
  3252. "}\n"
  3253. "#endif /* !PCC_REALLOC */\n"
  3254. "\n\n"
  3255. "#ifndef PCC_FREE\n"
  3256. "#define PCC_FREE(auxil, ptr) do { if (auxil) {} if (ptr) { free(ptr); } } while (0);\n"
  3257. "#endif /* !PCC_FREE */\n"
  3258. "\n"
  3259. "#ifndef PCC_DEBUG\n"
  3260. "#define PCC_DEBUG(event, rule, level, pos, buffer, length) ((void)0)\n"
  3261. "#endif /* !PCC_DEBUG */\n"
  3262. "\n"
  3263. /* not used
  3264. "static char *pcc_strdup_e(pcc_auxil_t auxil, const char *str) {\n"
  3265. " const size_t m = strlen(str);\n"
  3266. " char *const s = (char *)PCC_MALLOC(auxil, m + 1);\n"
  3267. " memmove(s, str, m);\n"
  3268. " s[m] = '\\0';\n"
  3269. " return s;\n"
  3270. "}\n"
  3271. "\n"
  3272. */
  3273. "static char *pcc_strndup_e(pcc_auxil_t auxil, const char *str, size_t len) {\n"
  3274. " const size_t m = strnlen(str, len); /* prototype is in <string.h> */\n"
  3275. " char *const s = (char *)PCC_MALLOC(auxil, m + 1);\n if (auxil) {}\n"
  3276. " memmove(s, str, m); if (auxil){}\n"
  3277. " s[m] = '\\0';\n"
  3278. " return s;\n"
  3279. "}\n"
  3280. "\n",
  3281. sstream
  3282. );
  3283. fputs_e(
  3284. "static void pcc_char_array__init(pcc_auxil_t auxil, pcc_char_array_t *array, size_t max) {\n"
  3285. " array->len = 0;\n"
  3286. " array->max = max;\n"
  3287. " array->buf = (char *)PCC_MALLOC(auxil, array->max);\n if (auxil) {}\n"
  3288. "}\n"
  3289. "\n"
  3290. "static void pcc_char_array__add(pcc_auxil_t auxil, pcc_char_array_t *array, char ch) {\n if (auxil){}\n"
  3291. " if (array->max <= array->len) {\n"
  3292. " const size_t n = array->len + 1;\n"
  3293. " size_t m = array->max;\n"
  3294. " if (m == 0) m = 1;\n"
  3295. " while (m < n && m != 0) m <<= 1;\n"
  3296. " if (m == 0) m = n;\n"
  3297. " array->buf = (char *)PCC_REALLOC(auxil, array->buf, m);\n if (auxil) {}\n"
  3298. " array->max = m;\n"
  3299. " }\n"
  3300. " array->buf[array->len++] = ch;\n"
  3301. "}\n"
  3302. "\n"
  3303. "static void pcc_char_array__term(pcc_auxil_t auxil, pcc_char_array_t *array) {\n"
  3304. " PCC_FREE(auxil, array->buf);\n"
  3305. "}\n"
  3306. "\n",
  3307. sstream
  3308. );
  3309. fputs_e(
  3310. "static void pcc_value_table__init(pcc_auxil_t auxil, pcc_value_table_t *table, size_t max) {\n"
  3311. " table->len = 0;\n"
  3312. " table->max = max;\n"
  3313. " table->buf = (pcc_value_t *)PCC_MALLOC(auxil, sizeof(pcc_value_t) * table->max);\n if (auxil) {}\n"
  3314. "}\n"
  3315. "\n"
  3316. "static void pcc_value_table__resize(pcc_auxil_t auxil, pcc_value_table_t *table, size_t len) {\n"
  3317. " if (table->max < len) {\n"
  3318. " size_t m = table->max;\n"
  3319. " if (m == 0) m = 1;\n"
  3320. " while (m < len && m != 0) m <<= 1;\n"
  3321. " if (m == 0) m = len;\n"
  3322. " table->buf = (pcc_value_t *)PCC_REALLOC(auxil, table->buf, sizeof(pcc_value_t) * m);\n if (auxil) {}\n"
  3323. " table->max = m;\n"
  3324. " }\n"
  3325. " table->len = len;\n"
  3326. "}\n"
  3327. "\n"
  3328. "static void pcc_value_table__term(pcc_auxil_t auxil, pcc_value_table_t *table) {\n"
  3329. " PCC_FREE(auxil, table->buf);\n"
  3330. "}\n"
  3331. "\n",
  3332. sstream
  3333. );
  3334. fputs_e(
  3335. "static void pcc_value_refer_table__init(pcc_auxil_t auxil, pcc_value_refer_table_t *table, size_t max) {\n"
  3336. " table->len = 0;\n"
  3337. " table->max = max;\n"
  3338. " table->buf = (pcc_value_t **)PCC_MALLOC(auxil, sizeof(pcc_value_t *) * table->max);\n if (auxil) {}\n"
  3339. "}\n"
  3340. "\n"
  3341. "static void pcc_value_refer_table__resize(pcc_auxil_t auxil, pcc_value_refer_table_t *table, size_t len) {\n"
  3342. " size_t i;\n"
  3343. " if (table->max < len) {\n"
  3344. " size_t m = table->max;\n"
  3345. " if (m == 0) m = 1;\n"
  3346. " while (m < len && m != 0) m <<= 1;\n"
  3347. " if (m == 0) m = len;\n"
  3348. " table->buf = (pcc_value_t **)PCC_REALLOC(auxil, table->buf, sizeof(pcc_value_t *) * m);\n if (auxil) {}\n"
  3349. " table->max = m;\n"
  3350. " }\n"
  3351. " for (i = table->len; i < len; i++) table->buf[i] = NULL;\n"
  3352. " table->len = len;\n"
  3353. "}\n"
  3354. "\n"
  3355. "static void pcc_value_refer_table__term(pcc_auxil_t auxil, pcc_value_refer_table_t *table) {\n if (auxil){}\n"
  3356. " PCC_FREE(auxil, table->buf);\n"
  3357. "}\n"
  3358. "\n",
  3359. sstream
  3360. );
  3361. fputs_e(
  3362. "static void pcc_capture_table__init(pcc_auxil_t auxil, pcc_capture_table_t *table, size_t max) {\n"
  3363. " table->len = 0;\n"
  3364. " table->max = max;\n"
  3365. " table->buf = (pcc_capture_t *)PCC_MALLOC(auxil, sizeof(pcc_capture_t) * table->max);\n if (auxil) {}\n"
  3366. "}\n"
  3367. "\n"
  3368. "static void pcc_capture_table__resize(pcc_auxil_t auxil, pcc_capture_table_t *table, size_t len) {\n"
  3369. " size_t i;\n"
  3370. " for (i = len; i < table->len; i++) PCC_FREE(auxil, table->buf[i].string);\n"
  3371. " if (table->max < len) {\n"
  3372. " size_t m = table->max;\n"
  3373. " if (m == 0) m = 1;\n"
  3374. " while (m < len && m != 0) m <<= 1;\n"
  3375. " if (m == 0) m = len;\n"
  3376. " table->buf = (pcc_capture_t *)PCC_REALLOC(auxil, table->buf, sizeof(pcc_capture_t) * m);\n if (auxil) {}\n"
  3377. " table->max = m;\n"
  3378. " }\n"
  3379. " for (i = table->len; i < len; i++) {\n"
  3380. " table->buf[i].range.start = 0;\n"
  3381. " table->buf[i].range.end = 0;\n"
  3382. " table->buf[i].string = NULL;\n"
  3383. " }\n"
  3384. " table->len = len;\n"
  3385. "}\n"
  3386. "\n"
  3387. "static void pcc_capture_table__term(pcc_auxil_t auxil, pcc_capture_table_t *table) {\n if (auxil){}\n"
  3388. " while (table->len > 0) {\n"
  3389. " table->len--;\n"
  3390. " PCC_FREE(auxil, table->buf[table->len].string);\n"
  3391. " }\n"
  3392. " PCC_FREE(auxil, table->buf);\n"
  3393. "}\n"
  3394. "\n",
  3395. sstream
  3396. );
  3397. fputs_e(
  3398. "static void pcc_capture_const_table__init(pcc_auxil_t auxil, pcc_capture_const_table_t *table, size_t max) {\n"
  3399. " table->len = 0;\n"
  3400. " table->max = max;\n"
  3401. " table->buf = (const pcc_capture_t **)PCC_MALLOC(auxil, sizeof(const pcc_capture_t *) * table->max);\n if (auxil) {}\n"
  3402. "}\n"
  3403. "\n"
  3404. "static void pcc_capture_const_table__resize(pcc_auxil_t auxil, pcc_capture_const_table_t *table, size_t len) {\n if (auxil){}\n"
  3405. " size_t i;\n"
  3406. " if (table->max < len) {\n"
  3407. " size_t m = table->max;\n"
  3408. " if (m == 0) m = 1;\n"
  3409. " while (m < len && m != 0) m <<= 1;\n"
  3410. " if (m == 0) m = len;\n"
  3411. " table->buf = (const pcc_capture_t **)PCC_REALLOC(auxil, (pcc_capture_t **)table->buf, sizeof(const pcc_capture_t *) * m);\n if (auxil) {}\n"
  3412. " table->max = m;\n"
  3413. " }\n"
  3414. " for (i = table->len; i < len; i++) table->buf[i] = NULL;\n"
  3415. " table->len = len;\n"
  3416. "}\n"
  3417. "\n"
  3418. "static void pcc_capture_const_table__term(pcc_auxil_t auxil, pcc_capture_const_table_t *table) {\n if (auxil){}\n"
  3419. " PCC_FREE(auxil, table->buf);\n"
  3420. "}\n"
  3421. "\n",
  3422. sstream
  3423. );
  3424. fputs_e(
  3425. "static pcc_thunk_t *pcc_thunk__create_leaf(pcc_auxil_t auxil, pcc_action_t action, size_t valuec, size_t captc) {\n"
  3426. " pcc_thunk_t *const thunk = (pcc_thunk_t *)PCC_MALLOC(auxil, sizeof(pcc_thunk_t)); if (auxil) {}\n\n"
  3427. " thunk->type = PCC_THUNK_LEAF;\n"
  3428. " pcc_value_refer_table__init(auxil, &thunk->data.leaf.values, valuec);\n"
  3429. " pcc_value_refer_table__resize(auxil, &thunk->data.leaf.values, valuec);\n"
  3430. " pcc_capture_const_table__init(auxil, &thunk->data.leaf.capts, captc);\n"
  3431. " pcc_capture_const_table__resize(auxil, &thunk->data.leaf.capts, captc);\n"
  3432. " thunk->data.leaf.capt0.range.start = 0;\n"
  3433. " thunk->data.leaf.capt0.range.end = 0;\n"
  3434. " thunk->data.leaf.capt0.string = NULL;\n"
  3435. " thunk->data.leaf.action = action;\n"
  3436. " return thunk;\n"
  3437. "}\n"
  3438. "\n"
  3439. "static pcc_thunk_t *pcc_thunk__create_node(pcc_auxil_t auxil, const pcc_thunk_array_t *thunks, pcc_value_t *value) {\n if (auxil) {}\n"
  3440. " pcc_thunk_t *const thunk = (pcc_thunk_t *)PCC_MALLOC(auxil, sizeof(pcc_thunk_t));\n if (auxil) {}\n"
  3441. " thunk->type = PCC_THUNK_NODE;\n"
  3442. " thunk->data.node.thunks = thunks;\n"
  3443. " thunk->data.node.value = value;\n"
  3444. " return thunk;\n"
  3445. "}\n"
  3446. "\n"
  3447. "static void pcc_thunk__destroy(pcc_auxil_t auxil, pcc_thunk_t *thunk) {\n"
  3448. " if (thunk == NULL) return;\n"
  3449. " switch (thunk->type) {\n"
  3450. " case PCC_THUNK_LEAF:\n"
  3451. " PCC_FREE(auxil, thunk->data.leaf.capt0.string);\n"
  3452. " pcc_capture_const_table__term(auxil, &thunk->data.leaf.capts);\n"
  3453. " pcc_value_refer_table__term(auxil, &thunk->data.leaf.values);\n"
  3454. " break;\n"
  3455. " case PCC_THUNK_NODE:\n"
  3456. " break;\n"
  3457. " default: /* unknown */\n"
  3458. " break;\n"
  3459. " }\n"
  3460. " PCC_FREE(auxil, thunk);\n"
  3461. "}\n"
  3462. "\n",
  3463. sstream
  3464. );
  3465. fputs_e(
  3466. "static void pcc_thunk_array__init(pcc_auxil_t auxil, pcc_thunk_array_t *array, size_t max) {\n"
  3467. " array->len = 0;\n"
  3468. " array->max = max;\n"
  3469. " array->buf = (pcc_thunk_t **)PCC_MALLOC(auxil, sizeof(pcc_thunk_t *) * array->max);\nif (auxil) {}\n"
  3470. "}\n"
  3471. "\n"
  3472. "static void pcc_thunk_array__add(pcc_auxil_t auxil, pcc_thunk_array_t *array, pcc_thunk_t *thunk) {\n if(auxil){}"
  3473. " if (array->max <= array->len) {\n"
  3474. " const size_t n = array->len + 1;\n"
  3475. " size_t m = array->max;\n"
  3476. " if (m == 0) m = 1;\n"
  3477. " while (m < n && m != 0) m <<= 1;\n"
  3478. " if (m == 0) m = n;\n"
  3479. " array->buf = (pcc_thunk_t **)PCC_REALLOC(auxil, array->buf, sizeof(pcc_thunk_t *) * m);\n if (auxil) {}\n"
  3480. " array->max = m;\n"
  3481. " }\n"
  3482. " array->buf[array->len++] = thunk;\n"
  3483. "}\n"
  3484. "\n"
  3485. "static void pcc_thunk_array__revert(pcc_auxil_t auxil, pcc_thunk_array_t *array, size_t len) {\n"
  3486. " while (array->len > len) {\n"
  3487. " array->len--;\n"
  3488. " pcc_thunk__destroy(auxil, array->buf[array->len]);\n"
  3489. " }\n"
  3490. "}\n"
  3491. "\n"
  3492. "static void pcc_thunk_array__term(pcc_auxil_t auxil, pcc_thunk_array_t *array) {\n"
  3493. " while (array->len > 0) {\n"
  3494. " array->len--;\n"
  3495. " pcc_thunk__destroy(auxil, array->buf[array->len]);\n"
  3496. " }\n"
  3497. " PCC_FREE(auxil, array->buf);\n"
  3498. "}\n"
  3499. "\n",
  3500. sstream
  3501. );
  3502. fputs_e(
  3503. "static pcc_thunk_chunk_t *pcc_thunk_chunk__create(pcc_auxil_t auxil) {\n"
  3504. " pcc_thunk_chunk_t *const chunk = (pcc_thunk_chunk_t *)PCC_MALLOC(auxil, sizeof(pcc_thunk_chunk_t));\n if (auxil) {}\n"
  3505. " pcc_value_table__init(auxil, &chunk->values, PCC_ARRAYSIZE);\n"
  3506. " pcc_capture_table__init(auxil, &chunk->capts, PCC_ARRAYSIZE);\n"
  3507. " pcc_thunk_array__init(auxil, &chunk->thunks, PCC_ARRAYSIZE);\n"
  3508. " chunk->pos = 0;\n"
  3509. " return chunk;\n"
  3510. "}\n"
  3511. "\n"
  3512. "static void pcc_thunk_chunk__destroy(pcc_auxil_t auxil, pcc_thunk_chunk_t *chunk) {\n"
  3513. " if (chunk == NULL) return;\n"
  3514. " pcc_thunk_array__term(auxil, &chunk->thunks);\n"
  3515. " pcc_capture_table__term(auxil, &chunk->capts);\n"
  3516. " pcc_value_table__term(auxil, &chunk->values);\n"
  3517. " PCC_FREE(auxil, chunk);\n"
  3518. "}\n"
  3519. "\n",
  3520. sstream
  3521. );
  3522. fputs_e(
  3523. "static void pcc_rule_set__init(pcc_auxil_t auxil, pcc_rule_set_t *set, size_t max) {\nif (auxil){}\n"
  3524. " set->len = 0;\n"
  3525. " set->max = max;\n"
  3526. " set->buf = (pcc_rule_t *)PCC_MALLOC(auxil, sizeof(pcc_rule_t) * set->max);\n if (auxil) {}\n"
  3527. "}\n"
  3528. "\n"
  3529. "static size_t pcc_rule_set__index(pcc_auxil_t auxil, const pcc_rule_set_t *set, pcc_rule_t rule) {\n if (auxil){}\n"
  3530. " size_t i;\n"
  3531. " for (i = 0; i < set->len; i++) {\n"
  3532. " if (set->buf[i] == rule) return i;\n"
  3533. " }\n"
  3534. " return VOID_VALUE;\n"
  3535. "}\n"
  3536. "\n"
  3537. "static pcc_bool_t pcc_rule_set__add(pcc_auxil_t auxil, pcc_rule_set_t *set, pcc_rule_t rule) {\n"
  3538. " const size_t i = pcc_rule_set__index(auxil, set, rule);\n"
  3539. " if (i != VOID_VALUE) return PCC_FALSE;\n"
  3540. " if (set->max <= set->len) {\n"
  3541. " const size_t n = set->len + 1;\n"
  3542. " size_t m = set->max;\n"
  3543. " if (m == 0) m = 1;\n"
  3544. " while (m < n && m != 0) m <<= 1;\n"
  3545. " if (m == 0) m = n;\n"
  3546. " set->buf = (pcc_rule_t *)PCC_REALLOC(auxil, set->buf, sizeof(pcc_rule_t) * m);\n if (auxil) {}\n"
  3547. " set->max = m;\n"
  3548. " }\n"
  3549. " set->buf[set->len++] = rule;\n"
  3550. " return PCC_TRUE;\n"
  3551. "}\n"
  3552. "\n"
  3553. "static pcc_bool_t pcc_rule_set__remove(pcc_auxil_t auxil, pcc_rule_set_t *set, pcc_rule_t rule) {\n"
  3554. " const size_t i = pcc_rule_set__index(auxil, set, rule);\n"
  3555. " if (i == VOID_VALUE) return PCC_FALSE;\n"
  3556. " memmove(set->buf + i, set->buf + (i + 1), sizeof(pcc_rule_t) * (set->len - (i + 1)));\n"
  3557. " return PCC_TRUE;\n"
  3558. "}\n"
  3559. "\n"
  3560. "static void pcc_rule_set__clear(pcc_auxil_t auxil, pcc_rule_set_t *set) {\nif (auxil){}\n"
  3561. " set->len = 0;\n"
  3562. "}\n"
  3563. "\n"
  3564. "static void pcc_rule_set__copy(pcc_auxil_t auxil, pcc_rule_set_t *set, const pcc_rule_set_t *src) {\n"
  3565. " size_t i;\n"
  3566. " pcc_rule_set__clear(auxil, set);\n"
  3567. " for (i = 0; i < src->len; i++) {\n"
  3568. " pcc_rule_set__add(auxil, set, src->buf[i]);\n"
  3569. " }\n"
  3570. "}\n"
  3571. "\n"
  3572. "static void pcc_rule_set__term(pcc_auxil_t auxil, pcc_rule_set_t *set) {\n if (auxil) {}\n"
  3573. " PCC_FREE(auxil, set->buf);\n"
  3574. "}\n"
  3575. "\n",
  3576. sstream
  3577. );
  3578. fputs_e(
  3579. "static pcc_lr_head_t *pcc_lr_head__create(pcc_auxil_t auxil, pcc_rule_t rule) {\n"
  3580. " pcc_lr_head_t *const head = (pcc_lr_head_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_head_t));\n if (auxil) {}\n"
  3581. " head->rule = rule;\n"
  3582. " pcc_rule_set__init(auxil, &head->invol, PCC_ARRAYSIZE);\n"
  3583. " pcc_rule_set__init(auxil, &head->eval, PCC_ARRAYSIZE);\n"
  3584. " head->hold = NULL;\n"
  3585. " return head;\n"
  3586. "}\n"
  3587. "\n"
  3588. "static void pcc_lr_head__destroy(pcc_auxil_t auxil, pcc_lr_head_t *head) {\n"
  3589. " if (head == NULL) return;\n"
  3590. " pcc_lr_head__destroy(auxil, head->hold);\n"
  3591. " pcc_rule_set__term(auxil, &head->eval);\n"
  3592. " pcc_rule_set__term(auxil, &head->invol);\n"
  3593. " PCC_FREE(auxil, head);\n"
  3594. "}\n"
  3595. "\n",
  3596. sstream
  3597. );
  3598. fputs_e(
  3599. "static void pcc_lr_entry__destroy(pcc_auxil_t auxil, pcc_lr_entry_t *lr); /* prototype */\n\n"
  3600. "\n"
  3601. "static pcc_lr_answer_t *pcc_lr_answer__create(pcc_auxil_t auxil, pcc_lr_answer_type_t type, size_t pos) {\n if (auxil){}\n"
  3602. " pcc_lr_answer_t *answer = (pcc_lr_answer_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_answer_t));\n if (auxil) {}\n"
  3603. " answer->type = type;\n"
  3604. " answer->pos = pos;\n"
  3605. " answer->hold = NULL;\n"
  3606. " switch (answer->type) {\n"
  3607. " case PCC_LR_ANSWER_LR:\n"
  3608. " answer->data.lr = NULL;\n"
  3609. " break;\n"
  3610. " case PCC_LR_ANSWER_CHUNK:\n"
  3611. " answer->data.chunk = NULL;\n"
  3612. " break;\n"
  3613. " default: /* unknown */\n"
  3614. " PCC_FREE(auxil, answer);\n"
  3615. " answer = NULL;\n"
  3616. " }\n"
  3617. " return answer;\n"
  3618. "}\n"
  3619. "\n"
  3620. "static void pcc_lr_answer__set_chunk(pcc_auxil_t auxil, pcc_lr_answer_t *answer, pcc_thunk_chunk_t *chunk) {\n"
  3621. " pcc_lr_answer_t *const a = pcc_lr_answer__create(auxil, answer->type, answer->pos);\n"
  3622. " switch (answer->type) {\n"
  3623. " case PCC_LR_ANSWER_LR:\n"
  3624. " a->data.lr = answer->data.lr;\n"
  3625. " break;\n"
  3626. " case PCC_LR_ANSWER_CHUNK:\n"
  3627. " a->data.chunk = answer->data.chunk;\n"
  3628. " break;\n"
  3629. " default: /* unknown */\n"
  3630. " break;\n"
  3631. " }\n"
  3632. " a->hold = answer->hold;\n"
  3633. " answer->hold = a;\n"
  3634. " answer->type = PCC_LR_ANSWER_CHUNK;\n"
  3635. " answer->data.chunk = chunk;\n"
  3636. "}\n"
  3637. "\n"
  3638. "static void pcc_lr_answer__destroy(pcc_auxil_t auxil, pcc_lr_answer_t *answer) {\n"
  3639. " if (answer == NULL) return;\n"
  3640. " pcc_lr_answer__destroy(auxil, answer->hold);\n"
  3641. " switch (answer->type) {\n"
  3642. " case PCC_LR_ANSWER_LR:\n"
  3643. " pcc_lr_entry__destroy(auxil, answer->data.lr);\n"
  3644. " break;\n"
  3645. " case PCC_LR_ANSWER_CHUNK:\n"
  3646. " pcc_thunk_chunk__destroy(auxil, answer->data.chunk);\n"
  3647. " break;\n"
  3648. " default: /* unknown */\n"
  3649. " break;\n"
  3650. " }\n"
  3651. " PCC_FREE(auxil, answer);\n"
  3652. "}\n"
  3653. "\n",
  3654. sstream
  3655. );
  3656. fputs_e(
  3657. "static void pcc_lr_memo_map__init(pcc_auxil_t auxil, pcc_lr_memo_map_t *map, size_t max) {\n if (auxil){}\n"
  3658. " map->len = 0;\n"
  3659. " map->max = max;\n"
  3660. " map->buf = (pcc_lr_memo_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_memo_t) * map->max);\n if (auxil) {}\n"
  3661. "}\n"
  3662. "\n"
  3663. "static size_t pcc_lr_memo_map__index(pcc_auxil_t auxil, pcc_lr_memo_map_t *map, pcc_rule_t rule) {\n if (auxil){}\n"
  3664. " size_t i;\n"
  3665. " for (i = 0; i < map->len; i++) {\n"
  3666. " if (map->buf[i].rule == rule) return i;\n"
  3667. " }\n"
  3668. " return VOID_VALUE;\n"
  3669. "}\n"
  3670. "\n"
  3671. "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) {\n"
  3672. " const size_t i = pcc_lr_memo_map__index(auxil, map, rule);\n"
  3673. " if (i != VOID_VALUE) {\n"
  3674. " pcc_lr_answer__destroy(auxil, map->buf[i].answer);\n"
  3675. " map->buf[i].answer = answer;\n"
  3676. " }\n"
  3677. " else {\n"
  3678. " if (map->max <= map->len) {\n"
  3679. " const size_t n = map->len + 1;\n"
  3680. " size_t m = map->max;\n"
  3681. " if (m == 0) m = 1;\n"
  3682. " while (m < n && m != 0) m <<= 1;\n"
  3683. " if (m == 0) m = n;\n"
  3684. " map->buf = (pcc_lr_memo_t *)PCC_REALLOC(auxil, map->buf, sizeof(pcc_lr_memo_t) * m);\n if (auxil) {}\n"
  3685. " map->max = m;\n"
  3686. " }\n"
  3687. " map->buf[map->len].rule = rule;\n"
  3688. " map->buf[map->len].answer = answer;\n"
  3689. " map->len++;\n"
  3690. " }\n"
  3691. "}\n"
  3692. "\n"
  3693. "static pcc_lr_answer_t *pcc_lr_memo_map__get(pcc_auxil_t auxil, pcc_lr_memo_map_t *map, pcc_rule_t rule) {\n"
  3694. " const size_t i = pcc_lr_memo_map__index(auxil, map, rule);\n"
  3695. " return (i != VOID_VALUE) ? map->buf[i].answer : NULL;\n"
  3696. "}\n"
  3697. "\n"
  3698. "static void pcc_lr_memo_map__term(pcc_auxil_t auxil, pcc_lr_memo_map_t *map) {\n"
  3699. " while (map->len > 0) {\n"
  3700. " map->len--;\n"
  3701. " pcc_lr_answer__destroy(auxil, map->buf[map->len].answer);\n"
  3702. " }\n"
  3703. " PCC_FREE(auxil, map->buf);\n"
  3704. "}\n"
  3705. "\n",
  3706. sstream
  3707. );
  3708. fputs_e(
  3709. "static pcc_lr_table_entry_t *pcc_lr_table_entry__create(pcc_auxil_t auxil) {\n"
  3710. " pcc_lr_table_entry_t *const entry = (pcc_lr_table_entry_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_table_entry_t));\n if (auxil) {}\n"
  3711. " entry->head = NULL;\n"
  3712. " pcc_lr_memo_map__init(auxil, &entry->memos, PCC_ARRAYSIZE);\n"
  3713. " entry->hold_a = NULL;\n"
  3714. " entry->hold_h = NULL;\n"
  3715. " return entry;\n"
  3716. "}\n"
  3717. "\n"
  3718. "static void pcc_lr_table_entry__destroy(pcc_auxil_t auxil, pcc_lr_table_entry_t *entry) {\nif (auxil){}"
  3719. " if (entry == NULL) return;\n"
  3720. " pcc_lr_head__destroy(auxil, entry->hold_h);\n"
  3721. " pcc_lr_answer__destroy(auxil, entry->hold_a);\n"
  3722. " pcc_lr_memo_map__term(auxil, &entry->memos);\n"
  3723. " PCC_FREE(auxil, entry);\n"
  3724. "}\n"
  3725. "\n",
  3726. sstream
  3727. );
  3728. fputs_e(
  3729. "static void pcc_lr_table__init(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t max) {\nif (auxil) {}\n"
  3730. " table->len = 0;\n"
  3731. " table->max = max;\n"
  3732. " table->buf = (pcc_lr_table_entry_t **)PCC_MALLOC(auxil, sizeof(pcc_lr_table_entry_t *) * table->max);\n if (auxil) {}\n"
  3733. "}\n"
  3734. "\n"
  3735. "static void pcc_lr_table__resize(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t len) {\n"
  3736. " size_t i;\n"
  3737. " for (i = len; i < table->len; i++) pcc_lr_table_entry__destroy(auxil, table->buf[i]);\n"
  3738. " if (table->max < len) {\n"
  3739. " size_t m = table->max;\n"
  3740. " if (m == 0) m = 1;\n"
  3741. " while (m < len && m != 0) m <<= 1;\n"
  3742. " if (m == 0) m = len;\n"
  3743. " table->buf = (pcc_lr_table_entry_t **)PCC_REALLOC(auxil, table->buf, sizeof(pcc_lr_table_entry_t *) * m);\n if (auxil) {}\n"
  3744. " table->max = m;\n"
  3745. " }\n"
  3746. " for (i = table->len; i < len; i++) table->buf[i] = NULL;\n"
  3747. " table->len = len;\n"
  3748. "}\n"
  3749. "\n"
  3750. "static void pcc_lr_table__set_head(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index, pcc_lr_head_t *head) {\n"
  3751. " if (index >= table->len) pcc_lr_table__resize(auxil, table, index + 1);\n"
  3752. " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(auxil);\n"
  3753. " table->buf[index]->head = head;\n"
  3754. "}\n"
  3755. "\n"
  3756. "static void pcc_lr_table__hold_head(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index, pcc_lr_head_t *head) {\n"
  3757. " if (index >= table->len) pcc_lr_table__resize(auxil, table, index + 1);\n"
  3758. " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(auxil);\n"
  3759. " head->hold = table->buf[index]->hold_h;\n"
  3760. " table->buf[index]->hold_h = head;\n"
  3761. "}\n"
  3762. "\n"
  3763. "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) {\n"
  3764. " if (index >= table->len) pcc_lr_table__resize(auxil, table, index + 1);\n"
  3765. " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(auxil);\n"
  3766. " pcc_lr_memo_map__put(auxil, &table->buf[index]->memos, rule, answer);\n"
  3767. "}\n"
  3768. "\n"
  3769. "static void pcc_lr_table__hold_answer(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index, pcc_lr_answer_t *answer) {\n"
  3770. " if (index >= table->len) pcc_lr_table__resize(auxil, table, index + 1);\n"
  3771. " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(auxil);\n"
  3772. " answer->hold = table->buf[index]->hold_a;\n"
  3773. " table->buf[index]->hold_a = answer;\n"
  3774. "}\n"
  3775. "\n"
  3776. "static pcc_lr_head_t *pcc_lr_table__get_head(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index) {\n if (auxil) {}\n"
  3777. " if (index >= table->len || table->buf[index] == NULL) return NULL;\n"
  3778. " return table->buf[index]->head;\n"
  3779. "}\n"
  3780. "\n"
  3781. "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) {\n if (auxil) {}\n"
  3782. " if (index >= table->len || table->buf[index] == NULL) return NULL;\n"
  3783. " return pcc_lr_memo_map__get(auxil, &table->buf[index]->memos, rule);\n"
  3784. "}\n"
  3785. "\n"
  3786. "static void pcc_lr_table__shift(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t count) {\n"
  3787. " size_t i;\n"
  3788. " if (count > table->len) count = table->len;\n"
  3789. " for (i = 0; i < count; i++) pcc_lr_table_entry__destroy(auxil, table->buf[i]);\n"
  3790. " memmove(table->buf, table->buf + count, sizeof(pcc_lr_table_entry_t *) * (table->len - count));\n"
  3791. " table->len -= count;\n"
  3792. "}\n"
  3793. "\n"
  3794. "static void pcc_lr_table__term(pcc_auxil_t auxil, pcc_lr_table_t *table) {\n"
  3795. " while (table->len > 0) {\n"
  3796. " table->len--;\n"
  3797. " pcc_lr_table_entry__destroy(auxil, table->buf[table->len]);\n"
  3798. " }\n"
  3799. " PCC_FREE(auxil, table->buf);\n"
  3800. "}\n"
  3801. "\n",
  3802. sstream
  3803. );
  3804. fputs_e(
  3805. "static pcc_lr_entry_t *pcc_lr_entry__create(pcc_auxil_t auxil, pcc_rule_t rule) {\nif (auxil) {}\n"
  3806. " pcc_lr_entry_t *const lr = (pcc_lr_entry_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_entry_t));\n if (auxil) {}\n"
  3807. " lr->rule = rule;\n"
  3808. " lr->seed = NULL;\n"
  3809. " lr->head = NULL;\n"
  3810. " return lr;\n"
  3811. "}\n"
  3812. "\n"
  3813. "static void pcc_lr_entry__destroy(pcc_auxil_t auxil, pcc_lr_entry_t *lr) {\n if (auxil) {}\n"
  3814. " PCC_FREE(auxil, lr);\n"
  3815. "}\n"
  3816. "\n",
  3817. sstream
  3818. );
  3819. fputs_e(
  3820. "static void pcc_lr_stack__init(pcc_auxil_t auxil, pcc_lr_stack_t *stack, size_t max) {\nif (auxil){}\n"
  3821. " stack->len = 0;\n"
  3822. " stack->max = max;\n"
  3823. " stack->buf = (pcc_lr_entry_t **)PCC_MALLOC(auxil, sizeof(pcc_lr_entry_t *) * stack->max);\n if (auxil) {}\n"
  3824. "}\n"
  3825. "\n"
  3826. "static void pcc_lr_stack__push(pcc_auxil_t auxil, pcc_lr_stack_t *stack, pcc_lr_entry_t *lr) {\nif (auxil) {}\n"
  3827. " if (stack->max <= stack->len) {\n"
  3828. " const size_t n = stack->len + 1;\n"
  3829. " size_t m = stack->max;\n"
  3830. " if (m == 0) m = 1;\n"
  3831. " while (m < n && m != 0) m <<= 1;\n"
  3832. " if (m == 0) m = n;\n"
  3833. " stack->buf = (pcc_lr_entry_t **)PCC_REALLOC(auxil, stack->buf, sizeof(pcc_lr_entry_t *) * m);\n if (auxil) {}\n"
  3834. " stack->max = m;\n"
  3835. " }\n"
  3836. " stack->buf[stack->len++] = lr;\n"
  3837. "}\n"
  3838. "\n"
  3839. "static pcc_lr_entry_t *pcc_lr_stack__pop(pcc_auxil_t auxil, pcc_lr_stack_t *stack) {\nif (auxil) {}\n"
  3840. " return stack->buf[--stack->len];\n"
  3841. "}\n"
  3842. "\n"
  3843. "static void pcc_lr_stack__term(pcc_auxil_t auxil, pcc_lr_stack_t *stack) {\nif (auxil) {}\n"
  3844. " PCC_FREE(auxil, stack->buf);\n"
  3845. "}\n"
  3846. "\n",
  3847. sstream
  3848. );
  3849. fprintf_e(
  3850. sstream,
  3851. "static %s_context_t *pcc_context__create(pcc_auxil_t auxil) {\n"
  3852. " %s_context_t *const ctx = (%s_context_t *)PCC_MALLOC(auxil, sizeof(%s_context_t));\n if (auxil) {}\n",
  3853. get_prefix(ctx), get_prefix(ctx), get_prefix(ctx), get_prefix(ctx)
  3854. );
  3855. fputs_e(
  3856. " ctx->pos = 0;\n"
  3857. " ctx->cur = 0;\n"
  3858. " ctx->level = 0;\n"
  3859. " pcc_char_array__init(auxil, &ctx->buffer, PCC_BUFFERSIZE);\n"
  3860. " pcc_lr_table__init(auxil, &ctx->lrtable, PCC_BUFFERSIZE);\n"
  3861. " pcc_lr_stack__init(auxil, &ctx->lrstack, PCC_ARRAYSIZE);\n"
  3862. " ctx->auxil = auxil;\n"
  3863. " return ctx;\n"
  3864. "}\n"
  3865. "\n",
  3866. sstream
  3867. );
  3868. fprintf_e(
  3869. sstream,
  3870. "static void pcc_context__destroy(%s_context_t *ctx) {\n",
  3871. get_prefix(ctx)
  3872. );
  3873. fputs_e(
  3874. " if (ctx == NULL) return;\n"
  3875. " pcc_lr_stack__term(ctx->auxil, &ctx->lrstack);\n"
  3876. " pcc_lr_table__term(ctx->auxil, &ctx->lrtable);\n"
  3877. " pcc_char_array__term(ctx->auxil, &ctx->buffer);\n"
  3878. " PCC_FREE(ctx->auxil, ctx);\n"
  3879. "}\n"
  3880. "\n",
  3881. sstream
  3882. );
  3883. fprintf_e(
  3884. sstream,
  3885. "static size_t pcc_refill_buffer(%s_context_t *ctx, size_t num) {\n",
  3886. get_prefix(ctx)
  3887. );
  3888. fputs_e(
  3889. " if (ctx->buffer.len >= ctx->cur + num) return ctx->buffer.len - ctx->cur;\n"
  3890. " while (ctx->buffer.len < ctx->cur + num) {\n"
  3891. " const int c = PCC_GETCHAR(ctx->auxil);\n"
  3892. " if (c == EOF) break;\n"
  3893. " pcc_char_array__add(ctx->auxil, &ctx->buffer, (char)c);\n"
  3894. " }\n"
  3895. " return ctx->buffer.len - ctx->cur;\n"
  3896. "}\n"
  3897. "\n",
  3898. sstream
  3899. );
  3900. fprintf_e(
  3901. sstream,
  3902. "static void pcc_commit_buffer(%s_context_t *ctx) {\n",
  3903. get_prefix(ctx)
  3904. );
  3905. fputs_e(
  3906. " memmove(ctx->buffer.buf, ctx->buffer.buf + ctx->cur, ctx->buffer.len - ctx->cur);\n"
  3907. " ctx->buffer.len -= ctx->cur;\n"
  3908. " ctx->pos += ctx->cur;\n"
  3909. " pcc_lr_table__shift(ctx->auxil, &ctx->lrtable, ctx->cur);\n"
  3910. " ctx->cur = 0;\n"
  3911. "}\n"
  3912. "\n",
  3913. sstream
  3914. );
  3915. fprintf_e(
  3916. sstream,
  3917. "static const char *pcc_get_capture_string(%s_context_t *ctx, const pcc_capture_t *capt) {\n",
  3918. get_prefix(ctx)
  3919. );
  3920. fputs_e(
  3921. " if (capt->string == NULL)\n"
  3922. " ((pcc_capture_t *)capt)->string =\n"
  3923. " pcc_strndup_e(ctx->auxil, ctx->buffer.buf + capt->range.start, capt->range.end - capt->range.start);\n"
  3924. " return capt->string;\n"
  3925. "}\n"
  3926. "\n",
  3927. sstream
  3928. );
  3929. if (ctx->flags & CODE_FLAG__UTF8_CHARCLASS_USED) {
  3930. fprintf_e(
  3931. sstream,
  3932. "static size_t pcc_get_char_as_utf32(%s_context_t *ctx, int *out) { /* with checking UTF-8 validity */\n",
  3933. get_prefix(ctx)
  3934. );
  3935. fputs_e(
  3936. " int c, u;\n"
  3937. " size_t n;\n"
  3938. " if (pcc_refill_buffer(ctx, 1) < 1) return 0;\n"
  3939. " c = (int)(unsigned char)ctx->buffer.buf[ctx->cur];\n"
  3940. " n = (c < 0x80) ? 1 :\n"
  3941. " ((c & 0xe0) == 0xc0) ? 2 :\n"
  3942. " ((c & 0xf0) == 0xe0) ? 3 :\n"
  3943. " ((c & 0xf8) == 0xf0) ? 4 : 0;\n"
  3944. " if (n < 1) return 0;\n"
  3945. " if (pcc_refill_buffer(ctx, n) < n) return 0;\n"
  3946. " switch (n) {\n"
  3947. " case 1:\n"
  3948. " u = c;\n"
  3949. " break;\n"
  3950. " case 2:\n"
  3951. " u = c & 0x1f;\n"
  3952. " c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 1];\n"
  3953. " if ((c & 0xc0) != 0x80) return 0;\n"
  3954. " u <<= 6; u |= c & 0x3f;\n"
  3955. " if (u < 0x80) return 0;\n"
  3956. " break;\n"
  3957. " case 3:\n"
  3958. " u = c & 0x0f;\n"
  3959. " c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 1];\n"
  3960. " if ((c & 0xc0) != 0x80) return 0;\n"
  3961. " u <<= 6; u |= c & 0x3f;\n"
  3962. " c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 2];\n"
  3963. " if ((c & 0xc0) != 0x80) return 0;\n"
  3964. " u <<= 6; u |= c & 0x3f;\n"
  3965. " if (u < 0x800) return 0;\n"
  3966. " break;\n"
  3967. " case 4:\n"
  3968. " u = c & 0x07;\n"
  3969. " c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 1];\n"
  3970. " if ((c & 0xc0) != 0x80) return 0;\n"
  3971. " u <<= 6; u |= c & 0x3f;\n"
  3972. " c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 2];\n"
  3973. " if ((c & 0xc0) != 0x80) return 0;\n"
  3974. " u <<= 6; u |= c & 0x3f;\n"
  3975. " c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 3];\n"
  3976. " if ((c & 0xc0) != 0x80) return 0;\n"
  3977. " u <<= 6; u |= c & 0x3f;\n"
  3978. " if (u < 0x10000 || u > 0x10ffff) return 0;\n"
  3979. " break;\n"
  3980. " default:\n"
  3981. " return 0;\n"
  3982. " }\n"
  3983. " if (out) *out = u;\n"
  3984. " return n;\n"
  3985. "}\n"
  3986. "\n",
  3987. sstream
  3988. );
  3989. }
  3990. fprintf_e(
  3991. sstream,
  3992. "static pcc_bool_t pcc_apply_rule(%s_context_t *ctx, pcc_rule_t rule, pcc_thunk_array_t *thunks, pcc_value_t *value) {\n",
  3993. get_prefix(ctx)
  3994. );
  3995. fputs_e(
  3996. " static pcc_value_t null;\n"
  3997. " pcc_thunk_chunk_t *c = NULL;\n"
  3998. " const size_t p = ctx->pos + ctx->cur;\n"
  3999. " pcc_bool_t b = PCC_TRUE;\n"
  4000. " pcc_lr_answer_t *a = pcc_lr_table__get_answer(ctx->auxil, &ctx->lrtable, p, rule);\n"
  4001. " pcc_lr_head_t *h = pcc_lr_table__get_head(ctx->auxil, &ctx->lrtable, p);\n"
  4002. " if (h != NULL) {\n"
  4003. " if (a == NULL && rule != h->rule && pcc_rule_set__index(ctx->auxil, &h->invol, rule) == VOID_VALUE) {\n"
  4004. " b = PCC_FALSE;\n"
  4005. " c = NULL;\n"
  4006. " }\n"
  4007. " else if (pcc_rule_set__remove(ctx->auxil, &h->eval, rule)) {\n"
  4008. " b = PCC_FALSE;\n"
  4009. " c = rule(ctx);\n"
  4010. " a = pcc_lr_answer__create(ctx->auxil, PCC_LR_ANSWER_CHUNK, ctx->pos + ctx->cur);\n"
  4011. " a->data.chunk = c;\n"
  4012. " pcc_lr_table__hold_answer(ctx->auxil, &ctx->lrtable, p, a);\n"
  4013. " }\n"
  4014. " }\n"
  4015. " if (b) {\n"
  4016. " if (a != NULL) {\n"
  4017. " ctx->cur = a->pos - ctx->pos;\n"
  4018. " switch (a->type) {\n"
  4019. " case PCC_LR_ANSWER_LR:\n"
  4020. " if (a->data.lr->head == NULL) {\n"
  4021. " a->data.lr->head = pcc_lr_head__create(ctx->auxil, rule);\n"
  4022. " pcc_lr_table__hold_head(ctx->auxil, &ctx->lrtable, p, a->data.lr->head);\n"
  4023. " }\n"
  4024. " {\n"
  4025. " size_t i = ctx->lrstack.len;\n"
  4026. " while (i > 0) {\n"
  4027. " i--;\n"
  4028. " if (ctx->lrstack.buf[i]->head == a->data.lr->head) break;\n"
  4029. " ctx->lrstack.buf[i]->head = a->data.lr->head;\n"
  4030. " pcc_rule_set__add(ctx->auxil, &a->data.lr->head->invol, ctx->lrstack.buf[i]->rule);\n"
  4031. " }\n"
  4032. " }\n"
  4033. " c = a->data.lr->seed;\n"
  4034. " break;\n"
  4035. " case PCC_LR_ANSWER_CHUNK:\n"
  4036. " c = a->data.chunk;\n"
  4037. " break;\n"
  4038. " default: /* unknown */\n"
  4039. " break;\n"
  4040. " }\n"
  4041. " }\n"
  4042. " else {\n"
  4043. " pcc_lr_entry_t *const e = pcc_lr_entry__create(ctx->auxil, rule);\n"
  4044. " pcc_lr_stack__push(ctx->auxil, &ctx->lrstack, e);\n"
  4045. " a = pcc_lr_answer__create(ctx->auxil, PCC_LR_ANSWER_LR, p);\n"
  4046. " a->data.lr = e;\n"
  4047. " pcc_lr_table__set_answer(ctx->auxil, &ctx->lrtable, p, rule, a);\n"
  4048. " c = rule(ctx);\n"
  4049. " pcc_lr_stack__pop(ctx->auxil, &ctx->lrstack);\n"
  4050. " a->pos = ctx->pos + ctx->cur;\n"
  4051. " if (e->head == NULL) {\n"
  4052. " pcc_lr_answer__set_chunk(ctx->auxil, a, c);\n"
  4053. " }\n"
  4054. " else {\n"
  4055. " e->seed = c;\n"
  4056. " h = a->data.lr->head;\n"
  4057. " if (h->rule != rule) {\n"
  4058. " c = a->data.lr->seed;\n"
  4059. " a = pcc_lr_answer__create(ctx->auxil, PCC_LR_ANSWER_CHUNK, ctx->pos + ctx->cur);\n"
  4060. " a->data.chunk = c;\n"
  4061. " pcc_lr_table__hold_answer(ctx->auxil, &ctx->lrtable, p, a);\n"
  4062. " }\n"
  4063. " else {\n"
  4064. " pcc_lr_answer__set_chunk(ctx->auxil, a, a->data.lr->seed);\n"
  4065. " if (a->data.chunk == NULL) {\n"
  4066. " c = NULL;\n"
  4067. " }\n"
  4068. " else {\n"
  4069. " pcc_lr_table__set_head(ctx->auxil, &ctx->lrtable, p, h);\n"
  4070. " for (;;) {\n"
  4071. " ctx->cur = p - ctx->pos;\n"
  4072. " pcc_rule_set__copy(ctx->auxil, &h->eval, &h->invol);\n"
  4073. " c = rule(ctx);\n"
  4074. " if (c == NULL || ctx->pos + ctx->cur <= a->pos) break;\n"
  4075. " pcc_lr_answer__set_chunk(ctx->auxil, a, c);\n"
  4076. " a->pos = ctx->pos + ctx->cur;\n"
  4077. " }\n"
  4078. " pcc_thunk_chunk__destroy(ctx->auxil, c);\n"
  4079. " pcc_lr_table__set_head(ctx->auxil, &ctx->lrtable, p, NULL);\n"
  4080. " ctx->cur = a->pos - ctx->pos;\n"
  4081. " c = a->data.chunk;\n"
  4082. " }\n"
  4083. " }\n"
  4084. " }\n"
  4085. " }\n"
  4086. " }\n"
  4087. " if (c == NULL) return PCC_FALSE;\n"
  4088. " if (value == NULL) value = &null;\n"
  4089. " memset(value, 0, sizeof(pcc_value_t)); /* in case */\n"
  4090. " pcc_thunk_array__add(ctx->auxil, thunks, pcc_thunk__create_node(ctx->auxil, &c->thunks, value));\n"
  4091. " return PCC_TRUE;\n"
  4092. "}\n"
  4093. "\n",
  4094. sstream
  4095. );
  4096. fprintf_e(
  4097. sstream,
  4098. "static void pcc_do_action(%s_context_t *ctx, const pcc_thunk_array_t *thunks, pcc_value_t *value) {\n",
  4099. get_prefix(ctx)
  4100. );
  4101. fputs_e(
  4102. " size_t i;\n"
  4103. " for (i = 0; i < thunks->len; i++) {\n"
  4104. " pcc_thunk_t *const thunk = thunks->buf[i];\n"
  4105. " switch (thunk->type) {\n"
  4106. " case PCC_THUNK_LEAF:\n"
  4107. " thunk->data.leaf.action(ctx, thunk, value);\n"
  4108. " break;\n"
  4109. " case PCC_THUNK_NODE:\n"
  4110. " pcc_do_action(ctx, thunk->data.node.thunks, thunk->data.node.value);\n"
  4111. " break;\n"
  4112. " default: /* unknown */\n"
  4113. " break;\n"
  4114. " }\n"
  4115. " }\n"
  4116. "}\n"
  4117. "\n",
  4118. sstream
  4119. );
  4120. {
  4121. size_t i, j, k;
  4122. for (i = 0; i < ctx->rules.len; i++) {
  4123. const node_rule_t *const r = &ctx->rules.buf[i]->data.rule;
  4124. for (j = 0; j < r->codes.len; j++) {
  4125. const char *s;
  4126. size_t d;
  4127. const node_const_array_t *v, *c;
  4128. switch (r->codes.buf[j]->type) {
  4129. case NODE_ACTION:
  4130. s = r->codes.buf[j]->data.action.value;
  4131. d = r->codes.buf[j]->data.action.index;
  4132. v = &r->codes.buf[j]->data.action.vars;
  4133. c = &r->codes.buf[j]->data.action.capts;
  4134. break;
  4135. case NODE_ERROR:
  4136. s = r->codes.buf[j]->data.error.value;
  4137. d = r->codes.buf[j]->data.error.index;
  4138. v = &r->codes.buf[j]->data.error.vars;
  4139. c = &r->codes.buf[j]->data.error.capts;
  4140. break;
  4141. default:
  4142. print_error("Internal error [%d]\n", __LINE__);
  4143. exit(-1);
  4144. }
  4145. fprintf_e(
  4146. sstream,
  4147. "static void pcc_action_%s_" FMT_LU "(%s_context_t *__pcc_ctx, pcc_thunk_t *__pcc_in, pcc_value_t *__pcc_out) { /* generated by generate() line %d */ \n",
  4148. r->name, (ulong_t)d, get_prefix(ctx),__LINE__
  4149. );
  4150. fputs_e(
  4151. "#define auxil (__pcc_ctx->auxil)\nif (auxil) {}\n"
  4152. "#define __ (*__pcc_out)\nif (__pcc_out) {}\n",
  4153. sstream
  4154. );
  4155. k = 0;
  4156. while (k < v->len) {
  4157. assert(v->buf[k]->type == NODE_REFERENCE);
  4158. fprintf_e(
  4159. sstream,
  4160. "#define %s (*__pcc_in->data.leaf.values.buf[" FMT_LU "])\n",
  4161. v->buf[k]->data.reference.var, (ulong_t)v->buf[k]->data.reference.index
  4162. );
  4163. k++;
  4164. }
  4165. fputs_e(
  4166. "#define _0 pcc_get_capture_string(__pcc_ctx, &__pcc_in->data.leaf.capt0)\nif (__pcc_in) {}\n"
  4167. "#define _0s ((const size_t)(__pcc_ctx->pos + __pcc_in->data.leaf.capt0.range.start))\n"
  4168. "#define _0e ((const size_t)(__pcc_ctx->pos + __pcc_in->data.leaf.capt0.range.end))\n",
  4169. sstream
  4170. );
  4171. k = 0;
  4172. while (k < c->len) {
  4173. assert(c->buf[k]->type == NODE_CAPTURE);
  4174. fprintf_e(
  4175. sstream,
  4176. "#define _" FMT_LU " pcc_get_capture_string(__pcc_ctx, __pcc_in->data.leaf.capts.buf[" FMT_LU "])\n",
  4177. (ulong_t)(c->buf[k]->data.capture.index + 1), (ulong_t)c->buf[k]->data.capture.index
  4178. );
  4179. fprintf_e(
  4180. sstream,
  4181. "#define _" FMT_LU "s ((const size_t)(__pcc_ctx->pos + __pcc_in->data.leaf.capts.buf[" FMT_LU "]->range.start))\n",
  4182. (ulong_t)(c->buf[k]->data.capture.index + 1), (ulong_t)c->buf[k]->data.capture.index
  4183. );
  4184. fprintf_e(
  4185. sstream,
  4186. "#define _" FMT_LU "e ((const size_t)(__pcc_ctx->pos + __pcc_in->data.leaf.capts.buf[" FMT_LU "]->range.end))\n",
  4187. (ulong_t)(c->buf[k]->data.capture.index + 1), (ulong_t)c->buf[k]->data.capture.index
  4188. );
  4189. k++;
  4190. }
  4191. write_code_block(sstream, s, strlen(s), 4);
  4192. k = c->len;
  4193. while (k > 0) {
  4194. k--;
  4195. assert(c->buf[k]->type == NODE_CAPTURE);
  4196. fprintf_e(
  4197. sstream,
  4198. "#undef _" FMT_LU "e\n",
  4199. (ulong_t)(c->buf[k]->data.capture.index + 1)
  4200. );
  4201. fprintf_e(
  4202. sstream,
  4203. "#undef _" FMT_LU "s\n",
  4204. (ulong_t)(c->buf[k]->data.capture.index + 1)
  4205. );
  4206. fprintf_e(
  4207. sstream,
  4208. "#undef _" FMT_LU "\n",
  4209. (ulong_t)(c->buf[k]->data.capture.index + 1)
  4210. );
  4211. }
  4212. fputs_e(
  4213. "#undef _0e\n"
  4214. "#undef _0s\n"
  4215. "#undef _0\n",
  4216. sstream
  4217. );
  4218. k = v->len;
  4219. while (k > 0) {
  4220. k--;
  4221. assert(v->buf[k]->type == NODE_REFERENCE);
  4222. fprintf_e(
  4223. sstream,
  4224. "#undef %s\n",
  4225. v->buf[k]->data.reference.var
  4226. );
  4227. }
  4228. fputs_e(
  4229. "#undef __\n"
  4230. "#undef auxil\n",
  4231. sstream
  4232. );
  4233. fputs_e(
  4234. "}\n"
  4235. "\n",
  4236. sstream
  4237. );
  4238. }
  4239. }
  4240. }
  4241. {
  4242. size_t i;
  4243. for (i = 0; i < ctx->rules.len; i++) {
  4244. fprintf_e(
  4245. sstream,
  4246. "static pcc_thunk_chunk_t *pcc_evaluate_rule_%s(%s_context_t *ctx);\n",
  4247. ctx->rules.buf[i]->data.rule.name, get_prefix(ctx)
  4248. );
  4249. }
  4250. fputs_e(
  4251. "\n",
  4252. sstream
  4253. );
  4254. for (i = 0; i < ctx->rules.len; i++) {
  4255. code_reach_t r;
  4256. generate_t g;
  4257. g.stream = sstream;
  4258. g.rule = ctx->rules.buf[i];
  4259. g.label = 0;
  4260. g.ascii = ctx->ascii;
  4261. fprintf_e(
  4262. sstream,
  4263. "static pcc_thunk_chunk_t *pcc_evaluate_rule_%s(%s_context_t *ctx) {\n",
  4264. ctx->rules.buf[i]->data.rule.name, get_prefix(ctx)
  4265. );
  4266. fprintf_e(
  4267. sstream,
  4268. " pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);\n"
  4269. " chunk->pos = ctx->cur;\n"
  4270. " PCC_DEBUG(PCC_DBG_EVALUATE, \"%s\", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));\n"
  4271. " ctx->level++;\n",
  4272. ctx->rules.buf[i]->data.rule.name
  4273. );
  4274. fprintf_e(
  4275. sstream,
  4276. " pcc_value_table__resize(ctx->auxil, &chunk->values, " FMT_LU ");\n",
  4277. (ulong_t)ctx->rules.buf[i]->data.rule.vars.len
  4278. );
  4279. fprintf_e(
  4280. sstream,
  4281. " pcc_capture_table__resize(ctx->auxil, &chunk->capts, " FMT_LU ");\n",
  4282. (ulong_t)ctx->rules.buf[i]->data.rule.capts.len
  4283. );
  4284. r = generate_code(&g, ctx->rules.buf[i]->data.rule.expr, 0, 4, FALSE);
  4285. fprintf_e(
  4286. sstream,
  4287. " ctx->level--;\n"
  4288. " PCC_DEBUG(PCC_DBG_MATCH, \"%s\", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));\n"
  4289. " return chunk;\n",
  4290. ctx->rules.buf[i]->data.rule.name
  4291. );
  4292. if (r != CODE_REACH__ALWAYS_SUCCEED) {
  4293. fprintf_e(
  4294. sstream,
  4295. "L0000:;\n"
  4296. " ctx->level--;\n"
  4297. " PCC_DEBUG(PCC_DBG_NOMATCH, \"%s\", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));\n"
  4298. " pcc_thunk_chunk__destroy(ctx->auxil, chunk);\n"
  4299. " return NULL;\n",
  4300. ctx->rules.buf[i]->data.rule.name
  4301. );
  4302. }
  4303. fputs_e(
  4304. "}\n"
  4305. "\n",
  4306. sstream
  4307. );
  4308. }
  4309. }
  4310. fprintf_e(
  4311. sstream,
  4312. "%s_context_t *%s_create(%s%sauxil) {\n",
  4313. get_prefix(ctx), get_prefix(ctx),
  4314. at, ap ? "" : " "
  4315. );
  4316. fputs_e(
  4317. " return pcc_context__create(auxil);\n"
  4318. "}\n"
  4319. "\n",
  4320. sstream
  4321. );
  4322. fprintf_e(
  4323. sstream,
  4324. "int %s_parse(%s_context_t *ctx, %s%s*ret) {\n",
  4325. get_prefix(ctx), get_prefix(ctx),
  4326. vt, vp ? "" : " "
  4327. );
  4328. fputs_e(
  4329. " pcc_thunk_array_t thunks;\n"
  4330. " pcc_thunk_array__init(ctx->auxil, &thunks, PCC_ARRAYSIZE);\n",
  4331. sstream
  4332. );
  4333. if (ctx->rules.len > 0) {
  4334. fprintf_e(
  4335. sstream,
  4336. " if (pcc_apply_rule(ctx, pcc_evaluate_rule_%s, &thunks, ret))\n",
  4337. ctx->rules.buf[0]->data.rule.name
  4338. );
  4339. fputs_e(
  4340. " pcc_do_action(ctx, &thunks, ret);\n"
  4341. " else\n"
  4342. " PCC_ERROR(ctx->auxil);\n"
  4343. " pcc_commit_buffer(ctx);\n",
  4344. sstream
  4345. );
  4346. }
  4347. fputs_e(
  4348. " pcc_thunk_array__term(ctx->auxil, &thunks);\n"
  4349. " return pcc_refill_buffer(ctx, 1) >= 1;\n"
  4350. "}\n"
  4351. "\n",
  4352. sstream
  4353. );
  4354. fprintf_e(
  4355. sstream,
  4356. "void %s_destroy(%s_context_t *ctx) {\n",
  4357. get_prefix(ctx), get_prefix(ctx)
  4358. );
  4359. fputs_e(
  4360. " pcc_context__destroy(ctx);\n"
  4361. "}\n",
  4362. sstream
  4363. );
  4364. }
  4365. {
  4366. fputs_e(
  4367. "#ifdef __cplusplus\n"
  4368. "extern \"C\" {\n"
  4369. "#endif\n"
  4370. "\n",
  4371. hstream
  4372. );
  4373. fprintf_e(
  4374. hstream,
  4375. "typedef struct %s_context_tag %s_context_t;\n"
  4376. "\n",
  4377. get_prefix(ctx), get_prefix(ctx)
  4378. );
  4379. fprintf_e(
  4380. hstream,
  4381. "%s_context_t *%s_create(%s%sauxil);\n",
  4382. get_prefix(ctx), get_prefix(ctx),
  4383. at, ap ? "" : " "
  4384. );
  4385. fprintf_e(
  4386. hstream,
  4387. "int %s_parse(%s_context_t *ctx, %s%s*ret);\n",
  4388. get_prefix(ctx), get_prefix(ctx),
  4389. vt, vp ? "" : " "
  4390. );
  4391. fprintf_e(
  4392. hstream,
  4393. "void %s_destroy(%s_context_t *ctx);\n",
  4394. get_prefix(ctx), get_prefix(ctx)
  4395. );
  4396. fputs_e(
  4397. "\n"
  4398. "#ifdef __cplusplus\n"
  4399. "}\n"
  4400. "#endif\n",
  4401. hstream
  4402. );
  4403. fprintf_e(
  4404. hstream,
  4405. "\n"
  4406. "#endif /* !PCC_INCLUDED_%s */\n",
  4407. ctx->hid
  4408. );
  4409. }
  4410. {
  4411. match_eol(ctx);
  4412. if (!match_eof(ctx)) fputc_e('\n', sstream);
  4413. commit_buffer(ctx);
  4414. while (refill_buffer(ctx, ctx->buffer.max) > 0) {
  4415. const size_t n = (ctx->buffer.len > 0 && ctx->buffer.buf[ctx->buffer.len - 1] == '\r') ? ctx->buffer.len - 1 : ctx->buffer.len;
  4416. write_text(sstream, ctx->buffer.buf, n);
  4417. ctx->bufcur = n;
  4418. commit_buffer(ctx);
  4419. }
  4420. }
  4421. fclose_e(hstream);
  4422. fclose_e(sstream);
  4423. if (ctx->errnum) {
  4424. unlink(ctx->hname);
  4425. unlink(ctx->sname);
  4426. return FALSE;
  4427. }
  4428. return TRUE;
  4429. }
  4430. static void print_version(FILE *output) {
  4431. fprintf(output, "%s version %s\n", g_cmdname, VERSION);
  4432. fprintf(output, "Copyright (c) 2014, 2019-2021 Arihiro Yoshida. All rights reserved.\n");
  4433. }
  4434. static void print_usage(FILE *output) {
  4435. fprintf(output, "Usage: %s [OPTIONS] [FILE]\n", g_cmdname);
  4436. fprintf(output, "Generates a packrat parser for C.\n");
  4437. fprintf(output, "\n");
  4438. fprintf(output, " -o BASENAME specify a base name of output source and header files\n");
  4439. fprintf(output, " -a, --ascii disable UTF-8 support\n");
  4440. fprintf(output, " -d, --debug with debug information\n");
  4441. fprintf(output, " -h, --help print this help message and exit\n");
  4442. fprintf(output, " -v, --version print the version and exit\n");
  4443. }
  4444. int main(int argc, char **argv) {
  4445. const char *iname = NULL;
  4446. const char *oname = NULL;
  4447. bool_t ascii = FALSE;
  4448. bool_t debug = FALSE;
  4449. /* for Linux this is not needed. */
  4450. #ifdef _MSC_VER
  4451. #ifdef _DEBUG
  4452. _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  4453. _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
  4454. _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
  4455. #endif
  4456. #endif
  4457. g_cmdname = extract_filename(argv[0]);
  4458. {
  4459. const char *fname = NULL;
  4460. const char *opt_o = NULL;
  4461. bool_t opt_a = FALSE;
  4462. bool_t opt_d = FALSE;
  4463. bool_t opt_h = FALSE;
  4464. bool_t opt_v = FALSE;
  4465. int i;
  4466. for (i = 1; i < argc; i++) {
  4467. if (argv[i][0] != '-') {
  4468. break;
  4469. }
  4470. else if (strcmp(argv[i], "--") == 0) {
  4471. i++; break;
  4472. }
  4473. else if (argv[i][1] == 'o') {
  4474. const char *const o = (argv[i][2] != '\0') ? argv[i] + 2 : (++i < argc) ? argv[i] : NULL;
  4475. if (o == NULL) {
  4476. print_error("Output base name missing\n");
  4477. fprintf(stderr, "\n");
  4478. print_usage(stderr);
  4479. exit(1);
  4480. }
  4481. if (opt_o != NULL) {
  4482. print_error("Extra output base name '%s'\n", o);
  4483. fprintf(stderr, "\n");
  4484. print_usage(stderr);
  4485. exit(1);
  4486. }
  4487. opt_o = o;
  4488. }
  4489. else if (strcmp(argv[i], "-a") == 0 || strcmp(argv[i], "--ascii") == 0) {
  4490. opt_a = TRUE;
  4491. }
  4492. else if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--debug") == 0) {
  4493. opt_d = TRUE;
  4494. }
  4495. else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
  4496. opt_h = TRUE;
  4497. }
  4498. else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) {
  4499. opt_v = TRUE;
  4500. }
  4501. else {
  4502. print_error("Invalid option '%s'\n", argv[i]);
  4503. fprintf(stderr, "\n");
  4504. print_usage(stderr);
  4505. exit(1);
  4506. }
  4507. }
  4508. switch (argc - i) {
  4509. case 0:
  4510. break;
  4511. case 1:
  4512. fname = argv[i];
  4513. break;
  4514. default:
  4515. print_error("Multiple input files\n");
  4516. fprintf(stderr, "\n");
  4517. print_usage(stderr);
  4518. exit(1);
  4519. }
  4520. if (opt_h || opt_v) {
  4521. if (opt_v) print_version(stdout);
  4522. if (opt_v && opt_h) fprintf(stdout, "\n");
  4523. if (opt_h) print_usage(stdout);
  4524. exit(0);
  4525. }
  4526. iname = (fname != NULL && fname[0] != '\0') ? fname : NULL;
  4527. oname = (opt_o != NULL && opt_o[0] != '\0') ? opt_o : NULL;
  4528. ascii = opt_a;
  4529. debug = opt_d;
  4530. }
  4531. {
  4532. context_t *const ctx = create_context(iname, oname, ascii, debug);
  4533. const int b = parse(ctx) && generate(ctx);
  4534. destroy_context(ctx);
  4535. if (!b) exit(10);
  4536. }
  4537. return 0;
  4538. }