32 lexer::lexer(
reader &r,
size_t max_token_length) :
34 m_last_put_back(false),
35 m_max_token_length(max_token_length),
36 m_buffer(new char[max_token_length])
45 void lexer::consume_white_space()
49 int c = m_reader.
next();
51 if ((c == 0x20) || (c == 0x09) || (c == 0x0A) || (c == 0x0D))
63 void lexer::read_token()
65 consume_white_space();
66 int c = m_reader.
next();
119 void lexer::append_to_number_buffer(
char *s,
size_t &index,
int c)
121 if (index >= m_max_token_length)
132 void lexer::throw_number_exception(
int c)
142 static_cast<char>(c),
147 size_t lexer::read_digits(
size_t &index)
149 size_t num_digits = 0;
153 int c = m_reader.
next();
156 append_to_number_buffer(m_buffer, index, c);
167 void lexer::read_number()
169 int c = m_reader.
next();
172 bool is_double =
false;
173 size_t num_int_digits = 0;
178 append_to_number_buffer(m_buffer, n, c);
183 append_to_number_buffer(m_buffer, n, c);
187 throw_number_exception(c);
197 append_to_number_buffer(m_buffer, n, c);
207 if (num_int_digits == 0)
210 throw_number_exception(c);
212 else if (num_int_digits > 1 && m_buffer[n - num_int_digits] ==
'0')
216 throw_number_exception(
'0');
224 append_to_number_buffer(m_buffer, n, c);
228 if (read_digits(n) == 0)
230 throw_number_exception(c);
237 if (c ==
'e' || c ==
'E')
239 append_to_number_buffer(m_buffer, n, c);
245 if (c ==
'+' || c ==
'-')
247 append_to_number_buffer(m_buffer, n, c);
250 if (read_digits(n) == 0)
252 throw_number_exception(c);
257 throw_number_exception(c);
268 void lexer::read_string()
271 bool in_escape =
false;
282 static_cast<char>(c),
285 else if (c ==
'"' && !in_escape)
292 if (n >= m_max_token_length)
304 in_escape = (c ==
'\\');
308 void lexer::read_false()
310 read_matching(
"alse");
314 void lexer::read_true()
316 read_matching(
"rue");
320 void lexer::read_null()
322 read_matching(
"ull");
326 void lexer::read_matching(
const char *s)
331 char c = m_reader.
next();
344 m_last_put_back =
false;
356 m_last_put_back =
true;
The json_parser_exception class.
Exception class for parser errors.
A class to read JSON messages from various types of input stream.
virtual int next()
Get the next character from the reader.
#define NAMESPACE
You can change the namespace of the whole library by changing this value.
A string had too many characters in it.
A number had too many characters in it.
Common defs needed everywhere and, as far as is possible, platform specific changes.
The file ended where more tokens were expected.
Lexical tokens read from a JSON message.
size_t get_byte_index() const
Get the current byte index in the input.
Something that looked like a number ended up not being (e.g. 1234AAAA).
An invalid character was found during parsing (e.g. ! outside of a string).
virtual void put_back(int c)
Put back a character so that it is returned by the next call to next().