Awali
Another Weighted Automata library
parser.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2021 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  void ignore_spaces();
56 
57  bool check(char e, std::string oth = "", std::unordered_map<char,std::string> names_override = {});
58  bool inline check(char e, std::unordered_map<char,std::string> names_override)
59  {return check(e, "", names_override);}
60 
61  int peek();
62  std::string extract_and_unescape_string(bool quote = true);
63 
65  bool _error;
66 
68  std::stringstream _error_message;
69 
72  std::list<uint_or_string_t> _error_path;
73 
74  std::istream& _in;
75 public:
76 
77  inline bool error() { return _error; }
78  inline std::string error_message() { return _error_message.str(); }
79 
80  /* Can only be called once. */
81  inline std::list<uint_or_string_t> move_error_path()
82  { return std::move(_error_path); }
83 
85  parser_t(std::istream& i) : _error(false), _in(i) {}
86 
87  inline position_t position() { return position_of(_in.tellg()); };
88 
89 
90  static std::ostream& escape_and_put(std::ostream&, std::string const&);
91  static std::string const& escape(char);
92  static std::string escape(std::string const&);
93  static std::string unescape(std::string const&, bool quote = false);
94 };
95 
96 
97 node_t* parse(std::istream&);
98 
99 }// end of namespace awali::json
100 }// end of namespace awali
101 
102 #endif
Definition: node.hh:191
Definition: parser.hh:28
bool error()
Definition: parser.hh:77
parser_t(std::istream &i)
Definition: parser.hh:85
std::list< uint_or_string_t > _error_path
Attempt to give localisation of parsing error (only set if an error occured).
Definition: parser.hh:72
static std::string const & escape(char)
std::istream & _in
Definition: parser.hh:74
position_t position_of(int pos)
bool check(char e, std::unordered_map< char, std::string > names_override)
Definition: parser.hh:58
static std::string unescape(std::string const &, bool quote=false)
static std::string const true_repr
Definition: parser.hh:42
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:68
static std::string const false_repr
Definition: parser.hh:43
static std::ostream & escape_and_put(std::ostream &, std::string const &)
bool _error
Whether on error occured.
Definition: parser.hh:65
bool check(char e, std::string oth="", std::unordered_map< char, std::string > names_override={})
static std::string escape(std::string const &)
position_t position()
Definition: parser.hh:87
node_t * parse_constant(std::string const &)
std::list< uint_or_string_t > move_error_path()
Definition: parser.hh:81
std::string error_message()
Definition: parser.hh:78
std::ostream & json(automaton_t aut, std::ostream &out)
node_t * parse(std::istream &)
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