Skip to content

Commit

Permalink
more parser
Browse files Browse the repository at this point in the history
  • Loading branch information
marcj committed May 30, 2022
1 parent 5de0eba commit 0b0b564
Show file tree
Hide file tree
Showing 7 changed files with 1,519 additions and 1,278 deletions.
67 changes: 44 additions & 23 deletions src/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <string>
#include <map>
#include <set>
#include <unordered_map>
#include <vector>
#include <functional>
Expand Down Expand Up @@ -51,8 +52,8 @@ namespace ts {
}

/**
* shared_ptr has optional semantic already built-in, so we use it instead of std::optional,
* but instead of using shared_ptr directly, we use opt<T> to make it clear that it can be empty.
* shared_ptr has optional semantic already built-in, so we use it instead of std::optional<shared_ptr<>>,
* but instead of using shared_ptr directly, we use sharedOpt<T> to make it clear that it can be empty.
*/
template<typename T>
using sharedOpt = shared_ptr<T>;
Expand All @@ -76,7 +77,7 @@ namespace ts {
* Returns the last element of an array if non-empty, `undefined` otherwise.
*/
template<typename T>
optional<T> lastOrUndefined(const vector<T> &array) {
optional<T> lastOrUndefined(vector<T> &array) {
if (array.empty()) return std::nullopt;
return array.back();
}
Expand All @@ -87,6 +88,14 @@ namespace ts {
return def;
}

bool isTrue(optional<bool> b = {}) {
return b && *b;
}

bool isFalse(optional<bool> b = {}) {
return !b || !*b;
}

vector<string> charToStringVector(vector<const char *> chars) {
vector<string> s;
for (auto c: chars) s.push_back(c);
Expand All @@ -109,8 +118,15 @@ namespace ts {
}

template<typename T>
vector<T> slice(const vector<T> &v, int start) {
return vector<T>(v.begin() + start, v.end());
vector<T> slice(const vector<T> &v, int start = 0, int end = 0) {
if (!end) end = v.size();
return std::vector<T>(v.begin() + start, v.begin() + end);
}

template<typename T>
optional<vector<T>> slice(const optional<vector<T>> &v, int start = 0, int end = 0) {
if (!v) return std::nullopt;
return slice(*v, start, end);
}

string join(const vector<string> &vec, const char *delim) {
Expand Down Expand Up @@ -209,24 +225,24 @@ namespace ts {
return some(optional(array), optional(predicate));
}

template<typename T>
class LogicalOrReturnLast {
protected:
T value;
public:
LogicalOrReturnLast(T value): value(value) {}
operator T() { return value; }
LogicalOrReturnLast operator||(LogicalOrReturnLast other) {
if (value) return *this;

return other;
}
LogicalOrReturnLast operator||(T other) {
if (value) return *this;

return LogicalOrReturnLast(other);
}
};
// template<typename T>
// class LogicalOrReturnLast {
// protected:
// T value;
// public:
// LogicalOrReturnLast(T value): value(value) {}
// operator T() { return value; }
// LogicalOrReturnLast operator||(LogicalOrReturnLast other) {
// if (value) return *this;
//
// return other;
// }
// LogicalOrReturnLast operator||(T other) {
// if (value) return *this;
//
// return LogicalOrReturnLast(other);
// }
// };

//template<typename T>
//inline bool some(ts::NodeArray<T> array, std::function<bool(typename decltype(array)::value_type::value_type)> predicate) {
Expand All @@ -247,6 +263,11 @@ namespace ts {
vector.erase(remove(vector.begin(), vector.end(), item), vector.end());
};

template<typename T>
static bool has(std::set<T> &s, T item) {
return s.find(item) != s.end();
};

template<typename T>
static bool has(vector<T> &vector, T item) {
return find(vector.begin(), vector.end(), item) != vector.end();
Expand Down
Loading

0 comments on commit 0b0b564

Please sign in to comment.