LCOV - code coverage report
Current view: top level - libutf8 - json_tokens.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1 1 100.0 %
Date: 2022-07-31 10:17:08 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2000-2022  Made to Order Software Corp.  All Rights Reserved
       2             : //
       3             : // https://snapwebsites.org/project/libutf8
       4             : // contact@m2osw.com
       5             : //
       6             : // This program is free software; you can redistribute it and/or modify
       7             : // it under the terms of the GNU General Public License as published by
       8             : // the Free Software Foundation; either version 2 of the License, or
       9             : // (at your option) any later version.
      10             : //
      11             : // This program is distributed in the hope that it will be useful,
      12             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             : // GNU General Public License for more details.
      15             : //
      16             : // You should have received a copy of the GNU General Public License along
      17             : // with this program; if not, write to the Free Software Foundation, Inc.,
      18             : // 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
      19             : #pragma once
      20             : 
      21             : /** \file
      22             :  * \brief The declarations of the JSON tokens class.
      23             :  *
      24             :  * This file is the declarations of the JSON tokens class one can use to
      25             :  * interpret the contents of a JSON file.
      26             :  *
      27             :  * The idea of this simple parser is to (1) show how one can use the
      28             :  * libutf8 library and (2) give you the ability to parse a simple JSON
      29             :  * structure.
      30             :  */
      31             : 
      32             : // self
      33             : //
      34             : #include    <libutf8/iterator.h>
      35             : 
      36             : 
      37             : // C++
      38             : //
      39             : #include    <cmath>
      40             : #include    <string>
      41             : 
      42             : 
      43             : 
      44             : namespace libutf8
      45             : {
      46             : 
      47             : 
      48             : enum class token_t
      49             : {
      50             :     TOKEN_END,
      51             :     TOKEN_ERROR,
      52             :     TOKEN_OPEN_ARRAY,
      53             :     TOKEN_CLOSE_ARRAY,
      54             :     TOKEN_OPEN_OBJECT,
      55             :     TOKEN_CLOSE_OBJECT,
      56             :     TOKEN_NUMBER,
      57             :     TOKEN_STRING,
      58             :     TOKEN_COMMA,
      59             :     TOKEN_COLON,
      60             :     TOKEN_TRUE,
      61             :     TOKEN_FALSE,
      62             :     TOKEN_NULL,
      63             : };
      64             : 
      65             : 
      66     2225164 : class json_tokens
      67             : {
      68             : public:
      69             :                         json_tokens(std::string const & input);
      70             : 
      71             :     int                 line() const;
      72             :     int                 column() const;
      73             :     token_t             next_token();
      74             :     double              number() const;
      75             :     std::string const & string() const;
      76             :     std::string const & error() const;
      77             : 
      78             : private:
      79             :     char32_t            getc();
      80             :     void                ungetc(char32_t c);
      81             :     char32_t            char16(char32_t & c);
      82             :     void                add_error_character(char32_t c);
      83             : 
      84             :     std::string         f_input = std::string();
      85             :     utf8_iterator       f_iterator; // initialize in the constructor
      86             :     char32_t            f_unget[16];
      87             :     std::size_t         f_unget_pos = 0;
      88             :     std::uint32_t       f_line = 1;
      89             :     std::uint32_t       f_last_line = 0;
      90             :     std::uint32_t       f_column = 1;
      91             :     std::uint32_t       f_last_column = 0;
      92             :     double              f_number = 0.0;
      93             :     std::string         f_string = std::string();
      94             :     std::string         f_error = std::string();
      95             : };
      96             : 
      97             : 
      98             : } // libutf8 namespace
      99             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13