Merge pull request #51 from YosysHQ/json-update

Json update
This commit is contained in:
Clifford Wolf 2018-08-18 17:21:15 +02:00 committed by GitHub
commit 060be78c09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -52,7 +52,7 @@ struct JsonNode
std::map<string, JsonNode *> data_dict; std::map<string, JsonNode *> data_dict;
std::vector<string> data_dict_keys; std::vector<string> data_dict_keys;
JsonNode(std::istream &f) JsonNode(std::istream &f, int &lineno)
{ {
type = 0; type = 0;
data_number = 0; data_number = 0;
@ -63,6 +63,8 @@ struct JsonNode
if (ch == EOF) if (ch == EOF)
log_error("Unexpected EOF in JSON file.\n"); log_error("Unexpected EOF in JSON file.\n");
if (ch == '\n')
lineno++;
if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n')
continue; continue;
@ -91,9 +93,12 @@ struct JsonNode
break; break;
} }
if ('0' <= ch && ch <= '9') { if (('0' <= ch && ch <= '9') || ('-' == ch)) {
type = 'N'; type = 'N';
data_number = ch - '0'; if (ch == '-')
data_number = 0;
else
data_number = ch - '0';
data_string += ch; data_string += ch;
while (1) { while (1) {
@ -114,6 +119,8 @@ struct JsonNode
data_string += ch; data_string += ch;
} }
if (data_string[0] == '-')
data_number = -data_number;
data_string = ""; data_string = "";
break; break;
@ -148,6 +155,8 @@ struct JsonNode
if (ch == EOF) if (ch == EOF)
log_error("Unexpected EOF in JSON file.\n"); log_error("Unexpected EOF in JSON file.\n");
if (ch == '\n')
lineno++;
if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == ',') if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == ',')
continue; continue;
@ -155,7 +164,7 @@ struct JsonNode
break; break;
f.unget(); f.unget();
data_array.push_back(new JsonNode(f)); data_array.push_back(new JsonNode(f, lineno));
} }
break; break;
@ -170,6 +179,8 @@ struct JsonNode
if (ch == EOF) if (ch == EOF)
log_error("Unexpected EOF in JSON file.\n"); log_error("Unexpected EOF in JSON file.\n");
if (ch == '\n')
lineno++;
if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == ',') if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == ',')
continue; continue;
@ -177,7 +188,7 @@ struct JsonNode
break; break;
f.unget(); f.unget();
JsonNode key(f); JsonNode key(f, lineno);
while (1) { while (1) {
ch = f.get(); ch = f.get();
@ -185,6 +196,8 @@ struct JsonNode
if (ch == EOF) if (ch == EOF)
log_error("Unexpected EOF in JSON file.\n"); log_error("Unexpected EOF in JSON file.\n");
if (ch == '\n')
lineno++;
if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == ':') if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == ':')
continue; continue;
@ -192,10 +205,10 @@ struct JsonNode
break; break;
} }
JsonNode *value = new JsonNode(f); JsonNode *value = new JsonNode(f, lineno);
if (key.type != 'S') if (key.type != 'S')
log_error("Unexpected non-string key in JSON dict.\n"); log_error("Unexpected non-string key in JSON dict, line %d.\n", lineno);
data_dict[key.data_string] = value; data_dict[key.data_string] = value;
data_dict_keys.push_back(key.data_string); data_dict_keys.push_back(key.data_string);
@ -204,7 +217,7 @@ struct JsonNode
break; break;
} }
log_error("Unexpected character in JSON file: '%c'\n", ch); log_error("Unexpected character in JSON file, line %d: '%c'\n", lineno, ch);
} }
} }
@ -736,8 +749,9 @@ bool parse_json_file(std::istream &f, std::string &filename, Context *ctx)
{ {
try { try {
using namespace JsonParser; using namespace JsonParser;
int lineno = 1;
JsonNode root(f); JsonNode root(f, lineno);
if (root.type != 'D') if (root.type != 'D')
log_error("JSON root node is not a dictionary.\n"); log_error("JSON root node is not a dictionary.\n");