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
89a1b024
Commit
89a1b024
authored
Feb 11, 2015
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed a major bug in the parser
parent
3f8dc632
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
425 additions
and
360 deletions
+425
-360
src/json.hpp
src/json.hpp
+376
-345
src/json.hpp.re2c
src/json.hpp.re2c
+41
-15
test/unit.cpp
test/unit.cpp
+8
-0
No files found.
src/json.hpp
View file @
89a1b024
This diff is collapsed.
Click to expand it.
src/json.hpp.re2c
View file @
89a1b024
...
...
@@ -2524,7 +2524,7 @@ class basic_json
'\000' { return token_type::end_of_input; }
// anything else is an error
*
{ return token_type::parse_error; }
.
{ return token_type::parse_error; }
*/
}
...
...
@@ -2612,7 +2612,19 @@ class basic_json
get_token();
}
/// public parser interface
inline basic_json parse()
{
basic_json result = parse_internal();
expect(lexer::token_type::end_of_input);
return result;
}
private:
/// the actual parser
inline basic_json parse_internal()
{
switch (last_token)
{
...
...
@@ -2627,12 +2639,19 @@ class basic_json
// closing } -> we are done
if (last_token == lexer::token_type::end_object)
{
get_token();
return result;
}
// otherwise: parse key-value pairs
do
{
// ugly, but could be fixed with loop reorganization
if (last_token == lexer::token_type::value_separator)
{
get_token();
}
// store key
expect(lexer::token_type::value_string);
const auto key = m_lexer.get_string();
...
...
@@ -2643,16 +2662,13 @@ class basic_json
// parse value
get_token();
result[key] = parse();
// read next character
get_token();
result[key] = parse_internal();
}
while (last_token == lexer::token_type::value_separator
and get_token() == last_token);
while (last_token == lexer::token_type::value_separator);
// closing }
expect(lexer::token_type::end_object);
get_token();
return result;
}
...
...
@@ -2668,44 +2684,53 @@ class basic_json
// closing ] -> we are done
if (last_token == lexer::token_type::end_array)
{
get_token();
return result;
}
// otherwise: parse values
do
{
// parse value
result.push_back(parse());
// ugly, but could be fixed with loop reorganization
if (last_token == lexer::token_type::value_separator)
{
get_token();
}
//
read next character
get_token(
);
//
parse value
result.push_back(parse_internal()
);
}
while (last_token == lexer::token_type::value_separator
and get_token() == last_token);
while (last_token == lexer::token_type::value_separator);
// closing ]
expect(lexer::token_type::end_array);
get_token();
return result;
}
case (lexer::token_type::literal_null):
{
get_token();
return basic_json(nullptr);
}
case (lexer::token_type::value_string):
{
return basic_json(m_lexer.get_string());
const auto s = m_lexer.get_string();
get_token();
return basic_json(s);
}
case (lexer::token_type::literal_true):
{
get_token();
return basic_json(true);
}
case (lexer::token_type::literal_false):
{
get_token();
return basic_json(false);
}
...
...
@@ -2719,6 +2744,8 @@ class basic_json
m_lexer.get_string_value() + " is not a number");
}
get_token();
// check if conversion loses precision
const auto int_val = static_cast<number_integer_t>(float_val);
if (float_val == int_val)
...
...
@@ -2744,7 +2771,6 @@ class basic_json
}
}
private:
/// get next token from lexer
inline typename lexer::token_type get_token()
{
...
...
test/unit.cpp
View file @
89a1b024
...
...
@@ -5052,7 +5052,15 @@ TEST_CASE("parser class")
CHECK_THROWS_AS
(
json
::
parser
(
"--"
).
parse
(),
std
::
invalid_argument
);
CHECK_THROWS_AS
(
json
::
parser
(
"-0."
).
parse
(),
std
::
invalid_argument
);
CHECK_THROWS_AS
(
json
::
parser
(
"-."
).
parse
(),
std
::
invalid_argument
);
CHECK_THROWS_AS
(
json
::
parser
(
"-:"
).
parse
(),
std
::
invalid_argument
);
CHECK_THROWS_AS
(
json
::
parser
(
"0.:"
).
parse
(),
std
::
invalid_argument
);
CHECK_THROWS_AS
(
json
::
parser
(
"e."
).
parse
(),
std
::
invalid_argument
);
CHECK_THROWS_AS
(
json
::
parser
(
"1e."
).
parse
(),
std
::
invalid_argument
);
CHECK_THROWS_AS
(
json
::
parser
(
"1e/"
).
parse
(),
std
::
invalid_argument
);
CHECK_THROWS_AS
(
json
::
parser
(
"1e:"
).
parse
(),
std
::
invalid_argument
);
CHECK_THROWS_AS
(
json
::
parser
(
"1E."
).
parse
(),
std
::
invalid_argument
);
CHECK_THROWS_AS
(
json
::
parser
(
"1E/"
).
parse
(),
std
::
invalid_argument
);
CHECK_THROWS_AS
(
json
::
parser
(
"1E:"
).
parse
(),
std
::
invalid_argument
);
// unexpected end of null
CHECK_THROWS_AS
(
json
::
parser
(
"n"
).
parse
(),
std
::
invalid_argument
);
...
...
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