Skip to content

Commit

Permalink
jsonify
Browse files Browse the repository at this point in the history
  • Loading branch information
Ze7111 committed Jul 4, 2024
1 parent 70c8359 commit 0e9e88f
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 22 deletions.
75 changes: 75 additions & 0 deletions source/core/utils/josnify.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* @author Dhruvan Kartik
* @copyright Copyright (c) 2024 (CC BY 4.0)
*
* @note This code is part of the Helix Language Project and is licensed under the Attribution 4.0
* International license (CC BY 4.0). You are allowed to use, modify, redistribute, and create
* derivative works, even for commercial purposes, provided that you give appropriate credit,
* provide a link to the license, and indicate if changes were made. For more information, please
* visit: https://creativecommons.org/licenses/by/4.0/ SPDX-License-Identifier: CC-BY-4.0
*
* @note This code is provided by the creators of Helix. Visit our website at:
* https://helix-lang.com/ for more information.
*/
#ifndef __JOSNIFY_H__
#define __JOSNIFY_H__

#include <string>
#include <type_traits>

#include "core/types/hx_ints"

#define stringify(object) std::string(#object)

#define TO_JSON_SIGNATURE std::string to_json(u32 depth = 0, bool indent_start = false) const

#define TO_JSON_SIGNATURE_EXTEND(extend) std::string extend::to_json(u32 depth, bool indent_start) const

namespace jsonify {
constexpr inline std::string indent(const u32 &depth) {
return std::string(static_cast<u32>(depth * 4), ' ');
}

constexpr inline std::string escape(const std::string& val) {
std::string result;
result.reserve(val.size());

for (char car : val) {
switch (car) {
case '"': result += "\\\""; break;
case '\\': result += "\\\\"; break;
case '\n': result += "\\n"; break;
case '\t': result += "\\t"; break;
case '\r': result += "\\r"; break;
case '\b': result += "\\b"; break;
case '\f': result += "\\f"; break;
default: result += car; break;
}
}
return result;
}

template <typename T>
concept HasToJson = requires(T type) {
{ type.to_json() } -> std::same_as<std::string>;
};

template <typename T>
constexpr inline std::string to_json(T key, const u32 &depth, const std::string &alias = "") {
if constexpr (HasToJson<T>) {
return indent(depth) + (alias.empty() ? stringify(key) : '"' + alias + '"') + ": " +
key.to_json(depth + 1);
} else if constexpr (std::is_same_v<T, char>) {
return indent(depth) + (alias.empty() ? stringify(key) : '"' + alias + '"') + ": \"" +
std::string(1, key) + "\"";
} else if constexpr (std::is_arithmetic_v<T>) {
return indent(depth) + (alias.empty() ? stringify(key) : '"' + alias + '"') + ": " +
std::to_string(key);
} else {
return indent(depth) + (alias.empty() ? stringify(key) : '"' + alias + '"') + ": \"" +
std::string(key) + "\"";
}
}
} // end namespace jsonify

#endif // __JOSNIFY_H__
1 change: 1 addition & 0 deletions source/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ int main(int argc, char **argv) {
auto tmp = node->parse();

print(node->to_string());
print(tokens.to_json());
/// PRINT THE NODES AND THE NODES WITH
///

Expand Down
34 changes: 14 additions & 20 deletions source/parser/cst/include/nodes.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "core/error/error.hh"
#include "core/utils/hx_print"
#include "core/utils/josnify.hh"
#include "parser/cst/include/cst.hh"
#include "token/include/token.hh"
#include "token/include/token_list.hh"
Expand All @@ -40,7 +41,6 @@

namespace parser::cst {
using namespace token;
inline std::string indent(u32 depth = 0) { return std::string(static_cast<u32>(depth * 4), ' '); }

template <const char quote, const tokens toke_type>
struct Quoted final : CSTBase<Quoted<quote, toke_type>> {
Expand Down Expand Up @@ -120,20 +120,12 @@ struct Quoted final : CSTBase<Quoted<quote, toke_type>> {
},
*/

return indent(depth) + "\"Quoted\" : {\n" + indent(depth + 1) + R"("openSymbol" : ')" +
std::string(1, quote) + "\',\n" + indent(depth + 1) + "\"value\" : {\n" +
indent(depth + 2) + "\"token\" : {\n" + indent(depth + 3) + R"("filename" : ")" +
this->value.file_name() + "\",\n" + indent(depth + 3) +
"\"line_number\" : " + std::to_string(this->value.line_number()) + ",\n" +
indent(depth + 3) +
"\"column_number\" : " + std::to_string(this->value.column_number()) + ",\n" +
indent(depth + 3) + "\"length\" : " + std::to_string(this->value.length()) + ",\n" +
indent(depth + 3) + "\"offset\" : " + std::to_string(this->value.offset()) + ",\n" +
indent(depth + 3) + R"("token_kind" : ")" +
std::string(this->value.token_kind_repr()) + "\",\n" + indent(depth + 3) +
R"("value" : ")" + this->value.value() + "\"\n" + indent(depth + 2) + "}\n" +
indent(depth + 1) + "},\n" + indent(depth + 1) + R"("closeSymbol" : ')" +
std::string(1, quote) + "\'\n" + indent(depth) + "}\n";
return jsonify::indent(depth) + "\"Quoted\" : {\n"
+ jsonify::to_json(jsonify::escape(std::string(1, quote)), depth + 1, "openSymbol") + ",\n"
+ jsonify::indent(depth + 1) + "\"value\" : "
+ this->value.to_json(depth + 1, false) + ",\n"
+ jsonify::to_json(jsonify::escape(std::string(1, quote)), depth + 1, "closeSymbol") + "\n"
+ jsonify::indent(depth) + "}\n";
};

private:
Expand Down Expand Up @@ -210,11 +202,13 @@ struct Delimited final : CSTBase<Delimited<StartToken, StartChar, Middle, EndCha
},
*/

return indent(depth) + "\"Delimited\" : {\n" + indent(depth + 1) + R"("openSymbol" : ')" +
std::string(1, StartChar) + "\',\n" + indent(depth + 1) + "\"value\" : {\n" +
this->value.value()->to_string(depth + 2) + indent(depth + 1) + "},\n" +
indent(depth + 1) + R"("closeSymbol" : ')" + std::string(1, EndChar) + "\'\n" +
"},\n";
return jsonify::indent(depth) + "\"Delimited\" : {\n"
+ jsonify::to_json(jsonify::escape(std::string(1, StartChar)), depth + 1, "openSymbol") + ",\n"
+ jsonify::indent(depth + 1) + "\"value\" : {\n"
+ this->value.value()->to_string(depth + 2)
+ jsonify::indent(depth + 1) + "},\n"
+ jsonify::to_json(jsonify::escape(std::string(1, EndChar)), depth + 1, "closeSymbol") + "\n"
+ jsonify::indent(depth) + "}\n";
}

private:
Expand Down
4 changes: 3 additions & 1 deletion source/token/include/token.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <string_view>

#include "core/types/hx_ints"
#include "core/utils/josnify.hh"
#include "token/include/generate.hh"

namespace token {
Expand Down Expand Up @@ -56,8 +57,9 @@ struct Token {
std::string value() const;
std::string_view token_kind_repr() const;
std::string file_name() const;

std::string to_string() const;
TO_JSON_SIGNATURE;

bool operator==(const Token &rhs) const;
std::ostream &operator<<(std::ostream &os) const;

Expand Down
2 changes: 2 additions & 0 deletions source/token/include/token_list.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <vector>

#include "core/types/hx_ints"
#include "core/utils/josnify.hh"
#include "token/include/token.hh"

namespace token {
Expand Down Expand Up @@ -114,6 +115,7 @@ class TokenList : public std::vector<Token> {
void remove_left();
void reset();
TokenList slice(u64 start, i64 end = -1);
TO_JSON_SIGNATURE;

[[nodiscard]] const std::string &file_name() const;
void insert_remove(TokenList &tokens, u64 start, u64 end);
Expand Down
13 changes: 13 additions & 0 deletions source/token/source/token.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <mutex>
#include <shared_mutex>

#include "core/utils/josnify.hh"
#include "token/include/generate.hh"

namespace token {
Expand Down Expand Up @@ -147,6 +148,18 @@ std::string Token::to_string() const {
std::string(val) + "\")";
}

TO_JSON_SIGNATURE_EXTEND(Token) {
return (indent_start ? jsonify::indent(depth) : "") + "{\n"
+ jsonify::to_json(jsonify::escape(filename), depth+1, "filename") + ",\n"
+ jsonify::to_json(line, depth+1, "line_number") + ",\n"
+ jsonify::to_json(column, depth+1, "column_number") + ",\n"
+ jsonify::to_json(len, depth+1, "length") + ",\n"
+ jsonify::to_json(_offset, depth+1, "offset") + ",\n"
+ jsonify::to_json(jsonify::escape(std::string(token_kind_repr())), depth+1, "kind") + ",\n"
+ jsonify::to_json(val, depth+1, "value") + "\n"
+ jsonify::indent(depth) + "}";
}

bool Token::operator==(const Token &rhs) const {
return (line == rhs.line && column == rhs.column && len == rhs.len && _offset == rhs._offset &&
kind == rhs.kind && val == rhs.val && filename == rhs.filename);
Expand Down
18 changes: 18 additions & 0 deletions source/token/source/token_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <stdexcept>

#include "core/utils/colors_ansi.hh"
#include "core/utils/josnify.hh"
#include "token/include/generate.hh"
#include "token/include/token.hh"
#include "token/include/token_list.hh"
Expand Down Expand Up @@ -207,4 +208,21 @@ std::reference_wrapper<Token> TokenList::TokenListIter::current() {
return tokens.get()[cursor_position];
}

TO_JSON_SIGNATURE_EXTEND(TokenList) {
std::string result = "{\n"
+ jsonify::indent(depth+1) + "\"tokens\" : [\n";

for (auto &tok : *this) {
result += tok.to_json(depth+2, true) + ",\n";
}

if (!this->empty()) {
result.erase(result.size() - 2, 2);
}

result += "\n";

return result + jsonify::indent(depth+1) + "]\n" + jsonify::indent(depth) + "}\n";
}

} // namespace token
2 changes: 1 addition & 1 deletion tests/main.hlx
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(r"hello world");
(r"hello \" world");

0 comments on commit 0e9e88f

Please sign in to comment.