lexer update
parse decimals and negatives.
This commit is contained in:
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
/.idea
|
||||
/.cache
|
||||
/.ccls-cache
|
||||
/compile_commands.json
|
||||
/cmake-build-debug
|
||||
/build
|
33
main.cpp
33
main.cpp
@@ -1,22 +1,29 @@
|
||||
#include <jjx.hpp>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
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");
|
||||
}
|
||||
|
@@ -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": {
|
||||
|
@@ -1,3 +0,0 @@
|
||||
//
|
||||
// Created by josh on 8/19/24.
|
||||
//
|
31
src/json.cpp
31
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++;
|
||||
|
Reference in New Issue
Block a user