@@ -703,7 +703,6 @@ class value {
703703
704704public:
705705 enum Type {
706- type_invalid,
707706 type_null,
708707 type_boolean,
709708 type_object,
@@ -842,16 +841,15 @@ class value {
842841 }
843842
844843private:
845- struct Invalid {};
846844 struct Null {};
847845
848846 enum class Boolean {
849847 False,
850848 True,
851849 };
852850
853- std::variant<Invalid, Null, Boolean, object_pointer, array_pointer, std::string> storage_;
854- Type type_ = type_invalid ;
851+ std::variant<Null, Boolean, object_pointer, array_pointer, std::string> storage_;
852+ Type type_;
855853};
856854
857855inline value array::operator [](std::size_t n) const {
@@ -885,6 +883,7 @@ struct location_type {
885883
886884class reader {
887885 friend class parser ;
886+
888887public:
889888 reader (std::string_view s) noexcept
890889 : str_(s) {
@@ -972,39 +971,41 @@ class reader {
972971 */
973972class parser {
974973public:
974+ enum token_type {
975+ ArrayBegin,
976+ ArrayEnd,
977+ Colon,
978+ Comma,
979+ ObjectBegin,
980+ ObjectEnd,
981+ Quote,
982+ Number,
983+ String,
984+ LiteralTrue,
985+ LiteralFalse,
986+ LiteralNull,
987+ };
988+
975989 struct token {
976- enum type {
977- ArrayBegin,
978- ArrayEnd,
979- Colon,
980- Comma,
981- ObjectBegin,
982- ObjectEnd,
983- Quote,
984- Number,
985- String,
986- LiteralTrue,
987- LiteralFalse,
988- LiteralNull,
989- } type;
990-
991- // TODO(eteran): make this a string view, and reduce copies
990+ token_type type;
991+
992+ // TODO(eteran): make this a string view, and reduce copies?
992993 std::string value;
993994 size_t index;
994995 };
995996
996997private:
997998 template <class T >
998- const token &require (typename token::type type) {
999- if (tokens_[token_index_ ].type != type) {
999+ const token &require (token_type type) {
1000+ if (tokens_[index_ ].type != type) {
10001001 throw T ();
10011002 }
1002- return tokens_[token_index_ ++];
1003+ return tokens_[index_ ++];
10031004 }
10041005
1005- bool match (typename token::type type) noexcept {
1006- if (tokens_[token_index_ ].type == type) {
1007- ++token_index_ ;
1006+ bool match (token_type type) noexcept {
1007+ if (tokens_[index_ ].type == type) {
1008+ ++index_ ;
10081009 return true ;
10091010 }
10101011 return false ;
@@ -1030,23 +1031,23 @@ class parser {
10301031
10311032 // extract the next token...
10321033 if (reader_.match (' [' )) {
1033- tokens.push_back ({token::type ::ArrayBegin, " [" , token_start});
1034+ tokens.push_back ({token_type ::ArrayBegin, " [" , token_start});
10341035 } else if (reader_.match (' ]' )) {
1035- tokens.push_back ({token::type ::ArrayEnd, " ]" , token_start});
1036+ tokens.push_back ({token_type ::ArrayEnd, " ]" , token_start});
10361037 } else if (reader_.match (' :' )) {
1037- tokens.push_back ({token::type ::Colon, " :" , token_start});
1038+ tokens.push_back ({token_type ::Colon, " :" , token_start});
10381039 } else if (reader_.match (' ,' )) {
1039- tokens.push_back ({token::type ::Comma, " ," , token_start});
1040+ tokens.push_back ({token_type ::Comma, " ," , token_start});
10401041 } else if (reader_.match (' {' )) {
1041- tokens.push_back ({token::type ::ObjectBegin, " {" , token_start});
1042+ tokens.push_back ({token_type ::ObjectBegin, " {" , token_start});
10421043 } else if (reader_.match (' }' )) {
1043- tokens.push_back ({token::type ::ObjectEnd, " }" , token_start});
1044+ tokens.push_back ({token_type ::ObjectEnd, " }" , token_start});
10441045 } else if (reader_.match (" true" )) {
1045- tokens.push_back ({token::type ::LiteralTrue, " true" , token_start});
1046+ tokens.push_back ({token_type ::LiteralTrue, " true" , token_start});
10461047 } else if (reader_.match (" false" )) {
1047- tokens.push_back ({token::type ::LiteralFalse, " false" , token_start});
1048+ tokens.push_back ({token_type ::LiteralFalse, " false" , token_start});
10481049 } else if (reader_.match (" null" )) {
1049- tokens.push_back ({token::type ::LiteralNull, " null" , token_start});
1050+ tokens.push_back ({token_type ::LiteralNull, " null" , token_start});
10501051 } else if (reader_.match (' "' )) {
10511052
10521053 std::string s;
@@ -1133,7 +1134,7 @@ class parser {
11331134 }
11341135
11351136 reader_.require <quote_expected>(' "' );
1136- tokens.push_back ({token::type ::String, s, token_start});
1137+ tokens.push_back ({token_type ::String, s, token_start});
11371138
11381139 } else {
11391140
@@ -1189,7 +1190,7 @@ class parser {
11891190 }
11901191 }
11911192
1192- tokens.push_back ({token::type ::Number, s, token_start});
1193+ tokens.push_back ({token_type ::Number, s, token_start});
11931194 }
11941195 }
11951196
@@ -1213,75 +1214,64 @@ class parser {
12131214
12141215 auto arr = std::make_shared<array>();
12151216
1216- require<bracket_expected>(token::ArrayBegin);
1217-
1218- if (!match (token::ArrayEnd)) {
1217+ if (!match (token_type::ArrayEnd)) {
12191218 do {
12201219 arr->push_back (get_value ());
1221- } while (match (token ::Comma));
1220+ } while (match (token_type ::Comma));
12221221 }
12231222
1224- require<bracket_expected>(token ::ArrayEnd);
1223+ require<bracket_expected>(token_type ::ArrayEnd);
12251224 return arr;
12261225 }
12271226
12281227 object_pointer get_object () {
12291228
12301229 auto obj = std::make_shared<object>();
12311230
1232- require<brace_expected>(token::ObjectBegin);
1233-
1234- if (!match (token::ObjectEnd)) {
1231+ if (!match (token_type::ObjectEnd)) {
12351232 do {
1236- obj->insert (get_pair ());
1237- } while (match (token::Comma));
1233+ const token &key = require<string_expected>(token_type::String);
1234+ require<colon_expected>(token_type::Colon);
1235+ obj->insert (key.value , get_value ());
1236+ } while (match (token_type::Comma));
12381237 }
12391238
1240- require<brace_expected>(token ::ObjectEnd);
1239+ require<brace_expected>(token_type ::ObjectEnd);
12411240 return obj;
12421241 }
12431242
1244- object_entry get_pair () {
1245-
1246- const token &key = require<string_expected>(token::String);
1247- require<colon_expected>(token::Colon);
1248- return std::make_pair (key.value , get_value ());
1249- }
1250-
12511243 value get_value () {
12521244
1253- switch (tokens_[token_index_].type ) {
1254- case token::type::ObjectBegin:
1245+ const token &tok = tokens_[index_++];
1246+ switch (tok.type ) {
1247+ case token_type::ObjectBegin:
12551248 return value (get_object ());
1256- case token::type ::ArrayBegin:
1249+ case token_type ::ArrayBegin:
12571250 return value (get_array ());
1258- case token::type::String:
1259- return value (tokens_[token_index_++].value );
1260- case token::type::LiteralTrue:
1261- token_index_++;
1251+ case token_type::String:
1252+ return value (tok.value );
1253+ case token_type::LiteralTrue:
12621254 return value (true );
1263- case token::type::LiteralFalse:
1264- token_index_++;
1255+ case token_type::LiteralFalse:
12651256 return value (false );
1266- case token::type::LiteralNull:
1267- token_index_++;
1257+ case token_type::LiteralNull:
12681258 return value (nullptr );
1269- case token::type ::Number:
1270- return value (tokens_[token_index_++] .value , value::numeric_type ());
1259+ case token_type ::Number:
1260+ return value (tok .value , value::numeric_type ());
12711261 default :
12721262 throw value_expected ();
12731263 }
12741264 }
12751265
12761266public:
12771267 location_type location () noexcept {
1278- return reader_.location (tokens_[token_index_ ].index );
1268+ return reader_.location (tokens_[index_ ].index );
12791269 }
12801270
12811271private:
12821272 reader reader_;
12831273 std::vector<token> tokens_;
1284- size_t token_index_ = 0 ;
1274+ size_t index_ = 0 ;
12851275};
12861276
12871277inline std::string to_string (const value &v) {
@@ -1676,8 +1666,6 @@ inline void value_to_string(std::ostream &os, const value &v, Options options, i
16761666 case value::type_array:
16771667 value_to_string (os, as_array (v), options, indent, true );
16781668 break ;
1679- case value::type_invalid:
1680- break ;
16811669 }
16821670}
16831671
@@ -1748,17 +1736,13 @@ inline void serialize(std::ostream &os, const value &v, Options options) {
17481736 case value::type_boolean:
17491737 os << (to_bool (v) ? " true" : " false" );
17501738 break ;
1751- case value::type_object: {
1739+ case value::type_object:
17521740 serialize (os, as_object (v), options);
17531741 break ;
1754- }
1755- case value::type_array: {
1742+ case value::type_array:
17561743 serialize (os, as_array (v), options);
17571744 break ;
17581745 }
1759- case value::type_invalid:
1760- break ;
1761- }
17621746}
17631747
17641748template <class T , class = typename std::enable_if<std::is_same<T, value>::value || std::is_same<T, object>::value || std::is_same<T, array>::value>::type>
@@ -2181,7 +2165,7 @@ inline bool operator==(const value &lhs, const value &rhs) {
21812165 case value::type_string:
21822166 return as_string (lhs) == as_string (rhs);
21832167 case value::type_number:
2184- // NOTE(eteran): this is kinda a "best effort", we can't compare their string
2168+ // NOTE(eteran): this is kinda a "best effort", we can't compare the string
21852169 // contents because things like 1e5 equals 100000
21862170 return to_number<double >(lhs) == to_number<double >(rhs);
21872171 case value::type_null:
@@ -2192,8 +2176,6 @@ inline bool operator==(const value &lhs, const value &rhs) {
21922176 return as_array (lhs) == as_array (rhs);
21932177 case value::type_object:
21942178 return as_object (lhs) == as_object (rhs);
2195- case value::type_invalid:
2196- break ;
21972179 }
21982180 }
21992181 return false ;
0 commit comments