Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
json
Commits
6ef3cb51
Commit
6ef3cb51
authored
Feb 14, 2015
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
some reorganization
parent
cc4a8319
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
665 additions
and
646 deletions
+665
-646
src/json.hpp
src/json.hpp
+571
-572
src/json.hpp.re2c
src/json.hpp.re2c
+76
-74
test/unit.cpp
test/unit.cpp
+18
-0
No files found.
src/json.hpp
View file @
6ef3cb51
This diff is collapsed.
Click to expand it.
src/json.hpp.re2c
View file @
6ef3cb51
...
@@ -2516,68 +2516,73 @@ class basic_json
...
@@ -2516,68 +2516,73 @@ class basic_json
// pointer for backtracking information
// pointer for backtracking information
const char* m_marker = nullptr;
const char* m_marker = nullptr;
// remember the begin of the token
while (true)
m_start = m_cursor;
{
// remember the begin of the token
/*!re2c
m_start = m_cursor;
re2c:define:YYCTYPE = char;
re2c:define:YYCURSOR = m_cursor;
/*!re2c
re2c:define:YYLIMIT = m_limit;
re2c:define:YYCTYPE = char;
re2c:define:YYMARKER = m_marker;
re2c:define:YYCURSOR = m_cursor;
re2c:indent:string = " ";
re2c:define:YYLIMIT = m_limit;
re2c:indent:top = 1;
re2c:define:YYMARKER = m_marker;
re2c:labelprefix = "json_parser_";
re2c:indent:string = " ";
re2c:yyfill:enable = 0;
re2c:indent:top = 1;
re2c:labelprefix = "basic_json_parser_";
// whitespace
re2c:yyfill:enable = 0;
ws = [ \t\n\r]*;
ws { return scan(); }
// whitespace
ws = [ \t\n\r]+;
// structural characters
ws { continue; }
"[" { return token_type::begin_array; }
"]" { return token_type::end_array; }
// structural characters
"{" { return token_type::begin_object; }
"[" { return token_type::begin_array; }
"}" { return token_type::end_object; }
"]" { return token_type::end_array; }
"," { return token_type::value_separator; }
"{" { return token_type::begin_object; }
":" { return token_type::name_separator; }
"}" { return token_type::end_object; }
"," { return token_type::value_separator; }
// literal names
":" { return token_type::name_separator; }
"null" { return token_type::literal_null; }
"true" { return token_type::literal_true; }
// literal names
"false" { return token_type::literal_false; }
"null" { return token_type::literal_null; }
"true" { return token_type::literal_true; }
// number
"false" { return token_type::literal_false; }
decimal_point = [.];
digit = [0-9];
// number
digit_1_9 = [1-9];
decimal_point = [.];
e = [eE];
digit = [0-9];
minus = [-];
digit_1_9 = [1-9];
plus = [+];
e = [eE];
zero = [0];
minus = [-];
exp = e (minus|plus)? digit+;
plus = [+];
frac = decimal_point digit+;
zero = [0];
int = (zero|digit_1_9 digit*);
exp = e (minus|plus)? digit+;
number = minus? int frac? exp?;
frac = decimal_point digit+;
number { return token_type::value_number; }
int = (zero|digit_1_9 digit*);
number = minus? int frac? exp?;
// string
number { return token_type::value_number; }
quotation_mark = [\"];
escape = [\\];
// string
unescaped = [^\"\\\000];
quotation_mark = [\"];
escaped = escape ([\"\\/bfnrt] | [u][0-9a-fA-F]{4});
escape = [\\];
char = unescaped | escaped;
unescaped = [^\"\\\000];
string = quotation_mark char* quotation_mark;
single_escaped = [\"\\/bfnrt];
string { return token_type::value_string; }
unicode_escaped = [u][0-9a-fA-F]{4};
escaped = escape (single_escaped | unicode_escaped);
// end of file
char = unescaped | escaped;
'\000' { return token_type::end_of_input; }
string = quotation_mark char* quotation_mark;
string { return token_type::value_string; }
// anything else is an error
. { return token_type::parse_error; }
// end of file
*/
'\000' { return token_type::end_of_input; }
}
// anything else is an error
inline std::string get_string_value() const
. { return token_type::parse_error; }
*/
}
}
inline std::string get_token() const
{
{
return std::string(m_start, static_cast<size_t>(m_cursor - m_start));
return std::string(m_start, static_cast<size_t>(m_cursor - m_start));
}
}
...
@@ -2612,15 +2617,9 @@ class basic_json
...
@@ -2612,15 +2617,9 @@ class basic_json
char* endptr;
char* endptr;
const auto float_val = std::strtod(reinterpret_cast<const char*>(m_start), &endptr);
const auto float_val = std::strtod(reinterpret_cast<const char*>(m_start), &endptr);
// check if strtod read beyond the end of the lexem
// return float_val if the whole number was translated and NAN
if (endptr != m_cursor)
// otherwise
{
return (endptr == m_cursor) ? float_val : NAN;
return NAN;
}
else
{
return float_val;
}
}
}
private:
private:
...
@@ -2787,10 +2786,12 @@ class basic_json
...
@@ -2787,10 +2786,12 @@ class basic_json
{
{
auto float_val = m_lexer.get_number();
auto float_val = m_lexer.get_number();
// NAN is returned if token could not be translated
// completely
if (std::isnan(float_val))
if (std::isnan(float_val))
{
{
throw std::invalid_argument(std::string("parse error - ") +
throw std::invalid_argument(std::string("parse error - ") +
m_lexer.get_
string_value
() + " is not a number");
m_lexer.get_
token
() + " is not a number");
}
}
get_token();
get_token();
...
@@ -2812,7 +2813,7 @@ class basic_json
...
@@ -2812,7 +2813,7 @@ class basic_json
default:
default:
{
{
std::string error_msg = "parse error - unexpected \'";
std::string error_msg = "parse error - unexpected \'";
error_msg += m_lexer.get_
string_value
();
error_msg += m_lexer.get_
token
();
error_msg += "\' (";
error_msg += "\' (";
error_msg += lexer::token_type_name(last_token) + ")";
error_msg += lexer::token_type_name(last_token) + ")";
throw std::invalid_argument(error_msg);
throw std::invalid_argument(error_msg);
...
@@ -2832,7 +2833,7 @@ class basic_json
...
@@ -2832,7 +2833,7 @@ class basic_json
if (t != last_token)
if (t != last_token)
{
{
std::string error_msg = "parse error - unexpected \'";
std::string error_msg = "parse error - unexpected \'";
error_msg += m_lexer.get_
string_value
();
error_msg += m_lexer.get_
token
();
error_msg += "\' (" + lexer::token_type_name(last_token);
error_msg += "\' (" + lexer::token_type_name(last_token);
error_msg += "); expected " + lexer::token_type_name(t);
error_msg += "); expected " + lexer::token_type_name(t);
throw std::invalid_argument(error_msg);
throw std::invalid_argument(error_msg);
...
@@ -2844,6 +2845,7 @@ class basic_json
...
@@ -2844,6 +2845,7 @@ class basic_json
std::string m_buffer;
std::string m_buffer;
/// the type of the last read token
/// the type of the last read token
typename lexer::token_type last_token = lexer::token_type::uninitialized;
typename lexer::token_type last_token = lexer::token_type::uninitialized;
/// the lexer
lexer m_lexer;
lexer m_lexer;
};
};
};
};
...
...
test/unit.cpp
View file @
6ef3cb51
...
@@ -3630,6 +3630,8 @@ TEST_CASE("capacity")
...
@@ -3630,6 +3630,8 @@ TEST_CASE("capacity")
{
{
CHECK
(
std
::
distance
(
j
.
begin
(),
j
.
end
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j
.
begin
(),
j
.
end
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j_const
.
begin
(),
j_const
.
end
())
==
j_const
.
size
());
CHECK
(
std
::
distance
(
j_const
.
begin
(),
j_const
.
end
())
==
j_const
.
size
());
CHECK
(
std
::
distance
(
j
.
rbegin
(),
j
.
rend
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j_const
.
crbegin
(),
j_const
.
crend
())
==
j_const
.
size
());
}
}
}
}
...
@@ -3648,6 +3650,8 @@ TEST_CASE("capacity")
...
@@ -3648,6 +3650,8 @@ TEST_CASE("capacity")
{
{
CHECK
(
std
::
distance
(
j
.
begin
(),
j
.
end
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j
.
begin
(),
j
.
end
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j_const
.
begin
(),
j_const
.
end
())
==
j_const
.
size
());
CHECK
(
std
::
distance
(
j_const
.
begin
(),
j_const
.
end
())
==
j_const
.
size
());
CHECK
(
std
::
distance
(
j
.
rbegin
(),
j
.
rend
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j_const
.
crbegin
(),
j_const
.
crend
())
==
j_const
.
size
());
}
}
}
}
...
@@ -3668,6 +3672,8 @@ TEST_CASE("capacity")
...
@@ -3668,6 +3672,8 @@ TEST_CASE("capacity")
{
{
CHECK
(
std
::
distance
(
j
.
begin
(),
j
.
end
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j
.
begin
(),
j
.
end
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j_const
.
begin
(),
j_const
.
end
())
==
j_const
.
size
());
CHECK
(
std
::
distance
(
j_const
.
begin
(),
j_const
.
end
())
==
j_const
.
size
());
CHECK
(
std
::
distance
(
j
.
rbegin
(),
j
.
rend
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j_const
.
crbegin
(),
j_const
.
crend
())
==
j_const
.
size
());
}
}
}
}
...
@@ -3686,6 +3692,8 @@ TEST_CASE("capacity")
...
@@ -3686,6 +3692,8 @@ TEST_CASE("capacity")
{
{
CHECK
(
std
::
distance
(
j
.
begin
(),
j
.
end
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j
.
begin
(),
j
.
end
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j_const
.
begin
(),
j_const
.
end
())
==
j_const
.
size
());
CHECK
(
std
::
distance
(
j_const
.
begin
(),
j_const
.
end
())
==
j_const
.
size
());
CHECK
(
std
::
distance
(
j
.
rbegin
(),
j
.
rend
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j_const
.
crbegin
(),
j_const
.
crend
())
==
j_const
.
size
());
}
}
}
}
}
}
...
@@ -3707,6 +3715,8 @@ TEST_CASE("capacity")
...
@@ -3707,6 +3715,8 @@ TEST_CASE("capacity")
{
{
CHECK
(
std
::
distance
(
j
.
begin
(),
j
.
end
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j
.
begin
(),
j
.
end
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j_const
.
begin
(),
j_const
.
end
())
==
j_const
.
size
());
CHECK
(
std
::
distance
(
j_const
.
begin
(),
j_const
.
end
())
==
j_const
.
size
());
CHECK
(
std
::
distance
(
j
.
rbegin
(),
j
.
rend
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j_const
.
crbegin
(),
j_const
.
crend
())
==
j_const
.
size
());
}
}
}
}
...
@@ -3725,6 +3735,8 @@ TEST_CASE("capacity")
...
@@ -3725,6 +3735,8 @@ TEST_CASE("capacity")
{
{
CHECK
(
std
::
distance
(
j
.
begin
(),
j
.
end
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j
.
begin
(),
j
.
end
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j_const
.
begin
(),
j_const
.
end
())
==
j_const
.
size
());
CHECK
(
std
::
distance
(
j_const
.
begin
(),
j_const
.
end
())
==
j_const
.
size
());
CHECK
(
std
::
distance
(
j
.
rbegin
(),
j
.
rend
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j_const
.
crbegin
(),
j_const
.
crend
())
==
j_const
.
size
());
}
}
}
}
}
}
...
@@ -3744,6 +3756,8 @@ TEST_CASE("capacity")
...
@@ -3744,6 +3756,8 @@ TEST_CASE("capacity")
{
{
CHECK
(
std
::
distance
(
j
.
begin
(),
j
.
end
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j
.
begin
(),
j
.
end
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j_const
.
begin
(),
j_const
.
end
())
==
j_const
.
size
());
CHECK
(
std
::
distance
(
j_const
.
begin
(),
j_const
.
end
())
==
j_const
.
size
());
CHECK
(
std
::
distance
(
j
.
rbegin
(),
j
.
rend
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j_const
.
crbegin
(),
j_const
.
crend
())
==
j_const
.
size
());
}
}
}
}
...
@@ -3762,6 +3776,8 @@ TEST_CASE("capacity")
...
@@ -3762,6 +3776,8 @@ TEST_CASE("capacity")
{
{
CHECK
(
std
::
distance
(
j
.
begin
(),
j
.
end
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j
.
begin
(),
j
.
end
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j_const
.
begin
(),
j_const
.
end
())
==
j_const
.
size
());
CHECK
(
std
::
distance
(
j_const
.
begin
(),
j_const
.
end
())
==
j_const
.
size
());
CHECK
(
std
::
distance
(
j
.
rbegin
(),
j
.
rend
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j_const
.
crbegin
(),
j_const
.
crend
())
==
j_const
.
size
());
}
}
}
}
...
@@ -3780,6 +3796,8 @@ TEST_CASE("capacity")
...
@@ -3780,6 +3796,8 @@ TEST_CASE("capacity")
{
{
CHECK
(
std
::
distance
(
j
.
begin
(),
j
.
end
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j
.
begin
(),
j
.
end
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j_const
.
begin
(),
j_const
.
end
())
==
j_const
.
size
());
CHECK
(
std
::
distance
(
j_const
.
begin
(),
j_const
.
end
())
==
j_const
.
size
());
CHECK
(
std
::
distance
(
j
.
rbegin
(),
j
.
rend
())
==
j
.
size
());
CHECK
(
std
::
distance
(
j_const
.
crbegin
(),
j_const
.
crend
())
==
j_const
.
size
());
}
}
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment