Awali
Another Weighted Automata library
parser.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef COMMON_JSON_PARSER_HH
18 #define COMMON_JSON_PARSER_HH
19 
20 #include<unordered_map>
21 #include<iostream>
22 
24 
25 namespace awali {
26 namespace json {
27 
28 class parser_t {
29 public:
30  struct position_t {
32  int line;
33  int column;
34 
35  static inline position_t tellg_unsupported() { return {-1,0}; }
36  inline bool is_tellg_unsupported() { return (line == -1); }
37  static inline position_t eof() { return {-2,0}; }
38  inline bool is_eof() { return (line == -2); }
39  };
40 protected:
41  static std::string const null_repr;
42  static std::string const true_repr;
43  static std::string const false_repr;
44 
45 
50  node_t* parse_constant(std::string const&);
51 
52 
54 
55  bool check(char e, std::string const& oth = "", std::unordered_map<char,std::string> const& names_override = {});
56  bool inline check(char e, std::unordered_map<char,std::string> const& names_override)
57  {return check(e, "", names_override);}
58 
59  std::string extract_and_unescape_string(bool quote = true);
60 
62  bool _error;
63 
65  std::stringstream _error_message;
66 
69  std::list<uint_or_string_t> _error_path;
70 
71  std::istream& _in;
72 
74  static int const none = -3455678;
76 
77  int peek(bool ignore_spaces = true);
78  int get(bool ignore_spaces = true);
79  void unget(int c);
81 
82 public:
83 
84  inline bool error() { return _error; }
85  inline std::string error_message() { return _error_message.str(); }
86 
87  /* Can only be called once. */
88  inline std::list<uint_or_string_t> move_error_path()
89  { return std::move(_error_path); }
90 
92  parser_t(std::istream& i, bool only_metadata = false) : _error(false), _in(i), _only_metadata(only_metadata) {}
93 
94 
95  inline position_t position() { return position_of(_in.tellg()); };
96 
97 
98  static std::ostream& escape_and_put(std::ostream&, std::string const&);
99  static std::string const& escape(char);
100  static std::string escape(std::string const&);
101  static std::string unescape(std::string const&, bool quote = false);
102 };
103 
104 
105 node_t* parse(std::istream&, bool stop_after_metadata= false);
106 
107 }// end of namespace awali::json
108 }// end of namespace awali
109 
110 #endif
Definition: node.hh:193
Definition: parser.hh:28
bool error()
Definition: parser.hh:84
int _current_char
Definition: parser.hh:75
bool _only_metadata
Definition: parser.hh:73
std::list< uint_or_string_t > _error_path
Attempt to give localisation of parsing error (only set if an error occured).
Definition: parser.hh:69
static std::string const & escape(char)
bool check(char e, std::unordered_map< char, std::string > const &names_override)
Definition: parser.hh:56
std::istream & _in
Definition: parser.hh:71
int get(bool ignore_spaces=true)
bool check(char e, std::string const &oth="", std::unordered_map< char, std::string > const &names_override={})
position_t position_of(int pos)
int peek(bool ignore_spaces=true)
static std::string unescape(std::string const &, bool quote=false)
static std::string const true_repr
Definition: parser.hh:42
static int const none
Definition: parser.hh:74
std::string extract_and_unescape_string(bool quote=true)
static std::string const null_repr
Definition: parser.hh:41
std::stringstream _error_message
Error message if on error occured.
Definition: parser.hh:65
static std::string const false_repr
Definition: parser.hh:43
parser_t(std::istream &i, bool only_metadata=false)
Definition: parser.hh:92
static std::ostream & escape_and_put(std::ostream &, std::string const &)
bool _error
Whether on error occured.
Definition: parser.hh:62
static std::string escape(std::string const &)
position_t position()
Definition: parser.hh:95
node_t * parse_constant(std::string const &)
std::list< uint_or_string_t > move_error_path()
Definition: parser.hh:88
std::string error_message()
Definition: parser.hh:85
std::ostream & json(automaton_t aut, std::ostream &out)
node_t * parse(std::istream &, bool stop_after_metadata=false)
Main namespace of Awali.
Definition: ato.hh:22
Definition: parser.hh:30
int line
Only positive values are allowed.
Definition: parser.hh:32
static position_t tellg_unsupported()
Definition: parser.hh:35
bool is_tellg_unsupported()
Definition: parser.hh:36
int column
Definition: parser.hh:33
static position_t eof()
Definition: parser.hh:37
bool is_eof()
Definition: parser.hh:38