Skip to content

Commit

Permalink
Add recursive structure of json parser
Browse files Browse the repository at this point in the history
Signed-off-by: Kunlin Yu <[email protected]>
  • Loading branch information
kunlinyu committed Jan 5, 2025
1 parent ea90589 commit f72d2ca
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion include/cql2cpp/cql2_parser_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#pragma once

#include <geos/vend/include_nlohmann_json.hpp>

#include "ast_node.h"

namespace cql2cpp {
Expand All @@ -21,9 +23,47 @@ class Cql2ParserJson {
public:
Cql2ParserJson() {}

bool parse() { return true; }
AstNodePtr parse(const geos_nlohmann::json& j) {
AstNodePtr node;
std::vector<geos_nlohmann::json> children;
if (parse(j, &node, &children))
for (const auto& child_j : children) {
AstNodePtr child_node = parse(child_j);
if (child_node != nullptr)
node->append(child_node);
else
return nullptr;
}

return node;
}

AstNodePtr root() const { return root_; }

private:
bool parse(const geos_nlohmann::json& j, AstNodePtr* node,
std::vector<geos_nlohmann::json>* children);
};

class JsonParser {
public:
virtual bool detect(const geos_nlohmann::json& j) = 0;
virtual bool parse(const geos_nlohmann::json& j, AstNodePtr* node,
std::vector<geos_nlohmann::json>* children) = 0;
};

class JsonParserOperator : public JsonParser {
public:
JsonParserOperator() {}

bool detect(const geos_nlohmann::json& j) override {
return j.contains("op");
}

bool parse(const geos_nlohmann::json& j, AstNodePtr* node,
std::vector<geos_nlohmann::json>* children) override {
return false;
}
};

} // namespace cql2cpp

0 comments on commit f72d2ca

Please sign in to comment.