lexer update

parse decimals and negatives.
This commit is contained in:
2024-11-02 09:51:22 -04:00
parent eff8c17da8
commit da75265fcb
5 changed files with 55 additions and 24 deletions

6
.gitignore vendored Normal file
View File

@@ -0,0 +1,6 @@
/.idea
/.cache
/.ccls-cache
/compile_commands.json
/cmake-build-debug
/build

View File

@@ -1,22 +1,29 @@
#include <jjx.hpp> #include <jjx.hpp>
#include <iostream> #include <iostream>
#include <fstream>
using namespace jjx; using namespace jjx;
int main(int argc, char *argv[]) { void ParseJsonFile(const std::string& file_path) {
if (argc == 1) { std::ifstream file(file_path, std::ios::binary);
std::cerr << "Expected JSON input argument to parse" << std::endl; if (!file)
return 1; 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); std::string file_content(file_size, '\0');
if (error.size()) { file.read(&file_content[0], file_size);
std::cerr << error << std::endl; file.close();
return 1;
}
std::cout << json::deparse(ast); auto [text, parse_error] = json::parse(file_content);
return 0; 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");
} }

View File

@@ -4,13 +4,13 @@
"title": "Sample Konfabulator Widget", "title": "Sample Konfabulator Widget",
"name": "main_window", "name": "main_window",
"width": 500, "width": 500,
"height": 500 "height": -500.75
}, },
"image": { "image": {
"src": "Images/Sun.png", "src": "Images/Sun.png",
"name": "sun1", "name": "sun1",
"hOffset": 250, "hOffset": -250,
"vOffset": 250, "vOffset": 2.50,
"alignment": "center" "alignment": "center"
}, },
"text": { "text": {

View File

@@ -1,3 +0,0 @@
//
// Created by josh on 8/19/24.
//

View File

@@ -101,16 +101,37 @@ namespace jjx::json {
int index = original_index; int index = original_index;
json::token token {"", token_type::number, index}; json::token token {"", token_type::number, index};
std::string value = ""; 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) { while(true) {
if (index == raw_json.length()) { if (index == raw_json.length())
break; break;
}
auto c = raw_json[index]; auto c = raw_json[index];
if (!(c >= '0' && c <= '9')) { if (c == '.') {
break; 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; token.value += c;
index++; index++;