basic_parser_impl.hpp 92 KB

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