basic_parser_impl.hpp 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  3. // Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // Official repository: https://github.com/boostorg/json
  9. //
  10. #ifndef BOOST_JSON_BASIC_PARSER_IMPL_HPP
  11. #define BOOST_JSON_BASIC_PARSER_IMPL_HPP
  12. #include <boost/json/detail/config.hpp>
  13. #include <boost/json/basic_parser.hpp>
  14. #include <boost/json/error.hpp>
  15. #include <boost/json/detail/buffer.hpp>
  16. #include <boost/json/detail/sse2.hpp>
  17. #include <cmath>
  18. #include <limits>
  19. #include <cstring>
  20. #ifdef _MSC_VER
  21. #pragma warning(push)
  22. #pragma warning(disable: 4702) // unreachable code
  23. #pragma warning(disable: 4127) // conditional expression is constant
  24. #endif
  25. /* This file must be manually included to get the
  26. function template definitions for basic_parser.
  27. */
  28. /* Reference:
  29. https://www.json.org/
  30. RFC 7159: The JavaScript Object Notation (JSON) Data Interchange Format
  31. https://tools.ietf.org/html/rfc7159
  32. https://ampl.com/netlib/fp/dtoa.c
  33. */
  34. #ifndef BOOST_JSON_DOCS
  35. namespace boost {
  36. namespace json {
  37. namespace detail {
  38. inline
  39. double
  40. pow10(int exp) noexcept
  41. {
  42. static double const tab[618] = {
  43. 1e-308, 1e-307, 1e-306, 1e-305, 1e-304, 1e-303, 1e-302, 1e-301,
  44. 1e-300, 1e-299, 1e-298, 1e-297, 1e-296, 1e-295, 1e-294, 1e-293, 1e-292, 1e-291,
  45. 1e-290, 1e-289, 1e-288, 1e-287, 1e-286, 1e-285, 1e-284, 1e-283, 1e-282, 1e-281,
  46. 1e-280, 1e-279, 1e-278, 1e-277, 1e-276, 1e-275, 1e-274, 1e-273, 1e-272, 1e-271,
  47. 1e-270, 1e-269, 1e-268, 1e-267, 1e-266, 1e-265, 1e-264, 1e-263, 1e-262, 1e-261,
  48. 1e-260, 1e-259, 1e-258, 1e-257, 1e-256, 1e-255, 1e-254, 1e-253, 1e-252, 1e-251,
  49. 1e-250, 1e-249, 1e-248, 1e-247, 1e-246, 1e-245, 1e-244, 1e-243, 1e-242, 1e-241,
  50. 1e-240, 1e-239, 1e-238, 1e-237, 1e-236, 1e-235, 1e-234, 1e-233, 1e-232, 1e-231,
  51. 1e-230, 1e-229, 1e-228, 1e-227, 1e-226, 1e-225, 1e-224, 1e-223, 1e-222, 1e-221,
  52. 1e-220, 1e-219, 1e-218, 1e-217, 1e-216, 1e-215, 1e-214, 1e-213, 1e-212, 1e-211,
  53. 1e-210, 1e-209, 1e-208, 1e-207, 1e-206, 1e-205, 1e-204, 1e-203, 1e-202, 1e-201,
  54. 1e-200, 1e-199, 1e-198, 1e-197, 1e-196, 1e-195, 1e-194, 1e-193, 1e-192, 1e-191,
  55. 1e-190, 1e-189, 1e-188, 1e-187, 1e-186, 1e-185, 1e-184, 1e-183, 1e-182, 1e-181,
  56. 1e-180, 1e-179, 1e-178, 1e-177, 1e-176, 1e-175, 1e-174, 1e-173, 1e-172, 1e-171,
  57. 1e-170, 1e-169, 1e-168, 1e-167, 1e-166, 1e-165, 1e-164, 1e-163, 1e-162, 1e-161,
  58. 1e-160, 1e-159, 1e-158, 1e-157, 1e-156, 1e-155, 1e-154, 1e-153, 1e-152, 1e-151,
  59. 1e-150, 1e-149, 1e-148, 1e-147, 1e-146, 1e-145, 1e-144, 1e-143, 1e-142, 1e-141,
  60. 1e-140, 1e-139, 1e-138, 1e-137, 1e-136, 1e-135, 1e-134, 1e-133, 1e-132, 1e-131,
  61. 1e-130, 1e-129, 1e-128, 1e-127, 1e-126, 1e-125, 1e-124, 1e-123, 1e-122, 1e-121,
  62. 1e-120, 1e-119, 1e-118, 1e-117, 1e-116, 1e-115, 1e-114, 1e-113, 1e-112, 1e-111,
  63. 1e-110, 1e-109, 1e-108, 1e-107, 1e-106, 1e-105, 1e-104, 1e-103, 1e-102, 1e-101,
  64. 1e-100, 1e-099, 1e-098, 1e-097, 1e-096, 1e-095, 1e-094, 1e-093, 1e-092, 1e-091,
  65. 1e-090, 1e-089, 1e-088, 1e-087, 1e-086, 1e-085, 1e-084, 1e-083, 1e-082, 1e-081,
  66. 1e-080, 1e-079, 1e-078, 1e-077, 1e-076, 1e-075, 1e-074, 1e-073, 1e-072, 1e-071,
  67. 1e-070, 1e-069, 1e-068, 1e-067, 1e-066, 1e-065, 1e-064, 1e-063, 1e-062, 1e-061,
  68. 1e-060, 1e-059, 1e-058, 1e-057, 1e-056, 1e-055, 1e-054, 1e-053, 1e-052, 1e-051,
  69. 1e-050, 1e-049, 1e-048, 1e-047, 1e-046, 1e-045, 1e-044, 1e-043, 1e-042, 1e-041,
  70. 1e-040, 1e-039, 1e-038, 1e-037, 1e-036, 1e-035, 1e-034, 1e-033, 1e-032, 1e-031,
  71. 1e-030, 1e-029, 1e-028, 1e-027, 1e-026, 1e-025, 1e-024, 1e-023, 1e-022, 1e-021,
  72. 1e-020, 1e-019, 1e-018, 1e-017, 1e-016, 1e-015, 1e-014, 1e-013, 1e-012, 1e-011,
  73. 1e-010, 1e-009, 1e-008, 1e-007, 1e-006, 1e-005, 1e-004, 1e-003, 1e-002, 1e-001,
  74. 1e+000, 1e+001, 1e+002, 1e+003, 1e+004, 1e+005, 1e+006, 1e+007, 1e+008, 1e+009,
  75. 1e+010, 1e+011, 1e+012, 1e+013, 1e+014, 1e+015, 1e+016, 1e+017, 1e+018, 1e+019,
  76. 1e+020, 1e+021, 1e+022, 1e+023, 1e+024, 1e+025, 1e+026, 1e+027, 1e+028, 1e+029,
  77. 1e+030, 1e+031, 1e+032, 1e+033, 1e+034, 1e+035, 1e+036, 1e+037, 1e+038, 1e+039,
  78. 1e+040, 1e+041, 1e+042, 1e+043, 1e+044, 1e+045, 1e+046, 1e+047, 1e+048, 1e+049,
  79. 1e+050, 1e+051, 1e+052, 1e+053, 1e+054, 1e+055, 1e+056, 1e+057, 1e+058, 1e+059,
  80. 1e+060, 1e+061, 1e+062, 1e+063, 1e+064, 1e+065, 1e+066, 1e+067, 1e+068, 1e+069,
  81. 1e+070, 1e+071, 1e+072, 1e+073, 1e+074, 1e+075, 1e+076, 1e+077, 1e+078, 1e+079,
  82. 1e+080, 1e+081, 1e+082, 1e+083, 1e+084, 1e+085, 1e+086, 1e+087, 1e+088, 1e+089,
  83. 1e+090, 1e+091, 1e+092, 1e+093, 1e+094, 1e+095, 1e+096, 1e+097, 1e+098, 1e+099,
  84. 1e+100, 1e+101, 1e+102, 1e+103, 1e+104, 1e+105, 1e+106, 1e+107, 1e+108, 1e+109,
  85. 1e+110, 1e+111, 1e+112, 1e+113, 1e+114, 1e+115, 1e+116, 1e+117, 1e+118, 1e+119,
  86. 1e+120, 1e+121, 1e+122, 1e+123, 1e+124, 1e+125, 1e+126, 1e+127, 1e+128, 1e+129,
  87. 1e+130, 1e+131, 1e+132, 1e+133, 1e+134, 1e+135, 1e+136, 1e+137, 1e+138, 1e+139,
  88. 1e+140, 1e+141, 1e+142, 1e+143, 1e+144, 1e+145, 1e+146, 1e+147, 1e+148, 1e+149,
  89. 1e+150, 1e+151, 1e+152, 1e+153, 1e+154, 1e+155, 1e+156, 1e+157, 1e+158, 1e+159,
  90. 1e+160, 1e+161, 1e+162, 1e+163, 1e+164, 1e+165, 1e+166, 1e+167, 1e+168, 1e+169,
  91. 1e+170, 1e+171, 1e+172, 1e+173, 1e+174, 1e+175, 1e+176, 1e+177, 1e+178, 1e+179,
  92. 1e+180, 1e+181, 1e+182, 1e+183, 1e+184, 1e+185, 1e+186, 1e+187, 1e+188, 1e+189,
  93. 1e+190, 1e+191, 1e+192, 1e+193, 1e+194, 1e+195, 1e+196, 1e+197, 1e+198, 1e+199,
  94. 1e+200, 1e+201, 1e+202, 1e+203, 1e+204, 1e+205, 1e+206, 1e+207, 1e+208, 1e+209,
  95. 1e+210, 1e+211, 1e+212, 1e+213, 1e+214, 1e+215, 1e+216, 1e+217, 1e+218, 1e+219,
  96. 1e+220, 1e+221, 1e+222, 1e+223, 1e+224, 1e+225, 1e+226, 1e+227, 1e+228, 1e+229,
  97. 1e+230, 1e+231, 1e+232, 1e+233, 1e+234, 1e+235, 1e+236, 1e+237, 1e+238, 1e+239,
  98. 1e+240, 1e+241, 1e+242, 1e+243, 1e+244, 1e+245, 1e+246, 1e+247, 1e+248, 1e+249,
  99. 1e+250, 1e+251, 1e+252, 1e+253, 1e+254, 1e+255, 1e+256, 1e+257, 1e+258, 1e+259,
  100. 1e+260, 1e+261, 1e+262, 1e+263, 1e+264, 1e+265, 1e+266, 1e+267, 1e+268, 1e+269,
  101. 1e+270, 1e+271, 1e+272, 1e+273, 1e+274, 1e+275, 1e+276, 1e+277, 1e+278, 1e+279,
  102. 1e+280, 1e+281, 1e+282, 1e+283, 1e+284, 1e+285, 1e+286, 1e+287, 1e+288, 1e+289,
  103. 1e+290, 1e+291, 1e+292, 1e+293, 1e+294, 1e+295, 1e+296, 1e+297, 1e+298, 1e+299,
  104. 1e+300, 1e+301, 1e+302, 1e+303, 1e+304, 1e+305, 1e+306, 1e+307, 1e+308 };
  105. if( exp > 308 )
  106. {
  107. return std::numeric_limits<double>::infinity();
  108. }
  109. else if( exp < -308 )
  110. {
  111. // due to the way pow10 is used by dec_to_float,
  112. // we can afford to return 0.0 here
  113. return 0.0;
  114. }
  115. else
  116. {
  117. exp += 308;
  118. BOOST_ASSERT(exp >= 0 && exp < 618);
  119. return tab[exp];
  120. }
  121. }
  122. inline
  123. double
  124. dec_to_float(
  125. std::uint64_t m,
  126. std::int32_t e,
  127. bool neg) noexcept
  128. {
  129. // convert to double explicitly to silence warnings
  130. double x = static_cast<double>(m);
  131. if(neg)
  132. x = -x;
  133. if(e < -305)
  134. {
  135. x *= 1e-305 ;
  136. e += 305;
  137. }
  138. if(e >= -22 && e < 0)
  139. return x / pow10(-e);
  140. return x * pow10(e);
  141. }
  142. inline
  143. bool
  144. is_control(char c) noexcept
  145. {
  146. return static_cast<unsigned char>(c) < 32;
  147. }
  148. inline
  149. int
  150. hex_digit(unsigned char c) noexcept
  151. {
  152. // by Peter Dimov
  153. if( c >= '0' && c <= '9' )
  154. return c - '0';
  155. c &= ~0x20;
  156. if( c >= 'A' && c <= 'F' )
  157. return 10 + c - 'A';
  158. return -1;
  159. }
  160. } // detail
  161. //----------------------------------------------------------
  162. template<class Handler>
  163. void
  164. basic_parser<Handler>::
  165. reserve()
  166. {
  167. if(BOOST_JSON_LIKELY(
  168. ! st_.empty()))
  169. return;
  170. // Reserve the largest stack we need,
  171. // to avoid reallocation during suspend.
  172. st_.reserve(
  173. sizeof(state) + // document parsing state
  174. (sizeof(state) +
  175. sizeof(std::size_t)) * depth() + // array and object state + size
  176. sizeof(state) + // value parsing state
  177. sizeof(std::size_t) + // string size
  178. sizeof(state)); // comment state
  179. }
  180. //----------------------------------------------------------
  181. //
  182. // The sentinel value is returned by parse functions
  183. // to indicate that the parser failed, or suspended.
  184. // this is used as it is distinct from all valid values
  185. // for data in write
  186. template<class Handler>
  187. const char*
  188. basic_parser<Handler>::
  189. sentinel()
  190. {
  191. // the "+1" ensures that the returned pointer is unique even if
  192. // the given input buffer borders on this object
  193. return reinterpret_cast<
  194. const char*>(this) + 1;
  195. return nullptr;
  196. }
  197. template<class Handler>
  198. bool
  199. basic_parser<Handler>::
  200. incomplete(
  201. const detail::const_stream_wrapper& cs)
  202. {
  203. return cs.begin() == sentinel();
  204. }
  205. //----------------------------------------------------------
  206. //
  207. // These functions are declared with the BOOST_NOINLINE
  208. // attribute to avoid polluting the parsers hot-path.
  209. // They return the canary value to indicate suspension
  210. // or failure.
  211. template<class Handler>
  212. const char*
  213. basic_parser<Handler>::
  214. suspend_or_fail(state st)
  215. {
  216. if(BOOST_JSON_LIKELY(
  217. ! ec_ && more_))
  218. {
  219. // suspend
  220. reserve();
  221. st_.push_unchecked(st);
  222. }
  223. return sentinel();
  224. }
  225. template<class Handler>
  226. const char*
  227. basic_parser<Handler>::
  228. suspend_or_fail(
  229. state st,
  230. std::size_t n)
  231. {
  232. if(BOOST_JSON_LIKELY(
  233. ! ec_ && more_))
  234. {
  235. // suspend
  236. reserve();
  237. st_.push_unchecked(n);
  238. st_.push_unchecked(st);
  239. }
  240. return sentinel();
  241. }
  242. template<class Handler>
  243. const char*
  244. basic_parser<Handler>::
  245. fail(const char* p) noexcept
  246. {
  247. end_ = p;
  248. return sentinel();
  249. }
  250. template<class Handler>
  251. const char*
  252. basic_parser<Handler>::
  253. fail(
  254. const char* p,
  255. error ev,
  256. source_location const* loc) noexcept
  257. {
  258. end_ = p;
  259. ec_.assign(ev, loc);
  260. return sentinel();
  261. }
  262. template<class Handler>
  263. const char*
  264. basic_parser<Handler>::
  265. maybe_suspend(
  266. const char* p,
  267. state st)
  268. {
  269. end_ = p;
  270. if(BOOST_JSON_LIKELY(more_))
  271. {
  272. // suspend
  273. reserve();
  274. st_.push_unchecked(st);
  275. }
  276. return sentinel();
  277. }
  278. template<class Handler>
  279. const char*
  280. basic_parser<Handler>::
  281. maybe_suspend(
  282. const char* p,
  283. state st,
  284. std::size_t n)
  285. {
  286. end_ = p;
  287. if(BOOST_JSON_LIKELY(more_))
  288. {
  289. // suspend
  290. reserve();
  291. st_.push_unchecked(n);
  292. st_.push_unchecked(st);
  293. }
  294. return sentinel();
  295. }
  296. template<class Handler>
  297. const char*
  298. basic_parser<Handler>::
  299. maybe_suspend(
  300. const char* p,
  301. state st,
  302. const number& num)
  303. {
  304. end_ = p;
  305. if(BOOST_JSON_LIKELY(more_))
  306. {
  307. // suspend
  308. num_ = num;
  309. reserve();
  310. st_.push_unchecked(st);;
  311. }
  312. return sentinel();
  313. }
  314. template<class Handler>
  315. const char*
  316. basic_parser<Handler>::
  317. suspend(
  318. const char* p,
  319. state st)
  320. {
  321. end_ = p;
  322. // suspend
  323. reserve();
  324. st_.push_unchecked(st);
  325. return sentinel();
  326. }
  327. template<class Handler>
  328. const char*
  329. basic_parser<Handler>::
  330. suspend(
  331. const char* p,
  332. state st,
  333. const number& num)
  334. {
  335. end_ = p;
  336. // suspend
  337. num_ = num;
  338. reserve();
  339. st_.push_unchecked(st);
  340. return sentinel();
  341. }
  342. template<class Handler>
  343. template<
  344. bool StackEmpty_/*,
  345. bool Terminal_*/>
  346. const char*
  347. basic_parser<Handler>::
  348. parse_comment(const char* p,
  349. std::integral_constant<bool, StackEmpty_> stack_empty,
  350. /*std::integral_constant<bool, Terminal_>*/ bool terminal)
  351. {
  352. detail::const_stream_wrapper cs(p, end_);
  353. const char* start = cs.begin();
  354. std::size_t remain;
  355. if(! stack_empty && ! st_.empty())
  356. {
  357. state st;
  358. st_.pop(st);
  359. switch(st)
  360. {
  361. default: BOOST_JSON_UNREACHABLE();
  362. case state::com1: goto do_com1;
  363. case state::com2: goto do_com2;
  364. case state::com3: goto do_com3;
  365. case state::com4: goto do_com4;
  366. }
  367. }
  368. BOOST_ASSERT(*cs == '/');
  369. ++cs;
  370. do_com1:
  371. if(BOOST_JSON_UNLIKELY(! cs))
  372. return maybe_suspend(cs.begin(), state::com1);
  373. switch(*cs)
  374. {
  375. default:
  376. {
  377. BOOST_STATIC_CONSTEXPR source_location loc
  378. = BOOST_CURRENT_LOCATION;
  379. return fail(cs.begin(), error::syntax, &loc);
  380. }
  381. case '/':
  382. ++cs;
  383. do_com2:
  384. // KRYSTIAN TODO: this is a mess, we have to fix this
  385. remain = cs.remain();
  386. cs = remain ? static_cast<const char*>(
  387. std::memchr(cs.begin(), '\n', remain)) : sentinel();
  388. if(! cs.begin())
  389. cs = sentinel();
  390. if(BOOST_JSON_UNLIKELY(incomplete(cs)))
  391. {
  392. // if the doc does not terminate
  393. // with a newline, treat it as the
  394. // end of the comment
  395. if(terminal && ! more_)
  396. {
  397. if(BOOST_JSON_UNLIKELY(! h_.on_comment(
  398. {start, cs.remain(start)}, ec_)))
  399. return fail(cs.end());
  400. return cs.end();
  401. }
  402. if(BOOST_JSON_UNLIKELY(! h_.on_comment_part(
  403. {start, cs.remain(start)}, ec_)))
  404. return fail(cs.end());
  405. if(terminal)
  406. return suspend(cs.end(), state::com2);
  407. return maybe_suspend(cs.end(), state::com2);
  408. }
  409. break;
  410. case '*':
  411. do
  412. {
  413. ++cs;
  414. do_com3:
  415. // KRYSTIAN TODO: this is a mess, we have to fix this
  416. remain = cs.remain();
  417. cs = remain ? static_cast<const char*>(
  418. std::memchr(cs.begin(), '*', remain)) : sentinel();
  419. if(! cs.begin())
  420. cs = sentinel();
  421. // stopped inside a c comment
  422. if(BOOST_JSON_UNLIKELY(incomplete(cs)))
  423. {
  424. if(BOOST_JSON_UNLIKELY(! h_.on_comment_part(
  425. {start, cs.remain(start)}, ec_)))
  426. return fail(cs.end());
  427. return maybe_suspend(cs.end(), state::com3);
  428. }
  429. // found a asterisk, check if the next char is a slash
  430. ++cs;
  431. do_com4:
  432. if(BOOST_JSON_UNLIKELY(! cs))
  433. {
  434. if(BOOST_JSON_UNLIKELY(! h_.on_comment_part(
  435. {start, cs.used(start)}, ec_)))
  436. return fail(cs.begin());
  437. return maybe_suspend(cs.begin(), state::com4);
  438. }
  439. }
  440. while(*cs != '/');
  441. }
  442. ++cs;
  443. if(BOOST_JSON_UNLIKELY(! h_.on_comment(
  444. {start, cs.used(start)}, ec_)))
  445. return fail(cs.begin());
  446. return cs.begin();
  447. }
  448. template<class Handler>
  449. template<bool StackEmpty_>
  450. const char*
  451. basic_parser<Handler>::
  452. parse_document(const char* p,
  453. std::integral_constant<bool, StackEmpty_> stack_empty)
  454. {
  455. detail::const_stream_wrapper cs(p, end_);
  456. if(! stack_empty && ! st_.empty())
  457. {
  458. state st;
  459. st_.pop(st);
  460. switch(st)
  461. {
  462. default: BOOST_JSON_UNREACHABLE();
  463. case state::doc1: goto do_doc1;
  464. case state::doc2: goto do_doc2;
  465. case state::doc3: goto do_doc3;
  466. case state::doc4: goto do_doc4;
  467. }
  468. }
  469. do_doc1:
  470. cs = detail::count_whitespace(cs.begin(), cs.end());
  471. if(BOOST_JSON_UNLIKELY(! cs))
  472. return maybe_suspend(cs.begin(), state::doc1);
  473. do_doc2:
  474. switch(+opt_.allow_comments |
  475. (opt_.allow_trailing_commas << 1) |
  476. (opt_.allow_invalid_utf8 << 2))
  477. {
  478. // no extensions
  479. default:
  480. cs = parse_value(cs.begin(), stack_empty, std::false_type(), std::false_type(), std::false_type());
  481. break;
  482. // comments
  483. case 1:
  484. cs = parse_value(cs.begin(), stack_empty, std::true_type(), std::false_type(), std::false_type());
  485. break;
  486. // trailing
  487. case 2:
  488. cs = parse_value(cs.begin(), stack_empty, std::false_type(), std::true_type(), std::false_type());
  489. break;
  490. // comments & trailing
  491. case 3:
  492. cs = parse_value(cs.begin(), stack_empty, std::true_type(), std::true_type(), std::false_type());
  493. break;
  494. // skip validation
  495. case 4:
  496. cs = parse_value(cs.begin(), stack_empty, std::false_type(), std::false_type(), std::true_type());
  497. break;
  498. // comments & skip validation
  499. case 5:
  500. cs = parse_value(cs.begin(), stack_empty, std::true_type(), std::false_type(), std::true_type());
  501. break;
  502. // trailing & skip validation
  503. case 6:
  504. cs = parse_value(cs.begin(), stack_empty, std::false_type(), std::true_type(), std::true_type());
  505. break;
  506. // comments & trailing & skip validation
  507. case 7:
  508. cs = parse_value(cs.begin(), stack_empty, std::true_type(), std::true_type(), std::true_type());
  509. break;
  510. }
  511. if(BOOST_JSON_UNLIKELY(incomplete(cs)))
  512. return suspend_or_fail(state::doc2);
  513. do_doc3:
  514. cs = detail::count_whitespace(cs.begin(), cs.end());
  515. if(BOOST_JSON_UNLIKELY(! cs))
  516. {
  517. if(more_)
  518. return suspend(cs.begin(), state::doc3);
  519. }
  520. else if(opt_.allow_comments && *cs == '/')
  521. {
  522. do_doc4:
  523. cs = parse_comment(cs.begin(), stack_empty, std::true_type());
  524. if(BOOST_JSON_UNLIKELY(incomplete(cs)))
  525. return suspend_or_fail(state::doc4);
  526. goto do_doc3;
  527. }
  528. return cs.begin();
  529. }
  530. template<class Handler>
  531. template<
  532. bool StackEmpty_,
  533. bool AllowComments_/*,
  534. bool AllowTrailing_,
  535. bool AllowBadUTF8_*/>
  536. const char*
  537. basic_parser<Handler>::
  538. parse_value(const char* p,
  539. std::integral_constant<bool, StackEmpty_> stack_empty,
  540. std::integral_constant<bool, AllowComments_> allow_comments,
  541. /*std::integral_constant<bool, AllowTrailing_>*/ bool allow_trailing,
  542. /*std::integral_constant<bool, AllowBadUTF8_>*/ bool allow_bad_utf8)
  543. {
  544. if(stack_empty || st_.empty())
  545. {
  546. loop:
  547. switch(*p)
  548. {
  549. case '0':
  550. return parse_number(p, std::true_type(), std::integral_constant<char, '0'>());
  551. case '-':
  552. return parse_number(p, std::true_type(), std::integral_constant<char, '-'>());
  553. case '1': case '2': case '3':
  554. case '4': case '5': case '6':
  555. case '7': case '8': case '9':
  556. return parse_number(p, std::true_type(), std::integral_constant<char, '+'>());
  557. case 't':
  558. return parse_true(p, std::true_type());
  559. case 'f':
  560. return parse_false(p, std::true_type());
  561. case 'n':
  562. return parse_null(p, std::true_type());
  563. case '"':
  564. return parse_unescaped(p, std::true_type(), std::false_type(), allow_bad_utf8);
  565. case '[':
  566. return parse_array(p, std::true_type(), allow_comments, allow_trailing, allow_bad_utf8);
  567. case '{':
  568. return parse_object(p, std::true_type(), allow_comments, allow_trailing, allow_bad_utf8);
  569. case '/':
  570. if(! allow_comments)
  571. {
  572. BOOST_STATIC_CONSTEXPR source_location loc
  573. = BOOST_CURRENT_LOCATION;
  574. return fail(p, error::syntax, &loc);
  575. }
  576. p = parse_comment(p, stack_empty, std::false_type());
  577. // KRYSTIAN NOTE: incomplete takes const_stream, we either
  578. // can add an overload, change the existing one to take a pointer,
  579. // or just leave it as is
  580. if(BOOST_JSON_UNLIKELY(p == sentinel()))
  581. return maybe_suspend(p, state::val2);
  582. // intentional fallthrough
  583. case ' ':
  584. case '\t':
  585. case '\n':
  586. case '\r':
  587. p = detail::count_whitespace(p, end_);
  588. if(BOOST_JSON_UNLIKELY(p == end_))
  589. return maybe_suspend(p, state::val1);
  590. goto loop;
  591. default:
  592. {
  593. BOOST_STATIC_CONSTEXPR source_location loc
  594. = BOOST_CURRENT_LOCATION;
  595. return fail(p, error::syntax, &loc);
  596. }
  597. }
  598. }
  599. return resume_value(p, stack_empty, allow_comments, allow_trailing, allow_bad_utf8);
  600. }
  601. template<class Handler>
  602. template<
  603. bool StackEmpty_,
  604. bool AllowComments_/*,
  605. bool AllowTrailing_,
  606. bool AllowBadUTF8_*/>
  607. const char*
  608. basic_parser<Handler>::
  609. resume_value(const char* p,
  610. std::integral_constant<bool, StackEmpty_> stack_empty,
  611. std::integral_constant<bool, AllowComments_> allow_comments,
  612. /*std::integral_constant<bool, AllowTrailing_>*/ bool allow_trailing,
  613. /*std::integral_constant<bool, AllowBadUTF8_>*/ bool allow_bad_utf8)
  614. {
  615. state st;
  616. st_.peek(st);
  617. switch(st)
  618. {
  619. default: BOOST_JSON_UNREACHABLE();
  620. case state::nul1: case state::nul2:
  621. case state::nul3:
  622. return parse_null(p, stack_empty);
  623. case state::tru1: case state::tru2:
  624. case state::tru3:
  625. return parse_true(p, stack_empty);
  626. case state::fal1: case state::fal2:
  627. case state::fal3: case state::fal4:
  628. return parse_false(p, stack_empty);
  629. case state::str1:
  630. return parse_unescaped(p, stack_empty, std::false_type(), allow_bad_utf8);
  631. case state::str2: case state::str3:
  632. case state::str4: case state::str5:
  633. case state::str6: case state::str7:
  634. case state::str8:
  635. case state::sur1: case state::sur2:
  636. case state::sur3: case state::sur4:
  637. case state::sur5: case state::sur6:
  638. return parse_escaped(p, 0, stack_empty, std::false_type(), allow_bad_utf8);
  639. case state::arr1: case state::arr2:
  640. case state::arr3: case state::arr4:
  641. case state::arr5: case state::arr6:
  642. return parse_array(p, stack_empty, allow_comments, allow_trailing, allow_bad_utf8);
  643. case state::obj1: case state::obj2:
  644. case state::obj3: case state::obj4:
  645. case state::obj5: case state::obj6:
  646. case state::obj7: case state::obj8:
  647. case state::obj9: case state::obj10:
  648. case state::obj11:
  649. return parse_object(p, stack_empty, allow_comments, allow_trailing, allow_bad_utf8);
  650. case state::num1: case state::num2:
  651. case state::num3: case state::num4:
  652. case state::num5: case state::num6:
  653. case state::num7: case state::num8:
  654. case state::exp1: case state::exp2:
  655. case state::exp3:
  656. return parse_number(p, stack_empty, std::integral_constant<char, 0>());
  657. case state::com1: case state::com2:
  658. case state::com3: case state::com4:
  659. return parse_comment(p, stack_empty, std::false_type());
  660. // KRYSTIAN NOTE: these are special cases
  661. case state::val1:
  662. {
  663. st_.pop(st);
  664. BOOST_ASSERT(st_.empty());
  665. p = detail::count_whitespace(p, end_);
  666. if(BOOST_JSON_UNLIKELY(p == end_))
  667. return maybe_suspend(p, state::val1);
  668. return parse_value(p, std::true_type(), allow_comments, allow_trailing, allow_bad_utf8);
  669. }
  670. case state::val2:
  671. {
  672. st_.pop(st);
  673. p = parse_comment(p, stack_empty, std::false_type());
  674. if(BOOST_JSON_UNLIKELY(p == sentinel()))
  675. return maybe_suspend(p, state::val2);
  676. BOOST_ASSERT(st_.empty());
  677. return parse_value(p, std::true_type(), std::true_type(), allow_trailing, allow_bad_utf8);
  678. }
  679. }
  680. }
  681. template<class Handler>
  682. template<bool StackEmpty_>
  683. const char*
  684. basic_parser<Handler>::
  685. parse_null(const char* p,
  686. std::integral_constant<bool, StackEmpty_> stack_empty)
  687. {
  688. detail::const_stream_wrapper cs(p, end_);
  689. if(stack_empty || st_.empty())
  690. {
  691. if(BOOST_JSON_LIKELY(cs.remain() >= 4))
  692. {
  693. if(BOOST_JSON_UNLIKELY(
  694. std::memcmp(cs.begin(), "null", 4) != 0))
  695. {
  696. BOOST_STATIC_CONSTEXPR source_location loc
  697. = BOOST_CURRENT_LOCATION;
  698. return fail(cs.begin(), error::syntax, &loc);
  699. }
  700. if(BOOST_JSON_UNLIKELY(
  701. ! h_.on_null(ec_)))
  702. return fail(cs.begin());
  703. cs += 4;
  704. return cs.begin();
  705. }
  706. }
  707. else
  708. {
  709. state st;
  710. st_.pop(st);
  711. switch(st)
  712. {
  713. default: BOOST_JSON_UNREACHABLE();
  714. case state::nul1: goto do_nul1;
  715. case state::nul2: goto do_nul2;
  716. case state::nul3: goto do_nul3;
  717. }
  718. }
  719. ++cs;
  720. do_nul1:
  721. if(BOOST_JSON_UNLIKELY(! cs))
  722. return maybe_suspend(cs.begin(), state::nul1);
  723. if(BOOST_JSON_UNLIKELY(*cs != 'u'))
  724. {
  725. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  726. return fail(cs.begin(), error::syntax, &loc);
  727. }
  728. ++cs;
  729. do_nul2:
  730. if(BOOST_JSON_UNLIKELY(! cs))
  731. return maybe_suspend(cs.begin(), state::nul2);
  732. if(BOOST_JSON_UNLIKELY(*cs != 'l'))
  733. {
  734. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  735. return fail(cs.begin(), error::syntax, &loc);
  736. }
  737. ++cs;
  738. do_nul3:
  739. if(BOOST_JSON_UNLIKELY(! cs))
  740. return maybe_suspend(cs.begin(), state::nul3);
  741. if(BOOST_JSON_UNLIKELY(*cs != 'l'))
  742. {
  743. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  744. return fail(cs.begin(), error::syntax, &loc);
  745. }
  746. if(BOOST_JSON_UNLIKELY(
  747. ! h_.on_null(ec_)))
  748. return fail(cs.begin());
  749. ++cs;
  750. return cs.begin();
  751. }
  752. template<class Handler>
  753. template<bool StackEmpty_>
  754. const char*
  755. basic_parser<Handler>::
  756. parse_true(const char* p,
  757. std::integral_constant<bool, StackEmpty_> stack_empty)
  758. {
  759. detail::const_stream_wrapper cs(p, end_);
  760. if(stack_empty || st_.empty())
  761. {
  762. if(BOOST_JSON_LIKELY(cs.remain() >= 4))
  763. {
  764. if(BOOST_JSON_UNLIKELY(
  765. std::memcmp(cs.begin(), "true", 4) != 0))
  766. {
  767. BOOST_STATIC_CONSTEXPR source_location loc
  768. = BOOST_CURRENT_LOCATION;
  769. return fail(cs.begin(), error::syntax, &loc);
  770. }
  771. if(BOOST_JSON_UNLIKELY(
  772. ! h_.on_bool(true, ec_)))
  773. return fail(cs.begin());
  774. cs += 4;
  775. return cs.begin();
  776. }
  777. }
  778. else
  779. {
  780. state st;
  781. st_.pop(st);
  782. switch(st)
  783. {
  784. default: BOOST_JSON_UNREACHABLE();
  785. case state::tru1: goto do_tru1;
  786. case state::tru2: goto do_tru2;
  787. case state::tru3: goto do_tru3;
  788. }
  789. }
  790. ++cs;
  791. do_tru1:
  792. if(BOOST_JSON_UNLIKELY(! cs))
  793. return maybe_suspend(cs.begin(), state::tru1);
  794. if(BOOST_JSON_UNLIKELY(*cs != 'r'))
  795. {
  796. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  797. return fail(cs.begin(), error::syntax, &loc);
  798. }
  799. ++cs;
  800. do_tru2:
  801. if(BOOST_JSON_UNLIKELY(! cs))
  802. return maybe_suspend(cs.begin(), state::tru2);
  803. if(BOOST_JSON_UNLIKELY(*cs != 'u'))
  804. {
  805. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  806. return fail(cs.begin(), error::syntax, &loc);
  807. }
  808. ++cs;
  809. do_tru3:
  810. if(BOOST_JSON_UNLIKELY(! cs))
  811. return maybe_suspend(cs.begin(), state::tru3);
  812. if(BOOST_JSON_UNLIKELY(*cs != 'e'))
  813. {
  814. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  815. return fail(cs.begin(), error::syntax, &loc);
  816. }
  817. if(BOOST_JSON_UNLIKELY(
  818. ! h_.on_bool(true, ec_)))
  819. return fail(cs.begin());
  820. ++cs;
  821. return cs.begin();
  822. }
  823. template<class Handler>
  824. template<bool StackEmpty_>
  825. const char*
  826. basic_parser<Handler>::
  827. parse_false(const char* p,
  828. std::integral_constant<bool, StackEmpty_> stack_empty)
  829. {
  830. detail::const_stream_wrapper cs(p, end_);
  831. if(stack_empty || st_.empty())
  832. {
  833. if(BOOST_JSON_LIKELY(cs.remain() >= 5))
  834. {
  835. if(BOOST_JSON_UNLIKELY(
  836. std::memcmp(cs.begin() + 1, "alse", 4) != 0))
  837. {
  838. BOOST_STATIC_CONSTEXPR source_location loc
  839. = BOOST_CURRENT_LOCATION;
  840. return fail(cs.begin(), error::syntax, &loc);
  841. }
  842. if(BOOST_JSON_UNLIKELY(
  843. ! h_.on_bool(false, ec_)))
  844. return fail(cs.begin());
  845. cs += 5;
  846. return cs.begin();
  847. }
  848. }
  849. else
  850. {
  851. state st;
  852. st_.pop(st);
  853. switch(st)
  854. {
  855. default: BOOST_JSON_UNREACHABLE();
  856. case state::fal1: goto do_fal1;
  857. case state::fal2: goto do_fal2;
  858. case state::fal3: goto do_fal3;
  859. case state::fal4: goto do_fal4;
  860. }
  861. }
  862. ++cs;
  863. do_fal1:
  864. if(BOOST_JSON_UNLIKELY(! cs))
  865. return maybe_suspend(cs.begin(), state::fal1);
  866. if(BOOST_JSON_UNLIKELY(*cs != 'a'))
  867. {
  868. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  869. return fail(cs.begin(), error::syntax, &loc);
  870. }
  871. ++cs;
  872. do_fal2:
  873. if(BOOST_JSON_UNLIKELY(! cs))
  874. return maybe_suspend(cs.begin(), state::fal2);
  875. if(BOOST_JSON_UNLIKELY(*cs != 'l'))
  876. {
  877. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  878. return fail(cs.begin(), error::syntax, &loc);
  879. }
  880. ++cs;
  881. do_fal3:
  882. if(BOOST_JSON_UNLIKELY(! cs))
  883. return maybe_suspend(cs.begin(), state::fal3);
  884. if(BOOST_JSON_UNLIKELY(*cs != 's'))
  885. {
  886. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  887. return fail(cs.begin(), error::syntax, &loc);
  888. }
  889. ++cs;
  890. do_fal4:
  891. if(BOOST_JSON_UNLIKELY(! cs))
  892. return maybe_suspend(cs.begin(), state::fal4);
  893. if(BOOST_JSON_UNLIKELY(*cs != 'e'))
  894. {
  895. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  896. return fail(cs.begin(), error::syntax, &loc);
  897. }
  898. if(BOOST_JSON_UNLIKELY(
  899. ! h_.on_bool(false, ec_)))
  900. return fail(cs.begin());
  901. ++cs;
  902. return cs.begin();
  903. }
  904. //----------------------------------------------------------
  905. template<class Handler>
  906. template<
  907. bool StackEmpty_,
  908. bool IsKey_/*,
  909. bool AllowBadUTF8_*/>
  910. const char*
  911. basic_parser<Handler>::
  912. parse_string(const char* p,
  913. std::integral_constant<bool, StackEmpty_> stack_empty,
  914. std::integral_constant<bool, IsKey_> is_key,
  915. /*std::integral_constant<bool, AllowBadUTF8_>*/ bool allow_bad_utf8)
  916. {
  917. if(! stack_empty && ! st_.empty())
  918. {
  919. state st;
  920. st_.peek(st);
  921. switch(st)
  922. {
  923. default: BOOST_JSON_UNREACHABLE();
  924. case state::str1:
  925. return parse_unescaped(p, stack_empty, is_key, allow_bad_utf8);
  926. case state::str2: case state::str3:
  927. case state::str4: case state::str5:
  928. case state::str6: case state::str7:
  929. case state::str8:
  930. case state::sur1: case state::sur2:
  931. case state::sur3: case state::sur4:
  932. case state::sur5: case state::sur6:
  933. return parse_escaped(p, 0, stack_empty, is_key, allow_bad_utf8);
  934. }
  935. }
  936. return parse_unescaped(p, std::true_type(), is_key, allow_bad_utf8);
  937. }
  938. template<class Handler>
  939. template<
  940. bool StackEmpty_,
  941. bool IsKey_/*,
  942. bool AllowBadUTF8_*/>
  943. const char*
  944. basic_parser<Handler>::
  945. parse_unescaped(const char* p,
  946. std::integral_constant<bool, StackEmpty_> stack_empty,
  947. std::integral_constant<bool, IsKey_> is_key,
  948. /*std::integral_constant<bool, AllowBadUTF8_>*/ bool allow_bad_utf8)
  949. {
  950. detail::const_stream_wrapper cs(p, end_);
  951. std::size_t total;
  952. if(stack_empty || st_.empty())
  953. {
  954. BOOST_ASSERT(*cs == '\x22'); // '"'
  955. ++cs;
  956. total = 0;
  957. }
  958. else
  959. {
  960. state st;
  961. st_.pop(st);
  962. st_.pop(total);
  963. }
  964. char const* start = cs.begin();
  965. cs = allow_bad_utf8?
  966. detail::count_valid<true>(cs.begin(), cs.end()):
  967. detail::count_valid<false>(cs.begin(), cs.end());
  968. std::size_t size = cs.used(start);
  969. if(is_key)
  970. {
  971. BOOST_ASSERT(total <= Handler::max_key_size);
  972. if(BOOST_JSON_UNLIKELY(size >
  973. Handler::max_key_size - total))
  974. {
  975. BOOST_STATIC_CONSTEXPR source_location loc
  976. = BOOST_CURRENT_LOCATION;
  977. return fail(cs.begin(), error::key_too_large, &loc);
  978. }
  979. }
  980. else
  981. {
  982. BOOST_ASSERT(total <= Handler::max_string_size);
  983. if(BOOST_JSON_UNLIKELY(size >
  984. Handler::max_string_size - total))
  985. {
  986. BOOST_STATIC_CONSTEXPR source_location loc
  987. = BOOST_CURRENT_LOCATION;
  988. return fail(cs.begin(), error::string_too_large, &loc);
  989. }
  990. }
  991. total += size;
  992. if(BOOST_JSON_UNLIKELY(! cs))
  993. {
  994. // call handler if the string isn't empty
  995. if(BOOST_JSON_LIKELY(size))
  996. {
  997. {
  998. bool r = is_key?
  999. h_.on_key_part( {start, size}, total, ec_ ):
  1000. h_.on_string_part( {start, size}, total, ec_ );
  1001. if(BOOST_JSON_UNLIKELY(!r))
  1002. {
  1003. return fail(cs.begin());
  1004. }
  1005. }
  1006. }
  1007. return maybe_suspend(cs.begin(), state::str1, total);
  1008. }
  1009. // at this point all valid characters have been skipped, so any remaining
  1010. // if there are any more characters, they are either escaped, or incomplete
  1011. // utf8, or invalid utf8
  1012. if(BOOST_JSON_UNLIKELY(*cs != '\x22')) // '"'
  1013. {
  1014. // sequence is invalid or incomplete
  1015. if((*cs & 0x80) && !allow_bad_utf8)
  1016. {
  1017. seq_.save(cs.begin(), cs.remain());
  1018. if(BOOST_JSON_UNLIKELY(seq_.complete()))
  1019. {
  1020. BOOST_STATIC_CONSTEXPR source_location loc
  1021. = BOOST_CURRENT_LOCATION;
  1022. return fail(cs.begin(), error::syntax, &loc);
  1023. }
  1024. if(BOOST_JSON_LIKELY(size))
  1025. {
  1026. {
  1027. bool r = is_key?
  1028. h_.on_key_part( {start, size}, total, ec_ ):
  1029. h_.on_string_part( {start, size}, total, ec_ );
  1030. if(BOOST_JSON_UNLIKELY(!r))
  1031. {
  1032. return fail(cs.begin());
  1033. }
  1034. }
  1035. }
  1036. return maybe_suspend(cs.end(), state::str8, total);
  1037. }
  1038. else if(BOOST_JSON_LIKELY(*cs == '\\'))
  1039. {
  1040. // flush unescaped run from input
  1041. if(BOOST_JSON_LIKELY(size))
  1042. {
  1043. {
  1044. bool r = is_key?
  1045. h_.on_key_part( {start, size}, total, ec_ ):
  1046. h_.on_string_part( {start, size}, total, ec_ );
  1047. if(BOOST_JSON_UNLIKELY(!r))
  1048. {
  1049. return fail(cs.begin());
  1050. }
  1051. }
  1052. }
  1053. return parse_escaped(cs.begin(), total, stack_empty, is_key, allow_bad_utf8);
  1054. }
  1055. // illegal control
  1056. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  1057. return fail(cs.begin(), error::syntax, &loc);
  1058. }
  1059. {
  1060. bool r = is_key?
  1061. h_.on_key( {start, size}, total, ec_ ):
  1062. h_.on_string( {start, size}, total, ec_ );
  1063. if(BOOST_JSON_UNLIKELY(!r))
  1064. {
  1065. return fail(cs.begin());
  1066. }
  1067. }
  1068. ++cs;
  1069. return cs.begin();
  1070. }
  1071. template<class Handler>
  1072. template<
  1073. bool StackEmpty_/*,
  1074. bool IsKey_,
  1075. bool AllowBadUTF8_*/>
  1076. const char*
  1077. basic_parser<Handler>::
  1078. parse_escaped(
  1079. const char* p,
  1080. std::size_t total,
  1081. std::integral_constant<bool, StackEmpty_> stack_empty,
  1082. /*std::integral_constant<bool, IsKey_>*/ bool is_key,
  1083. /*std::integral_constant<bool, AllowBadUTF8_>*/ bool allow_bad_utf8)
  1084. {
  1085. //---------------------------------------------------------------
  1086. //
  1087. // To handle escapes, a local temporary buffer accumulates
  1088. // the unescaped result. The algorithm attempts to fill the
  1089. // buffer to capacity before invoking the handler.
  1090. // In some cases the temporary buffer needs to be flushed
  1091. // before it is full:
  1092. // * When the closing double quote is seen
  1093. // * When there in no more input (and more is expected later)
  1094. // A goal of the algorithm is to call the handler as few times
  1095. // as possible. Thus, when the first escape is encountered,
  1096. // the algorithm attempts to fill the temporary buffer first.
  1097. //
  1098. auto const ev_too_large = is_key?
  1099. error::key_too_large : error::string_too_large;
  1100. auto const max_size = is_key?
  1101. Handler::max_key_size : Handler::max_string_size;
  1102. detail::clipped_const_stream cs(p, end_);
  1103. detail::buffer<BOOST_JSON_STACK_BUFFER_SIZE> temp;
  1104. int digit;
  1105. char c;
  1106. cs.clip(temp.max_size());
  1107. if(! stack_empty && ! st_.empty())
  1108. {
  1109. state st;
  1110. st_.pop(st);
  1111. st_.pop(total);
  1112. switch(st)
  1113. {
  1114. default: BOOST_JSON_UNREACHABLE();
  1115. case state::str2: goto do_str2;
  1116. case state::str3: goto do_str3;
  1117. case state::str4: goto do_str4;
  1118. case state::str5: goto do_str5;
  1119. case state::str6: goto do_str6;
  1120. case state::str7: goto do_str7;
  1121. case state::str8: goto do_str8;
  1122. case state::sur1: goto do_sur1;
  1123. case state::sur2: goto do_sur2;
  1124. case state::sur3: goto do_sur3;
  1125. case state::sur4: goto do_sur4;
  1126. case state::sur5: goto do_sur5;
  1127. case state::sur6: goto do_sur6;
  1128. }
  1129. }
  1130. // Unescaped JSON is never larger than its escaped version.
  1131. // To efficiently process only what will fit in the temporary buffer,
  1132. // the size of the input stream is temporarily "clipped" to the size
  1133. // of the temporary buffer.
  1134. // handle escaped character
  1135. BOOST_ASSERT(*cs == '\\');
  1136. ++cs;
  1137. do_str3:
  1138. if(BOOST_JSON_UNLIKELY(! cs))
  1139. {
  1140. if(BOOST_JSON_LIKELY(! temp.empty()))
  1141. {
  1142. BOOST_ASSERT(total <= max_size);
  1143. if(BOOST_JSON_UNLIKELY(
  1144. temp.size() > max_size - total))
  1145. {
  1146. BOOST_STATIC_CONSTEXPR source_location loc
  1147. = BOOST_CURRENT_LOCATION;
  1148. return fail(cs.begin(), ev_too_large, &loc);
  1149. }
  1150. total += temp.size();
  1151. {
  1152. bool r = is_key
  1153. ? h_.on_key_part(temp.get(), total, ec_)
  1154. : h_.on_string_part(temp.get(), total, ec_);
  1155. if(BOOST_JSON_UNLIKELY(!r))
  1156. {
  1157. return fail(cs.begin());
  1158. }
  1159. }
  1160. temp.clear();
  1161. }
  1162. cs.clip(temp.max_size());
  1163. if(BOOST_JSON_UNLIKELY(! cs))
  1164. return maybe_suspend(cs.begin(), state::str3, total);
  1165. }
  1166. switch(*cs)
  1167. {
  1168. default:
  1169. {
  1170. BOOST_STATIC_CONSTEXPR source_location loc
  1171. = BOOST_CURRENT_LOCATION;
  1172. return fail(cs.begin(), error::syntax, &loc);
  1173. }
  1174. case '\x22': // '"'
  1175. temp.push_back('\x22');
  1176. ++cs;
  1177. break;
  1178. case '\\':
  1179. temp.push_back('\\');
  1180. ++cs;
  1181. break;
  1182. case '/':
  1183. temp.push_back('/');
  1184. ++cs;
  1185. break;
  1186. case 'b':
  1187. temp.push_back('\x08');
  1188. ++cs;
  1189. break;
  1190. case 'f':
  1191. temp.push_back('\x0c');
  1192. ++cs;
  1193. break;
  1194. case 'n':
  1195. temp.push_back('\x0a');
  1196. ++cs;
  1197. break;
  1198. case 'r':
  1199. temp.push_back('\x0d');
  1200. ++cs;
  1201. break;
  1202. case 't':
  1203. temp.push_back('\x09');
  1204. ++cs;
  1205. break;
  1206. case 'u':
  1207. // utf16 escape
  1208. //
  1209. // fast path only when the buffer
  1210. // is large enough for 2 surrogates
  1211. if(BOOST_JSON_LIKELY(cs.remain() > 10))
  1212. {
  1213. // KRYSTIAN TODO: this could be done
  1214. // with fewer instructions
  1215. digit = detail::load_little_endian<4>(
  1216. cs.begin() + 1);
  1217. int d4 = detail::hex_digit(static_cast<
  1218. unsigned char>(digit >> 24));
  1219. int d3 = detail::hex_digit(static_cast<
  1220. unsigned char>(digit >> 16));
  1221. int d2 = detail::hex_digit(static_cast<
  1222. unsigned char>(digit >> 8));
  1223. int d1 = detail::hex_digit(static_cast<
  1224. unsigned char>(digit));
  1225. if(BOOST_JSON_UNLIKELY(
  1226. (d1 | d2 | d3 | d4) == -1))
  1227. {
  1228. if(d1 != -1)
  1229. ++cs;
  1230. if(d2 != -1)
  1231. ++cs;
  1232. if(d3 != -1)
  1233. ++cs;
  1234. BOOST_STATIC_CONSTEXPR source_location loc
  1235. = BOOST_CURRENT_LOCATION;
  1236. return fail(cs.begin(), error::expected_hex_digit, &loc);
  1237. }
  1238. // 32 bit unicode scalar value
  1239. unsigned const u1 =
  1240. (d1 << 12) + (d2 << 8) +
  1241. (d3 << 4) + d4;
  1242. // valid unicode scalar values are
  1243. // [0, D7FF] and [E000, 10FFFF]
  1244. // values within this range are valid utf-8
  1245. // code points and invalid leading surrogates.
  1246. if(BOOST_JSON_LIKELY(
  1247. u1 < 0xd800 || u1 > 0xdfff))
  1248. {
  1249. cs += 5;
  1250. temp.append_utf8(u1);
  1251. break;
  1252. }
  1253. if(BOOST_JSON_UNLIKELY(u1 > 0xdbff))
  1254. {
  1255. BOOST_STATIC_CONSTEXPR source_location loc
  1256. = BOOST_CURRENT_LOCATION;
  1257. return fail(cs.begin(), error::illegal_leading_surrogate,
  1258. &loc);
  1259. }
  1260. cs += 5;
  1261. // KRYSTIAN TODO: this can be a two byte load
  1262. // and a single comparison. We lose error information,
  1263. // but it's faster.
  1264. if(BOOST_JSON_UNLIKELY(*cs != '\\'))
  1265. {
  1266. BOOST_STATIC_CONSTEXPR source_location loc
  1267. = BOOST_CURRENT_LOCATION;
  1268. return fail(cs.begin(), error::syntax, &loc);
  1269. }
  1270. ++cs;
  1271. if(BOOST_JSON_UNLIKELY(*cs != 'u'))
  1272. {
  1273. BOOST_STATIC_CONSTEXPR source_location loc
  1274. = BOOST_CURRENT_LOCATION;
  1275. return fail(cs.begin(), error::syntax, &loc);
  1276. }
  1277. ++cs;
  1278. digit = detail::load_little_endian<4>(cs.begin());
  1279. d4 = detail::hex_digit(static_cast<
  1280. unsigned char>(digit >> 24));
  1281. d3 = detail::hex_digit(static_cast<
  1282. unsigned char>(digit >> 16));
  1283. d2 = detail::hex_digit(static_cast<
  1284. unsigned char>(digit >> 8));
  1285. d1 = detail::hex_digit(static_cast<
  1286. unsigned char>(digit));
  1287. if(BOOST_JSON_UNLIKELY(
  1288. (d1 | d2 | d3 | d4) == -1))
  1289. {
  1290. if(d1 != -1)
  1291. ++cs;
  1292. if(d2 != -1)
  1293. ++cs;
  1294. if(d3 != -1)
  1295. ++cs;
  1296. BOOST_STATIC_CONSTEXPR source_location loc
  1297. = BOOST_CURRENT_LOCATION;
  1298. return fail(cs.begin(), error::expected_hex_digit, &loc);
  1299. }
  1300. unsigned const u2 =
  1301. (d1 << 12) + (d2 << 8) +
  1302. (d3 << 4) + d4;
  1303. // valid trailing surrogates are [DC00, DFFF]
  1304. if(BOOST_JSON_UNLIKELY(
  1305. u2 < 0xdc00 || u2 > 0xdfff))
  1306. {
  1307. BOOST_STATIC_CONSTEXPR source_location loc
  1308. = BOOST_CURRENT_LOCATION;
  1309. return fail(cs.begin(), error::illegal_trailing_surrogate,
  1310. &loc);
  1311. }
  1312. cs += 4;
  1313. unsigned cp =
  1314. ((u1 - 0xd800) << 10) +
  1315. ((u2 - 0xdc00)) +
  1316. 0x10000;
  1317. // utf-16 surrogate pair
  1318. temp.append_utf8(cp);
  1319. break;
  1320. }
  1321. // flush
  1322. if(BOOST_JSON_LIKELY(! temp.empty()))
  1323. {
  1324. BOOST_ASSERT(total <= max_size);
  1325. if(BOOST_JSON_UNLIKELY(
  1326. temp.size() > max_size - total))
  1327. {
  1328. BOOST_STATIC_CONSTEXPR source_location loc
  1329. = BOOST_CURRENT_LOCATION;
  1330. return fail(cs.begin(), ev_too_large, &loc);
  1331. }
  1332. total += temp.size();
  1333. {
  1334. bool r = is_key
  1335. ? h_.on_key_part(temp.get(), total, ec_)
  1336. : h_.on_string_part(temp.get(), total, ec_);
  1337. if(BOOST_JSON_UNLIKELY(!r))
  1338. {
  1339. return fail(cs.begin());
  1340. }
  1341. }
  1342. temp.clear();
  1343. cs.clip(temp.max_size());
  1344. }
  1345. ++cs;
  1346. // utf-16 escape
  1347. do_str4:
  1348. if(BOOST_JSON_UNLIKELY(! cs))
  1349. return maybe_suspend(cs.begin(), state::str4, total);
  1350. digit = detail::hex_digit(*cs);
  1351. if(BOOST_JSON_UNLIKELY(digit == -1))
  1352. {
  1353. BOOST_STATIC_CONSTEXPR source_location loc
  1354. = BOOST_CURRENT_LOCATION;
  1355. return fail(cs.begin(), error::expected_hex_digit, &loc);
  1356. }
  1357. ++cs;
  1358. u1_ = digit << 12;
  1359. do_str5:
  1360. if(BOOST_JSON_UNLIKELY(! cs))
  1361. return maybe_suspend(cs.begin(), state::str5, total);
  1362. digit = detail::hex_digit(*cs);
  1363. if(BOOST_JSON_UNLIKELY(digit == -1))
  1364. {
  1365. BOOST_STATIC_CONSTEXPR source_location loc
  1366. = BOOST_CURRENT_LOCATION;
  1367. return fail(cs.begin(), error::expected_hex_digit, &loc);
  1368. }
  1369. ++cs;
  1370. u1_ += digit << 8;
  1371. do_str6:
  1372. if(BOOST_JSON_UNLIKELY(! cs))
  1373. return maybe_suspend(cs.begin(), state::str6, total);
  1374. digit = detail::hex_digit(*cs);
  1375. if(BOOST_JSON_UNLIKELY(digit == -1))
  1376. {
  1377. BOOST_STATIC_CONSTEXPR source_location loc
  1378. = BOOST_CURRENT_LOCATION;
  1379. return fail(cs.begin(), error::expected_hex_digit, &loc);
  1380. }
  1381. ++cs;
  1382. u1_ += digit << 4;
  1383. do_str7:
  1384. if(BOOST_JSON_UNLIKELY(! cs))
  1385. return maybe_suspend(cs.begin(), state::str7, total);
  1386. digit = detail::hex_digit(*cs);
  1387. if(BOOST_JSON_UNLIKELY(digit == -1))
  1388. {
  1389. BOOST_STATIC_CONSTEXPR source_location loc
  1390. = BOOST_CURRENT_LOCATION;
  1391. return fail(cs.begin(), error::expected_hex_digit, &loc);
  1392. }
  1393. ++cs;
  1394. u1_ += digit;
  1395. if(BOOST_JSON_LIKELY(
  1396. u1_ < 0xd800 || u1_ > 0xdfff))
  1397. {
  1398. BOOST_ASSERT(temp.empty());
  1399. // utf-8 codepoint
  1400. temp.append_utf8(u1_);
  1401. break;
  1402. }
  1403. if(BOOST_JSON_UNLIKELY(u1_ > 0xdbff))
  1404. {
  1405. BOOST_STATIC_CONSTEXPR source_location loc
  1406. = BOOST_CURRENT_LOCATION;
  1407. return fail(cs.begin(), error::illegal_trailing_surrogate, &loc);
  1408. }
  1409. do_sur1:
  1410. if(BOOST_JSON_UNLIKELY(! cs))
  1411. return maybe_suspend(cs.begin(), state::sur1, total);
  1412. if(BOOST_JSON_UNLIKELY(*cs != '\\'))
  1413. {
  1414. BOOST_STATIC_CONSTEXPR source_location loc
  1415. = BOOST_CURRENT_LOCATION;
  1416. return fail(cs.begin(), error::syntax, &loc);
  1417. }
  1418. ++cs;
  1419. do_sur2:
  1420. if(BOOST_JSON_UNLIKELY(! cs))
  1421. return maybe_suspend(cs.begin(), state::sur2, total);
  1422. if(BOOST_JSON_UNLIKELY(*cs != 'u'))
  1423. {
  1424. BOOST_STATIC_CONSTEXPR source_location loc
  1425. = BOOST_CURRENT_LOCATION;
  1426. return fail(cs.begin(), error::syntax, &loc);
  1427. }
  1428. ++cs;
  1429. do_sur3:
  1430. if(BOOST_JSON_UNLIKELY(! cs))
  1431. return maybe_suspend(cs.begin(), state::sur3, total);
  1432. digit = detail::hex_digit(*cs);
  1433. if(BOOST_JSON_UNLIKELY(digit == -1))
  1434. {
  1435. BOOST_STATIC_CONSTEXPR source_location loc
  1436. = BOOST_CURRENT_LOCATION;
  1437. return fail(cs.begin(), error::expected_hex_digit, &loc);
  1438. }
  1439. ++cs;
  1440. u2_ = digit << 12;
  1441. do_sur4:
  1442. if(BOOST_JSON_UNLIKELY(! cs))
  1443. return maybe_suspend(cs.begin(), state::sur4, total);
  1444. digit = detail::hex_digit(*cs);
  1445. if(BOOST_JSON_UNLIKELY(digit == -1))
  1446. {
  1447. BOOST_STATIC_CONSTEXPR source_location loc
  1448. = BOOST_CURRENT_LOCATION;
  1449. return fail(cs.begin(), error::expected_hex_digit, &loc);
  1450. }
  1451. ++cs;
  1452. u2_ += digit << 8;
  1453. do_sur5:
  1454. if(BOOST_JSON_UNLIKELY(! cs))
  1455. return maybe_suspend(cs.begin(), state::sur5, total);
  1456. digit = detail::hex_digit(*cs);
  1457. if(BOOST_JSON_UNLIKELY(digit == -1))
  1458. {
  1459. BOOST_STATIC_CONSTEXPR source_location loc
  1460. = BOOST_CURRENT_LOCATION;
  1461. return fail(cs.begin(), error::expected_hex_digit, &loc);
  1462. }
  1463. ++cs;
  1464. u2_ += digit << 4;
  1465. do_sur6:
  1466. if(BOOST_JSON_UNLIKELY(! cs))
  1467. return maybe_suspend(cs.begin(), state::sur6, total);
  1468. digit = detail::hex_digit(*cs);
  1469. if(BOOST_JSON_UNLIKELY(digit == -1))
  1470. {
  1471. BOOST_STATIC_CONSTEXPR source_location loc
  1472. = BOOST_CURRENT_LOCATION;
  1473. return fail(cs.begin(), error::expected_hex_digit, &loc);
  1474. }
  1475. ++cs;
  1476. u2_ += digit;
  1477. if(BOOST_JSON_UNLIKELY(
  1478. u2_ < 0xdc00 || u2_ > 0xdfff))
  1479. {
  1480. BOOST_STATIC_CONSTEXPR source_location loc
  1481. = BOOST_CURRENT_LOCATION;
  1482. return fail(cs.begin(), error::expected_hex_digit, &loc);
  1483. }
  1484. unsigned cp =
  1485. ((u1_ - 0xd800) << 10) +
  1486. ((u2_ - 0xdc00)) +
  1487. 0x10000;
  1488. BOOST_ASSERT(temp.empty());
  1489. // utf-16 surrogate pair
  1490. temp.append_utf8(cp);
  1491. }
  1492. do_str2:
  1493. // KRYSTIAN TODO: we can append the characters
  1494. // all at once instead of one at a time
  1495. for(;;)
  1496. {
  1497. if(BOOST_JSON_UNLIKELY(! cs || temp.capacity() == 0 ))
  1498. {
  1499. // flush
  1500. if(BOOST_JSON_LIKELY(! temp.empty()))
  1501. {
  1502. BOOST_ASSERT(total <= max_size);
  1503. if(BOOST_JSON_UNLIKELY(
  1504. temp.size() > max_size - total))
  1505. {
  1506. BOOST_STATIC_CONSTEXPR source_location loc
  1507. = BOOST_CURRENT_LOCATION;
  1508. return fail(cs.begin(), ev_too_large, &loc);
  1509. }
  1510. total += temp.size();
  1511. {
  1512. bool r = is_key
  1513. ? h_.on_key_part(temp.get(), total, ec_)
  1514. : h_.on_string_part(temp.get(), total, ec_);
  1515. if(BOOST_JSON_UNLIKELY(!r))
  1516. {
  1517. return fail(cs.begin());
  1518. }
  1519. }
  1520. temp.clear();
  1521. }
  1522. cs.clip(temp.max_size());
  1523. if(BOOST_JSON_UNLIKELY(! cs))
  1524. return maybe_suspend(cs.begin(), state::str2, total);
  1525. }
  1526. c = *cs;
  1527. if(BOOST_JSON_LIKELY(c == '\x22')) // '"'
  1528. {
  1529. BOOST_ASSERT(total <= max_size);
  1530. if(BOOST_JSON_UNLIKELY(
  1531. temp.size() > max_size - total))
  1532. {
  1533. BOOST_STATIC_CONSTEXPR source_location loc
  1534. = BOOST_CURRENT_LOCATION;
  1535. return fail(cs.begin(), ev_too_large, &loc);
  1536. }
  1537. total += temp.size();
  1538. {
  1539. bool r = is_key
  1540. ? h_.on_key(temp.get(), total, ec_)
  1541. : h_.on_string(temp.get(), total, ec_);
  1542. if(BOOST_JSON_UNLIKELY(!r))
  1543. {
  1544. return fail(cs.begin());
  1545. }
  1546. }
  1547. ++cs;
  1548. return cs.begin();
  1549. }
  1550. else if((c & 0x80) && !allow_bad_utf8)
  1551. {
  1552. seq_.save(cs.begin(), cs.remain());
  1553. if(BOOST_JSON_UNLIKELY(! seq_.complete()))
  1554. {
  1555. if(BOOST_JSON_LIKELY(! temp.empty()))
  1556. {
  1557. BOOST_ASSERT(total <= max_size);
  1558. if(BOOST_JSON_UNLIKELY(
  1559. temp.size() > max_size - total))
  1560. {
  1561. BOOST_STATIC_CONSTEXPR source_location loc
  1562. = BOOST_CURRENT_LOCATION;
  1563. return fail(cs.begin(), ev_too_large, &loc);
  1564. }
  1565. total += temp.size();
  1566. {
  1567. bool r = is_key
  1568. ? h_.on_key_part(temp.get(), total, ec_)
  1569. : h_.on_string_part(temp.get(), total, ec_);
  1570. if(BOOST_JSON_UNLIKELY(!r))
  1571. {
  1572. return fail(cs.begin());
  1573. }
  1574. }
  1575. temp.clear();
  1576. }
  1577. cs = cs.end();
  1578. // ensure there is room for the saved byte sequence
  1579. cs.clip(temp.max_size() - seq_.length());
  1580. goto do_str8;
  1581. }
  1582. if(BOOST_JSON_UNLIKELY(! seq_.valid()))
  1583. {
  1584. BOOST_STATIC_CONSTEXPR source_location loc
  1585. = BOOST_CURRENT_LOCATION;
  1586. return fail(cs.begin(), error::syntax, &loc);
  1587. }
  1588. temp.append(seq_.data(), seq_.length());
  1589. cs += seq_.length();
  1590. continue;
  1591. }
  1592. else if(BOOST_JSON_LIKELY(c == '\\'))
  1593. {
  1594. ++cs;
  1595. goto do_str3;
  1596. }
  1597. else if(BOOST_JSON_UNLIKELY(
  1598. detail::is_control(c)))
  1599. {
  1600. BOOST_STATIC_CONSTEXPR source_location loc
  1601. = BOOST_CURRENT_LOCATION;
  1602. return fail(cs.begin(), error::syntax, &loc);
  1603. }
  1604. temp.push_back(c);
  1605. ++cs;
  1606. }
  1607. do_str8:
  1608. uint8_t needed = seq_.needed();
  1609. if(BOOST_JSON_UNLIKELY(
  1610. ! seq_.append(cs.begin(), cs.remain())))
  1611. return maybe_suspend(cs.end(), state::str8, total);
  1612. if(BOOST_JSON_UNLIKELY(! seq_.valid()))
  1613. {
  1614. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  1615. return fail(cs.begin(), error::syntax, &loc);
  1616. }
  1617. temp.append(seq_.data(), seq_.length());
  1618. cs += needed;
  1619. goto do_str2;
  1620. }
  1621. //----------------------------------------------------------
  1622. template<class Handler>
  1623. template<
  1624. bool StackEmpty_,
  1625. bool AllowComments_/*,
  1626. bool AllowTrailing_,
  1627. bool AllowBadUTF8_*/>
  1628. const char*
  1629. basic_parser<Handler>::
  1630. parse_object(const char* p,
  1631. std::integral_constant<bool, StackEmpty_> stack_empty,
  1632. std::integral_constant<bool, AllowComments_> allow_comments,
  1633. /*std::integral_constant<bool, AllowTrailing_>*/ bool allow_trailing,
  1634. /*std::integral_constant<bool, AllowBadUTF8_>*/ bool allow_bad_utf8)
  1635. {
  1636. detail::const_stream_wrapper cs(p, end_);
  1637. std::size_t size;
  1638. if(! stack_empty && ! st_.empty())
  1639. {
  1640. // resume
  1641. state st;
  1642. st_.pop(st);
  1643. st_.pop(size);
  1644. switch(st)
  1645. {
  1646. default: BOOST_JSON_UNREACHABLE();
  1647. case state::obj1: goto do_obj1;
  1648. case state::obj2: goto do_obj2;
  1649. case state::obj3: goto do_obj3;
  1650. case state::obj4: goto do_obj4;
  1651. case state::obj5: goto do_obj5;
  1652. case state::obj6: goto do_obj6;
  1653. case state::obj7: goto do_obj7;
  1654. case state::obj8: goto do_obj8;
  1655. case state::obj9: goto do_obj9;
  1656. case state::obj10: goto do_obj10;
  1657. case state::obj11: goto do_obj11;
  1658. }
  1659. }
  1660. BOOST_ASSERT(*cs == '{');
  1661. size = 0;
  1662. if(BOOST_JSON_UNLIKELY(! depth_))
  1663. {
  1664. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  1665. return fail(cs.begin(), error::too_deep, &loc);
  1666. }
  1667. --depth_;
  1668. if(BOOST_JSON_UNLIKELY(
  1669. ! h_.on_object_begin(ec_)))
  1670. return fail(cs.begin());
  1671. ++cs;
  1672. // object:
  1673. // '{' *ws '}'
  1674. // '{' *ws string *ws ':' *ws value *ws *[ ',' *ws string *ws ':' *ws value *ws ] '}'
  1675. do_obj1:
  1676. cs = detail::count_whitespace(cs.begin(), cs.end());
  1677. if(BOOST_JSON_UNLIKELY(! cs))
  1678. return maybe_suspend(cs.begin(), state::obj1, size);
  1679. if(BOOST_JSON_LIKELY(*cs != '}'))
  1680. {
  1681. if(BOOST_JSON_UNLIKELY(*cs != '\x22'))
  1682. {
  1683. if(allow_comments && *cs == '/')
  1684. {
  1685. do_obj2:
  1686. cs = parse_comment(cs.begin(), stack_empty, std::false_type());
  1687. if(BOOST_JSON_UNLIKELY(incomplete(cs)))
  1688. return suspend_or_fail(state::obj2, size);
  1689. goto do_obj1;
  1690. }
  1691. BOOST_STATIC_CONSTEXPR source_location loc
  1692. = BOOST_CURRENT_LOCATION;
  1693. return fail(cs.begin(), error::syntax, &loc);
  1694. }
  1695. loop:
  1696. if(BOOST_JSON_UNLIKELY(++size >
  1697. Handler::max_object_size))
  1698. {
  1699. BOOST_STATIC_CONSTEXPR source_location loc
  1700. = BOOST_CURRENT_LOCATION;
  1701. return fail(cs.begin(), error::object_too_large, &loc);
  1702. }
  1703. do_obj3:
  1704. cs = parse_string(cs.begin(), stack_empty, std::true_type(), allow_bad_utf8);
  1705. if(BOOST_JSON_UNLIKELY(incomplete(cs)))
  1706. return suspend_or_fail(state::obj3, size);
  1707. do_obj4:
  1708. cs = detail::count_whitespace(cs.begin(), cs.end());
  1709. if(BOOST_JSON_UNLIKELY(! cs))
  1710. return maybe_suspend(cs.begin(), state::obj4, size);
  1711. if(BOOST_JSON_UNLIKELY(*cs != ':'))
  1712. {
  1713. if(allow_comments && *cs == '/')
  1714. {
  1715. do_obj5:
  1716. cs = parse_comment(cs.begin(), stack_empty, std::false_type());
  1717. if(BOOST_JSON_UNLIKELY(incomplete(cs)))
  1718. return suspend_or_fail(state::obj5, size);
  1719. goto do_obj4;
  1720. }
  1721. BOOST_STATIC_CONSTEXPR source_location loc
  1722. = BOOST_CURRENT_LOCATION;
  1723. return fail(cs.begin(), error::syntax, &loc);
  1724. }
  1725. ++cs;
  1726. do_obj6:
  1727. cs = detail::count_whitespace(cs.begin(), cs.end());
  1728. if(BOOST_JSON_UNLIKELY(! cs))
  1729. return maybe_suspend(cs.begin(), state::obj6, size);
  1730. do_obj7:
  1731. cs = parse_value(cs.begin(), stack_empty, allow_comments, allow_trailing, allow_bad_utf8);
  1732. if(BOOST_JSON_UNLIKELY(incomplete(cs)))
  1733. return suspend_or_fail(state::obj7, size);
  1734. do_obj8:
  1735. cs = detail::count_whitespace(cs.begin(), cs.end());
  1736. if(BOOST_JSON_UNLIKELY(! cs))
  1737. return maybe_suspend(cs.begin(), state::obj8, size);
  1738. if(BOOST_JSON_LIKELY(*cs == ','))
  1739. {
  1740. ++cs;
  1741. do_obj9:
  1742. cs = detail::count_whitespace(cs.begin(), cs.end());
  1743. if(BOOST_JSON_UNLIKELY(! cs))
  1744. return maybe_suspend(cs.begin(), state::obj9, size);
  1745. // loop for next element
  1746. if(BOOST_JSON_LIKELY(*cs == '\x22'))
  1747. goto loop;
  1748. if(! allow_trailing || *cs != '}')
  1749. {
  1750. if(allow_comments && *cs == '/')
  1751. {
  1752. do_obj10:
  1753. cs = parse_comment(cs.begin(), stack_empty, std::false_type());
  1754. if(BOOST_JSON_UNLIKELY(incomplete(cs)))
  1755. return suspend_or_fail(state::obj10, size);
  1756. goto do_obj9;
  1757. }
  1758. BOOST_STATIC_CONSTEXPR source_location loc
  1759. = BOOST_CURRENT_LOCATION;
  1760. return fail(cs.begin(), error::syntax, &loc);
  1761. }
  1762. }
  1763. else if(BOOST_JSON_UNLIKELY(*cs != '}'))
  1764. {
  1765. if(allow_comments && *cs == '/')
  1766. {
  1767. do_obj11:
  1768. cs = parse_comment(cs.begin(), stack_empty, std::false_type());
  1769. if(BOOST_JSON_UNLIKELY(incomplete(cs)))
  1770. return suspend_or_fail(state::obj11, size);
  1771. goto do_obj8;
  1772. }
  1773. BOOST_STATIC_CONSTEXPR source_location loc
  1774. = BOOST_CURRENT_LOCATION;
  1775. return fail(cs.begin(), error::syntax, &loc);
  1776. }
  1777. // got closing brace, fall through
  1778. }
  1779. if(BOOST_JSON_UNLIKELY(
  1780. ! h_.on_object_end(size, ec_)))
  1781. return fail(cs.begin());
  1782. ++depth_;
  1783. ++cs;
  1784. return cs.begin();
  1785. }
  1786. //----------------------------------------------------------
  1787. template<class Handler>
  1788. template<
  1789. bool StackEmpty_,
  1790. bool AllowComments_/*,
  1791. bool AllowTrailing_,
  1792. bool AllowBadUTF8_*/>
  1793. const char*
  1794. basic_parser<Handler>::
  1795. parse_array(const char* p,
  1796. std::integral_constant<bool, StackEmpty_> stack_empty,
  1797. std::integral_constant<bool, AllowComments_> allow_comments,
  1798. /*std::integral_constant<bool, AllowTrailing_>*/ bool allow_trailing,
  1799. /*std::integral_constant<bool, AllowBadUTF8_>*/ bool allow_bad_utf8)
  1800. {
  1801. detail::const_stream_wrapper cs(p, end_);
  1802. std::size_t size;
  1803. if(! stack_empty && ! st_.empty())
  1804. {
  1805. // resume
  1806. state st;
  1807. st_.pop(st);
  1808. st_.pop(size);
  1809. switch(st)
  1810. {
  1811. default: BOOST_JSON_UNREACHABLE();
  1812. case state::arr1: goto do_arr1;
  1813. case state::arr2: goto do_arr2;
  1814. case state::arr3: goto do_arr3;
  1815. case state::arr4: goto do_arr4;
  1816. case state::arr5: goto do_arr5;
  1817. case state::arr6: goto do_arr6;
  1818. }
  1819. }
  1820. BOOST_ASSERT(*cs == '[');
  1821. size = 0;
  1822. if(BOOST_JSON_UNLIKELY(! depth_))
  1823. {
  1824. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  1825. return fail(cs.begin(), error::too_deep, &loc);
  1826. }
  1827. --depth_;
  1828. if(BOOST_JSON_UNLIKELY(
  1829. ! h_.on_array_begin(ec_)))
  1830. return fail(cs.begin());
  1831. ++cs;
  1832. // array:
  1833. // '[' *ws ']'
  1834. // '[' *ws value *ws *[ ',' *ws value *ws ] ']'
  1835. do_arr1:
  1836. cs = detail::count_whitespace(cs.begin(), cs.end());
  1837. if(BOOST_JSON_UNLIKELY(! cs))
  1838. return maybe_suspend(cs.begin(), state::arr1, size);
  1839. if(BOOST_JSON_LIKELY(*cs != ']'))
  1840. {
  1841. loop:
  1842. if(allow_comments && *cs == '/')
  1843. {
  1844. do_arr2:
  1845. cs = parse_comment(cs.begin(), stack_empty, std::false_type());
  1846. if(BOOST_JSON_UNLIKELY(incomplete(cs)))
  1847. return suspend_or_fail(state::arr2, size);
  1848. goto do_arr1;
  1849. }
  1850. if(BOOST_JSON_UNLIKELY(++size >
  1851. Handler::max_array_size))
  1852. {
  1853. BOOST_STATIC_CONSTEXPR source_location loc
  1854. = BOOST_CURRENT_LOCATION;
  1855. return fail(cs.begin(), error::array_too_large, &loc);
  1856. }
  1857. do_arr3:
  1858. // array is not empty, value required
  1859. cs = parse_value(cs.begin(), stack_empty, allow_comments, allow_trailing, allow_bad_utf8);
  1860. if(BOOST_JSON_UNLIKELY(incomplete(cs)))
  1861. return suspend_or_fail(state::arr3, size);
  1862. do_arr4:
  1863. cs = detail::count_whitespace(cs.begin(), cs.end());
  1864. if(BOOST_JSON_UNLIKELY(! cs))
  1865. return maybe_suspend(cs.begin(), state::arr4, size);
  1866. if(BOOST_JSON_LIKELY(*cs == ','))
  1867. {
  1868. ++cs;
  1869. do_arr5:
  1870. cs = detail::count_whitespace(cs.begin(), cs.end());
  1871. if(BOOST_JSON_UNLIKELY(! cs))
  1872. return maybe_suspend(cs.begin(), state::arr5, size);
  1873. // loop for next element
  1874. if(! allow_trailing || *cs != ']')
  1875. goto loop;
  1876. }
  1877. else if(BOOST_JSON_UNLIKELY(*cs != ']'))
  1878. {
  1879. if(allow_comments && *cs == '/')
  1880. {
  1881. do_arr6:
  1882. cs = parse_comment(cs.begin(), stack_empty, std::false_type());
  1883. if(BOOST_JSON_UNLIKELY(incomplete(cs)))
  1884. return suspend_or_fail(state::arr6, size);
  1885. goto do_arr4;
  1886. }
  1887. BOOST_STATIC_CONSTEXPR source_location loc
  1888. = BOOST_CURRENT_LOCATION;
  1889. return fail(cs.begin(), error::syntax, &loc);
  1890. }
  1891. // got closing bracket; fall through
  1892. }
  1893. if(BOOST_JSON_UNLIKELY(
  1894. ! h_.on_array_end(size, ec_)))
  1895. return fail(cs.begin());
  1896. ++depth_;
  1897. ++cs;
  1898. return cs.begin();
  1899. }
  1900. //----------------------------------------------------------
  1901. template<class Handler>
  1902. template<bool StackEmpty_, char First_>
  1903. const char*
  1904. basic_parser<Handler>::
  1905. parse_number(const char* p,
  1906. std::integral_constant<bool, StackEmpty_> stack_empty,
  1907. std::integral_constant<char, First_> first)
  1908. {
  1909. // only one of these will be true if we are not resuming
  1910. // if negative then !zero_first && !nonzero_first
  1911. // if zero_first then !nonzero_first && !negative
  1912. // if nonzero_first then !zero_first && !negative
  1913. bool const negative = first == '-';
  1914. bool const zero_first = first == '0';
  1915. bool const nonzero_first = first == '+';
  1916. detail::const_stream_wrapper cs(p, end_);
  1917. number num;
  1918. const char* begin = cs.begin();
  1919. if(stack_empty || st_.empty())
  1920. {
  1921. num.bias = 0;
  1922. num.exp = 0;
  1923. num.frac = false;
  1924. //----------------------------------
  1925. //
  1926. // '-'
  1927. // leading minus sign
  1928. //
  1929. BOOST_ASSERT(cs);
  1930. if(negative)
  1931. ++cs;
  1932. num.neg = negative;
  1933. num.frac = false;
  1934. num.exp = 0;
  1935. num.bias = 0;
  1936. // fast path
  1937. if( cs.remain() >= 16 + 1 + 16 ) // digits . digits
  1938. {
  1939. int n1;
  1940. if( nonzero_first ||
  1941. (negative && *cs != '0') )
  1942. {
  1943. n1 = detail::count_digits( cs.begin() );
  1944. BOOST_ASSERT(n1 >= 0 && n1 <= 16);
  1945. if( ! nonzero_first && n1 == 0 )
  1946. {
  1947. // digit required
  1948. BOOST_STATIC_CONSTEXPR source_location loc
  1949. = BOOST_CURRENT_LOCATION;
  1950. return fail(cs.begin(), error::syntax, &loc);
  1951. }
  1952. num.mant = detail::parse_unsigned( 0, cs.begin(), n1 );
  1953. cs += n1;
  1954. // integer or floating-point with
  1955. // >= 16 leading digits
  1956. if( n1 == 16 )
  1957. {
  1958. goto do_num2;
  1959. }
  1960. }
  1961. else
  1962. {
  1963. // 0. floating-point or 0e integer
  1964. num.mant = 0;
  1965. n1 = 0;
  1966. ++cs;
  1967. }
  1968. {
  1969. const char c = *cs;
  1970. if(c != '.')
  1971. {
  1972. if((c | 32) == 'e')
  1973. {
  1974. ++cs;
  1975. goto do_exp1;
  1976. }
  1977. if(negative)
  1978. num.mant = ~num.mant + 1;
  1979. goto finish_signed;
  1980. }
  1981. }
  1982. // floating-point number
  1983. ++cs;
  1984. int n2 = detail::count_digits( cs.begin() );
  1985. BOOST_ASSERT(n2 >= 0 && n2 <= 16);
  1986. if( n2 == 0 )
  1987. {
  1988. // digit required
  1989. BOOST_STATIC_CONSTEXPR source_location loc
  1990. = BOOST_CURRENT_LOCATION;
  1991. return fail(cs.begin(), error::syntax, &loc);
  1992. }
  1993. // floating-point mantissa overflow
  1994. if( n1 + n2 >= 19 )
  1995. {
  1996. goto do_num7;
  1997. }
  1998. num.mant = detail::parse_unsigned( num.mant, cs.begin(), n2 );
  1999. BOOST_ASSERT(num.bias == 0);
  2000. num.bias -= n2;
  2001. cs += n2;
  2002. char ch = *cs;
  2003. if( (ch | 32) == 'e' )
  2004. {
  2005. ++cs;
  2006. goto do_exp1;
  2007. }
  2008. else if( ch >= '0' && ch <= '9' )
  2009. {
  2010. goto do_num8;
  2011. }
  2012. goto finish_dub;
  2013. }
  2014. }
  2015. else
  2016. {
  2017. num = num_;
  2018. state st;
  2019. st_.pop(st);
  2020. switch(st)
  2021. {
  2022. default: BOOST_JSON_UNREACHABLE();
  2023. case state::num1: goto do_num1;
  2024. case state::num2: goto do_num2;
  2025. case state::num3: goto do_num3;
  2026. case state::num4: goto do_num4;
  2027. case state::num5: goto do_num5;
  2028. case state::num6: goto do_num6;
  2029. case state::num7: goto do_num7;
  2030. case state::num8: goto do_num8;
  2031. case state::exp1: goto do_exp1;
  2032. case state::exp2: goto do_exp2;
  2033. case state::exp3: goto do_exp3;
  2034. }
  2035. }
  2036. //----------------------------------
  2037. //
  2038. // DIGIT
  2039. // first digit
  2040. //
  2041. do_num1:
  2042. if(zero_first || nonzero_first ||
  2043. BOOST_JSON_LIKELY(cs))
  2044. {
  2045. char const c = *cs;
  2046. if(zero_first)
  2047. {
  2048. ++cs;
  2049. num.mant = 0;
  2050. goto do_num6;
  2051. }
  2052. else if(nonzero_first || BOOST_JSON_LIKELY(
  2053. c >= '1' && c <= '9'))
  2054. {
  2055. ++cs;
  2056. num.mant = c - '0';
  2057. }
  2058. else if(BOOST_JSON_UNLIKELY(
  2059. c == '0'))
  2060. {
  2061. ++cs;
  2062. num.mant = 0;
  2063. goto do_num6;
  2064. }
  2065. else
  2066. {
  2067. BOOST_STATIC_CONSTEXPR source_location loc
  2068. = BOOST_CURRENT_LOCATION;
  2069. return fail(cs.begin(), error::syntax, &loc);
  2070. }
  2071. }
  2072. else
  2073. {
  2074. if(BOOST_JSON_UNLIKELY(
  2075. ! h_.on_number_part(
  2076. {begin, cs.used(begin)}, ec_)))
  2077. return fail(cs.begin());
  2078. return maybe_suspend(
  2079. cs.begin(), state::num1, num);
  2080. }
  2081. //----------------------------------
  2082. //
  2083. // 1*DIGIT
  2084. // significant digits left of decimal
  2085. //
  2086. do_num2:
  2087. if(negative || (!stack_empty && num.neg))
  2088. {
  2089. for(;;)
  2090. {
  2091. if(BOOST_JSON_UNLIKELY(! cs))
  2092. {
  2093. if(BOOST_JSON_UNLIKELY(more_))
  2094. {
  2095. if(BOOST_JSON_UNLIKELY(
  2096. ! h_.on_number_part(
  2097. {begin, cs.used(begin)}, ec_)))
  2098. return fail(cs.begin());
  2099. return suspend(cs.begin(), state::num2, num);
  2100. }
  2101. goto finish_int;
  2102. }
  2103. char const c = *cs;
  2104. if(BOOST_JSON_LIKELY(
  2105. c >= '0' && c <= '9'))
  2106. {
  2107. ++cs;
  2108. // 9223372036854775808 INT64_MIN
  2109. if( num.mant > 922337203685477580 || (
  2110. num.mant == 922337203685477580 && c > '8'))
  2111. break;
  2112. num.mant = 10 * num.mant + ( c - '0' );
  2113. continue;
  2114. }
  2115. goto do_num6; // [.eE]
  2116. }
  2117. }
  2118. else
  2119. {
  2120. for(;;)
  2121. {
  2122. if(BOOST_JSON_UNLIKELY(! cs))
  2123. {
  2124. if(BOOST_JSON_UNLIKELY(more_))
  2125. {
  2126. if(BOOST_JSON_UNLIKELY(
  2127. ! h_.on_number_part(
  2128. {begin, cs.used(begin)}, ec_)))
  2129. return fail(cs.begin());
  2130. return suspend(cs.begin(), state::num2, num);
  2131. }
  2132. goto finish_int;
  2133. }
  2134. char const c = *cs;
  2135. if(BOOST_JSON_LIKELY(
  2136. c >= '0' && c <= '9'))
  2137. {
  2138. ++cs;
  2139. // 18446744073709551615 UINT64_MAX
  2140. if( num.mant > 1844674407370955161 || (
  2141. num.mant == 1844674407370955161 && c > '5'))
  2142. break;
  2143. num.mant = 10 * num.mant + ( c - '0' );
  2144. }
  2145. else
  2146. {
  2147. goto do_num6; // [.eE]
  2148. }
  2149. }
  2150. }
  2151. ++num.bias;
  2152. //----------------------------------
  2153. //
  2154. // 1*DIGIT
  2155. // non-significant digits left of decimal
  2156. //
  2157. do_num3:
  2158. for(;;)
  2159. {
  2160. if(BOOST_JSON_UNLIKELY(! cs))
  2161. {
  2162. if(BOOST_JSON_UNLIKELY(more_))
  2163. {
  2164. if(BOOST_JSON_UNLIKELY(
  2165. ! h_.on_number_part(
  2166. {begin, cs.used(begin)}, ec_)))
  2167. return fail(cs.begin());
  2168. return suspend(cs.begin(), state::num3, num);
  2169. }
  2170. goto finish_dub;
  2171. }
  2172. char const c = *cs;
  2173. if(BOOST_JSON_UNLIKELY(
  2174. c >= '0' && c <= '9'))
  2175. {
  2176. ++cs;
  2177. // VFALCO check overflow
  2178. ++num.bias;
  2179. }
  2180. else if(BOOST_JSON_LIKELY(
  2181. c == '.'))
  2182. {
  2183. ++cs;
  2184. break;
  2185. }
  2186. else if((c | 32) == 'e')
  2187. {
  2188. ++cs;
  2189. goto do_exp1;
  2190. }
  2191. else
  2192. {
  2193. goto finish_dub;
  2194. }
  2195. }
  2196. //----------------------------------
  2197. //
  2198. // DIGIT
  2199. // first non-significant digit
  2200. // to the right of decimal
  2201. //
  2202. do_num4:
  2203. {
  2204. if(BOOST_JSON_UNLIKELY(! cs))
  2205. {
  2206. if(BOOST_JSON_UNLIKELY(
  2207. ! h_.on_number_part(
  2208. {begin, cs.used(begin)}, ec_)))
  2209. return fail(cs.begin());
  2210. return maybe_suspend(
  2211. cs.begin(), state::num4, num);
  2212. }
  2213. char const c = *cs;
  2214. if(BOOST_JSON_LIKELY(
  2215. //static_cast<unsigned char>(c - '0') < 10))
  2216. c >= '0' && c <= '9'))
  2217. {
  2218. ++cs;
  2219. }
  2220. else
  2221. {
  2222. // digit required
  2223. BOOST_STATIC_CONSTEXPR source_location loc
  2224. = BOOST_CURRENT_LOCATION;
  2225. return fail(cs.begin(), error::syntax, &loc);
  2226. }
  2227. }
  2228. //----------------------------------
  2229. //
  2230. // 1*DIGIT
  2231. // non-significant digits
  2232. // to the right of decimal
  2233. //
  2234. do_num5:
  2235. for(;;)
  2236. {
  2237. if(BOOST_JSON_UNLIKELY(! cs))
  2238. {
  2239. if(BOOST_JSON_UNLIKELY(more_))
  2240. {
  2241. if(BOOST_JSON_UNLIKELY(
  2242. ! h_.on_number_part(
  2243. {begin, cs.used(begin)}, ec_)))
  2244. return fail(cs.begin());
  2245. return suspend(cs.begin(), state::num5, num);
  2246. }
  2247. goto finish_dub;
  2248. }
  2249. char const c = *cs;
  2250. if(BOOST_JSON_LIKELY(
  2251. c >= '0' && c <= '9'))
  2252. {
  2253. ++cs;
  2254. }
  2255. else if((c | 32) == 'e')
  2256. {
  2257. ++cs;
  2258. goto do_exp1;
  2259. }
  2260. else
  2261. {
  2262. goto finish_dub;
  2263. }
  2264. }
  2265. //----------------------------------
  2266. //
  2267. // [.eE]
  2268. //
  2269. do_num6:
  2270. {
  2271. if(BOOST_JSON_UNLIKELY(! cs))
  2272. {
  2273. if(BOOST_JSON_UNLIKELY(more_))
  2274. {
  2275. if(BOOST_JSON_UNLIKELY(
  2276. ! h_.on_number_part(
  2277. {begin, cs.used(begin)}, ec_)))
  2278. return fail(cs.begin());
  2279. return suspend(cs.begin(), state::num6, num);
  2280. }
  2281. goto finish_int;
  2282. }
  2283. char const c = *cs;
  2284. if(BOOST_JSON_LIKELY(
  2285. c == '.'))
  2286. {
  2287. ++cs;
  2288. }
  2289. else if((c | 32) == 'e')
  2290. {
  2291. ++cs;
  2292. goto do_exp1;
  2293. }
  2294. else
  2295. {
  2296. goto finish_int;
  2297. }
  2298. }
  2299. //----------------------------------
  2300. //
  2301. // DIGIT
  2302. // first significant digit
  2303. // to the right of decimal
  2304. //
  2305. do_num7:
  2306. {
  2307. if(BOOST_JSON_UNLIKELY(! cs))
  2308. {
  2309. if(BOOST_JSON_UNLIKELY(more_))
  2310. {
  2311. if(BOOST_JSON_UNLIKELY(
  2312. ! h_.on_number_part(
  2313. {begin, cs.used(begin)}, ec_)))
  2314. return fail(cs.begin());
  2315. return suspend(cs.begin(), state::num7, num);
  2316. }
  2317. // digit required
  2318. BOOST_STATIC_CONSTEXPR source_location loc
  2319. = BOOST_CURRENT_LOCATION;
  2320. return fail(cs.begin(), error::syntax, &loc);
  2321. }
  2322. char const c = *cs;
  2323. if(BOOST_JSON_UNLIKELY(
  2324. c < '0' || c > '9'))
  2325. {
  2326. // digit required
  2327. BOOST_STATIC_CONSTEXPR source_location loc
  2328. = BOOST_CURRENT_LOCATION;
  2329. return fail(cs.begin(), error::syntax, &loc);
  2330. }
  2331. }
  2332. //----------------------------------
  2333. //
  2334. // 1*DIGIT
  2335. // significant digits
  2336. // to the right of decimal
  2337. //
  2338. do_num8:
  2339. for(;;)
  2340. {
  2341. if(BOOST_JSON_UNLIKELY(! cs))
  2342. {
  2343. if(BOOST_JSON_UNLIKELY(more_))
  2344. {
  2345. if(BOOST_JSON_UNLIKELY(
  2346. ! h_.on_number_part(
  2347. {begin, cs.used(begin)}, ec_)))
  2348. return fail(cs.begin());
  2349. return suspend(cs.begin(), state::num8, num);
  2350. }
  2351. goto finish_dub;
  2352. }
  2353. char const c = *cs;
  2354. if(BOOST_JSON_LIKELY(
  2355. c >= '0' && c <= '9'))
  2356. {
  2357. ++cs;
  2358. if(BOOST_JSON_LIKELY(
  2359. num.mant <= 9007199254740991)) // 2^53-1
  2360. {
  2361. --num.bias;
  2362. num.mant = 10 * num.mant + ( c - '0' );
  2363. }
  2364. else
  2365. {
  2366. goto do_num5;
  2367. }
  2368. }
  2369. else if((c | 32) == 'e')
  2370. {
  2371. ++cs;
  2372. goto do_exp1;
  2373. }
  2374. else
  2375. {
  2376. goto finish_dub;
  2377. }
  2378. }
  2379. //----------------------------------
  2380. //
  2381. // *[+-]
  2382. //
  2383. do_exp1:
  2384. if(BOOST_JSON_UNLIKELY(! cs))
  2385. {
  2386. if(BOOST_JSON_UNLIKELY(
  2387. ! h_.on_number_part(
  2388. {begin, cs.used(begin)}, ec_)))
  2389. return fail(cs.begin());
  2390. return maybe_suspend(
  2391. cs.begin(), state::exp1, num);
  2392. }
  2393. if(*cs == '+')
  2394. {
  2395. ++cs;
  2396. }
  2397. else if(*cs == '-')
  2398. {
  2399. ++cs;
  2400. num.frac = true;
  2401. }
  2402. //----------------------------------
  2403. //
  2404. // DIGIT
  2405. // first digit of the exponent
  2406. //
  2407. do_exp2:
  2408. {
  2409. if(BOOST_JSON_UNLIKELY(! cs))
  2410. {
  2411. if(BOOST_JSON_UNLIKELY(more_))
  2412. {
  2413. if(BOOST_JSON_UNLIKELY(
  2414. ! h_.on_number_part(
  2415. {begin, cs.used(begin)}, ec_)))
  2416. return fail(cs.begin());
  2417. return suspend(cs.begin(), state::exp2, num);
  2418. }
  2419. // digit required
  2420. BOOST_STATIC_CONSTEXPR source_location loc
  2421. = BOOST_CURRENT_LOCATION;
  2422. return fail(cs.begin(), error::syntax, &loc);
  2423. }
  2424. char const c = *cs;
  2425. if(BOOST_JSON_UNLIKELY(
  2426. c < '0' || c > '9'))
  2427. {
  2428. // digit required
  2429. BOOST_STATIC_CONSTEXPR source_location loc
  2430. = BOOST_CURRENT_LOCATION;
  2431. return fail(cs.begin(), error::syntax, &loc);
  2432. }
  2433. ++cs;
  2434. num.exp = c - '0';
  2435. }
  2436. //----------------------------------
  2437. //
  2438. // 1*DIGIT
  2439. // subsequent digits in the exponent
  2440. //
  2441. do_exp3:
  2442. for(;;)
  2443. {
  2444. if(BOOST_JSON_UNLIKELY(! cs))
  2445. {
  2446. if(BOOST_JSON_UNLIKELY(more_))
  2447. {
  2448. if(BOOST_JSON_UNLIKELY(
  2449. ! h_.on_number_part(
  2450. {begin, cs.used(begin)}, ec_)))
  2451. return fail(cs.begin());
  2452. return suspend(cs.begin(), state::exp3, num);
  2453. }
  2454. }
  2455. else
  2456. {
  2457. char const c = *cs;
  2458. if(BOOST_JSON_LIKELY(
  2459. c >= '0' && c <= '9'))
  2460. {
  2461. if(BOOST_JSON_UNLIKELY
  2462. // 2147483647 INT_MAX
  2463. (num.exp > 214748364 || (
  2464. num.exp == 214748364 && c > '7')))
  2465. {
  2466. BOOST_STATIC_CONSTEXPR source_location loc
  2467. = BOOST_CURRENT_LOCATION;
  2468. return fail(cs.begin(), error::exponent_overflow, &loc);
  2469. }
  2470. ++cs;
  2471. num.exp = 10 * num.exp + ( c - '0' );
  2472. continue;
  2473. }
  2474. }
  2475. BOOST_ASSERT(num.exp >= 0);
  2476. if ( num.frac )
  2477. {
  2478. if (BOOST_JSON_UNLIKELY( num.bias < (INT_MIN + num.exp) ))
  2479. {
  2480. BOOST_STATIC_CONSTEXPR source_location loc
  2481. = BOOST_CURRENT_LOCATION;
  2482. return fail(cs.begin(), error::exponent_overflow, &loc);
  2483. }
  2484. }
  2485. else
  2486. {
  2487. if (BOOST_JSON_UNLIKELY( num.bias > (INT_MAX - num.exp) ))
  2488. {
  2489. BOOST_STATIC_CONSTEXPR source_location loc
  2490. = BOOST_CURRENT_LOCATION;
  2491. return fail(cs.begin(), error::exponent_overflow, &loc);
  2492. }
  2493. }
  2494. goto finish_dub;
  2495. }
  2496. finish_int:
  2497. if(negative || (!stack_empty && num.neg))
  2498. {
  2499. if(BOOST_JSON_UNLIKELY(
  2500. ! h_.on_int64(static_cast<
  2501. int64_t>(~num.mant + 1), {begin, cs.used(begin)}, ec_)))
  2502. return fail(cs.begin());
  2503. return cs.begin();
  2504. }
  2505. if(num.mant <= INT64_MAX)
  2506. {
  2507. finish_signed:
  2508. if(BOOST_JSON_UNLIKELY(
  2509. ! h_.on_int64(static_cast<
  2510. int64_t>(num.mant), {begin, cs.used(begin)}, ec_)))
  2511. return fail(cs.begin());
  2512. return cs.begin();
  2513. }
  2514. if(BOOST_JSON_UNLIKELY(
  2515. ! h_.on_uint64(num.mant, {begin, cs.used(begin)}, ec_)))
  2516. return fail(cs.begin());
  2517. return cs.begin();
  2518. finish_dub:
  2519. double const d = detail::dec_to_float(
  2520. num.mant,
  2521. num.bias + (num.frac ?
  2522. -num.exp : num.exp),
  2523. num.neg);
  2524. if(BOOST_JSON_UNLIKELY(
  2525. ! h_.on_double(d, {begin, cs.used(begin)}, ec_)))
  2526. return fail(cs.begin());
  2527. return cs.begin();
  2528. }
  2529. //----------------------------------------------------------
  2530. template<class Handler>
  2531. template<class... Args>
  2532. basic_parser<Handler>::
  2533. basic_parser(
  2534. parse_options const& opt,
  2535. Args&&... args)
  2536. : h_(std::forward<Args>(args)...)
  2537. , opt_(opt)
  2538. {
  2539. }
  2540. //----------------------------------------------------------
  2541. template<class Handler>
  2542. void
  2543. basic_parser<Handler>::
  2544. reset() noexcept
  2545. {
  2546. ec_ = {};
  2547. st_.clear();
  2548. more_ = true;
  2549. done_ = false;
  2550. clean_ = true;
  2551. }
  2552. template<class Handler>
  2553. void
  2554. basic_parser<Handler>::
  2555. fail(error_code ec) noexcept
  2556. {
  2557. if(! ec)
  2558. {
  2559. // assign an arbitrary
  2560. // error code to prevent UB
  2561. BOOST_JSON_FAIL(ec_, error::incomplete);
  2562. }
  2563. else
  2564. {
  2565. ec_ = ec;
  2566. }
  2567. done_ = false;
  2568. }
  2569. //----------------------------------------------------------
  2570. template<class Handler>
  2571. std::size_t
  2572. basic_parser<Handler>::
  2573. write_some(
  2574. bool more,
  2575. char const* data,
  2576. std::size_t size,
  2577. error_code& ec)
  2578. {
  2579. // see if we exited via exception
  2580. // on the last call to write_some
  2581. if(! clean_)
  2582. {
  2583. // prevent UB
  2584. if(! ec_)
  2585. {
  2586. BOOST_JSON_FAIL(ec_, error::exception);
  2587. }
  2588. }
  2589. if(ec_)
  2590. {
  2591. // error is sticky
  2592. ec = ec_;
  2593. return 0;
  2594. }
  2595. clean_ = false;
  2596. more_ = more;
  2597. end_ = data + size;
  2598. const char* p;
  2599. if(BOOST_JSON_LIKELY(st_.empty()))
  2600. {
  2601. // first time
  2602. depth_ = opt_.max_depth;
  2603. if(BOOST_JSON_UNLIKELY(
  2604. ! h_.on_document_begin(ec_)))
  2605. {
  2606. ec = ec_;
  2607. return 0;
  2608. }
  2609. p = parse_document(data, std::true_type());
  2610. }
  2611. else
  2612. {
  2613. p = parse_document(data, std::false_type());
  2614. }
  2615. if(BOOST_JSON_LIKELY(p != sentinel()))
  2616. {
  2617. BOOST_ASSERT(! ec_);
  2618. if(! done_)
  2619. {
  2620. done_ = true;
  2621. h_.on_document_end(ec_);
  2622. }
  2623. }
  2624. else
  2625. {
  2626. if(! ec_)
  2627. {
  2628. if(! more_)
  2629. {
  2630. BOOST_JSON_FAIL(ec_, error::incomplete);
  2631. }
  2632. else if(! st_.empty())
  2633. {
  2634. // consume as much trailing whitespace in
  2635. // the JSON document as possible, but still
  2636. // consider the parse complete
  2637. state st;
  2638. st_.peek(st);
  2639. if( st == state::doc3 &&
  2640. ! done_)
  2641. {
  2642. done_ = true;
  2643. h_.on_document_end(ec_);
  2644. }
  2645. }
  2646. }
  2647. p = end_;
  2648. }
  2649. ec = ec_;
  2650. clean_ = true;
  2651. return p - data;
  2652. }
  2653. template<class Handler>
  2654. std::size_t
  2655. basic_parser<Handler>::
  2656. write_some(
  2657. bool more,
  2658. char const* data,
  2659. std::size_t size,
  2660. std::error_code& ec)
  2661. {
  2662. error_code jec;
  2663. std::size_t const result = write_some(more, data, size, jec);
  2664. ec = jec;
  2665. return result;
  2666. }
  2667. #endif
  2668. } // namespace json
  2669. } // namespace boost
  2670. #ifdef _MSC_VER
  2671. #pragma warning(pop)
  2672. #endif
  2673. #endif