basic_parser_impl.hpp 73 KB

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