diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..52c505d --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +/.idea +/.cache +/.ccls-cache +/compile_commands.json +/cmake-build-debug +/build diff --git a/main.cpp b/main.cpp index e954692..ed55985 100644 --- a/main.cpp +++ b/main.cpp @@ -1,22 +1,29 @@ #include #include +#include using namespace jjx; -int main(int argc, char *argv[]) { - if (argc == 1) { - std::cerr << "Expected JSON input argument to parse" << std::endl; - return 1; - } +void ParseJsonFile(const std::string& file_path) { + std::ifstream file(file_path, std::ios::binary); + if (!file) + throw std::runtime_error("We couldn't find the file: " + file_path); - std::string in{argv[1]}; + std::streamsize file_size; + file.seekg(0, std::ios::end); + file_size = file.tellg(); + file.seekg(0, std::ios::beg); - auto [ast, error] = json::parse(in); - if (error.size()) { - std::cerr << error << std::endl; - return 1; - } + std::string file_content(file_size, '\0'); + file.read(&file_content[0], file_size); + file.close(); - std::cout << json::deparse(ast); - return 0; + auto [text, parse_error] = json::parse(file_content); + if (!parse_error.empty()) + std::cerr << "Error while parsing json: " << parse_error << std::endl; + + std::cout << json::deparse(text) << std::endl; +} +int main(int argc, char* argv[]) { + ParseJsonFile("../samples/widgets.json"); } diff --git a/samples/widgets.json b/samples/widgets.json index ae96de2..c77e5fa 100644 --- a/samples/widgets.json +++ b/samples/widgets.json @@ -4,13 +4,13 @@ "title": "Sample Konfabulator Widget", "name": "main_window", "width": 500, - "height": 500 + "height": -500.75 }, "image": { "src": "Images/Sun.png", "name": "sun1", - "hOffset": 250, - "vOffset": 250, + "hOffset": -250, + "vOffset": 2.50, "alignment": "center" }, "text": { diff --git a/src/jjx.cpp b/src/jjx.cpp deleted file mode 100644 index bd6aa59..0000000 --- a/src/jjx.cpp +++ /dev/null @@ -1,3 +0,0 @@ -// -// Created by josh on 8/19/24. -// diff --git a/src/json.cpp b/src/json.cpp index 79e7004..6b74c98 100644 --- a/src/json.cpp +++ b/src/json.cpp @@ -101,16 +101,37 @@ namespace jjx::json { int index = original_index; json::token token {"", token_type::number, index}; std::string value = ""; - // TODO: handle not just integers + bool decimal_present = false; + + // Negative numbers. + if (raw_json[index] == '-') { + token.value += raw_json[index]; + index++; + } + + /* If there's a decimal before a digit. Like ".006", + * it's presumed a zero should be before the decimal. */ + if (raw_json[index] == '.') { + token.value += '0'; + token.value += raw_json[index]; + decimal_present = true; + index++; + } + while(true) { - if (index == raw_json.length()) { + if (index == raw_json.length()) break; - } auto c = raw_json[index]; - if (!(c >= '0' && c <= '9')) { - break; + if (c == '.') { + if (decimal_present) + break; + decimal_present = true; + token.value += c; } + // Only regex non-numeric values if we didn't catch it earlier. + else if (!(c >= '0' && c <= '9')) + break; token.value += c; index++;