parser.hpp 353 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922
  1. // Copyright (C) 2024 T. Zachary Laine
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_PARSER_PARSER_HPP
  7. #define BOOST_PARSER_PARSER_HPP
  8. #include <boost/parser/parser_fwd.hpp>
  9. #include <boost/parser/concepts.hpp>
  10. #include <boost/parser/error_handling.hpp>
  11. #include <boost/parser/tuple.hpp>
  12. #include <boost/parser/detail/hl.hpp>
  13. #include <boost/parser/detail/numeric.hpp>
  14. #include <boost/parser/detail/case_fold.hpp>
  15. #include <boost/parser/detail/unicode_char_sets.hpp>
  16. #include <boost/parser/detail/pp_for_each.hpp>
  17. #include <boost/parser/detail/printing.hpp>
  18. #include <boost/parser/detail/text/algorithm.hpp>
  19. #include <boost/parser/detail/text/trie_map.hpp>
  20. #include <boost/parser/detail/text/unpack.hpp>
  21. #include <type_traits>
  22. #include <variant>
  23. #include <vector>
  24. namespace boost { namespace parser {
  25. /** A placeholder type used to represent the absence of information,
  26. value, etc., inside semantic actions. For instance, calling
  27. `_locals(ctx)` in a semantic action associated with a parser that has
  28. no locals will yield a `none`. */
  29. struct none;
  30. #if defined(BOOST_PARSER_NO_RUNTIME_ASSERTIONS)
  31. struct none
  32. {};
  33. #else
  34. struct none
  35. {
  36. none() = default;
  37. // Constructible from, assignable from, and implicitly convertible to,
  38. // anything.
  39. template<typename T>
  40. none(T const &)
  41. {
  42. fail();
  43. }
  44. template<typename T>
  45. none & operator=(T const &)
  46. {
  47. fail();
  48. return *this;
  49. }
  50. template<typename T>
  51. operator T() const
  52. {
  53. fail();
  54. return T{};
  55. }
  56. // unary operators
  57. none operator+() const
  58. {
  59. fail();
  60. return none{};
  61. }
  62. none operator-() const
  63. {
  64. fail();
  65. return none{};
  66. }
  67. none operator*() const
  68. {
  69. fail();
  70. return none{};
  71. }
  72. none operator~() const
  73. {
  74. fail();
  75. return none{};
  76. }
  77. none operator&() const
  78. {
  79. fail();
  80. return none{};
  81. }
  82. none operator!() const
  83. {
  84. fail();
  85. return none{};
  86. }
  87. none operator++()
  88. {
  89. fail();
  90. return none{};
  91. }
  92. none & operator++(int)
  93. {
  94. fail();
  95. return *this;
  96. }
  97. none operator--()
  98. {
  99. fail();
  100. return none{};
  101. }
  102. none operator--(int)
  103. {
  104. fail();
  105. return *this;
  106. }
  107. // binary operators
  108. template<typename T>
  109. none operator<<(T const &) const
  110. {
  111. fail();
  112. return none{};
  113. }
  114. template<typename T>
  115. none operator>>(T const &) const
  116. {
  117. fail();
  118. return none{};
  119. }
  120. template<typename T>
  121. none operator*(T const &) const
  122. {
  123. fail();
  124. return none{};
  125. }
  126. template<typename T>
  127. none operator/(T const &) const
  128. {
  129. fail();
  130. return none{};
  131. }
  132. template<typename T>
  133. none operator%(T const &) const
  134. {
  135. fail();
  136. return none{};
  137. }
  138. template<typename T>
  139. none operator+(T const &) const
  140. {
  141. fail();
  142. return none{};
  143. }
  144. template<typename T>
  145. none operator-(T const &) const
  146. {
  147. fail();
  148. return none{};
  149. }
  150. template<typename T>
  151. none operator<(T const &) const
  152. {
  153. fail();
  154. return none{};
  155. }
  156. template<typename T>
  157. none operator>(T const &) const
  158. {
  159. fail();
  160. return none{};
  161. }
  162. template<typename T>
  163. none operator<=(T const &) const
  164. {
  165. fail();
  166. return none{};
  167. }
  168. template<typename T>
  169. none operator>=(T const &) const
  170. {
  171. fail();
  172. return none{};
  173. }
  174. template<typename T>
  175. none operator==(T const &) const
  176. {
  177. fail();
  178. return none{};
  179. }
  180. template<typename T>
  181. none operator!=(T const &) const
  182. {
  183. fail();
  184. return none{};
  185. }
  186. template<typename T>
  187. none operator||(T const &) const
  188. {
  189. fail();
  190. return none{};
  191. }
  192. template<typename T>
  193. none operator&&(T const &) const
  194. {
  195. fail();
  196. return none{};
  197. }
  198. template<typename T>
  199. none operator&(T const &) const
  200. {
  201. fail();
  202. return none{};
  203. }
  204. template<typename T>
  205. none operator|(T const &) const
  206. {
  207. fail();
  208. return none{};
  209. }
  210. template<typename T>
  211. none operator^(T const &) const
  212. {
  213. fail();
  214. return none{};
  215. }
  216. template<typename T>
  217. none operator,(T const &) const
  218. {
  219. fail();
  220. return none{};
  221. }
  222. template<typename T>
  223. none operator->*(T const &) const
  224. {
  225. fail();
  226. return none{};
  227. }
  228. template<typename T>
  229. none operator<<=(T const &)
  230. {
  231. fail();
  232. return none{};
  233. }
  234. template<typename T>
  235. none operator>>=(T const &)
  236. {
  237. fail();
  238. return none{};
  239. }
  240. template<typename T>
  241. none operator*=(T const &)
  242. {
  243. fail();
  244. return none{};
  245. }
  246. template<typename T>
  247. none operator/=(T const &)
  248. {
  249. fail();
  250. return none{};
  251. }
  252. template<typename T>
  253. none operator%=(T const &)
  254. {
  255. fail();
  256. return none{};
  257. }
  258. template<typename T>
  259. none operator+=(T const &)
  260. {
  261. fail();
  262. return none{};
  263. }
  264. template<typename T>
  265. none operator-=(T const &)
  266. {
  267. fail();
  268. return none{};
  269. }
  270. template<typename T>
  271. none operator&=(T const &)
  272. {
  273. fail();
  274. return none{};
  275. }
  276. template<typename T>
  277. none operator|=(T const &)
  278. {
  279. fail();
  280. return none{};
  281. }
  282. template<typename T>
  283. none operator^=(T const &)
  284. {
  285. fail();
  286. return none{};
  287. }
  288. template<typename T>
  289. none operator[](T const &) const
  290. {
  291. fail();
  292. return none{};
  293. }
  294. // n-ary operators
  295. template<typename... Args>
  296. none operator()(Args const &...) const
  297. {
  298. fail();
  299. return none{};
  300. }
  301. void fail() const
  302. {
  303. // If you're seeing this, you've probably gotten a `none` out of
  304. // the parse context, and are trying to use it because you think
  305. // it's something else. For instance, if your parser produces an
  306. // int attribute, the semantic ation `[](auto & ctx) { _attr(ctx)
  307. // = 0; }` may be fine. If you attach that same semantic action
  308. // to `eps`, you end up here, because `eps` has no attribute, and
  309. // so `_attr(ctx)` produces a `none`.
  310. BOOST_PARSER_DEBUG_ASSERT(false);
  311. }
  312. };
  313. #endif
  314. namespace detail {
  315. // Follows boost/mpl/print.hpp.
  316. #if defined(_MSC_VER)
  317. #pragma warning(push, 3)
  318. #pragma warning(disable : 4307)
  319. #endif
  320. #if defined(__EDG_VERSION__)
  321. namespace print_aux {
  322. template<typename T>
  323. struct dependent_unsigned
  324. {
  325. static const unsigned int value = 1;
  326. };
  327. }
  328. #endif
  329. template<typename T>
  330. struct identity_t
  331. {
  332. using type = T;
  333. };
  334. template<typename T>
  335. struct print_t : identity_t<T>
  336. {
  337. #if defined(__clang__)
  338. #pragma clang diagnostic push
  339. #pragma clang diagnostic ignored "-Wc++11-extensions"
  340. const int x_ = 1 / (sizeof(T) - sizeof(T));
  341. #pragma clang diagnostic pop
  342. #elif defined(_MSC_VER)
  343. enum { n = sizeof(T) + -1 };
  344. #elif defined(__MWERKS__)
  345. void f(int);
  346. #else
  347. enum {
  348. n =
  349. #if defined(__EDG_VERSION__)
  350. print_aux::dependent_unsigned<T>::value > -1
  351. #else
  352. sizeof(T) > -1
  353. #endif
  354. };
  355. #endif
  356. };
  357. #if defined(_MSC_VER)
  358. #pragma warning(pop)
  359. #endif
  360. template<typename T>
  361. using print_type = typename print_t<T>::type;
  362. struct null_parser
  363. {};
  364. template<typename R, typename Parser, typename SkipParser = null_parser>
  365. struct attribute_impl;
  366. // Utility types.
  367. struct nope
  368. {
  369. template<typename T>
  370. constexpr nope & operator=(T const &)
  371. {
  372. return *this;
  373. }
  374. operator std::nullopt_t() const noexcept { return std::nullopt; }
  375. template<typename Context>
  376. constexpr bool operator()(Context const &) const noexcept
  377. {
  378. return true;
  379. }
  380. constexpr nope operator*() const noexcept { return nope{}; }
  381. friend constexpr bool operator==(nope, nope) { return true; }
  382. friend constexpr bool operator!=(nope, nope) { return false; }
  383. template<typename T>
  384. friend constexpr bool operator==(T, nope)
  385. {
  386. return false;
  387. }
  388. template<typename T>
  389. friend constexpr bool operator!=(T, nope)
  390. {
  391. return false;
  392. }
  393. };
  394. inline nope const global_nope;
  395. template<typename T>
  396. using parser_interface_tag_expr =
  397. typename T::parser_interface_derivation_tag;
  398. template<typename T>
  399. constexpr bool derived_from_parser_interface_v =
  400. is_detected_v<parser_interface_tag_expr, T>;
  401. template<typename T, bool AlwaysConst = false>
  402. using nope_or_pointer_t = std::conditional_t<
  403. std::is_same_v<std::remove_const_t<T>, nope>,
  404. nope,
  405. std::conditional_t<AlwaysConst, T const *, T *>>;
  406. template<
  407. bool DoTrace,
  408. bool UseCallbacks,
  409. typename I,
  410. typename S,
  411. typename ErrorHandler,
  412. typename GlobalState = nope const,
  413. typename Callbacks = nope,
  414. typename Attr = nope,
  415. typename Val = nope,
  416. typename RuleTag = void,
  417. typename RuleLocals = nope,
  418. typename RuleParams = nope,
  419. typename Where = nope>
  420. struct parse_context
  421. {
  422. parse_context() = default;
  423. parse_context(parse_context const &) = default;
  424. parse_context & operator=(parse_context const &) = default;
  425. using rule_tag = RuleTag;
  426. static constexpr bool do_trace = DoTrace;
  427. static constexpr bool use_callbacks = UseCallbacks;
  428. I first_;
  429. S last_;
  430. bool * pass_ = nullptr;
  431. int * trace_indent_ = nullptr;
  432. symbol_table_tries_t * symbol_table_tries_ = nullptr;
  433. pending_symbol_table_operations_t *
  434. pending_symbol_table_operations_ = nullptr;
  435. ErrorHandler const * error_handler_ = nullptr;
  436. nope_or_pointer_t<GlobalState> globals_{};
  437. nope_or_pointer_t<Callbacks, true> callbacks_{};
  438. nope_or_pointer_t<Attr> attr_{};
  439. nope_or_pointer_t<Val> val_{};
  440. nope_or_pointer_t<RuleLocals> locals_{};
  441. nope_or_pointer_t<RuleParams, true> params_{};
  442. nope_or_pointer_t<Where, true> where_{};
  443. int no_case_depth_ = 0;
  444. // These exist in order to provide an address, if requested, for
  445. // either kind of symbol table struct. The nonstatic member
  446. // pointers for these will be null if this context was created
  447. // inside of detail::skip(), but nothing prevents the user from
  448. // trying to use a symbol_parser anyway. So, we have these.
  449. static std::optional<symbol_table_tries_t>
  450. empty_symbol_table_tries_;
  451. static std::optional<pending_symbol_table_operations_t>
  452. empty_pending_symbol_table_operations_;
  453. symbol_table_tries_t & get_symbol_table_tries() const
  454. {
  455. if (symbol_table_tries_)
  456. return *symbol_table_tries_;
  457. if (!empty_symbol_table_tries_)
  458. empty_symbol_table_tries_ = symbol_table_tries_t();
  459. return *empty_symbol_table_tries_;
  460. }
  461. pending_symbol_table_operations_t &
  462. get_pending_symbol_table_operations() const
  463. {
  464. if (pending_symbol_table_operations_)
  465. return *pending_symbol_table_operations_;
  466. if (!empty_pending_symbol_table_operations_) {
  467. empty_pending_symbol_table_operations_ =
  468. pending_symbol_table_operations_t();
  469. }
  470. return *empty_pending_symbol_table_operations_;
  471. }
  472. template<typename T>
  473. static auto nope_or_address(T & x)
  474. {
  475. if constexpr (std::is_same_v<std::remove_const_t<T>, nope>)
  476. return nope{};
  477. else
  478. return std::addressof(x);
  479. }
  480. template<typename T, typename U>
  481. static auto other_or_address(T other, U & x)
  482. {
  483. if constexpr (std::is_same_v<std::remove_const_t<U>, nope>)
  484. return other;
  485. else
  486. return std::addressof(x);
  487. }
  488. parse_context(
  489. std::bool_constant<DoTrace>,
  490. std::bool_constant<UseCallbacks>,
  491. I & first,
  492. S last,
  493. bool & success,
  494. int & indent,
  495. ErrorHandler const & error_handler,
  496. GlobalState & globals,
  497. symbol_table_tries_t & symbol_table_tries,
  498. pending_symbol_table_operations_t &
  499. pending_symbol_table_operations) :
  500. first_(first),
  501. last_(last),
  502. pass_(std::addressof(success)),
  503. trace_indent_(std::addressof(indent)),
  504. symbol_table_tries_(std::addressof(symbol_table_tries)),
  505. pending_symbol_table_operations_(
  506. std::addressof(pending_symbol_table_operations)),
  507. error_handler_(std::addressof(error_handler)),
  508. globals_(nope_or_address(globals))
  509. {}
  510. parse_context(
  511. std::bool_constant<DoTrace>,
  512. std::bool_constant<UseCallbacks>,
  513. I & first,
  514. S last,
  515. bool & success,
  516. int & indent,
  517. ErrorHandler const & error_handler,
  518. GlobalState & globals) :
  519. first_(first),
  520. last_(last),
  521. pass_(std::addressof(success)),
  522. trace_indent_(std::addressof(indent)),
  523. error_handler_(std::addressof(error_handler)),
  524. globals_(nope_or_address(globals))
  525. {}
  526. // With callbacks.
  527. parse_context(
  528. std::bool_constant<DoTrace>,
  529. std::bool_constant<UseCallbacks>,
  530. I & first,
  531. S last,
  532. bool & success,
  533. int & indent,
  534. ErrorHandler const & error_handler,
  535. Callbacks const & callbacks,
  536. GlobalState & globals,
  537. symbol_table_tries_t & symbol_table_tries,
  538. pending_symbol_table_operations_t &
  539. pending_symbol_table_operations) :
  540. first_(first),
  541. last_(last),
  542. pass_(std::addressof(success)),
  543. trace_indent_(std::addressof(indent)),
  544. symbol_table_tries_(std::addressof(symbol_table_tries)),
  545. pending_symbol_table_operations_(
  546. std::addressof(pending_symbol_table_operations)),
  547. error_handler_(std::addressof(error_handler)),
  548. globals_(nope_or_address(globals)),
  549. callbacks_(std::addressof(callbacks))
  550. {}
  551. // For making rule contexts.
  552. template<
  553. typename OldVal,
  554. typename OldRuleTag,
  555. typename OldRuleLocals,
  556. typename OldRuleParams,
  557. typename NewVal,
  558. typename NewRuleTag,
  559. typename NewRuleLocals,
  560. typename NewRuleParams>
  561. parse_context(
  562. parse_context<
  563. DoTrace,
  564. UseCallbacks,
  565. I,
  566. S,
  567. ErrorHandler,
  568. GlobalState,
  569. Callbacks,
  570. Attr,
  571. OldVal,
  572. OldRuleTag,
  573. OldRuleLocals,
  574. OldRuleParams> const & other,
  575. NewRuleTag * tag_ptr,
  576. NewVal & value,
  577. NewRuleLocals & locals,
  578. NewRuleParams const & params) :
  579. first_(other.first_),
  580. last_(other.last_),
  581. pass_(other.pass_),
  582. trace_indent_(other.trace_indent_),
  583. symbol_table_tries_(other.symbol_table_tries_),
  584. pending_symbol_table_operations_(
  585. other.pending_symbol_table_operations_),
  586. error_handler_(other.error_handler_),
  587. globals_(other.globals_),
  588. callbacks_(other.callbacks_),
  589. attr_(other.attr_),
  590. no_case_depth_(other.no_case_depth_)
  591. {
  592. if constexpr (
  593. std::is_same_v<OldRuleTag, NewRuleTag> &&
  594. !std::is_same_v<OldRuleTag, void>) {
  595. val_ = other.val_;
  596. locals_ = other.locals_;
  597. params_ = other.params_;
  598. } else {
  599. val_ = other_or_address(other.val_, value);
  600. locals_ = other_or_address(other.locals_, locals);
  601. params_ = other_or_address(other.params_, params);
  602. }
  603. }
  604. // For making action contexts.
  605. template<typename OldAttr, typename OldWhere>
  606. parse_context(
  607. parse_context<
  608. DoTrace,
  609. UseCallbacks,
  610. I,
  611. S,
  612. ErrorHandler,
  613. GlobalState,
  614. Callbacks,
  615. OldAttr,
  616. Val,
  617. RuleTag,
  618. RuleLocals,
  619. RuleParams,
  620. OldWhere> const & other,
  621. Attr & attr,
  622. Where const & where) :
  623. first_(other.first_),
  624. last_(other.last_),
  625. pass_(other.pass_),
  626. trace_indent_(other.trace_indent_),
  627. symbol_table_tries_(other.symbol_table_tries_),
  628. pending_symbol_table_operations_(
  629. other.pending_symbol_table_operations_),
  630. error_handler_(other.error_handler_),
  631. globals_(other.globals_),
  632. callbacks_(other.callbacks_),
  633. attr_(nope_or_address(attr)),
  634. val_(other.val_),
  635. locals_(other.locals_),
  636. params_(other.params_),
  637. where_(nope_or_address(where)),
  638. no_case_depth_(other.no_case_depth_)
  639. {}
  640. };
  641. template<
  642. bool DoTrace,
  643. bool UseCallbacks,
  644. typename I,
  645. typename S,
  646. typename ErrorHandler,
  647. typename GlobalState,
  648. typename Callbacks,
  649. typename Attr,
  650. typename Val,
  651. typename RuleTag,
  652. typename RuleLocals,
  653. typename RuleParams,
  654. typename Where>
  655. std::optional<symbol_table_tries_t> parse_context<
  656. DoTrace,
  657. UseCallbacks,
  658. I,
  659. S,
  660. ErrorHandler,
  661. GlobalState,
  662. Callbacks,
  663. Attr,
  664. Val,
  665. RuleTag,
  666. RuleLocals,
  667. RuleParams,
  668. Where>::empty_symbol_table_tries_;
  669. template<
  670. bool DoTrace,
  671. bool UseCallbacks,
  672. typename I,
  673. typename S,
  674. typename ErrorHandler,
  675. typename GlobalState,
  676. typename Callbacks,
  677. typename Attr,
  678. typename Val,
  679. typename RuleTag,
  680. typename RuleLocals,
  681. typename RuleParams,
  682. typename Where>
  683. std::optional<pending_symbol_table_operations_t> parse_context<
  684. DoTrace,
  685. UseCallbacks,
  686. I,
  687. S,
  688. ErrorHandler,
  689. GlobalState,
  690. Callbacks,
  691. Attr,
  692. Val,
  693. RuleTag,
  694. RuleLocals,
  695. RuleParams,
  696. Where>::empty_pending_symbol_table_operations_;
  697. template<
  698. bool DoTrace,
  699. bool UseCallbacks,
  700. typename I,
  701. typename S,
  702. typename ErrorHandler,
  703. typename GlobalState,
  704. typename Callbacks,
  705. typename Val,
  706. typename RuleTag,
  707. typename RuleLocals,
  708. typename RuleParams,
  709. typename Attr,
  710. typename Where,
  711. typename OldAttr>
  712. auto make_action_context(
  713. parse_context<
  714. DoTrace,
  715. UseCallbacks,
  716. I,
  717. S,
  718. ErrorHandler,
  719. GlobalState,
  720. Callbacks,
  721. OldAttr,
  722. Val,
  723. RuleTag,
  724. RuleLocals,
  725. RuleParams> const & context,
  726. Attr & attr,
  727. Where const & where)
  728. {
  729. using result_type = parse_context<
  730. DoTrace,
  731. UseCallbacks,
  732. I,
  733. S,
  734. ErrorHandler,
  735. GlobalState,
  736. Callbacks,
  737. Attr,
  738. Val,
  739. RuleTag,
  740. RuleLocals,
  741. RuleParams,
  742. Where>;
  743. return result_type(context, attr, where);
  744. }
  745. template<
  746. bool DoTrace,
  747. bool UseCallbacks,
  748. typename I,
  749. typename S,
  750. typename ErrorHandler,
  751. typename GlobalState,
  752. typename Callbacks,
  753. typename Attr,
  754. typename Val,
  755. typename RuleTag,
  756. typename RuleLocals,
  757. typename RuleParams,
  758. typename NewVal,
  759. typename NewRuleTag,
  760. typename NewRuleLocals,
  761. typename NewRuleParams>
  762. auto make_rule_context(
  763. parse_context<
  764. DoTrace,
  765. UseCallbacks,
  766. I,
  767. S,
  768. ErrorHandler,
  769. GlobalState,
  770. Callbacks,
  771. Attr,
  772. Val,
  773. RuleTag,
  774. RuleLocals,
  775. RuleParams> const & context,
  776. NewRuleTag * tag_ptr,
  777. NewVal & value,
  778. NewRuleLocals & locals,
  779. NewRuleParams const & params)
  780. {
  781. using result_type = parse_context<
  782. DoTrace,
  783. UseCallbacks,
  784. I,
  785. S,
  786. ErrorHandler,
  787. GlobalState,
  788. Callbacks,
  789. Attr,
  790. std::conditional_t<std::is_same_v<NewVal, nope>, Val, NewVal>,
  791. NewRuleTag,
  792. std::conditional_t<
  793. std::is_same_v<NewRuleLocals, nope>,
  794. RuleLocals,
  795. NewRuleLocals>,
  796. std::conditional_t<
  797. std::is_same_v<NewRuleParams, nope>,
  798. RuleParams,
  799. NewRuleParams>>;
  800. return result_type(context, tag_ptr, value, locals, params);
  801. }
  802. template<
  803. bool DoTrace,
  804. bool UseCallbacks,
  805. typename Iter,
  806. typename Sentinel,
  807. typename ErrorHandler>
  808. auto make_context(
  809. Iter first,
  810. Sentinel last,
  811. bool & success,
  812. int & indent,
  813. ErrorHandler const & error_handler,
  814. nope const & n,
  815. symbol_table_tries_t & symbol_table_tries,
  816. pending_symbol_table_operations_t &
  817. pending_symbol_table_operations) noexcept
  818. {
  819. return parse_context(
  820. std::bool_constant<DoTrace>{},
  821. std::bool_constant<UseCallbacks>{},
  822. first,
  823. last,
  824. success,
  825. indent,
  826. error_handler,
  827. n,
  828. symbol_table_tries,
  829. pending_symbol_table_operations);
  830. }
  831. template<
  832. bool DoTrace,
  833. bool UseCallbacks,
  834. typename Iter,
  835. typename Sentinel,
  836. typename ErrorHandler>
  837. auto make_context(
  838. Iter first,
  839. Sentinel last,
  840. bool & success,
  841. int & indent,
  842. ErrorHandler const & error_handler,
  843. nope const & n,
  844. nope const &,
  845. nope const &) noexcept
  846. {
  847. return parse_context(
  848. std::bool_constant<DoTrace>{},
  849. std::bool_constant<UseCallbacks>{},
  850. first,
  851. last,
  852. success,
  853. indent,
  854. error_handler,
  855. n);
  856. }
  857. template<
  858. bool DoTrace,
  859. bool UseCallbacks,
  860. typename Iter,
  861. typename Sentinel,
  862. typename ErrorHandler,
  863. typename GlobalState>
  864. auto make_context(
  865. Iter first,
  866. Sentinel last,
  867. bool & success,
  868. int & indent,
  869. ErrorHandler const & error_handler,
  870. GlobalState & globals,
  871. symbol_table_tries_t & symbol_table_tries,
  872. pending_symbol_table_operations_t &
  873. pending_symbol_table_operations) noexcept
  874. {
  875. return parse_context(
  876. std::bool_constant<DoTrace>{},
  877. std::bool_constant<UseCallbacks>{},
  878. first,
  879. last,
  880. success,
  881. indent,
  882. error_handler,
  883. globals,
  884. symbol_table_tries,
  885. pending_symbol_table_operations);
  886. }
  887. template<
  888. bool DoTrace,
  889. bool UseCallbacks,
  890. typename Iter,
  891. typename Sentinel,
  892. typename ErrorHandler,
  893. typename Callbacks>
  894. auto make_context(
  895. Iter first,
  896. Sentinel last,
  897. bool & success,
  898. int & indent,
  899. ErrorHandler const & error_handler,
  900. Callbacks const & callbacks,
  901. nope & n,
  902. symbol_table_tries_t & symbol_table_tries,
  903. pending_symbol_table_operations_t &
  904. pending_symbol_table_operations) noexcept
  905. {
  906. return parse_context(
  907. std::bool_constant<DoTrace>{},
  908. std::bool_constant<UseCallbacks>{},
  909. first,
  910. last,
  911. success,
  912. indent,
  913. error_handler,
  914. callbacks,
  915. n,
  916. symbol_table_tries,
  917. pending_symbol_table_operations);
  918. }
  919. template<
  920. bool DoTrace,
  921. bool UseCallbacks,
  922. typename Iter,
  923. typename Sentinel,
  924. typename ErrorHandler,
  925. typename Callbacks,
  926. typename GlobalState>
  927. auto make_context(
  928. Iter first,
  929. Sentinel last,
  930. bool & success,
  931. int & indent,
  932. ErrorHandler const & error_handler,
  933. Callbacks const & callbacks,
  934. GlobalState & globals,
  935. symbol_table_tries_t & symbol_table_tries,
  936. pending_symbol_table_operations_t &
  937. pending_symbol_table_operations) noexcept
  938. {
  939. return parse_context(
  940. std::bool_constant<DoTrace>{},
  941. std::bool_constant<UseCallbacks>{},
  942. first,
  943. last,
  944. success,
  945. indent,
  946. error_handler,
  947. callbacks,
  948. globals,
  949. symbol_table_tries,
  950. pending_symbol_table_operations);
  951. }
  952. template<unsigned int I>
  953. struct param_t
  954. {
  955. template<typename Context>
  956. decltype(auto) operator()(Context const & context) const
  957. {
  958. return parser::get(parser::_params(context), llong<I>{});
  959. }
  960. };
  961. template<typename T, typename... Args>
  962. using callable = decltype(std::declval<T>()(std::declval<Args>()...));
  963. template<
  964. typename Context,
  965. typename T,
  966. bool Callable = is_detected_v<callable, T const &, Context const &>>
  967. struct resolve_impl
  968. {
  969. static auto call(Context const &, T const & x) { return x; }
  970. };
  971. template<typename Context, typename T>
  972. struct resolve_impl<Context, T, true>
  973. {
  974. static auto call(Context const & context, T const & x)
  975. {
  976. return x(context);
  977. }
  978. };
  979. template<typename Context, typename T>
  980. auto resolve(Context const & context, T const & x)
  981. {
  982. return resolve_impl<Context, T>::call(context, x);
  983. }
  984. template<typename Context>
  985. auto resolve(Context const &, nope n)
  986. {
  987. return n;
  988. }
  989. template<typename Context, typename ParamsTuple>
  990. auto
  991. resolve_rule_params(Context const & context, ParamsTuple const & params)
  992. {
  993. return detail::hl::transform(params, [&](auto const & x) {
  994. return detail::resolve(context, x);
  995. });
  996. }
  997. template<typename Context>
  998. nope resolve_rule_params(Context const & context, nope)
  999. {
  1000. return {};
  1001. }
  1002. template<typename LocalsType, typename Context>
  1003. LocalsType make_locals_impl(Context const & context, std::true_type)
  1004. {
  1005. return LocalsType(context);
  1006. }
  1007. template<typename LocalsType, typename Context>
  1008. LocalsType make_locals_impl(Context const & context, std::false_type)
  1009. {
  1010. return LocalsType();
  1011. }
  1012. template<typename LocalsType, typename Context>
  1013. LocalsType make_locals(Context const & context)
  1014. {
  1015. return detail::make_locals_impl<LocalsType>(
  1016. context,
  1017. typename std::is_convertible<Context const &, LocalsType>::
  1018. type{});
  1019. }
  1020. template<typename Context>
  1021. decltype(auto) _indent(Context const & context)
  1022. {
  1023. return *context.trace_indent_;
  1024. }
  1025. template<typename Context>
  1026. decltype(auto) _callbacks(Context const & context)
  1027. {
  1028. return *context.callbacks_;
  1029. }
  1030. // Type traits.
  1031. template<typename T>
  1032. using remove_cv_ref_t = typename std::remove_cv<
  1033. typename std::remove_reference<T>::type>::type;
  1034. template<typename T, typename U>
  1035. using comparison = decltype(std::declval<T>() == std::declval<U>());
  1036. template<typename T, typename U>
  1037. constexpr bool is_equality_comparable_with_v =
  1038. is_detected_v<comparison, T, U>;
  1039. template<typename T>
  1040. struct is_nope : std::false_type
  1041. {};
  1042. template<>
  1043. struct is_nope<nope> : std::true_type
  1044. {};
  1045. template<typename T>
  1046. constexpr bool is_nope_v = is_nope<remove_cv_ref_t<T>>::value;
  1047. template<typename T>
  1048. struct is_eps_p : std::false_type
  1049. {};
  1050. template<typename T>
  1051. struct is_eps_p<eps_parser<T>> : std::true_type
  1052. {};
  1053. template<typename T>
  1054. struct is_unconditional_eps : std::false_type
  1055. {};
  1056. template<>
  1057. struct is_unconditional_eps<eps_parser<nope>> : std::true_type
  1058. {};
  1059. template<typename T>
  1060. constexpr bool is_unconditional_eps_v =
  1061. is_unconditional_eps<remove_cv_ref_t<T>>::value;
  1062. template<typename T>
  1063. struct is_zero_plus_p : std::false_type
  1064. {};
  1065. template<typename T>
  1066. struct is_zero_plus_p<zero_plus_parser<T>> : std::true_type
  1067. {};
  1068. template<typename T>
  1069. struct is_or_p : std::false_type
  1070. {};
  1071. template<typename T>
  1072. struct is_or_p<or_parser<T>> : std::true_type
  1073. {};
  1074. template<typename T>
  1075. struct is_perm_p : std::false_type
  1076. {};
  1077. template<typename T, typename DelimiterParser>
  1078. struct is_perm_p<perm_parser<T, DelimiterParser>> : std::true_type
  1079. {};
  1080. template<typename T>
  1081. struct is_seq_p : std::false_type
  1082. {};
  1083. template<typename T, typename U, typename V>
  1084. struct is_seq_p<seq_parser<T, U, V>> : std::true_type
  1085. {};
  1086. template<typename T>
  1087. struct is_one_plus_p : std::false_type
  1088. {};
  1089. template<typename T>
  1090. struct is_one_plus_p<one_plus_parser<T>> : std::true_type
  1091. {};
  1092. template<typename T>
  1093. struct is_utf8_view : std::false_type
  1094. {};
  1095. template<typename V>
  1096. struct is_utf8_view<text::utf8_view<V>> : std::true_type
  1097. {};
  1098. template<typename T>
  1099. using optional_type = remove_cv_ref_t<decltype(*std::declval<T &>())>;
  1100. template<typename F, typename... Args>
  1101. constexpr bool is_invocable_v = is_detected_v<callable, F, Args...>;
  1102. template<typename T>
  1103. using has_begin =
  1104. decltype(*detail::text::detail::begin(std::declval<T &>()));
  1105. template<typename T>
  1106. using has_end =
  1107. decltype(detail::text::detail::end(std::declval<T &>()));
  1108. template<typename T>
  1109. constexpr bool is_range =
  1110. is_detected_v<has_begin, T> && is_detected_v<has_end, T>;
  1111. template<typename T>
  1112. using has_push_back =
  1113. decltype(std::declval<T &>().push_back(*std::declval<T>().begin()));
  1114. #if BOOST_PARSER_USE_CONCEPTS
  1115. template<typename T>
  1116. using iterator_t = std::ranges::iterator_t<T>;
  1117. template<typename T>
  1118. using sentinel_t = std::ranges::sentinel_t<T>;
  1119. template<typename T>
  1120. using iter_value_t = std::iter_value_t<T>;
  1121. template<typename T>
  1122. using iter_reference_t = std::iter_reference_t<T>;
  1123. template<typename T>
  1124. using range_value_t = std::ranges::range_value_t<T>;
  1125. template<typename T>
  1126. using range_reference_t = std::ranges::range_reference_t<T>;
  1127. template<typename T>
  1128. using range_rvalue_reference_t =
  1129. std::ranges::range_rvalue_reference_t<T>;
  1130. template<typename T>
  1131. constexpr bool is_parsable_code_unit_v = code_unit<T>;
  1132. #else
  1133. template<typename T>
  1134. using iterator_t =
  1135. decltype(detail::text::detail::begin(std::declval<T &>()));
  1136. template<typename Range>
  1137. using sentinel_t =
  1138. decltype(detail::text::detail::end(std::declval<Range &>()));
  1139. template<typename T>
  1140. using iter_value_t = typename std::iterator_traits<T>::value_type;
  1141. template<typename T>
  1142. using iter_reference_t = decltype(*std::declval<T &>());
  1143. template<typename T>
  1144. using iter_rvalue_reference_t =
  1145. decltype(std::move(*std::declval<T &>()));
  1146. template<typename T>
  1147. using range_value_t = iter_value_t<iterator_t<T>>;
  1148. template<typename T>
  1149. using range_reference_t = iter_reference_t<iterator_t<T>>;
  1150. template<typename T>
  1151. using range_rvalue_reference_t = iter_rvalue_reference_t<iterator_t<T>>;
  1152. template<typename T>
  1153. using has_insert = decltype(std::declval<T &>().insert(
  1154. std::declval<T>().begin(), *std::declval<T>().begin()));
  1155. template<typename T>
  1156. using has_range_insert = decltype(std::declval<T &>().insert(
  1157. std::declval<T>().begin(),
  1158. std::declval<T>().begin(),
  1159. std::declval<T>().end()));
  1160. template<typename T>
  1161. constexpr bool is_container_v = is_detected_v<has_insert, T>;
  1162. template<typename T, typename U>
  1163. constexpr bool container_and_value_type =
  1164. is_container_v<T> &&
  1165. (std::is_same_v<detected_t<range_value_t, T>, U> ||
  1166. (std::is_same_v<T, std::string> && std::is_same_v<U, char32_t>));
  1167. template<typename T>
  1168. constexpr bool is_parsable_code_unit_impl =
  1169. std::is_same_v<T, char> || std::is_same_v<T, wchar_t> ||
  1170. #if defined(__cpp_char8_t)
  1171. std::is_same_v<T, char8_t> ||
  1172. #endif
  1173. std::is_same_v<T, char16_t> || std::is_same_v<T, char32_t>;
  1174. template<typename T>
  1175. constexpr bool is_parsable_code_unit_v =
  1176. is_parsable_code_unit_impl<std::remove_cv_t<T>>;
  1177. template<typename T>
  1178. constexpr bool is_parsable_iter_v = is_parsable_code_unit_v<
  1179. remove_cv_ref_t<detected_t<iter_value_t, T>>>;
  1180. template<typename T>
  1181. constexpr bool is_parsable_range_v = is_parsable_code_unit_v<
  1182. remove_cv_ref_t<detected_t<has_begin, T>>> &&
  1183. is_detected_v<has_end, T>;
  1184. template<typename T>
  1185. constexpr bool is_parsable_pointer_v =
  1186. std::is_pointer_v<remove_cv_ref_t<T>> && is_parsable_code_unit_v<
  1187. std::remove_pointer_t<remove_cv_ref_t<T>>>;
  1188. template<typename T>
  1189. constexpr bool is_parsable_range_like_v =
  1190. is_parsable_range_v<T> || is_parsable_pointer_v<T>;
  1191. }
  1192. template<typename T>
  1193. constexpr bool container = detail::is_container_v<T>;
  1194. namespace detail {
  1195. #endif
  1196. template<typename T, bool = std::is_pointer_v<T>>
  1197. constexpr bool is_code_unit_pointer_v = false;
  1198. template<typename T>
  1199. constexpr bool is_code_unit_pointer_v<T, true> =
  1200. is_parsable_code_unit_v<std::remove_pointer_t<T>>;
  1201. template<typename T>
  1202. constexpr bool is_range_like = is_range<T> || is_code_unit_pointer_v<T>;
  1203. template<typename I>
  1204. constexpr bool is_char8_iter_v =
  1205. #if defined(__cpp_char8_t)
  1206. std::is_same_v<iter_value_t<I>, char8_t>
  1207. #else
  1208. false
  1209. #endif
  1210. ;
  1211. // Metafunctions.
  1212. template<bool WrapInOptional, typename Tuple>
  1213. struct to_hana_tuple_or_type_impl;
  1214. template<typename... T>
  1215. struct to_hana_tuple_or_type_impl<true, tuple<T...>>
  1216. {
  1217. using type = std::optional<std::variant<T...>>;
  1218. };
  1219. template<typename... T>
  1220. struct to_hana_tuple_or_type_impl<false, tuple<T...>>
  1221. {
  1222. using type = std::variant<T...>;
  1223. };
  1224. template<typename T>
  1225. struct to_hana_tuple_or_type_impl<true, tuple<T>>
  1226. {
  1227. // The reason this is not two separate specializations, one
  1228. // for tuple<t> and on for tuple<optional<T>>, is because
  1229. // MSVC.
  1230. using type =
  1231. std::conditional_t<is_optional_v<T>, T, std::optional<T>>;
  1232. };
  1233. template<typename T>
  1234. struct to_hana_tuple_or_type_impl<false, tuple<T>>
  1235. {
  1236. using type = T;
  1237. };
  1238. template<>
  1239. struct to_hana_tuple_or_type_impl<true, tuple<>>
  1240. {
  1241. using type = nope;
  1242. };
  1243. template<>
  1244. struct to_hana_tuple_or_type_impl<false, tuple<>>
  1245. {
  1246. using type = nope;
  1247. };
  1248. template<typename Pair>
  1249. struct to_hana_tuple_or_type;
  1250. template<typename Tuple, typename TrueFalse>
  1251. struct to_hana_tuple_or_type<tuple<Tuple, TrueFalse>>
  1252. {
  1253. // This has to be done in two steps like this because MSVC.
  1254. using type =
  1255. typename to_hana_tuple_or_type_impl<TrueFalse::value, Tuple>::
  1256. type;
  1257. };
  1258. template<typename T>
  1259. using to_hana_tuple_or_type_t = typename to_hana_tuple_or_type<T>::type;
  1260. template<typename T>
  1261. auto make_sequence_of()
  1262. {
  1263. if constexpr (
  1264. std::is_same_v<T, char> || std::is_same_v<T, char32_t>) {
  1265. return std::string{};
  1266. } else if constexpr (std::is_same_v<T, nope>) {
  1267. return nope{};
  1268. } else {
  1269. return std::vector<T>{};
  1270. }
  1271. }
  1272. template<typename T>
  1273. constexpr bool is_char_type_v =
  1274. std::is_same_v<T, char> || std::is_same_v<T, char32_t>;
  1275. template<typename T>
  1276. struct optional_of_impl
  1277. {
  1278. using type = std::optional<T>;
  1279. };
  1280. template<typename T>
  1281. struct optional_of_impl<std::optional<T>>
  1282. {
  1283. using type = std::optional<T>;
  1284. };
  1285. template<>
  1286. struct optional_of_impl<nope>
  1287. {
  1288. using type = nope;
  1289. };
  1290. template<typename T>
  1291. using optional_of = typename optional_of_impl<T>::type;
  1292. template<typename T>
  1293. struct unwrapped_optional
  1294. {
  1295. using type = T;
  1296. };
  1297. template<typename T>
  1298. struct unwrapped_optional<std::optional<T>>
  1299. {
  1300. using type = T;
  1301. };
  1302. template<typename T>
  1303. using unwrapped_optional_t = typename unwrapped_optional<T>::type;
  1304. // Etc.
  1305. template<typename T>
  1306. struct wrapper
  1307. {
  1308. using type = T;
  1309. constexpr bool operator==(wrapper) const { return true; }
  1310. };
  1311. struct wrap
  1312. {
  1313. template<typename T>
  1314. constexpr auto operator()(T type) const
  1315. {
  1316. return wrapper<T>{};
  1317. }
  1318. };
  1319. struct unwrap
  1320. {
  1321. template<typename T>
  1322. constexpr auto operator()(T wrapped_type) const
  1323. {
  1324. return typename T::type{};
  1325. }
  1326. };
  1327. template<typename Container, typename T>
  1328. void insert(Container & c, T && x)
  1329. {
  1330. if constexpr (is_detected_v<has_push_back, Container>) {
  1331. c.push_back((T &&) x);
  1332. } else {
  1333. c.insert((T &&) x);
  1334. }
  1335. }
  1336. template<typename Container, typename I>
  1337. void insert(Container & c, I first, I last)
  1338. {
  1339. std::for_each(first, last, [&](auto && x) {
  1340. using type = decltype(x);
  1341. insert(c, (type &&) x);
  1342. });
  1343. }
  1344. template<typename Container, typename T>
  1345. constexpr bool needs_transcoding_to_utf8 =
  1346. (std::is_same_v<range_value_t<remove_cv_ref_t<Container>>, char>
  1347. #if defined(__cpp_char8_t)
  1348. || std::is_same_v<range_value_t<remove_cv_ref_t<Container>>, char8_t>
  1349. #endif
  1350. ) && (std::is_same_v<remove_cv_ref_t<T>, char32_t>
  1351. #if !defined(_MSC_VER)
  1352. || std::is_same_v<remove_cv_ref_t<T>, wchar_t>
  1353. #endif
  1354. );
  1355. template<typename Container, typename T>
  1356. void append(Container & c, T && x, bool gen_attrs)
  1357. {
  1358. if (!gen_attrs)
  1359. return;
  1360. if constexpr (needs_transcoding_to_utf8<Container, T>) {
  1361. char32_t cps[1] = {(char32_t)x};
  1362. auto const r = cps | text::as_utf8;
  1363. c.insert(c.end(), r.begin(), r.end());
  1364. } else {
  1365. detail::insert(c, (T &&)x);
  1366. }
  1367. }
  1368. template<typename Container, typename T>
  1369. void append(std::optional<Container> & c, T && x, bool gen_attrs)
  1370. {
  1371. if (!gen_attrs)
  1372. return;
  1373. if (!c)
  1374. c = Container();
  1375. return detail::append(*c, (T &&) x, gen_attrs);
  1376. }
  1377. template<typename Container>
  1378. void append(Container & c, nope &&, bool)
  1379. {}
  1380. template<typename T>
  1381. void append(nope &, T &&, bool)
  1382. {}
  1383. inline void append(nope &, nope &&, bool) {}
  1384. template<typename Container, typename Iter, typename Sentinel>
  1385. void append(Container & c, Iter first, Sentinel last, bool gen_attrs)
  1386. {
  1387. if (!gen_attrs)
  1388. return;
  1389. if constexpr (needs_transcoding_to_utf8<
  1390. Container,
  1391. iter_value_t<Iter>>) {
  1392. auto const r =
  1393. BOOST_PARSER_SUBRANGE(first, last) | text::as_utf8;
  1394. c.insert(c.end(), r.begin(), r.end());
  1395. } else {
  1396. detail::insert(c, first, last);
  1397. }
  1398. }
  1399. template<typename Container, typename Iter, typename Sentinel>
  1400. void append(
  1401. std::optional<Container> & c,
  1402. Iter first,
  1403. Sentinel last,
  1404. bool gen_attrs)
  1405. {
  1406. if (!gen_attrs)
  1407. return;
  1408. if (!c)
  1409. c = Container();
  1410. return detail::append(*c, first, last, gen_attrs);
  1411. }
  1412. template<typename Iter, typename Sentinel>
  1413. void append(nope &, Iter first, Sentinel last, bool gen_attrs)
  1414. {}
  1415. constexpr inline flags default_flags()
  1416. {
  1417. return flags(
  1418. uint32_t(flags::gen_attrs) | uint32_t(flags::use_skip));
  1419. }
  1420. constexpr inline flags enable_skip(flags f)
  1421. {
  1422. return flags(uint32_t(f) | uint32_t(flags::use_skip));
  1423. }
  1424. constexpr inline flags disable_skip(flags f)
  1425. {
  1426. return flags(uint32_t(f) & ~uint32_t(flags::use_skip));
  1427. }
  1428. constexpr inline flags enable_attrs(flags f)
  1429. {
  1430. return flags(uint32_t(f) | uint32_t(flags::gen_attrs));
  1431. }
  1432. constexpr inline flags disable_attrs(flags f)
  1433. {
  1434. return flags(uint32_t(f) & ~uint32_t(flags::gen_attrs));
  1435. }
  1436. constexpr inline flags enable_trace(flags f)
  1437. {
  1438. return flags(uint32_t(f) | uint32_t(flags::trace));
  1439. }
  1440. constexpr inline flags disable_trace(flags f)
  1441. {
  1442. return flags(uint32_t(f) & ~uint32_t(flags::trace));
  1443. }
  1444. constexpr inline flags set_in_apply_parser(flags f)
  1445. {
  1446. return flags(uint32_t(f) | uint32_t(flags::in_apply_parser));
  1447. }
  1448. constexpr inline bool gen_attrs(flags f)
  1449. {
  1450. return (uint32_t(f) & uint32_t(flags::gen_attrs)) ==
  1451. uint32_t(flags::gen_attrs);
  1452. }
  1453. constexpr inline bool use_skip(flags f)
  1454. {
  1455. return (uint32_t(f) & uint32_t(flags::use_skip)) ==
  1456. uint32_t(flags::use_skip);
  1457. }
  1458. constexpr inline bool in_apply_parser(flags f)
  1459. {
  1460. return (uint32_t(f) & uint32_t(flags::in_apply_parser)) ==
  1461. uint32_t(flags::in_apply_parser);
  1462. }
  1463. struct skip_skipper
  1464. {
  1465. template<
  1466. typename Iter,
  1467. typename Sentinel,
  1468. typename Context,
  1469. typename SkipParser>
  1470. nope operator()(
  1471. Iter & first,
  1472. Sentinel last,
  1473. Context const & context,
  1474. SkipParser const & skip,
  1475. flags flags,
  1476. bool & success) const noexcept
  1477. {
  1478. return {};
  1479. }
  1480. template<
  1481. typename Iter,
  1482. typename Sentinel,
  1483. typename Context,
  1484. typename SkipParser,
  1485. typename Attribute>
  1486. void operator()(
  1487. Iter & first,
  1488. Sentinel last,
  1489. Context const & context,
  1490. SkipParser const & skip,
  1491. flags flags,
  1492. bool & success,
  1493. Attribute & retval) const
  1494. {}
  1495. };
  1496. template<typename Iter, typename Sentinel>
  1497. void
  1498. skip(Iter & first, Sentinel last, null_parser const & skip_, flags f)
  1499. {}
  1500. template<typename Iter, typename Sentinel, typename SkipParser>
  1501. void
  1502. skip(Iter & first, Sentinel last, SkipParser const & skip_, flags f)
  1503. {
  1504. if (!detail::use_skip(f))
  1505. return;
  1506. bool success = true;
  1507. int indent = 0;
  1508. rethrow_error_handler eh;
  1509. nope const n;
  1510. auto const context = detail::make_context<false, false>(
  1511. first, last, success, indent, eh, n, n, n);
  1512. while (success) {
  1513. skip_(
  1514. first,
  1515. last,
  1516. context,
  1517. skip_skipper{},
  1518. detail::disable_trace(detail::disable_skip(f)),
  1519. success);
  1520. }
  1521. }
  1522. enum : int64_t { unbounded = -1 };
  1523. template<typename T>
  1524. std::optional<T> make_parse_result(T & x, bool success)
  1525. {
  1526. std::optional<T> retval;
  1527. if (success)
  1528. retval = std::move(x);
  1529. return retval;
  1530. }
  1531. inline bool make_parse_result(nope &, bool success) { return success; }
  1532. inline bool make_parse_result(none &, bool success) { return success; }
  1533. template<typename LoType, typename HiType>
  1534. struct char_pair
  1535. {
  1536. LoType lo_;
  1537. HiType hi_;
  1538. };
  1539. using case_fold_array_t = std::array<char32_t, detail::longest_mapping>;
  1540. template<typename I, typename S>
  1541. struct no_case_iter : stl_interfaces::proxy_iterator_interface<
  1542. no_case_iter<I, S>,
  1543. std::forward_iterator_tag,
  1544. char32_t>
  1545. {
  1546. no_case_iter() : it_(), last_(), idx_(0), last_idx_() {}
  1547. no_case_iter(I it, S last) :
  1548. it_(it), last_(last), idx_(0), last_idx_(0)
  1549. {
  1550. fold();
  1551. }
  1552. char32_t operator*() const { return folded_[idx_]; }
  1553. no_case_iter & operator++()
  1554. {
  1555. ++idx_;
  1556. if (last_idx_ <= idx_) {
  1557. ++it_;
  1558. fold();
  1559. }
  1560. return *this;
  1561. }
  1562. I base() const { return it_; }
  1563. friend bool operator==(no_case_iter lhs, S rhs) noexcept
  1564. {
  1565. return lhs.it_ == rhs;
  1566. }
  1567. friend bool operator==(no_case_iter lhs, no_case_iter rhs) noexcept
  1568. {
  1569. return lhs.it_ == rhs.it_ && lhs.idx_ == rhs.idx_;
  1570. }
  1571. using base_type = stl_interfaces::proxy_iterator_interface<
  1572. no_case_iter<I, S>,
  1573. std::forward_iterator_tag,
  1574. char32_t>;
  1575. using base_type::operator++;
  1576. private:
  1577. void fold()
  1578. {
  1579. idx_ = 0;
  1580. if (it_ == last_) {
  1581. folded_[0] = 0;
  1582. last_idx_ = 1;
  1583. return;
  1584. }
  1585. auto const folded_last =
  1586. detail::case_fold(*it_, folded_.begin());
  1587. last_idx_ = int(folded_last - folded_.begin());
  1588. }
  1589. case_fold_array_t folded_;
  1590. I it_;
  1591. [[no_unique_address]] S last_;
  1592. int idx_;
  1593. int last_idx_;
  1594. };
  1595. template<typename V>
  1596. struct case_fold_view
  1597. {
  1598. using iterator =
  1599. no_case_iter<detail::iterator_t<V>, detail::sentinel_t<V>>;
  1600. case_fold_view(V base) : base_(std::move(base)) {}
  1601. iterator begin() const
  1602. {
  1603. return iterator(
  1604. text::detail::begin(base_), text::detail::end(base_));
  1605. }
  1606. auto end() const { return text::detail::end(base_); }
  1607. private:
  1608. V base_;
  1609. };
  1610. enum class symbol_table_op { insert, erase, clear };
  1611. template<typename T>
  1612. struct symbol_table_operation
  1613. {
  1614. std::string key_;
  1615. std::optional<T> value_;
  1616. symbol_table_op kind_;
  1617. };
  1618. template<typename T>
  1619. void apply_symbol_table_operations(
  1620. std::vector<std::pair<std::string, T>> & initial_elements,
  1621. std::vector<symbol_table_operation<T>> & pending_operations)
  1622. {
  1623. auto lower_bound = [&initial_elements](std::string const & str) {
  1624. return std::lower_bound(
  1625. initial_elements.begin(),
  1626. initial_elements.end(),
  1627. str,
  1628. [](auto const & a, auto b) {
  1629. return a.first < b;
  1630. });
  1631. };
  1632. for (auto & op : pending_operations) {
  1633. if (op.kind_ == symbol_table_op::insert) {
  1634. auto it = lower_bound(op.key_);
  1635. if (it == initial_elements.end() ||
  1636. it->first != op.key_) {
  1637. initial_elements.insert(
  1638. it,
  1639. std::pair<std::string, T>(
  1640. std::move(op.key_), std::move(*op.value_)));
  1641. } else {
  1642. it->second = std::move(*op.value_);
  1643. }
  1644. } else if (op.kind_ == symbol_table_op::erase) {
  1645. auto it = lower_bound(op.key_);
  1646. if (it != initial_elements.end() && it->first == op.key_)
  1647. initial_elements.erase(it);
  1648. } else {
  1649. initial_elements.clear();
  1650. }
  1651. }
  1652. pending_operations.clear();
  1653. }
  1654. template<typename Context, typename T>
  1655. auto get_trie(
  1656. Context const & context, symbol_parser<T> const & sym_parser)
  1657. {
  1658. using trie_t = text::trie_map<std::vector<char32_t>, T>;
  1659. using result_type = std::pair<trie_t &, bool>;
  1660. symbol_table_tries_t & symbol_table_tries =
  1661. context.get_symbol_table_tries();
  1662. auto & [any, has_case_folded] =
  1663. symbol_table_tries[(void *)&sym_parser.ref()];
  1664. bool const needs_case_folded = context.no_case_depth_;
  1665. if (!any.has_value()) {
  1666. any = trie_t{};
  1667. has_case_folded = false;
  1668. trie_t & trie = *std::any_cast<trie_t>(&any);
  1669. for (auto const & e : sym_parser.initial_elements()) {
  1670. trie.insert(e.first | text::as_utf32, e.second);
  1671. if (needs_case_folded) {
  1672. trie.insert(
  1673. case_fold_view(e.first | text::as_utf32), e.second);
  1674. has_case_folded = true;
  1675. }
  1676. }
  1677. return result_type(trie, has_case_folded);
  1678. } else {
  1679. trie_t & trie = *std::any_cast<trie_t>(&any);
  1680. if (needs_case_folded && !has_case_folded) {
  1681. trie_t new_trie = trie;
  1682. for (auto && [key, value] : trie) {
  1683. new_trie.insert(
  1684. case_fold_view(key | text::as_utf32), value);
  1685. }
  1686. std::swap(new_trie, trie);
  1687. }
  1688. return result_type(trie, has_case_folded);
  1689. }
  1690. }
  1691. template<typename Context, typename T>
  1692. decltype(auto) get_pending_symtab_ops(
  1693. Context const & context, symbol_parser<T> const & sym_parser)
  1694. {
  1695. void const * ptr = static_cast<void const *>(&sym_parser);
  1696. auto & entry = (context.get_pending_symbol_table_operations())[ptr];
  1697. std::vector<detail::symbol_table_operation<T>> * retval = nullptr;
  1698. if (entry.visit_) {
  1699. retval = std::any_cast<
  1700. std::vector<detail::symbol_table_operation<T>>>(
  1701. &entry.ops_);
  1702. } else {
  1703. entry.ops_ = std::vector<detail::symbol_table_operation<T>>{};
  1704. retval = std::any_cast<
  1705. std::vector<detail::symbol_table_operation<T>>>(
  1706. &entry.ops_);
  1707. entry.visit_ = [&sym_parser, ops_ptr = retval] {
  1708. detail::apply_symbol_table_operations(
  1709. sym_parser.initial_elements_, *ops_ptr);
  1710. };
  1711. }
  1712. return *retval;
  1713. }
  1714. template<>
  1715. struct char_subranges<hex_digit_subranges>
  1716. {
  1717. static constexpr char_subrange ranges[] = {
  1718. {U'0', U'9'},
  1719. {U'A', U'F'},
  1720. {U'a', U'f'},
  1721. {U'\uff10', U'\uff19'},
  1722. {U'\uff21', U'\uff26'},
  1723. {U'\uff41', U'\uff46'}};
  1724. };
  1725. template<>
  1726. struct char_subranges<control_subranges>
  1727. {
  1728. static constexpr char_subrange ranges[] = {
  1729. {U'\u0000', U'\u001f'}, {U'\u007f', U'\u009f'}};
  1730. };
  1731. template<typename Iter, typename Sentinel, bool SortedUTF32>
  1732. struct char_range
  1733. {
  1734. template<typename T, typename Context>
  1735. bool contains(T c_, Context const & context) const
  1736. {
  1737. if constexpr (SortedUTF32) {
  1738. return std::binary_search(chars_.begin(), chars_.end(), c_);
  1739. }
  1740. if (context.no_case_depth_) {
  1741. case_fold_array_t folded;
  1742. auto folded_last = detail::case_fold(c_, folded.begin());
  1743. if constexpr (std::is_same_v<T, char32_t>) {
  1744. auto const cps = chars_ | text::as_utf32;
  1745. auto chars_first = no_case_iter(cps.begin(), cps.end());
  1746. auto chars_last = cps.end();
  1747. auto result = text::search(
  1748. chars_first,
  1749. chars_last,
  1750. folded.begin(),
  1751. folded_last);
  1752. return !result.empty();
  1753. } else {
  1754. auto chars_first =
  1755. no_case_iter(chars_.begin(), chars_.end());
  1756. auto chars_last = chars_.end();
  1757. auto result = text::search(
  1758. chars_first,
  1759. chars_last,
  1760. folded.begin(),
  1761. folded_last);
  1762. return !result.empty();
  1763. }
  1764. } else {
  1765. if constexpr (std::is_same_v<T, char32_t>) {
  1766. auto const cps = chars_ | text::as_utf32;
  1767. return text::find(cps.begin(), cps.end(), c_) !=
  1768. cps.end();
  1769. } else {
  1770. using element_type =
  1771. remove_cv_ref_t<decltype(*chars_.begin())>;
  1772. element_type const c = c_;
  1773. return text::find(chars_.begin(), chars_.end(), c) !=
  1774. chars_.end();
  1775. }
  1776. }
  1777. }
  1778. BOOST_PARSER_SUBRANGE<Iter, Sentinel> chars_;
  1779. };
  1780. template<bool SortedUTF32, typename Iter, typename Sentinel>
  1781. constexpr auto make_char_range(Iter first, Sentinel last)
  1782. {
  1783. return char_range<Iter, Sentinel, SortedUTF32>{
  1784. BOOST_PARSER_SUBRANGE<Iter, Sentinel>{first, last}};
  1785. }
  1786. template<bool SortedUTF32, typename R>
  1787. constexpr auto make_char_range(R && r) noexcept
  1788. {
  1789. if constexpr (std::is_pointer_v<remove_cv_ref_t<R>>) {
  1790. return detail::make_char_range<SortedUTF32>(
  1791. r, text::null_sentinel);
  1792. } else {
  1793. return detail::make_char_range<SortedUTF32>(
  1794. text::detail::begin(r), text::detail::end(r));
  1795. }
  1796. }
  1797. template<bool Equal, typename Context>
  1798. auto no_case_aware_compare(Context const & context)
  1799. {
  1800. return [no_case = context.no_case_depth_](char32_t a, char32_t b) {
  1801. if (no_case) {
  1802. case_fold_array_t folded_a = {0, 0, 0};
  1803. detail::case_fold(a, folded_a.begin());
  1804. case_fold_array_t folded_b = {0, 0, 0};
  1805. detail::case_fold(b, folded_b.begin());
  1806. return Equal ? folded_a == folded_b : folded_a < folded_b;
  1807. } else {
  1808. return Equal ? a == b : a < b;
  1809. }
  1810. };
  1811. }
  1812. template<typename T, typename U>
  1813. constexpr bool both_character_types =
  1814. is_parsable_code_unit_v<T> && is_parsable_code_unit_v<U>;
  1815. template<typename T, typename U>
  1816. using eq_comparable =
  1817. decltype(std::declval<T &>() == std::declval<U &>());
  1818. template<
  1819. typename Context,
  1820. typename CharType,
  1821. typename Expected,
  1822. bool BothCharacters = both_character_types<CharType, Expected>>
  1823. struct unequal_impl
  1824. {
  1825. static bool
  1826. call(Context const & context, CharType c, Expected const & expected)
  1827. {
  1828. auto resolved = detail::resolve(context, expected);
  1829. if constexpr (is_detected_v<
  1830. eq_comparable,
  1831. CharType,
  1832. decltype(resolved)>) {
  1833. auto const compare =
  1834. detail::no_case_aware_compare<true>(context);
  1835. return !compare(c, resolved);
  1836. } else {
  1837. return !resolved.contains(c, context);
  1838. }
  1839. }
  1840. };
  1841. template<typename Context, typename CharType, typename Expected>
  1842. struct unequal_impl<Context, CharType, Expected, true>
  1843. {
  1844. static bool
  1845. call(Context const & context, CharType c, Expected expected)
  1846. {
  1847. return !detail::no_case_aware_compare<true>(context)(
  1848. c, expected);
  1849. }
  1850. };
  1851. template<typename Context, typename CharType, typename Expected>
  1852. bool unequal(Context const & context, CharType c, Expected expected)
  1853. {
  1854. return unequal_impl<Context, CharType, Expected>::call(
  1855. context, c, expected);
  1856. }
  1857. template<
  1858. typename Context,
  1859. typename CharType,
  1860. typename LoType,
  1861. typename HiType>
  1862. bool unequal(
  1863. Context const & context,
  1864. CharType c,
  1865. char_pair<LoType, HiType> const & expected)
  1866. {
  1867. auto const less = detail::no_case_aware_compare<false>(context);
  1868. {
  1869. auto lo = detail::resolve(context, expected.lo_);
  1870. if (less(c, lo))
  1871. return true;
  1872. }
  1873. {
  1874. auto hi = detail::resolve(context, expected.hi_);
  1875. if (less(hi, c))
  1876. return true;
  1877. }
  1878. return false;
  1879. }
  1880. template<typename Context, typename CharType>
  1881. bool unequal(Context const &, CharType, nope)
  1882. {
  1883. return false;
  1884. }
  1885. template<typename Container, typename T>
  1886. using insertable = decltype(std::declval<Container &>().insert(
  1887. std::declval<Container &>().end(), std::declval<T>()));
  1888. template<typename T, typename Tuple, int... Is>
  1889. auto
  1890. make_from_tuple_impl(Tuple && tup, std::integer_sequence<int, Is...>)
  1891. -> decltype(T(parser::get((Tuple &&)tup, llong<Is>{})...))
  1892. {
  1893. return T(parser::get((Tuple &&)tup, llong<Is>{})...);
  1894. }
  1895. template<typename T, typename... Args>
  1896. auto make_from_tuple(tuple<Args...> && tup)
  1897. -> decltype(detail::make_from_tuple_impl<T>(
  1898. std::move(tup),
  1899. std::make_integer_sequence<int, tuple_size_<tuple<Args...>>>()))
  1900. {
  1901. return detail::make_from_tuple_impl<T>(
  1902. std::move(tup),
  1903. std::make_integer_sequence<int, tuple_size_<tuple<Args...>>>());
  1904. }
  1905. template<typename T, typename Tuple>
  1906. using constructible_from_tuple_expr =
  1907. decltype(detail::make_from_tuple<T>(std::declval<Tuple>()));
  1908. template<typename T, typename Tuple, bool = is_tuple<Tuple>{}>
  1909. constexpr bool is_constructible_from_tuple_v = false;
  1910. template<typename T, typename Tuple>
  1911. constexpr bool is_constructible_from_tuple_v<T, Tuple, true> =
  1912. is_detected_v<constructible_from_tuple_expr, T, Tuple>;
  1913. template<typename Container, typename U>
  1914. constexpr void move_back_impl(Container & c, U && x)
  1915. {
  1916. using just_t = range_value_t<Container>;
  1917. using just_u = remove_cv_ref_t<U>;
  1918. if constexpr (needs_transcoding_to_utf8<Container, U>) {
  1919. char32_t cps[1] = {(char32_t)x};
  1920. auto const r = cps | text::as_utf8;
  1921. c.insert(c.end(), r.begin(), r.end());
  1922. } else if constexpr (std::is_convertible_v<just_u &&, just_t>) {
  1923. detail::insert(c, (U &&)x);
  1924. } else if constexpr (
  1925. !is_tuple<just_t>::value && is_tuple<just_u>::value &&
  1926. std::is_aggregate_v<just_t> &&
  1927. !is_detected_v<insertable, Container, just_u &&> &&
  1928. is_struct_assignable_v<just_t, just_u>) {
  1929. auto int_seq =
  1930. std::make_integer_sequence<int, tuple_size_<just_u>>();
  1931. detail::insert(
  1932. c, detail::tuple_to_aggregate<just_t>((U &&)x, int_seq));
  1933. } else if constexpr (
  1934. is_tuple<just_t>::value && !is_tuple<just_u>::value &&
  1935. std::is_aggregate_v<just_u> &&
  1936. !is_detected_v<insertable, Container, just_u &&> &&
  1937. is_tuple_assignable_v<just_t, just_u>) {
  1938. just_t t;
  1939. auto tie = detail::tie_aggregate(x);
  1940. detail::aggregate_to_tuple(
  1941. t,
  1942. tie,
  1943. std::make_integer_sequence<int, tuple_size_<just_t>>());
  1944. detail::insert(c, std::move(t));
  1945. } else if constexpr (is_constructible_from_tuple_v<
  1946. just_t,
  1947. just_u>) {
  1948. detail::insert(c, detail::make_from_tuple<just_t>((U &&)x));
  1949. } else {
  1950. static_assert(
  1951. sizeof(U) && false,
  1952. "Could not insert value into container, by: just inserting "
  1953. "it; doing tuple -> aggregate or aggregate -> tuple "
  1954. "conversions; or tuple -> class type construction.");
  1955. }
  1956. }
  1957. template<typename Container, typename T>
  1958. constexpr void move_back(Container & c, T && x, bool gen_attrs)
  1959. {
  1960. if (!gen_attrs)
  1961. return;
  1962. detail::move_back_impl(c, (T &&)x);
  1963. }
  1964. template<typename Container>
  1965. constexpr void move_back(Container & c, Container & x, bool gen_attrs)
  1966. {
  1967. if (!gen_attrs)
  1968. return;
  1969. c.insert(c.end(), x.begin(), x.end());
  1970. }
  1971. template<typename Container>
  1972. constexpr void
  1973. move_back(Container & c, std::optional<Container> && x, bool gen_attrs)
  1974. {
  1975. if (!gen_attrs || !x)
  1976. return;
  1977. c.insert(c.end(), x->begin(), x->end());
  1978. }
  1979. template<typename Container, typename T>
  1980. constexpr void
  1981. move_back(Container & c, std::optional<T> & x, bool gen_attrs)
  1982. {
  1983. if (!gen_attrs || !x)
  1984. return;
  1985. detail::move_back_impl(c, std::move(*x));
  1986. }
  1987. template<
  1988. typename Container,
  1989. typename T,
  1990. typename Enable = std::enable_if_t<!std::is_same_v<Container, T>>>
  1991. constexpr void
  1992. move_back(Container & c, std::optional<T> && x, bool gen_attrs)
  1993. {
  1994. if (!gen_attrs || !x)
  1995. return;
  1996. detail::move_back_impl(c, std::move(*x));
  1997. }
  1998. constexpr void move_back(nope, nope, bool gen_attrs) {}
  1999. template<typename Container>
  2000. constexpr void move_back(Container & c, nope, bool gen_attrs)
  2001. {}
  2002. template<typename From, typename To>
  2003. using move_assignable_expr =
  2004. decltype(std::declval<To &>() = std::declval<From &&>());
  2005. template<typename From, typename To>
  2006. constexpr bool is_move_assignable_v =
  2007. is_detected_v<move_assignable_expr, From, To>;
  2008. template<typename T, typename U>
  2009. constexpr void assign(T & t, U && u)
  2010. {
  2011. using just_t = remove_cv_ref_t<T>;
  2012. using just_u = remove_cv_ref_t<U>;
  2013. if constexpr (is_move_assignable_v<just_u, just_t>) {
  2014. static_assert(
  2015. (!std::is_same_v<just_t, std::string> ||
  2016. !std::is_arithmetic_v<just_u>),
  2017. "std::string is assignable from a char. Due to implicit "
  2018. "conversions among arithmetic types, any arithmetic type "
  2019. "(like int or double) is assignable to std::string. This "
  2020. "is almost certainly not what you meant to write, so "
  2021. "Boost.Parser disallows it. If you want to do this, write "
  2022. "a semantic action and do it explicitly.");
  2023. t = (U &&)u;
  2024. } else if constexpr (
  2025. !is_tuple<just_t>::value && is_tuple<just_u>::value &&
  2026. std::is_aggregate_v<just_t> &&
  2027. !std::is_convertible_v<just_u &&, just_t> &&
  2028. is_struct_assignable_v<just_t, just_u>) {
  2029. auto int_seq =
  2030. std::make_integer_sequence<int, tuple_size_<just_u>>();
  2031. t = detail::tuple_to_aggregate<just_t>((U &&)u, int_seq);
  2032. } else if constexpr (
  2033. is_tuple<just_t>::value && !is_tuple<just_u>::value &&
  2034. std::is_aggregate_v<just_u> &&
  2035. !std::is_convertible_v<just_u &&, just_t> &&
  2036. is_tuple_assignable_v<just_t, just_u>) {
  2037. auto tie = detail::tie_aggregate(u);
  2038. detail::aggregate_to_tuple(
  2039. t,
  2040. tie,
  2041. std::make_integer_sequence<int, tuple_size_<just_t>>());
  2042. } else if constexpr (is_constructible_from_tuple_v<
  2043. just_t,
  2044. just_u>) {
  2045. t = detail::make_from_tuple<just_t>((U &&)u);
  2046. } else {
  2047. static_assert(
  2048. sizeof(T) && false,
  2049. "Could not assign value, by: just assigning it; doing tuple "
  2050. "-> aggregate or aggregate -> tuple conversions; or tuple "
  2051. "-> class type construction.");
  2052. }
  2053. }
  2054. template<typename T>
  2055. constexpr void assign(T &, nope)
  2056. {}
  2057. template<typename T, typename U>
  2058. constexpr void assign_copy(T & t, U const & u)
  2059. {
  2060. t = u;
  2061. }
  2062. template<typename T>
  2063. constexpr void assign_copy(T &, nope)
  2064. {}
  2065. template<
  2066. typename Parser,
  2067. typename Iter,
  2068. typename Sentinel,
  2069. typename Context,
  2070. typename SkipParser,
  2071. typename... T>
  2072. void apply_parser(
  2073. Parser const & parser,
  2074. Iter & first,
  2075. Sentinel last,
  2076. Context const & context,
  2077. SkipParser const & skip,
  2078. flags flags,
  2079. bool & success,
  2080. std::optional<std::variant<T...>> & retval)
  2081. {
  2082. using attr_t = decltype(parser.call(
  2083. first, last, context, skip, flags, success));
  2084. if constexpr (std::is_same<
  2085. attr_t,
  2086. std::optional<std::variant<T...>>>{}) {
  2087. parser.call(first, last, context, skip, flags, success, retval);
  2088. } else if constexpr (is_nope_v<attr_t>) {
  2089. parser.call(first, last, context, skip, flags, success);
  2090. } else {
  2091. auto attr =
  2092. parser.call(first, last, context, skip, flags, success);
  2093. if (success)
  2094. detail::assign(retval, std::variant<T...>(std::move(attr)));
  2095. }
  2096. }
  2097. template<
  2098. typename Parser,
  2099. typename Iter,
  2100. typename Sentinel,
  2101. typename Context,
  2102. typename SkipParser,
  2103. typename... T>
  2104. void apply_parser(
  2105. Parser const & parser,
  2106. Iter & first,
  2107. Sentinel last,
  2108. Context const & context,
  2109. SkipParser const & skip,
  2110. flags flags,
  2111. bool & success,
  2112. std::variant<T...> & retval)
  2113. {
  2114. auto attr = parser.call(first, last, context, skip, flags, success);
  2115. if (success)
  2116. detail::assign(retval, std::move(attr));
  2117. }
  2118. template<
  2119. typename Parser,
  2120. typename Iter,
  2121. typename Sentinel,
  2122. typename Context,
  2123. typename SkipParser,
  2124. typename T>
  2125. void apply_parser(
  2126. Parser const & parser,
  2127. Iter & first,
  2128. Sentinel last,
  2129. Context const & context,
  2130. SkipParser const & skip,
  2131. flags flags,
  2132. bool & success,
  2133. std::optional<T> & retval)
  2134. {
  2135. auto attr = parser.call(first, last, context, skip, flags, success);
  2136. if (success)
  2137. detail::assign(retval, std::move(attr));
  2138. }
  2139. template<
  2140. typename Parser,
  2141. typename Iter,
  2142. typename Sentinel,
  2143. typename Context,
  2144. typename SkipParser,
  2145. typename Attribute>
  2146. void apply_parser(
  2147. Parser const & parser,
  2148. Iter & first,
  2149. Sentinel last,
  2150. Context const & context,
  2151. SkipParser const & skip,
  2152. flags flags,
  2153. bool & success,
  2154. Attribute & retval)
  2155. {
  2156. parser.call(first, last, context, skip, flags, success, retval);
  2157. }
  2158. // API implementations
  2159. template<typename Iter, typename Sentinel, typename Parser>
  2160. auto has_attribute(Iter first, Sentinel last, Parser parser);
  2161. template<typename BaseIter, typename Iter>
  2162. struct scoped_base_assign
  2163. {
  2164. scoped_base_assign(BaseIter & base, Iter & it) :
  2165. base_(base), it_(it)
  2166. {}
  2167. ~scoped_base_assign() { base_ = it_.base(); }
  2168. BaseIter & base_;
  2169. Iter & it_;
  2170. };
  2171. template<typename Parser>
  2172. using has_parser_data_member_expr =
  2173. decltype(std::declval<Parser>().parser_);
  2174. template<typename Parser>
  2175. constexpr bool has_parser_data_member_v =
  2176. is_detected_v<has_parser_data_member_expr, Parser>;
  2177. template<typename Parser>
  2178. using has_parsers_data_member_expr =
  2179. decltype(std::declval<Parser>().parsers_);
  2180. template<typename Parser>
  2181. constexpr bool has_parsers_data_member_v =
  2182. is_detected_v<has_parsers_data_member_expr, Parser>;
  2183. struct scoped_apply_pending_symbol_table_operations
  2184. {
  2185. scoped_apply_pending_symbol_table_operations(
  2186. pending_symbol_table_operations_t & pending_ops) :
  2187. pending_ops_(pending_ops)
  2188. {}
  2189. ~scoped_apply_pending_symbol_table_operations()
  2190. {
  2191. for (auto & [_, entry] : pending_ops_) {
  2192. entry.visit_();
  2193. }
  2194. }
  2195. pending_symbol_table_operations_t & pending_ops_;
  2196. };
  2197. template<
  2198. bool Debug,
  2199. typename Iter,
  2200. typename Sentinel,
  2201. typename Parser,
  2202. typename Attr,
  2203. typename ErrorHandler>
  2204. bool parse_impl(
  2205. Iter & first,
  2206. Sentinel last,
  2207. Parser const & parser,
  2208. ErrorHandler const & error_handler,
  2209. Attr & attr)
  2210. {
  2211. auto const initial_first = first;
  2212. bool success = true;
  2213. int trace_indent = 0;
  2214. detail::symbol_table_tries_t symbol_table_tries;
  2215. pending_symbol_table_operations_t pending_symbol_table_operations;
  2216. scoped_apply_pending_symbol_table_operations apply_pending(
  2217. pending_symbol_table_operations);
  2218. auto context = detail::make_context<Debug, false>(
  2219. first,
  2220. last,
  2221. success,
  2222. trace_indent,
  2223. error_handler,
  2224. parser.globals_,
  2225. symbol_table_tries,
  2226. pending_symbol_table_operations);
  2227. auto const flags =
  2228. Debug ? detail::enable_trace(detail::flags::gen_attrs)
  2229. : detail::flags::gen_attrs;
  2230. try {
  2231. parser(
  2232. first,
  2233. last,
  2234. context,
  2235. detail::null_parser{},
  2236. flags,
  2237. success,
  2238. attr);
  2239. if (Debug)
  2240. detail::final_trace(context, flags, attr);
  2241. return success;
  2242. } catch (parse_error<Iter> const & e) {
  2243. if (error_handler(initial_first, last, e) ==
  2244. error_handler_result::rethrow) {
  2245. throw;
  2246. }
  2247. return false;
  2248. }
  2249. }
  2250. template<
  2251. bool Debug,
  2252. typename Iter,
  2253. typename Sentinel,
  2254. typename Parser,
  2255. typename ErrorHandler>
  2256. auto parse_impl(
  2257. Iter & first,
  2258. Sentinel last,
  2259. Parser const & parser,
  2260. ErrorHandler const & error_handler)
  2261. {
  2262. auto const initial_first = first;
  2263. bool success = true;
  2264. int trace_indent = 0;
  2265. detail::symbol_table_tries_t symbol_table_tries;
  2266. pending_symbol_table_operations_t pending_symbol_table_operations;
  2267. scoped_apply_pending_symbol_table_operations apply_pending(
  2268. pending_symbol_table_operations);
  2269. auto context = detail::make_context<Debug, false>(
  2270. first,
  2271. last,
  2272. success,
  2273. trace_indent,
  2274. error_handler,
  2275. parser.globals_,
  2276. symbol_table_tries,
  2277. pending_symbol_table_operations);
  2278. auto const flags =
  2279. Debug ? detail::enable_trace(detail::flags::gen_attrs)
  2280. : detail::flags::gen_attrs;
  2281. using attr_t = typename detail::attribute_impl<
  2282. BOOST_PARSER_SUBRANGE<std::remove_const_t<Iter>, Sentinel>,
  2283. Parser>::type;
  2284. try {
  2285. attr_t attr_ = parser(
  2286. first,
  2287. last,
  2288. context,
  2289. detail::null_parser{},
  2290. flags,
  2291. success);
  2292. if (Debug)
  2293. detail::final_trace(context, flags, nope{});
  2294. return detail::make_parse_result(attr_, success);
  2295. } catch (parse_error<Iter> const & e) {
  2296. if (error_handler(initial_first, last, e) ==
  2297. error_handler_result::rethrow) {
  2298. throw;
  2299. }
  2300. attr_t attr_{};
  2301. return detail::make_parse_result(attr_, false);
  2302. }
  2303. }
  2304. template<
  2305. bool Debug,
  2306. typename Iter,
  2307. typename Sentinel,
  2308. typename Parser,
  2309. typename ErrorHandler,
  2310. typename Callbacks>
  2311. bool callback_parse_impl(
  2312. Iter & first,
  2313. Sentinel last,
  2314. Parser const & parser,
  2315. ErrorHandler const & error_handler,
  2316. Callbacks const & callbacks)
  2317. {
  2318. auto const initial_first = first;
  2319. bool success = true;
  2320. int trace_indent = 0;
  2321. detail::symbol_table_tries_t symbol_table_tries;
  2322. pending_symbol_table_operations_t pending_symbol_table_operations;
  2323. scoped_apply_pending_symbol_table_operations apply_pending(
  2324. pending_symbol_table_operations);
  2325. auto context = detail::make_context<Debug, true>(
  2326. first,
  2327. last,
  2328. success,
  2329. trace_indent,
  2330. error_handler,
  2331. callbacks,
  2332. parser.globals_,
  2333. symbol_table_tries,
  2334. pending_symbol_table_operations);
  2335. auto const flags =
  2336. Debug ? detail::enable_trace(detail::flags::gen_attrs)
  2337. : detail::flags::gen_attrs;
  2338. try {
  2339. parser(
  2340. first,
  2341. last,
  2342. context,
  2343. detail::null_parser{},
  2344. flags,
  2345. success);
  2346. if (Debug)
  2347. detail::final_trace(context, flags, nope{});
  2348. return success;
  2349. } catch (parse_error<Iter> const & e) {
  2350. if (error_handler(initial_first, last, e) ==
  2351. error_handler_result::rethrow) {
  2352. throw;
  2353. }
  2354. return false;
  2355. }
  2356. }
  2357. template<
  2358. bool Debug,
  2359. typename Iter,
  2360. typename Sentinel,
  2361. typename Parser,
  2362. typename SkipParser,
  2363. typename Attr,
  2364. typename ErrorHandler>
  2365. bool skip_parse_impl(
  2366. Iter & first,
  2367. Sentinel last,
  2368. Parser const & parser,
  2369. SkipParser const & skip,
  2370. ErrorHandler const & error_handler,
  2371. Attr & attr)
  2372. {
  2373. auto const initial_first = first;
  2374. bool success = true;
  2375. int trace_indent = 0;
  2376. detail::symbol_table_tries_t symbol_table_tries;
  2377. pending_symbol_table_operations_t pending_symbol_table_operations;
  2378. scoped_apply_pending_symbol_table_operations apply_pending(
  2379. pending_symbol_table_operations);
  2380. auto context = detail::make_context<Debug, false>(
  2381. first,
  2382. last,
  2383. success,
  2384. trace_indent,
  2385. error_handler,
  2386. parser.globals_,
  2387. symbol_table_tries,
  2388. pending_symbol_table_operations);
  2389. auto const flags =
  2390. Debug ? detail::enable_trace(detail::default_flags())
  2391. : detail::default_flags();
  2392. detail::skip(first, last, skip, flags);
  2393. try {
  2394. parser(first, last, context, skip, flags, success, attr);
  2395. detail::skip(first, last, skip, flags);
  2396. if (Debug)
  2397. detail::final_trace(context, flags, attr);
  2398. return success;
  2399. } catch (parse_error<Iter> const & e) {
  2400. if (error_handler(initial_first, last, e) ==
  2401. error_handler_result::rethrow) {
  2402. throw;
  2403. }
  2404. return false;
  2405. }
  2406. }
  2407. template<
  2408. bool Debug,
  2409. typename Iter,
  2410. typename Sentinel,
  2411. typename Parser,
  2412. typename SkipParser,
  2413. typename ErrorHandler>
  2414. auto skip_parse_impl(
  2415. Iter & first,
  2416. Sentinel last,
  2417. Parser const & parser,
  2418. SkipParser const & skip,
  2419. ErrorHandler const & error_handler)
  2420. {
  2421. auto const initial_first = first;
  2422. bool success = true;
  2423. int trace_indent = 0;
  2424. detail::symbol_table_tries_t symbol_table_tries;
  2425. pending_symbol_table_operations_t pending_symbol_table_operations;
  2426. scoped_apply_pending_symbol_table_operations apply_pending(
  2427. pending_symbol_table_operations);
  2428. auto context = detail::make_context<Debug, false>(
  2429. first,
  2430. last,
  2431. success,
  2432. trace_indent,
  2433. error_handler,
  2434. parser.globals_,
  2435. symbol_table_tries,
  2436. pending_symbol_table_operations);
  2437. auto const flags =
  2438. Debug ? detail::enable_trace(detail::default_flags())
  2439. : detail::default_flags();
  2440. detail::skip(first, last, skip, flags);
  2441. using attr_t = typename detail::attribute_impl<
  2442. BOOST_PARSER_SUBRANGE<std::remove_const_t<Iter>, Sentinel>,
  2443. Parser, SkipParser>::type;
  2444. try {
  2445. attr_t attr_ =
  2446. parser(first, last, context, skip, flags, success);
  2447. detail::skip(first, last, skip, flags);
  2448. if (Debug)
  2449. detail::final_trace(context, flags, nope{});
  2450. return detail::make_parse_result(attr_, success);
  2451. } catch (parse_error<Iter> const & e) {
  2452. if (error_handler(initial_first, last, e) ==
  2453. error_handler_result::rethrow) {
  2454. throw;
  2455. }
  2456. attr_t attr_{};
  2457. return detail::make_parse_result(attr_, false);
  2458. }
  2459. }
  2460. template<
  2461. bool Debug,
  2462. typename Iter,
  2463. typename Sentinel,
  2464. typename Parser,
  2465. typename SkipParser,
  2466. typename ErrorHandler,
  2467. typename Callbacks>
  2468. bool callback_skip_parse_impl(
  2469. Iter & first,
  2470. Sentinel last,
  2471. Parser const & parser,
  2472. SkipParser const & skip,
  2473. ErrorHandler const & error_handler,
  2474. Callbacks const & callbacks)
  2475. {
  2476. auto const initial_first = first;
  2477. bool success = true;
  2478. int trace_indent = 0;
  2479. detail::symbol_table_tries_t symbol_table_tries;
  2480. pending_symbol_table_operations_t pending_symbol_table_operations;
  2481. scoped_apply_pending_symbol_table_operations apply_pending(
  2482. pending_symbol_table_operations);
  2483. auto context = detail::make_context<Debug, true>(
  2484. first,
  2485. last,
  2486. success,
  2487. trace_indent,
  2488. error_handler,
  2489. callbacks,
  2490. parser.globals_,
  2491. symbol_table_tries,
  2492. pending_symbol_table_operations);
  2493. auto const flags =
  2494. Debug ? detail::enable_trace(detail::default_flags())
  2495. : detail::default_flags();
  2496. detail::skip(first, last, skip, flags);
  2497. try {
  2498. parser(first, last, context, skip, flags, success);
  2499. detail::skip(first, last, skip, flags);
  2500. if (Debug)
  2501. detail::final_trace(context, flags, nope{});
  2502. return success;
  2503. } catch (parse_error<Iter> const & e) {
  2504. if (error_handler(initial_first, last, e) ==
  2505. error_handler_result::rethrow) {
  2506. throw;
  2507. }
  2508. return false;
  2509. }
  2510. }
  2511. template<typename R>
  2512. constexpr auto make_input_subrange(R && r) noexcept
  2513. {
  2514. using r_t = remove_cv_ref_t<R>;
  2515. if constexpr (std::is_pointer_v<r_t>) {
  2516. using value_type = iter_value_t<r_t>;
  2517. if constexpr (std::is_same_v<value_type, char>) {
  2518. return BOOST_PARSER_SUBRANGE(r, text::null_sentinel);
  2519. } else {
  2520. return r | text::as_utf32;
  2521. }
  2522. } else {
  2523. using value_type = range_value_t<r_t>;
  2524. if constexpr (text::detail::is_bounded_array_v<r_t>) {
  2525. if constexpr (std::is_same_v<value_type, char>) {
  2526. auto first = detail::text::detail::begin(r);
  2527. auto last = detail::text::detail::end(r);
  2528. if (first != last && !*std::prev(last))
  2529. --last;
  2530. return BOOST_PARSER_SUBRANGE(first, last);
  2531. } else {
  2532. return r | text::as_utf32;
  2533. }
  2534. } else {
  2535. if constexpr (
  2536. std::is_same_v<value_type, char> &&
  2537. !is_utf8_view<r_t>::value) {
  2538. return BOOST_PARSER_SUBRANGE(
  2539. detail::text::detail::begin(r),
  2540. detail::text::detail::end(r));
  2541. } else {
  2542. return r | text::as_utf32;
  2543. }
  2544. }
  2545. }
  2546. }
  2547. template<typename R>
  2548. constexpr auto make_view_begin(R & r) noexcept
  2549. {
  2550. if constexpr (std::is_pointer_v<std::decay_t<R>>) {
  2551. return r;
  2552. } else {
  2553. return detail::text::detail::begin(r);
  2554. }
  2555. }
  2556. template<typename R>
  2557. constexpr auto make_view_end(R & r) noexcept
  2558. {
  2559. if constexpr (std::is_pointer_v<std::decay_t<R>>) {
  2560. return text::null_sentinel;
  2561. } else {
  2562. return detail::text::detail::end(r);
  2563. }
  2564. }
  2565. template<
  2566. typename Iter1,
  2567. typename Sentinel1,
  2568. typename Iter2,
  2569. typename Sentinel2,
  2570. typename Pred>
  2571. std::pair<Iter1, Iter2> mismatch(
  2572. Iter1 first1,
  2573. Sentinel1 last1,
  2574. Iter2 first2,
  2575. Sentinel2 last2,
  2576. Pred pred)
  2577. {
  2578. std::pair<Iter1, Iter2> retval{first1, first2};
  2579. while (retval.first != last1 && retval.second != last2 &&
  2580. pred(*retval.first, *retval.second)) {
  2581. ++retval.first;
  2582. ++retval.second;
  2583. }
  2584. return retval;
  2585. }
  2586. template<
  2587. typename Iter1,
  2588. typename Sentinel1,
  2589. typename Iter2,
  2590. typename Sentinel2>
  2591. std::pair<Iter1, Iter2> no_case_aware_string_mismatch(
  2592. Iter1 first1,
  2593. Sentinel1 last1,
  2594. Iter2 first2,
  2595. Sentinel2 last2,
  2596. bool no_case)
  2597. {
  2598. if (no_case) {
  2599. auto it1 = no_case_iter(first1, last1);
  2600. auto it2 = no_case_iter(first2, last2);
  2601. auto const mismatch = detail::mismatch(
  2602. it1, last1, it2, last2, std::equal_to<char32_t>{});
  2603. return std::pair<Iter1, Iter2>{
  2604. mismatch.first.base(), mismatch.second.base()};
  2605. } else {
  2606. return detail::mismatch(
  2607. first1, last1, first2, last2, std::equal_to<char32_t>{});
  2608. }
  2609. }
  2610. template<typename I, typename S, typename ErrorHandler, typename T>
  2611. T if_full_parse(
  2612. I initial_first,
  2613. I & first,
  2614. S last,
  2615. ErrorHandler const & error_handler,
  2616. T retval)
  2617. {
  2618. if (first != last) {
  2619. if (retval && error_handler(
  2620. initial_first,
  2621. last,
  2622. parse_error<I>(first, "end of input")) ==
  2623. error_handler_result::rethrow) {
  2624. throw;
  2625. }
  2626. if constexpr (std::is_same_v<T, bool>)
  2627. retval = false;
  2628. else
  2629. retval = std::nullopt;
  2630. }
  2631. return std::move(retval);
  2632. }
  2633. // The notion of comaptibility is that, given a parser with the
  2634. // Attribute Tuple, we can parse into Struct instead.
  2635. template<typename Struct, typename Tuple>
  2636. constexpr auto is_struct_compatible();
  2637. struct element_compatibility
  2638. {
  2639. template<typename T, typename U>
  2640. constexpr auto operator()(T result, U x) const
  2641. {
  2642. using struct_elem =
  2643. remove_cv_ref_t<decltype(parser::get(x, llong<0>{}))>;
  2644. using tuple_elem =
  2645. remove_cv_ref_t<decltype(parser::get(x, llong<1>{}))>;
  2646. if constexpr (!T::value) {
  2647. return std::false_type{};
  2648. } else if constexpr (
  2649. is_optional_v<struct_elem> && is_optional_v<tuple_elem>) {
  2650. using struct_opt_type = optional_type<struct_elem>;
  2651. using tuple_opt_type = optional_type<tuple_elem>;
  2652. using retval_t = decltype((*this)(
  2653. result,
  2654. detail::hl::make_tuple(
  2655. std::declval<struct_opt_type &>(),
  2656. std::declval<tuple_opt_type &>())));
  2657. return retval_t{};
  2658. } else if constexpr (std::is_convertible_v<
  2659. tuple_elem &&,
  2660. struct_elem>) {
  2661. return std::true_type{};
  2662. } else if constexpr (
  2663. container<struct_elem> && container<tuple_elem>) {
  2664. return detail::is_struct_compatible<
  2665. range_value_t<struct_elem>,
  2666. range_value_t<tuple_elem>>();
  2667. } else {
  2668. return std::bool_constant<detail::is_struct_compatible<
  2669. struct_elem,
  2670. tuple_elem>()>{};
  2671. }
  2672. }
  2673. };
  2674. template<typename T, typename U>
  2675. constexpr auto is_struct_compatible()
  2676. {
  2677. if constexpr (
  2678. !std::is_aggregate_v<T> ||
  2679. struct_arity_v<T> != tuple_size_<U>) {
  2680. return std::false_type{};
  2681. } else {
  2682. using result_t = decltype(detail::hl::fold_left(
  2683. detail::hl::zip(
  2684. detail::tie_aggregate(std::declval<T &>()),
  2685. std::declval<U &>()),
  2686. std::true_type{},
  2687. element_compatibility{}));
  2688. return result_t{};
  2689. }
  2690. }
  2691. template<typename Struct, typename Tuple>
  2692. constexpr bool is_struct_compatible_v =
  2693. detail::is_struct_compatible<Struct, Tuple>();
  2694. template<typename ParserAttr, typename GivenContainerAttr>
  2695. constexpr auto parser_attr_or_container_value_type()
  2696. {
  2697. if constexpr (is_nope_v<ParserAttr>) {
  2698. return nope{};
  2699. } else if constexpr (is_optional_v<ParserAttr>) {
  2700. return ParserAttr{};
  2701. } else {
  2702. using value_type = range_value_t<GivenContainerAttr>;
  2703. return std::conditional_t<
  2704. std::is_convertible_v<ParserAttr, value_type>,
  2705. ParserAttr,
  2706. value_type>{};
  2707. }
  2708. }
  2709. template<typename ParserAttr, typename GivenContainerAttr>
  2710. using parser_attr_or_container_value_type_v =
  2711. decltype(parser_attr_or_container_value_type<
  2712. ParserAttr,
  2713. GivenContainerAttr>());
  2714. template<typename T>
  2715. constexpr auto tuple_or_struct_size(T && x)
  2716. {
  2717. if constexpr (is_tuple<remove_cv_ref_t<T>>{}) {
  2718. return hl::size(x);
  2719. } else {
  2720. return llong<struct_arity_v<remove_cv_ref_t<T>>>{};
  2721. }
  2722. }
  2723. template<typename T>
  2724. struct attr_reset
  2725. {
  2726. attr_reset(T & x) : x_(std::addressof(x)) {}
  2727. attr_reset(attr_reset const &) = delete;
  2728. attr_reset(attr_reset &&) = delete;
  2729. attr_reset & operator=(attr_reset const &) = delete;
  2730. attr_reset & operator=(attr_reset &&) = delete;
  2731. ~attr_reset()
  2732. {
  2733. if (x_)
  2734. *x_ = T();
  2735. }
  2736. bool operator=(bool b)
  2737. {
  2738. if (b)
  2739. x_ = nullptr;
  2740. return b;
  2741. }
  2742. private:
  2743. T * x_;
  2744. };
  2745. }
  2746. #ifndef BOOST_PARSER_DOXYGEN
  2747. // This constraint is only here to allow the alternate-call semantic
  2748. // action metaprogramming logic to function on MSVC.
  2749. template<typename Context>
  2750. auto _val(Context const & context) -> std::conditional_t<
  2751. detail::is_nope_v<decltype(*context.val_)>,
  2752. none,
  2753. decltype(*context.val_)>
  2754. {
  2755. if constexpr (detail::is_nope_v<decltype(*context.val_)>)
  2756. return none{};
  2757. else
  2758. return *context.val_;
  2759. }
  2760. template<typename Context>
  2761. decltype(auto) _attr(Context const & context)
  2762. {
  2763. if constexpr (detail::is_nope_v<decltype(*context.attr_)>)
  2764. return none{};
  2765. else
  2766. return *context.attr_;
  2767. }
  2768. template<typename Context>
  2769. decltype(auto) _where(Context const & context)
  2770. {
  2771. return *context.where_;
  2772. }
  2773. template<typename Context>
  2774. decltype(auto) _begin(Context const & context)
  2775. {
  2776. return context.first_;
  2777. }
  2778. template<typename Context>
  2779. decltype(auto) _end(Context const & context)
  2780. {
  2781. return context.last_;
  2782. }
  2783. template<typename Context>
  2784. decltype(auto) _pass(Context const & context)
  2785. {
  2786. return *context.pass_;
  2787. }
  2788. template<typename Context>
  2789. decltype(auto) _locals(Context const & context)
  2790. {
  2791. if constexpr (detail::is_nope_v<decltype(*context.locals_)>)
  2792. return none{};
  2793. else
  2794. return *context.locals_;
  2795. }
  2796. template<typename Context>
  2797. decltype(auto) _params(Context const & context)
  2798. {
  2799. if constexpr (detail::is_nope_v<decltype(*context.params_)>)
  2800. return none{};
  2801. else
  2802. return *context.params_;
  2803. }
  2804. template<typename Context>
  2805. decltype(auto) _globals(Context const & context)
  2806. {
  2807. if constexpr (detail::is_nope_v<decltype(*context.globals_)>)
  2808. return none{};
  2809. else
  2810. return *context.globals_;
  2811. }
  2812. template<typename Context>
  2813. decltype(auto) _no_case(Context const & context)
  2814. {
  2815. return context.no_case_depth_;
  2816. }
  2817. template<typename Context>
  2818. decltype(auto) _error_handler(Context const & context)
  2819. {
  2820. return *context.error_handler_;
  2821. }
  2822. #if BOOST_PARSER_USE_CONCEPTS
  2823. template<std::forward_iterator I, typename Context>
  2824. #else
  2825. template<typename I, typename Context>
  2826. #endif
  2827. void
  2828. _report_error(Context const & context, std::string_view message, I location)
  2829. {
  2830. return context.error_handler_->diagnose(
  2831. diagnostic_kind::error, message, context, location);
  2832. }
  2833. template<typename Context>
  2834. void _report_error(Context const & context, std::string_view message)
  2835. {
  2836. return context.error_handler_->diagnose(
  2837. diagnostic_kind::error, message, context);
  2838. }
  2839. #if BOOST_PARSER_USE_CONCEPTS
  2840. template<std::forward_iterator I, typename Context>
  2841. #else
  2842. template<typename I, typename Context>
  2843. #endif
  2844. void _report_warning(
  2845. Context const & context, std::string_view message, I location)
  2846. {
  2847. return context.error_handler_->diagnose(
  2848. diagnostic_kind::warning, message, context, location);
  2849. }
  2850. template<typename Context>
  2851. void _report_warning(Context const & context, std::string_view message)
  2852. {
  2853. return context.error_handler_->diagnose(
  2854. diagnostic_kind::warning, message, context);
  2855. }
  2856. #endif
  2857. /** An invocable that returns the `I`th parameter to the bottommost rule.
  2858. This is useful for forwarding parameters to sub-rules. */
  2859. template<unsigned int I>
  2860. inline constexpr detail::param_t<I> _p = {};
  2861. // Second order parsers.
  2862. /** A very large sentinel value used to represent pseudo-infinity. */
  2863. int64_t const Inf = detail::unbounded;
  2864. #ifndef BOOST_PARSER_DOXYGEN
  2865. template<
  2866. typename Parser,
  2867. typename DelimiterParser,
  2868. typename MinType,
  2869. typename MaxType>
  2870. struct repeat_parser
  2871. {
  2872. constexpr repeat_parser(
  2873. Parser parser,
  2874. MinType _min,
  2875. MaxType _max,
  2876. DelimiterParser delimiter_parser = DelimiterParser{}) :
  2877. parser_(parser),
  2878. delimiter_parser_(delimiter_parser),
  2879. min_(_min),
  2880. max_(_max)
  2881. {}
  2882. template<
  2883. typename Iter,
  2884. typename Sentinel,
  2885. typename Context,
  2886. typename SkipParser>
  2887. auto call(
  2888. Iter & first,
  2889. Sentinel last,
  2890. Context const & context,
  2891. SkipParser const & skip,
  2892. detail::flags flags,
  2893. bool & success) const
  2894. {
  2895. using attr_t = decltype(parser_.call(
  2896. first, last, context, skip, flags, success));
  2897. auto retval = detail::make_sequence_of<attr_t>();
  2898. call(first, last, context, skip, flags, success, retval);
  2899. return retval;
  2900. }
  2901. template<
  2902. typename Iter,
  2903. typename Sentinel,
  2904. typename Context,
  2905. typename SkipParser,
  2906. typename Attribute>
  2907. void call(
  2908. Iter & first,
  2909. Sentinel last,
  2910. Context const & context,
  2911. SkipParser const & skip,
  2912. detail::flags flags,
  2913. bool & success,
  2914. Attribute & retval) const
  2915. {
  2916. [[maybe_unused]] auto _ = detail::scoped_trace(
  2917. *this,
  2918. first,
  2919. last,
  2920. context,
  2921. detail::in_apply_parser(flags) ? detail::disable_trace(flags)
  2922. : flags,
  2923. retval);
  2924. if constexpr (detail::is_optional_v<Attribute>) {
  2925. detail::optional_type<Attribute> attr;
  2926. detail::apply_parser(
  2927. *this,
  2928. first,
  2929. last,
  2930. context,
  2931. skip,
  2932. detail::set_in_apply_parser(flags),
  2933. success,
  2934. attr);
  2935. if (success)
  2936. retval = std::move(attr);
  2937. } else { // Otherwise, Attribute must be a container or a nope.
  2938. using attr_t = detail::parser_attr_or_container_value_type_v<
  2939. decltype(parser_.call(
  2940. first, last, context, skip, flags, success)),
  2941. Attribute>;
  2942. int64_t count = 0;
  2943. for (int64_t end = detail::resolve(context, min_); count != end;
  2944. ++count) {
  2945. detail::skip(first, last, skip, flags);
  2946. attr_t attr{};
  2947. parser_.call(
  2948. first, last, context, skip, flags, success, attr);
  2949. if (!success) {
  2950. detail::assign(retval, Attribute());
  2951. return;
  2952. }
  2953. detail::move_back(
  2954. retval, std::move(attr), detail::gen_attrs(flags));
  2955. }
  2956. int64_t const end = detail::resolve(context, max_);
  2957. // It looks like you've created a repeated epsilon parser, by
  2958. // writing "*eps", "+eps", "repeat(2, Inf)[eps]", or similar.
  2959. BOOST_PARSER_DEBUG_ASSERT(
  2960. !detail::is_unconditional_eps<Parser>{} || end < Inf);
  2961. for (; count != end; ++count) {
  2962. auto const prev_first = first;
  2963. // This is only ever used in delimited_parser, which
  2964. // always has a min=1; we therefore know we're after a
  2965. // previous element when this executes.
  2966. if constexpr (!detail::is_nope_v<DelimiterParser>) {
  2967. detail::skip(first, last, skip, flags);
  2968. delimiter_parser_.call(
  2969. first,
  2970. last,
  2971. context,
  2972. skip,
  2973. detail::disable_attrs(flags),
  2974. success);
  2975. if (!success) {
  2976. success = true;
  2977. first = prev_first;
  2978. break;
  2979. }
  2980. }
  2981. detail::skip(first, last, skip, flags);
  2982. attr_t attr{};
  2983. parser_.call(
  2984. first, last, context, skip, flags, success, attr);
  2985. if (!success) {
  2986. success = true;
  2987. first = prev_first;
  2988. break;
  2989. }
  2990. detail::move_back(
  2991. retval, std::move(attr), detail::gen_attrs(flags));
  2992. }
  2993. }
  2994. }
  2995. Parser parser_;
  2996. DelimiterParser delimiter_parser_;
  2997. MinType min_;
  2998. MaxType max_;
  2999. };
  3000. #endif
  3001. template<typename Parser>
  3002. struct zero_plus_parser : repeat_parser<Parser>
  3003. {
  3004. constexpr zero_plus_parser(Parser parser) :
  3005. repeat_parser<Parser>(parser, 0, Inf)
  3006. {}
  3007. };
  3008. template<typename Parser>
  3009. struct one_plus_parser : repeat_parser<Parser>
  3010. {
  3011. constexpr one_plus_parser(Parser parser) :
  3012. repeat_parser<Parser>(parser, 1, Inf)
  3013. {}
  3014. };
  3015. template<typename Parser, typename DelimiterParser>
  3016. struct delimited_seq_parser : repeat_parser<Parser, DelimiterParser>
  3017. {
  3018. constexpr delimited_seq_parser(
  3019. Parser parser, DelimiterParser delimiter_parser) :
  3020. repeat_parser<Parser, DelimiterParser>(
  3021. parser, 1, Inf, delimiter_parser)
  3022. {}
  3023. };
  3024. //[ opt_parser_beginning
  3025. template<typename Parser>
  3026. struct opt_parser
  3027. {
  3028. //]
  3029. //[ opt_parser_attr_call
  3030. template<
  3031. typename Iter,
  3032. typename Sentinel,
  3033. typename Context,
  3034. typename SkipParser>
  3035. auto call(
  3036. Iter & first,
  3037. Sentinel last,
  3038. Context const & context,
  3039. SkipParser const & skip,
  3040. detail::flags flags,
  3041. bool & success) const
  3042. {
  3043. using attr_t = decltype(parser_.call(
  3044. first, last, context, skip, flags, success));
  3045. detail::optional_of<attr_t> retval;
  3046. call(first, last, context, skip, flags, success, retval);
  3047. return retval;
  3048. }
  3049. //]
  3050. //[ opt_parser_out_param_call
  3051. template<
  3052. typename Iter,
  3053. typename Sentinel,
  3054. typename Context,
  3055. typename SkipParser,
  3056. typename Attribute>
  3057. void call(
  3058. Iter & first,
  3059. Sentinel last,
  3060. Context const & context,
  3061. SkipParser const & skip,
  3062. detail::flags flags,
  3063. bool & success,
  3064. Attribute & retval) const
  3065. {
  3066. //[ opt_parser_trace
  3067. [[maybe_unused]] auto _ = detail::scoped_trace(
  3068. *this, first, last, context, flags, retval);
  3069. //]
  3070. //[ opt_parser_skip
  3071. detail::skip(first, last, skip, flags);
  3072. //]
  3073. //[ opt_parser_no_gen_attr_path
  3074. if (!detail::gen_attrs(flags)) {
  3075. parser_.call(first, last, context, skip, flags, success);
  3076. success = true;
  3077. return;
  3078. }
  3079. //]
  3080. //[ opt_parser_gen_attr_path
  3081. parser_.call(first, last, context, skip, flags, success, retval);
  3082. if (!success)
  3083. retval = Attribute();
  3084. success = true;
  3085. //]
  3086. }
  3087. //]
  3088. //[ opt_parser_end
  3089. Parser parser_;
  3090. };
  3091. //]
  3092. template<typename ParserTuple>
  3093. struct or_parser
  3094. {
  3095. constexpr or_parser(ParserTuple parsers) : parsers_(parsers) {}
  3096. #ifndef BOOST_PARSER_DOXYGEN
  3097. template<
  3098. typename Iter,
  3099. typename Sentinel,
  3100. typename Context,
  3101. typename SkipParser>
  3102. struct use_parser_t
  3103. {
  3104. template<typename Parser>
  3105. auto operator()(Parser const & parser) const
  3106. {
  3107. detail::skip(first_, last_, skip_, flags_);
  3108. success_ = true; // In case someone earlier already failed...
  3109. return parser.call(
  3110. first_,
  3111. last_,
  3112. context_,
  3113. skip_,
  3114. flags_,
  3115. success_);
  3116. }
  3117. template<typename Parser, typename Attribute>
  3118. void operator()(Parser const & parser, Attribute & retval) const
  3119. {
  3120. detail::skip(first_, last_, skip_, flags_);
  3121. success_ = true; // In case someone earlier already failed...
  3122. detail::apply_parser(
  3123. parser,
  3124. first_,
  3125. last_,
  3126. context_,
  3127. skip_,
  3128. flags_,
  3129. success_,
  3130. retval);
  3131. }
  3132. Iter & first_;
  3133. Sentinel last_;
  3134. Context const & context_;
  3135. SkipParser const & skip_;
  3136. detail::flags flags_;
  3137. bool & success_;
  3138. };
  3139. #endif
  3140. template<
  3141. typename Iter,
  3142. typename Sentinel,
  3143. typename Context,
  3144. typename SkipParser>
  3145. auto call(
  3146. Iter & first,
  3147. Sentinel last,
  3148. Context const & context,
  3149. SkipParser const & skip,
  3150. detail::flags flags,
  3151. bool & success) const
  3152. {
  3153. use_parser_t<Iter, Sentinel, Context, SkipParser> const use_parser{
  3154. first, last, context, skip, flags, success};
  3155. // A result type for each of the parsers in parsers_.
  3156. using all_types =
  3157. decltype(detail::hl::transform(parsers_, use_parser));
  3158. // Same as above, wrapped in detail::wrapper.
  3159. using all_types_wrapped =
  3160. decltype(detail::hl::transform(all_types{}, detail::wrap{}));
  3161. // Returns a tuple<> containing two things: 1) A tuple of only the
  3162. // unique wrapped types from above, without nopes; this may be
  3163. // empty. 2) std::true_type or std::false_type indicating whether
  3164. // nopes were found; if so, the final result is an optional.
  3165. auto append_unique = [](auto result, auto x) {
  3166. using x_type = typename decltype(x)::type;
  3167. if constexpr (detail::is_nope_v<x_type>) {
  3168. return detail::hl::make_pair(
  3169. detail::hl::first(result), std::true_type{});
  3170. } else if constexpr (detail::hl::contains(
  3171. detail::hl::first(result), x)) {
  3172. return result;
  3173. } else {
  3174. return detail::hl::make_pair(
  3175. detail::hl::append(detail::hl::first(result), x),
  3176. detail::hl::second(result));
  3177. }
  3178. };
  3179. using wrapped_unique_types = decltype(detail::hl::fold_left(
  3180. all_types_wrapped{},
  3181. detail::hl::make_pair(tuple<>{}, std::false_type{}),
  3182. append_unique));
  3183. // Same as above, with the tuple types unwrapped.
  3184. using unwrapped_types = decltype(detail::hl::make_pair(
  3185. detail::hl::transform(
  3186. detail::hl::first(wrapped_unique_types{}),
  3187. detail::unwrap{}),
  3188. detail::hl::second(wrapped_unique_types{})));
  3189. // Types above converted to a "variant", which may actually be a
  3190. // non-variant type T if that is the only unique non-nope type, or a
  3191. // nope if unwrapped_types is empty.
  3192. using result_t = detail::to_hana_tuple_or_type_t<unwrapped_types>;
  3193. result_t retval{};
  3194. call(first, last, context, skip, flags, success, retval);
  3195. return retval;
  3196. }
  3197. template<
  3198. typename Iter,
  3199. typename Sentinel,
  3200. typename Context,
  3201. typename SkipParser,
  3202. typename Attribute>
  3203. void call(
  3204. Iter & first,
  3205. Sentinel last,
  3206. Context const & context,
  3207. SkipParser const & skip,
  3208. detail::flags flags,
  3209. bool & success,
  3210. Attribute & retval) const
  3211. {
  3212. [[maybe_unused]] auto _ = detail::scoped_trace(
  3213. *this, first, last, context, flags, retval);
  3214. use_parser_t<Iter, Sentinel, Context, SkipParser> const use_parser{
  3215. first, last, context, skip, flags, success};
  3216. bool done = false;
  3217. auto try_parser = [prev_first = first,
  3218. &use_parser,
  3219. &success,
  3220. flags,
  3221. &retval,
  3222. &done](auto const & parser) {
  3223. if (done)
  3224. return;
  3225. if (detail::gen_attrs(flags))
  3226. use_parser(parser, retval);
  3227. else
  3228. use_parser(parser);
  3229. if (success)
  3230. done = true;
  3231. else
  3232. use_parser.first_ = prev_first;
  3233. };
  3234. detail::hl::for_each(parsers_, try_parser); // TODO: -> fold-expr
  3235. if (!done)
  3236. success = false;
  3237. }
  3238. #ifndef BOOST_PARSER_DOXYGEN
  3239. template<typename Parser>
  3240. constexpr auto prepend(parser_interface<Parser> parser) const noexcept;
  3241. template<typename Parser>
  3242. constexpr auto append(parser_interface<Parser> parser) const noexcept;
  3243. #endif
  3244. ParserTuple parsers_;
  3245. };
  3246. template<typename ParserTuple, typename DelimiterParser>
  3247. struct perm_parser
  3248. {
  3249. constexpr perm_parser(ParserTuple parsers) : parsers_(parsers) {}
  3250. constexpr perm_parser(
  3251. ParserTuple parsers, DelimiterParser delimiter_parser) :
  3252. parsers_(parsers), delimiter_parser_(delimiter_parser)
  3253. {}
  3254. #ifndef BOOST_PARSER_DOXYGEN
  3255. template<
  3256. typename Iter,
  3257. typename Sentinel,
  3258. typename Context,
  3259. typename SkipParser>
  3260. struct use_parser_t
  3261. {
  3262. template<typename Parser>
  3263. auto operator()(Parser const & parser) const
  3264. {
  3265. detail::skip(first_, last_, skip_, flags_);
  3266. success_ = true; // In case someone earlier already failed...
  3267. return parser.call(
  3268. first_,
  3269. last_,
  3270. context_,
  3271. skip_,
  3272. flags_,
  3273. success_);
  3274. }
  3275. template<typename Parser, typename Attribute>
  3276. void operator()(Parser const & parser, Attribute & retval) const
  3277. {
  3278. detail::skip(first_, last_, skip_, flags_);
  3279. success_ = true; // In case someone earlier already failed...
  3280. detail::apply_parser(
  3281. parser,
  3282. first_,
  3283. last_,
  3284. context_,
  3285. skip_,
  3286. flags_,
  3287. success_,
  3288. retval);
  3289. }
  3290. Iter & first_;
  3291. Sentinel last_;
  3292. Context const & context_;
  3293. SkipParser const & skip_;
  3294. detail::flags flags_;
  3295. bool & success_;
  3296. };
  3297. #endif
  3298. template<
  3299. typename Iter,
  3300. typename Sentinel,
  3301. typename Context,
  3302. typename SkipParser>
  3303. auto call(
  3304. Iter & first_,
  3305. Sentinel last,
  3306. Context const & context,
  3307. SkipParser const & skip,
  3308. detail::flags flags,
  3309. bool & success) const
  3310. {
  3311. Iter first = first_;
  3312. use_parser_t<Iter, Sentinel, Context, SkipParser> const use_parser{
  3313. first, last, context, skip, flags, success};
  3314. using result_t =
  3315. decltype(detail::hl::transform(parsers_, use_parser));
  3316. result_t retval{};
  3317. [[maybe_unused]] auto _ = detail::scoped_trace(
  3318. *this, first_, last, context, flags, retval);
  3319. call_impl(
  3320. first,
  3321. last,
  3322. context,
  3323. skip,
  3324. flags,
  3325. success,
  3326. retval,
  3327. std::make_integer_sequence<
  3328. int,
  3329. detail::tuple_size_<ParserTuple>>{});
  3330. if (success)
  3331. first_ = first;
  3332. return retval;
  3333. }
  3334. template<
  3335. typename Iter,
  3336. typename Sentinel,
  3337. typename Context,
  3338. typename SkipParser,
  3339. typename Attribute>
  3340. void call(
  3341. Iter & first_,
  3342. Sentinel last,
  3343. Context const & context,
  3344. SkipParser const & skip,
  3345. detail::flags flags,
  3346. bool & success,
  3347. Attribute & retval) const
  3348. {
  3349. [[maybe_unused]] auto _ = detail::scoped_trace(
  3350. *this, first_, last, context, flags, retval);
  3351. Iter first = first_;
  3352. use_parser_t<Iter, Sentinel, Context, SkipParser> const use_parser{
  3353. first, last, context, skip, flags, success};
  3354. using result_t =
  3355. decltype(detail::hl::transform(parsers_, use_parser));
  3356. constexpr auto indices = std::
  3357. make_integer_sequence<int, detail::tuple_size_<ParserTuple>>{};
  3358. if constexpr (detail::is_optional_v<Attribute>) {
  3359. typename Attribute::value_type attr;
  3360. call(first, last, context, skip, flags, success, attr);
  3361. if (success)
  3362. detail::assign(retval, std::move(attr));
  3363. } else if constexpr (detail::is_tuple<Attribute>{}) {
  3364. call_impl(
  3365. first,
  3366. last,
  3367. context,
  3368. skip,
  3369. flags,
  3370. success,
  3371. retval,
  3372. indices);
  3373. if (!success)
  3374. detail::assign(retval, Attribute());
  3375. } else if constexpr (
  3376. detail::is_struct_compatible_v<Attribute, result_t> ||
  3377. detail::is_constructible_from_tuple_v<Attribute, result_t>) {
  3378. result_t temp_retval{};
  3379. call_impl(
  3380. first,
  3381. last,
  3382. context,
  3383. skip,
  3384. flags,
  3385. success,
  3386. temp_retval,
  3387. indices);
  3388. if (success && detail::gen_attrs(flags)) {
  3389. if constexpr (detail::is_struct_compatible_v<
  3390. Attribute,
  3391. result_t>) {
  3392. detail::assign(retval, temp_retval);
  3393. } else {
  3394. detail::assign(
  3395. retval,
  3396. detail::make_from_tuple<Attribute>(
  3397. std::move(temp_retval)));
  3398. }
  3399. }
  3400. } else {
  3401. #if 0 // TODO Seems incompatible with this parser.
  3402. // call_impl requires a tuple, so we must wrap this scalar.
  3403. tuple<Attribute> temp_retval{};
  3404. call_impl(
  3405. first,
  3406. last,
  3407. context,
  3408. skip,
  3409. flags,
  3410. success,
  3411. temp_retval,
  3412. indices);
  3413. if (success && detail::gen_attrs(flags)) {
  3414. detail::assign(
  3415. retval, std::move(detail::hl::front(temp_retval)));
  3416. }
  3417. #else
  3418. static_assert(
  3419. std::is_same_v<Attribute, void> && false,
  3420. "It looks like you passed an attribute to this permutation "
  3421. "parser that is not capable of taking the number, or the "
  3422. "types of values compatible with the ones it produces.");
  3423. #endif
  3424. }
  3425. if (success)
  3426. first_ = first;
  3427. }
  3428. template<
  3429. typename Iter,
  3430. typename Sentinel,
  3431. typename Context,
  3432. typename SkipParser,
  3433. typename... Ts,
  3434. int... Is>
  3435. void call_impl(
  3436. Iter & first,
  3437. Sentinel last,
  3438. Context const & context,
  3439. SkipParser const & skip,
  3440. detail::flags flags,
  3441. bool & success,
  3442. tuple<Ts...> & retval,
  3443. std::integer_sequence<int, Is...>) const
  3444. {
  3445. std::array<bool, sizeof...(Ts)> used_parsers = {{}};
  3446. // Use "parser" to fill in attribute "x", unless "parser" has
  3447. // previously been used.
  3448. auto parse_into = [&](int i, auto const & parser, auto & x) {
  3449. if (used_parsers[i])
  3450. return false;
  3451. detail::skip(first, last, skip, flags);
  3452. parser.call(first, last, context, skip, flags, success, x);
  3453. if (success) {
  3454. used_parsers[i] = true;
  3455. return true;
  3456. }
  3457. success = true;
  3458. return false;
  3459. };
  3460. // Use one of the previously-unused parsers to parse one
  3461. // alternative.
  3462. bool first_iteration = true;
  3463. auto parsed_one = [&](auto) {
  3464. if constexpr (!detail::is_nope_v<DelimiterParser>) {
  3465. if (!first_iteration) {
  3466. detail::skip(first, last, skip, flags);
  3467. bool local_success = true;
  3468. delimiter_parser_.call(
  3469. first, last, context, skip, flags, local_success);
  3470. if (!local_success)
  3471. return false;
  3472. }
  3473. first_iteration = false;
  3474. }
  3475. return (
  3476. parse_into(
  3477. Is,
  3478. parser::get(parsers_, llong<Is>{}),
  3479. parser::get(retval, llong<Is>{})) ||
  3480. ...);
  3481. };
  3482. success = (parsed_one(Is) && ...);
  3483. if (!success)
  3484. retval = tuple<Ts...>{};
  3485. }
  3486. #ifndef BOOST_PARSER_DOXYGEN
  3487. template<typename Parser>
  3488. constexpr auto prepend(parser_interface<Parser> parser) const noexcept;
  3489. template<typename Parser>
  3490. constexpr auto append(parser_interface<Parser> parser) const noexcept;
  3491. #endif
  3492. ParserTuple parsers_;
  3493. DelimiterParser delimiter_parser_;
  3494. };
  3495. namespace detail {
  3496. template<int N, int... I>
  3497. constexpr auto
  3498. make_default_combining_impl(std::integer_sequence<int, I...>)
  3499. {
  3500. return hl::make_tuple(((void)I, llong<N>{})...);
  3501. }
  3502. template<template<class...> class Tuple, typename... Args>
  3503. constexpr auto make_default_combining(Tuple<Args...>)
  3504. {
  3505. return detail::make_default_combining_impl<0>(
  3506. std::make_integer_sequence<int, sizeof...(Args)>());
  3507. }
  3508. template<typename ParserTuple>
  3509. using default_combining_t = decltype(detail::make_default_combining(
  3510. std::declval<ParserTuple>()));
  3511. struct merge_t
  3512. {};
  3513. struct separate_t
  3514. {};
  3515. template<
  3516. typename Iter,
  3517. typename Sentinel,
  3518. typename Context,
  3519. typename SkipParser>
  3520. struct dummy_use_parser_t
  3521. {
  3522. dummy_use_parser_t(
  3523. Iter & first,
  3524. Sentinel last,
  3525. Context const & context,
  3526. SkipParser const & skip,
  3527. detail::flags flags,
  3528. bool & success) :
  3529. first_(first),
  3530. last_(last),
  3531. context_(context),
  3532. skip_(skip),
  3533. flags_(flags),
  3534. success_(success)
  3535. {}
  3536. template<typename Parser>
  3537. auto operator()(Parser const & parser) const
  3538. {
  3539. return parser.call(
  3540. first_,
  3541. last_,
  3542. context_,
  3543. skip_,
  3544. flags_,
  3545. success_);
  3546. }
  3547. Iter & first_;
  3548. Sentinel last_;
  3549. Context const & context_;
  3550. SkipParser const & skip_;
  3551. detail::flags flags_;
  3552. bool & success_;
  3553. };
  3554. template<typename... Args>
  3555. constexpr void static_assert_merge_attributes(tuple<Args...> parsers);
  3556. // Combining groups are: 0, which is default merge behavior, as in
  3557. // seq_parser::combine; -1, which is don't merge with anything, ever;
  3558. // and N>0, which is merge with other members of group N.
  3559. template<typename CombiningGroups, typename... Args>
  3560. constexpr auto make_combining(tuple<Args...> parsers)
  3561. {
  3562. if constexpr (std::is_same_v<CombiningGroups, merge_t>) {
  3563. detail::static_assert_merge_attributes(parsers);
  3564. return detail::make_default_combining_impl<1>(
  3565. std::make_integer_sequence<int, sizeof...(Args)>());
  3566. } else if constexpr (std::is_same_v<CombiningGroups, separate_t>) {
  3567. return detail::make_default_combining_impl<-1>(
  3568. std::make_integer_sequence<int, sizeof...(Args)>());
  3569. } else {
  3570. return CombiningGroups{};
  3571. }
  3572. }
  3573. template<typename ParserTuple, typename CombiningGroups>
  3574. using combining_t = decltype(detail::make_combining<CombiningGroups>(
  3575. std::declval<ParserTuple>()));
  3576. struct max_
  3577. {
  3578. template<typename T, typename U>
  3579. constexpr auto operator()(T x, U y) const
  3580. {
  3581. if constexpr (T::value < U::value)
  3582. return y;
  3583. else
  3584. return x;
  3585. }
  3586. };
  3587. template<int MaxGroupIdx>
  3588. struct adjust_combining_groups
  3589. {
  3590. template<typename T, typename U>
  3591. constexpr auto operator()(T result, U x) const
  3592. {
  3593. if constexpr (U::value <= 0)
  3594. return hl::append(result, x);
  3595. else
  3596. return hl::append(result, llong<MaxGroupIdx + U::value>{});
  3597. }
  3598. };
  3599. template<typename Tuple1, typename Tuple2>
  3600. constexpr auto make_combined_combining(Tuple1 lhs, Tuple2 rhs)
  3601. {
  3602. auto max_group_idx = detail::hl::fold_left(lhs, llong<0>{}, max_{});
  3603. auto rhs_adjusted = detail::hl::fold_left(
  3604. rhs,
  3605. tuple<>{},
  3606. adjust_combining_groups<decltype(max_group_idx)::value>{});
  3607. return hl::concat(lhs, rhs_adjusted);
  3608. }
  3609. template<typename CombiningGroups1, typename CombiningGroups2>
  3610. using combined_combining_t = decltype(detail::make_combined_combining(
  3611. std::declval<CombiningGroups1>(),
  3612. std::declval<CombiningGroups2>()));
  3613. enum class merge_kind { second_pass_detect, singleton, merged, group };
  3614. template<merge_kind Kind>
  3615. struct merge_kind_t
  3616. {
  3617. static constexpr merge_kind kind = Kind;
  3618. };
  3619. template<merge_kind Kind>
  3620. static constexpr auto merge_wrap = merge_kind_t<Kind>{};
  3621. }
  3622. #ifndef BOOST_PARSER_DOXYGEN
  3623. template<
  3624. typename ParserTuple,
  3625. typename BacktrackingTuple,
  3626. typename CombiningGroups>
  3627. struct seq_parser
  3628. {
  3629. using backtracking = BacktrackingTuple;
  3630. using combining_groups = CombiningGroups;
  3631. constexpr seq_parser(ParserTuple parsers) : parsers_(parsers) {}
  3632. static constexpr auto true_ = std::true_type{};
  3633. static constexpr auto false_ = std::false_type{};
  3634. struct combine
  3635. {
  3636. template<typename T, typename U>
  3637. auto operator()(
  3638. T result_merging_indices_and_prev_group, U x_and_group) const
  3639. {
  3640. using namespace literals;
  3641. using detail::merge_wrap;
  3642. using detail::merge_kind;
  3643. auto x = parser::get(x_and_group, 0_c);
  3644. auto group = parser::get(x_and_group, 1_c);
  3645. auto result =
  3646. parser::get(result_merging_indices_and_prev_group, 0_c);
  3647. using result_back_type =
  3648. typename std::decay_t<decltype(detail::hl::back(
  3649. result))>::type;
  3650. using unwrapped_optional_result_back_type =
  3651. detail::unwrapped_optional_t<result_back_type>;
  3652. auto merging =
  3653. parser::get(result_merging_indices_and_prev_group, 1_c);
  3654. auto indices =
  3655. parser::get(result_merging_indices_and_prev_group, 2_c);
  3656. auto prev_group =
  3657. parser::get(result_merging_indices_and_prev_group, 3_c);
  3658. using x_type = typename decltype(x)::type;
  3659. using unwrapped_optional_x_type =
  3660. detail::unwrapped_optional_t<x_type>;
  3661. if constexpr (detail::is_nope_v<x_type>) {
  3662. if constexpr (
  3663. !detail::is_nope_v<result_back_type> &&
  3664. 0 < decltype(group)::value &&
  3665. decltype(group)::value != decltype(prev_group)::value) {
  3666. // T >> merge[nope >> ...] -> nope
  3667. // This is a very special case. If we see a nope at
  3668. // the begining of a group, and there's a non-nope
  3669. // before it, we put the nope in place in the result
  3670. // tuple temporarily, knowing that a non-nope will
  3671. // come along later in the group to replace it.
  3672. return detail::hl::make_tuple(
  3673. detail::hl::append(result, x),
  3674. detail::hl::append(
  3675. merging,
  3676. merge_wrap<merge_kind::second_pass_detect>),
  3677. detail::hl::append(
  3678. indices, detail::hl::size(result)),
  3679. group);
  3680. } else {
  3681. // T >> nope -> T
  3682. return detail::hl::make_tuple(
  3683. result,
  3684. detail::hl::append(
  3685. merging,
  3686. merge_wrap<merge_kind::second_pass_detect>),
  3687. detail::hl::append(
  3688. indices, detail::hl::size_minus_one(result)),
  3689. prev_group);
  3690. }
  3691. } else if constexpr (detail::is_nope_v<result_back_type>) {
  3692. // tuple<nope> >> T -> tuple<T>
  3693. constexpr auto merge =
  3694. 0 < decltype(group)::value
  3695. ? merge_kind::group
  3696. : (decltype(group)::value == -1
  3697. ? merge_kind::singleton
  3698. : merge_kind::second_pass_detect);
  3699. return detail::hl::make_tuple(
  3700. detail::hl::append(detail::hl::drop_back(result), x),
  3701. detail::hl::append(merging, merge_wrap<merge>),
  3702. detail::hl::append(
  3703. indices, detail::hl::size_minus_one(result)),
  3704. group);
  3705. } else if constexpr (0 < decltype(group)::value) {
  3706. if constexpr (
  3707. decltype(prev_group)::value == decltype(group)::value) {
  3708. return detail::hl::make_tuple(
  3709. result,
  3710. detail::hl::append(
  3711. merging, merge_wrap<merge_kind::group>),
  3712. detail::hl::append(
  3713. indices, detail::hl::size_minus_one(result)),
  3714. group);
  3715. } else {
  3716. return detail::hl::make_tuple(
  3717. detail::hl::append(result, x),
  3718. detail::hl::append(
  3719. merging, merge_wrap<merge_kind::group>),
  3720. detail::hl::append(
  3721. indices, detail::hl::size(result)),
  3722. group);
  3723. }
  3724. } else if constexpr (
  3725. decltype(group)::value == -1 ||
  3726. decltype(group)::value != decltype(prev_group)::value) {
  3727. constexpr auto merge = decltype(group)::value == -1
  3728. ? merge_kind::singleton
  3729. : merge_kind::second_pass_detect;
  3730. return detail::hl::make_tuple(
  3731. detail::hl::append(result, x),
  3732. detail::hl::append(merging, merge_wrap<merge>),
  3733. detail::hl::append(indices, detail::hl::size(result)),
  3734. group);
  3735. } else if constexpr (
  3736. detail::is_char_type_v<result_back_type> &&
  3737. (detail::is_char_type_v<x_type> ||
  3738. detail::is_char_type_v<unwrapped_optional_x_type>)) {
  3739. // CHAR >> CHAR -> string
  3740. return detail::hl::make_tuple(
  3741. detail::hl::append(
  3742. detail::hl::drop_back(result),
  3743. detail::wrapper<std::string>{}),
  3744. detail::hl::append(
  3745. detail::hl::append(
  3746. detail::hl::drop_front(merging),
  3747. merge_wrap<merge_kind::second_pass_detect>),
  3748. merge_wrap<merge_kind::second_pass_detect>),
  3749. detail::hl::append(
  3750. indices, detail::hl::size_minus_one(result)),
  3751. group);
  3752. } else if constexpr (
  3753. detail::
  3754. container_and_value_type<result_back_type, x_type> ||
  3755. detail::container_and_value_type<
  3756. result_back_type,
  3757. unwrapped_optional_x_type>) {
  3758. // C<T> >> T -> C<T>
  3759. // C<T> >> optional<T> -> C<T>
  3760. return detail::hl::make_tuple(
  3761. result,
  3762. detail::hl::append(
  3763. merging,
  3764. merge_wrap<merge_kind::second_pass_detect>),
  3765. detail::hl::append(
  3766. indices, detail::hl::size_minus_one(result)),
  3767. group);
  3768. } else if constexpr (
  3769. detail::
  3770. container_and_value_type<x_type, result_back_type> ||
  3771. detail::container_and_value_type<
  3772. x_type,
  3773. unwrapped_optional_result_back_type>) {
  3774. // T >> C<T> -> C<T>
  3775. // optional<T> >> C<T> -> C<T>
  3776. return detail::hl::make_tuple(
  3777. detail::hl::append(detail::hl::drop_back(result), x),
  3778. detail::hl::append(
  3779. detail::hl::append(
  3780. detail::hl::drop_front(merging),
  3781. merge_wrap<merge_kind::second_pass_detect>),
  3782. merge_wrap<merge_kind::second_pass_detect>),
  3783. detail::hl::append(
  3784. indices, detail::hl::size_minus_one(result)),
  3785. group);
  3786. } else {
  3787. // tuple<Ts...> >> T -> tuple<Ts..., T>
  3788. return detail::hl::make_tuple(
  3789. detail::hl::append(result, x),
  3790. detail::hl::append(
  3791. merging, merge_wrap<merge_kind::second_pass_detect>),
  3792. detail::hl::append(indices, detail::hl::size(result)),
  3793. group);
  3794. }
  3795. }
  3796. };
  3797. struct find_merged
  3798. {
  3799. template<typename T, typename U>
  3800. auto operator()(
  3801. T final_types_and_result, U x_index_and_prev_merged) const
  3802. {
  3803. using namespace literals;
  3804. using detail::merge_wrap;
  3805. using detail::merge_kind;
  3806. auto final_types = parser::get(final_types_and_result, 0_c);
  3807. auto result = parser::get(final_types_and_result, 1_c);
  3808. auto x_type_wrapper = parser::get(x_index_and_prev_merged, 0_c);
  3809. auto index = parser::get(x_index_and_prev_merged, 1_c);
  3810. auto prev_merged = parser::get(x_index_and_prev_merged, 2_c);
  3811. auto type_at_index_wrapper = parser::get(final_types, index);
  3812. using x_type = typename decltype(x_type_wrapper)::type;
  3813. using type_at_index =
  3814. typename decltype(type_at_index_wrapper)::type;
  3815. if constexpr (
  3816. decltype(prev_merged)::kind ==
  3817. merge_kind::second_pass_detect) {
  3818. if constexpr (
  3819. !std::is_same_v<x_type, type_at_index> &&
  3820. container<type_at_index>) {
  3821. return detail::hl::make_tuple(
  3822. final_types,
  3823. detail::hl::append(
  3824. result, merge_wrap<merge_kind::merged>));
  3825. } else {
  3826. return detail::hl::make_tuple(
  3827. final_types,
  3828. detail::hl::append(
  3829. result, merge_wrap<merge_kind::singleton>));
  3830. }
  3831. } else {
  3832. return detail::hl::make_tuple(
  3833. final_types, detail::hl::append(result, prev_merged));
  3834. }
  3835. }
  3836. };
  3837. template<long long I>
  3838. static constexpr auto
  3839. merging_from_group(integral_constant<long long, I>)
  3840. {
  3841. using detail::merge_wrap;
  3842. using detail::merge_kind;
  3843. if constexpr (0 < I)
  3844. return merge_wrap<merge_kind::group>;
  3845. else if constexpr (I == -1)
  3846. return merge_wrap<merge_kind::singleton>;
  3847. else
  3848. return merge_wrap<merge_kind::second_pass_detect>;
  3849. }
  3850. // Returns the tuple of values produced by this parser, and the
  3851. // indices into that tuple that each parser should use in turn. The
  3852. // case where the tuple only has one element is handled elsewhere.
  3853. template<
  3854. typename Iter,
  3855. typename Sentinel,
  3856. typename Context,
  3857. typename SkipParser>
  3858. auto make_temp_result(
  3859. Iter & first,
  3860. Sentinel last,
  3861. Context const & context,
  3862. SkipParser const & skip,
  3863. detail::flags flags,
  3864. bool & success) const
  3865. {
  3866. using namespace literals;
  3867. detail::
  3868. dummy_use_parser_t<Iter, Sentinel, Context, SkipParser> const
  3869. dummy_use_parser(
  3870. first, last, context, skip, flags, success);
  3871. // A result type for each of the parsers in parsers_.
  3872. using all_types =
  3873. decltype(detail::hl::transform(parsers_, dummy_use_parser));
  3874. // Same as above, wrapped in detail::wrapper.
  3875. using all_types_wrapped =
  3876. decltype(detail::hl::transform(all_types{}, detail::wrap{}));
  3877. using combining_groups =
  3878. detail::combining_t<ParserTuple, CombiningGroups>;
  3879. constexpr auto first_group = detail::hl::front(combining_groups{});
  3880. // Generate a tuple of outputs; the index that each parser should
  3881. // use to write into its output; and whether the attribute for
  3882. // each parser was merged into an adjacent container-attribute.
  3883. constexpr auto combine_start = detail::hl::make_tuple(
  3884. detail::hl::make_tuple(detail::hl::front(all_types_wrapped{})),
  3885. detail::hl::make_tuple(merging_from_group(first_group)),
  3886. tuple<llong<0>>{},
  3887. first_group);
  3888. using combined_types = decltype(detail::hl::fold_left(
  3889. detail::hl::zip(
  3890. detail::hl::drop_front(all_types_wrapped{}),
  3891. detail::hl::drop_front(combining_groups{})),
  3892. combine_start,
  3893. combine{}));
  3894. // Unwrap the result tuple's types.
  3895. constexpr auto result_type_wrapped =
  3896. parser::get(combined_types{}, 0_c);
  3897. using result_type = decltype(detail::hl::transform(
  3898. result_type_wrapped, detail::unwrap{}));
  3899. using indices = decltype(parser::get(combined_types{}, 2_c));
  3900. using first_pass_merged =
  3901. decltype(parser::get(combined_types{}, 1_c));
  3902. constexpr auto find_merged_start =
  3903. detail::hl::make_tuple(result_type_wrapped, tuple<>{});
  3904. using merged = decltype(detail::hl::fold_left(
  3905. detail::hl::zip(
  3906. all_types_wrapped{}, indices{}, first_pass_merged{}),
  3907. find_merged_start,
  3908. find_merged{}));
  3909. return detail::hl::make_tuple(
  3910. result_type{}, indices{}, parser::get(merged{}, 1_c));
  3911. }
  3912. template<
  3913. typename Iter,
  3914. typename Sentinel,
  3915. typename Context,
  3916. typename SkipParser>
  3917. auto call(
  3918. Iter & first_,
  3919. Sentinel last,
  3920. Context const & context,
  3921. SkipParser const & skip,
  3922. detail::flags flags,
  3923. bool & success) const
  3924. {
  3925. Iter first = first_;
  3926. auto temp_result =
  3927. make_temp_result(first, last, context, skip, flags, success);
  3928. std::decay_t<decltype(parser::get(temp_result, llong<0>{}))>
  3929. retval{};
  3930. [[maybe_unused]] auto _ = detail::scoped_trace(
  3931. *this,
  3932. first_,
  3933. last,
  3934. context,
  3935. detail::in_apply_parser(flags) ? detail::disable_trace(flags)
  3936. : flags,
  3937. retval);
  3938. std::decay_t<decltype(parser::get(temp_result, llong<1>{}))>
  3939. indices;
  3940. std::decay_t<decltype(parser::get(temp_result, llong<2>{}))>
  3941. merged;
  3942. call_impl(
  3943. first,
  3944. last,
  3945. context,
  3946. skip,
  3947. flags,
  3948. success,
  3949. retval,
  3950. indices,
  3951. merged);
  3952. if (success)
  3953. first_ = first;
  3954. // A 1-tuple is converted to a scalar.
  3955. if constexpr (detail::hl::size(retval) == llong<1>{}) {
  3956. using namespace literals;
  3957. return parser::get(retval, 0_c);
  3958. } else {
  3959. return retval;
  3960. }
  3961. }
  3962. template<
  3963. typename Iter,
  3964. typename Sentinel,
  3965. typename Context,
  3966. typename SkipParser,
  3967. typename Attribute>
  3968. void call(
  3969. Iter & first_,
  3970. Sentinel last,
  3971. Context const & context,
  3972. SkipParser const & skip,
  3973. detail::flags flags,
  3974. bool & success,
  3975. Attribute & retval) const
  3976. {
  3977. [[maybe_unused]] auto _ = detail::scoped_trace(
  3978. *this,
  3979. first_,
  3980. last,
  3981. context,
  3982. detail::in_apply_parser(flags) ? detail::disable_trace(flags)
  3983. : flags,
  3984. retval);
  3985. Iter first = first_;
  3986. auto temp_result =
  3987. make_temp_result(first, last, context, skip, flags, success);
  3988. using temp_result_attr_t =
  3989. std::decay_t<decltype(parser::get(temp_result, llong<0>{}))>;
  3990. std::decay_t<decltype(parser::get(temp_result, llong<1>{}))>
  3991. indices;
  3992. std::decay_t<decltype(parser::get(temp_result, llong<2>{}))> merged;
  3993. auto max_ = [](auto result, auto x) {
  3994. if constexpr (decltype(result)::value < decltype(x)::value) {
  3995. return x;
  3996. } else {
  3997. return result;
  3998. }
  3999. };
  4000. using max_index_t =
  4001. decltype(detail::hl::fold_left(indices, llong<0>{}, max_));
  4002. if constexpr (detail::is_optional_v<Attribute>) {
  4003. typename Attribute::value_type attr;
  4004. call(first, last, context, skip, flags, success, attr);
  4005. if (success)
  4006. detail::assign(retval, std::move(attr));
  4007. } else if constexpr (
  4008. detail::is_tuple<Attribute>{} ||
  4009. detail::is_struct_compatible_v<Attribute, temp_result_attr_t>) {
  4010. call_impl(
  4011. first,
  4012. last,
  4013. context,
  4014. skip,
  4015. flags,
  4016. success,
  4017. retval,
  4018. indices,
  4019. merged);
  4020. if (!success)
  4021. detail::assign(retval, Attribute());
  4022. } else if constexpr (
  4023. 0 < max_index_t::value && detail::is_constructible_from_tuple_v<
  4024. Attribute,
  4025. temp_result_attr_t>) {
  4026. temp_result_attr_t temp_retval{};
  4027. call_impl(
  4028. first,
  4029. last,
  4030. context,
  4031. skip,
  4032. flags,
  4033. success,
  4034. temp_retval,
  4035. indices,
  4036. merged);
  4037. if (success && detail::gen_attrs(flags)) {
  4038. detail::assign(
  4039. retval,
  4040. detail::make_from_tuple<Attribute>(
  4041. std::move(temp_retval)));
  4042. }
  4043. } else {
  4044. // call_impl requires a tuple, so we must wrap this scalar.
  4045. tuple<Attribute> temp_retval{};
  4046. call_impl(
  4047. first,
  4048. last,
  4049. context,
  4050. skip,
  4051. flags,
  4052. success,
  4053. temp_retval,
  4054. indices,
  4055. merged);
  4056. if (success && detail::gen_attrs(flags)) {
  4057. detail::assign(
  4058. retval, std::move(detail::hl::front(temp_retval)));
  4059. }
  4060. }
  4061. if (success)
  4062. first_ = first;
  4063. }
  4064. // Invokes each parser, placing the resulting values (if any) into
  4065. // retval, using the index mapping in indices. The case of a tuple
  4066. // containing only a single value is handled elsewhere.
  4067. template<
  4068. typename Iter,
  4069. typename Sentinel,
  4070. typename Context,
  4071. typename SkipParser,
  4072. typename Attribute,
  4073. typename Indices,
  4074. typename Merged>
  4075. void call_impl(
  4076. Iter & first,
  4077. Sentinel last,
  4078. Context const & context,
  4079. SkipParser const & skip,
  4080. detail::flags flags,
  4081. bool & success,
  4082. Attribute & retval,
  4083. Indices const & indices,
  4084. Merged const & merged) const
  4085. {
  4086. using detail::merge_wrap;
  4087. using detail::merge_kind;
  4088. static_assert(
  4089. detail::is_tuple<Attribute>{} || std::is_aggregate_v<Attribute>,
  4090. "");
  4091. auto use_parser = [&first,
  4092. last,
  4093. &context,
  4094. &skip,
  4095. flags_ = flags,
  4096. &success,
  4097. &retval](auto const &
  4098. parser_index_merged_and_backtrack) {
  4099. if (!success) // Someone earlier already failed...
  4100. return;
  4101. auto flags = flags_;
  4102. using namespace literals;
  4103. detail::skip(first, last, skip, flags);
  4104. auto const & parser =
  4105. parser::get(parser_index_merged_and_backtrack, 0_c);
  4106. auto merge_kind_t_ =
  4107. parser::get(parser_index_merged_and_backtrack, 2_c);
  4108. constexpr bool was_merged_into_adjacent_container =
  4109. decltype(merge_kind_t_)::kind == merge_kind::merged;
  4110. constexpr bool is_in_a_group =
  4111. decltype(merge_kind_t_)::kind == merge_kind::group;
  4112. bool const can_backtrack =
  4113. parser::get(parser_index_merged_and_backtrack, 3_c);
  4114. if (!detail::gen_attrs(flags)) {
  4115. parser.call(first, last, context, skip, flags, success);
  4116. if (!success && !can_backtrack) {
  4117. std::stringstream oss;
  4118. detail::print_parser(context, parser, oss);
  4119. throw parse_error<Iter>(first, oss.str());
  4120. }
  4121. return;
  4122. }
  4123. auto const tuple_idx =
  4124. parser::get(parser_index_merged_and_backtrack, 1_c);
  4125. auto const tuple_size = detail::tuple_or_struct_size(retval);
  4126. static_assert(
  4127. decltype(tuple_idx)::value < decltype(tuple_size)::value,
  4128. "Looks like you're trying to write some attribute into an "
  4129. "out-of-bounds position in a tuple/struct. In other "
  4130. "words, the attribute you're parsing into does not match "
  4131. "the default attribute used by this parser. This may be "
  4132. "because you passed an out-param to parse() at the top "
  4133. "level that is not compatible with the attribute type "
  4134. "generated by the parser you passed to parse().");
  4135. if constexpr (!(decltype(tuple_idx)::value <
  4136. decltype(tuple_size)::value)) {
  4137. [[maybe_unused]] detail::print_type<Attribute> _;
  4138. }
  4139. auto & out = parser::get(retval, tuple_idx);
  4140. using attr_t = decltype(parser.call(
  4141. first, last, context, skip, flags, success));
  4142. constexpr bool out_container =
  4143. container<std::decay_t<decltype(out)>>;
  4144. constexpr bool attr_container = container<attr_t>;
  4145. if constexpr (
  4146. (out_container == attr_container &&
  4147. !was_merged_into_adjacent_container &&
  4148. !detail::is_nope_v<attr_t>) ||
  4149. is_in_a_group) {
  4150. parser.call(
  4151. first, last, context, skip, flags, success, out);
  4152. if (!success) {
  4153. if (!can_backtrack) {
  4154. std::stringstream oss;
  4155. detail::print_parser(context, parser, oss);
  4156. throw parse_error<Iter>(first, oss.str());
  4157. }
  4158. out = std::decay_t<decltype(out)>();
  4159. return;
  4160. }
  4161. } else {
  4162. attr_t x =
  4163. parser.call(first, last, context, skip, flags, success);
  4164. if (!success) {
  4165. if (!can_backtrack) {
  4166. std::stringstream oss;
  4167. detail::print_parser(context, parser, oss);
  4168. throw parse_error<Iter>(first, oss.str());
  4169. }
  4170. return;
  4171. }
  4172. using just_x = attr_t;
  4173. using just_out = detail::remove_cv_ref_t<decltype(out)>;
  4174. if constexpr (detail::is_nope_v<attr_t>) {
  4175. // nothing to do
  4176. } if constexpr (
  4177. (!out_container ||
  4178. !std::is_same_v<just_x, just_out>) &&
  4179. std::is_assignable_v<just_out &, just_x &&> &&
  4180. (!std::is_same_v<just_out, std::string> ||
  4181. !std::is_integral_v<just_x>)) {
  4182. detail::assign(out, std::move(x));
  4183. } else {
  4184. detail::move_back(
  4185. out, std::move(x), detail::gen_attrs(flags));
  4186. }
  4187. }
  4188. };
  4189. auto const parsers_and_indices =
  4190. detail::hl::zip(parsers_, indices, merged, backtracking{});
  4191. detail::hl::for_each(parsers_and_indices, use_parser);
  4192. }
  4193. template<bool AllowBacktracking, typename Parser>
  4194. constexpr auto prepend(parser_interface<Parser> parser) const noexcept;
  4195. template<bool AllowBacktracking, typename Parser>
  4196. constexpr auto append(parser_interface<Parser> parser) const noexcept;
  4197. ParserTuple parsers_;
  4198. };
  4199. #endif
  4200. namespace detail {
  4201. template<typename Action, typename Attribute>
  4202. using action_direct_call_expr =
  4203. decltype(std::declval<Action>()(std::declval<Attribute>()));
  4204. template<typename Action, typename Attribute>
  4205. using action_apply_call_expr = decltype(hl::apply(
  4206. std::declval<Action>(), std::declval<Attribute>()));
  4207. template<typename Action, typename Attribute, typename Context>
  4208. using action_assignable_to_val_direct_expr =
  4209. decltype(_val(std::declval<Context>()) = std::declval<Action>()(std::declval<Attribute>()));
  4210. template<typename Action, typename Attribute, typename Context>
  4211. using action_assignable_to_val_apply_expr =
  4212. decltype(_val(std::declval<Context>()) = hl::apply(std::declval<Action>(), std::declval<Attribute>()));
  4213. template<typename Action, typename Attribute, typename Context>
  4214. constexpr auto action_assignable_to_val_direct()
  4215. {
  4216. if constexpr (is_nope_v<decltype(*std::declval<Context>().val_)>) {
  4217. return false;
  4218. } else if constexpr (!is_detected_v<
  4219. action_direct_call_expr,
  4220. Action,
  4221. Attribute>) {
  4222. return false;
  4223. } else if constexpr (std::is_same_v<
  4224. action_direct_call_expr<Action, Attribute>,
  4225. void>) {
  4226. return false;
  4227. } else {
  4228. return is_detected_v<
  4229. action_assignable_to_val_direct_expr,
  4230. Action,
  4231. Attribute,
  4232. Context>;
  4233. }
  4234. }
  4235. template<typename Action, typename Attribute, typename Context>
  4236. constexpr auto action_assignable_to_val_apply()
  4237. {
  4238. if constexpr (is_nope_v<decltype(*std::declval<Context>().val_)>) {
  4239. return false;
  4240. } else if constexpr (!is_tuple<remove_cv_ref_t<Attribute>>{}) {
  4241. return false;
  4242. } else if constexpr (tuple_size_<remove_cv_ref_t<Attribute>> < 2) {
  4243. return false;
  4244. } else if constexpr (!is_detected_v<
  4245. action_apply_call_expr,
  4246. Action,
  4247. Attribute>) {
  4248. return false;
  4249. } else if constexpr (std::is_same_v<
  4250. action_apply_call_expr<Action, Attribute>,
  4251. void>) {
  4252. return false;
  4253. } else {
  4254. return is_detected_v<
  4255. action_assignable_to_val_apply_expr,
  4256. Action,
  4257. Attribute,
  4258. Context>;
  4259. }
  4260. }
  4261. template<typename Context, typename TagType>
  4262. constexpr bool in_recursion =
  4263. std::is_same_v<typename Context::rule_tag, TagType> &&
  4264. !std::is_same_v<typename Context::rule_tag, void>;
  4265. }
  4266. #ifndef BOOST_PARSER_DOXYGEN
  4267. template<typename Parser, typename Action>
  4268. struct action_parser
  4269. {
  4270. template<
  4271. typename Iter,
  4272. typename Sentinel,
  4273. typename Context,
  4274. typename SkipParser>
  4275. detail::nope call(
  4276. Iter & first,
  4277. Sentinel last,
  4278. Context const & context,
  4279. SkipParser const & skip,
  4280. detail::flags flags,
  4281. bool & success) const
  4282. {
  4283. detail::nope retval;
  4284. call(first, last, context, skip, flags, success, retval);
  4285. return retval;
  4286. }
  4287. template<
  4288. typename Iter,
  4289. typename Sentinel,
  4290. typename Context,
  4291. typename SkipParser,
  4292. typename Attribute>
  4293. void call(
  4294. Iter & first,
  4295. Sentinel last,
  4296. Context const & context,
  4297. SkipParser const & skip,
  4298. detail::flags flags,
  4299. bool & success,
  4300. Attribute & retval) const
  4301. {
  4302. [[maybe_unused]] auto _ = detail::scoped_trace(
  4303. *this, first, last, context, flags, retval);
  4304. auto const initial_first = first;
  4305. auto attr = parser_.call(
  4306. first,
  4307. last,
  4308. context,
  4309. skip,
  4310. detail::enable_attrs(flags),
  4311. success);
  4312. if (!success)
  4313. return;
  4314. if constexpr (detail::action_assignable_to_val_apply<
  4315. decltype(action_) &,
  4316. decltype(attr),
  4317. decltype(context)>()) {
  4318. _val(context) = detail::hl::apply(action_, std::move(attr));
  4319. } else {
  4320. BOOST_PARSER_SUBRANGE const where(initial_first, first);
  4321. auto const action_context =
  4322. detail::make_action_context(context, attr, where);
  4323. if constexpr (detail::action_assignable_to_val_direct<
  4324. decltype(action_) &,
  4325. decltype(action_context) &,
  4326. decltype(action_context) &>()) {
  4327. _val(action_context) = action_(action_context);
  4328. } else if constexpr (std::is_same_v<
  4329. decltype(action_(action_context)),
  4330. void>) {
  4331. action_(action_context);
  4332. } else {
  4333. // If you see an error here, it's because you are using an
  4334. // invocable for a semantic action that returns a non-void
  4335. // type Ret, but values of type Ret is not assignable to
  4336. // _val(ctx). To fix this, only use this invocable within
  4337. // a rule whose attribute type is assignable from Ret, or
  4338. // remove the non-void return statement(s) from your
  4339. // invocable.
  4340. [[maybe_unused]] none n = action_(action_context);
  4341. }
  4342. }
  4343. }
  4344. Parser parser_;
  4345. Action action_;
  4346. };
  4347. template<typename Parser, typename F>
  4348. struct transform_parser
  4349. {
  4350. template<
  4351. typename Iter,
  4352. typename Sentinel,
  4353. typename Context,
  4354. typename SkipParser>
  4355. auto call(
  4356. Iter & first,
  4357. Sentinel last,
  4358. Context const & context,
  4359. SkipParser const & skip,
  4360. detail::flags flags,
  4361. bool & success) const
  4362. {
  4363. [[maybe_unused]] auto _ = detail::scoped_trace(
  4364. *this, first, last, context, flags, detail::global_nope);
  4365. auto attr =
  4366. parser_.call(first, last, context, skip, flags, success);
  4367. if (success && detail::gen_attrs(flags))
  4368. return f_(std::move(attr));
  4369. else
  4370. return decltype(f_(std::move(attr))){};
  4371. }
  4372. template<
  4373. typename Iter,
  4374. typename Sentinel,
  4375. typename Context,
  4376. typename SkipParser,
  4377. typename Attribute>
  4378. void call(
  4379. Iter & first,
  4380. Sentinel last,
  4381. Context const & context,
  4382. SkipParser const & skip,
  4383. detail::flags flags,
  4384. bool & success,
  4385. Attribute & retval) const
  4386. {
  4387. [[maybe_unused]] auto _ = detail::scoped_trace(
  4388. *this, first, last, context, flags, retval);
  4389. auto attr =
  4390. parser_.call(first, last, context, skip, flags, success);
  4391. if (success && detail::gen_attrs(flags))
  4392. detail::assign(retval, f_(std::move(attr)));
  4393. }
  4394. Parser parser_;
  4395. F f_;
  4396. };
  4397. template<typename Parser>
  4398. struct omit_parser
  4399. {
  4400. template<
  4401. typename Iter,
  4402. typename Sentinel,
  4403. typename Context,
  4404. typename SkipParser>
  4405. detail::nope call(
  4406. Iter & first,
  4407. Sentinel last,
  4408. Context const & context,
  4409. SkipParser const & skip,
  4410. detail::flags flags,
  4411. bool & success) const
  4412. {
  4413. [[maybe_unused]] auto _ = detail::scoped_trace(
  4414. *this, first, last, context, flags, detail::global_nope);
  4415. parser_.call(
  4416. first,
  4417. last,
  4418. context,
  4419. skip,
  4420. detail::disable_attrs(flags),
  4421. success);
  4422. return {};
  4423. }
  4424. template<
  4425. typename Iter,
  4426. typename Sentinel,
  4427. typename Context,
  4428. typename SkipParser,
  4429. typename Attribute>
  4430. void call(
  4431. Iter & first,
  4432. Sentinel last,
  4433. Context const & context,
  4434. SkipParser const & skip,
  4435. detail::flags flags,
  4436. bool & success,
  4437. Attribute & retval) const
  4438. {
  4439. [[maybe_unused]] auto _ = detail::scoped_trace(
  4440. *this, first, last, context, flags, retval);
  4441. parser_.call(
  4442. first,
  4443. last,
  4444. context,
  4445. skip,
  4446. detail::disable_attrs(flags),
  4447. success);
  4448. }
  4449. Parser parser_;
  4450. };
  4451. template<typename Parser>
  4452. struct raw_parser
  4453. {
  4454. template<
  4455. typename Iter,
  4456. typename Sentinel,
  4457. typename Context,
  4458. typename SkipParser>
  4459. BOOST_PARSER_SUBRANGE<Iter> call(
  4460. Iter & first,
  4461. Sentinel last,
  4462. Context const & context,
  4463. SkipParser const & skip,
  4464. detail::flags flags,
  4465. bool & success) const
  4466. {
  4467. BOOST_PARSER_SUBRANGE<Iter> retval;
  4468. call(first, last, context, skip, flags, success, retval);
  4469. return retval;
  4470. }
  4471. template<
  4472. typename Iter,
  4473. typename Sentinel,
  4474. typename Context,
  4475. typename SkipParser,
  4476. typename Attribute>
  4477. void call(
  4478. Iter & first,
  4479. Sentinel last,
  4480. Context const & context,
  4481. SkipParser const & skip,
  4482. detail::flags flags,
  4483. bool & success,
  4484. Attribute & retval) const
  4485. {
  4486. [[maybe_unused]] auto _ = detail::scoped_trace(
  4487. *this, first, last, context, flags, retval);
  4488. auto const initial_first = first;
  4489. parser_.call(
  4490. first,
  4491. last,
  4492. context,
  4493. skip,
  4494. detail::disable_attrs(flags),
  4495. success);
  4496. if (success && detail::gen_attrs(flags))
  4497. detail::assign(
  4498. retval, BOOST_PARSER_SUBRANGE<Iter>(initial_first, first));
  4499. }
  4500. Parser parser_;
  4501. };
  4502. #if BOOST_PARSER_USE_CONCEPTS
  4503. template<typename Parser>
  4504. struct string_view_parser
  4505. {
  4506. template<
  4507. typename Iter,
  4508. typename Sentinel,
  4509. typename Context,
  4510. typename SkipParser>
  4511. auto call(
  4512. Iter & first,
  4513. Sentinel last,
  4514. Context const & context,
  4515. SkipParser const & skip,
  4516. detail::flags flags,
  4517. bool & success) const
  4518. {
  4519. auto r =
  4520. parser::detail::text::unpack_iterator_and_sentinel(first, last);
  4521. static_assert(
  4522. std::contiguous_iterator<decltype(r.first)>,
  4523. "string_view_parser and the string_view[] directive that uses "
  4524. "it requires that the underlying char sequence being parsed be "
  4525. "a contiguous range. If you're seeing this static_assert, you "
  4526. "have not met this contract.");
  4527. using char_type = detail::remove_cv_ref_t<decltype(*r.first)>;
  4528. std::basic_string_view<char_type> retval;
  4529. call(first, last, context, skip, flags, success, retval);
  4530. return retval;
  4531. }
  4532. template<
  4533. typename Iter,
  4534. typename Sentinel,
  4535. typename Context,
  4536. typename SkipParser,
  4537. typename Attribute>
  4538. void call(
  4539. Iter & first,
  4540. Sentinel last,
  4541. Context const & context,
  4542. SkipParser const & skip,
  4543. detail::flags flags,
  4544. bool & success,
  4545. Attribute & retval) const
  4546. {
  4547. [[maybe_unused]] auto _ = detail::scoped_trace(
  4548. *this, first, last, context, flags, retval);
  4549. auto const initial_first = first;
  4550. parser_.call(
  4551. first,
  4552. last,
  4553. context,
  4554. skip,
  4555. detail::disable_attrs(flags),
  4556. success);
  4557. if (!success || !detail::gen_attrs(flags))
  4558. return;
  4559. auto r = parser::detail::text::unpack_iterator_and_sentinel(
  4560. initial_first, first);
  4561. static_assert(
  4562. std::contiguous_iterator<decltype(r.first)>,
  4563. "string_view_parser and the string_view[] directive that uses "
  4564. "it requires that the underlying char sequence being parsed be "
  4565. "a contiguous range. If you're seeing this static_assert, you "
  4566. "have not met this contract.");
  4567. using char_type = detail::remove_cv_ref_t<decltype(*r.first)>;
  4568. if (initial_first == first) {
  4569. detail::assign(retval, std::basic_string_view<char_type>{});
  4570. } else {
  4571. detail::assign(
  4572. retval,
  4573. std::basic_string_view<char_type>{
  4574. &*r.first, std::size_t(r.last - r.first)});
  4575. }
  4576. }
  4577. Parser parser_;
  4578. };
  4579. #endif
  4580. template<typename Parser>
  4581. struct lexeme_parser
  4582. {
  4583. template<
  4584. typename Iter,
  4585. typename Sentinel,
  4586. typename Context,
  4587. typename SkipParser>
  4588. auto call(
  4589. Iter & first,
  4590. Sentinel last,
  4591. Context const & context,
  4592. SkipParser const & skip,
  4593. detail::flags flags,
  4594. bool & success) const
  4595. {
  4596. using attr_t = decltype(parser_.call(
  4597. first, last, context, skip, flags, success));
  4598. attr_t retval{};
  4599. call(first, last, context, skip, flags, success, retval);
  4600. return retval;
  4601. }
  4602. template<
  4603. typename Iter,
  4604. typename Sentinel,
  4605. typename Context,
  4606. typename SkipParser,
  4607. typename Attribute>
  4608. void call(
  4609. Iter & first,
  4610. Sentinel last,
  4611. Context const & context,
  4612. SkipParser const & skip,
  4613. detail::flags flags,
  4614. bool & success,
  4615. Attribute & retval) const
  4616. {
  4617. [[maybe_unused]] auto _ = detail::scoped_trace(
  4618. *this, first, last, context, flags, retval);
  4619. parser_.call(
  4620. first,
  4621. last,
  4622. context,
  4623. skip,
  4624. detail::disable_skip(flags),
  4625. success,
  4626. retval);
  4627. }
  4628. Parser parser_;
  4629. };
  4630. template<typename Parser>
  4631. struct no_case_parser
  4632. {
  4633. template<
  4634. typename Iter,
  4635. typename Sentinel,
  4636. typename Context,
  4637. typename SkipParser>
  4638. auto call(
  4639. Iter & first,
  4640. Sentinel last,
  4641. Context const & context_,
  4642. SkipParser const & skip,
  4643. detail::flags flags,
  4644. bool & success) const
  4645. {
  4646. auto context = context_;
  4647. ++context.no_case_depth_;
  4648. using attr_t = decltype(parser_.call(
  4649. first, last, context, skip, flags, success));
  4650. attr_t retval{};
  4651. call(first, last, context, skip, flags, success, retval);
  4652. return retval;
  4653. }
  4654. template<
  4655. typename Iter,
  4656. typename Sentinel,
  4657. typename Context,
  4658. typename SkipParser,
  4659. typename Attribute>
  4660. void call(
  4661. Iter & first,
  4662. Sentinel last,
  4663. Context const & context_,
  4664. SkipParser const & skip,
  4665. detail::flags flags,
  4666. bool & success,
  4667. Attribute & retval) const
  4668. {
  4669. auto context = context_;
  4670. ++context.no_case_depth_;
  4671. [[maybe_unused]] auto _ = detail::scoped_trace(
  4672. *this, first, last, context, flags, retval);
  4673. parser_.call(first, last, context, skip, flags, success, retval);
  4674. }
  4675. Parser parser_;
  4676. };
  4677. template<typename Parser, typename SkipParser>
  4678. struct skip_parser
  4679. {
  4680. template<
  4681. typename Iter,
  4682. typename Sentinel,
  4683. typename Context,
  4684. typename SkipParser_>
  4685. auto call(
  4686. Iter & first,
  4687. Sentinel last,
  4688. Context const & context,
  4689. SkipParser_ const & skip,
  4690. detail::flags flags,
  4691. bool & success) const
  4692. {
  4693. using attr_t = decltype(parser_.call(
  4694. first, last, context, skip, flags, success));
  4695. attr_t retval{};
  4696. call(first, last, context, skip, flags, success, retval);
  4697. return retval;
  4698. }
  4699. template<
  4700. typename Iter,
  4701. typename Sentinel,
  4702. typename Context,
  4703. typename SkipParser_,
  4704. typename Attribute>
  4705. void call(
  4706. Iter & first,
  4707. Sentinel last,
  4708. Context const & context,
  4709. SkipParser_ const & skip,
  4710. detail::flags flags,
  4711. bool & success,
  4712. Attribute & retval) const
  4713. {
  4714. [[maybe_unused]] auto _ = detail::scoped_trace(
  4715. *this, first, last, context, flags, retval);
  4716. if constexpr (detail::is_nope_v<SkipParser>) {
  4717. parser_.call(
  4718. first,
  4719. last,
  4720. context,
  4721. skip,
  4722. detail::enable_skip(flags),
  4723. success,
  4724. retval);
  4725. } else {
  4726. parser_.call(
  4727. first,
  4728. last,
  4729. context,
  4730. skip_parser_,
  4731. detail::enable_skip(flags),
  4732. success,
  4733. retval);
  4734. }
  4735. }
  4736. Parser parser_;
  4737. SkipParser skip_parser_;
  4738. };
  4739. template<typename Parser, bool FailOnMatch>
  4740. struct expect_parser
  4741. {
  4742. template<
  4743. typename Iter,
  4744. typename Sentinel,
  4745. typename Context,
  4746. typename SkipParser>
  4747. detail::nope call(
  4748. Iter & first,
  4749. Sentinel last,
  4750. Context const & context,
  4751. SkipParser const & skip,
  4752. detail::flags flags,
  4753. bool & success) const
  4754. {
  4755. detail::nope retval;
  4756. call(first, last, context, skip, flags, success, retval);
  4757. return retval;
  4758. }
  4759. template<
  4760. typename Iter,
  4761. typename Sentinel,
  4762. typename Context,
  4763. typename SkipParser,
  4764. typename Attribute>
  4765. void call(
  4766. Iter & first,
  4767. Sentinel last,
  4768. Context const & context,
  4769. SkipParser const & skip,
  4770. detail::flags flags,
  4771. bool & success,
  4772. Attribute & retval) const
  4773. {
  4774. [[maybe_unused]] auto _ = detail::scoped_trace(
  4775. *this, first, last, context, flags, retval);
  4776. auto first_copy = first;
  4777. parser_.call(
  4778. first_copy,
  4779. last,
  4780. context,
  4781. skip,
  4782. detail::disable_attrs(flags),
  4783. success);
  4784. if (FailOnMatch)
  4785. success = !success;
  4786. }
  4787. Parser parser_;
  4788. };
  4789. template<typename T>
  4790. struct symbol_parser
  4791. {
  4792. symbol_parser() : copied_from_(nullptr) {}
  4793. explicit symbol_parser(std::string_view diagnostic_text) :
  4794. copied_from_(nullptr), diagnostic_text_(diagnostic_text)
  4795. {}
  4796. symbol_parser(symbol_parser const & other) :
  4797. initial_elements_(other.initial_elements_),
  4798. copied_from_(other.copied_from_ ? other.copied_from_ : &other),
  4799. diagnostic_text_(other.diagnostic_text_)
  4800. {}
  4801. symbol_parser(symbol_parser && other) :
  4802. initial_elements_(std::move(other.initial_elements_)),
  4803. copied_from_(other.copied_from_),
  4804. diagnostic_text_(other.diagnostic_text_)
  4805. {}
  4806. /** Inserts an entry consisting of a UTF-8 string `str` to match, and
  4807. an associated attribute `x`, to `*this`. The entry is added for
  4808. use in all subsequent top-level parses. Subsequent lookups during
  4809. the current top-level parse will not necessarily match `str`. */
  4810. template<typename Context>
  4811. void insert_for_next_parse(
  4812. Context const & context, std::string_view str, T x)
  4813. {
  4814. auto & pending_ops =
  4815. detail::get_pending_symtab_ops(context, ref());
  4816. pending_ops.push_back(detail::symbol_table_operation<T>{
  4817. std::string(str),
  4818. std::move(x),
  4819. detail::symbol_table_op::insert});
  4820. }
  4821. /** Erases the entry whose UTF-8 match string is `str`, from `*this`.
  4822. The entry will no longer be available for use in all subsequent
  4823. top-level parses. `str` will not be removed from the symbols
  4824. matched in the current top-level parse. */
  4825. template<typename Context>
  4826. void erase_for_next_parse(Context const & context, std::string_view str)
  4827. {
  4828. auto & pending_ops =
  4829. detail::get_pending_symtab_ops(context, ref());
  4830. pending_ops.push_back(detail::symbol_table_operation<T>{
  4831. std::string(str),
  4832. std::nullopt,
  4833. detail::symbol_table_op::erase});
  4834. }
  4835. /** Erases all the entries from the copy of the symbol table inside
  4836. the parse context `context`. */
  4837. template<typename Context>
  4838. void clear_for_next_parse(Context const & context)
  4839. {
  4840. auto & pending_ops =
  4841. detail::get_pending_symtab_ops(context, ref());
  4842. pending_ops.push_back(detail::symbol_table_operation<T>{
  4843. {}, std::nullopt, detail::symbol_table_op::clear});
  4844. }
  4845. /** Uses UTF-8 string `str` to look up an attribute in the table
  4846. during parsing, returning it as an optional reference. The lookup
  4847. is done on the copy of the symbol table inside the parse context
  4848. `context`. */
  4849. template<typename Context>
  4850. parser::detail::text::optional_ref<T>
  4851. find(Context const & context, std::string_view str) const
  4852. {
  4853. auto [trie, has_case_folded] = detail::get_trie(context, ref());
  4854. if (context.no_case_depth_) {
  4855. return trie[detail::case_fold_view(
  4856. str | detail::text::as_utf32)];
  4857. } else {
  4858. return trie[str | detail::text::as_utf32];
  4859. }
  4860. }
  4861. /** Inserts an entry consisting of a UTF-8 string `str` to match, and
  4862. an associtated attribute `x`, to the copy of the symbol table
  4863. inside the parse context `context`. */
  4864. template<typename Context>
  4865. void insert(Context const & context, std::string_view str, T && x) const
  4866. {
  4867. auto [trie, has_case_folded] = detail::get_trie(context, ref());
  4868. if (context.no_case_depth_) {
  4869. trie.insert(
  4870. detail::case_fold_view(str | detail::text::as_utf32),
  4871. std::move(x));
  4872. } else {
  4873. trie.insert(str | detail::text::as_utf32, std::move(x));
  4874. }
  4875. }
  4876. /** Erases the entry whose UTF-8 match string is `str` from the copy
  4877. of the symbol table inside the parse context `context`. */
  4878. template<typename Context>
  4879. void erase(Context const & context, std::string_view str) const
  4880. {
  4881. auto [trie, has_case_folded] = detail::get_trie(context, ref());
  4882. if (context.no_case_depth_) {
  4883. trie.erase(
  4884. detail::case_fold_view(str | detail::text::as_utf32));
  4885. } else {
  4886. trie.erase(str | detail::text::as_utf32);
  4887. }
  4888. }
  4889. /** Erases the entry whose UTF-8 match string is `str` from the copy
  4890. of the symbol table inside the parse context `context`. */
  4891. template<typename Context>
  4892. void clear(Context const & context) const
  4893. {
  4894. auto [trie, _] = detail::get_trie(context, ref());
  4895. trie.clear();
  4896. }
  4897. template<
  4898. typename Iter,
  4899. typename Sentinel,
  4900. typename Context,
  4901. typename SkipParser>
  4902. T call(
  4903. Iter & first,
  4904. Sentinel last,
  4905. Context const & context,
  4906. SkipParser const & skip,
  4907. detail::flags flags,
  4908. bool & success) const
  4909. {
  4910. T retval{};
  4911. call(first, last, context, skip, flags, success, retval);
  4912. return retval;
  4913. }
  4914. template<
  4915. typename Iter,
  4916. typename Sentinel,
  4917. typename Context,
  4918. typename SkipParser,
  4919. typename Attribute>
  4920. void call(
  4921. Iter & first,
  4922. Sentinel last,
  4923. Context const & context,
  4924. SkipParser const & skip,
  4925. detail::flags flags,
  4926. bool & success,
  4927. Attribute & retval) const
  4928. {
  4929. [[maybe_unused]] auto _ = detail::scoped_trace(
  4930. *this, first, last, context, flags, retval);
  4931. auto [trie, _0] = detail::get_trie(context, ref());
  4932. auto const lookup = context.no_case_depth_
  4933. ? trie.longest_match(detail::case_fold_view(
  4934. BOOST_PARSER_SUBRANGE(first, last)))
  4935. : trie.longest_match(first, last);
  4936. if (lookup.match) {
  4937. std::advance(first, lookup.size);
  4938. detail::assign(retval, T{*trie[lookup]});
  4939. } else {
  4940. success = false;
  4941. }
  4942. }
  4943. mutable std::vector<std::pair<std::string, T>> initial_elements_;
  4944. symbol_parser const * copied_from_;
  4945. symbol_parser const & ref() const noexcept
  4946. {
  4947. if (copied_from_)
  4948. return *copied_from_;
  4949. return *this;
  4950. }
  4951. std::vector<std::pair<std::string, T>> &
  4952. initial_elements() const noexcept
  4953. {
  4954. return ref().initial_elements_;
  4955. }
  4956. std::string_view diagnostic_text_;
  4957. };
  4958. template<
  4959. bool CanUseCallbacks,
  4960. typename TagType,
  4961. typename Attribute,
  4962. typename LocalState,
  4963. typename ParamsTuple>
  4964. struct rule_parser
  4965. {
  4966. using tag_type = TagType;
  4967. using attr_type = Attribute;
  4968. using locals_type = LocalState;
  4969. template<
  4970. typename Iter,
  4971. typename Sentinel,
  4972. typename Context,
  4973. typename SkipParser>
  4974. std::conditional_t<
  4975. detail::in_recursion<Context, tag_type>,
  4976. detail::nope,
  4977. attr_type>
  4978. call(
  4979. Iter & first,
  4980. Sentinel last,
  4981. Context const & context,
  4982. SkipParser const & skip,
  4983. detail::flags flags,
  4984. bool & success) const
  4985. {
  4986. constexpr bool in_recursion =
  4987. detail::in_recursion<Context, tag_type>;
  4988. if constexpr (in_recursion)
  4989. flags = detail::disable_attrs(flags);
  4990. attr_type retval{};
  4991. locals_type locals = detail::make_locals<locals_type>(context);
  4992. auto params = detail::resolve_rule_params(context, params_);
  4993. tag_type * const tag_ptr = nullptr;
  4994. auto const rule_context = detail::make_rule_context(
  4995. context, tag_ptr, retval, locals, params);
  4996. [[maybe_unused]] auto _ = detail::scoped_trace(
  4997. *this, first, last, rule_context, flags, retval);
  4998. bool dont_assign = false;
  4999. if constexpr (in_recursion) {
  5000. // We have to use this out-arg overload for iterations >= 1 in
  5001. // recursive rules, since every iteration past the first is
  5002. // defined to return nope.
  5003. parse_rule(
  5004. tag_ptr,
  5005. first,
  5006. last,
  5007. rule_context,
  5008. skip,
  5009. flags,
  5010. success,
  5011. dont_assign,
  5012. retval);
  5013. } else {
  5014. auto attr = parse_rule(
  5015. tag_ptr,
  5016. first,
  5017. last,
  5018. rule_context,
  5019. skip,
  5020. flags,
  5021. success,
  5022. dont_assign);
  5023. if (success && !dont_assign) {
  5024. if constexpr (!detail::is_nope_v<decltype(attr)>)
  5025. detail::assign(retval, attr);
  5026. }
  5027. }
  5028. if constexpr (
  5029. CanUseCallbacks && Context::use_callbacks && !in_recursion) {
  5030. if (!success)
  5031. return attr_type{};
  5032. auto const & callbacks = _callbacks(context);
  5033. if constexpr (detail::is_nope_v<attr_type>) {
  5034. static_assert(
  5035. detail::is_invocable_v<decltype(callbacks), tag_type>,
  5036. "For rules without attributes, Callbacks must be a "
  5037. "struct with overloads of the form void(tag_type). If "
  5038. "you're seeing an error here, you probably have not "
  5039. "met this contract.");
  5040. callbacks(tag_type{});
  5041. } else {
  5042. static_assert(
  5043. detail::is_invocable_v<
  5044. decltype(callbacks),
  5045. tag_type,
  5046. decltype(std::move(retval))>,
  5047. "For rules with attributes, Callbacks must be a struct "
  5048. "with overloads of the form void(tag_type, attr_type). "
  5049. "If you're seeing an error here, you probably have not "
  5050. "met this contract.");
  5051. callbacks(tag_type{}, std::move(retval));
  5052. }
  5053. return attr_type{};
  5054. } else {
  5055. if (!success && !in_recursion)
  5056. detail::assign(retval, attr_type());
  5057. if constexpr (in_recursion)
  5058. return detail::nope{};
  5059. else
  5060. return retval;
  5061. }
  5062. }
  5063. template<
  5064. typename Iter,
  5065. typename Sentinel,
  5066. typename Context,
  5067. typename SkipParser,
  5068. typename Attribute_>
  5069. void call(
  5070. Iter & first,
  5071. Sentinel last,
  5072. Context const & context,
  5073. SkipParser const & skip,
  5074. detail::flags flags,
  5075. bool & success,
  5076. Attribute_ & retval) const
  5077. {
  5078. if constexpr (CanUseCallbacks && Context::use_callbacks) {
  5079. call(first, last, context, skip, flags, success);
  5080. } else {
  5081. locals_type locals = detail::make_locals<locals_type>(context);
  5082. auto params = detail::resolve_rule_params(context, params_);
  5083. tag_type * const tag_ptr = nullptr;
  5084. auto const rule_context = detail::make_rule_context(
  5085. context, tag_ptr, retval, locals, params);
  5086. [[maybe_unused]] auto _ = detail::scoped_trace(
  5087. *this, first, last, rule_context, flags, retval);
  5088. bool dont_assign = false;
  5089. parse_rule(
  5090. tag_ptr,
  5091. first,
  5092. last,
  5093. rule_context,
  5094. skip,
  5095. flags,
  5096. success,
  5097. dont_assign,
  5098. retval);
  5099. if (!success || dont_assign)
  5100. retval = Attribute_();
  5101. }
  5102. }
  5103. std::string_view diagnostic_text_;
  5104. ParamsTuple params_;
  5105. };
  5106. #endif
  5107. namespace detail {
  5108. template<typename T>
  5109. using base_member_function_template_expr =
  5110. decltype(std::declval<T>().template base<2>());
  5111. template<typename T>
  5112. constexpr bool has_base_member_function_template_v =
  5113. is_detected_v<base_member_function_template_expr, T>;
  5114. template<typename T>
  5115. using has_digits1_member_function_template_expr =
  5116. decltype(std::declval<T>().template digits<1>());
  5117. template<typename T>
  5118. constexpr bool has_digits1_member_function_template_v =
  5119. is_detected_v<has_digits1_member_function_template_expr, T>;
  5120. template<typename T>
  5121. using has_digits2_member_function_template_expr =
  5122. decltype(std::declval<T>().template digits<1, 2>());
  5123. template<typename T>
  5124. constexpr bool has_digits2_member_function_template_v =
  5125. is_detected_v<has_digits2_member_function_template_expr, T>;
  5126. }
  5127. // Parser interface.
  5128. template<typename Parser, typename GlobalState, typename ErrorHandler>
  5129. struct parser_interface
  5130. {
  5131. using parser_type = Parser;
  5132. using global_state_type = GlobalState;
  5133. using error_handler_type = ErrorHandler;
  5134. constexpr parser_interface() : parser_() {}
  5135. constexpr parser_interface(parser_type p) : parser_(std::move(p)) {}
  5136. constexpr parser_interface(
  5137. parser_type p, global_state_type gs, error_handler_type eh) :
  5138. parser_(p), globals_(gs), error_handler_(eh)
  5139. {}
  5140. /** Returns a `parser_interface` containing a parser equivalent to an
  5141. `expect_parser` containing `parser_`, with `FailOnMatch ==
  5142. true`. */
  5143. constexpr auto operator!() const noexcept
  5144. {
  5145. return parser::parser_interface{
  5146. expect_parser<parser_type, true>{parser_}};
  5147. }
  5148. /** Returns a `parser_interface` containing a parser equivalent to an
  5149. `expect_parser` containing `parser_`, with `FailOnMatch ==
  5150. false`. */
  5151. constexpr auto operator&() const noexcept
  5152. {
  5153. return parser::parser_interface{
  5154. expect_parser<parser_type, false>{parser_}};
  5155. }
  5156. /** Returns a `parser_interface` containing a parser equivalent to a
  5157. `zero_plus_parser` containing `parser_`. */
  5158. constexpr auto operator*() const noexcept
  5159. {
  5160. if constexpr (detail::is_zero_plus_p<parser_type>{}) {
  5161. return *this;
  5162. } else if constexpr (detail::is_one_plus_p<parser_type>{}) {
  5163. using inner_parser = decltype(parser_type::parser_);
  5164. return parser::parser_interface{
  5165. zero_plus_parser<inner_parser>(parser_.parser_)};
  5166. } else {
  5167. return parser::parser_interface{
  5168. zero_plus_parser<parser_type>(parser_)};
  5169. }
  5170. }
  5171. /** Returns a `parser_interface` containing a parser equivalent to a
  5172. `one_plus_parser` containing `parser_`. */
  5173. constexpr auto operator+() const noexcept
  5174. {
  5175. if constexpr (detail::is_zero_plus_p<parser_type>{}) {
  5176. using inner_parser = decltype(parser_type::parser_);
  5177. return parser::parser_interface{
  5178. zero_plus_parser<inner_parser>(parser_.parser_)};
  5179. } else if constexpr (detail::is_one_plus_p<parser_type>{}) {
  5180. return *this;
  5181. } else {
  5182. return parser::parser_interface{
  5183. one_plus_parser<parser_type>(parser_)};
  5184. }
  5185. }
  5186. /** Returns a `parser_interface` containing a parser equivalent to a
  5187. `opt_parser` containing `parser_`. */
  5188. constexpr auto operator-() const noexcept
  5189. {
  5190. return parser::parser_interface{opt_parser<parser_type>{parser_}};
  5191. }
  5192. /** Returns a `parser_interface` containing a parser equivalent to a
  5193. `seq_parser` containing `parser_` followed by `rhs.parser_`. */
  5194. template<typename ParserType2>
  5195. constexpr auto
  5196. operator>>(parser_interface<ParserType2> rhs) const noexcept
  5197. {
  5198. if constexpr (detail::is_seq_p<parser_type>{}) {
  5199. return parser_.template append<true>(rhs);
  5200. } else if constexpr (detail::is_seq_p<ParserType2>{}) {
  5201. return rhs.parser_.template prepend<true>(*this);
  5202. } else {
  5203. using parser_t = seq_parser<
  5204. tuple<parser_type, ParserType2>,
  5205. tuple<std::true_type, std::true_type>,
  5206. tuple<llong<0>, llong<0>>>;
  5207. return parser::parser_interface{parser_t{
  5208. tuple<parser_type, ParserType2>{parser_, rhs.parser_}}};
  5209. }
  5210. }
  5211. /** Returns a `parser_interface` containing a parser equivalent to a
  5212. `seq_parser` containing `parser_` followed by `lit(rhs)`. */
  5213. constexpr auto operator>>(char rhs) const noexcept;
  5214. /** Returns a `parser_interface` containing a parser equivalent to a
  5215. `seq_parser` containing `parser_` followed by `lit(rhs)`. */
  5216. constexpr auto operator>>(char32_t rhs) const noexcept;
  5217. /** Returns a `parser_interface` containing a parser equivalent to a
  5218. `seq_parser` containing `parser_` followed by `lit(rhs)`. */
  5219. #if BOOST_PARSER_USE_CONCEPTS
  5220. template<parsable_range_like R>
  5221. #else
  5222. template<
  5223. typename R,
  5224. typename Enable =
  5225. std::enable_if_t<detail::is_parsable_range_like_v<R>>>
  5226. #endif
  5227. constexpr auto operator>>(R && r) const noexcept;
  5228. /** Returns a `parser_interface` containing a parser equivalent to a
  5229. `seq_parser` containing `parser_` followed by `rhs.parser_`. No
  5230. back-tracking is allowed after `parser_` succeeds; if
  5231. `rhs.parser_` fails after `parser_` succeeds, the top-level parse
  5232. fails. */
  5233. template<typename ParserType2>
  5234. constexpr auto
  5235. operator>(parser_interface<ParserType2> rhs) const noexcept
  5236. {
  5237. if constexpr (detail::is_seq_p<parser_type>{}) {
  5238. return parser_.template append<false>(rhs);
  5239. } else if constexpr (detail::is_seq_p<ParserType2>{}) {
  5240. return rhs.parser_.template prepend<false>(*this);
  5241. } else {
  5242. using parser_t = seq_parser<
  5243. tuple<parser_type, ParserType2>,
  5244. tuple<std::true_type, std::false_type>,
  5245. tuple<llong<0>, llong<0>>>;
  5246. return parser::parser_interface{parser_t{
  5247. tuple<parser_type, ParserType2>{parser_, rhs.parser_}}};
  5248. }
  5249. }
  5250. /** Returns a `parser_interface` containing a parser equivalent to a
  5251. `seq_parser` containing `parser_` followed by `lit(rhs)`. No
  5252. back-tracking is allowed after `parser_` succeeds; if `lit(rhs)`
  5253. fails after `parser_` succeeds, the top-level parse fails. */
  5254. constexpr auto operator>(char rhs) const noexcept;
  5255. /** Returns a `parser_interface` containing a parser equivalent to a
  5256. `seq_parser` containing `parser_` followed by `lit(rhs)`. No
  5257. back-tracking is allowed after `parser_` succeeds; if `lit(rhs)`
  5258. fails after `parser_` succeeds, the top-level parse fails. */
  5259. constexpr auto operator>(char32_t rhs) const noexcept;
  5260. /** Returns a `parser_interface` containing a parser equivalent to a
  5261. `seq_parser` containing `parser_` followed by `lit(rhs)`. No
  5262. back-tracking is allowed after `parser_` succeeds; if `lit(rhs)`
  5263. fails after `parser_` succeeds, the top-level parse fails. */
  5264. #if BOOST_PARSER_USE_CONCEPTS
  5265. template<parsable_range_like R>
  5266. #else
  5267. template<
  5268. typename R,
  5269. typename Enable =
  5270. std::enable_if_t<detail::is_parsable_range_like_v<R>>>
  5271. #endif
  5272. constexpr auto operator>(R && r) const noexcept;
  5273. /** Returns a `parser_interface` containing a parser equivalent to an
  5274. `or_parser` containing `parser_` followed by `rhs.parser_`. */
  5275. template<typename ParserType2>
  5276. constexpr auto
  5277. operator|(parser_interface<ParserType2> rhs) const noexcept
  5278. {
  5279. if constexpr (detail::is_or_p<parser_type>{}) {
  5280. return parser_.append(rhs);
  5281. } else if constexpr (detail::is_or_p<ParserType2>{}) {
  5282. return rhs.parser_.prepend(*this);
  5283. } else {
  5284. // If you're seeing this as a compile- or run-time failure,
  5285. // you've tried to put an eps parser at the beginning of an
  5286. // alternative-parser, such as "eps | int_". This is not what
  5287. // you meant. Since eps always matches any input, "eps |
  5288. // int_" is just an awkward spelling for "eps". To fix this
  5289. // this, put the eps as the last alternative, so the other
  5290. // alternatives get a chance. Possibly, you may have meant to
  5291. // add a condition to the eps, like "eps(condition) | int_",
  5292. // which also is meaningful, and so is allowed.
  5293. BOOST_PARSER_ASSERT(
  5294. !detail::is_unconditional_eps<parser_type>{});
  5295. return parser::parser_interface{
  5296. or_parser<tuple<parser_type, ParserType2>>{
  5297. tuple<parser_type, ParserType2>{parser_, rhs.parser_}}};
  5298. }
  5299. }
  5300. /** Returns a `parser_interface` containing a parser equivalent to a
  5301. `perm_parser` containing `parser_` followed by `rhs.parser_`. It
  5302. is an error to use `eps` (conditional or not) with this
  5303. operator. */
  5304. template<typename ParserType2>
  5305. constexpr auto
  5306. operator||(parser_interface<ParserType2> rhs) const noexcept
  5307. {
  5308. // If you're seeing this as a compile- or run-time failure, you've
  5309. // tried to put an eps parser in a permutation-parser, such as
  5310. // "eps || int_".
  5311. BOOST_PARSER_ASSERT(!detail::is_eps_p<parser_type>{});
  5312. BOOST_PARSER_ASSERT(!detail::is_eps_p<ParserType2>{});
  5313. if constexpr (detail::is_perm_p<parser_type>{}) {
  5314. return parser_.append(rhs);
  5315. } else if constexpr (detail::is_perm_p<ParserType2>{}) {
  5316. return rhs.parser_.prepend(*this);
  5317. } else {
  5318. return parser::parser_interface{
  5319. perm_parser<tuple<parser_type, ParserType2>, detail::nope>{
  5320. tuple<parser_type, ParserType2>{parser_, rhs.parser_}}};
  5321. }
  5322. }
  5323. /** Returns a `parser_interface` containing a parser equivalent to an
  5324. `or_parser` containing `parser_` followed by `lit(rhs)`. */
  5325. constexpr auto operator|(char rhs) const noexcept;
  5326. /** Returns a `parser_interface` containing a parser equivalent to an
  5327. `or_parser` containing `parser_` followed by `lit(rhs)`. */
  5328. constexpr auto operator|(char32_t rhs) const noexcept;
  5329. /** Returns a `parser_interface` containing a parser equivalent to an
  5330. `or_parser` containing `parser_` followed by `lit(rhs)`. */
  5331. #if BOOST_PARSER_USE_CONCEPTS
  5332. template<parsable_range_like R>
  5333. #else
  5334. template<
  5335. typename R,
  5336. typename Enable =
  5337. std::enable_if_t<detail::is_parsable_range_like_v<R>>>
  5338. #endif
  5339. constexpr auto operator|(R && r) const noexcept;
  5340. /** Returns a `parser_interface` containing a parser equivalent to
  5341. `!rhs >> *this`. */
  5342. template<typename ParserType2>
  5343. constexpr auto
  5344. operator-(parser_interface<ParserType2> rhs) const noexcept
  5345. {
  5346. return !rhs >> *this;
  5347. }
  5348. /** Returns a `parser_interface` containing a parser equivalent to
  5349. `!lit(rhs) >> *this`. */
  5350. constexpr auto operator-(char rhs) const noexcept;
  5351. /** Returns a `parser_interface` containing a parser equivalent to
  5352. `!lit(rhs) >> *this`. */
  5353. constexpr auto operator-(char32_t rhs) const noexcept;
  5354. /** Returns a `parser_interface` containing a parser equivalent to
  5355. `!lit(rhs) >> *this`. */
  5356. #if BOOST_PARSER_USE_CONCEPTS
  5357. template<parsable_range_like R>
  5358. #else
  5359. template<
  5360. typename R,
  5361. typename Enable =
  5362. std::enable_if_t<detail::is_parsable_range_like_v<R>>>
  5363. #endif
  5364. constexpr auto operator-(R && r) const noexcept;
  5365. /** Returns a `parser_interface` containing a parser equivalent to an
  5366. `delimited_seq_parser` containing `parser_` and `rhs.parser_`. */
  5367. template<typename ParserType2>
  5368. constexpr auto
  5369. operator%(parser_interface<ParserType2> rhs) const noexcept
  5370. {
  5371. return parser::parser_interface{
  5372. delimited_seq_parser<parser_type, ParserType2>(
  5373. parser_, rhs.parser_)};
  5374. }
  5375. /** Returns a `parser_interface` containing a parser equivalent to an
  5376. `delimited_seq_parser` containing `parser_` and `lit(rhs)`. */
  5377. constexpr auto operator%(char rhs) const noexcept;
  5378. /** Returns a `parser_interface` containing a parser equivalent to an
  5379. `delimited_seq_parser` containing `parser_` and `lit(rhs)`. */
  5380. constexpr auto operator%(char32_t rhs) const noexcept;
  5381. /** Returns a `parser_interface` containing a parser equivalent to an
  5382. `delimited_seq_parser` containing `parser_` and `lit(rhs)`. */
  5383. #if BOOST_PARSER_USE_CONCEPTS
  5384. template<parsable_range_like R>
  5385. #else
  5386. template<
  5387. typename R,
  5388. typename Enable =
  5389. std::enable_if_t<detail::is_parsable_range_like_v<R>>>
  5390. #endif
  5391. constexpr auto operator%(R && r) const noexcept;
  5392. /** Returns a `parser_interface` containing a parser equivalent to an
  5393. `action_parser` containing `parser_`, with semantic action
  5394. `action`. */
  5395. template<typename Action>
  5396. constexpr auto operator[](Action action) const
  5397. {
  5398. using action_parser_t = action_parser<parser_type, Action>;
  5399. return parser::parser_interface{action_parser_t{parser_, action}};
  5400. }
  5401. /** Returns `parser_((Arg &&)arg, (Args &&)args...)`. This is useful
  5402. for those parsers that have `operator()` overloads,
  5403. e.g. `char_('x')`. By convention, parsers' `operator()`s return
  5404. `parser_interface`s.
  5405. This function does not participate in overload resolution unless
  5406. `parser_((Arg &&)arg, (Args &&)args...)` is well-formed. */
  5407. template<typename Arg, typename... Args>
  5408. constexpr auto operator()(Arg && arg, Args &&... args) const noexcept
  5409. -> decltype(std::declval<parser_type const &>()(
  5410. (Arg &&) arg, (Args &&) args...))
  5411. {
  5412. return parser_((Arg &&) arg, (Args &&) args...);
  5413. }
  5414. #ifndef BOOST_PARSER_DOXYGEN
  5415. /** Applies `parser_`, returning the parsed attribute, if any, unless
  5416. the attribute is reported via callback. */
  5417. template<
  5418. typename Iter,
  5419. typename Sentinel,
  5420. typename Context,
  5421. typename SkipParserType>
  5422. auto operator()(
  5423. Iter & first,
  5424. Sentinel last,
  5425. Context const & context,
  5426. SkipParserType const & skip,
  5427. detail::flags flags,
  5428. bool & success) const
  5429. {
  5430. return parser_.call(first, last, context, skip, flags, success);
  5431. }
  5432. /** Applies `parser_`, assinging the parsed attribute, if any, to
  5433. `attr`, unless the attribute is reported via callback. */
  5434. template<
  5435. typename Iter,
  5436. typename Sentinel,
  5437. typename Context,
  5438. typename SkipParserType,
  5439. typename Attribute>
  5440. void operator()(
  5441. Iter & first,
  5442. Sentinel last,
  5443. Context const & context,
  5444. SkipParserType const & skip,
  5445. detail::flags flags,
  5446. bool & success,
  5447. Attribute & attr) const
  5448. {
  5449. parser_.call(first, last, context, skip, flags, success, attr);
  5450. }
  5451. /** Returns a new `parser_interface` constructed from
  5452. `parser_.base<Radix2>()`. Note that this only works for integral
  5453. numeric parsers like `int_` and `uint_`. */
  5454. template<int Radix2>
  5455. constexpr auto base() const noexcept
  5456. {
  5457. if constexpr (detail::has_base_member_function_template_v<
  5458. parser_type>) {
  5459. return parser::parser_interface{
  5460. parser_.template base<Radix2>()};
  5461. } else {
  5462. static_assert(
  5463. detail::has_base_member_function_template_v<parser_type>,
  5464. "Only certain parsers have a .base<>() member function. "
  5465. "This is not one of them.");
  5466. }
  5467. }
  5468. /** Returns a new `parser_interface` constructed from
  5469. `parser_.digits<Digits>()`. Note that this only works for
  5470. integral numeric parsers like `int_` and `uint_`. */
  5471. template<int Digits>
  5472. constexpr auto digits() const noexcept
  5473. {
  5474. if constexpr (detail::has_digits1_member_function_template_v<
  5475. parser_type>) {
  5476. return parser::parser_interface{
  5477. parser_.template digits<Digits>()};
  5478. } else {
  5479. static_assert(
  5480. detail::has_digits1_member_function_template_v<parser_type>,
  5481. "Only certain parsers have a .base<>() member function. "
  5482. "This is not one of them.");
  5483. }
  5484. }
  5485. /** Returns a new `parser_interface` constructed from
  5486. `parser_.digits<MinDigits2, MaxDigits2>()`. Note that this only
  5487. works for integral numeric parsers like `int_` and `uint_`. */
  5488. template<int MinDigits2, int MaxDigits2>
  5489. constexpr auto digits() const noexcept
  5490. {
  5491. if constexpr (detail::has_digits2_member_function_template_v<
  5492. parser_type>) {
  5493. return parser::parser_interface{
  5494. parser_.template digits<MinDigits2, MaxDigits2>()};
  5495. } else {
  5496. static_assert(
  5497. detail::has_digits2_member_function_template_v<parser_type>,
  5498. "Only certain parsers have a .base<>() member function. "
  5499. "This is not one of them.");
  5500. }
  5501. }
  5502. parser_type parser_;
  5503. global_state_type globals_;
  5504. error_handler_type error_handler_;
  5505. #endif
  5506. using parser_interface_derivation_tag = int;
  5507. };
  5508. /** Returns a `parser_interface` with the same parser and error handler,
  5509. with `globals` added. The result of passing any non-top-level parser
  5510. for the `parser` argument is undefined. */
  5511. template<typename Parser, typename GlobalState, typename ErrorHandler>
  5512. auto with_globals(
  5513. parser_interface<Parser, detail::nope, ErrorHandler> const & parser,
  5514. GlobalState & globals)
  5515. {
  5516. return parser_interface<Parser, GlobalState &, ErrorHandler>{
  5517. parser.parser_, globals, parser.error_handler_};
  5518. }
  5519. /** Returns a `parser_interface` with the same parser and globals, with
  5520. `error_handler` added. The result of passing any non-top-level parser
  5521. for the `parser` argument is undefined. */
  5522. template<typename Parser, typename GlobalState, typename ErrorHandler>
  5523. auto with_error_handler(
  5524. parser_interface<Parser, GlobalState, default_error_handler> const &
  5525. parser,
  5526. ErrorHandler & error_handler)
  5527. {
  5528. return parser_interface<Parser, GlobalState, ErrorHandler &>{
  5529. parser.parser_, parser.globals_, error_handler};
  5530. }
  5531. /** A `symbols<T>` represents the initial state of a symbol table parser
  5532. that produces attributes of type `T`. The entries in the symbol table
  5533. can be changed during parsing, but those mutations do not affect the
  5534. `symbols<T>` object itself; all mutations happen to a copy of the
  5535. symbol table in the parse context. For table entries that should be
  5536. used during every parse, add entries via `add()` or `operator()`. For
  5537. mid-parse mutations, use `insert()` and `erase()`. */
  5538. template<typename T>
  5539. struct symbols : parser_interface<symbol_parser<T>>
  5540. {
  5541. symbols() {}
  5542. symbols(char const * diagnostic_text) :
  5543. parser_interface<symbol_parser<T>>(
  5544. symbol_parser<T>(diagnostic_text))
  5545. {}
  5546. symbols(std::initializer_list<std::pair<std::string_view, T>> il)
  5547. {
  5548. this->parser_.initial_elements_.resize(il.size());
  5549. std::copy(il.begin(), il.end(),
  5550. this->parser_.initial_elements_.begin());
  5551. }
  5552. symbols(
  5553. char const * diagnostic_text,
  5554. std::initializer_list<std::pair<std::string_view, T>> il) :
  5555. parser_interface<symbol_parser<T>>(
  5556. symbol_parser<T>(diagnostic_text))
  5557. {
  5558. this->parser_.initial_elements_.resize(il.size());
  5559. std::copy(il.begin(), il.end(),
  5560. this->parser_.initial_elements_.begin());
  5561. }
  5562. /** Inserts an entry consisting of a UTF-8 string `str` to match, and
  5563. an associated attribute `x`, to `*this`. The entry is added for
  5564. use in all subsequent top-level parses. Subsequent lookups during
  5565. the current top-level parse will not necessarily match `str`. */
  5566. void insert_for_next_parse(std::string_view str, T x)
  5567. {
  5568. this->parser_.initial_elements_.push_back(
  5569. std::pair(std::string(str), std::move(x)));
  5570. }
  5571. /** Erases the entry whose UTF-8 match string is `str`, from `*this`.
  5572. The entry will no longer be available for use in all subsequent
  5573. top-level parses. `str` will not be removed from the symbols
  5574. matched in the current top-level parse. */
  5575. void erase_for_next_parse(std::string_view str)
  5576. {
  5577. auto it = std::find_if(
  5578. this->parser_.initial_elements_.begin(),
  5579. this->parser_.initial_elements_.end(),
  5580. [str](auto const & x) { return x.first == str; });
  5581. this->parser_.initial_elements_.erase(it);
  5582. }
  5583. /** Erases all the entries from the copy of the symbol table inside
  5584. the parse context `context`. */
  5585. void clear_for_next_parse() { this->parser_.initial_elements_.clear(); }
  5586. /** Inserts an entry consisting of a UTF-8 string `str` to match, and
  5587. an associated attribute `x`, to `*this`. The entry is added for
  5588. use in all subsequent top-level parses. Subsequent lookups during
  5589. the current top-level parse will not necessarily match `str`. */
  5590. template<typename Context>
  5591. void insert_for_next_parse(
  5592. Context const & context, std::string_view str, T x)
  5593. {
  5594. this->parser_.insert_for_next_parse(context, str, std::move(x));
  5595. }
  5596. /** Erases the entry whose UTF-8 match string is `str`, from `*this`.
  5597. The entry will no longer be available for use in all subsequent
  5598. top-level parses. `str` will not be removed from the symbols
  5599. matched in the current top-level parse. */
  5600. template<typename Context>
  5601. void erase_for_next_parse(Context const & context, std::string_view str)
  5602. {
  5603. this->parser_.erase_for_next_parse(context, str);
  5604. }
  5605. /** Erases all the entries from the copy of the symbol table inside
  5606. the parse context `context`. */
  5607. template<typename Context>
  5608. void clear_for_next_parse(Context const & context)
  5609. {
  5610. this->parser_.clear_for_next_parse(context);
  5611. }
  5612. /** Uses UTF-8 string `str` to look up an attribute in the table
  5613. during parsing, returning it as an optional reference. The lookup
  5614. is done on the copy of the symbol table inside the parse context
  5615. `context`, not `*this`. */
  5616. template<typename Context>
  5617. parser::detail::text::optional_ref<T>
  5618. find(Context const & context, std::string_view str) const
  5619. {
  5620. return this->parser_.find(context, str);
  5621. }
  5622. /** Inserts an entry consisting of a UTF-8 string to match `str`, and
  5623. an associated attribute `x`, to the copy of the symbol table
  5624. inside the parse context `context`. */
  5625. template<typename Context>
  5626. void insert(Context const & context, std::string_view str, T x) const
  5627. {
  5628. this->parser_.insert(context, str, std::move(x));
  5629. }
  5630. /** Erases the entry whose UTF-8 match string is `str` from the copy
  5631. of the symbol table inside the parse context `context`. */
  5632. template<typename Context>
  5633. void erase(Context const & context, std::string_view str) const
  5634. {
  5635. this->parser_.erase(context, str);
  5636. }
  5637. /** Erases all the entries from the copy of the symbol table inside
  5638. the parse context `context`. */
  5639. template<typename Context>
  5640. void clear(Context const & context) const
  5641. {
  5642. this->parser_.clear(context);
  5643. }
  5644. };
  5645. #ifndef BOOST_PARSER_DOXYGEN
  5646. template<
  5647. typename TagType,
  5648. typename Attribute,
  5649. typename LocalState,
  5650. typename ParamsTuple>
  5651. struct rule
  5652. : parser_interface<
  5653. rule_parser<false, TagType, Attribute, LocalState, ParamsTuple>>
  5654. {
  5655. static_assert(
  5656. !std::is_same_v<TagType, void>,
  5657. "void is not a valid tag type for a rule.");
  5658. constexpr rule(char const * diagnostic_text)
  5659. {
  5660. this->parser_.diagnostic_text_ = diagnostic_text;
  5661. }
  5662. template<typename T, typename... Ts>
  5663. constexpr auto with(T && x, Ts &&... xs) const
  5664. {
  5665. BOOST_PARSER_ASSERT(
  5666. (detail::is_nope_v<ParamsTuple> &&
  5667. "If you're seeing this, you tried to chain calls on a rule, "
  5668. "like 'rule.with(foo).with(bar)'. Quit it!'"));
  5669. using params_tuple_type = decltype(detail::hl::make_tuple(
  5670. static_cast<T &&>(x), static_cast<Ts &&>(xs)...));
  5671. using rule_parser_type = rule_parser<
  5672. false,
  5673. TagType,
  5674. Attribute,
  5675. LocalState,
  5676. params_tuple_type>;
  5677. using result_type = parser_interface<rule_parser_type>;
  5678. return result_type{rule_parser_type{
  5679. this->parser_.diagnostic_text_,
  5680. detail::hl::make_tuple(
  5681. static_cast<T &&>(x), static_cast<Ts &&>(xs)...)}};
  5682. }
  5683. };
  5684. template<
  5685. typename TagType,
  5686. typename Attribute,
  5687. typename LocalState,
  5688. typename ParamsTuple>
  5689. struct callback_rule
  5690. : parser_interface<
  5691. rule_parser<true, TagType, Attribute, LocalState, ParamsTuple>>
  5692. {
  5693. constexpr callback_rule(char const * diagnostic_text)
  5694. {
  5695. this->parser_.diagnostic_text_ = diagnostic_text;
  5696. }
  5697. template<typename T, typename... Ts>
  5698. constexpr auto with(T && x, Ts &&... xs) const
  5699. {
  5700. BOOST_PARSER_ASSERT(
  5701. (detail::is_nope_v<ParamsTuple> &&
  5702. "If you're seeing this, you tried to chain calls on a "
  5703. "callback_rule, like 'rule.with(foo).with(bar)'. Quit it!'"));
  5704. using params_tuple_type = decltype(detail::hl::make_tuple(
  5705. static_cast<T &&>(x), static_cast<Ts &&>(xs)...));
  5706. using rule_parser_type = rule_parser<
  5707. true,
  5708. TagType,
  5709. Attribute,
  5710. LocalState,
  5711. params_tuple_type>;
  5712. using result_type = parser_interface<rule_parser_type>;
  5713. return result_type{rule_parser_type{
  5714. this->parser_.diagnostic_text_,
  5715. detail::hl::make_tuple(
  5716. static_cast<T &&>(x), static_cast<Ts &&>(xs)...)}};
  5717. }
  5718. };
  5719. //[ define_rule_definition
  5720. #define BOOST_PARSER_DEFINE_IMPL(_, rule_name_) \
  5721. template< \
  5722. typename Iter, \
  5723. typename Sentinel, \
  5724. typename Context, \
  5725. typename SkipParser> \
  5726. decltype(rule_name_)::parser_type::attr_type parse_rule( \
  5727. decltype(rule_name_)::parser_type::tag_type *, \
  5728. Iter & first, \
  5729. Sentinel last, \
  5730. Context const & context, \
  5731. SkipParser const & skip, \
  5732. boost::parser::detail::flags flags, \
  5733. bool & success, \
  5734. bool & dont_assign) \
  5735. { \
  5736. auto const & parser = BOOST_PARSER_PP_CAT(rule_name_, _def); \
  5737. using attr_t = \
  5738. decltype(parser(first, last, context, skip, flags, success)); \
  5739. using attr_type = decltype(rule_name_)::parser_type::attr_type; \
  5740. if constexpr (boost::parser::detail::is_nope_v<attr_t>) { \
  5741. dont_assign = true; \
  5742. parser(first, last, context, skip, flags, success); \
  5743. return {}; \
  5744. } else if constexpr (std::is_same_v<attr_type, attr_t>) { \
  5745. return parser(first, last, context, skip, flags, success); \
  5746. } else if constexpr (std::is_constructible_v<attr_type, attr_t>) { \
  5747. return attr_type( \
  5748. parser(first, last, context, skip, flags, success)); \
  5749. } else { \
  5750. attr_type attr{}; \
  5751. parser(first, last, context, skip, flags, success, attr); \
  5752. return attr; \
  5753. } \
  5754. } \
  5755. \
  5756. template< \
  5757. typename Iter, \
  5758. typename Sentinel, \
  5759. typename Context, \
  5760. typename SkipParser, \
  5761. typename Attribute> \
  5762. void parse_rule( \
  5763. decltype(rule_name_)::parser_type::tag_type *, \
  5764. Iter & first, \
  5765. Sentinel last, \
  5766. Context const & context, \
  5767. SkipParser const & skip, \
  5768. boost::parser::detail::flags flags, \
  5769. bool & success, \
  5770. bool & /*dont_assign*/, \
  5771. Attribute & retval) \
  5772. { \
  5773. auto const & parser = BOOST_PARSER_PP_CAT(rule_name_, _def); \
  5774. using attr_t = \
  5775. decltype(parser(first, last, context, skip, flags, success)); \
  5776. if constexpr (boost::parser::detail::is_nope_v<attr_t>) { \
  5777. parser(first, last, context, skip, flags, success); \
  5778. } else { \
  5779. parser(first, last, context, skip, flags, success, retval); \
  5780. } \
  5781. }
  5782. //]
  5783. #endif
  5784. /** For each given token `t`, defines a pair of `parse_rule()`
  5785. overloads, used internally within Boost.Parser. Each such pair
  5786. implements the parsing behavior rule `t`, using the parser `t_def`.
  5787. This implementation is in the form of a pair of function templates.
  5788. You should therefore write this macro only at namespace scope. */
  5789. #define BOOST_PARSER_DEFINE_RULES(...) \
  5790. BOOST_PARSER_PP_FOR_EACH(BOOST_PARSER_DEFINE_IMPL, _, __VA_ARGS__)
  5791. #ifndef BOOST_PARSER_DOXYGEN
  5792. template<typename ParserTuple>
  5793. template<typename Parser>
  5794. constexpr auto or_parser<ParserTuple>::prepend(
  5795. parser_interface<Parser> parser) const noexcept
  5796. {
  5797. // If you're seeing this as a compile- or run-time failure, you've
  5798. // tried to put an eps parser at the beginning of an
  5799. // alternative-parser, such as "eps | (int_ | double_)". This is not
  5800. // what you meant. Since eps always matches any input, "eps | (int_ |
  5801. // double_)" is just an awkward spelling for "eps". To fix this this,
  5802. // put the eps as the last alternative, so the other alternatives get
  5803. // a chance. Possibly, you may have meant to add a condition to the
  5804. // eps, like "eps(condition) | (int_ | double_)", which also is
  5805. // meaningful, and so is allowed.
  5806. BOOST_PARSER_ASSERT(!detail::is_unconditional_eps<Parser>{});
  5807. return parser_interface{
  5808. or_parser<decltype(detail::hl::prepend(parsers_, parser.parser_))>{
  5809. detail::hl::prepend(parsers_, parser.parser_)}};
  5810. }
  5811. template<typename ParserTuple>
  5812. template<typename Parser>
  5813. constexpr auto or_parser<ParserTuple>::append(
  5814. parser_interface<Parser> parser) const noexcept
  5815. {
  5816. // If you're seeing this as a compile- or run-time failure, you've
  5817. // tried to put an eps parser in the middle of an alternative-parser,
  5818. // such as "int_ | eps | double_". This is not what you meant. Since
  5819. // eps always matches any input, "int_ | eps | double_" is just an
  5820. // awkward spelling for "int_ | eps". To fix this this, put the eps
  5821. // as the last alternative, so the other alternatives get a chance.
  5822. // Possibly, you may have meant to add a condition to the eps, like
  5823. // "int_ | eps(condition) | double_", which also is meaningful, and so
  5824. // is allowed.
  5825. BOOST_PARSER_ASSERT(!detail::is_unconditional_eps_v<decltype(
  5826. detail::hl::back(parsers_))>);
  5827. if constexpr (detail::is_or_p<Parser>{}) {
  5828. return parser_interface{or_parser<decltype(
  5829. detail::hl::concat(parsers_, parser.parser_.parsers_))>{
  5830. detail::hl::concat(parsers_, parser.parser_.parsers_)}};
  5831. } else {
  5832. return parser_interface{or_parser<decltype(
  5833. detail::hl::append(parsers_, parser.parser_))>{
  5834. detail::hl::append(parsers_, parser.parser_)}};
  5835. }
  5836. }
  5837. template<typename ParserTuple, typename DelimiterParser>
  5838. template<typename Parser>
  5839. constexpr auto perm_parser<ParserTuple, DelimiterParser>::prepend(
  5840. parser_interface<Parser> parser) const noexcept
  5841. {
  5842. // If you're seeing this as a compile- or run-time failure, you've
  5843. // tried to put an eps parser in a permutation-parser, such as "eps ||
  5844. // int_".
  5845. BOOST_PARSER_ASSERT(!detail::is_eps_p<Parser>{});
  5846. return parser_interface{perm_parser<
  5847. decltype(detail::hl::prepend(parsers_, parser.parser_)),
  5848. detail::nope>{detail::hl::prepend(parsers_, parser.parser_)}};
  5849. }
  5850. template<typename ParserTuple, typename DelimiterParser>
  5851. template<typename Parser>
  5852. constexpr auto perm_parser<ParserTuple, DelimiterParser>::append(
  5853. parser_interface<Parser> parser) const noexcept
  5854. {
  5855. // If you're seeing this as a compile- or run-time failure, you've
  5856. // tried to put an eps parser in a permutation-parser, such as "int_
  5857. // || eps".
  5858. BOOST_PARSER_ASSERT(!detail::is_eps_p<Parser>{});
  5859. if constexpr (detail::is_perm_p<Parser>{}) {
  5860. return parser_interface{perm_parser<
  5861. decltype(detail::hl::concat(parsers_, parser.parser_.parsers_)),
  5862. detail::nope>{
  5863. detail::hl::concat(parsers_, parser.parser_.parsers_)}};
  5864. } else {
  5865. return parser_interface{perm_parser<
  5866. decltype(detail::hl::append(parsers_, parser.parser_)),
  5867. detail::nope>{detail::hl::append(parsers_, parser.parser_)}};
  5868. }
  5869. }
  5870. template<
  5871. typename ParserTuple,
  5872. typename BacktrackingTuple,
  5873. typename CombiningGroups>
  5874. template<bool AllowBacktracking, typename Parser>
  5875. constexpr auto
  5876. seq_parser<ParserTuple, BacktrackingTuple, CombiningGroups>::prepend(
  5877. parser_interface<Parser> parser) const noexcept
  5878. {
  5879. using combining_groups =
  5880. detail::combining_t<ParserTuple, CombiningGroups>;
  5881. using final_combining_groups =
  5882. decltype(detail::hl::prepend(combining_groups{}, llong<0>{}));
  5883. using backtracking = decltype(detail::hl::prepend(
  5884. detail::hl::prepend(
  5885. detail::hl::drop_front(BacktrackingTuple{}),
  5886. std::bool_constant<AllowBacktracking>{}),
  5887. std::true_type{}));
  5888. using parser_t = seq_parser<
  5889. decltype(detail::hl::prepend(parsers_, parser.parser_)),
  5890. backtracking,
  5891. final_combining_groups>;
  5892. return parser_interface{
  5893. parser_t{detail::hl::prepend(parsers_, parser.parser_)}};
  5894. }
  5895. template<
  5896. typename ParserTuple,
  5897. typename BacktrackingTuple,
  5898. typename CombiningGroups>
  5899. template<bool AllowBacktracking, typename Parser>
  5900. constexpr auto
  5901. seq_parser<ParserTuple, BacktrackingTuple, CombiningGroups>::append(
  5902. parser_interface<Parser> parser) const noexcept
  5903. {
  5904. using combining_groups =
  5905. detail::combining_t<ParserTuple, CombiningGroups>;
  5906. if constexpr (detail::is_seq_p<Parser>{}) {
  5907. using parser_combining_groups = detail::combining_t<
  5908. decltype(parser.parser_.parsers_),
  5909. typename Parser::combining_groups>;
  5910. using final_combining_groups = detail::
  5911. combined_combining_t<combining_groups, parser_combining_groups>;
  5912. using rhs_backtracking = decltype(detail::hl::prepend(
  5913. detail::hl::drop_front(typename Parser::backtracking{}),
  5914. std::bool_constant<AllowBacktracking>{}));
  5915. using backtracking = decltype(detail::hl::concat(
  5916. BacktrackingTuple{}, rhs_backtracking{}));
  5917. using parser_t = seq_parser<
  5918. decltype(detail::hl::concat(parsers_, parser.parser_.parsers_)),
  5919. backtracking,
  5920. final_combining_groups>;
  5921. return parser_interface{parser_t{
  5922. detail::hl::concat(parsers_, parser.parser_.parsers_)}};
  5923. } else {
  5924. using final_combining_groups =
  5925. decltype(detail::hl::append(combining_groups{}, llong<0>{}));
  5926. using backtracking = decltype(detail::hl::append(
  5927. BacktrackingTuple{}, std::bool_constant<AllowBacktracking>{}));
  5928. using parser_t = seq_parser<
  5929. decltype(detail::hl::append(parsers_, parser.parser_)),
  5930. backtracking,
  5931. final_combining_groups>;
  5932. return parser_interface{
  5933. parser_t{detail::hl::append(parsers_, parser.parser_)}};
  5934. }
  5935. }
  5936. #endif
  5937. // Directives.
  5938. /** Represents an unparameterized higher-order parser (e.g. `omit_parser`)
  5939. as a directive (e.g. `omit[other_parser]`). */
  5940. template<template<class> class Parser>
  5941. struct directive
  5942. {
  5943. template<typename Parser2>
  5944. constexpr auto operator[](parser_interface<Parser2> rhs) const noexcept
  5945. {
  5946. return parser_interface{Parser<Parser2>{rhs.parser_}};
  5947. }
  5948. };
  5949. /** The `omit` directive, whose `operator[]` returns a
  5950. `parser_interface<omit_parser<P>>` from a given parser of type
  5951. `parser_interface<P>`. */
  5952. inline constexpr directive<omit_parser> omit;
  5953. /** The `raw` directive, whose `operator[]` returns a
  5954. `parser_interface<raw_parser<P>>` from a given parser of type
  5955. `parser_interface<P>`. */
  5956. inline constexpr directive<raw_parser> raw;
  5957. #if defined(BOOST_PARSER_DOXYGEN) || BOOST_PARSER_USE_CONCEPTS
  5958. /** The `string_view` directive, whose `operator[]` returns a
  5959. `parser_interface<string_view_parser<P>>` from a given parser of type
  5960. `parser_interface<P>`. This is only available in C++20 and later. */
  5961. inline constexpr directive<string_view_parser> string_view;
  5962. #endif
  5963. /** The `lexeme` directive, whose `operator[]` returns a
  5964. `parser_interface<lexeme_parser<P>>` from a given parser of type
  5965. `parser_interface<P>`. */
  5966. inline constexpr directive<lexeme_parser> lexeme;
  5967. /** The `no_case` directive, whose `operator[]` returns a
  5968. `parser_interface<no_case_parser<P>>` from a given parser of type
  5969. `parser_interface<P>`. */
  5970. inline constexpr directive<no_case_parser> no_case;
  5971. /** Represents a `repeat_parser` as a directive
  5972. (e.g. `repeat[other_parser]`). */
  5973. template<typename MinType, typename MaxType>
  5974. struct repeat_directive
  5975. {
  5976. template<typename Parser2>
  5977. constexpr auto operator[](parser_interface<Parser2> rhs) const noexcept
  5978. {
  5979. using repeat_parser_type =
  5980. repeat_parser<Parser2, detail::nope, MinType, MaxType>;
  5981. return parser_interface{
  5982. repeat_parser_type{rhs.parser_, min_, max_}};
  5983. }
  5984. MinType min_;
  5985. MaxType max_;
  5986. };
  5987. /** Returns a `repeat_directive` that repeats exactly `n` times, and whose
  5988. `operator[]` returns a `parser_interface<repeat_parser<P>>` from a
  5989. given parser of type `parser_interface<P>`. */
  5990. template<typename T>
  5991. constexpr repeat_directive<T, T> repeat(T n) noexcept
  5992. {
  5993. return repeat_directive<T, T>{n, n};
  5994. }
  5995. /** Returns a `repeat_directive` that repeats between `min_` and `max_`
  5996. times, inclusive, and whose `operator[]` returns a
  5997. `parser_interface<repeat_parser<P>>` from a given parser of type
  5998. `parser_interface<P>`. */
  5999. template<typename MinType, typename MaxType>
  6000. constexpr repeat_directive<MinType, MaxType>
  6001. repeat(MinType min_, MaxType max_) noexcept
  6002. {
  6003. return repeat_directive<MinType, MaxType>{min_, max_};
  6004. }
  6005. /** A directive that represents a `perm_parser`, where the items parsed
  6006. are delimited by `DelimiterParser`
  6007. (e.g. `delimiter(delimter_parser)[some_perm_parser]`). This directive
  6008. only applies to `perm_parser`s. */
  6009. template<typename DelimiterParser>
  6010. struct delimiter_directive
  6011. {
  6012. template<typename ParserTuple, typename DelimiterParser2>
  6013. constexpr auto operator[](
  6014. parser_interface<perm_parser<ParserTuple, DelimiterParser2>> rhs)
  6015. const noexcept
  6016. {
  6017. using parser_type = perm_parser<ParserTuple, DelimiterParser>;
  6018. return parser_interface{
  6019. parser_type{rhs.parser_.parsers_, delimiter_parser_}};
  6020. }
  6021. DelimiterParser delimiter_parser_;
  6022. };
  6023. /** Returns a `delimiter_directive` whose `operator[]` returns a
  6024. `perm_parser`, where the items parsed are delimited by
  6025. `delimiter_parser`. */
  6026. template<typename DelimiterParser>
  6027. constexpr delimiter_directive<DelimiterParser>
  6028. delimiter(parser_interface<DelimiterParser> delimiter_parser) noexcept
  6029. {
  6030. return delimiter_directive<DelimiterParser>{delimiter_parser.parser_};
  6031. }
  6032. /** Represents a skip parser as a directive. When used without a skip
  6033. parser, e.g. `skip[parser_in_which_to_do_skipping]`, the skipper for
  6034. the entire parse is used. When given another parser, e.g.
  6035. `skip(skip_parser)[parser_in_which_to_do_skipping]`, that other parser
  6036. is used as the skipper within the directive. */
  6037. template<typename SkipParser = detail::nope>
  6038. struct skip_directive
  6039. {
  6040. template<typename Parser>
  6041. constexpr auto operator[](parser_interface<Parser> rhs) const noexcept
  6042. {
  6043. return parser_interface{
  6044. skip_parser<Parser, SkipParser>{rhs.parser_, skip_parser_}};
  6045. }
  6046. /** Returns a `skip_directive` with `skip_parser` as its skipper. */
  6047. template<typename SkipParser2>
  6048. constexpr auto
  6049. operator()(parser_interface<SkipParser2> skip_parser) const noexcept
  6050. {
  6051. BOOST_PARSER_ASSERT(
  6052. (detail::is_nope_v<SkipParser> &&
  6053. "If you're seeing this, you tried to chain calls on skip, "
  6054. "like 'skip(foo)(bar)'. Quit it!'"));
  6055. return skip_directive<parser_interface<SkipParser2>>{skip_parser};
  6056. }
  6057. SkipParser skip_parser_;
  6058. };
  6059. /** The `skip_directive`, whose `operator[]` returns a
  6060. `parser_interface<skip_parser<P>>` from a given parser of type
  6061. `parser_interface<P>`. */
  6062. inline constexpr skip_directive<> skip;
  6063. /** A directive type that can only be used on sequence parsers, that
  6064. forces the merge of all the sequence_parser's subparser's attributes
  6065. into a single attribute. */
  6066. struct merge_directive
  6067. {
  6068. template<
  6069. typename ParserTuple,
  6070. typename BacktrackingTuple,
  6071. typename CombiningGroups>
  6072. constexpr auto
  6073. operator[](parser_interface<
  6074. seq_parser<ParserTuple, BacktrackingTuple, CombiningGroups>>
  6075. rhs) const noexcept
  6076. {
  6077. return parser_interface{
  6078. seq_parser<ParserTuple, BacktrackingTuple, detail::merge_t>{
  6079. rhs.parser_.parsers_}};
  6080. }
  6081. };
  6082. /** The `merge_directive`, whose `operator[]` returns a
  6083. `parser_interface<P2>`, from a given parser of type
  6084. `parser_interface<P>`, where `P` is a `seq_parser`. `P2` is the same
  6085. as `P`, except that its `CombiningGroups` template parameter is
  6086. replaced with a tag type that causes the subparser's attributes to be
  6087. merged into a single attribute. */
  6088. inline constexpr merge_directive merge;
  6089. /** A directive type that can only be used on sequence parsers, that
  6090. prevents each of the sequence_parser's subparser's attributes from
  6091. merging with any other subparser's attribute. */
  6092. struct separate_directive
  6093. {
  6094. template<
  6095. typename ParserTuple,
  6096. typename BacktrackingTuple,
  6097. typename CombiningGroups>
  6098. constexpr auto
  6099. operator[](parser_interface<
  6100. seq_parser<ParserTuple, BacktrackingTuple, CombiningGroups>>
  6101. rhs) const noexcept
  6102. {
  6103. return parser_interface{
  6104. seq_parser<ParserTuple, BacktrackingTuple, detail::separate_t>{
  6105. rhs.parser_.parsers_}};
  6106. }
  6107. };
  6108. /** The `separate_directive`, whose `operator[]` returns a
  6109. `parser_interface<P2>`, from a given parser of type
  6110. `parser_interface<P>`, where `P` is a `seq_parser`. `P2` is the same
  6111. as `P`, except that its `CombiningGroups` template parameter is
  6112. replaced with a tag type that prevents each subparser's attribute from
  6113. merging with any other subparser's attribute. */
  6114. inline constexpr separate_directive separate;
  6115. /** A directive that transforms the attribute generated by a parser.
  6116. `operator[]` returns a `parser_interface<transform_parser<Parser,
  6117. F>>`. */
  6118. template<typename F>
  6119. struct transform_directive
  6120. {
  6121. template<typename Parser>
  6122. constexpr auto operator[](parser_interface<Parser> rhs) const noexcept
  6123. {
  6124. return parser_interface{
  6125. transform_parser<Parser, F>{rhs.parser_, f_}};
  6126. }
  6127. F f_;
  6128. };
  6129. /** Returns a `transform_directive` that uses invocable `F` to do its
  6130. work. */
  6131. template<typename F>
  6132. constexpr auto transform(F f)
  6133. {
  6134. return transform_directive<F>{std::move(f)};
  6135. }
  6136. // First order parsers.
  6137. #ifndef BOOST_PARSER_DOXYGEN
  6138. template<typename Predicate>
  6139. struct eps_parser
  6140. {
  6141. template<
  6142. typename Iter,
  6143. typename Sentinel,
  6144. typename Context,
  6145. typename SkipParser>
  6146. detail::nope call(
  6147. Iter & first,
  6148. Sentinel last,
  6149. Context const & context,
  6150. SkipParser const & skip,
  6151. detail::flags flags,
  6152. bool & success) const noexcept
  6153. {
  6154. [[maybe_unused]] auto _ = detail::scoped_trace(
  6155. *this, first, last, context, flags, detail::global_nope);
  6156. BOOST_PARSER_SUBRANGE const where(first, first);
  6157. auto const predicate_context = detail::make_action_context(
  6158. context, detail::global_nope, where);
  6159. // Predicate must be a parse predicate. If you see an error here,
  6160. // you have not met this contract. See the terminology section of
  6161. // the online docs if you don't know what that a parse predicate
  6162. // is.
  6163. success = pred_(predicate_context);
  6164. return {};
  6165. }
  6166. template<
  6167. typename Iter,
  6168. typename Sentinel,
  6169. typename Context,
  6170. typename SkipParser,
  6171. typename Attribute>
  6172. void call(
  6173. Iter & first,
  6174. Sentinel last,
  6175. Context const & context,
  6176. SkipParser const & skip,
  6177. detail::flags flags,
  6178. bool & success,
  6179. Attribute & retval) const
  6180. {
  6181. [[maybe_unused]] auto _ = detail::scoped_trace(
  6182. *this, first, last, context, flags, retval);
  6183. BOOST_PARSER_SUBRANGE const where(first, first);
  6184. auto const predicate_context = detail::make_action_context(
  6185. context, detail::global_nope, where);
  6186. // Predicate must be a parse predicate. If you see an error here,
  6187. // you have not met this contract. See the terminology section of
  6188. // the online docs if you don't know what that a parse predicate
  6189. // is.
  6190. success = pred_(predicate_context);
  6191. }
  6192. /** Returns a `parser_interface` containing an `eps_parser` that will
  6193. fail if `pred` evaluates to false. */
  6194. template<typename Predicate2>
  6195. constexpr auto operator()(Predicate2 pred) const noexcept
  6196. {
  6197. BOOST_PARSER_ASSERT(
  6198. (detail::is_nope_v<Predicate> &&
  6199. "If you're seeing this, you tried to chain calls on eps, "
  6200. "like 'eps(foo)(bar)'. Quit it!'"));
  6201. return parser_interface{eps_parser<Predicate2>{std::move(pred)}};
  6202. }
  6203. Predicate pred_;
  6204. };
  6205. #endif
  6206. /** The epsilon parser. This matches anything, and consumes no input. If
  6207. used with an optional predicate, like `eps(pred)`, it matches iff
  6208. `pred(ctx)` evaluates to true, where `ctx` is the parser context. */
  6209. inline constexpr parser_interface<eps_parser<detail::nope>> eps;
  6210. #ifndef BOOST_PARSER_DOXYGEN
  6211. struct eoi_parser
  6212. {
  6213. template<
  6214. typename Iter,
  6215. typename Sentinel,
  6216. typename Context,
  6217. typename SkipParser>
  6218. detail::nope call(
  6219. Iter & first,
  6220. Sentinel last,
  6221. Context const & context,
  6222. SkipParser const & skip,
  6223. detail::flags flags,
  6224. bool & success) const
  6225. {
  6226. [[maybe_unused]] auto _ = detail::scoped_trace(
  6227. *this, first, last, context, flags, detail::global_nope);
  6228. if (first != last)
  6229. success = false;
  6230. return {};
  6231. }
  6232. template<
  6233. typename Iter,
  6234. typename Sentinel,
  6235. typename Context,
  6236. typename SkipParser,
  6237. typename Attribute>
  6238. void call(
  6239. Iter & first,
  6240. Sentinel last,
  6241. Context const & context,
  6242. SkipParser const & skip,
  6243. detail::flags flags,
  6244. bool & success,
  6245. Attribute & retval) const
  6246. {
  6247. [[maybe_unused]] auto _ = detail::scoped_trace(
  6248. *this, first, last, context, flags, retval);
  6249. if (first != last)
  6250. success = false;
  6251. }
  6252. };
  6253. #endif
  6254. /** The end-of-input parser. It matches only the end of input. */
  6255. inline constexpr parser_interface<eoi_parser> eoi;
  6256. #ifndef BOOST_PARSER_DOXYGEN
  6257. template<typename Attribute>
  6258. struct attr_parser
  6259. {
  6260. template<
  6261. typename Iter,
  6262. typename Sentinel,
  6263. typename Context,
  6264. typename SkipParser>
  6265. auto call(
  6266. Iter & first,
  6267. Sentinel last,
  6268. Context const & context,
  6269. SkipParser const &,
  6270. detail::flags flags,
  6271. bool &) const
  6272. {
  6273. [[maybe_unused]] auto _ = detail::scoped_trace(
  6274. *this, first, last, context, flags, detail::global_nope);
  6275. return detail::resolve(context, attr_);
  6276. }
  6277. template<
  6278. typename Iter,
  6279. typename Sentinel,
  6280. typename Context,
  6281. typename SkipParser,
  6282. typename Attribute_>
  6283. void call(
  6284. Iter & first,
  6285. Sentinel last,
  6286. Context const & context,
  6287. SkipParser const & skip,
  6288. detail::flags flags,
  6289. bool & success,
  6290. Attribute_ & retval) const
  6291. {
  6292. [[maybe_unused]] auto _ = detail::scoped_trace(
  6293. *this, first, last, context, flags, retval);
  6294. if (detail::gen_attrs(flags))
  6295. detail::assign_copy(retval, detail::resolve(context, attr_));
  6296. }
  6297. Attribute attr_;
  6298. };
  6299. #endif
  6300. /** Returns an `attr_parser` which matches anything, and consumes no
  6301. input, and which produces `a` as its attribute. */
  6302. template<typename Attribute>
  6303. constexpr auto attr(Attribute a) noexcept
  6304. {
  6305. return parser_interface{attr_parser<Attribute>{std::move(a)}};
  6306. }
  6307. #ifndef BOOST_PARSER_DOXYGEN
  6308. template<typename Expected, typename AttributeType>
  6309. struct char_parser
  6310. {
  6311. constexpr char_parser() {}
  6312. constexpr char_parser(Expected expected) : expected_(expected) {}
  6313. template<typename T>
  6314. using attribute_type = std::conditional_t<
  6315. std::is_same_v<AttributeType, void>,
  6316. std::decay_t<T>,
  6317. AttributeType>;
  6318. template<
  6319. typename Iter,
  6320. typename Sentinel,
  6321. typename Context,
  6322. typename SkipParser>
  6323. auto call(
  6324. Iter & first,
  6325. Sentinel last,
  6326. Context const & context,
  6327. SkipParser const & skip,
  6328. detail::flags flags,
  6329. bool & success) const -> attribute_type<decltype(*first)>
  6330. {
  6331. attribute_type<decltype(*first)> retval{};
  6332. call(first, last, context, skip, flags, success, retval);
  6333. return retval;
  6334. }
  6335. template<
  6336. typename Iter,
  6337. typename Sentinel,
  6338. typename Context,
  6339. typename SkipParser,
  6340. typename Attribute>
  6341. void call(
  6342. Iter & first,
  6343. Sentinel last,
  6344. Context const & context,
  6345. SkipParser const & skip,
  6346. detail::flags flags,
  6347. bool & success,
  6348. Attribute & retval) const
  6349. {
  6350. [[maybe_unused]] auto _ = detail::scoped_trace(
  6351. *this, first, last, context, flags, retval);
  6352. if (first == last) {
  6353. success = false;
  6354. return;
  6355. }
  6356. attribute_type<decltype(*first)> const x = *first;
  6357. if (detail::unequal(context, x, expected_)) {
  6358. success = false;
  6359. return;
  6360. }
  6361. detail::assign(retval, x);
  6362. ++first;
  6363. }
  6364. /** Returns a `parser_interface` containing a `char_parser` that
  6365. matches `x`.
  6366. \tparam T Constrained by `!parsable_range_like<T>`. */
  6367. #if BOOST_PARSER_USE_CONCEPTS
  6368. template<typename T>
  6369. requires(!parsable_range_like<T>)
  6370. #else
  6371. template<
  6372. typename T,
  6373. typename Enable =
  6374. std::enable_if_t<!detail::is_parsable_range_like_v<T>>>
  6375. #endif
  6376. constexpr auto operator()(T x) const noexcept
  6377. {
  6378. BOOST_PARSER_ASSERT(
  6379. (detail::is_nope_v<Expected> &&
  6380. "If you're seeing this, you tried to chain calls on char_, "
  6381. "like 'char_('a')('b')'. Quit it!'"));
  6382. return parser_interface{
  6383. char_parser<T, AttributeType>{std::move(x)}};
  6384. }
  6385. /** Returns a `parser_interface` containing a `char_parser` that
  6386. matches any value in `[lo, hi]`. */
  6387. template<typename LoType, typename HiType>
  6388. constexpr auto operator()(LoType lo, HiType hi) const noexcept
  6389. {
  6390. BOOST_PARSER_ASSERT(
  6391. (detail::is_nope_v<Expected> &&
  6392. "If you're seeing this, you tried to chain calls on char_, "
  6393. "like 'char_('a', 'b')('c', 'd')'. Quit it!'"));
  6394. using char_pair_t = detail::char_pair<LoType, HiType>;
  6395. using char_parser_t = char_parser<char_pair_t, AttributeType>;
  6396. return parser_interface(
  6397. char_parser_t(char_pair_t{std::move(lo), std::move(hi)}));
  6398. }
  6399. /** Returns a `parser_interface` containing a `char_parser` that
  6400. matches one of the values in `r`. If the character being matched
  6401. during the parse is a `char32_t` value, the elements of `r` are
  6402. transcoded from their presumed encoding to UTF-32 during the
  6403. comparison. Otherwise, the character begin matched is directly
  6404. compared to the elements of `r`. */
  6405. #if BOOST_PARSER_USE_CONCEPTS
  6406. template<parsable_range_like R>
  6407. #else
  6408. template<
  6409. typename R,
  6410. typename Enable =
  6411. std::enable_if_t<detail::is_parsable_range_like_v<R>>>
  6412. #endif
  6413. constexpr auto operator()(R && r) const noexcept
  6414. {
  6415. BOOST_PARSER_ASSERT(
  6416. ((!std::is_rvalue_reference_v<R &&> ||
  6417. !detail::is_range<detail::remove_cv_ref_t<R>>) &&
  6418. "It looks like you tried to pass an rvalue range to "
  6419. "char_(). Don't do that, or you'll end up with dangling "
  6420. "references."));
  6421. BOOST_PARSER_ASSERT(
  6422. (detail::is_nope_v<Expected> &&
  6423. "If you're seeing this, you tried to chain calls on char_, "
  6424. "like 'char_(char-set)(char-set)'. Quit it!'"));
  6425. auto chars = detail::make_char_range<false>(r);
  6426. using char_range_t = decltype(chars);
  6427. using char_parser_t = char_parser<char_range_t, AttributeType>;
  6428. return parser_interface(char_parser_t(chars));
  6429. }
  6430. /** Returns a `parser_interface` containing a `char_parser` that
  6431. matches one of the values in `r`. `r` must be a sorted,
  6432. random-access sequence of `char32_t`. The character begin matched
  6433. is directly compared to the elements of `r`. The match is found
  6434. via binary search. No case folding is performed.
  6435. \tparam R Additionally constrained by
  6436. `std::same_as<std::ranges::range_value_t<R>, char32_t>`. */
  6437. #if BOOST_PARSER_USE_CONCEPTS
  6438. template<parsable_range_like R>
  6439. requires std::same_as<std::ranges::range_value_t<R>, char32_t>
  6440. #else
  6441. template<
  6442. typename R,
  6443. typename Enable = std::enable_if_t<
  6444. detail::is_parsable_range_like_v<R> &&
  6445. std::is_same_v<detail::range_value_t<R>, char32_t>>>
  6446. #endif
  6447. constexpr auto operator()(sorted_t, R && r) const noexcept
  6448. {
  6449. BOOST_PARSER_ASSERT(
  6450. ((!std::is_rvalue_reference_v<R &&> ||
  6451. !detail::is_range<detail::remove_cv_ref_t<R>>) &&
  6452. "It looks like you tried to pass an rvalue range to "
  6453. "char_(). Don't do that, or you'll end up with dangling "
  6454. "references."));
  6455. BOOST_PARSER_ASSERT(
  6456. (detail::is_nope_v<Expected> &&
  6457. "If you're seeing this, you tried to chain calls on char_, "
  6458. "like 'char_(char-set)(char-set)'. Quit it!'"));
  6459. auto chars = detail::make_char_range<true>(r);
  6460. using char_range_t = decltype(chars);
  6461. using char_parser_t = char_parser<char_range_t, AttributeType>;
  6462. return parser_interface(char_parser_t(chars));
  6463. }
  6464. Expected expected_;
  6465. };
  6466. struct digit_parser
  6467. {
  6468. constexpr digit_parser() {}
  6469. template<typename T>
  6470. using attribute_type = std::decay_t<T>;
  6471. template<
  6472. typename Iter,
  6473. typename Sentinel,
  6474. typename Context,
  6475. typename SkipParser>
  6476. auto call(
  6477. Iter & first,
  6478. Sentinel last,
  6479. Context const & context,
  6480. SkipParser const & skip,
  6481. detail::flags flags,
  6482. bool & success) const -> attribute_type<decltype(*first)>
  6483. {
  6484. attribute_type<decltype(*first)> retval{};
  6485. call(first, last, context, skip, flags, success, retval);
  6486. return retval;
  6487. }
  6488. template<
  6489. typename Iter,
  6490. typename Sentinel,
  6491. typename Context,
  6492. typename SkipParser,
  6493. typename Attribute>
  6494. void call(
  6495. Iter & first,
  6496. Sentinel last,
  6497. Context const & context,
  6498. SkipParser const & skip,
  6499. detail::flags flags,
  6500. bool & success,
  6501. Attribute & retval) const
  6502. {
  6503. [[maybe_unused]] auto _ = detail::scoped_trace(
  6504. *this, first, last, context, flags, retval);
  6505. if (first == last) {
  6506. success = false;
  6507. return;
  6508. }
  6509. attribute_type<decltype(*first)> const x = *first;
  6510. char32_t const x_cmp = x;
  6511. if (x_cmp < U'\x0100' && (x_cmp < U'0' || U'9' < x_cmp)) {
  6512. success = false;
  6513. return;
  6514. }
  6515. char32_t const * it = std::upper_bound(
  6516. std::begin(zero_cps) + 1, std::end(zero_cps), x_cmp);
  6517. if (it == std::begin(zero_cps) || x_cmp < *(it - 1) ||
  6518. *(it - 1) + 9 < x_cmp) {
  6519. success = false;
  6520. return;
  6521. }
  6522. detail::assign(retval, x);
  6523. ++first;
  6524. }
  6525. // Produced from
  6526. // https://util.unicode.org/UnicodeJsps/list-unicodeset.jsp, using
  6527. // "[:nt=De:]" for the Input field.
  6528. static constexpr char32_t zero_cps[] = {
  6529. U'\u0030', // U+0030 DIGIT ZERO
  6530. U'\u0660', // U+0660 ARABIC-INDIC DIGIT ZERO
  6531. U'\u06F0', // U+06F0 EXTENDED ARABIC-INDIC DIGIT ZERO
  6532. U'\u07C0', // U+07C0 NKO DIGIT ZERO
  6533. U'\u0966', // U+0966 DEVANAGARI DIGIT ZERO
  6534. U'\u09E6', // U+09E6 BENGALI DIGIT ZERO
  6535. U'\u0A66', // U+0A66 GURMUKHI DIGIT ZERO
  6536. U'\u0AE6', // U+0AE6 GUJARATI DIGIT ZERO
  6537. U'\u0B66', // U+0B66 ORIYA DIGIT ZERO
  6538. U'\u0BE6', // U+0BE6 TAMIL DIGIT ZERO
  6539. U'\u0C66', // U+0C66 TELUGU DIGIT ZERO
  6540. U'\u0CE6', // U+0CE6 KANNADA DIGIT ZERO
  6541. U'\u0D66', // U+0D66 MALAYALAM DIGIT ZERO
  6542. U'\u0DE6', // U+0DE6 SINHALA LITH DIGIT ZERO
  6543. U'\u0E50', // U+0E50 THAI DIGIT ZERO
  6544. U'\u0ED0', // U+0ED0 LAO DIGIT ZERO
  6545. U'\u0F20', // U+0F20 TIBETAN DIGIT ZERO
  6546. U'\u1040', // U+1040 MYANMAR DIGIT ZERO
  6547. U'\u1090', // U+1090 MYANMAR SHAN DIGIT ZERO
  6548. U'\u17E0', // U+17E0 KHMER DIGIT ZERO
  6549. U'\u1810', // U+1810 MONGOLIAN DIGIT ZERO
  6550. U'\u1946', // U+1946 LIMBU DIGIT ZERO
  6551. U'\u19D0', // U+19D0 NEW TAI LUE DIGIT ZERO
  6552. U'\u1A80', // U+1A80 TAI THAM HORA DIGIT ZERO
  6553. U'\u1A90', // U+1A90 TAI THAM THAM DIGIT ZERO
  6554. U'\u1B50', // U+1B50 BALINESE DIGIT ZERO
  6555. U'\u1BB0', // U+1BB0 SUNDANESE DIGIT ZERO
  6556. U'\u1C40', // U+1C40 LEPCHA DIGIT ZERO
  6557. U'\u1C50', // U+1C50 OL CHIKI DIGIT ZERO
  6558. U'\uA620', // U+A620 VAI DIGIT ZERO
  6559. U'\uA8D0', // U+A8D0 SAURASHTRA DIGIT ZERO
  6560. U'\uA900', // U+A900 KAYAH LI DIGIT ZERO
  6561. U'\uA9D0', // U+A9D0 JAVANESE DIGIT ZERO
  6562. U'\uA9F0', // U+A9F0 MYANMAR TAI LAING DIGIT ZERO
  6563. U'\uAA50', // U+AA50 CHAM DIGIT ZERO
  6564. U'\uABF0', // U+ABF0 MEETEI MAYEK DIGIT ZERO
  6565. U'\uFF10', // U+FF10 FULLWIDTH DIGIT ZERO
  6566. U'\U000104A0', // U+104A0 OSMANYA DIGIT ZERO
  6567. U'\U00010D30', // U+10D30 HANIFI ROHINGYA DIGIT ZERO
  6568. U'\U00011066', // U+11066 BRAHMI DIGIT ZERO
  6569. U'\U000110F0', // U+110F0 SORA SOMPENG DIGIT ZERO
  6570. U'\U00011136', // U+11136 CHAKMA DIGIT ZERO
  6571. U'\U000111D0', // U+111D0 SHARADA DIGIT ZERO
  6572. U'\U000112F0', // U+112F0 KHUDAWADI DIGIT ZERO
  6573. U'\U00011450', // U+11450 NEWA DIGIT ZERO
  6574. U'\U000114D0', // U+114D0 TIRHUTA DIGIT ZERO
  6575. U'\U00011650', // U+11650 MODI DIGIT ZERO
  6576. U'\U000116C0', // U+116C0 TAKRI DIGIT ZERO
  6577. U'\U00011730', // U+11730 AHOM DIGIT ZERO
  6578. U'\U000118E0', // U+118E0 WARANG CITI DIGIT ZERO
  6579. U'\U00011950', // U+11950 DIVES AKURU DIGIT ZERO
  6580. U'\U00011C50', // U+11C50 BHAIKSUKI DIGIT ZERO
  6581. U'\U00011D50', // U+11D50 MASARAM GONDI DIGIT ZERO
  6582. U'\U00011DA0', // U+11DA0 GUNJALA GONDI DIGIT ZERO
  6583. U'\U00011F50', // U+11F50 KAWI DIGIT ZERO
  6584. U'\U00016A60', // U+16A60 MRO DIGIT ZERO
  6585. U'\U00016AC0', // U+16AC0 TANGSA DIGIT ZERO
  6586. U'\U00016B50', // U+16B50 PAHAWH HMONG DIGIT ZERO
  6587. U'\U0001D7CE', // U+1D7CE MATHEMATICAL BOLD DIGIT ZERO
  6588. U'\U0001D7D8', // U+1D7D8 MATHEMATICAL DOUBLE-STRUCK DIGIT ZERO
  6589. U'\U0001D7E2', // U+1D7E2 MATHEMATICAL SANS-SERIF DIGIT ZERO
  6590. U'\U0001D7EC', // U+1D7EC MATHEMATICAL SANS-SERIF BOLD DIGIT ZERO
  6591. U'\U0001D7F6', // U+1D7F6 MATHEMATICAL MONOSPACE DIGIT ZERO
  6592. U'\U0001E140', // U+1E140 NYIAKENG PUACHUE HMONG DIGIT ZERO
  6593. U'\U0001E2F0', // U+1E2F0 WANCHO DIGIT ZERO
  6594. U'\U0001E4F0', // U+1E4F0 NAG MUNDARI DIGIT ZERO
  6595. U'\U0001E950', // U+1E950 ADLAM DIGIT ZERO
  6596. U'\U0001FBF0' // U+1FBF0 SEGMENTED DIGIT ZERO
  6597. };
  6598. };
  6599. template<typename Tag>
  6600. struct char_set_parser
  6601. {
  6602. BOOST_PARSER_ALGO_CONSTEXPR char_set_parser()
  6603. {
  6604. auto const & chars = detail::char_set<Tag>::chars;
  6605. auto const first = std::begin(chars);
  6606. auto const last = std::end(chars);
  6607. auto it = std::upper_bound(first, last, 0x100, [](auto x, auto y){
  6608. using common_t = std::common_type_t<decltype(x), decltype(x)>;
  6609. return (common_t)x < (common_t)y;
  6610. });
  6611. if (it != last)
  6612. one_byte_offset_ = int(it - first);
  6613. }
  6614. template<typename T>
  6615. using attribute_type = std::decay_t<T>;
  6616. template<
  6617. typename Iter,
  6618. typename Sentinel,
  6619. typename Context,
  6620. typename SkipParser>
  6621. auto call(
  6622. Iter & first,
  6623. Sentinel last,
  6624. Context const & context,
  6625. SkipParser const & skip,
  6626. detail::flags flags,
  6627. bool & success) const -> attribute_type<decltype(*first)>
  6628. {
  6629. attribute_type<decltype(*first)> retval{};
  6630. call(first, last, context, skip, flags, success, retval);
  6631. return retval;
  6632. }
  6633. template<
  6634. typename Iter,
  6635. typename Sentinel,
  6636. typename Context,
  6637. typename SkipParser,
  6638. typename Attribute>
  6639. void call(
  6640. Iter & first,
  6641. Sentinel last,
  6642. Context const & context,
  6643. SkipParser const & skip,
  6644. detail::flags flags,
  6645. bool & success,
  6646. Attribute & retval) const
  6647. {
  6648. [[maybe_unused]] auto _ = detail::scoped_trace(
  6649. *this, first, last, context, flags, retval);
  6650. if (first == last) {
  6651. success = false;
  6652. return;
  6653. }
  6654. auto const & chars = detail::char_set<Tag>::chars;
  6655. attribute_type<decltype(*first)> const x = *first;
  6656. uint32_t const x_cmp = x;
  6657. if (x_cmp < U'\x0100') {
  6658. uint32_t const * it = std::lower_bound(
  6659. std::begin(chars),
  6660. std::begin(chars) + one_byte_offset_,
  6661. x_cmp);
  6662. if (it != std::end(chars) && *it == x_cmp) {
  6663. detail::assign(retval, x_cmp);
  6664. ++first;
  6665. } else {
  6666. success = false;
  6667. }
  6668. return;
  6669. }
  6670. uint32_t const * it = std::lower_bound(
  6671. std::begin(chars) + one_byte_offset_, std::end(chars), x_cmp);
  6672. if (it != std::end(chars) && *it == x_cmp) {
  6673. detail::assign(retval, x_cmp);
  6674. ++first;
  6675. return;
  6676. }
  6677. success = false;
  6678. }
  6679. int one_byte_offset_ = 0;
  6680. };
  6681. template<typename Tag>
  6682. struct char_subrange_parser
  6683. {
  6684. constexpr char_subrange_parser() {}
  6685. template<typename T>
  6686. using attribute_type = std::decay_t<T>;
  6687. template<
  6688. typename Iter,
  6689. typename Sentinel,
  6690. typename Context,
  6691. typename SkipParser>
  6692. auto call(
  6693. Iter & first,
  6694. Sentinel last,
  6695. Context const & context,
  6696. SkipParser const & skip,
  6697. detail::flags flags,
  6698. bool & success) const -> attribute_type<decltype(*first)>
  6699. {
  6700. attribute_type<decltype(*first)> retval{};
  6701. call(first, last, context, skip, flags, success, retval);
  6702. return retval;
  6703. }
  6704. template<
  6705. typename Iter,
  6706. typename Sentinel,
  6707. typename Context,
  6708. typename SkipParser,
  6709. typename Attribute>
  6710. void call(
  6711. Iter & first,
  6712. Sentinel last,
  6713. Context const & context,
  6714. SkipParser const & skip,
  6715. detail::flags flags,
  6716. bool & success,
  6717. Attribute & retval) const
  6718. {
  6719. [[maybe_unused]] auto _ = detail::scoped_trace(
  6720. *this, first, last, context, flags, retval);
  6721. if (first == last) {
  6722. success = false;
  6723. return;
  6724. }
  6725. attribute_type<decltype(*first)> const x = *first;
  6726. char32_t const x_cmp = x;
  6727. success = false;
  6728. for (auto subrange : detail::char_subranges<Tag>::ranges) {
  6729. if (subrange.lo_ <= x_cmp && x_cmp <= subrange.hi_) {
  6730. success = true;
  6731. detail::assign(retval, x);
  6732. ++first;
  6733. return;
  6734. }
  6735. }
  6736. }
  6737. };
  6738. #endif
  6739. /** The single-character parser. The produced attribute is the type of
  6740. the matched code point (`char` or `char32_t`). Used as-is, `char_`
  6741. matches any code point. `char_` can also be used to create code
  6742. point parsers that match one or more specific code point values, by
  6743. calling it with: a single value comparable to a code point; a closed
  6744. range of code point values `[lo, hi]`, or a set of code point values
  6745. passed as a range. When calling with a range, only the iterators that
  6746. bound the range are stored. Make sure the range you pass outlives the
  6747. use of the resulting parser. Note that a string literal is a range,
  6748. and that it outlives any parser it is used to construct. */
  6749. inline constexpr parser_interface<char_parser<detail::nope>> char_;
  6750. /** The code point parser. It produces a `char32_t` attribute. Used
  6751. as-is, `cp` matches any code point. `cp` can also be used to
  6752. create code point parsers that match one or more specific code point
  6753. values, by calling it with: a single value comparable to a code point;
  6754. a closed range of code point values `[lo, hi]`, or a set of code point
  6755. values passed as a range. When calling with a range, only the
  6756. iterators that bound the range are stored. Make sure the range you
  6757. pass outlives the use of the resulting parser. Note that a string
  6758. literal is a range, and that it outlives any parser it is used to
  6759. construct. */
  6760. inline constexpr parser_interface<char_parser<detail::nope, char32_t>> cp;
  6761. /** The code unit parser. It produces a `char` attribute. Used as-is,
  6762. `cu` matches any code point. `cu` can also can be used to create code
  6763. point parsers that match one or more specific code point values, by
  6764. calling it with: a single value comparable to a code point; a closed
  6765. range of code point values `[lo, hi]`, or a set of code point values
  6766. passed as a range. When calling with a range, only the iterators that
  6767. bound the range are stored. Make sure the range you pass outlives the
  6768. use of the resulting parser. Note that a string literal is a range,
  6769. and that it outlives any parser it is used to construct. */
  6770. inline constexpr parser_interface<char_parser<detail::nope, char>> cu;
  6771. /** Returns a literal code point parser that produces no attribute. */
  6772. inline constexpr auto lit(char c) noexcept { return omit[char_(c)]; }
  6773. #if defined(__cpp_char8_t) || defined(BOOST_PARSER_DOXYGEN)
  6774. /** Returns a literal code point parser that produces no attribute. */
  6775. inline constexpr auto lit(char8_t c) noexcept { return omit[char_(c)]; }
  6776. #endif
  6777. /** Returns a literal code point parser that produces no attribute. */
  6778. inline constexpr auto lit(char32_t c) noexcept { return omit[char_(c)]; }
  6779. #ifndef BOOST_PARSER_DOXYGEN
  6780. template<typename StrIter, typename StrSentinel>
  6781. struct string_parser
  6782. {
  6783. constexpr string_parser() : expected_first_(), expected_last_() {}
  6784. #if BOOST_PARSER_USE_CONCEPTS
  6785. template<parsable_range_like R>
  6786. #else
  6787. template<
  6788. typename R,
  6789. typename Enable =
  6790. std::enable_if_t<detail::is_parsable_range_like_v<R>>>
  6791. #endif
  6792. constexpr string_parser(R && r) :
  6793. expected_first_(detail::make_view_begin(r)),
  6794. expected_last_(detail::make_view_end(r))
  6795. {}
  6796. template<
  6797. typename Iter,
  6798. typename Sentinel,
  6799. typename Context,
  6800. typename SkipParser>
  6801. std::string call(
  6802. Iter & first,
  6803. Sentinel last,
  6804. Context const & context,
  6805. SkipParser const & skip,
  6806. detail::flags flags,
  6807. bool & success) const
  6808. {
  6809. std::string retval;
  6810. call(first, last, context, skip, flags, success, retval);
  6811. return retval;
  6812. }
  6813. template<
  6814. typename Iter,
  6815. typename Sentinel,
  6816. typename Context,
  6817. typename SkipParser,
  6818. typename Attribute>
  6819. void call(
  6820. Iter & first,
  6821. Sentinel last,
  6822. Context const & context,
  6823. SkipParser const & skip,
  6824. detail::flags flags,
  6825. bool & success,
  6826. Attribute & retval) const
  6827. {
  6828. [[maybe_unused]] auto _ = detail::scoped_trace(
  6829. *this, first, last, context, flags, retval);
  6830. if (first == last) {
  6831. success = false;
  6832. return;
  6833. }
  6834. if constexpr (std::is_same_v<
  6835. detail::remove_cv_ref_t<decltype(*first)>,
  6836. char32_t>) {
  6837. auto const cps =
  6838. BOOST_PARSER_SUBRANGE(expected_first_, expected_last_) |
  6839. detail::text::as_utf32;
  6840. auto const mismatch = detail::no_case_aware_string_mismatch(
  6841. first,
  6842. last,
  6843. cps.begin(),
  6844. cps.end(),
  6845. context.no_case_depth_);
  6846. if (mismatch.second != cps.end()) {
  6847. success = false;
  6848. return;
  6849. }
  6850. detail::append(
  6851. retval, first, mismatch.first, detail::gen_attrs(flags));
  6852. first = mismatch.first;
  6853. } else {
  6854. auto const mismatch = detail::no_case_aware_string_mismatch(
  6855. first,
  6856. last,
  6857. expected_first_,
  6858. expected_last_,
  6859. context.no_case_depth_);
  6860. if (mismatch.second != expected_last_) {
  6861. success = false;
  6862. return;
  6863. }
  6864. detail::append(
  6865. retval, first, mismatch.first, detail::gen_attrs(flags));
  6866. first = mismatch.first;
  6867. }
  6868. }
  6869. StrIter expected_first_;
  6870. StrSentinel expected_last_;
  6871. };
  6872. #if BOOST_PARSER_USE_CONCEPTS
  6873. template<parsable_range_like R>
  6874. #else
  6875. template<typename R>
  6876. #endif
  6877. string_parser(R r) -> string_parser<
  6878. decltype(detail::make_view_begin(r)),
  6879. decltype(detail::make_view_end(r))>;
  6880. #endif
  6881. /** Returns a parser that matches `str` that produces the matched string
  6882. as its attribute. */
  6883. #if BOOST_PARSER_USE_CONCEPTS
  6884. template<parsable_range_like R>
  6885. #else
  6886. template<typename R>
  6887. #endif
  6888. constexpr auto string(R && str) noexcept
  6889. {
  6890. return parser_interface{string_parser(str)};
  6891. }
  6892. template<typename Quotes, typename Escapes, typename CharParser>
  6893. struct quoted_string_parser
  6894. {
  6895. constexpr quoted_string_parser() : chs_(), ch_('"') {}
  6896. #if BOOST_PARSER_USE_CONCEPTS
  6897. template<parsable_range_like R>
  6898. #else
  6899. template<
  6900. typename R,
  6901. typename Enable =
  6902. std::enable_if_t<detail::is_parsable_range_like_v<R>>>
  6903. #endif
  6904. constexpr quoted_string_parser(
  6905. R && r,
  6906. parser_interface<CharParser> char_p =
  6907. parser_interface{CharParser()}) :
  6908. chs_((R &&)r), char_p_(char_p), ch_(0)
  6909. {
  6910. BOOST_PARSER_DEBUG_ASSERT(r.begin() != r.end());
  6911. }
  6912. #if BOOST_PARSER_USE_CONCEPTS
  6913. template<parsable_range_like R>
  6914. #else
  6915. template<
  6916. typename R,
  6917. typename Enable =
  6918. std::enable_if_t<detail::is_parsable_range_like_v<R>>>
  6919. #endif
  6920. constexpr quoted_string_parser(
  6921. R && r,
  6922. Escapes escapes,
  6923. parser_interface<CharParser> char_p =
  6924. parser_interface{CharParser()}) :
  6925. chs_((R &&)r), escapes_(escapes), char_p_(char_p), ch_(0)
  6926. {
  6927. BOOST_PARSER_DEBUG_ASSERT(r.begin() != r.end());
  6928. }
  6929. constexpr quoted_string_parser(
  6930. char32_t cp,
  6931. parser_interface<CharParser> char_p =
  6932. parser_interface{CharParser()}) :
  6933. chs_(), char_p_(char_p), ch_(cp)
  6934. {}
  6935. constexpr quoted_string_parser(
  6936. char32_t cp,
  6937. Escapes escapes,
  6938. parser_interface<CharParser> char_p =
  6939. parser_interface{CharParser()}) :
  6940. chs_(), escapes_(escapes), char_p_(char_p), ch_(cp)
  6941. {}
  6942. template<
  6943. typename Iter,
  6944. typename Sentinel,
  6945. typename Context,
  6946. typename SkipParser>
  6947. std::string call(
  6948. Iter & first,
  6949. Sentinel last,
  6950. Context const & context,
  6951. SkipParser const & skip,
  6952. detail::flags flags,
  6953. bool & success) const
  6954. {
  6955. std::string retval;
  6956. call(first, last, context, skip, flags, success, retval);
  6957. return retval;
  6958. }
  6959. template<
  6960. typename Iter,
  6961. typename Sentinel,
  6962. typename Context,
  6963. typename SkipParser,
  6964. typename Attribute>
  6965. void call(
  6966. Iter & first,
  6967. Sentinel last,
  6968. Context const & context,
  6969. SkipParser const & skip,
  6970. detail::flags flags,
  6971. bool & success,
  6972. Attribute & retval) const
  6973. {
  6974. [[maybe_unused]] auto _ = detail::scoped_trace(
  6975. *this, first, last, context, flags, retval);
  6976. if (first == last) {
  6977. success = false;
  6978. return;
  6979. }
  6980. auto const prev_first = first;
  6981. auto append = [&retval,
  6982. gen_attrs = detail::gen_attrs(flags)](auto & ctx) {
  6983. detail::move_back(retval, _attr(ctx), gen_attrs);
  6984. };
  6985. auto quote_ch = [&]() {
  6986. if constexpr (detail::is_nope_v<Quotes>) {
  6987. detail::remove_cv_ref_t<decltype(*first)> curr = *first;
  6988. if ((char32_t)curr == ch_)
  6989. ++first;
  6990. else
  6991. success = false;
  6992. return ch_;
  6993. } else {
  6994. detail::remove_cv_ref_t<decltype(*first)> const ch = *first;
  6995. bool found = false;
  6996. if constexpr (std::
  6997. is_same_v<decltype(ch), char32_t const>) {
  6998. auto r = chs_ | detail::text::as_utf32;
  6999. found = detail::text::find(r.begin(), r.end(), ch) !=
  7000. r.end();
  7001. } else {
  7002. found = detail::text::find(
  7003. chs_.begin(), chs_.end(), ch) != chs_.end();
  7004. }
  7005. if (found)
  7006. ++first;
  7007. else
  7008. success = false;
  7009. return ch;
  7010. }
  7011. };
  7012. auto const ch = quote_ch();
  7013. if (!success)
  7014. return;
  7015. decltype(ch) const backslash_and_delim[] = {'\\', ch};
  7016. auto const back_delim = char_(backslash_and_delim);
  7017. auto make_parser = [&]() {
  7018. if constexpr (detail::is_nope_v<Escapes>) {
  7019. return *((lit('\\') >> back_delim) |
  7020. (char_p_ - back_delim))[append] > ch;
  7021. } else {
  7022. return *((lit('\\') >> back_delim)[append] |
  7023. (lit('\\') >> parser_interface(escapes_))[append] |
  7024. (char_p_ - back_delim)[append]) > ch;
  7025. }
  7026. };
  7027. auto const p = make_parser();
  7028. p.parser_.call(
  7029. first,
  7030. last,
  7031. context,
  7032. skip,
  7033. detail::disable_skip(flags),
  7034. success);
  7035. if (!success) {
  7036. retval = Attribute();
  7037. first = prev_first;
  7038. }
  7039. }
  7040. /** Returns a `parser_interface` containing a `quoted_string_parser`
  7041. that uses `x` as its quotation marks. */
  7042. #if BOOST_PARSER_USE_CONCEPTS
  7043. template<typename T, typename Parser = char_parser<detail::nope>>
  7044. requires(!parsable_range_like<T>)
  7045. #else
  7046. template<
  7047. typename T,
  7048. typename Parser = char_parser<detail::nope>,
  7049. typename Enable =
  7050. std::enable_if_t<!detail::is_parsable_range_like_v<T>>>
  7051. #endif
  7052. constexpr auto
  7053. operator()(T x, parser_interface<Parser> char_p = char_) const noexcept
  7054. {
  7055. if constexpr (!detail::is_nope_v<Quotes>) {
  7056. BOOST_PARSER_ASSERT(
  7057. (chs_.empty() && ch_ == '"' &&
  7058. "If you're seeing this, you tried to chain calls on "
  7059. "quoted_string, like 'quoted_string('\"')('\\'')'. Quit "
  7060. "it!'"));
  7061. }
  7062. return parser_interface(
  7063. quoted_string_parser<detail::nope, detail::nope, Parser>(
  7064. std::move(x), char_p));
  7065. }
  7066. /** Returns a `parser_interface` containing a `quoted_string_parser`
  7067. that accepts any of the values in `r` as its quotation marks. If
  7068. the input being matched during the parse is a a sequence of
  7069. `char32_t`, the elements of `r` are transcoded from their presumed
  7070. encoding to UTF-32 during the comparison. Otherwise, the
  7071. character begin matched is directly compared to the elements of
  7072. `r`. */
  7073. #if BOOST_PARSER_USE_CONCEPTS
  7074. template<
  7075. parsable_range_like R,
  7076. typename Parser = char_parser<detail::nope>>
  7077. #else
  7078. template<
  7079. typename R,
  7080. typename Parser = char_parser<detail::nope>,
  7081. typename Enable =
  7082. std::enable_if_t<detail::is_parsable_range_like_v<R>>>
  7083. #endif
  7084. constexpr auto operator()(
  7085. R && r, parser_interface<Parser> char_p = char_) const noexcept
  7086. {
  7087. BOOST_PARSER_ASSERT(((
  7088. !std::is_rvalue_reference_v<R &&> ||
  7089. !detail::is_range<detail::remove_cv_ref_t<
  7090. R>>)&&"It looks like you tried to pass an rvalue range to "
  7091. "quoted_string(). Don't do that, or you'll end up "
  7092. "with dangling references."));
  7093. if constexpr (!detail::is_nope_v<Quotes>) {
  7094. BOOST_PARSER_ASSERT(
  7095. (chs_.empty() && ch_ == '"' &&
  7096. "If you're seeing this, you tried to chain calls on "
  7097. "quoted_string, like "
  7098. "'quoted_string(char-range)(char-range)'. Quit it!'"));
  7099. }
  7100. return parser_interface(
  7101. quoted_string_parser<
  7102. decltype(BOOST_PARSER_SUBRANGE(
  7103. detail::make_view_begin(r), detail::make_view_end(r))),
  7104. detail::nope,
  7105. Parser>(
  7106. BOOST_PARSER_SUBRANGE(
  7107. detail::make_view_begin(r), detail::make_view_end(r)),
  7108. char_p));
  7109. }
  7110. /** Returns a `parser_interface` containing a `quoted_string_parser`
  7111. that uses `x` as its quotation marks. `symbols` provides a list
  7112. of strings that may appear after a backslash to form an escape
  7113. sequence, and what character(s) each escape sequence represents.
  7114. Note that `"\\"` and `"\ch"` are always valid escape sequences. */
  7115. #if BOOST_PARSER_USE_CONCEPTS
  7116. template<
  7117. typename T,
  7118. typename U,
  7119. typename Parser = char_parser<detail::nope>>
  7120. requires(!parsable_range_like<T>)
  7121. #else
  7122. template<
  7123. typename T,
  7124. typename U,
  7125. typename Parser = char_parser<detail::nope>,
  7126. typename Enable =
  7127. std::enable_if_t<!detail::is_parsable_range_like_v<T>>>
  7128. #endif
  7129. auto operator()(
  7130. T x,
  7131. symbols<U> const & escapes,
  7132. parser_interface<Parser> char_p = char_) const noexcept
  7133. {
  7134. if constexpr (!detail::is_nope_v<Quotes>) {
  7135. BOOST_PARSER_ASSERT(
  7136. (chs_.empty() && ch_ == '"' &&
  7137. "If you're seeing this, you tried to chain calls on "
  7138. "quoted_string, like 'quoted_string('\"')('\\'')'. Quit "
  7139. "it!'"));
  7140. }
  7141. auto symbols = symbol_parser(escapes.parser_);
  7142. auto parser =
  7143. quoted_string_parser<detail::nope, decltype(symbols), Parser>(
  7144. char32_t(x), symbols, char_p);
  7145. return parser_interface(parser);
  7146. }
  7147. /** Returns a `parser_interface` containing a `quoted_string_parser`
  7148. that accepts any of the values in `r` as its quotation marks. If
  7149. the input being matched during the parse is a a sequence of
  7150. `char32_t`, the elements of `r` are transcoded from their presumed
  7151. encoding to UTF-32 during the comparison. Otherwise, the
  7152. character begin matched is directly compared to the elements of
  7153. `r`. `symbols` provides a list of strings that may appear after a
  7154. backslash to form an escape sequence, and what character(s) each
  7155. escape sequence represents. Note that `"\\"` and `"\ch"` are
  7156. always valid escape sequences. */
  7157. #if BOOST_PARSER_USE_CONCEPTS
  7158. template<
  7159. parsable_range_like R,
  7160. typename T,
  7161. typename Parser = char_parser<detail::nope>>
  7162. #else
  7163. template<
  7164. typename R,
  7165. typename T,
  7166. typename Parser = char_parser<detail::nope>,
  7167. typename Enable =
  7168. std::enable_if_t<detail::is_parsable_range_like_v<R>>>
  7169. #endif
  7170. auto operator()(
  7171. R && r,
  7172. symbols<T> const & escapes,
  7173. parser_interface<Parser> char_p = char_) const noexcept
  7174. {
  7175. BOOST_PARSER_ASSERT(((
  7176. !std::is_rvalue_reference_v<R &&> ||
  7177. !detail::is_range<detail::remove_cv_ref_t<
  7178. R>>)&&"It looks like you tried to pass an rvalue range to "
  7179. "quoted_string(). Don't do that, or you'll end up "
  7180. "with dangling references."));
  7181. if constexpr (!detail::is_nope_v<Quotes>) {
  7182. BOOST_PARSER_ASSERT(
  7183. (chs_.empty() && ch_ == '"' &&
  7184. "If you're seeing this, you tried to chain calls on "
  7185. "quoted_string, like "
  7186. "'quoted_string(char-range)(char-range)'. Quit it!'"));
  7187. }
  7188. auto symbols = symbol_parser(escapes.parser_);
  7189. auto quotes = BOOST_PARSER_SUBRANGE(
  7190. detail::make_view_begin(r), detail::make_view_end(r));
  7191. auto parser = quoted_string_parser<
  7192. decltype(quotes),
  7193. decltype(symbols),
  7194. Parser>(quotes, symbols, char_p);
  7195. return parser_interface(parser);
  7196. }
  7197. Quotes chs_;
  7198. Escapes escapes_;
  7199. parser_interface<CharParser> char_p_;
  7200. char32_t ch_;
  7201. };
  7202. /** Parses a string delimited by quotation marks. This parser can be used
  7203. to create parsers that accept one or more specific quotation mark
  7204. characters. By default, the quotation marks are `'"'`; an alternate
  7205. quotation mark can be specified by calling this parser with a single
  7206. character, or a range of characters. If a range is specified, the
  7207. opening quote must be one of the characters specified, and the closing
  7208. quote must match the opening quote. Quotation marks may appear within
  7209. the string if escaped with a backslash, and a pair of backslashes is
  7210. treated as a single escaped backslash; all other backslashes cause the
  7211. parse to fail, unless a symbol table is in use. A symbol table can be
  7212. provided as a second parameter after the single character or range
  7213. described above. The symbol table is used to recognize escape
  7214. sequences. Each escape sequence is a backslash followed by a value in
  7215. the symbol table. When using a symbol table, any backslash that is
  7216. not followed by another backslash, the opening quote character, or a
  7217. symbol from the symbol table will cause the parse to fail. Skipping
  7218. is disabled during parsing of the entire quoted string, including the
  7219. quotation marks. There is an expectation point before the closing
  7220. quotation mark. Produces a `std::string` attribute. */
  7221. inline constexpr parser_interface<quoted_string_parser<>> quoted_string;
  7222. /** Returns a parser that matches `str` that produces no attribute. */
  7223. #if BOOST_PARSER_USE_CONCEPTS
  7224. template<parsable_range_like R>
  7225. #else
  7226. template<typename R>
  7227. #endif
  7228. constexpr auto lit(R && str) noexcept
  7229. {
  7230. return omit[parser::string(str)];
  7231. }
  7232. #ifndef BOOST_PARSER_DOXYGEN
  7233. template<bool NewlinesOnly, bool NoNewlines>
  7234. struct ws_parser
  7235. {
  7236. constexpr ws_parser() {}
  7237. static_assert(!NewlinesOnly || !NoNewlines);
  7238. template<
  7239. typename Iter,
  7240. typename Sentinel,
  7241. typename Context,
  7242. typename SkipParser>
  7243. detail::nope call(
  7244. Iter & first,
  7245. Sentinel last,
  7246. Context const & context,
  7247. SkipParser const & skip,
  7248. detail::flags flags,
  7249. bool & success) const
  7250. {
  7251. detail::nope nope;
  7252. call(first, last, context, skip, flags, success, nope);
  7253. return {};
  7254. }
  7255. template<
  7256. typename Iter,
  7257. typename Sentinel,
  7258. typename Context,
  7259. typename SkipParser,
  7260. typename Attribute>
  7261. void call(
  7262. Iter & first,
  7263. Sentinel last,
  7264. Context const & context,
  7265. SkipParser const & skip,
  7266. detail::flags flags,
  7267. bool & success,
  7268. Attribute & retval) const
  7269. {
  7270. [[maybe_unused]] auto _ = detail::scoped_trace(
  7271. *this, first, last, context, flags, retval);
  7272. if (first == last) {
  7273. success = false;
  7274. return;
  7275. }
  7276. int const x = *first;
  7277. if constexpr (NewlinesOnly) {
  7278. if (x == 0x000a) { // lf
  7279. ++first;
  7280. return;
  7281. }
  7282. if (x == 0x000d) { // cr
  7283. ++first;
  7284. if (first != last && *first == 0x000a) // lf
  7285. ++first;
  7286. return;
  7287. }
  7288. if (0x000b == x || x == 0x000c || x == 0x0085 || x == 0x2028 ||
  7289. x == 0x2029) {
  7290. ++first;
  7291. return;
  7292. }
  7293. success = false;
  7294. } else if constexpr (NoNewlines) {
  7295. if (x == 0x0020) { // space
  7296. ++first;
  7297. return;
  7298. }
  7299. if (x == 0x0009) { // tab
  7300. ++first;
  7301. return;
  7302. }
  7303. if (x == 0x00a0 || x == 0x1680 ||
  7304. (0x2000 <= x && x <= 0x200a) || x == 0x202F ||
  7305. x == 0x205F || x == 0x3000) {
  7306. ++first;
  7307. return;
  7308. }
  7309. success = false;
  7310. } else {
  7311. if (x == 0x0020 || x == 0x000a) { // space, lf
  7312. ++first;
  7313. return;
  7314. }
  7315. if (x == 0x000d) { // cr
  7316. ++first;
  7317. if (first != last && *first == 0x000a) // lf
  7318. ++first;
  7319. return;
  7320. }
  7321. if (0x0009 <= x && x <= 0x000c) { // tab through cr
  7322. ++first;
  7323. return;
  7324. }
  7325. if (x == 0x0085 || x == 0x00a0 || x == 0x1680 ||
  7326. (0x2000 <= x && x <= 0x200a) || x == 0x2028 ||
  7327. x == 0x2029 || x == 0x202F || x == 0x205F || x == 0x3000) {
  7328. ++first;
  7329. return;
  7330. }
  7331. success = false;
  7332. }
  7333. }
  7334. };
  7335. #endif
  7336. /** The end-of-line parser. This matches "\r\n", or any one of the line
  7337. break code points from the Unicode Line Break Algorithm, described in
  7338. https://unicode.org/reports/tr14. Produces no attribute. */
  7339. inline constexpr parser_interface<ws_parser<true, false>> eol;
  7340. /** The whitespace parser. This matches "\r\n", or any one of the Unicode
  7341. code points with the White_Space property, as defined in
  7342. https://www.unicode.org/Public/UCD/latest/ucd/PropList.txt. Produces
  7343. no attribute. */
  7344. inline constexpr parser_interface<ws_parser<false, false>> ws;
  7345. /** The whitespace parser that does not match end-of-line. This matches
  7346. any one of the Unicode code points with the White_Space property, as
  7347. defined in https://www.unicode.org/Public/UCD/latest/ucd/PropList.txt,
  7348. except for the ones matched by `eol`. Produces no attribute. */
  7349. inline constexpr parser_interface<ws_parser<false, true>> blank;
  7350. /** The decimal digit parser. Matches the full set of Unicode decimal
  7351. digits; in other words, all Unicode code points with the "Nd"
  7352. character property. Note that this covers all Unicode scripts, only a
  7353. few of which are Latin. */
  7354. inline constexpr parser_interface<digit_parser> digit;
  7355. /** The hexadecimal digit parser. Matches the full set of Unicode
  7356. hexadecimal digits (upper or lower case); in other words, all Unicode
  7357. code points with the "Hex_Digit" character property. */
  7358. inline constexpr parser_interface<
  7359. char_subrange_parser<detail::hex_digit_subranges>>
  7360. hex_digit;
  7361. /** The control character parser. Matches the all Unicode code points
  7362. with the "Cc" ("control character") character property. */
  7363. inline constexpr parser_interface<
  7364. char_subrange_parser<detail::control_subranges>>
  7365. control;
  7366. /** The punctuation character parser. Matches the full set of Unicode
  7367. punctuation classes (specifically, "Pc", "Pd", "Pe", "Pf", "Pi", "Ps",
  7368. and "Po"). */
  7369. inline BOOST_PARSER_ALGO_CONSTEXPR
  7370. parser_interface<char_set_parser<detail::punct_chars>>
  7371. punct;
  7372. /** The symbol character parser. Matches the full set of Unicode
  7373. symbol classes (specifically, "Sc", "Sk", "Sm", and "So"). */
  7374. inline BOOST_PARSER_ALGO_CONSTEXPR
  7375. parser_interface<char_set_parser<detail::symb_chars>>
  7376. symb;
  7377. /** The lower case character parser. Matches the full set of Unicode
  7378. lower case code points (class "Ll"). */
  7379. inline BOOST_PARSER_ALGO_CONSTEXPR
  7380. parser_interface<char_set_parser<detail::lower_case_chars>>
  7381. lower;
  7382. /** The lower case character parser. Matches the full set of Unicode
  7383. upper case code points (class "Lu"). */
  7384. inline BOOST_PARSER_ALGO_CONSTEXPR
  7385. parser_interface<char_set_parser<detail::upper_case_chars>>
  7386. upper;
  7387. #ifndef BOOST_PARSER_DOXYGEN
  7388. struct bool_parser
  7389. {
  7390. template<
  7391. typename Iter,
  7392. typename Sentinel,
  7393. typename Context,
  7394. typename SkipParser>
  7395. bool call(
  7396. Iter & first,
  7397. Sentinel last,
  7398. Context const & context,
  7399. SkipParser const & skip,
  7400. detail::flags flags,
  7401. bool & success) const
  7402. {
  7403. bool retval{};
  7404. call(first, last, context, skip, flags, success, retval);
  7405. return retval;
  7406. }
  7407. template<
  7408. typename Iter,
  7409. typename Sentinel,
  7410. typename Context,
  7411. typename SkipParser,
  7412. typename Attribute>
  7413. void call(
  7414. Iter & first,
  7415. Sentinel last,
  7416. Context const & context,
  7417. SkipParser const & skip,
  7418. detail::flags flags,
  7419. bool & success,
  7420. Attribute & retval) const
  7421. {
  7422. [[maybe_unused]] auto _ = detail::scoped_trace(
  7423. *this, first, last, context, flags, retval);
  7424. auto compare =
  7425. [no_case = context.no_case_depth_](char32_t a, char32_t b) {
  7426. if (no_case && 0x41 <= b && b < 0x5b)
  7427. b += 0x20;
  7428. return a == b;
  7429. };
  7430. // The lambda quiets a signed/unsigned mismatch warning when
  7431. // comparing the chars here to code points.
  7432. char const t[] = "true";
  7433. if (detail::mismatch(t, t + 4, first, last, compare).first ==
  7434. t + 4) {
  7435. std::advance(first, 4);
  7436. detail::assign(retval, true);
  7437. return;
  7438. }
  7439. char const f[] = "false";
  7440. if (detail::mismatch(f, f + 5, first, last, compare).first ==
  7441. f + 5) {
  7442. std::advance(first, 5);
  7443. detail::assign(retval, false);
  7444. return;
  7445. }
  7446. success = false;
  7447. }
  7448. };
  7449. #endif
  7450. /** The Boolean parser. Parses "true" and "false", producing attributes
  7451. `true` and `false`, respectively, and fails on any other input. */
  7452. inline constexpr parser_interface<bool_parser> bool_;
  7453. #ifndef BOOST_PARSER_DOXYGEN
  7454. template<
  7455. typename T,
  7456. int Radix,
  7457. int MinDigits,
  7458. int MaxDigits,
  7459. typename Expected>
  7460. struct uint_parser
  7461. {
  7462. static_assert(
  7463. Radix == 2 || Radix == 8 || Radix == 10 || Radix == 16,
  7464. "Unsupported radix.");
  7465. static_assert(1 <= MinDigits);
  7466. static_assert(MaxDigits == -1 || MinDigits <= MaxDigits);
  7467. constexpr uint_parser() {}
  7468. explicit constexpr uint_parser(Expected expected) : expected_(expected)
  7469. {}
  7470. template<
  7471. typename Iter,
  7472. typename Sentinel,
  7473. typename Context,
  7474. typename SkipParser>
  7475. T call(
  7476. Iter & first,
  7477. Sentinel last,
  7478. Context const & context,
  7479. SkipParser const & skip,
  7480. detail::flags flags,
  7481. bool & success) const
  7482. {
  7483. T retval{};
  7484. call(first, last, context, skip, flags, success, retval);
  7485. return retval;
  7486. }
  7487. template<
  7488. typename Iter,
  7489. typename Sentinel,
  7490. typename Context,
  7491. typename SkipParser,
  7492. typename Attribute>
  7493. void call(
  7494. Iter & first,
  7495. Sentinel last,
  7496. Context const & context,
  7497. SkipParser const & skip,
  7498. detail::flags flags,
  7499. bool & success,
  7500. Attribute & retval) const
  7501. {
  7502. [[maybe_unused]] auto _ = detail::scoped_trace(
  7503. *this, first, last, context, flags, retval);
  7504. T attr = 0;
  7505. auto const initial = first;
  7506. success =
  7507. detail::numeric::parse_int<false, Radix, MinDigits, MaxDigits>(
  7508. first, last, attr);
  7509. if (first == initial || attr != detail::resolve(context, expected_))
  7510. success = false;
  7511. if (success)
  7512. detail::assign(retval, attr);
  7513. }
  7514. /** Returns a `parser_interface` containing a `uint_parser` that
  7515. matches the exact value `expected`. */
  7516. template<typename Expected2>
  7517. constexpr auto operator()(Expected2 expected) const noexcept
  7518. {
  7519. BOOST_PARSER_ASSERT(
  7520. (detail::is_nope_v<Expected> &&
  7521. "If you're seeing this, you tried to chain calls on this "
  7522. "parser, like 'uint_(2)(3)'. Quit it!'"));
  7523. using parser_t =
  7524. uint_parser<T, Radix, MinDigits, MaxDigits, Expected2>;
  7525. return parser_interface{parser_t{expected}};
  7526. }
  7527. /** Returns a `uint_parser` identical to `*this`, except that it
  7528. parses digits as base-`Radix2` instead of base-`Radix`. */
  7529. template<int Radix2>
  7530. constexpr auto base() const noexcept
  7531. {
  7532. return uint_parser<T, Radix2, MinDigits, MaxDigits, Expected>{
  7533. expected_};
  7534. }
  7535. /** Returns a `uint_parser` identical to `*this`, except that it only
  7536. accepts numbers exactly `Digits` digits. */
  7537. template<int Digits>
  7538. constexpr auto digits() const noexcept
  7539. {
  7540. return uint_parser<T, Radix, Digits, Digits, Expected>{expected_};
  7541. }
  7542. /** Returns a `uint_parser` identical to `*this`, except that it
  7543. only accepts numbers `D` digits long, where `D` is in
  7544. [`MinDigits2`, MaxDigits2`]. */
  7545. template<int MinDigits2, int MaxDigits2>
  7546. constexpr auto digits() const noexcept
  7547. {
  7548. return uint_parser<T, Radix, MinDigits2, MaxDigits2, Expected>{
  7549. expected_};
  7550. }
  7551. Expected expected_;
  7552. };
  7553. #endif
  7554. /** The binary unsigned integer parser. Produces an `unsigned int`
  7555. attribute. To parse a particular value `x`, use `bin(x)`. */
  7556. inline constexpr parser_interface<uint_parser<unsigned int, 2>> bin;
  7557. /** The octal unsigned integer parser. Produces an `unsigned int`
  7558. attribute. To parse a particular value `x`, use `oct(x)`. */
  7559. inline constexpr parser_interface<uint_parser<unsigned int, 8>> oct;
  7560. /** The hexadecimal unsigned integer parser. Produces an `unsigned int`
  7561. attribute. To parse a particular value `x`, use `hex(x)`. */
  7562. inline constexpr parser_interface<uint_parser<unsigned int, 16>> hex;
  7563. /** The `unsigned short` parser. Produces an `unsigned short` attribute.
  7564. To parse a particular value `x`, use `ushort_(x)`. */
  7565. inline constexpr parser_interface<uint_parser<unsigned short>> ushort_;
  7566. /** The `unsigned int` parser. Produces an `unsigned int` attribute. To
  7567. parse a particular value `x`, use `uint_(x)`. */
  7568. inline constexpr parser_interface<uint_parser<unsigned int>> uint_;
  7569. /** The `unsigned long` parser. Produces an `unsigned long` attribute.
  7570. To parse a particular value `x`, use `ulong_(x)`. */
  7571. inline constexpr parser_interface<uint_parser<unsigned long>> ulong_;
  7572. /** The `unsigned long long` parser. Produces an `unsigned long long`
  7573. attribute. To parse a particular value `x`, use `ulong_long(x)`. */
  7574. inline constexpr parser_interface<uint_parser<unsigned long long>>
  7575. ulong_long;
  7576. #ifndef BOOST_PARSER_DOXYGEN
  7577. template<
  7578. typename T,
  7579. int Radix,
  7580. int MinDigits,
  7581. int MaxDigits,
  7582. typename Expected>
  7583. struct int_parser
  7584. {
  7585. static_assert(
  7586. Radix == 2 || Radix == 8 || Radix == 10 || Radix == 16,
  7587. "Unsupported radix.");
  7588. static_assert(1 <= MinDigits);
  7589. static_assert(MaxDigits == -1 || MinDigits <= MaxDigits);
  7590. constexpr int_parser() {}
  7591. explicit constexpr int_parser(Expected expected) : expected_(expected)
  7592. {}
  7593. template<
  7594. typename Iter,
  7595. typename Sentinel,
  7596. typename Context,
  7597. typename SkipParser>
  7598. T call(
  7599. Iter & first,
  7600. Sentinel last,
  7601. Context const & context,
  7602. SkipParser const & skip,
  7603. detail::flags flags,
  7604. bool & success) const
  7605. {
  7606. T retval{};
  7607. call(first, last, context, skip, flags, success, retval);
  7608. return retval;
  7609. }
  7610. template<
  7611. typename Iter,
  7612. typename Sentinel,
  7613. typename Context,
  7614. typename SkipParser,
  7615. typename Attribute>
  7616. void call(
  7617. Iter & first,
  7618. Sentinel last,
  7619. Context const & context,
  7620. SkipParser const & skip,
  7621. detail::flags flags,
  7622. bool & success,
  7623. Attribute & retval) const
  7624. {
  7625. [[maybe_unused]] auto _ = detail::scoped_trace(
  7626. *this, first, last, context, flags, retval);
  7627. T attr = 0;
  7628. auto const initial = first;
  7629. success =
  7630. detail::numeric::parse_int<true, Radix, MinDigits, MaxDigits>(
  7631. first, last, attr);
  7632. if (first == initial || attr != detail::resolve(context, expected_))
  7633. success = false;
  7634. if (success)
  7635. detail::assign(retval, attr);
  7636. }
  7637. /** Returns a `parser_interface` containing an `int_parser` that
  7638. matches the exact value `expected`. */
  7639. template<typename Expected2>
  7640. constexpr auto operator()(Expected2 expected) const noexcept
  7641. {
  7642. BOOST_PARSER_ASSERT(
  7643. (detail::is_nope_v<Expected> &&
  7644. "If you're seeing this, you tried to chain calls on this "
  7645. "parser, like 'int_(2)(3)'. Quit it!'"));
  7646. using parser_t =
  7647. int_parser<T, Radix, MinDigits, MaxDigits, Expected2>;
  7648. return parser_interface{parser_t{expected}};
  7649. }
  7650. /** Returns an `int_parser` identical to `*this`, except that it
  7651. parses digits as base-`Radix2` instead of base-`Radix`. */
  7652. template<int Radix2>
  7653. constexpr auto base() const noexcept
  7654. {
  7655. return int_parser<T, Radix2, MinDigits, MaxDigits, Expected>{
  7656. expected_};
  7657. }
  7658. /** Returns an `int_parser` identical to `*this`, except that it only
  7659. accepts numbers exactly `Digits` digits. */
  7660. template<int Digits>
  7661. constexpr auto digits() const noexcept
  7662. {
  7663. return int_parser<T, Radix, Digits, Digits, Expected>{expected_};
  7664. }
  7665. /** Returns an `int_parser` identical to `*this`, except that it
  7666. only accepts numbers `D` digits long, where `D` is in
  7667. [`MinDigits2`, MaxDigits2`]. */
  7668. template<int MinDigits2, int MaxDigits2>
  7669. constexpr auto digits() const noexcept
  7670. {
  7671. return int_parser<T, Radix, MinDigits2, MaxDigits2, Expected>{
  7672. expected_};
  7673. }
  7674. Expected expected_;
  7675. };
  7676. #endif
  7677. /** The `short` parser. Produces a `short` attribute. To parse a
  7678. particular value `x`, use `short_(x)`. */
  7679. inline constexpr parser_interface<int_parser<short>> short_;
  7680. /** The `int` parser. Produces an `int` attribute. To parse a particular
  7681. value `x`, use `int_(x)`. */
  7682. inline constexpr parser_interface<int_parser<int>> int_;
  7683. /** The `long` parser. Produces a `long` attribute. To parse a particular
  7684. value `x`, use `long_(x)`. */
  7685. inline constexpr parser_interface<int_parser<long>> long_;
  7686. /** The `long long` parser. Produces a `long long` attribute. To parse a
  7687. particular value `x`, use `long_long(x)`. */
  7688. inline constexpr parser_interface<int_parser<long long>> long_long;
  7689. #ifndef BOOST_PARSER_DOXYGEN
  7690. template<typename T>
  7691. struct float_parser
  7692. {
  7693. constexpr float_parser() {}
  7694. template<
  7695. typename Iter,
  7696. typename Sentinel,
  7697. typename Context,
  7698. typename SkipParser>
  7699. T call(
  7700. Iter & first,
  7701. Sentinel last,
  7702. Context const & context,
  7703. SkipParser const & skip,
  7704. detail::flags flags,
  7705. bool & success) const
  7706. {
  7707. T retval = 0;
  7708. call(first, last, context, skip, flags, success, retval);
  7709. return retval;
  7710. }
  7711. template<
  7712. typename Iter,
  7713. typename Sentinel,
  7714. typename Context,
  7715. typename SkipParser,
  7716. typename Attribute>
  7717. void call(
  7718. Iter & first,
  7719. Sentinel last,
  7720. Context const & context,
  7721. SkipParser const & skip,
  7722. detail::flags flags,
  7723. bool & success,
  7724. Attribute & retval) const
  7725. {
  7726. [[maybe_unused]] auto _ = detail::scoped_trace(
  7727. *this, first, last, context, flags, retval);
  7728. T attr = 0;
  7729. auto const initial = first;
  7730. success = detail::numeric::parse_real(first, last, attr);
  7731. if (first == initial)
  7732. success = false;
  7733. if (success)
  7734. detail::assign(retval, attr);
  7735. }
  7736. };
  7737. #endif
  7738. /** The `float` parser. Produces a `float` attribute. */
  7739. inline constexpr parser_interface<float_parser<float>> float_;
  7740. /** The `double` parser. Produces a `double` attribute. */
  7741. inline constexpr parser_interface<float_parser<double>> double_;
  7742. /** Represents a sequence parser, the first parser of which is an
  7743. `epsilon_parser` with predicate, as a directive
  7744. (e.g. `if_(pred)[p]`). */
  7745. template<typename Predicate>
  7746. struct if_directive
  7747. {
  7748. template<typename Parser2>
  7749. constexpr auto operator[](parser_interface<Parser2> rhs) const noexcept
  7750. {
  7751. return eps(pred_) >> rhs;
  7752. }
  7753. Predicate pred_;
  7754. };
  7755. /** Returns an `if_directive` that fails if the given predicate `pred` is
  7756. `false`, and otherwise, applies another parser. For instance, in
  7757. `if_(pred)[p]`, `p` is only applied if `pred` is true. */
  7758. template<typename Predicate>
  7759. constexpr auto if_(Predicate pred) noexcept
  7760. {
  7761. return if_directive<Predicate>{pred};
  7762. }
  7763. namespace detail {
  7764. template<typename SwitchValue, typename Value>
  7765. struct switch_parser_equal
  7766. {
  7767. template<typename Context>
  7768. bool operator()(Context & context) const
  7769. {
  7770. auto const switch_value =
  7771. detail::resolve(context, switch_value_);
  7772. auto const value = detail::resolve(context, value_);
  7773. return value == switch_value;
  7774. }
  7775. SwitchValue switch_value_;
  7776. Value value_;
  7777. };
  7778. }
  7779. #ifndef BOOST_PARSER_DOXYGEN
  7780. template<typename SwitchValue, typename OrParser>
  7781. struct switch_parser
  7782. {
  7783. switch_parser() {}
  7784. switch_parser(SwitchValue switch_value) : switch_value_(switch_value) {}
  7785. switch_parser(SwitchValue switch_value, OrParser or_parser) :
  7786. switch_value_(switch_value), or_parser_(or_parser)
  7787. {}
  7788. template<
  7789. typename Iter,
  7790. typename Sentinel,
  7791. typename Context,
  7792. typename SkipParser>
  7793. auto call(
  7794. Iter & first,
  7795. Sentinel last,
  7796. Context const & context,
  7797. SkipParser const & skip,
  7798. detail::flags flags,
  7799. bool & success) const
  7800. {
  7801. BOOST_PARSER_ASSERT(
  7802. (!detail::is_nope_v<OrParser> &&
  7803. "It looks like you tried to write switch_(val). You need at "
  7804. "least one alternative, like: switch_(val)(value_1, "
  7805. "parser_1)(value_2, parser_2)..."));
  7806. using attr_t = decltype(or_parser_.call(
  7807. first, last, context, skip, flags, success));
  7808. attr_t attr{};
  7809. [[maybe_unused]] auto _ =
  7810. detail::scoped_trace(*this, first, last, context, flags, attr);
  7811. attr = or_parser_.call(first, last, context, skip, flags, success);
  7812. return attr;
  7813. }
  7814. template<
  7815. typename Iter,
  7816. typename Sentinel,
  7817. typename Context,
  7818. typename SkipParser,
  7819. typename Attribute>
  7820. void call(
  7821. Iter & first,
  7822. Sentinel last,
  7823. Context const & context,
  7824. SkipParser const & skip,
  7825. detail::flags flags,
  7826. bool & success,
  7827. Attribute & retval) const
  7828. {
  7829. BOOST_PARSER_ASSERT(
  7830. (!detail::is_nope_v<OrParser> &&
  7831. "It looks like you tried to write switch_(val). You need at "
  7832. "least one alternative, like: switch_(val)(value_1, "
  7833. "parser_1)(value_2, parser_2)..."));
  7834. [[maybe_unused]] auto _ = detail::scoped_trace(
  7835. *this, first, last, context, flags, retval);
  7836. or_parser_.call(first, last, context, skip, flags, success, retval);
  7837. }
  7838. /** Returns a `parser_interface` containing a `switch_parser`, with
  7839. the case `value_`,`rhs` appended to its `or_parser_`. */
  7840. template<typename Value, typename Parser2>
  7841. constexpr auto
  7842. operator()(Value value_, parser_interface<Parser2> rhs) const noexcept
  7843. {
  7844. auto const match = detail::switch_parser_equal<SwitchValue, Value>{
  7845. switch_value_, value_};
  7846. auto or_parser = make_or_parser(or_parser_, eps(match) >> rhs);
  7847. using switch_parser_type =
  7848. switch_parser<SwitchValue, decltype(or_parser)>;
  7849. return parser_interface{
  7850. switch_parser_type{switch_value_, or_parser}};
  7851. }
  7852. #ifndef BOOST_PARSER_DOXYGEN
  7853. template<typename Parser1, typename Parser2>
  7854. static constexpr auto
  7855. make_or_parser(Parser1 parser1, parser_interface<Parser2> parser2)
  7856. {
  7857. return (parser_interface{parser1} | parser2).parser_;
  7858. }
  7859. template<typename Parser>
  7860. static constexpr auto
  7861. make_or_parser(detail::nope, parser_interface<Parser> parser)
  7862. {
  7863. return parser.parser_;
  7864. }
  7865. #endif
  7866. SwitchValue switch_value_;
  7867. OrParser or_parser_;
  7868. };
  7869. #endif
  7870. /** Returns a `switch`-like parser. The resulting parser uses the given
  7871. value `x` to select one of the following value/parser pairs, and to
  7872. apply the selected parser. `x` may be a value to be used directly, or
  7873. a unary invocable that takes a reference to the parse context, and
  7874. returns the value to use. You can add more value/parser cases to the
  7875. returned parser, using its call operator, e.g. `switch_(x)(y1, p1)(y2,
  7876. p2)`. As with the `x` passed to this function, each `yN` value can be
  7877. a value or a unary invocable. */
  7878. template<typename T>
  7879. constexpr auto switch_(T x) noexcept
  7880. {
  7881. return switch_parser<T>{x};
  7882. }
  7883. #ifndef BOOST_PARSER_DOXYGEN
  7884. template<typename Parser, typename GlobalState, typename ErrorHandler>
  7885. constexpr auto
  7886. parser_interface<Parser, GlobalState, ErrorHandler>::operator>>(
  7887. char rhs) const noexcept
  7888. {
  7889. return *this >> parser::lit(rhs);
  7890. }
  7891. template<typename Parser, typename GlobalState, typename ErrorHandler>
  7892. constexpr auto
  7893. parser_interface<Parser, GlobalState, ErrorHandler>::operator>>(
  7894. char32_t rhs) const noexcept
  7895. {
  7896. return *this >> parser::lit(rhs);
  7897. }
  7898. template<typename Parser, typename GlobalState, typename ErrorHandler>
  7899. #if BOOST_PARSER_USE_CONCEPTS
  7900. template<parsable_range_like R>
  7901. #else
  7902. template<typename R, typename>
  7903. #endif
  7904. constexpr auto
  7905. parser_interface<Parser, GlobalState, ErrorHandler>::operator>>(
  7906. R && r) const noexcept
  7907. {
  7908. return *this >> parser::lit(r);
  7909. }
  7910. #endif
  7911. /** Returns a parser equivalent to `lit(c) >> rhs`. */
  7912. template<typename Parser>
  7913. constexpr auto operator>>(char c, parser_interface<Parser> rhs) noexcept
  7914. {
  7915. if constexpr (detail::is_seq_p<Parser>{}) {
  7916. return rhs.parser_.template prepend<true>(parser::lit(c));
  7917. } else {
  7918. return parser::lit(c) >> rhs;
  7919. }
  7920. }
  7921. /** Returns a parser equivalent to `lit(c) >> rhs`. */
  7922. template<typename Parser>
  7923. constexpr auto operator>>(char32_t c, parser_interface<Parser> rhs) noexcept
  7924. {
  7925. if constexpr (detail::is_seq_p<Parser>{}) {
  7926. return rhs.parser_.template prepend<true>(parser::lit(c));
  7927. } else {
  7928. return parser::lit(c) >> rhs;
  7929. }
  7930. }
  7931. /** Returns a parser equivalent to `lit(str) >> rhs`. */
  7932. #if BOOST_PARSER_USE_CONCEPTS
  7933. template<parsable_range_like R, typename Parser>
  7934. #else
  7935. template<
  7936. typename R,
  7937. typename Parser,
  7938. typename Enable = std::enable_if_t<detail::is_parsable_range_like_v<R>>>
  7939. #endif
  7940. constexpr auto operator>>(R && r, parser_interface<Parser> rhs) noexcept
  7941. {
  7942. if constexpr (detail::is_seq_p<Parser>{}) {
  7943. return rhs.parser_.template prepend<true>(parser::lit(r));
  7944. } else {
  7945. return parser::lit(r) >> rhs;
  7946. }
  7947. }
  7948. #ifndef BOOST_PARSER_DOXYGEN
  7949. template<typename Parser, typename GlobalState, typename ErrorHandler>
  7950. constexpr auto
  7951. parser_interface<Parser, GlobalState, ErrorHandler>::operator>(
  7952. char rhs) const noexcept
  7953. {
  7954. return *this > parser::lit(rhs);
  7955. }
  7956. template<typename Parser, typename GlobalState, typename ErrorHandler>
  7957. constexpr auto
  7958. parser_interface<Parser, GlobalState, ErrorHandler>::operator>(
  7959. char32_t rhs) const noexcept
  7960. {
  7961. return *this > parser::lit(rhs);
  7962. }
  7963. template<typename Parser, typename GlobalState, typename ErrorHandler>
  7964. #if BOOST_PARSER_USE_CONCEPTS
  7965. template<parsable_range_like R>
  7966. #else
  7967. template<typename R, typename>
  7968. #endif
  7969. constexpr auto
  7970. parser_interface<Parser, GlobalState, ErrorHandler>::operator>(
  7971. R && r) const noexcept
  7972. {
  7973. return *this > parser::lit(r);
  7974. }
  7975. #endif
  7976. /** Returns a parser equivalent to `lit(c) > rhs`. */
  7977. template<typename Parser>
  7978. constexpr auto operator>(char c, parser_interface<Parser> rhs) noexcept
  7979. {
  7980. if constexpr (detail::is_seq_p<Parser>{}) {
  7981. return rhs.parser_.template prepend<false>(parser::lit(c));
  7982. } else {
  7983. return parser::lit(c) > rhs;
  7984. }
  7985. }
  7986. /** Returns a parser equivalent to `lit(c) > rhs`. */
  7987. template<typename Parser>
  7988. constexpr auto operator>(char32_t c, parser_interface<Parser> rhs) noexcept
  7989. {
  7990. if constexpr (detail::is_seq_p<Parser>{}) {
  7991. return rhs.parser_.template prepend<false>(parser::lit(c));
  7992. } else {
  7993. return parser::lit(c) > rhs;
  7994. }
  7995. }
  7996. /** Returns a parser equivalent to `lit(str) > rhs`. */
  7997. #if BOOST_PARSER_USE_CONCEPTS
  7998. template<parsable_range_like R, typename Parser>
  7999. #else
  8000. template<
  8001. typename R,
  8002. typename Parser,
  8003. typename Enable = std::enable_if_t<detail::is_parsable_range_like_v<R>>>
  8004. #endif
  8005. constexpr auto operator>(R && r, parser_interface<Parser> rhs) noexcept
  8006. {
  8007. if constexpr (detail::is_seq_p<Parser>{}) {
  8008. return rhs.parser_.template prepend<false>(parser::lit(r));
  8009. } else {
  8010. return parser::lit(r) > rhs;
  8011. }
  8012. }
  8013. #ifndef BOOST_PARSER_DOXYGEN
  8014. template<typename Parser, typename GlobalState, typename ErrorHandler>
  8015. constexpr auto
  8016. parser_interface<Parser, GlobalState, ErrorHandler>::operator|(
  8017. char rhs) const noexcept
  8018. {
  8019. return *this | parser::lit(rhs);
  8020. }
  8021. template<typename Parser, typename GlobalState, typename ErrorHandler>
  8022. constexpr auto
  8023. parser_interface<Parser, GlobalState, ErrorHandler>::operator|(
  8024. char32_t rhs) const noexcept
  8025. {
  8026. return *this | parser::lit(rhs);
  8027. }
  8028. template<typename Parser, typename GlobalState, typename ErrorHandler>
  8029. #if BOOST_PARSER_USE_CONCEPTS
  8030. template<parsable_range_like R>
  8031. #else
  8032. template<typename R, typename>
  8033. #endif
  8034. constexpr auto
  8035. parser_interface<Parser, GlobalState, ErrorHandler>::operator|(
  8036. R && r) const noexcept
  8037. {
  8038. return *this | parser::lit(r);
  8039. }
  8040. #endif
  8041. /** Returns a parser equivalent to `lit(c) | rhs`. */
  8042. template<typename Parser>
  8043. constexpr auto operator|(char c, parser_interface<Parser> rhs) noexcept
  8044. {
  8045. if constexpr (detail::is_or_p<Parser>{}) {
  8046. return rhs.parser_.prepend(parser::lit(c));
  8047. } else {
  8048. return parser::lit(c) | rhs;
  8049. }
  8050. }
  8051. /** Returns a parser equivalent to `lit(c) | rhs`. */
  8052. template<typename Parser>
  8053. constexpr auto operator|(char32_t c, parser_interface<Parser> rhs) noexcept
  8054. {
  8055. if constexpr (detail::is_or_p<Parser>{}) {
  8056. return rhs.parser_.prepend(parser::lit(c));
  8057. } else {
  8058. return parser::lit(c) | rhs;
  8059. }
  8060. }
  8061. /** Returns a parser equivalent to `lit(str) | rhs`. */
  8062. #if BOOST_PARSER_USE_CONCEPTS
  8063. template<parsable_range_like R, typename Parser>
  8064. #else
  8065. template<
  8066. typename R,
  8067. typename Parser,
  8068. typename Enable = std::enable_if_t<detail::is_parsable_range_like_v<R>>>
  8069. #endif
  8070. constexpr auto operator|(R && r, parser_interface<Parser> rhs) noexcept
  8071. {
  8072. if constexpr (detail::is_or_p<Parser>{}) {
  8073. return rhs.parser_.prepend(parser::lit(r));
  8074. } else {
  8075. return parser::lit(r) | rhs;
  8076. }
  8077. }
  8078. #ifndef BOOST_PARSER_DOXYGEN
  8079. template<typename Parser, typename GlobalState, typename ErrorHandler>
  8080. constexpr auto
  8081. parser_interface<Parser, GlobalState, ErrorHandler>::operator-(
  8082. char rhs) const noexcept
  8083. {
  8084. return !parser::lit(rhs) >> *this;
  8085. }
  8086. template<typename Parser, typename GlobalState, typename ErrorHandler>
  8087. constexpr auto
  8088. parser_interface<Parser, GlobalState, ErrorHandler>::operator-(
  8089. char32_t rhs) const noexcept
  8090. {
  8091. return !parser::lit(rhs) >> *this;
  8092. }
  8093. template<typename Parser, typename GlobalState, typename ErrorHandler>
  8094. #if BOOST_PARSER_USE_CONCEPTS
  8095. template<parsable_range_like R>
  8096. #else
  8097. template<typename R, typename>
  8098. #endif
  8099. constexpr auto
  8100. parser_interface<Parser, GlobalState, ErrorHandler>::operator-(
  8101. R && r) const noexcept
  8102. {
  8103. return !parser::lit(r) >> *this;
  8104. }
  8105. #endif
  8106. /** Returns a parser equivalent to `!rhs >> lit(c)`. */
  8107. template<typename Parser>
  8108. constexpr auto operator-(char c, parser_interface<Parser> rhs) noexcept
  8109. {
  8110. return !rhs >> parser::lit(c);
  8111. }
  8112. /** Returns a parser equivalent to `!rhs >> lit(c)`. */
  8113. template<typename Parser>
  8114. constexpr auto operator-(char32_t c, parser_interface<Parser> rhs) noexcept
  8115. {
  8116. return !rhs >> parser::lit(c);
  8117. }
  8118. /** Returns a parser equivalent to `!rhs >> lit(str)`. */
  8119. #if BOOST_PARSER_USE_CONCEPTS
  8120. template<parsable_range_like R, typename Parser>
  8121. #else
  8122. template<
  8123. typename R,
  8124. typename Parser,
  8125. typename Enable = std::enable_if_t<detail::is_parsable_range_like_v<R>>>
  8126. #endif
  8127. constexpr auto operator-(R && r, parser_interface<Parser> rhs) noexcept
  8128. {
  8129. return !rhs >> parser::lit(r);
  8130. }
  8131. #ifndef BOOST_PARSER_DOXYGEN
  8132. template<typename Parser, typename GlobalState, typename ErrorHandler>
  8133. constexpr auto
  8134. parser_interface<Parser, GlobalState, ErrorHandler>::operator%(
  8135. char rhs) const noexcept
  8136. {
  8137. return *this % parser::lit(rhs);
  8138. }
  8139. template<typename Parser, typename GlobalState, typename ErrorHandler>
  8140. constexpr auto
  8141. parser_interface<Parser, GlobalState, ErrorHandler>::operator%(
  8142. char32_t rhs) const noexcept
  8143. {
  8144. return *this % parser::lit(rhs);
  8145. }
  8146. template<typename Parser, typename GlobalState, typename ErrorHandler>
  8147. #if BOOST_PARSER_USE_CONCEPTS
  8148. template<parsable_range_like R>
  8149. #else
  8150. template<typename R, typename>
  8151. #endif
  8152. constexpr auto
  8153. parser_interface<Parser, GlobalState, ErrorHandler>::operator%(
  8154. R && r) const noexcept
  8155. {
  8156. return *this % parser::lit(r);
  8157. }
  8158. #endif
  8159. /** Returns a parser equivalent to `lit(c) % rhs`. */
  8160. template<typename Parser>
  8161. constexpr auto operator%(char c, parser_interface<Parser> rhs) noexcept
  8162. {
  8163. return parser::lit(c) % rhs;
  8164. }
  8165. /** Returns a parser equivalent to `lit(c) % rhs`. */
  8166. template<typename Parser>
  8167. constexpr auto operator%(char32_t c, parser_interface<Parser> rhs) noexcept
  8168. {
  8169. return parser::lit(c) % rhs;
  8170. }
  8171. /** Returns a parser equivalent to `lit(str) % rhs`. */
  8172. #if BOOST_PARSER_USE_CONCEPTS
  8173. template<parsable_range_like R, typename Parser>
  8174. #else
  8175. template<
  8176. typename R,
  8177. typename Parser,
  8178. typename Enable = std::enable_if_t<detail::is_parsable_range_like_v<R>>>
  8179. #endif
  8180. constexpr auto operator%(R && r, parser_interface<Parser> rhs) noexcept
  8181. {
  8182. return parser::lit(r) % rhs;
  8183. }
  8184. }}
  8185. #include <boost/parser/detail/printing_impl.hpp>
  8186. namespace boost { namespace parser {
  8187. /** An enumeration used for parameters to enable and disable trace in the
  8188. `*parse()` functions. */
  8189. enum class trace { off, on };
  8190. // Parse API.
  8191. /** Parses `[first, last)` using `parser`, and returns whether the parse
  8192. was successful. On success, `attr` will be assigned the value of the
  8193. attribute produced by `parser`. If `trace_mode == trace::on`, a
  8194. verbose trace of the parse will be streamed to `std::cout`.
  8195. \tparam Attr Constrained by
  8196. `!detail::derived_from_parser_interface_v<std::remove_cvref_t<Attr>`. */
  8197. #if BOOST_PARSER_USE_CONCEPTS
  8198. template<
  8199. parsable_iter I,
  8200. std::sentinel_for<I> S,
  8201. typename Parser,
  8202. typename GlobalState,
  8203. error_handler<I, S, GlobalState> ErrorHandler,
  8204. typename Attr>
  8205. #else
  8206. template<
  8207. typename I,
  8208. typename S,
  8209. typename Parser,
  8210. typename GlobalState,
  8211. typename ErrorHandler,
  8212. typename Attr,
  8213. typename Enable = std::enable_if_t<
  8214. detail::is_parsable_iter_v<I> &&
  8215. detail::is_equality_comparable_with_v<I, S> &&
  8216. !detail::derived_from_parser_interface_v<
  8217. detail::remove_cv_ref_t<Attr>>>>
  8218. #endif
  8219. bool prefix_parse(
  8220. I & first,
  8221. S last,
  8222. parser_interface<Parser, GlobalState, ErrorHandler> const & parser,
  8223. Attr & attr,
  8224. trace trace_mode = trace::off)
  8225. #if BOOST_PARSER_USE_CONCEPTS
  8226. requires(
  8227. !detail::derived_from_parser_interface_v<std::remove_cvref_t<Attr>>)
  8228. #endif
  8229. {
  8230. detail::attr_reset reset(attr);
  8231. if constexpr (!detail::is_char8_iter_v<I>) {
  8232. static_assert(
  8233. decltype(detail::has_attribute(first, last, parser)){},
  8234. "If you're seeing this error, you're trying to get parse() to "
  8235. "fill in attr above, using the attribute generated by parser. "
  8236. "However, parser does not generate an attribute.");
  8237. if (trace_mode == trace::on) {
  8238. return reset = detail::parse_impl<true>(
  8239. first, last, parser, parser.error_handler_, attr);
  8240. } else {
  8241. return reset = detail::parse_impl<false>(
  8242. first, last, parser, parser.error_handler_, attr);
  8243. }
  8244. } else {
  8245. auto r =
  8246. BOOST_PARSER_SUBRANGE(first, last) | detail::text::as_utf32;
  8247. auto f = r.begin();
  8248. auto const l = r.end();
  8249. auto _ = detail::scoped_base_assign(first, f);
  8250. static_assert(
  8251. decltype(detail::has_attribute(f, l, parser)){},
  8252. "If you're seeing this error, you're trying to get parse() to "
  8253. "fill in attr above, using the attribute generated by parser. "
  8254. "However, parser does not generate an attribute.");
  8255. if (trace_mode == trace::on) {
  8256. return reset = detail::parse_impl<true>(
  8257. f, l, parser, parser.error_handler_, attr);
  8258. } else {
  8259. return reset = detail::parse_impl<false>(
  8260. f, l, parser, parser.error_handler_, attr);
  8261. }
  8262. }
  8263. }
  8264. /** Parses `r` using `parser`, and returns whether the parse was
  8265. successful. The entire input range `r` must be consumed for the parse
  8266. to be considered successful. On success, `attr` will be assigned the
  8267. value of the attribute produced by `parser`. If `trace_mode ==
  8268. trace::on`, a verbose trace of the parse will be streamed to
  8269. `std::cout`.
  8270. \tparam ErrorHandler Constrained by `error_handler<ErrorHandler,std::ranges::iterator_t<decltype(subrange_of(r))>, std::ranges::sentinel_t<decltype(subrange_of(r))>, GlobalState>`,
  8271. where `subrange_of` is an implementation detail that: creates
  8272. subranges out of pointers; trims trailing zeros off of bounded
  8273. arrays (such as string literals); and transcodes to UTF-32 if the
  8274. input is non-`char`.
  8275. \tparam Attr Constrained by
  8276. `!detail::derived_from_parser_interface_v<std::remove_cvref_t<Attr>`. */
  8277. #if BOOST_PARSER_USE_CONCEPTS
  8278. template<
  8279. parsable_range R,
  8280. typename Parser,
  8281. typename GlobalState,
  8282. typename ErrorHandler,
  8283. typename Attr>
  8284. #else
  8285. template<
  8286. typename R,
  8287. typename Parser,
  8288. typename GlobalState,
  8289. typename ErrorHandler,
  8290. typename Attr,
  8291. typename Enable = std::enable_if_t<
  8292. detail::is_parsable_range_v<R> &&
  8293. !detail::derived_from_parser_interface_v<
  8294. detail::remove_cv_ref_t<Attr>>>>
  8295. #endif
  8296. bool parse(
  8297. R const & r,
  8298. parser_interface<Parser, GlobalState, ErrorHandler> const & parser,
  8299. Attr & attr,
  8300. trace trace_mode = trace::off)
  8301. #if BOOST_PARSER_USE_CONCEPTS
  8302. // clang-format off
  8303. requires error_handler<
  8304. ErrorHandler,
  8305. std::ranges::iterator_t<decltype(detail::make_input_subrange(r))>,
  8306. std::ranges::sentinel_t<decltype(detail::make_input_subrange(r))>,
  8307. GlobalState> &&
  8308. (!detail::derived_from_parser_interface_v<std::remove_cvref_t<Attr>>)
  8309. // clang-format on
  8310. #endif
  8311. {
  8312. detail::attr_reset reset(attr);
  8313. auto r_ = detail::make_input_subrange(r);
  8314. auto first = r_.begin();
  8315. auto const last = r_.end();
  8316. auto const initial_first = first;
  8317. return reset = detail::if_full_parse(
  8318. initial_first,
  8319. first,
  8320. last,
  8321. parser.error_handler_,
  8322. parser::prefix_parse(first, last, parser, attr, trace_mode));
  8323. }
  8324. /** Parses `[first, last)` using `parser`. Returns a `std::optional`
  8325. containing the attribute produced by `parser` on parse success, and
  8326. `std::nullopt` on parse failure. If `trace_mode == trace::on`, a
  8327. verbose trace of the parse will be streamed to `std::cout`.
  8328. \tparam Attr Constrained by
  8329. `!detail::derived_from_parser_interface_v<std::remove_cvref_t<Attr>`. */
  8330. #if BOOST_PARSER_USE_CONCEPTS
  8331. template<
  8332. parsable_iter I,
  8333. std::sentinel_for<I> S,
  8334. typename Parser,
  8335. typename GlobalState,
  8336. error_handler<I, S, GlobalState> ErrorHandler>
  8337. #else
  8338. template<
  8339. typename I,
  8340. typename S,
  8341. typename Parser,
  8342. typename GlobalState,
  8343. typename ErrorHandler,
  8344. typename Enable = std::enable_if_t<
  8345. detail::is_parsable_iter_v<I> &&
  8346. detail::is_equality_comparable_with_v<I, S>>>
  8347. #endif
  8348. auto prefix_parse(
  8349. I & first,
  8350. S last,
  8351. parser_interface<Parser, GlobalState, ErrorHandler> const & parser,
  8352. trace trace_mode = trace::off)
  8353. {
  8354. if constexpr (!detail::is_char8_iter_v<I>) {
  8355. if (trace_mode == trace::on) {
  8356. return detail::parse_impl<true>(
  8357. first, last, parser, parser.error_handler_);
  8358. } else {
  8359. return detail::parse_impl<false>(
  8360. first, last, parser, parser.error_handler_);
  8361. }
  8362. } else {
  8363. auto r =
  8364. BOOST_PARSER_SUBRANGE(first, last) | detail::text::as_utf32;
  8365. auto f = r.begin();
  8366. auto const l = r.end();
  8367. auto _ = detail::scoped_base_assign(first, f);
  8368. if (trace_mode == trace::on) {
  8369. return detail::parse_impl<true>(
  8370. f, l, parser, parser.error_handler_);
  8371. } else {
  8372. return detail::parse_impl<false>(
  8373. f, l, parser, parser.error_handler_);
  8374. }
  8375. }
  8376. }
  8377. /** Parses `r` using `parser`. Returns a `std::optional` containing the
  8378. attribute produced by `parser` on parse success, and `std::nullopt` on
  8379. parse failure. The entire input range `r` must be consumed for the
  8380. parse to be considered successful. If `trace_mode == trace::on`, a
  8381. verbose trace of the parse will be streamed to `std::cout`.
  8382. \tparam ErrorHandler Constrained by `error_handler<ErrorHandler,std::ranges::iterator_t<decltype(subrange_of(r))>, std::ranges::sentinel_t<decltype(subrange_of(r))>, GlobalState>`,
  8383. where `subrange_of` is an implementation detail that: creates
  8384. subranges out of pointers; trims trailing zeros off of bounded
  8385. arrays (such as string literals); and transcodes to UTF-32 if the
  8386. input is non-`char`.
  8387. \tparam Attr Constrained by
  8388. `!detail::derived_from_parser_interface_v<std::remove_cvref_t<Attr>`. */
  8389. #if BOOST_PARSER_USE_CONCEPTS
  8390. template<
  8391. parsable_range R,
  8392. typename Parser,
  8393. typename GlobalState,
  8394. typename ErrorHandler>
  8395. #else
  8396. template<
  8397. typename R,
  8398. typename Parser,
  8399. typename GlobalState,
  8400. typename ErrorHandler,
  8401. typename Enable = std::enable_if_t<detail::is_parsable_range_v<R>>>
  8402. #endif
  8403. auto parse(
  8404. R const & r,
  8405. parser_interface<Parser, GlobalState, ErrorHandler> const & parser,
  8406. trace trace_mode = trace::off)
  8407. #if BOOST_PARSER_USE_CONCEPTS
  8408. // clang-format off
  8409. requires error_handler<
  8410. ErrorHandler,
  8411. std::ranges::iterator_t<decltype(detail::make_input_subrange(r))>,
  8412. std::ranges::sentinel_t<decltype(detail::make_input_subrange(r))>,
  8413. GlobalState>
  8414. // clang-format on
  8415. #endif
  8416. {
  8417. auto r_ = detail::make_input_subrange(r);
  8418. auto first = r_.begin();
  8419. auto const last = r_.end();
  8420. auto const initial_first = first;
  8421. return detail::if_full_parse(
  8422. initial_first,
  8423. first,
  8424. last,
  8425. parser.error_handler_,
  8426. parser::prefix_parse(first, last, parser, trace_mode));
  8427. }
  8428. /** Parses `[first, last)` using `parser`, skipping all input recognized
  8429. by `skip` between the application of any two parsers, and returns
  8430. whether the parse was successful. On success, `attr` will be assigned
  8431. the value of the attribute produced by `parser`. If `trace_mode ==
  8432. trace::on`, a verbose trace of the parse will be streamed to
  8433. `std::cout`. */
  8434. #if BOOST_PARSER_USE_CONCEPTS
  8435. template<
  8436. parsable_iter I,
  8437. std::sentinel_for<I> S,
  8438. typename Parser,
  8439. typename GlobalState,
  8440. error_handler<I, S, GlobalState> ErrorHandler,
  8441. typename SkipParser,
  8442. typename Attr>
  8443. #else
  8444. template<
  8445. typename I,
  8446. typename S,
  8447. typename Parser,
  8448. typename GlobalState,
  8449. typename ErrorHandler,
  8450. typename SkipParser,
  8451. typename Attr,
  8452. typename Enable = std::enable_if_t<
  8453. detail::is_parsable_iter_v<I> &&
  8454. detail::is_equality_comparable_with_v<I, S>>>
  8455. #endif
  8456. bool prefix_parse(
  8457. I & first,
  8458. S last,
  8459. parser_interface<Parser, GlobalState, ErrorHandler> const & parser,
  8460. parser_interface<SkipParser> const & skip,
  8461. Attr & attr,
  8462. trace trace_mode = trace::off)
  8463. {
  8464. detail::attr_reset reset(attr);
  8465. if constexpr (!detail::is_char8_iter_v<I>) {
  8466. static_assert(
  8467. decltype(detail::has_attribute(first, last, parser)){},
  8468. "If you're seeing this error, you're trying to get parse() to "
  8469. "fill in attr above, using the attribute generated by parser. "
  8470. "However, parser does not generate an attribute.");
  8471. if (trace_mode == trace::on) {
  8472. return reset = detail::skip_parse_impl<true>(
  8473. first,
  8474. last,
  8475. parser,
  8476. skip,
  8477. parser.error_handler_,
  8478. attr);
  8479. } else {
  8480. return reset = detail::skip_parse_impl<false>(
  8481. first,
  8482. last,
  8483. parser,
  8484. skip,
  8485. parser.error_handler_,
  8486. attr);
  8487. }
  8488. } else {
  8489. auto r =
  8490. BOOST_PARSER_SUBRANGE(first, last) | detail::text::as_utf32;
  8491. auto f = r.begin();
  8492. auto const l = r.end();
  8493. auto _ = detail::scoped_base_assign(first, f);
  8494. static_assert(
  8495. decltype(detail::has_attribute(f, l, parser)){},
  8496. "If you're seeing this error, you're trying to get parse() to "
  8497. "fill in attr above, using the attribute generated by parser. "
  8498. "However, parser does not generate an attribute.");
  8499. if (trace_mode == trace::on) {
  8500. return reset = detail::skip_parse_impl<true>(
  8501. f, l, parser, skip, parser.error_handler_, attr);
  8502. } else {
  8503. return reset = detail::skip_parse_impl<false>(
  8504. f, l, parser, skip, parser.error_handler_, attr);
  8505. }
  8506. }
  8507. }
  8508. /** Parses `r` using `parser`, skipping all input recognized by `skip`
  8509. between the application of any two parsers, and returns whether the
  8510. parse was successful. The entire input range `r` must be consumed for
  8511. the parse to be considered successful. On success, `attr` will be
  8512. assigned the value of the attribute produced by `parser`. If
  8513. `trace_mode == trace::on`, a verbose trace of the parse will be
  8514. streamed to `std::cout`.
  8515. \tparam ErrorHandler Constrained by `error_handler<ErrorHandler,std::ranges::iterator_t<decltype(subrange_of(r))>, std::ranges::sentinel_t<decltype(subrange_of(r))>, GlobalState>`,
  8516. where `subrange_of` is an implementation detail that: creates
  8517. subranges out of pointers; trims trailing zeros off of bounded
  8518. arrays (such as string literals); and transcodes to UTF-32 if the
  8519. input is non-`char`. */
  8520. #if BOOST_PARSER_USE_CONCEPTS
  8521. template<
  8522. parsable_range R,
  8523. typename Parser,
  8524. typename GlobalState,
  8525. typename ErrorHandler,
  8526. typename SkipParser,
  8527. typename Attr>
  8528. #else
  8529. template<
  8530. typename R,
  8531. typename Parser,
  8532. typename GlobalState,
  8533. typename ErrorHandler,
  8534. typename SkipParser,
  8535. typename Attr,
  8536. typename Enable = std::enable_if_t<detail::is_parsable_range_v<R>>>
  8537. #endif
  8538. bool parse(
  8539. R const & r,
  8540. parser_interface<Parser, GlobalState, ErrorHandler> const & parser,
  8541. parser_interface<SkipParser> const & skip,
  8542. Attr & attr,
  8543. trace trace_mode = trace::off)
  8544. #if BOOST_PARSER_USE_CONCEPTS
  8545. // clang-format off
  8546. requires error_handler<
  8547. ErrorHandler,
  8548. std::ranges::iterator_t<decltype(detail::make_input_subrange(r))>,
  8549. std::ranges::sentinel_t<decltype(detail::make_input_subrange(r))>,
  8550. GlobalState>
  8551. // clang-format on
  8552. #endif
  8553. {
  8554. detail::attr_reset reset(attr);
  8555. auto r_ = detail::make_input_subrange(r);
  8556. auto first = r_.begin();
  8557. auto const last = r_.end();
  8558. auto const initial_first = first;
  8559. return reset = detail::if_full_parse(
  8560. initial_first,
  8561. first,
  8562. last,
  8563. parser.error_handler_,
  8564. parser::prefix_parse(
  8565. first, last, parser, skip, attr, trace_mode));
  8566. }
  8567. /** Parses `[first, last)` using `parser`, skipping all input recognized
  8568. by `skip` between the application of any two parsers. Returns a
  8569. `std::optional` containing the attribute produced by `parser` on parse
  8570. success, and `std::nullopt` on parse failure. If `trace_mode ==
  8571. trace::on`, a verbose trace of the parse will be streamed to
  8572. `std::cout`. */
  8573. #if BOOST_PARSER_USE_CONCEPTS
  8574. template<
  8575. parsable_iter I,
  8576. std::sentinel_for<I> S,
  8577. typename Parser,
  8578. typename GlobalState,
  8579. error_handler<I, S, GlobalState> ErrorHandler,
  8580. typename SkipParser>
  8581. #else
  8582. template<
  8583. typename I,
  8584. typename S,
  8585. typename Parser,
  8586. typename GlobalState,
  8587. typename ErrorHandler,
  8588. typename SkipParser,
  8589. typename Enable = std::enable_if_t<
  8590. detail::is_parsable_iter_v<I> &&
  8591. detail::is_equality_comparable_with_v<I, S>>>
  8592. #endif
  8593. auto prefix_parse(
  8594. I & first,
  8595. S last,
  8596. parser_interface<Parser, GlobalState, ErrorHandler> const & parser,
  8597. parser_interface<SkipParser> const & skip,
  8598. trace trace_mode = trace::off)
  8599. {
  8600. if constexpr (!detail::is_char8_iter_v<I>) {
  8601. if (trace_mode == trace::on) {
  8602. return detail::skip_parse_impl<true>(
  8603. first, last, parser, skip, parser.error_handler_);
  8604. } else {
  8605. return detail::skip_parse_impl<false>(
  8606. first, last, parser, skip, parser.error_handler_);
  8607. }
  8608. } else {
  8609. auto r =
  8610. BOOST_PARSER_SUBRANGE(first, last) | detail::text::as_utf32;
  8611. auto f = r.begin();
  8612. auto const l = r.end();
  8613. auto _ = detail::scoped_base_assign(first, f);
  8614. if (trace_mode == trace::on) {
  8615. return detail::skip_parse_impl<true>(
  8616. f, l, parser, skip, parser.error_handler_);
  8617. } else {
  8618. return detail::skip_parse_impl<false>(
  8619. f, l, parser, skip, parser.error_handler_);
  8620. }
  8621. }
  8622. }
  8623. /** Parses `r` using `parser`, skipping all input recognized by `skip`
  8624. between the application of any two parsers. Returns a `std::optional`
  8625. containing the attribute produced by `parser` on parse success, and
  8626. `std::nullopt` on parse failure. The entire input range `r` must be
  8627. consumed for the parse to be considered successful. If `trace_mode ==
  8628. trace::on`, a verbose trace of the parse will be streamed to
  8629. `std::cout`.
  8630. \tparam ErrorHandler Constrained by `error_handler<ErrorHandler,std::ranges::iterator_t<decltype(subrange_of(r))>, std::ranges::sentinel_t<decltype(subrange_of(r))>, GlobalState>`,
  8631. where `subrange_of` is an implementation detail that: creates
  8632. subranges out of pointers; trims trailing zeros off of bounded
  8633. arrays (such as string literals); and transcodes to UTF-32 if the
  8634. input is non-`char`. */
  8635. #if BOOST_PARSER_USE_CONCEPTS
  8636. template<
  8637. parsable_range R,
  8638. typename Parser,
  8639. typename GlobalState,
  8640. typename ErrorHandler,
  8641. typename SkipParser>
  8642. #else
  8643. template<
  8644. typename R,
  8645. typename Parser,
  8646. typename GlobalState,
  8647. typename ErrorHandler,
  8648. typename SkipParser,
  8649. typename Enable = std::enable_if_t<detail::is_parsable_range_v<R>>>
  8650. #endif
  8651. auto parse(
  8652. R const & r,
  8653. parser_interface<Parser, GlobalState, ErrorHandler> const & parser,
  8654. parser_interface<SkipParser> const & skip,
  8655. trace trace_mode = trace::off)
  8656. #if BOOST_PARSER_USE_CONCEPTS
  8657. // clang-format off
  8658. requires error_handler<
  8659. ErrorHandler,
  8660. std::ranges::iterator_t<decltype(detail::make_input_subrange(r))>,
  8661. std::ranges::sentinel_t<decltype(detail::make_input_subrange(r))>,
  8662. GlobalState>
  8663. // clang-format on
  8664. #endif
  8665. {
  8666. auto r_ = detail::make_input_subrange(r);
  8667. auto first = r_.begin();
  8668. auto const last = r_.end();
  8669. auto const initial_first = first;
  8670. return detail::if_full_parse(
  8671. initial_first,
  8672. first,
  8673. last,
  8674. parser.error_handler_,
  8675. parser::prefix_parse(first, last, parser, skip, trace_mode));
  8676. }
  8677. /** Parses `[first, last)` using `parser`, and returns whether the parse
  8678. was successful. When a callback rule `r` is successful during the
  8679. parse, one of two things happens: 1) if `r` has an attribute,
  8680. `callbacks(tag, x)` will be called (where `tag` is
  8681. `decltype(r)::tag_type{}`, and `x` is the attribute produced by `r`);
  8682. or 2) if `r` has no attribute, `callbacks(tag)` will be called.
  8683. `Callbacks` is expected to be an invocable with the correct overloads
  8684. required to support all successful rule parses that might occur. If
  8685. `trace_mode == trace::on`, a verbose trace of the parse will be
  8686. streamed to `std::cout`. */
  8687. #if BOOST_PARSER_USE_CONCEPTS
  8688. template<
  8689. parsable_iter I,
  8690. std::sentinel_for<I> S,
  8691. typename Parser,
  8692. typename GlobalState,
  8693. error_handler<I, S, GlobalState> ErrorHandler,
  8694. typename Callbacks>
  8695. #else
  8696. template<
  8697. typename I,
  8698. typename S,
  8699. typename Parser,
  8700. typename GlobalState,
  8701. typename ErrorHandler,
  8702. typename Callbacks,
  8703. typename Enable = std::enable_if_t<
  8704. detail::is_parsable_iter_v<I> &&
  8705. detail::is_equality_comparable_with_v<I, S>>>
  8706. #endif
  8707. bool callback_prefix_parse(
  8708. I & first,
  8709. S last,
  8710. parser_interface<Parser, GlobalState, ErrorHandler> const & parser,
  8711. Callbacks const & callbacks,
  8712. trace trace_mode = trace::off)
  8713. {
  8714. if constexpr (!detail::is_char8_iter_v<I>) {
  8715. if (trace_mode == trace::on) {
  8716. return detail::callback_parse_impl<true>(
  8717. first, last, parser, parser.error_handler_, callbacks);
  8718. } else {
  8719. return detail::callback_parse_impl<false>(
  8720. first, last, parser, parser.error_handler_, callbacks);
  8721. }
  8722. } else {
  8723. auto r =
  8724. BOOST_PARSER_SUBRANGE(first, last) | detail::text::as_utf32;
  8725. auto f = r.begin();
  8726. auto const l = r.end();
  8727. auto _ = detail::scoped_base_assign(first, f);
  8728. if (trace_mode == trace::on) {
  8729. return detail::callback_parse_impl<true>(
  8730. f, l, parser, parser.error_handler_, callbacks);
  8731. } else {
  8732. return detail::callback_parse_impl<false>(
  8733. f, l, parser, parser.error_handler_, callbacks);
  8734. }
  8735. }
  8736. }
  8737. /** Parses `r` using `parser`, and returns whether the parse was
  8738. successful. The entire input range `r` must be consumed for the parse
  8739. to be considered successful. When a callback rule `r` is successful
  8740. during the parse, one of two things happens: 1) if `r` has an
  8741. attribute, `callbacks(tag, x)` will be called (where `tag` is
  8742. `decltype(r)::tag_type{}`, and `x` is the attribute produced by `r`);
  8743. or 2) if `r` has no attribute, `callbacks(tag)` will be called.
  8744. `Callbacks` is expected to be an invocable with the correct overloads
  8745. required to support all successful rule parses that might occur. If
  8746. `trace_mode == trace::on`, a verbose trace of the parse will be
  8747. streamed to `std::cout`.
  8748. \tparam ErrorHandler Constrained by `error_handler<ErrorHandler,std::ranges::iterator_t<decltype(subrange_of(r))>, std::ranges::sentinel_t<decltype(subrange_of(r))>, GlobalState>`,
  8749. where `subrange_of` is an implementation detail that: creates
  8750. subranges out of pointers; trims trailing zeros off of bounded
  8751. arrays (such as string literals); and transcodes to UTF-32 if the
  8752. input is non-`char`. */
  8753. #if BOOST_PARSER_USE_CONCEPTS
  8754. template<
  8755. parsable_range R,
  8756. typename Parser,
  8757. typename GlobalState,
  8758. typename ErrorHandler,
  8759. typename Callbacks>
  8760. #else
  8761. template<
  8762. typename R,
  8763. typename Parser,
  8764. typename GlobalState,
  8765. typename ErrorHandler,
  8766. typename Callbacks,
  8767. typename Enable = std::enable_if_t<detail::is_parsable_range_v<R>>>
  8768. #endif
  8769. bool callback_parse(
  8770. R const & r,
  8771. parser_interface<Parser, GlobalState, ErrorHandler> const & parser,
  8772. Callbacks const & callbacks,
  8773. trace trace_mode = trace::off)
  8774. #if BOOST_PARSER_USE_CONCEPTS
  8775. // clang-format off
  8776. requires error_handler<
  8777. ErrorHandler,
  8778. std::ranges::iterator_t<decltype(detail::make_input_subrange(r))>,
  8779. std::ranges::sentinel_t<decltype(detail::make_input_subrange(r))>,
  8780. GlobalState>
  8781. // clang-format on
  8782. #endif
  8783. {
  8784. auto r_ = detail::make_input_subrange(r);
  8785. auto first = r_.begin();
  8786. auto const last = r_.end();
  8787. auto const initial_first = first;
  8788. return detail::if_full_parse(
  8789. initial_first,
  8790. first,
  8791. last,
  8792. parser.error_handler_,
  8793. parser::callback_prefix_parse(first, last, parser, callbacks));
  8794. }
  8795. /** Parses `[first, last)` using `parser`, skipping all input recognized
  8796. by `skip` between the application of any two parsers, and returns
  8797. whether the parse was successful. When a callback rule `r` is
  8798. successful during the parse, one of two things happens: 1) if `r` has
  8799. an attribute, `callbacks(tag, x)` will be called (where `tag` is
  8800. `decltype(r)::tag_type{}`, and `x` is the attribute produced by `r`);
  8801. or 2) if `r` has no attribute, `callbacks(tag)` will be called.
  8802. `Callbacks` is expected to be an invocable with the correct overloads
  8803. required to support all successful rule parses that might occur. If
  8804. `trace_mode == trace::on`, a verbose trace of the parse will be
  8805. streamed to `std::cout`. */
  8806. #if BOOST_PARSER_USE_CONCEPTS
  8807. template<
  8808. parsable_iter I,
  8809. std::sentinel_for<I> S,
  8810. typename Parser,
  8811. typename GlobalState,
  8812. error_handler<I, S, GlobalState> ErrorHandler,
  8813. typename SkipParser,
  8814. typename Callbacks>
  8815. #else
  8816. template<
  8817. typename I,
  8818. typename S,
  8819. typename Parser,
  8820. typename GlobalState,
  8821. typename ErrorHandler,
  8822. typename SkipParser,
  8823. typename Callbacks,
  8824. typename Enable = std::enable_if_t<
  8825. detail::is_parsable_iter_v<I> &&
  8826. detail::is_equality_comparable_with_v<I, S>>>
  8827. #endif
  8828. bool callback_prefix_parse(
  8829. I & first,
  8830. S last,
  8831. parser_interface<Parser, GlobalState, ErrorHandler> const & parser,
  8832. parser_interface<SkipParser> const & skip,
  8833. Callbacks const & callbacks,
  8834. trace trace_mode = trace::off)
  8835. {
  8836. if constexpr (!detail::is_char8_iter_v<I>) {
  8837. if (trace_mode == trace::on) {
  8838. return detail::callback_skip_parse_impl<true>(
  8839. first,
  8840. last,
  8841. parser,
  8842. skip,
  8843. parser.error_handler_,
  8844. callbacks);
  8845. } else {
  8846. return detail::callback_skip_parse_impl<false>(
  8847. first,
  8848. last,
  8849. parser,
  8850. skip,
  8851. parser.error_handler_,
  8852. callbacks);
  8853. }
  8854. } else {
  8855. auto r =
  8856. BOOST_PARSER_SUBRANGE(first, last) | detail::text::as_utf32;
  8857. auto f = r.begin();
  8858. auto const l = r.end();
  8859. auto _ = detail::scoped_base_assign(first, f);
  8860. if (trace_mode == trace::on) {
  8861. return detail::callback_skip_parse_impl<true>(
  8862. f, l, parser, skip, parser.error_handler_, callbacks);
  8863. } else {
  8864. return detail::callback_skip_parse_impl<false>(
  8865. f, l, parser, skip, parser.error_handler_, callbacks);
  8866. }
  8867. }
  8868. }
  8869. /** Parses `r` using `parser`, skipping all input recognized by `skip`
  8870. between the application of any two parsers, and returns whether the
  8871. parse was successful. The entire input range `r` must be consumed for
  8872. the parse to be considered successful. When a callback rule `r` is
  8873. successful during the parse, one of two things happens: 1) if `r` has
  8874. an attribute, `callbacks(tag, x)` will be called (where `tag` is
  8875. `decltype(r)::tag_type{}`, and `x` is the attribute produced by `r`);
  8876. or 2) if `r` has no attribute, `callbacks(tag)` will be called.
  8877. `Callbacks` is expected to be an invocable with the correct overloads
  8878. required to support all successful rule parses that might occur. If
  8879. `trace_mode == trace::on`, a verbose trace of the parse will be
  8880. streamed to `std::cout`.
  8881. \tparam ErrorHandler Constrained by `error_handler<ErrorHandler,std::ranges::iterator_t<decltype(subrange_of(r))>, std::ranges::sentinel_t<decltype(subrange_of(r))>, GlobalState>`,
  8882. where `subrange_of` is an implementation detail that: creates
  8883. subranges out of pointers; trims trailing zeros off of bounded
  8884. arrays (such as string literals); and transcodes to UTF-32 if the
  8885. input is non-`char`. */
  8886. #if BOOST_PARSER_USE_CONCEPTS
  8887. template<
  8888. parsable_range R,
  8889. typename Parser,
  8890. typename GlobalState,
  8891. typename ErrorHandler,
  8892. typename SkipParser,
  8893. typename Callbacks>
  8894. #else
  8895. template<
  8896. typename R,
  8897. typename Parser,
  8898. typename GlobalState,
  8899. typename ErrorHandler,
  8900. typename SkipParser,
  8901. typename Callbacks,
  8902. typename Enable = std::enable_if_t<detail::is_parsable_range_v<R>>>
  8903. #endif
  8904. bool callback_parse(
  8905. R const & r,
  8906. parser_interface<Parser, GlobalState, ErrorHandler> const & parser,
  8907. parser_interface<SkipParser> const & skip,
  8908. Callbacks const & callbacks,
  8909. trace trace_mode = trace::off)
  8910. #if BOOST_PARSER_USE_CONCEPTS
  8911. // clang-format off
  8912. requires error_handler<
  8913. ErrorHandler,
  8914. std::ranges::iterator_t<decltype(detail::make_input_subrange(r))>,
  8915. std::ranges::sentinel_t<decltype(detail::make_input_subrange(r))>,
  8916. GlobalState>
  8917. // clang-format on
  8918. #endif
  8919. {
  8920. auto r_ = detail::make_input_subrange(r);
  8921. auto first = r_.begin();
  8922. auto const last = r_.end();
  8923. auto const initial_first = first;
  8924. return detail::if_full_parse(
  8925. initial_first,
  8926. first,
  8927. last,
  8928. parser.error_handler_,
  8929. parser::callback_prefix_parse(
  8930. first, last, parser, skip, callbacks, trace_mode));
  8931. }
  8932. namespace literals {
  8933. /** Returns a literal parser equivalent to `lit(c)`. */
  8934. constexpr auto operator""_l(char c) { return parser::lit(c); }
  8935. #if defined(__cpp_char8_t) || defined(BOOST_PARSER_DOXYGEN)
  8936. /** Returns a literal parser equivalent to `lit(c)`. */
  8937. constexpr auto operator""_l(char8_t c) { return parser::lit(c); }
  8938. #endif
  8939. /** Returns a literal parser equivalent to `lit(c)`. */
  8940. constexpr auto operator""_l(char32_t c) { return parser::lit(c); }
  8941. /** Returns a literal parser equivalent to `lit(str)`. */
  8942. constexpr auto operator""_l(char const * str, std::size_t)
  8943. {
  8944. return parser::lit(str);
  8945. }
  8946. #if defined(__cpp_char8_t) || defined(BOOST_PARSER_DOXYGEN)
  8947. /** Returns a literal parser equivalent to `lit(str)`. */
  8948. constexpr auto operator""_l(char8_t const * str, std::size_t)
  8949. {
  8950. return parser::lit(str);
  8951. }
  8952. #endif
  8953. /** Returns a literal parser equivalent to `lit(str)`. */
  8954. constexpr auto operator""_l(char32_t const * str, std::size_t)
  8955. {
  8956. return parser::lit(str);
  8957. }
  8958. /** Returns a character parser equivalent to `char_(c)`. */
  8959. constexpr auto operator""_p(char c) { return char_(c); }
  8960. #if defined(__cpp_char8_t) || defined(BOOST_PARSER_DOXYGEN)
  8961. /** Returns a character parser equivalent to `char_(c)`. */
  8962. constexpr auto operator""_p(char8_t c) { return char_(c); }
  8963. #endif
  8964. /** Returns a character parser equivalent to `char_(c)`. */
  8965. constexpr auto operator""_p(char32_t c) { return char_(c); }
  8966. /** Returns a string parser equivalent to `string(str)`. */
  8967. constexpr auto operator""_p(char const * str, std::size_t)
  8968. {
  8969. return parser::string(str);
  8970. }
  8971. #if defined(__cpp_char8_t) || defined(BOOST_PARSER_DOXYGEN)
  8972. /** Returns a string parser equivalent to `string(str)`. */
  8973. constexpr auto operator""_p(char8_t const * str, std::size_t)
  8974. {
  8975. return parser::string(str);
  8976. }
  8977. #endif
  8978. /** Returns a string parser equivalent to `string(str)`. */
  8979. constexpr auto operator""_p(char32_t const * str, std::size_t)
  8980. {
  8981. return parser::string(str);
  8982. }
  8983. }
  8984. namespace detail {
  8985. template<typename R, typename Parser, typename SkipParser>
  8986. struct attribute_impl
  8987. {
  8988. using parser_type = typename Parser::parser_type;
  8989. using global_state_type = typename Parser::global_state_type;
  8990. using error_handler_type = typename Parser::error_handler_type;
  8991. using iterator = detail::iterator_t<R>;
  8992. using sentinel = detail::sentinel_t<R>;
  8993. using context = decltype(detail::make_context<false, false>(
  8994. std::declval<iterator>(),
  8995. std::declval<sentinel>(),
  8996. std::declval<bool &>(),
  8997. std::declval<int &>(),
  8998. std::declval<error_handler_type>(),
  8999. std::declval<global_state_type &>(),
  9000. std::declval<detail::symbol_table_tries_t &>(),
  9001. std::declval<detail::pending_symbol_table_operations_t &>()));
  9002. using type = decltype(std::declval<Parser>()(
  9003. std::declval<iterator &>(),
  9004. std::declval<sentinel>(),
  9005. std::declval<context>(),
  9006. SkipParser{},
  9007. detail::flags::gen_attrs,
  9008. std::declval<bool &>()));
  9009. };
  9010. template<typename Iter, typename Sentinel, typename Parser>
  9011. auto has_attribute(Iter first, Sentinel last, Parser parser)
  9012. {
  9013. using attr_t = typename attribute_impl<
  9014. BOOST_PARSER_SUBRANGE<Iter, Sentinel>,
  9015. Parser>::type;
  9016. return std::integral_constant<bool, !is_nope_v<attr_t>>{};
  9017. }
  9018. template<typename T>
  9019. constexpr wrapper<T> attr_wrapped_final;
  9020. template<>
  9021. inline constexpr wrapper<none> attr_wrapped_final<nope>;
  9022. }
  9023. template<typename R, typename Parser>
  9024. struct attribute
  9025. {
  9026. using initial_type = typename detail::attribute_impl<
  9027. decltype(detail::make_input_subrange(std::declval<R>())),
  9028. Parser>::type;
  9029. using type =
  9030. typename decltype(detail::attr_wrapped_final<initial_type>)::type;
  9031. };
  9032. namespace detail {
  9033. template<typename... Args>
  9034. constexpr void static_assert_merge_attributes(tuple<Args...> parsers)
  9035. {
  9036. using context_t = parse_context<
  9037. false,
  9038. false,
  9039. char const *,
  9040. char const *,
  9041. default_error_handler>;
  9042. using skipper_t = parser_interface<ws_parser<false, false>>;
  9043. using use_parser_t = dummy_use_parser_t<
  9044. char const *,
  9045. char const *,
  9046. context_t,
  9047. skipper_t> const;
  9048. using all_types =
  9049. decltype(hl::transform(parsers, std::declval<use_parser_t>()));
  9050. auto all_types_wrapped = hl::transform(all_types{}, detail::wrap{});
  9051. auto first_non_nope = hl::fold_left(
  9052. all_types_wrapped,
  9053. wrapper<nope>{},
  9054. [=](auto result, auto type) {
  9055. if constexpr (is_nope_v<typename decltype(result)::type>) {
  9056. return type;
  9057. } else {
  9058. return result;
  9059. }
  9060. });
  9061. using first_t = typename decltype(first_non_nope)::type;
  9062. static_assert(
  9063. !is_nope_v<first_t>,
  9064. "It looks like you wrote merge[p1 >> p2 >> ... pn], and none "
  9065. "of the parsers p1, p2, ... pn produces an attribute. Please "
  9066. "fix.");
  9067. if constexpr (is_nope_v<first_t>) {
  9068. [[maybe_unused]] detail::print_type<tuple<Args...>> tuple_types;
  9069. [[maybe_unused]] detail::print_type<all_types> attribute_types;
  9070. }
  9071. hl::for_each(all_types_wrapped, [=](auto type) {
  9072. using t = typename decltype(type)::type;
  9073. if constexpr (!is_nope_v<t>) {
  9074. static_assert(
  9075. std::is_same_v<t, first_t>,
  9076. "If you see an error here, you wrote merge[p1 >> "
  9077. "p2 >> ... pn] where at least one of the types in "
  9078. "ATTR(p1), ATTR(p2), ... ATTR(pn) is not the same "
  9079. "type as one of the others.");
  9080. if constexpr (!std::is_same_v<t, first_t>) {
  9081. [[maybe_unused]] detail::print_type<tuple<Args...>>
  9082. tuple_types(parsers);
  9083. [[maybe_unused]] detail::print_type<all_types>
  9084. attribute_types;
  9085. [[maybe_unused]] detail::print_type<first_t> first_type;
  9086. [[maybe_unused]] detail::print_type<t> this_type;
  9087. }
  9088. }
  9089. });
  9090. }
  9091. }
  9092. }}
  9093. #endif