1 ////////////////////////////////////////////////////////////////////////////////
3 // Copyright 2006 - 2017, Paul Beckingham, Federico Hernandez.
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included
13 // in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // http://www.opensource.org/licenses/mit-license.php
25 ////////////////////////////////////////////////////////////////////////////////
52 static value* parse (Pig&);
53 virtual jtype type ();
54 virtual std::string dump () const;
57 class string : public value
61 string (const std::string&);
63 static string* parse (Pig&);
65 std::string dump () const;
71 class number : public value
74 number () : _dvalue (0.0) {}
76 static number* parse (Pig&);
78 std::string dump () const;
79 operator double () const;
85 class literal : public value
88 literal () : _lvalue (none) {}
90 static literal* parse (Pig&);
92 std::string dump () const;
95 enum literal_value {none, nullvalue, falsevalue, truevalue};
96 literal_value _lvalue;
99 class array : public value
104 static array* parse (Pig&);
106 std::string dump () const;
109 std::vector <value*> _data;
112 class object : public value
117 static object* parse (Pig&);
118 static bool parse_pair (Pig&, std::string&, value*&);
120 std::string dump () const;
123 std::map <std::string, value*> _data;
126 // Parser entry point.
127 value* parse (const std::string&);
129 // Encode/decode for JSON entities.
130 std::string encode (const std::string&);
131 std::string decode (const std::string&);
139 virtual void eventDocStart () {}
140 virtual void eventDocEnd () {}
141 virtual void eventObjectStart () {}
142 virtual void eventObjectEnd (int) {}
143 virtual void eventArrayStart () {}
144 virtual void eventArrayEnd (int) {}
145 virtual void eventName (const std::string&) {}
146 virtual void eventValueNull () {}
147 virtual void eventValueBool (bool) {}
148 virtual void eventValueInt (int64_t) {}
149 virtual void eventValueUint (uint64_t) {}
150 virtual void eventValueDouble (double) {}
151 virtual void eventValueString (const std::string&) {}
154 bool parse (const std::string&, json::SAX::Sink&);
157 void ignoreWhitespace (const std::string&, std::string::size_type&);
158 bool isObject (const std::string&, std::string::size_type&, SAX::Sink&);
159 bool isArray (const std::string&, std::string::size_type&, SAX::Sink&);
160 bool isPair (const std::string&, std::string::size_type&, SAX::Sink&);
161 bool isValue (const std::string&, std::string::size_type&, SAX::Sink&);
162 bool isKey (const std::string&, std::string::size_type&, SAX::Sink&);
163 bool isString (const std::string&, std::string::size_type&, SAX::Sink&);
164 bool isStringValue (const std::string&, std::string::size_type&, std::string&);
165 bool isNumber (const std::string&, std::string::size_type&, SAX::Sink&);
166 bool isInt (const std::string&, std::string::size_type&, std::string&);
167 bool isFrac (const std::string&, std::string::size_type&, std::string&);
168 bool isDigits (const std::string&, std::string::size_type&);
169 bool isDecDigit (int);
170 bool isHexDigit (int);
172 int hexToInt (int, int, int, int);
173 bool isExp (const std::string&, std::string::size_type&, std::string&);
174 bool isE (const std::string&, std::string::size_type&);
175 bool isBool (const std::string&, std::string::size_type&, SAX::Sink&);
176 bool isNull (const std::string&, std::string::size_type&, SAX::Sink&);
177 bool isLiteral (const std::string&, char, std::string::size_type&);
178 void error (const std::string&, std::string::size_type);
183 ////////////////////////////////////////////////////////////////////////////////