Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
json
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
json
Commits
82b95ca6
Unverified
Commit
82b95ca6
authored
Jun 20, 2017
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
🔨
simplified error handling in parser
parent
f2cdb3d5
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
20 deletions
+22
-20
json.hpp
src/json.hpp
+22
-20
unit-class_parser.cpp
test/src/unit-class_parser.cpp
+0
-0
No files found.
src/json.hpp
View file @
82b95ca6
...
@@ -12902,7 +12902,7 @@ scan_number_done:
...
@@ -12902,7 +12902,7 @@ scan_number_done:
default
:
default
:
{
{
// the last token was unexpected
// the last token was unexpected
unexpect
(
last_token
);
unexpect
();
}
}
}
}
...
@@ -13040,51 +13040,49 @@ scan_number_done:
...
@@ -13040,51 +13040,49 @@ scan_number_done:
/// get next token from lexer
/// get next token from lexer
typename
lexer
::
token_type
get_token
()
typename
lexer
::
token_type
get_token
()
{
{
last_token
=
m_lexer
.
scan
();
return
(
last_token
=
m_lexer
.
scan
());
return
last_token
;
}
}
/*!
/*!
@throw parse_error.101 if expected token did not occur
@throw parse_error.101 if expected token did not occur
*/
*/
void
expect
(
typename
lexer
::
token_type
t
)
const
void
expect
(
typename
lexer
::
token_type
t
)
{
{
if
(
JSON_UNLIKELY
(
t
!=
last_token
))
if
(
JSON_UNLIKELY
(
t
!=
last_token
))
{
{
std
::
string
error_msg
=
"syntax error - "
;
errored
=
true
;
if
(
last_token
==
lexer
::
token_type
::
parse_error
)
expected
=
t
;
{
throw_exception
();
error_msg
+=
std
::
string
(
m_lexer
.
get_error_message
())
+
"; last read: '"
+
m_lexer
.
get_token_string
()
+
"'"
;
}
else
{
error_msg
+=
"unexpected "
+
std
::
string
(
lexer
::
token_type_name
(
last_token
));
}
error_msg
+=
"; expected "
+
std
::
string
(
lexer
::
token_type_name
(
t
));
JSON_THROW
(
parse_error
::
create
(
101
,
m_lexer
.
get_position
(),
error_msg
));
}
}
}
}
/*!
/*!
@throw parse_error.101 if unexpected token occurred
@throw parse_error.101 if unexpected token occurred
*/
*/
void
unexpect
(
typename
lexer
::
token_type
t
)
const
void
unexpect
(
)
{
{
if
(
JSON_UNLIKELY
(
t
==
last_token
))
errored
=
true
;
throw_exception
();
}
[[
noreturn
]]
void
throw_exception
()
const
{
{
std
::
string
error_msg
=
"syntax error - "
;
std
::
string
error_msg
=
"syntax error - "
;
if
(
last_token
==
lexer
::
token_type
::
parse_error
)
if
(
last_token
==
lexer
::
token_type
::
parse_error
)
{
{
error_msg
+=
std
::
string
(
m_lexer
.
get_error_message
())
+
"; last read
'"
+
m_lexer
.
get_token_string
()
+
"'"
;
error_msg
+=
std
::
string
(
m_lexer
.
get_error_message
())
+
"; last read:
'"
+
m_lexer
.
get_token_string
()
+
"'"
;
}
}
else
else
{
{
error_msg
+=
"unexpected "
+
std
::
string
(
lexer
::
token_type_name
(
last_token
));
error_msg
+=
"unexpected "
+
std
::
string
(
lexer
::
token_type_name
(
last_token
));
}
}
JSON_THROW
(
parse_error
::
create
(
101
,
m_lexer
.
get_position
(),
error_msg
));
if
(
expected
!=
lexer
::
token_type
::
uninitialized
)
{
error_msg
+=
"; expected "
+
std
::
string
(
lexer
::
token_type_name
(
expected
));
}
}
JSON_THROW
(
parse_error
::
create
(
101
,
m_lexer
.
get_position
(),
error_msg
));
}
}
private
:
private
:
...
@@ -13096,6 +13094,10 @@ scan_number_done:
...
@@ -13096,6 +13094,10 @@ scan_number_done:
typename
lexer
::
token_type
last_token
=
lexer
::
token_type
::
uninitialized
;
typename
lexer
::
token_type
last_token
=
lexer
::
token_type
::
uninitialized
;
/// the lexer
/// the lexer
lexer
m_lexer
;
lexer
m_lexer
;
/// whether a syntax error occurred
bool
errored
=
false
;
/// possible reason for the syntax error
typename
lexer
::
token_type
expected
=
lexer
::
token_type
::
uninitialized
;
};
};
public
:
public
:
...
...
test/src/unit-class_parser.cpp
View file @
82b95ca6
This diff is collapsed.
Click to expand it.
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