basic_parser.hpp 21.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
//
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
// Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/json
//

#ifndef BOOST_JSON_BASIC_PARSER_HPP
#define BOOST_JSON_BASIC_PARSER_HPP

#include <boost/json/detail/config.hpp>
#include <boost/json/detail/except.hpp>
#include <boost/json/error.hpp>
#include <boost/json/kind.hpp>
#include <boost/json/parse_options.hpp>
#include <boost/json/detail/stack.hpp>
#include <boost/json/detail/stream.hpp>
#include <boost/json/detail/utf8.hpp>

/*  VFALCO NOTE

    This file is in the detail namespace because it
    is not allowed to be included directly by users,
    who should be including <boost/json/basic_parser.hpp>
    instead, which provides the member function definitions.

    The source code is arranged this way to keep compile
    times down.
*/

BOOST_JSON_NS_BEGIN

/** An incremental SAX parser for serialized JSON.

    This implements a SAX-style parser, invoking a
    caller-supplied handler with each parsing event.
    To use, first declare a variable of type
    `basic_parser<T>` where `T` meets the handler
    requirements specified below. Then call
    @ref write_some one or more times with the input,
    setting `more = false` on the final buffer.
    The parsing events are realized through member
    function calls on the handler, which exists
    as a data member of the parser.
\n
    The parser may dynamically allocate intermediate
    storage as needed to accommodate the nesting level
    of the input JSON. On subsequent invocations, the
    parser can cheaply re-use this memory, improving
    performance. This storage is freed when the
    parser is destroyed

    @par Usage

    To get the declaration and function definitions
    for this class it is necessary to include this
    file instead:
    @code
    #include <boost/json/basic_parser_impl.hpp>
    @endcode

    Users who wish to parse JSON into the DOM container
    @ref value will not use this class directly; instead
    they will create an instance of @ref parser or
    @ref stream_parser and use that instead. Alternatively,
    they may call the function @ref parse. This class is
    designed for users who wish to perform custom actions
    instead of building a @ref value. For example, to
    produce a DOM from an external library.
\n
    @note

    By default, only conforming JSON using UTF-8
    encoding is accepted. However, select non-compliant
    syntax can be allowed by construction using a
    @ref parse_options set to desired values.

    @par Handler

    The handler provided must be implemented as an
    object of class type which defines each of the
    required event member functions below. The event
    functions return a `bool` where `true` indicates
    success, and `false` indicates failure. If the
    member function returns `false`, it must set
    the error code to a suitable value. This error
    code will be returned by the write function to
    the caller.
\n
    Handlers are required to declare the maximum
    limits on various elements. If these limits
    are exceeded during parsing, then parsing
    fails with an error.
\n
    The following declaration meets the parser's
    handler requirements:

    @code
    struct handler
    {
        /// The maximum number of elements allowed in an array
        static constexpr std::size_t max_array_size = -1;

        /// The maximum number of elements allowed in an object
        static constexpr std::size_t max_object_size = -1;

        /// The maximum number of characters allowed in a string
        static constexpr std::size_t max_string_size = -1;

        /// The maximum number of characters allowed in a key
        static constexpr std::size_t max_key_size = -1;

        /// Called once when the JSON parsing begins.
        ///
        /// @return `true` on success.
        /// @param ec Set to the error, if any occurred.
        ///
        bool on_document_begin( error_code& ec );

        /// Called when the JSON parsing is done.
        ///
        /// @return `true` on success.
        /// @param ec Set to the error, if any occurred.
        ///
        bool on_document_end( error_code& ec );

        /// Called when the beginning of an array is encountered.
        ///
        /// @return `true` on success.
        /// @param ec Set to the error, if any occurred.
        ///
        bool on_array_begin( error_code& ec );

        /// Called when the end of the current array is encountered.
        ///
        /// @return `true` on success.
        /// @param n The number of elements in the array.
        /// @param ec Set to the error, if any occurred.
        ///
        bool on_array_end( std::size_t n, error_code& ec );

        /// Called when the beginning of an object is encountered.
        ///
        /// @return `true` on success.
        /// @param ec Set to the error, if any occurred.
        ///
        bool on_object_begin( error_code& ec );

        /// Called when the end of the current object is encountered.
        ///
        /// @return `true` on success.
        /// @param n The number of elements in the object.
        /// @param ec Set to the error, if any occurred.
        ///
        bool on_object_end( std::size_t n, error_code& ec );

        /// Called with characters corresponding to part of the current string.
        ///
        /// @return `true` on success.
        /// @param s The partial characters
        /// @param n The total size of the string thus far
        /// @param ec Set to the error, if any occurred.
        ///
        bool on_string_part( string_view s, std::size_t n, error_code& ec );

        /// Called with the last characters corresponding to the current string.
        ///
        /// @return `true` on success.
        /// @param s The remaining characters
        /// @param n The total size of the string
        /// @param ec Set to the error, if any occurred.
        ///
        bool on_string( string_view s, std::size_t n, error_code& ec );

        /// Called with characters corresponding to part of the current key.
        ///
        /// @return `true` on success.
        /// @param s The partial characters
        /// @param n The total size of the key thus far
        /// @param ec Set to the error, if any occurred.
        ///
        bool on_key_part( string_view s, std::size_t n, error_code& ec );

        /// Called with the last characters corresponding to the current key.
        ///
        /// @return `true` on success.
        /// @param s The remaining characters
        /// @param n The total size of the key
        /// @param ec Set to the error, if any occurred.
        ///
        bool on_key( string_view s, std::size_t n, error_code& ec );

        /// Called with the characters corresponding to part of the current number.
        ///
        /// @return `true` on success.
        /// @param s The partial characters
        /// @param ec Set to the error, if any occurred.
        ///
        bool on_number_part( string_view s, error_code& ec );

        /// Called when a signed integer is parsed.
        ///
        /// @return `true` on success.
        /// @param i The value
        /// @param s The remaining characters
        /// @param ec Set to the error, if any occurred.
        ///
        bool on_int64( int64_t i, string_view s, error_code& ec );

        /// Called when an unsigend integer is parsed.
        ///
        /// @return `true` on success.
        /// @param u The value
        /// @param s The remaining characters
        /// @param ec Set to the error, if any occurred.
        ///
        bool on_uint64( uint64_t u, string_view s, error_code& ec );

        /// Called when a double is parsed.
        ///
        /// @return `true` on success.
        /// @param d The value
        /// @param s The remaining characters
        /// @param ec Set to the error, if any occurred.
        ///
        bool on_double( double d, string_view s, error_code& ec );

        /// Called when a boolean is parsed.
        ///
        /// @return `true` on success.
        /// @param b The value
        /// @param s The remaining characters
        /// @param ec Set to the error, if any occurred.
        ///
        bool on_bool( bool b, error_code& ec );

        /// Called when a null is parsed.
        ///
        /// @return `true` on success.
        /// @param ec Set to the error, if any occurred.
        ///
        bool on_null( error_code& ec );

        /// Called with characters corresponding to part of the current comment.
        ///
        /// @return `true` on success.
        /// @param s The partial characters.
        /// @param ec Set to the error, if any occurred.
        ///
        bool on_comment_part( string_view s, error_code& ec );

        /// Called with the last characters corresponding to the current comment.
        ///
        /// @return `true` on success.
        /// @param s The remaining characters
        /// @param ec Set to the error, if any occurred.
        ///
        bool on_comment( string_view s, error_code& ec );
    };
    @endcode

    @see
        @ref parse,
        @ref stream_parser,
        [Validating parser example](../../doc/html/json/examples.html#json.examples.validate).

    @headerfile <boost/json/basic_parser.hpp>
*/
template<class Handler>
class basic_parser
{
    enum class state : char
    {
        doc1,  doc2,  doc3, doc4,
        com1,  com2,  com3, com4,
        nul1,  nul2,  nul3,
        tru1,  tru2,  tru3,
        fal1,  fal2,  fal3,  fal4,
        str1,  str2,  str3,  str4,
        str5,  str6,  str7,  str8,
        sur1,  sur2,  sur3,
        sur4,  sur5,  sur6,
        obj1,  obj2,  obj3,  obj4,
        obj5,  obj6,  obj7,  obj8,
        obj9,  obj10, obj11,
        arr1,  arr2,  arr3,
        arr4,  arr5,  arr6,
        num1,  num2,  num3,  num4,
        num5,  num6,  num7,  num8,
        exp1,  exp2,  exp3,
        val1,  val2
    };

    struct number
    {
        uint64_t mant;
        int bias;
        int exp;
        bool frac;
        bool neg;
    };

    // optimization: must come first
    Handler h_;

    number num_;
    error_code ec_;
    detail::stack st_;
    detail::utf8_sequence seq_;
    unsigned u1_;
    unsigned u2_;
    bool more_; // false for final buffer
    bool done_ = false; // true on complete parse
    bool clean_ = true; // write_some exited cleanly
    const char* end_;
    parse_options opt_;
    // how many levels deeper the parser can go
    std::size_t depth_ = opt_.max_depth;

    inline void reserve();
    inline const char* sentinel();
    inline bool incomplete(
        const detail::const_stream_wrapper& cs);

#ifdef __INTEL_COMPILER
#pragma warning push
#pragma warning disable 2196
#endif

    BOOST_NOINLINE
    inline
    const char*
    suspend_or_fail(state st);

    BOOST_NOINLINE
    inline
    const char*
    suspend_or_fail(
        state st,
        std::size_t n);

    BOOST_NOINLINE
    inline
    const char*
    fail(const char* p) noexcept;

    BOOST_NOINLINE
    inline
    const char*
    fail(
        const char* p,
        error ev,
        source_location const* loc) noexcept;

    BOOST_NOINLINE
    inline
    const char*
    maybe_suspend(
        const char* p,
        state st);

    BOOST_NOINLINE
    inline
    const char*
    maybe_suspend(
        const char* p,
        state st,
        std::size_t n);

    BOOST_NOINLINE
    inline
    const char*
    maybe_suspend(
        const char* p,
        state st,
        const number& num);

    BOOST_NOINLINE
    inline
    const char*
    suspend(
        const char* p,
        state st);

    BOOST_NOINLINE
    inline
    const char*
    suspend(
        const char* p,
        state st,
        const number& num);

#ifdef __INTEL_COMPILER
#pragma warning pop
#endif

    template<bool StackEmpty_/*, bool Terminal_*/>
    const char* parse_comment(const char* p,
        std::integral_constant<bool, StackEmpty_> stack_empty,
        /*std::integral_constant<bool, Terminal_>*/ bool terminal);

    template<bool StackEmpty_>
    const char* parse_document(const char* p,
        std::integral_constant<bool, StackEmpty_> stack_empty);

    template<bool StackEmpty_, bool AllowComments_/*,
        bool AllowTrailing_, bool AllowBadUTF8_*/>
    const char* parse_value(const char* p,
        std::integral_constant<bool, StackEmpty_> stack_empty,
        std::integral_constant<bool, AllowComments_> allow_comments,
        /*std::integral_constant<bool, AllowTrailing_>*/ bool allow_trailing,
        /*std::integral_constant<bool, AllowBadUTF8_>*/ bool allow_bad_utf8);

    template<bool StackEmpty_, bool AllowComments_/*,
        bool AllowTrailing_, bool AllowBadUTF8_*/>
    const char* resume_value(const char* p,
        std::integral_constant<bool, StackEmpty_> stack_empty,
        std::integral_constant<bool, AllowComments_> allow_comments,
        /*std::integral_constant<bool, AllowTrailing_>*/ bool allow_trailing,
        /*std::integral_constant<bool, AllowBadUTF8_>*/ bool allow_bad_utf8);

    template<bool StackEmpty_, bool AllowComments_/*,
        bool AllowTrailing_, bool AllowBadUTF8_*/>
    const char* parse_object(const char* p,
        std::integral_constant<bool, StackEmpty_> stack_empty,
        std::integral_constant<bool, AllowComments_> allow_comments,
        /*std::integral_constant<bool, AllowTrailing_>*/ bool allow_trailing,
        /*std::integral_constant<bool, AllowBadUTF8_>*/ bool allow_bad_utf8);

    template<bool StackEmpty_, bool AllowComments_/*,
        bool AllowTrailing_, bool AllowBadUTF8_*/>
    const char* parse_array(const char* p,
        std::integral_constant<bool, StackEmpty_> stack_empty,
        std::integral_constant<bool, AllowComments_> allow_comments,
        /*std::integral_constant<bool, AllowTrailing_>*/ bool allow_trailing,
        /*std::integral_constant<bool, AllowBadUTF8_>*/ bool allow_bad_utf8);

    template<bool StackEmpty_>
    const char* parse_null(const char* p,
        std::integral_constant<bool, StackEmpty_> stack_empty);

    template<bool StackEmpty_>
    const char* parse_true(const char* p,
        std::integral_constant<bool, StackEmpty_> stack_empty);

    template<bool StackEmpty_>
    const char* parse_false(const char* p,
        std::integral_constant<bool, StackEmpty_> stack_empty);

    template<bool StackEmpty_, bool IsKey_/*,
        bool AllowBadUTF8_*/>
    const char* parse_string(const char* p,
        std::integral_constant<bool, StackEmpty_> stack_empty,
        std::integral_constant<bool, IsKey_> is_key,
        /*std::integral_constant<bool, AllowBadUTF8_>*/ bool allow_bad_utf8);

    template<bool StackEmpty_, char First_>
    const char* parse_number(const char* p,
        std::integral_constant<bool, StackEmpty_> stack_empty,
        std::integral_constant<char, First_> first);

    template<bool StackEmpty_, bool IsKey_/*,
        bool AllowBadUTF8_*/>
    const char* parse_unescaped(const char* p,
        std::integral_constant<bool, StackEmpty_> stack_empty,
        std::integral_constant<bool, IsKey_> is_key,
        /*std::integral_constant<bool, AllowBadUTF8_>*/ bool allow_bad_utf8);

    template<bool StackEmpty_/*, bool IsKey_,
        bool AllowBadUTF8_*/>
    const char* parse_escaped(
        const char* p,
        std::size_t total,
        std::integral_constant<bool, StackEmpty_> stack_empty,
        /*std::integral_constant<bool, IsKey_>*/ bool is_key,
        /*std::integral_constant<bool, AllowBadUTF8_>*/ bool allow_bad_utf8);

    // intentionally private
    std::size_t
    depth() const noexcept
    {
        return opt_.max_depth - depth_;
    }

public:
    /// Copy constructor (deleted)
    basic_parser(
        basic_parser const&) = delete;

    /// Copy assignment (deleted)
    basic_parser& operator=(
        basic_parser const&) = delete;

    /** Destructor.

        All dynamically allocated internal memory is freed.

        @par Effects
        @code
        this->handler().~Handler()
        @endcode

        @par Complexity
        Same as `~Handler()`.

        @par Exception Safety
        Same as `~Handler()`.
    */
    ~basic_parser() = default;

    /** Constructor.

        This function constructs the parser with
        the specified options, with any additional
        arguments forwarded to the handler's constructor.

        @par Complexity
        Same as `Handler( std::forward< Args >( args )... )`.

        @par Exception Safety
        Same as `Handler( std::forward< Args >( args )... )`.

        @param opt Configuration settings for the parser.
        If this structure is default constructed, the
        parser will accept only standard JSON.

        @param args Optional additional arguments
        forwarded to the handler's constructor.

        @see parse_options
    */
    template<class... Args>
    explicit
    basic_parser(
        parse_options const& opt,
        Args&&... args);

    /** Return a reference to the handler.

        This function provides access to the constructed
        instance of the handler owned by the parser.

        @par Complexity
        Constant.

        @par Exception Safety
        No-throw guarantee.
    */
    Handler&
    handler() noexcept
    {
        return h_;
    }

    /** Return a reference to the handler.

        This function provides access to the constructed
        instance of the handler owned by the parser.

        @par Complexity
        Constant.

        @par Exception Safety
        No-throw guarantee.
    */
    Handler const&
    handler() const noexcept
    {
        return h_;
    }

    /** Return the last error.

        This returns the last error code which
        was generated in the most recent call
        to @ref write_some.

        @par Complexity
        Constant.

        @par Exception Safety
        No-throw guarantee.
    */
    error_code
    last_error() const noexcept
    {
        return ec_;
    }

    /** Return true if a complete JSON has been parsed.

        This function returns `true` when all of these
        conditions are met:

        @li A complete serialized JSON has been
            presented to the parser, and

        @li No error or exception has occurred since the
            parser was constructed, or since the last call
            to @ref reset,

        @par Complexity
        Constant.

        @par Exception Safety
        No-throw guarantee.
    */
    bool
    done() const noexcept
    {
        return done_;
    }

    /** Reset the state, to parse a new document.

        This function discards the current parsing
        state, to prepare for parsing a new document.
        Dynamically allocated temporary memory used
        by the implementation is not deallocated.

        @par Complexity
        Constant.

        @par Exception Safety
        No-throw guarantee.
    */
    void
    reset() noexcept;

    /** Indicate a parsing failure.

        This changes the state of the parser to indicate
        that the parse has failed. A parser implementation
        can use this to fail the parser if needed due to
        external inputs.

        @note

        If `!ec`, the stored error code is unspecified.

        @par Complexity
        Constant.

        @par Exception Safety
        No-throw guarantee.

        @param ec The error code to set. If the code does
        not indicate failure, an implementation-defined
        error code that indicates failure will be stored
        instead.
    */
    void
    fail(error_code ec) noexcept;

    /** Parse some of an input string as JSON, incrementally.

        This function parses the JSON in the specified
        buffer, calling the handler to emit each SAX
        parsing event. The parse proceeds from the
        current state, which is at the beginning of a
        new JSON or in the middle of the current JSON
        if any characters were already parsed.
    \n
        The characters in the buffer are processed
        starting from the beginning, until one of the
        following conditions is met:

        @li All of the characters in the buffer
        have been parsed, or

        @li Some of the characters in the buffer
        have been parsed and the JSON is complete, or

        @li A parsing error occurs.

        The supplied buffer does not need to contain the
        entire JSON. Subsequent calls can provide more
        serialized data, allowing JSON to be processed
        incrementally. The end of the serialized JSON
        can be indicated by passing `more = false`.

        @par Complexity
        Linear in `size`.

        @par Exception Safety
        Basic guarantee.
        Calls to the handler may throw.
        Upon error or exception, subsequent calls will
        fail until @ref reset is called to parse a new JSON.

        @return The number of characters successfully
        parsed, which may be smaller than `size`.

        @param more `true` if there are possibly more
        buffers in the current JSON, otherwise `false`.

        @param data A pointer to a buffer of `size`
        characters to parse.

        @param size The number of characters pointed to
        by `data`.

        @param ec Set to the error, if any occurred.
    */
/** @{ */
    std::size_t
    write_some(
        bool more,
        char const* data,
        std::size_t size,
        error_code& ec);

    std::size_t
    write_some(
        bool more,
        char const* data,
        std::size_t size,
        std::error_code& ec);
/** @} */
};

BOOST_JSON_NS_END

#endif