From 57a3ecc62079b5d6f88c0588389fd0ef07033349 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 5 Jun 2015 13:50:07 -0400 Subject: [PATCH] Fixed the token detect logic. --- peglib.h | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/peglib.h b/peglib.h index 1ed6cef..21cdd59 100644 --- a/peglib.h +++ b/peglib.h @@ -180,10 +180,8 @@ struct SemanticValues : protected std::vector const char* s; size_t n; size_t choice; - bool has_anchor; - bool is_leaf; - SemanticValues() : s(nullptr), n(0), choice(0), has_anchor(false), is_leaf(true) {} + SemanticValues() : s(nullptr), n(0), choice(0) {} std::string str(size_t i = 0) const { if (i > 0) { @@ -192,10 +190,6 @@ struct SemanticValues : protected std::vector return std::string(s, n); } - bool is_token() const { - return has_anchor || is_leaf; - } - typedef SemanticValue T; using std::vector::iterator; using std::vector::const_iterator; @@ -521,8 +515,6 @@ struct Context } sv.s = nullptr; sv.n = 0; - sv.has_anchor = false; - sv.is_leaf = true; return sv; } @@ -626,8 +618,6 @@ public: sv.s = chldsv.s; sv.n = chldsv.n; sv.choice = id; - sv.has_anchor = chldsv.has_anchor; - sv.is_leaf = chldsv.is_leaf; c.pop(); return len; } @@ -890,7 +880,6 @@ public: if (success(len)) { sv.s = s; sv.n = len; - sv.has_anchor = true; } return len; } @@ -1320,7 +1309,6 @@ inline any Holder::reduce(const SemanticValues& sv, any& dt, const Action& actio inline size_t DefinitionReference::parse( const char* s, size_t n, SemanticValues& sv, Context& c, any& dt) const { - sv.is_leaf = false; const auto& rule = *get_rule(); return rule.parse(s, n, sv, c, dt); } @@ -2086,8 +2074,10 @@ private: } void ast_node(const AstNodeInfo& info) { - (*this)[info.name] = [info](const SemanticValues& sv) { - if (sv.is_token()) { + auto& rule = (*this)[info.name]; + auto is_token = rule.is_token; + rule = [info, is_token](const SemanticValues& sv) { + if (is_token) { return std::make_shared(info.name, info.tag, std::string(sv.s, sv.n)); } if (info.optimize && sv.size() == 1) { @@ -2104,8 +2094,9 @@ private: auto& rule = x.second; auto& action = rule.actions.front(); if (!action) { - action = [name](const SemanticValues& sv) { - if (sv.is_token()) { + auto is_token = rule.is_token; + action = [name, is_token](const SemanticValues& sv) { + if (is_token) { return std::make_shared(name.c_str(), AstDefaultTag, std::string(sv.s, sv.n)); } if (sv.size() == 1) {