diff options
| author | Dylan <boss@tehbox.org> | 2026-06-13 15:58:49 +1200 |
|---|---|---|
| committer | Dylan <boss@tehbox.org> | 2026-06-13 15:58:49 +1200 |
| commit | e742522e2f428a17cd389e030d167fadcd25c8ef (patch) | |
| tree | 555a8476730dd2379a2a89b59ef60db6426f34a8 /src | |
| parent | 49a8dae0f3a6e589dfad65cbbf230137a7b67f4a (diff) | |
| download | tehjson-e742522e2f428a17cd389e030d167fadcd25c8ef.tar.gz tehjson-e742522e2f428a17cd389e030d167fadcd25c8ef.zip | |
feat: Added booleans
Diffstat (limited to 'src')
| -rw-r--r-- | src/json.cpp | 1 | ||||
| -rw-r--r-- | src/tokenizer.cpp | 26 | ||||
| -rw-r--r-- | src/tokenizer.h | 20 |
3 files changed, 37 insertions, 10 deletions
diff --git a/src/json.cpp b/src/json.cpp index 0232a8a..25779b9 100644 --- a/src/json.cpp +++ b/src/json.cpp @@ -127,6 +127,7 @@ namespace TehJSON case TokenType::StringLit: children[childName.content].set<std::string>(consume(TokenType::StringLit).content); break; case TokenType::IntLit: children[childName.content].set<int>(std::stoi(consume(TokenType::IntLit).content)); break; case TokenType::FloatLit: children[childName.content].set<float>(std::stof(consume(TokenType::FloatLit).content)); break; + case TokenType::BoolLit: children[childName.content].set<bool>(consume(TokenType::BoolLit).content == "true"); break; case TokenType::LSquare: { consume(TokenType::LSquare); diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp index a32879e..a010703 100644 --- a/src/tokenizer.cpp +++ b/src/tokenizer.cpp @@ -1,4 +1,5 @@ #include "tokenizer.h" +#include <iostream> #include <stdexcept> namespace TehJSON @@ -16,6 +17,7 @@ namespace TehJSON case TokenType::StringLit: return "StringLit"; case TokenType::IntLit: return "IntLit"; case TokenType::FloatLit: return "FloatLit"; + case TokenType::BoolLit: return "BoolLit"; } } @@ -55,6 +57,7 @@ namespace TehJSON tokens.push_back({TokenType::StringLit, literalContent}); break; } + case '-': case '0' ... '9': { std::string literalContent{c}; pos++; @@ -78,6 +81,29 @@ namespace TehJSON tokens.push_back({isInt?TokenType::IntLit:TokenType::FloatLit, literalContent}); break; } + case 't': + { + if(input.at(++pos) != 'r'|| + input.at(++pos) != 'u'|| + input.at(++pos) != 'e') + { + throw std::runtime_error("Was expecting 'true' but got something else"); + } + tokens.push_back({TokenType::BoolLit, "true"}); + break; + } + case 'f': + { + if(input.at(++pos) != 'a'|| + input.at(++pos) != 'l'|| + input.at(++pos) != 's'|| + input.at(++pos) != 'e') + { + throw std::runtime_error("Was expecting 'false' but got something else"); + } + tokens.push_back({TokenType::BoolLit, "false"}); + break; + } case '{': tokens.push_back({TokenType::LBrace, std::string{c}}); break; case '}': tokens.push_back({TokenType::RBrace, std::string{c}}); break; case '[': tokens.push_back({TokenType::LSquare, std::string{c}}); break; diff --git a/src/tokenizer.h b/src/tokenizer.h index d0362a3..312498f 100644 --- a/src/tokenizer.h +++ b/src/tokenizer.h @@ -7,16 +7,16 @@ namespace TehJSON { enum struct TokenType { - LBrace, - RBrace, - LSquare, - RSquare, - Colon, - Comma, - StringLit, - IntLit, - FloatLit, - BoolLit, + LBrace, // { + RBrace, // } + LSquare, // [ + RSquare, // ] + Colon, // : + Comma, // , + StringLit, // ".+" + IntLit, // -?[0-9]+ + FloatLit, // -?[0-9]+.[0-9]+ + BoolLit, // true|false }; std::string getTokenName(TokenType t); |
