14#ifndef CPPPEGLIB_HEURISTIC_ERROR_TOKEN_MAX_CHAR_COUNT
15#define CPPPEGLIB_HEURISTIC_ERROR_TOKEN_MAX_CHAR_COUNT 32
22#if __has_include(<charconv>)
27#include <initializer_list>
36#include <unordered_map>
37#include <unordered_set>
40#if !defined(__cplusplus) || __cplusplus < 201703L
41#error "Requires complete C++17 support"
84 auto b =
static_cast<uint8_t
>(s8[0]);
85 if ((b & 0x80) == 0) {
87 }
else if ((b & 0xE0) == 0xC0 && l >= 2) {
89 }
else if ((b & 0xF0) == 0xE0 && l >= 3) {
91 }
else if ((b & 0xF8) == 0xF0 && l >= 4) {
108 buff[0] =
static_cast<char>(cp & 0x7F);
110 }
else if (cp < 0x0800) {
111 buff[0] =
static_cast<char>(0xC0 | ((cp >> 6) & 0x1F));
112 buff[1] =
static_cast<char>(0x80 | (cp & 0x3F));
114 }
else if (cp < 0xD800) {
115 buff[0] =
static_cast<char>(0xE0 | ((cp >> 12) & 0xF));
116 buff[1] =
static_cast<char>(0x80 | ((cp >> 6) & 0x3F));
117 buff[2] =
static_cast<char>(0x80 | (cp & 0x3F));
119 }
else if (cp < 0xE000) {
122 }
else if (cp < 0x10000) {
123 buff[0] =
static_cast<char>(0xE0 | ((cp >> 12) & 0xF));
124 buff[1] =
static_cast<char>(0x80 | ((cp >> 6) & 0x3F));
125 buff[2] =
static_cast<char>(0x80 | (cp & 0x3F));
127 }
else if (cp < 0x110000) {
128 buff[0] =
static_cast<char>(0xF0 | ((cp >> 18) & 0x7));
129 buff[1] =
static_cast<char>(0x80 | ((cp >> 12) & 0x3F));
130 buff[2] =
static_cast<char>(0x80 | ((cp >> 6) & 0x3F));
131 buff[3] =
static_cast<char>(0x80 | (cp & 0x3F));
140 return std::string(buff, l);
146 auto b =
static_cast<uint8_t
>(s8[0]);
147 if ((b & 0x80) == 0) {
151 }
else if ((b & 0xE0) == 0xC0) {
154 cp = ((
static_cast<char32_t>(s8[0] & 0x1F)) << 6) |
155 (
static_cast<char32_t>(s8[1] & 0x3F));
158 }
else if ((b & 0xF0) == 0xE0) {
161 cp = ((
static_cast<char32_t>(s8[0] & 0x0F)) << 12) |
162 ((
static_cast<char32_t>(s8[1] & 0x3F)) << 6) |
163 (
static_cast<char32_t>(s8[2] & 0x3F));
166 }
else if ((b & 0xF8) == 0xF0) {
169 cp = ((
static_cast<char32_t>(s8[0] & 0x07)) << 18) |
170 ((
static_cast<char32_t>(s8[1] & 0x3F)) << 12) |
171 ((
static_cast<char32_t>(s8[2] & 0x3F)) << 6) |
172 (
static_cast<char32_t>(s8[3] & 0x3F));
192inline std::u32string
decode(
const char *s8,
size_t l) {
197 while (i < l && (s8[i] & 0xc0) == 0x80) {
205template <
typename T>
const char *
u8(
const T *s) {
206 return reinterpret_cast<const char *
>(s);
215 for (
size_t i = 0; i < n; i++) {
218 case '\f': str +=
"\\f";
break;
219 case '\n': str +=
"\\n";
break;
220 case '\r': str +=
"\\r";
break;
221 case '\t': str +=
"\\t";
break;
222 case '\v': str +=
"\\v";
break;
223 default: str += c;
break;
238 if (
'0' <= c && c <=
'9') {
241 }
else if (
'a' <= c && c <=
'f') {
244 }
else if (
'A' <= c && c <=
'F') {
252 if (
'0' <= c && c <=
'9') {
263 while (i < n &&
is_hex(s[i], val)) {
264 ret =
static_cast<int>(ret * 16 + val);
267 return std::pair(ret, i);
274 while (i < n &&
is_digit(s[i], val)) {
275 ret =
static_cast<int>(ret * 8 + val);
278 return std::pair(ret, i);
290 if (i == n) {
throw std::runtime_error(
"Invalid escape sequence..."); }
360#if __has_include(<charconv>)
361 if constexpr (!std::is_floating_point<T>::value) {
362 std::from_chars(sv.data(), sv.data() + sv.size(), n);
364 if constexpr (
false) {
367 auto s = std::string(sv);
368 std::istringstream ss(s);
380 Trie(
const std::vector<std::string> &items,
bool ignore_case)
383 for (
const auto &item : items) {
384 const auto &s = ignore_case ?
to_lower(item) : item;
385 for (
size_t len = 1; len <= item.size(); len++) {
386 auto last = len == item.size();
387 std::string_view sv(s.data(), len);
388 auto it =
dic_.find(sv);
389 if (it ==
dic_.end()) {
390 dic_.emplace(sv,
Info{last, last,
id});
392 it->second.match =
true;
394 it->second.done =
false;
401 size_t match(
const char *text,
size_t text_len,
size_t &
id)
const {
402 std::string lower_text;
405 text = lower_text.data();
408 size_t match_len = 0;
411 while (!done && len <= text_len) {
412 std::string_view sv(text, len);
413 auto it =
dic_.find(sv);
414 if (it ==
dic_.end()) {
417 if (it->second.match) {
421 if (it->second.done) { done =
true; }
446 std::map<std::string, Info, std::less<>>
dic_;
458inline std::pair<size_t, size_t>
line_info(
const char *start,
const char *cur) {
473 return std::pair(no, col);
483 (h * 33) ^
static_cast<unsigned char>(*s));
486inline constexpr unsigned int str2tag(std::string_view sv) {
492inline constexpr unsigned int operator"" _(
const char *s,
size_t l) {
509 const char *
ss =
nullptr;
512 std::string_view
sv()
const {
return sv_; }
517 std::vector<unsigned int>
tags;
520 std::pair<size_t, size_t>
line_info()
const;
531 std::string_view
token(
size_t id = 0)
const {
533 assert(
id <
tokens.size());
539 return std::string(
token(
id));
547 template <
typename T>
549 size_t end =
static_cast<size_t>(-1))
const {
551 end = (std::min)(end, size());
552 for (
size_t i = beg; i < end; i++) {
553 r.emplace_back(std::any_cast<T>((*
this)[i]));
560 for (
auto &v : chvs) {
561 emplace_back(std::move(v));
563 for (
auto &tag : chvs.
tags) {
564 tags.emplace_back(std::move(tag));
571 using std::vector<std::any>::iterator;
572 using std::vector<std::any>::const_iterator;
573 using std::vector<std::any>::size;
574 using std::vector<std::any>::empty;
575 using std::vector<std::any>::assign;
576 using std::vector<std::any>::begin;
577 using std::vector<std::any>::end;
578 using std::vector<std::any>::rbegin;
579 using std::vector<std::any>::rend;
580 using std::vector<std::any>::operator[];
581 using std::vector<std::any>::at;
582 using std::vector<std::any>::resize;
583 using std::vector<std::any>::front;
584 using std::vector<std::any>::back;
585 using std::vector<std::any>::push_back;
586 using std::vector<std::any>::pop_back;
587 using std::vector<std::any>::insert;
588 using std::vector<std::any>::erase;
589 using std::vector<std::any>::clear;
590 using std::vector<std::any>::swap;
591 using std::vector<std::any>::emplace;
592 using std::vector<std::any>::emplace_back;
613template <
typename F,
typename... Args> std::any
call(F fn, Args &&...args) {
614 using R =
decltype(fn(std::forward<Args>(args)...));
615 if constexpr (std::is_void<R>::value) {
616 fn(std::forward<Args>(args)...);
618 }
else if constexpr (std::is_same<typename std::remove_cv<R>::type,
620 return fn(std::forward<Args>(args)...);
622 return std::any(fn(std::forward<Args>(args)...));
628template <
typename R,
typename... Args>
630 : std::integral_constant<unsigned,
sizeof...(Args)> {};
631template <
typename R,
typename C,
typename... Args>
633 : std::integral_constant<unsigned, sizeof...(Args)> {};
634template <
typename R,
typename C,
typename... Args>
636 : std::integral_constant<unsigned, sizeof...(Args)> {};
646 operator bool()
const {
return bool(
fn_); }
657 return [fn](
auto &vs,
auto & ) {
return call(fn, vs); };
659 return [fn](
auto &vs,
auto &dt) {
return call(fn, vs, dt); };
669inline bool success(
size_t len) {
return len !=
static_cast<size_t>(-1); }
671inline bool fail(
size_t len) {
return len ==
static_cast<size_t>(-1); }
676using Log = std::function<void(
size_t line,
size_t col,
const std::string &msg,
677 const std::string &rule)>;
702 if (t == error_literal && r == error_rule) {
return; }
710 int cast_char(
char c)
const {
return static_cast<unsigned char>(c); }
713 const char *pos)
const {
714 auto len = n - std::distance(s, pos);
718 if (!std::ispunct(c) && !std::isspace(c)) {
719 while (i < len && !std::ispunct(
cast_char(pos[i])) &&
727 while (count > 0 && j < i) {
734 return std::string();
738 const std::string &to)
const {
740 while ((pos = str.find(from, pos)) != std::string::npos) {
741 str.replace(pos, from.length(), to);
755 const Context &c,
const std::any &dt, std::any &trace_data)>;
759 const Context &c,
const std::any &dt,
size_t, std::any &trace_data)>;
795 std::map<std::pair<size_t, size_t>, std::tuple<size_t, std::any>>
833 template <
typename T>
834 void packrat(
const char *a_s,
size_t def_id,
size_t &len, std::any &val,
842 auto idx =
def_count *
static_cast<size_t>(col) + def_id;
846 auto key = std::pair(col, def_id);
850 len =
static_cast<size_t>(-1);
858 auto key = std::pair(col, def_id);
879 value_stack.emplace_back(std::make_shared<SemanticValues>(
this));
884 if (!vs.tags.empty()) { vs.tags.clear(); }
886 vs.sv_ = std::string_view();
887 vs.choice_count_ = 0;
889 if (!vs.tokens.empty()) { vs.tokens.clear(); }
901 void push_args(std::vector<std::shared_ptr<Ope>> &&args) {
907 const std::vector<std::shared_ptr<Ope>> &
top_args()
const {
916 std::map<std::string_view, std::string>());
919 if (!cs.empty()) { cs.clear(); }
929 auto prev = curr - 1;
930 for (
const auto &[k, v] : *curr) {
936 void set_error_pos(
const char *a_s,
const char *literal =
nullptr);
946 std::pair<size_t, size_t>
line_info(
const char *cur)
const {
948 for (
size_t pos = 0; pos <
l; pos++) {
954 auto pos =
static_cast<size_t>(std::distance(
s, cur));
956 auto it = std::lower_bound(
958 [](
size_t element,
size_t value) { return element < value; });
962 return std::pair(
id + 1, off + 1);
983 Context &c, std::any &dt)
const = 0;
989 template <
typename... Args>
991 :
opes_{static_cast<std::shared_ptr<
Ope>>(args)...} {}
996 std::any &dt)
const override {
1000 for (
const auto &ope :
opes_) {
1001 auto len = ope->parse(s + i, n - i, chvs, c, dt);
1002 if (
fail(len)) {
return len; }
1009 void accept(Visitor &v)
override;
1011 std::vector<std::shared_ptr<Ope>>
opes_;
1016 template <
typename... Args>
1018 :
opes_{static_cast<std::shared_ptr<
Ope>>(args)...},
1025 std::any &dt)
const override {
1026 size_t len =
static_cast<size_t>(-1);
1034 for (
const auto &ope :
opes_) {
1037 auto &chvs = c.
push();
1044 len = ope->parse(s, n, chvs, c, dt);
1062 void accept(Visitor &v)
override;
1066 std::vector<std::shared_ptr<Ope>>
opes_;
1072 Repetition(
const std::shared_ptr<Ope> &ope,
size_t min,
size_t max)
1076 std::any &dt)
const override {
1079 while (count <
min_) {
1080 auto &chvs = c.
push();
1083 auto len =
ope_->parse(s + i, n - i, chvs, c, dt);
1095 while (count <
max_) {
1096 auto &chvs = c.
push();
1099 auto len =
ope_->parse(s + i, n - i, chvs, c, dt);
1113 void accept(Visitor &v)
override;
1116 return min_ == 0 &&
max_ == std::numeric_limits<size_t>::max();
1119 static std::shared_ptr<Repetition>
zom(
const std::shared_ptr<Ope> &ope) {
1120 return std::make_shared<Repetition>(ope, 0,
1121 std::numeric_limits<size_t>::max());
1124 static std::shared_ptr<Repetition>
oom(
const std::shared_ptr<Ope> &ope) {
1125 return std::make_shared<Repetition>(ope, 1,
1126 std::numeric_limits<size_t>::max());
1129 static std::shared_ptr<Repetition>
opt(
const std::shared_ptr<Ope> &ope) {
1130 return std::make_shared<Repetition>(ope, 0, 1);
1143 Context &c, std::any &dt)
const override {
1144 auto &chvs = c.
push();
1147 auto len =
ope_->parse(s, n, chvs, c, dt);
1156 void accept(Visitor &v)
override;
1166 Context &c, std::any &dt)
const override {
1167 auto &chvs = c.
push();
1169 auto len =
ope_->parse(s, n, chvs, c, dt);
1172 return static_cast<size_t>(-1);
1178 void accept(Visitor &v)
override;
1186 :
trie_(v, ignore_case) {}
1189 std::any &dt)
const override;
1191 void accept(Visitor &v)
override;
1197 public std::enable_shared_from_this<LiteralString> {
1206 std::any &dt)
const override;
1208 void accept(Visitor &v)
override;
1217 public std::enable_shared_from_this<CharacterClass> {
1221 auto chars =
decode(s.data(), s.length());
1223 while (i < chars.size()) {
1224 if (i + 2 < chars.size() && chars[i + 1] ==
'-') {
1225 auto cp1 = chars[i];
1226 auto cp2 = chars[i + 2];
1227 ranges_.emplace_back(std::pair(cp1, cp2));
1231 ranges_.emplace_back(std::pair(cp, cp));
1239 bool negated,
bool ignore_case)
1245 Context &c, std::any & )
const override {
1248 return static_cast<size_t>(-1);
1254 for (
const auto &range :
ranges_) {
1258 return static_cast<size_t>(-1);
1269 return static_cast<size_t>(-1);
1273 void accept(Visitor &v)
override;
1276 bool in_range(
const std::pair<char32_t, char32_t> &range,
char32_t cp)
const {
1278 auto cpl = std::tolower(cp);
1279 return std::tolower(range.first) <= cpl &&
1280 cpl <= std::tolower(range.second);
1282 return range.first <= cp && cp <= range.second;
1286 std::vector<std::pair<char32_t, char32_t>>
ranges_;
1291class Character :
public Ope,
public std::enable_shared_from_this<Character> {
1296 Context &c, std::any & )
const override {
1297 if (n < 1 || s[0] !=
ch_) {
1299 return static_cast<size_t>(-1);
1304 void accept(Visitor &v)
override;
1310 public std::enable_shared_from_this<AnyCharacter> {
1313 Context &c, std::any & )
const override {
1317 return static_cast<size_t>(-1);
1322 void accept(Visitor &v)
override;
1330 std::any &dt)
const override {
1333 return ope_->parse(s, n, vs, c, dt);
1336 void accept(Visitor &v)
override;
1349 std::any &dt)
const override {
1350 auto len =
ope_->parse(s, n, vs, c, dt);
1355 void accept(Visitor &v)
override;
1366 std::any &dt)
const override;
1368 void accept(Visitor &v)
override;
1378 Context &c, std::any &dt)
const override {
1381 return ope_->parse(s, n, chvs, c, dt);
1384 void accept(Visitor &v)
override;
1396 Context & , std::any &dt)
const override {
1398 return fn_(s, n, vs, dt);
1400 void accept(Visitor &v)
override;
1401 std::function<size_t(
const char *s,
size_t n,
SemanticValues &vs,
1411 std::any &dt)
const override {
1412 auto ope =
weak_.lock();
1414 return ope->parse(s, n, vs, c, dt);
1417 void accept(Visitor &v)
override;
1427 std::any &dt)
const override;
1429 void accept(Visitor &v)
override;
1433 const std::string &
name()
const;
1444using Grammar = std::unordered_map<std::string, Definition>;
1446class Reference :
public Ope,
public std::enable_shared_from_this<Reference> {
1449 bool is_macro,
const std::vector<std::shared_ptr<Ope>> &args)
1454 std::any &dt)
const override;
1456 void accept(Visitor &v)
override;
1465 const std::vector<std::shared_ptr<Ope>>
args_;
1476 std::any &dt)
const override {
1480 return ope_->parse(s, n, vs, c, dt);
1483 void accept(Visitor &v)
override;
1495 std::any &dt)
const override;
1497 void accept(Visitor &v)
override;
1504 using BinOpeInfo = std::map<std::string_view, std::pair<size_t, char>>;
1507 const std::shared_ptr<Ope> &binop,
const BinOpeInfo &info,
1512 std::any &dt)
const override {
1516 void accept(Visitor &v)
override;
1525 Context &c, std::any &dt,
size_t min_prec)
const;
1535 std::any &dt)
const override;
1537 void accept(Visitor &v)
override;
1542class Cut :
public Ope,
public std::enable_shared_from_this<Cut> {
1545 Context &c, std::any & )
const override {
1550 void accept(Visitor &v)
override;
1556template <
typename... Args> std::shared_ptr<Ope>
seq(Args &&...args) {
1557 return std::make_shared<Sequence>(
static_cast<std::shared_ptr<Ope>
>(args)...);
1560template <
typename... Args> std::shared_ptr<Ope>
cho(Args &&...args) {
1561 return std::make_shared<PrioritizedChoice>(
1562 false,
static_cast<std::shared_ptr<Ope>
>(args)...);
1565template <
typename... Args> std::shared_ptr<Ope>
cho4label_(Args &&...args) {
1566 return std::make_shared<PrioritizedChoice>(
1567 true,
static_cast<std::shared_ptr<Ope>
>(args)...);
1570inline std::shared_ptr<Ope>
zom(
const std::shared_ptr<Ope> &ope) {
1574inline std::shared_ptr<Ope>
oom(
const std::shared_ptr<Ope> &ope) {
1578inline std::shared_ptr<Ope>
opt(
const std::shared_ptr<Ope> &ope) {
1582inline std::shared_ptr<Ope>
rep(
const std::shared_ptr<Ope> &ope,
size_t min,
1584 return std::make_shared<Repetition>(ope, min, max);
1587inline std::shared_ptr<Ope>
apd(
const std::shared_ptr<Ope> &ope) {
1588 return std::make_shared<AndPredicate>(ope);
1591inline std::shared_ptr<Ope>
npd(
const std::shared_ptr<Ope> &ope) {
1592 return std::make_shared<NotPredicate>(ope);
1595inline std::shared_ptr<Ope>
dic(
const std::vector<std::string> &v,
1597 return std::make_shared<Dictionary>(v, ignore_case);
1600inline std::shared_ptr<Ope>
lit(std::string &&s) {
1601 return std::make_shared<LiteralString>(s,
false);
1604inline std::shared_ptr<Ope>
liti(std::string &&s) {
1605 return std::make_shared<LiteralString>(s,
true);
1608inline std::shared_ptr<Ope>
cls(
const std::string &s) {
1609 return std::make_shared<CharacterClass>(s,
false,
false);
1612inline std::shared_ptr<Ope>
1613cls(
const std::vector<std::pair<char32_t, char32_t>> &ranges,
1614 bool ignore_case =
false) {
1615 return std::make_shared<CharacterClass>(ranges,
false, ignore_case);
1618inline std::shared_ptr<Ope>
ncls(
const std::string &s) {
1619 return std::make_shared<CharacterClass>(s,
true,
false);
1622inline std::shared_ptr<Ope>
1623ncls(
const std::vector<std::pair<char32_t, char32_t>> &ranges,
1624 bool ignore_case =
false) {
1625 return std::make_shared<CharacterClass>(ranges,
true, ignore_case);
1628inline std::shared_ptr<Ope>
chr(
char dt) {
1629 return std::make_shared<Character>(dt);
1632inline std::shared_ptr<Ope>
dot() {
return std::make_shared<AnyCharacter>(); }
1634inline std::shared_ptr<Ope>
csc(
const std::shared_ptr<Ope> &ope) {
1635 return std::make_shared<CaptureScope>(ope);
1638inline std::shared_ptr<Ope>
cap(
const std::shared_ptr<Ope> &ope,
1640 return std::make_shared<Capture>(ope, ma);
1643inline std::shared_ptr<Ope>
tok(
const std::shared_ptr<Ope> &ope) {
1644 return std::make_shared<TokenBoundary>(ope);
1647inline std::shared_ptr<Ope>
ign(
const std::shared_ptr<Ope> &ope) {
1648 return std::make_shared<Ignore>(ope);
1651inline std::shared_ptr<Ope>
1655 return std::make_shared<User>(fn);
1658inline std::shared_ptr<Ope>
ref(
const Grammar &grammar,
const std::string &name,
1659 const char *s,
bool is_macro,
1660 const std::vector<std::shared_ptr<Ope>> &args) {
1661 return std::make_shared<Reference>(grammar, name, s, is_macro, args);
1664inline std::shared_ptr<Ope>
wsp(
const std::shared_ptr<Ope> &ope) {
1665 return std::make_shared<Whitespace>(std::make_shared<Ignore>(ope));
1668inline std::shared_ptr<Ope>
bkr(std::string &&name) {
1669 return std::make_shared<BackReference>(name);
1672inline std::shared_ptr<Ope>
pre(
const std::shared_ptr<Ope> &atom,
1673 const std::shared_ptr<Ope> &binop,
1676 return std::make_shared<PrecedenceClimbing>(atom, binop, info, rule);
1679inline std::shared_ptr<Ope>
rec(
const std::shared_ptr<Ope> &ope) {
1680 return std::make_shared<Recovery>(ope);
1683inline std::shared_ptr<Ope>
cut() {
return std::make_shared<Cut>(); }
1756 for (
auto op : ope.
opes_) {
1761 for (
auto op : ope.
opes_) {
1779 std::unordered_map<void *, size_t>
ids;
1786 for (
auto op : ope.
opes_) {
1809 for (
auto op : ope.
opes_) {
1814 for (
auto op : ope.
opes_) {
1868 for (
auto op : ope.
opes_) {
1879 for (
auto op : ope.
opes_) {
1888 ope.
ope_->accept(*
this);
1892 ope.
ope_->accept(*
this);
1896 ope.
ope_->accept(*
this);
1930 std::unordered_map<std::string, bool> &has_error_cache)
1935 for (
auto op : ope.
opes_) {
1941 if (ope.
min_ == 0) {
1944 ope.
ope_->accept(*
this);
1972 std::vector<std::pair<const char *, std::string>> &
refs_;
1980 std::vector<std::pair<const char *, std::string>> &refs,
1981 std::unordered_map<std::string, bool> &has_error_cache)
1983 refs_.emplace_back(s, name);
1987 std::unordered_map<std::string, bool> &has_error_cache)
1991 for (
auto op : ope.
opes_) {
1997 for (
auto op : ope.
opes_) {
2003 if (ope.
max_ == std::numeric_limits<size_t>::max()) {
2005 ope.
ope_->accept(vis);
2012 ope.
ope_->accept(*
this);
2033 std::vector<std::pair<const char *, std::string>> &
refs_;
2041 const std::vector<std::string> ¶ms)
2045 for (
auto op : ope.
opes_) {
2050 for (
auto op : ope.
opes_) {
2068 std::unordered_map<std::string, const char *>
error_s;
2084 for (
auto op : ope.
opes_) {
2089 for (
auto op : ope.
opes_) {
2116 const std::vector<std::string> ¶ms)
2120 std::vector<std::shared_ptr<Ope>> opes;
2121 for (
auto o : ope.
opes_) {
2125 found_ope = std::make_shared<Sequence>(opes);
2128 std::vector<std::shared_ptr<Ope>> opes;
2129 for (
auto o : ope.
opes_) {
2133 found_ope = std::make_shared<PrioritizedChoice>(opes);
2136 ope.
ope_->accept(*
this);
2140 ope.
ope_->accept(*
this);
2144 ope.
ope_->accept(*
this);
2157 ope.
ope_->accept(*
this);
2161 ope.
ope_->accept(*
this);
2165 ope.
ope_->accept(*
this);
2169 ope.
ope_->accept(*
this);
2176 ope.
ope_->accept(*
this);
2180 ope.
atom_->accept(*
this);
2184 ope.
ope_->accept(*
this);
2192 const std::vector<std::shared_ptr<Ope>> &
args_;
2226 operator std::shared_ptr<Ope>() {
2227 return std::make_shared<WeakHolder>(
holder_);
2236 Log log =
nullptr)
const {
2243 Log log =
nullptr)
const {
2245 return parse(s, n, path, log);
2249 const char *path =
nullptr,
Log log =
nullptr)
const {
2255 Log log =
nullptr)
const {
2257 return parse(s, n, dt, path, log);
2260 template <
typename T>
2262 const char *path =
nullptr,
2263 Log log =
nullptr)
const {
2266 auto r =
parse_core(s, n, vs, dt, path, log);
2267 if (r.ret && !vs.empty() && vs.front().has_value()) {
2268 val = std::any_cast<T>(vs[0]);
2273 template <
typename T>
2275 Log log =
nullptr)
const {
2280 template <
typename T>
2282 const char *path =
nullptr,
2283 Log log =
nullptr)
const {
2285 auto r =
parse_core(s, n, vs, dt, path, log);
2286 if (r.ret && !vs.empty() && vs.front().has_value()) {
2287 val = std::any_cast<T>(vs[0]);
2292 template <
typename T>
2294 const char *path =
nullptr,
2295 Log log =
nullptr)
const {
2300#if defined(__cpp_lib_char8_t)
2301 Result parse(
const char8_t *s,
size_t n,
const char *path =
nullptr,
2302 Log log =
nullptr)
const {
2303 return parse(
reinterpret_cast<const char *
>(s), n, path, log);
2306 Result parse(
const char8_t *s,
const char *path =
nullptr,
2307 Log log =
nullptr)
const {
2308 return parse(
reinterpret_cast<const char *
>(s), path, log);
2311 Result parse(
const char8_t *s,
size_t n, std::any &dt,
2312 const char *path =
nullptr,
Log log =
nullptr)
const {
2313 return parse(
reinterpret_cast<const char *
>(s), n, dt, path, log);
2316 Result parse(
const char8_t *s, std::any &dt,
const char *path =
nullptr,
2317 Log log =
nullptr)
const {
2318 return parse(
reinterpret_cast<const char *
>(s), dt, path, log);
2321 template <
typename T>
2323 const char *path =
nullptr,
2324 Log log =
nullptr)
const {
2329 template <
typename T>
2331 const char *path =
nullptr,
2332 Log log =
nullptr)
const {
2337 template <
typename T>
2339 const char *path =
nullptr,
2340 Log log =
nullptr)
const {
2345 template <
typename T>
2347 const char *path =
nullptr,
2348 Log log =
nullptr)
const {
2378 const char *
s_ =
nullptr;
2379 std::pair<size_t, size_t>
line_ = {1, 1};
2387 std::function<void(
const Context &c,
const char *s,
size_t n, std::any &dt)>
2389 std::function<void(
const Context &c,
const char *s,
size_t n,
size_t matchlen,
2390 std::any &value, std::any &dt)>
2429 const char *path,
Log log)
const {
2432 std::shared_ptr<Ope> ope =
holder_;
2434 std::any trace_data;
2447 auto save_ignore_trace_state = c.ignore_trace_state;
2448 c.ignore_trace_state = !c.verbose_trace;
2450 scope_exit([&]() { c.ignore_trace_state = save_ignore_trace_state; });
2453 if (
fail(len)) {
return Result{
false, c.recovered, i, c.error_info}; }
2458 auto len = ope->parse(s + i, n - i, vs, c, dt);
2464 if (c.error_info.error_pos - c.s < s + i - c.s) {
2465 c.error_info.message_pos = s + i;
2466 c.error_info.message =
"expected end of input";
2472 return Result{ret, c.recovered, i, c.error_info};
2488 Context &c, std::any &dt,
const std::string &
lit,
2489 std::once_flag &init_is_word,
bool &is_word,
2492 for (; i <
lit.size(); i++) {
2493 if (i >= n || (ignore_case ? (std::tolower(s[i]) != std::tolower(
lit[i]))
2494 : (s[i] !=
lit[i]))) {
2496 return static_cast<size_t>(-1);
2507 std::call_once(init_is_word, [&]() {
2509 Context dummy_c(
nullptr, c.
s, c.
l, 0,
nullptr,
nullptr,
false,
nullptr,
2510 nullptr,
nullptr,
false,
nullptr);
2514 c.
wordOpe->parse(
lit.data(),
lit.size(), dummy_vs, dummy_c, dummy_dt);
2520 Context dummy_c(
nullptr, c.
s, c.
l, 0,
nullptr,
nullptr,
false,
nullptr,
2521 nullptr,
nullptr,
false,
nullptr);
2525 auto len = ope.
parse(s + i, n - i, dummy_vs, dummy_c, dummy_dt);
2542 if (
fail(len)) {
return len; }
2551 return c_->line_info(
sv_.data());
2561 !unexpected_token.empty()) {
2564 auto unexpected_char = unexpected_token.substr(
2572 log(line.first, line.second, msg,
label);
2581 msg =
"syntax error.";
2583 msg =
"syntax error";
2587 !unexpected_token.empty()) {
2588 msg +=
", unexpected '";
2589 msg += unexpected_token;
2593 auto first_item =
true;
2599 if (!(error_rule && error_rule->name[0] ==
'_')) {
2600 msg += (first_item ?
", expecting " :
", ");
2601 if (error_literal) {
2603 msg += error_literal;
2606 msg +=
"<" + error_rule->name +
">";
2607 if (
label.empty()) {
label = error_rule->name; }
2616 log(line.first, line.second, msg,
label);
2629 const char *error_literal =
nullptr;
2633 error_literal = literal;
2636 auto ope = rule->get_core_operator();
2638 token && token[0] !=
'\0') {
2639 error_literal = token;
2645 if (r->is_token()) {
break; }
2648 if (error_literal || error_rule) {
2677 Context &c, std::any &dt)
const {
2689 std::any &dt)
const {
2691 auto i =
trie_.match(s, n,
id);
2695 return static_cast<size_t>(-1);
2710 Context dummy_c(
nullptr, c.
s, c.
l, 0,
nullptr,
nullptr,
false,
nullptr,
2711 nullptr,
nullptr,
false,
nullptr);
2715 auto len = ope.
parse(s + i, n - i, dummy_vs, dummy_c, dummy_dt);
2732 if (
fail(len)) {
return len; }
2741 std::any &dt)
const {
2748 std::any &dt)
const {
2758 len =
ope_->parse(s, n, vs, c, dt);
2762 vs.
tokens.emplace_back(std::string_view(s, len));
2766 auto l = c.
whitespaceOpe->parse(s + len, n - len, vs, c, dt);
2767 if (
fail(l)) {
return l; }
2776 Context &c, std::any &dt)
const {
2778 throw std::logic_error(
"Uninitialized definition ope was used...");
2784 auto len =
ope_->parse(s, n, vs, c, dt);
2793 if (outer_->enter) { outer_->enter(c, s, n, dt); }
2797 if (
outer_->leave) {
outer_->leave(c, s, n, len, a_val, dt); }
2801 len =
ope_->parse(s, n, chvs, c, dt);
2806 chvs.sv_ = std::string_view(s, len);
2807 chvs.name_ =
outer_->name;
2809 auto ope_ptr =
ope_.get();
2812 if (tok_ptr) { ope_ptr = tok_ptr->ope_.get(); }
2816 chvs.choice_count_ = 0;
2821 if (
outer_->predicate && !
outer_->predicate(chvs, dt, msg)) {
2827 len =
static_cast<size_t>(-1);
2840 if (c.
log && !
outer_->error_message.empty() &&
2850 if (!outer_->ignoreSemanticValue) {
2851 vs.emplace_back(std::move(val));
2852 vs.tags.emplace_back(
str2tag(outer_->name));
2861 return outer_->action(vs, dt);
2862 }
else if (vs.empty()) {
2865 return std::move(vs.front());
2878 Context &c, std::any &dt)
const {
2888 if (
rule_->is_macro) {
2893 std::vector<std::shared_ptr<Ope>> args;
2894 for (
auto arg :
args_) {
2896 args.emplace_back(std::move(vis.
found_ope));
2902 return ope->parse(s, n, vs, c, dt);
2905 c.
push_args(std::vector<std::shared_ptr<Ope>>());
2908 return ope->parse(s, n, vs, c, dt);
2913 return args[
iarg_]->parse(s, n, vs, c, dt);
2918 return rule_->holder_;
2923 std::any &dt)
const {
2925 for (
auto i = size - 1; i >= 0; i--) {
2926 auto index =
static_cast<size_t>(i);
2928 if (cs.find(
name_) != cs.end()) {
2930 std::once_flag init_is_word;
2931 auto is_word =
false;
2938 return static_cast<size_t>(-1);
2943 if (
rule_.is_macro) {
2947 auto arg = args[iarg];
2957 size_t min_prec)
const {
2958 auto len =
atom_->parse(s, n, vs, c, dt);
2959 if (
fail(len)) {
return len; }
2963 auto action = std::move(rule.action);
2968 return action(vs2, dt2);
2969 }
else if (!vs2.empty()) {
2974 auto action_se =
scope_exit([&]() { rule.action = std::move(action); });
2978 std::vector<std::any> save_values(vs.begin(), vs.end());
2979 auto save_tokens = vs.
tokens;
2982 auto chlen =
binop_->parse(s + i, n - i, chvs, c, dt);
2985 if (
fail(chlen)) {
break; }
2988 if (it ==
info_.end()) {
break; }
2990 auto level = std::get<0>(it->second);
2991 auto assoc = std::get<1>(it->second);
2993 if (level < min_prec) {
break; }
2995 vs.emplace_back(std::move(chvs[0]));
2998 auto next_min_prec = level;
2999 if (assoc ==
'L') { next_min_prec = level + 1; }
3006 vs.assign(save_values.begin(), save_values.end());
3012 vs.emplace_back(std::move(chvs[0]));
3017 vs.
sv_ = std::string_view(s, i);
3018 val =
rule_.action(vs, dt);
3019 }
else if (!vs.empty()) {
3023 vs.emplace_back(std::move(val));
3031 std::any & )
const {
3032 const auto &rule =
dynamic_cast<Reference &
>(*ope_);
3036 auto label =
dynamic_cast<Reference *
>(rule.args_[0].get());
3037 if (label && !label->rule_->error_message.empty()) {
3045 auto len =
static_cast<size_t>(-1);
3047 auto save_log = c.
log;
3054 len = rule.parse(s, n, dummy_vs, c, dummy_dt);
3103 auto p =
static_cast<void *
>(ope.
outer_);
3104 if (
ids.count(p)) {
return; }
3105 auto id =
ids.size();
3108 ope.
ope_->accept(*
this);
3113 for (
auto arg : ope.
args_) {
3121 ope.
atom_->accept(*
this);
3122 ope.
binop_->accept(*
this);
3127 for (
auto arg : ope.
args_) {
3138 for (
auto arg : ope.
args_) {
3151 if (
done_ ==
false) {
return; }
3158 auto save_is_empty =
false;
3159 const char *save_error_s =
nullptr;
3160 std::string save_error_name;
3162 auto it = ope.
opes_.begin();
3163 while (it != ope.
opes_.end()) {
3164 (*it)->accept(*
this);
3167 while (it != ope.
opes_.end()) {
3195 auto it = std::find_if(
refs_.begin(),
refs_.end(),
3196 [&](
const std::pair<const char *, std::string> &
ref) {
3197 return ope.name_ == ref.second;
3199 if (it !=
refs_.end()) {
return; }
3209 auto it1 = std::find_if(
refs_.begin(),
refs_.end(),
3210 [&](
const std::pair<const char *, std::string> &
ref) {
3211 return ope.name_ == ref.second;
3213 if (it1 !=
refs_.end()) {
return; }
3228 for (
auto arg : ope.
args_) {
3236 if (it !=
params_.end()) {
return; }
3244 if (rule.is_macro) {
3253 for (
auto arg : ope.
args_) {
3261 auto found_param =
false;
3262 for (
size_t i = 0; i <
params_.size(); i++) {
3263 const auto ¶m =
params_[i];
3264 if (param == ope.
name_) {
3277 for (
auto arg : ope.
args_) {
3283 for (
size_t i = 0; i <
args_.size(); i++) {
3284 const auto &name =
params_[i];
3285 if (name == ope.
name_) {
3297using Rules = std::unordered_map<std::string, std::shared_ptr<Ope>>;
3308 Log log, std::string_view start) {
3315 std::any dt = &data;
3319 return r.ret && r.len == n;
3322#if defined(__cpp_lib_char8_t)
3323 static bool parse_test(
const char *d,
const char8_t *s) {
3324 return parse_test(d,
reinterpret_cast<const char *
>(s));
3366 g[
"Grammar"] <=
seq(
g[
"Spacing"],
oom(
g[
"Definition"]),
g[
"EndOfFile"]);
3368 cho(
seq(
g[
"Ignore"],
g[
"IdentCont"],
g[
"Parameters"],
g[
"LEFTARROW"],
3369 g[
"Expression"],
opt(
g[
"Instruction"])),
3370 seq(
g[
"Ignore"],
g[
"Identifier"],
g[
"LEFTARROW"],
g[
"Expression"],
3371 opt(
g[
"Instruction"])));
3372 g[
"Expression"] <=
seq(
g[
"Sequence"],
zom(
seq(
g[
"SLASH"],
g[
"Sequence"])));
3373 g[
"Sequence"] <=
zom(
cho(
g[
"CUT"],
g[
"Prefix"]));
3374 g[
"Prefix"] <=
seq(
opt(
cho(
g[
"AND"],
g[
"NOT"])),
g[
"SuffixWithLabel"]);
3375 g[
"SuffixWithLabel"] <=
3376 seq(
g[
"Suffix"],
opt(
seq(
g[
"LABEL"],
g[
"Identifier"])));
3377 g[
"Suffix"] <=
seq(
g[
"Primary"],
opt(
g[
"Loop"]));
3378 g[
"Loop"] <=
cho(
g[
"QUESTION"],
g[
"STAR"],
g[
"PLUS"],
g[
"Repetition"]);
3379 g[
"Primary"] <=
cho(
seq(
g[
"Ignore"],
g[
"IdentCont"],
g[
"Arguments"],
3380 npd(
g[
"LEFTARROW"])),
3381 seq(
g[
"Ignore"],
g[
"Identifier"],
3383 seq(
g[
"OPEN"],
g[
"Expression"],
g[
"CLOSE"]),
3384 seq(
g[
"BeginTok"],
g[
"Expression"],
g[
"EndTok"]),
3386 seq(
g[
"BeginCap"],
g[
"Expression"],
g[
"EndCap"]),
3387 g[
"BackRef"],
g[
"DictionaryI"],
g[
"LiteralI"],
3388 g[
"Dictionary"],
g[
"Literal"],
g[
"NegatedClassI"],
3389 g[
"NegatedClass"],
g[
"ClassI"],
g[
"Class"],
g[
"DOT"]);
3391 g[
"Identifier"] <=
seq(
g[
"IdentCont"],
g[
"Spacing"]);
3392 g[
"IdentCont"] <=
tok(
seq(
g[
"IdentStart"],
zom(
g[
"IdentRest"])));
3394 const static std::vector<std::pair<char32_t, char32_t>> range = {
3399 g[
"IdentRest"] <=
cho(
g[
"IdentStart"],
cls(
"0-9"));
3401 g[
"Dictionary"] <=
seq(
g[
"LiteralD"],
oom(
seq(
g[
"PIPE"],
g[
"LiteralD"])));
3404 seq(
g[
"LiteralID"],
oom(
seq(
g[
"PIPE"],
g[
"LiteralID"])));
3407 cls(
"'"),
g[
"Spacing"]),
3409 cls(
"\""),
g[
"Spacing"]));
3410 g[
"Literal"] <= lit_ope;
3411 g[
"LiteralD"] <= lit_ope;
3413 auto lit_case_ignore_ope =
3418 g[
"LiteralI"] <= lit_case_ignore_ope;
3419 g[
"LiteralID"] <= lit_case_ignore_ope;
3429 g[
"NegatedClass"] <=
seq(
lit(
"[^"),
3432 g[
"NegatedClassI"] <=
seq(
lit(
"[^"),
3434 lit(
"]i"),
g[
"Spacing"]);
3448 rep(
cls(
"0-9a-fA-F"), 4, 4)),
3449 rep(
cls(
"0-9a-fA-F"), 4, 5))),
3453 seq(
g[
"BeginBracket"],
g[
"RepetitionRange"],
g[
"EndBracket"]);
3454 g[
"RepetitionRange"] <=
cho(
seq(
g[
"Number"],
g[
"COMMA"],
g[
"Number"]),
3455 seq(
g[
"Number"],
g[
"COMMA"]),
g[
"Number"],
3456 seq(
g[
"COMMA"],
g[
"Number"]));
3457 g[
"Number"] <=
seq(
oom(
cls(
"0-9")),
g[
"Spacing"]);
3459 g[
"CapScope"] <=
seq(
g[
"BeginCapScope"],
g[
"Expression"],
g[
"EndCapScope"]);
3462 ~g[
"SLASH"] <=
seq(
chr(
'/'),
g[
"Spacing"]);
3463 ~g[
"PIPE"] <=
seq(
chr(
'|'),
g[
"Spacing"]);
3464 g[
"AND"] <=
seq(
chr(
'&'),
g[
"Spacing"]);
3465 g[
"NOT"] <=
seq(
chr(
'!'),
g[
"Spacing"]);
3466 g[
"QUESTION"] <=
seq(
chr(
'?'),
g[
"Spacing"]);
3467 g[
"STAR"] <=
seq(
chr(
'*'),
g[
"Spacing"]);
3468 g[
"PLUS"] <=
seq(
chr(
'+'),
g[
"Spacing"]);
3469 ~g[
"OPEN"] <=
seq(
chr(
'('),
g[
"Spacing"]);
3470 ~g[
"CLOSE"] <=
seq(
chr(
')'),
g[
"Spacing"]);
3471 g[
"DOT"] <=
seq(
chr(
'.'),
g[
"Spacing"]);
3476 ~g[
"Spacing"] <=
zom(
cho(
g[
"Space"],
g[
"Comment"]));
3479 g[
"Space"] <=
cho(
chr(
' '),
chr(
'\t'),
g[
"EndOfLine"]);
3483 ~g[
"BeginTok"] <=
seq(
chr(
'<'),
g[
"Spacing"]);
3484 ~g[
"EndTok"] <=
seq(
chr(
'>'),
g[
"Spacing"]);
3486 ~g[
"BeginCapScope"] <=
seq(
chr(
'$'),
chr(
'('),
g[
"Spacing"]);
3487 ~g[
"EndCapScope"] <=
seq(
chr(
')'),
g[
"Spacing"]);
3489 g[
"BeginCap"] <=
seq(
chr(
'$'),
tok(
g[
"IdentCont"]),
chr(
'<'),
g[
"Spacing"]);
3490 ~g[
"EndCap"] <=
seq(
chr(
'>'),
g[
"Spacing"]);
3492 g[
"BackRef"] <=
seq(
chr(
'$'),
tok(
g[
"IdentCont"]),
g[
"Spacing"]);
3494 g[
"IGNORE"] <=
chr(
'~');
3496 g[
"Ignore"] <=
opt(
g[
"IGNORE"]);
3497 g[
"Parameters"] <=
seq(
g[
"OPEN"],
g[
"Identifier"],
3498 zom(
seq(
g[
"COMMA"],
g[
"Identifier"])),
g[
"CLOSE"]);
3499 g[
"Arguments"] <=
seq(
g[
"OPEN"],
g[
"Expression"],
3500 zom(
seq(
g[
"COMMA"],
g[
"Expression"])),
g[
"CLOSE"]);
3501 ~g[
"COMMA"] <=
seq(
chr(
','),
g[
"Spacing"]);
3505 seq(
g[
"BeginBracket"],
3506 opt(
seq(
g[
"InstructionItem"],
zom(
seq(
g[
"InstructionItemSeparator"],
3507 g[
"InstructionItem"])))),
3509 g[
"InstructionItem"] <=
3510 cho(
g[
"PrecedenceClimbing"],
g[
"ErrorMessage"],
g[
"NoAstOpt"]);
3511 ~g[
"InstructionItemSeparator"] <=
seq(
chr(
';'),
g[
"Spacing"]);
3513 ~g[
"SpacesZom"] <=
zom(
g[
"Space"]);
3514 ~g[
"SpacesOom"] <=
oom(
g[
"Space"]);
3515 ~g[
"BeginBracket"] <=
seq(
chr(
'{'),
g[
"Spacing"]);
3516 ~g[
"EndBracket"] <=
seq(
chr(
'}'),
g[
"Spacing"]);
3519 g[
"PrecedenceClimbing"] <=
3520 seq(
lit(
"precedence"),
g[
"SpacesOom"],
g[
"PrecedenceInfo"],
3521 zom(
seq(
g[
"SpacesOom"],
g[
"PrecedenceInfo"])),
g[
"SpacesZom"]);
3522 g[
"PrecedenceInfo"] <=
3523 seq(
g[
"PrecedenceAssoc"],
3524 oom(
seq(
ign(
g[
"SpacesOom"]),
g[
"PrecedenceOpe"])));
3525 g[
"PrecedenceOpe"] <=
3534 g[
"PrecedenceAssoc"] <=
cls(
"LR");
3537 g[
"ErrorMessage"] <=
seq(
lit(
"error_message"),
g[
"SpacesOom"],
3538 g[
"LiteralD"],
g[
"SpacesZom"]);
3541 g[
"NoAstOpt"] <=
seq(
lit(
"no_ast_opt"),
g[
"SpacesZom"]);
3545 x.second.name = x.first;
3551 auto &data = *std::any_cast<Data *>(dt);
3553 auto is_macro = vs.
choice() == 0;
3554 auto ignore = std::any_cast<bool>(vs[0]);
3555 auto name = std::any_cast<std::string>(vs[1]);
3557 std::vector<std::string> params;
3558 std::shared_ptr<Ope> ope;
3559 auto has_instructions =
false;
3562 params = std::any_cast<std::vector<std::string>>(vs[2]);
3563 ope = std::any_cast<std::shared_ptr<Ope>>(vs[4]);
3564 if (vs.size() == 6) { has_instructions =
true; }
3566 ope = std::any_cast<std::shared_ptr<Ope>>(vs[3]);
3567 if (vs.size() == 5) { has_instructions =
true; }
3570 if (has_instructions) {
3571 auto index = is_macro ? 5 : 4;
3572 std::unordered_set<std::string> types;
3573 for (
const auto &instruction :
3574 std::any_cast<std::vector<Instruction>>(vs[index])) {
3575 const auto &type = instruction.type;
3576 if (types.find(type) == types.end()) {
3577 data.instructions[name].push_back(instruction);
3578 types.insert(instruction.type);
3579 if (type ==
"declare_symbol" || type ==
"check_symbol") {
3583 data.duplicates_of_instruction.emplace_back(type,
3584 instruction.sv.data());
3589 auto &grammar = *data.grammar;
3590 if (!grammar.count(name)) {
3591 auto &rule = grammar[name];
3594 rule.s_ = vs.
sv().data();
3596 rule.ignoreSemanticValue = ignore;
3597 rule.is_macro = is_macro;
3598 rule.params = params;
3600 if (data.start.empty()) {
3601 data.start = rule.name;
3602 data.start_pos = rule.s_;
3605 data.duplicates_of_definition.emplace_back(name, vs.
sv().data());
3609 g[
"Definition"].enter = [](
const Context & ,
const char * ,
3610 size_t , std::any &dt) {
3611 auto &data = *std::any_cast<Data *>(dt);
3612 data.captures_in_current_definition.clear();
3616 if (vs.size() == 1) {
3617 return std::any_cast<std::shared_ptr<Ope>>(vs[0]);
3619 std::vector<std::shared_ptr<Ope>> opes;
3620 for (
auto i = 0u; i < vs.size(); i++) {
3621 opes.emplace_back(std::any_cast<std::shared_ptr<Ope>>(vs[i]));
3623 const std::shared_ptr<Ope> ope =
3624 std::make_shared<PrioritizedChoice>(opes);
3632 }
else if (vs.size() == 1) {
3633 return std::any_cast<std::shared_ptr<Ope>>(vs[0]);
3635 std::vector<std::shared_ptr<Ope>> opes;
3636 for (
const auto &x : vs) {
3637 opes.emplace_back(std::any_cast<std::shared_ptr<Ope>>(x));
3639 const std::shared_ptr<Ope> ope = std::make_shared<Sequence>(opes);
3645 std::shared_ptr<Ope> ope;
3646 if (vs.size() == 1) {
3647 ope = std::any_cast<std::shared_ptr<Ope>>(vs[0]);
3649 assert(vs.size() == 2);
3650 auto tok = std::any_cast<char>(vs[0]);
3651 ope = std::any_cast<std::shared_ptr<Ope>>(vs[1]);
3662 auto ope = std::any_cast<std::shared_ptr<Ope>>(vs[0]);
3663 if (vs.size() == 1) {
3666 assert(vs.size() == 2);
3667 auto &data = *std::any_cast<Data *>(dt);
3668 const auto &ident = std::any_cast<std::string>(vs[1]);
3669 auto label =
ref(*data.grammar, ident, vs.
sv().data(),
false, {});
3671 vs.
sv().data(),
true, {label}));
3679 std::pair<size_t, size_t> range;
3683 auto ope = std::any_cast<std::shared_ptr<Ope>>(vs[0]);
3684 if (vs.size() == 1) {
3687 assert(vs.size() == 2);
3688 auto loop = std::any_cast<Loop>(vs[1]);
3689 switch (loop.type) {
3690 case Loop::Type::opt:
return opt(ope);
3691 case Loop::Type::zom:
return zom(ope);
3692 case Loop::Type::oom:
return oom(ope);
3694 return rep(ope, loop.range.first, loop.range.second);
3702 return Loop{Loop::Type::opt, std::pair<size_t, size_t>()};
3704 return Loop{Loop::Type::zom, std::pair<size_t, size_t>()};
3706 return Loop{Loop::Type::oom, std::pair<size_t, size_t>()};
3708 return Loop{Loop::Type::rep,
3709 std::any_cast<std::pair<size_t, size_t>>(vs[0])};
3714 auto &data = *std::any_cast<Data *>(dt);
3719 auto is_macro = vs.
choice() == 0;
3720 auto ignore = std::any_cast<bool>(vs[0]);
3721 const auto &ident = std::any_cast<std::string>(vs[1]);
3723 std::vector<std::shared_ptr<Ope>> args;
3725 args = std::any_cast<std::vector<std::shared_ptr<Ope>>>(vs[2]);
3728 auto ope =
ref(*data.grammar, ident, vs.
sv().data(), is_macro, args);
3738 return std::any_cast<std::shared_ptr<Ope>>(vs[0]);
3741 return tok(std::any_cast<std::shared_ptr<Ope>>(vs[0]));
3744 return csc(std::any_cast<std::shared_ptr<Ope>>(vs[0]));
3747 const auto &name = std::any_cast<std::string_view>(vs[0]);
3748 auto ope = std::any_cast<std::shared_ptr<Ope>>(vs[1]);
3750 data.captures_stack.back().insert(name);
3751 data.captures_in_current_definition.insert(name);
3753 return cap(ope, [name](
const char *a_s,
size_t a_n,
Context &c) {
3755 cs[name] = std::string(a_s, a_n);
3759 return std::any_cast<std::shared_ptr<Ope>>(vs[0]);
3765 return std::string(vs.
sv().data(), vs.
sv().length());
3769 auto items = vs.
transform<std::string>();
3770 return dic(items,
false);
3773 auto items = vs.
transform<std::string>();
3774 return dic(items,
true);
3795 auto ranges = vs.
transform<std::pair<char32_t, char32_t>>();
3799 auto ranges = vs.
transform<std::pair<char32_t, char32_t>>();
3800 return cls(ranges,
true);
3803 auto ranges = vs.
transform<std::pair<char32_t, char32_t>>();
3804 return ncls(ranges);
3807 auto ranges = vs.
transform<std::pair<char32_t, char32_t>>();
3808 return ncls(ranges,
true);
3813 auto s1 = std::any_cast<std::string>(vs[0]);
3814 auto s2 = std::any_cast<std::string>(vs[1]);
3817 return std::pair(cp1, cp2);
3820 auto s = std::any_cast<std::string>(vs[0]);
3822 return std::pair(cp, cp);
3825 return std::pair<char32_t, char32_t>(0, 0);
3834 auto min = std::any_cast<size_t>(vs[0]);
3835 auto max = std::any_cast<size_t>(vs[1]);
3836 return std::pair(min, max);
3839 return std::pair(std::any_cast<size_t>(vs[0]),
3840 std::numeric_limits<size_t>::max());
3842 auto n = std::any_cast<size_t>(vs[0]);
3843 return std::pair(n, n);
3846 return std::pair(std::numeric_limits<size_t>::min(),
3847 std::any_cast<size_t>(vs[0]));
3854 g[
"CapScope"].enter = [](
const Context & ,
const char * ,
3855 size_t , std::any &dt) {
3856 auto &data = *std::any_cast<Data *>(dt);
3857 data.captures_stack.emplace_back();
3859 g[
"CapScope"].leave = [](
const Context & ,
const char * ,
3861 std::any & , std::any &dt) {
3862 auto &data = *std::any_cast<Data *>(dt);
3863 data.captures_stack.pop_back();
3879 auto &data = *std::any_cast<Data *>(dt);
3884 auto it = data.captures_stack.rbegin();
3885 while (it != data.captures_stack.rend()) {
3886 if (it->find(vs.
token()) != it->end()) {
3893 auto ptr = vs.
token().data() - 1;
3894 data.undefined_back_references.emplace_back(vs.
token(), ptr);
3900 if (data.captures_in_current_definition.find(vs.
token()) ==
3901 data.captures_in_current_definition.end()) {
3902 data.enablePackratParsing =
false;
3915 return vs.
transform<std::shared_ptr<Ope>>();
3922 auto tokens = std::any_cast<std::vector<std::string_view>>(v);
3923 auto assoc = tokens[0][0];
3924 for (
size_t i = 1; i < tokens.size(); i++) {
3925 binOpeInfo[tokens[i]] = std::pair(level, assoc);
3930 instruction.
type =
"precedence";
3931 instruction.
data = binOpeInfo;
3932 instruction.
sv = vs.sv();
3936 return vs.
transform<std::string_view>();
3943 instruction.
type =
"error_message";
3944 instruction.
data = std::any_cast<std::string>(vs[0]);
3945 instruction.
sv = vs.
sv();
3951 instruction.
type =
"no_ast_opt";
3952 instruction.
sv = vs.
sv();
3963 const char *s,
Log log) {
3966 auto atom =
seq.opes_[0];
3969 auto binop = seq1.
opes_[0];
3970 auto atom1 = seq1.opes_[1];
3976 if (!
rep.is_zom() || atom_name != atom1_name || atom_name == binop_name) {
3979 log(line.first, line.second,
3980 "'precedence' instruction cannot be applied to '" + rule.
name +
3987 rule.
holder_->ope_ =
pre(atom, binop, info, rule);
3992 log(line.first, line.second,
3993 "'precedence' instruction cannot be applied to '" + rule.
name +
4003 Log log, std::string requested_start) {
4005 auto &grammar = *data.
grammar;
4012 rule <=
ref(grammar,
"x",
"",
false, {});
4014 rule.s_ =
"[native]";
4015 rule.ignoreSemanticValue =
true;
4016 rule.is_macro =
true;
4017 rule.params = {
"x"};
4021 std::any dt = &data;
4022 auto r =
g[
"Grammar"].parse(s, n, dt,
nullptr, log);
4026 if (r.error_info.message_pos) {
4027 auto line =
line_info(s, r.error_info.message_pos);
4028 log(line.first, line.second, r.error_info.message,
4029 r.error_info.label);
4031 auto line =
line_info(s, r.error_info.error_pos);
4032 log(line.first, line.second,
"syntax error", r.error_info.label);
4039 for (
auto [user_name, user_rule] : rules) {
4040 auto name = user_name;
4041 auto ignore =
false;
4042 if (!name.empty() && name[0] ==
'~') {
4046 if (!name.empty()) {
4047 auto &rule = grammar[name];
4050 rule.ignoreSemanticValue = ignore;
4061 log(line.first, line.second,
4062 "The definition '" + name +
"' is already defined.",
"");
4073 log(line.first, line.second,
4074 "The instruction '" + type +
"' is already defined.",
"");
4085 log(line.first, line.second,
4086 "The back reference '" + name +
"' is undefined.",
"");
4093 auto start = data.
start;
4095 if (!requested_start.empty()) {
4096 if (grammar.count(requested_start)) {
4097 start = requested_start;
4101 log(line.first, line.second,
4102 "The specified start rule '" + requested_start +
4110 if (!ret) {
return {}; }
4112 auto &start_rule = grammar[start];
4116 if (start_rule.ignoreSemanticValue) {
4118 auto line =
line_info(s, start_rule.s_);
4119 log(line.first, line.second,
4120 "Ignore operator cannot be applied to '" + start_rule.name +
"'.",
4127 if (!ret) {
return {}; }
4130 auto referenced = std::unordered_set<std::string>{
4137 for (
auto &[_, rule] : grammar) {
4141 for (
const auto &[name, ptr] : vis.
error_s) {
4150 for (
auto &[name, rule] : grammar) {
4151 if (!referenced.count(name)) {
4154 auto msg =
"'" + name +
"' is not referenced.";
4155 log(line.first, line.second, msg,
"");
4160 if (!ret) {
return {}; }
4163 for (
auto &x : grammar) {
4164 auto &rule = x.second;
4172 for (
auto &[name, rule] : grammar) {
4178 log(line.first, line.second,
"'" + name +
"' is left recursive.",
"");
4184 if (!ret) {
return {}; }
4191 for (
auto &x : grammar) {
4192 auto &rule = x.second;
4193 auto ope = rule.get_core_operator();
4198 start_rule.whitespaceOpe =
wsp(rule.get_core_operator());
4206 start_rule.wordOpe = rule.get_core_operator();
4212 for (
const auto &[name, instructions] : data.
instructions) {
4213 auto &rule = grammar[name];
4215 for (
const auto &instruction : instructions) {
4216 if (instruction.type ==
"precedence") {
4218 std::any_cast<PrecedenceClimbing::BinOpeInfo>(instruction.data);
4221 }
else if (instruction.type ==
"error_message") {
4222 rule.error_message = std::any_cast<std::string>(instruction.data);
4223 }
else if (instruction.type ==
"no_ast_opt") {
4224 rule.no_ast_opt =
true;
4233 const char *s)
const {
4234 std::vector<std::pair<const char *, std::string>> refs;
4235 std::unordered_map<std::string, bool> has_error_cache;
4241 log(line.first, line.second,
4242 "infinite loop is detected in '" + vis.
error_name +
"'.",
"");
4256template <
typename Annotation>
struct AstBase :
public Annotation {
4258 const std::vector<std::shared_ptr<AstBase>> &
nodes,
4305 std::vector<std::shared_ptr<AstBase<Annotation>>>
nodes;
4310 return std::string(
token);
4318template <
typename T>
4320 std::function<std::string(
const T &ast,
int level)> fn) {
4321 const auto &ast = *ptr;
4322 for (
auto i = 0; i < level; i++) {
4325 auto name = ast.original_name;
4326 if (ast.original_choice_count > 0) {
4327 name +=
"/" + std::to_string(ast.original_choice);
4329 if (ast.name != ast.original_name) { name +=
"[" + ast.name +
"]"; }
4331 s +=
"- " + name +
" (";
4335 s +=
"+ " + name +
"\n";
4337 if (fn) { s += fn(ast, level + 1); }
4338 for (
auto node : ast.nodes) {
4343template <
typename T>
4346 std::function<std::string(
const T &ast,
int level)> fn =
nullptr) {
4356 template <
typename T>
4357 std::shared_ptr<T>
optimize(std::shared_ptr<T> original,
4358 std::shared_ptr<T> parent =
nullptr) {
4363 if (
opt && original->nodes.size() == 1) {
4364 auto child =
optimize(original->nodes[0], parent);
4365 auto ast = std::make_shared<T>(*child, original->name.data(),
4366 original->choice_count, original->position,
4367 original->length, original->choice);
4368 for (
auto node : ast->nodes) {
4374 auto ast = std::make_shared<T>(*original);
4375 ast->parent = parent;
4377 for (
auto node : original->nodes) {
4379 ast->nodes.push_back(child);
4394 auto line = vs.line_info();
4397 return std::make_shared<T>(
4398 vs.path, line.first, line.second, rule.
name.data(), vs.token(),
4399 std::distance(vs.ss, vs.sv().data()), vs.sv().length(),
4400 vs.choice_count(), vs.choice());
4404 std::make_shared<T>(vs.path, line.first, line.second, rule.
name.data(),
4405 vs.transform<std::shared_ptr<T>>(),
4406 std::distance(vs.ss, vs.sv().data()),
4407 vs.sv().length(), vs.choice_count(), vs.choice());
4409 for (
auto node : ast->nodes) {
4416#define PEG_EXPAND(...) __VA_ARGS__
4417#define PEG_CONCAT(a, b) a##b
4418#define PEG_CONCAT2(a, b) PEG_CONCAT(a, b)
4421 a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, \
4422 a17, a18, a19, a20, a21, a22, a23, a24, a25, a26, a27, a28, a29, a30, a31, \
4423 a32, a33, a34, a35, a36, a37, a38, a39, a40, a41, a42, a43, a44, a45, a46, \
4424 a47, a48, a49, a50, a51, a52, a53, a54, a55, a56, a57, a58, a59, a60, a61, \
4425 a62, a63, a64, a65, a66, a67, a68, a69, a70, a71, a72, a73, a74, a75, a76, \
4426 a77, a78, a79, a80, a81, a82, a83, a84, a85, a86, a87, a88, a89, a90, a91, \
4427 a92, a93, a94, a95, a96, a97, a98, a99, a100, ...) \
4430#define PEG_COUNT(...) \
4431 PEG_EXPAND(PEG_PICK( \
4432 __VA_ARGS__, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, \
4433 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, \
4434 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, \
4435 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, \
4436 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, \
4437 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0))
4439#define PEG_DEF_1(r) \
4440 peg::Definition r; \
4442 peg::add_ast_action(r);
4444#define PEG_DEF_2(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_1(__VA_ARGS__))
4445#define PEG_DEF_3(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_2(__VA_ARGS__))
4446#define PEG_DEF_4(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_3(__VA_ARGS__))
4447#define PEG_DEF_5(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_4(__VA_ARGS__))
4448#define PEG_DEF_6(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_5(__VA_ARGS__))
4449#define PEG_DEF_7(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_6(__VA_ARGS__))
4450#define PEG_DEF_8(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_7(__VA_ARGS__))
4451#define PEG_DEF_9(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_8(__VA_ARGS__))
4452#define PEG_DEF_10(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_9(__VA_ARGS__))
4453#define PEG_DEF_11(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_10(__VA_ARGS__))
4454#define PEG_DEF_12(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_11(__VA_ARGS__))
4455#define PEG_DEF_13(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_12(__VA_ARGS__))
4456#define PEG_DEF_14(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_13(__VA_ARGS__))
4457#define PEG_DEF_15(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_14(__VA_ARGS__))
4458#define PEG_DEF_16(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_15(__VA_ARGS__))
4459#define PEG_DEF_17(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_16(__VA_ARGS__))
4460#define PEG_DEF_18(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_17(__VA_ARGS__))
4461#define PEG_DEF_19(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_18(__VA_ARGS__))
4462#define PEG_DEF_20(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_19(__VA_ARGS__))
4463#define PEG_DEF_21(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_20(__VA_ARGS__))
4464#define PEG_DEF_22(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_21(__VA_ARGS__))
4465#define PEG_DEF_23(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_22(__VA_ARGS__))
4466#define PEG_DEF_24(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_23(__VA_ARGS__))
4467#define PEG_DEF_25(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_24(__VA_ARGS__))
4468#define PEG_DEF_26(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_25(__VA_ARGS__))
4469#define PEG_DEF_27(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_26(__VA_ARGS__))
4470#define PEG_DEF_28(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_27(__VA_ARGS__))
4471#define PEG_DEF_29(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_28(__VA_ARGS__))
4472#define PEG_DEF_30(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_29(__VA_ARGS__))
4473#define PEG_DEF_31(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_30(__VA_ARGS__))
4474#define PEG_DEF_32(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_31(__VA_ARGS__))
4475#define PEG_DEF_33(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_32(__VA_ARGS__))
4476#define PEG_DEF_34(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_33(__VA_ARGS__))
4477#define PEG_DEF_35(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_34(__VA_ARGS__))
4478#define PEG_DEF_36(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_35(__VA_ARGS__))
4479#define PEG_DEF_37(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_36(__VA_ARGS__))
4480#define PEG_DEF_38(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_37(__VA_ARGS__))
4481#define PEG_DEF_39(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_38(__VA_ARGS__))
4482#define PEG_DEF_40(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_39(__VA_ARGS__))
4483#define PEG_DEF_41(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_40(__VA_ARGS__))
4484#define PEG_DEF_42(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_41(__VA_ARGS__))
4485#define PEG_DEF_43(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_42(__VA_ARGS__))
4486#define PEG_DEF_44(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_43(__VA_ARGS__))
4487#define PEG_DEF_45(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_44(__VA_ARGS__))
4488#define PEG_DEF_46(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_45(__VA_ARGS__))
4489#define PEG_DEF_47(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_46(__VA_ARGS__))
4490#define PEG_DEF_48(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_47(__VA_ARGS__))
4491#define PEG_DEF_49(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_48(__VA_ARGS__))
4492#define PEG_DEF_50(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_49(__VA_ARGS__))
4493#define PEG_DEF_51(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_50(__VA_ARGS__))
4494#define PEG_DEF_52(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_51(__VA_ARGS__))
4495#define PEG_DEF_53(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_52(__VA_ARGS__))
4496#define PEG_DEF_54(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_53(__VA_ARGS__))
4497#define PEG_DEF_55(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_54(__VA_ARGS__))
4498#define PEG_DEF_56(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_55(__VA_ARGS__))
4499#define PEG_DEF_57(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_56(__VA_ARGS__))
4500#define PEG_DEF_58(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_57(__VA_ARGS__))
4501#define PEG_DEF_59(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_58(__VA_ARGS__))
4502#define PEG_DEF_60(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_59(__VA_ARGS__))
4503#define PEG_DEF_61(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_60(__VA_ARGS__))
4504#define PEG_DEF_62(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_61(__VA_ARGS__))
4505#define PEG_DEF_63(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_62(__VA_ARGS__))
4506#define PEG_DEF_64(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_63(__VA_ARGS__))
4507#define PEG_DEF_65(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_64(__VA_ARGS__))
4508#define PEG_DEF_66(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_65(__VA_ARGS__))
4509#define PEG_DEF_67(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_66(__VA_ARGS__))
4510#define PEG_DEF_68(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_67(__VA_ARGS__))
4511#define PEG_DEF_69(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_68(__VA_ARGS__))
4512#define PEG_DEF_70(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_69(__VA_ARGS__))
4513#define PEG_DEF_71(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_70(__VA_ARGS__))
4514#define PEG_DEF_72(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_71(__VA_ARGS__))
4515#define PEG_DEF_73(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_72(__VA_ARGS__))
4516#define PEG_DEF_74(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_73(__VA_ARGS__))
4517#define PEG_DEF_75(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_74(__VA_ARGS__))
4518#define PEG_DEF_76(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_75(__VA_ARGS__))
4519#define PEG_DEF_77(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_76(__VA_ARGS__))
4520#define PEG_DEF_78(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_77(__VA_ARGS__))
4521#define PEG_DEF_79(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_78(__VA_ARGS__))
4522#define PEG_DEF_80(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_79(__VA_ARGS__))
4523#define PEG_DEF_81(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_80(__VA_ARGS__))
4524#define PEG_DEF_82(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_81(__VA_ARGS__))
4525#define PEG_DEF_83(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_82(__VA_ARGS__))
4526#define PEG_DEF_84(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_83(__VA_ARGS__))
4527#define PEG_DEF_85(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_84(__VA_ARGS__))
4528#define PEG_DEF_86(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_85(__VA_ARGS__))
4529#define PEG_DEF_87(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_86(__VA_ARGS__))
4530#define PEG_DEF_88(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_87(__VA_ARGS__))
4531#define PEG_DEF_89(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_88(__VA_ARGS__))
4532#define PEG_DEF_90(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_89(__VA_ARGS__))
4533#define PEG_DEF_91(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_90(__VA_ARGS__))
4534#define PEG_DEF_92(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_91(__VA_ARGS__))
4535#define PEG_DEF_93(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_92(__VA_ARGS__))
4536#define PEG_DEF_94(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_93(__VA_ARGS__))
4537#define PEG_DEF_95(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_94(__VA_ARGS__))
4538#define PEG_DEF_96(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_95(__VA_ARGS__))
4539#define PEG_DEF_97(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_96(__VA_ARGS__))
4540#define PEG_DEF_98(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_97(__VA_ARGS__))
4541#define PEG_DEF_99(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_98(__VA_ARGS__))
4542#define PEG_DEF_100(r1, ...) PEG_EXPAND(PEG_DEF_1(r1) PEG_DEF_99(__VA_ARGS__))
4544#define AST_DEFINITIONS(...) \
4545 PEG_EXPAND(PEG_CONCAT2(PEG_DEF_, PEG_COUNT(__VA_ARGS__))(__VA_ARGS__))
4556 std::string_view start = {}) {
4560 parser(
const char *s,
size_t n, std::string_view start = {})
4563 parser(std::string_view sv,
const Rules &rules, std::string_view start = {})
4564 :
parser(sv.data(), sv.size(), rules, start) {}
4566 parser(std::string_view sv, std::string_view start = {})
4569#if defined(__cpp_lib_char8_t)
4570 parser(std::u8string_view sv,
const Rules &rules, std::string_view start = {})
4571 :
parser(reinterpret_cast<const char *>(sv.data()), sv.size(), rules,
4574 parser(std::u8string_view sv, std::string_view start = {})
4575 :
parser(reinterpret_cast<const char *>(sv.data()), sv.size(),
Rules(),
4582 std::string_view start = {}) {
4595 std::string_view start = {}) {
4596 return load_grammar(sv.data(), sv.size(), rules, start);
4603 bool parse_n(
const char *s,
size_t n,
const char *path =
nullptr)
const {
4605 const auto &rule = (*grammar_)[
start_];
4606 auto result = rule.parse(s, n, path,
log_);
4612 bool parse_n(
const char *s,
size_t n, std::any &dt,
4613 const char *path =
nullptr)
const {
4615 const auto &rule = (*grammar_)[
start_];
4616 auto result = rule.parse(s, n, dt, path,
log_);
4622 template <
typename T>
4624 const char *path =
nullptr)
const {
4626 const auto &rule = (*grammar_)[
start_];
4627 auto result = rule.parse_and_get_value(s, n, val, path,
log_);
4633 template <
typename T>
4634 bool parse_n(
const char *s,
size_t n, std::any &dt, T &val,
4635 const char *path =
nullptr)
const {
4637 const auto &rule = (*grammar_)[
start_];
4638 auto result = rule.parse_and_get_value(s, n, dt, val, path,
log_);
4644 bool parse(std::string_view sv,
const char *path =
nullptr)
const {
4645 return parse_n(sv.data(), sv.size(), path);
4648 bool parse(std::string_view sv, std::any &dt,
4649 const char *path =
nullptr)
const {
4650 return parse_n(sv.data(), sv.size(), dt, path);
4653 template <
typename T>
4654 bool parse(std::string_view sv, T &val,
const char *path =
nullptr)
const {
4655 return parse_n(sv.data(), sv.size(), val, path);
4658 template <
typename T>
4659 bool parse(std::string_view sv, std::any &dt, T &val,
4660 const char *path =
nullptr)
const {
4661 return parse_n(sv.data(), sv.size(), dt, val, path);
4664#if defined(__cpp_lib_char8_t)
4665 bool parse(std::u8string_view sv,
const char *path =
nullptr)
const {
4666 return parse_n(
reinterpret_cast<const char *
>(sv.data()), sv.size(), path);
4669 bool parse(std::u8string_view sv, std::any &dt,
4670 const char *path =
nullptr)
const {
4671 return parse_n(
reinterpret_cast<const char *
>(sv.data()), sv.size(), dt,
4675 template <
typename T>
4676 bool parse(std::u8string_view sv, T &val,
const char *path =
nullptr)
const {
4677 return parse_n(
reinterpret_cast<const char *
>(sv.data()), sv.size(), val,
4681 template <
typename T>
4682 bool parse(std::u8string_view sv, std::any &dt, T &val,
4683 const char *path =
nullptr)
const {
4684 return parse_n(
reinterpret_cast<const char *
>(sv.data()), sv.size(), dt,
4697 auto &rule = (*grammar_)[
start_];
4698 rule.eoi_check =
false;
4704 auto &rule = (*grammar_)[
start_];
4711 auto &rule = (*grammar_)[
start_];
4712 rule.tracer_enter = tracer_enter;
4713 rule.tracer_leave = tracer_leave;
4721 auto &rule = (*grammar_)[
start_];
4722 rule.tracer_enter = tracer_enter;
4723 rule.tracer_leave = tracer_leave;
4724 rule.tracer_start = tracer_start;
4725 rule.tracer_end = tracer_end;
4731 auto &rule = (*grammar_)[
start_];
4732 rule.verbose_trace = verbose_trace;
4737 for (
auto &[_, rule] : *
grammar_) {
4743 template <
typename T>
4745 bool opt_mode =
true)
const {
4752 std::function<
void(
size_t line,
size_t col,
const std::string &msg)>
4754 log_ = [log](
size_t line,
size_t col,
const std::string &msg,
4755 const std::string & ) { log(line, col, msg); };
4765 std::vector<std::string> rules;
4766 for (
auto &[name, rule] : *
grammar_) {
4767 if (rule.no_ast_opt) { rules.push_back(name); }
4784 [&](
auto &ope,
auto s,
auto,
auto &,
auto &c,
auto &,
auto &trace_data) {
4785 auto prev_pos = std::any_cast<size_t>(trace_data);
4786 auto pos =
static_cast<size_t>(s - c.s);
4787 auto backtrack = (pos < prev_pos ?
"*" :
"");
4789 auto level = c.trace_ids.size() - 1;
4800 os <<
"E " << pos + 1 << backtrack <<
"\t" << indent <<
"┌" << name
4801 <<
" #" << c.trace_ids.back() << std::endl;
4802 trace_data =
static_cast<size_t>(pos);
4804 [&](
auto &ope,
auto s,
auto,
auto &sv,
auto &c,
auto &,
auto len,
4806 auto pos =
static_cast<size_t>(s - c.s);
4807 if (len !=
static_cast<size_t>(-1)) { pos += len; }
4809 auto level = c.trace_ids.size() - 1;
4813 auto ret = len !=
static_cast<size_t>(-1) ?
"└o " :
"└x ";
4815 std::stringstream choice;
4816 if (sv.choice_count() > 0) {
4817 choice <<
" " << sv.choice() <<
"/" << sv.choice_count();
4820 if (!sv.tokens.empty()) {
4821 token +=
", token '";
4822 token += sv.tokens[0];
4825 std::string matched;
4830 os <<
"L " << pos + 1 <<
"\t" << indent << ret << name <<
" #"
4831 << c.trace_ids.back() << choice.str() << token << matched
4834 [&](
auto &trace_data) { trace_data =
static_cast<size_t>(0); },
4849 std::vector<Item> items;
4850 std::map<std::string, size_t> index;
4852 std::chrono::steady_clock::time_point start;
4856 [&](
auto &ope,
auto,
auto,
auto &,
auto &,
auto &, std::any &trace_data) {
4857 if (
auto holder =
dynamic_cast<const peg::Holder *
>(&ope)) {
4858 auto &stats = *std::any_cast<Stats *>(trace_data);
4860 auto &name = holder->name();
4861 if (stats.index.find(name) == stats.index.end()) {
4862 stats.index[name] = stats.index.size();
4863 stats.items.push_back({name, 0, 0});
4868 [&](
auto &ope,
auto,
auto,
auto &,
auto &,
auto &,
auto len,
4869 std::any &trace_data) {
4870 if (
auto holder =
dynamic_cast<const peg::Holder *
>(&ope)) {
4871 auto &stats = *std::any_cast<Stats *>(trace_data);
4873 auto &name = holder->name();
4874 auto index = stats.index[name];
4875 auto &stat = stats.items[index];
4876 if (len !=
static_cast<size_t>(-1)) {
4883 auto end = std::chrono::steady_clock::now();
4884 auto nano = std::chrono::duration_cast<std::chrono::microseconds>(
4887 auto sec = nano / 1000000.0;
4888 os <<
"duration: " << sec <<
"s (" << nano <<
"µs)" << std::endl
4892 size_t total_success = 0;
4893 size_t total_fail = 0;
4894 for (
auto &[name_,
success,
fail] : stats.items) {
4899 os <<
" id total % success fail "
4903 auto grand_total = total_success + total_fail;
4904 snprintf(buff, BUFSIZ,
"%4s %10zu %5s %10zu %10zu %s",
"",
4905 grand_total,
"", total_success, total_fail,
4907 os << buff << std::endl;
4909 snprintf(buff, BUFSIZ,
"%4s %10s %5s %10.2f %10.2f %s",
"",
"",
4910 "", total_success * 100.0 / grand_total,
4911 total_fail * 100.0 / grand_total,
"% success/fail");
4912 os << buff << std::endl << std::endl;
4916 for (
auto &[name_,
success,
fail] : stats.items) {
4918 auto ratio = total * 100.0 / stats.total;
4919 snprintf(buff, BUFSIZ,
"%4zu %10zu %5.2f %10zu %10zu %s",
id,
4921 os << buff << std::endl;
4927 [&](
auto &trace_data) {
4928 auto stats =
new Stats{};
4929 stats->start = std::chrono::steady_clock::now();
4932 [&](
auto &trace_data) {
4933 auto stats = std::any_cast<Stats *>(trace_data);
void operator=(F fn)
Definition peglib.h:643
Action(Action &&rhs)=default
Fty make_adaptor(F fn)
Definition peglib.h:655
std::function< std::any(SemanticValues &vs, std::any &dt)> Fty
Definition peglib.h:653
Fty fn_
Definition peglib.h:663
Action(F fn)
Definition peglib.h:642
std::any operator()(SemanticValues &vs, std::any &dt) const
Definition peglib.h:648
Action & operator=(const Action &rhs)=default
size_t parse_core(const char *s, size_t n, SemanticValues &, Context &c, std::any &dt) const override
Definition peglib.h:1142
AndPredicate(const std::shared_ptr< Ope > &ope)
Definition peglib.h:1140
std::shared_ptr< Ope > ope_
Definition peglib.h:1158
void accept(Visitor &v) override
Definition peglib.h:3081
size_t parse_core(const char *s, size_t n, SemanticValues &, Context &c, std::any &) const override
Definition peglib.h:1312
void accept(Visitor &v) override
Definition peglib.h:3087
std::string name_
Definition peglib.h:1499
BackReference(const std::string &name)
Definition peglib.h:1492
BackReference(std::string &&name)
Definition peglib.h:1490
size_t parse_core(const char *s, size_t n, SemanticValues &vs, Context &c, std::any &dt) const override
Definition peglib.h:2921
void accept(Visitor &v) override
Definition peglib.h:3097
void accept(Visitor &v) override
Definition peglib.h:3088
CaptureScope(const std::shared_ptr< Ope > &ope)
Definition peglib.h:1327
size_t parse_core(const char *s, size_t n, SemanticValues &vs, Context &c, std::any &dt) const override
Definition peglib.h:1329
std::shared_ptr< Ope > ope_
Definition peglib.h:1338
MatchAction match_action_
Definition peglib.h:1358
std::function< void(const char *s, size_t n, Context &c)> MatchAction
Definition peglib.h:1343
std::shared_ptr< Ope > ope_
Definition peglib.h:1357
void accept(Visitor &v) override
Definition peglib.h:3089
Capture(const std::shared_ptr< Ope > &ope, MatchAction ma)
Definition peglib.h:1345
size_t parse_core(const char *s, size_t n, SemanticValues &vs, Context &c, std::any &dt) const override
Definition peglib.h:1348
bool negated_
Definition peglib.h:1287
bool ignore_case_
Definition peglib.h:1288
std::vector< std::pair< char32_t, char32_t > > ranges_
Definition peglib.h:1286
CharacterClass(const std::string &s, bool negated, bool ignore_case)
Definition peglib.h:1219
size_t parse_core(const char *s, size_t n, SemanticValues &, Context &c, std::any &) const override
Definition peglib.h:1244
bool in_range(const std::pair< char32_t, char32_t > &range, char32_t cp) const
Definition peglib.h:1276
CharacterClass(const std::vector< std::pair< char32_t, char32_t > > &ranges, bool negated, bool ignore_case)
Definition peglib.h:1238
void accept(Visitor &v) override
Definition peglib.h:3085
void accept(Visitor &v) override
Definition peglib.h:3086
size_t parse_core(const char *s, size_t n, SemanticValues &, Context &c, std::any &) const override
Definition peglib.h:1295
char ch_
Definition peglib.h:1306
Character(char ch)
Definition peglib.h:1293
size_t in_token_boundary_count
Definition peglib.h:778
std::vector< Definition * > rule_stack
Definition peglib.h:775
void trace_leave(const Ope &ope, const char *a_s, size_t n, const SemanticValues &vs, std::any &dt, size_t len)
Definition peglib.h:2661
std::once_flag source_line_index_init_
Definition peglib.h:968
std::shared_ptr< Ope > wordOpe
Definition peglib.h:783
void trace_enter(const Ope &ope, const char *a_s, size_t n, const SemanticValues &vs, std::any &dt)
Definition peglib.h:2655
TracerEnter tracer_enter
Definition peglib.h:798
std::vector< bool > cut_stack
Definition peglib.h:788
const std::vector< std::shared_ptr< Ope > > & top_args() const
Definition peglib.h:907
std::vector< bool > cache_success
Definition peglib.h:793
Context operator=(const Context &)=delete
std::shared_ptr< Ope > whitespaceOpe
Definition peglib.h:780
std::map< std::pair< size_t, size_t >, std::tuple< size_t, std::any > > cache_values
Definition peglib.h:796
const size_t def_count
Definition peglib.h:790
Context(Context &&)=delete
Context(const char *path, const char *s, size_t l, size_t def_count, std::shared_ptr< Ope > whitespaceOpe, std::shared_ptr< Ope > wordOpe, bool enablePackratParsing, TracerEnter tracer_enter, TracerLeave tracer_leave, std::any trace_data, bool verbose_trace, Log log)
Definition peglib.h:805
const bool verbose_trace
Definition peglib.h:801
Log log
Definition peglib.h:803
void push_capture_scope()
Definition peglib.h:912
const char * s
Definition peglib.h:766
size_t next_trace_id
Definition peglib.h:965
void shift_capture_values()
Definition peglib.h:926
SemanticValues & push_semantic_values_scope()
Definition peglib.h:876
void pop_semantic_values_scope()
Definition peglib.h:898
bool is_traceable(const Ope &ope) const
Definition peglib.h:2668
const char * path
Definition peglib.h:765
std::vector< std::map< std::string_view, std::string > > capture_scope_stack
Definition peglib.h:785
std::any trace_data
Definition peglib.h:800
size_t capture_scope_stack_size
Definition peglib.h:786
TracerLeave tracer_leave
Definition peglib.h:799
std::vector< size_t > trace_ids
Definition peglib.h:966
size_t value_stack_size
Definition peglib.h:773
const size_t l
Definition peglib.h:767
void push_args(std::vector< std::shared_ptr< Ope > > &&args)
Definition peglib.h:901
void pop_args()
Definition peglib.h:905
ErrorInfo error_info
Definition peglib.h:769
std::vector< bool > cache_registered
Definition peglib.h:792
bool in_whitespace
Definition peglib.h:781
std::pair< size_t, size_t > line_info(const char *cur) const
Definition peglib.h:946
void pop_capture_scope()
Definition peglib.h:924
void pop()
Definition peglib.h:870
std::vector< size_t > source_line_index
Definition peglib.h:969
bool recovered
Definition peglib.h:770
SemanticValues & push()
Definition peglib.h:865
std::vector< std::shared_ptr< SemanticValues > > value_stack
Definition peglib.h:772
~Context()
Definition peglib.h:821
void packrat(const char *a_s, size_t def_id, size_t &len, std::any &val, T fn)
Definition peglib.h:834
const bool enablePackratParsing
Definition peglib.h:791
Context(const Context &)=delete
void set_error_pos(const char *a_s, const char *literal=nullptr)
Definition peglib.h:2621
bool ignore_trace_state
Definition peglib.h:967
std::vector< std::vector< std::shared_ptr< Ope > > > args_stack
Definition peglib.h:776
void accept(Visitor &v) override
Definition peglib.h:3100
size_t parse_core(const char *, size_t, SemanticValues &, Context &c, std::any &) const override
Definition peglib.h:1544
std::shared_ptr< Ope > wordOpe
Definition peglib.h:2394
bool is_macro
Definition peglib.h:2396
bool ignoreSemanticValue
Definition peglib.h:2392
bool eoi_check
Definition peglib.h:2409
Definition & operator<=(const std::shared_ptr< Ope > &ope)
Definition peglib.h:2230
std::function< void(const Context &c, const char *s, size_t n, size_t matchlen, std::any &value, std::any &dt)> leave
Definition peglib.h:2391
Definition()
Definition peglib.h:2215
Result parse(const char *s, std::any &dt, const char *path=nullptr, Log log=nullptr) const
Definition peglib.h:2254
bool disable_action
Definition peglib.h:2398
TracerEnter tracer_enter
Definition peglib.h:2400
std::vector< std::string > params
Definition peglib.h:2397
Definition & operator~()
Definition peglib.h:2361
Result parse(const char *s, size_t n, const char *path=nullptr, Log log=nullptr) const
Definition peglib.h:2235
bool enablePackratParsing
Definition peglib.h:2395
std::pair< size_t, size_t > line_
Definition peglib.h:2379
friend class ParserGenerator
Definition peglib.h:2413
std::once_flag is_token_init_
Definition peglib.h:2476
TracerStartOrEnd tracer_end
Definition peglib.h:2404
std::once_flag definition_ids_init_
Definition peglib.h:2479
bool no_ast_opt
Definition peglib.h:2407
Definition & operator,(T fn)
Definition peglib.h:2356
friend class Reference
Definition peglib.h:2412
void operator=(Action a)
Definition peglib.h:2354
Result parse_and_get_value(const char *s, size_t n, std::any &dt, T &val, const char *path=nullptr, Log log=nullptr) const
Definition peglib.h:2281
Result parse(const char *s, size_t n, std::any &dt, const char *path=nullptr, Log log=nullptr) const
Definition peglib.h:2248
std::unordered_map< void *, size_t > definition_ids_
Definition peglib.h:2480
Result parse_and_get_value(const char *s, size_t n, T &val, const char *path=nullptr, Log log=nullptr) const
Definition peglib.h:2261
std::once_flag assign_id_to_definition_init_
Definition peglib.h:2478
TracerLeave tracer_leave
Definition peglib.h:2401
size_t id
Definition peglib.h:2385
std::shared_ptr< Ope > whitespaceOpe
Definition peglib.h:2393
bool is_token() const
Definition peglib.h:2370
Result parse_and_get_value(const char *s, T &val, const char *path=nullptr, Log log=nullptr) const
Definition peglib.h:2274
Definition & operator=(Definition &&rhs)
bool is_token_
Definition peglib.h:2477
Definition(const Definition &rhs)
Definition peglib.h:2217
Result parse_core(const char *s, size_t n, SemanticValues &vs, std::any &dt, const char *path, Log log) const
Definition peglib.h:2428
std::function< bool(const SemanticValues &vs, const std::any &dt, std::string &msg)> predicate
Definition peglib.h:2383
Definition & operator=(const Definition &rhs)
void accept(Ope::Visitor &v)
Definition peglib.h:2366
bool verbose_trace
Definition peglib.h:2402
std::string name
Definition peglib.h:2377
std::shared_ptr< Holder > holder_
Definition peglib.h:2475
std::string error_message
Definition peglib.h:2406
std::function< void(const Context &c, const char *s, size_t n, std::any &dt)> enter
Definition peglib.h:2388
Result parse_and_get_value(const char *s, std::any &dt, T &val, const char *path=nullptr, Log log=nullptr) const
Definition peglib.h:2293
TracerStartOrEnd tracer_start
Definition peglib.h:2403
Action action
Definition peglib.h:2386
Result parse(const char *s, const char *path=nullptr, Log log=nullptr) const
Definition peglib.h:2242
void initialize_definition_ids() const
Definition peglib.h:2418
std::shared_ptr< Ope > get_core_operator() const
Definition peglib.h:2368
Definition(const std::shared_ptr< Ope > &ope)
Definition peglib.h:2221
const char * s_
Definition peglib.h:2378
Dictionary(const std::vector< std::string > &v, bool ignore_case)
Definition peglib.h:1185
void accept(Visitor &v) override
Definition peglib.h:3083
size_t parse_core(const char *s, size_t n, SemanticValues &vs, Context &c, std::any &dt) const override
Definition peglib.h:2687
Trie trie_
Definition peglib.h:1193
Holder(Definition *outer)
Definition peglib.h:1424
const std::string & name() const
Definition peglib.h:2869
Definition * outer_
Definition peglib.h:1437
size_t parse_core(const char *s, size_t n, SemanticValues &vs, Context &c, std::any &dt) const override
Definition peglib.h:2775
const std::string & trace_name() const
Definition peglib.h:2871
void accept(Visitor &v) override
Definition peglib.h:3094
std::string trace_name_
Definition peglib.h:1439
friend class Definition
Definition peglib.h:1441
std::any reduce(SemanticValues &vs, std::any &dt) const
Definition peglib.h:2859
std::once_flag trace_name_init_
Definition peglib.h:1438
std::shared_ptr< Ope > ope_
Definition peglib.h:1436
void accept(Visitor &v) override
Definition peglib.h:3091
Ignore(const std::shared_ptr< Ope > &ope)
Definition peglib.h:1375
std::shared_ptr< Ope > ope_
Definition peglib.h:1386
size_t parse_core(const char *s, size_t n, SemanticValues &, Context &c, std::any &dt) const override
Definition peglib.h:1377
void accept(Visitor &v) override
Definition peglib.h:3084
bool ignore_case_
Definition peglib.h:1211
size_t parse_core(const char *s, size_t n, SemanticValues &vs, Context &c, std::any &dt) const override
Definition peglib.h:2739
std::string lit_
Definition peglib.h:1210
LiteralString(std::string &&s, bool ignore_case)
Definition peglib.h:1199
std::once_flag init_is_word_
Definition peglib.h:1212
bool is_word_
Definition peglib.h:1213
LiteralString(const std::string &s, bool ignore_case)
Definition peglib.h:1202
std::shared_ptr< Ope > ope_
Definition peglib.h:1180
NotPredicate(const std::shared_ptr< Ope > &ope)
Definition peglib.h:1163
void accept(Visitor &v) override
Definition peglib.h:3082
size_t parse_core(const char *s, size_t n, SemanticValues &, Context &c, std::any &dt) const override
Definition peglib.h:1165
size_t parse(const char *s, size_t n, SemanticValues &vs, Context &c, std::any &dt) const
Definition peglib.h:2676
virtual size_t parse_core(const char *s, size_t n, SemanticValues &vs, Context &c, std::any &dt) const =0
virtual void accept(Visitor &v)=0
static ParserContext parse(const char *s, size_t n, const Rules &rules, Log log, std::string_view start)
Definition peglib.h:3307
bool apply_precedence_instruction(Definition &rule, const PrecedenceClimbing::BinOpeInfo &info, const char *s, Log log)
Definition peglib.h:3961
void make_grammar()
Definition peglib.h:3364
Grammar g
Definition peglib.h:4249
ParserGenerator()
Definition peglib.h:3334
static bool parse_test(const char *d, const char *s)
Definition peglib.h:3313
bool detect_infiniteLoop(const Data &data, Definition &rule, const Log &log, const char *s) const
Definition peglib.h:4232
ParserContext perform_core(const char *s, size_t n, const Rules &rules, Log log, std::string requested_start)
Definition peglib.h:4002
static ParserGenerator & get_instance()
Definition peglib.h:3329
void setup_actions()
Definition peglib.h:3549
std::shared_ptr< Ope > atom_
Definition peglib.h:1518
std::map< std::string_view, std::pair< size_t, char > > BinOpeInfo
Definition peglib.h:1504
PrecedenceClimbing(const std::shared_ptr< Ope > &atom, const std::shared_ptr< Ope > &binop, const BinOpeInfo &info, const Definition &rule)
Definition peglib.h:1506
size_t parse_core(const char *s, size_t n, SemanticValues &vs, Context &c, std::any &dt) const override
Definition peglib.h:1511
const Definition & rule_
Definition peglib.h:1521
std::shared_ptr< Ope > binop_
Definition peglib.h:1519
Definition & get_reference_for_binop(Context &c) const
Definition peglib.h:2942
BinOpeInfo info_
Definition peglib.h:1520
void accept(Visitor &v) override
Definition peglib.h:3098
size_t parse_expression(const char *s, size_t n, SemanticValues &vs, Context &c, std::any &dt, size_t min_prec) const
Definition peglib.h:2954
PrioritizedChoice(bool for_label, const Args &...args)
Definition peglib.h:1017
size_t size() const
Definition peglib.h:1064
size_t parse_core(const char *s, size_t n, SemanticValues &vs, Context &c, std::any &dt) const override
Definition peglib.h:1024
void accept(Visitor &v) override
Definition peglib.h:3079
bool for_label_
Definition peglib.h:1067
std::vector< std::shared_ptr< Ope > > opes_
Definition peglib.h:1066
PrioritizedChoice(const std::vector< std::shared_ptr< Ope > > &opes)
Definition peglib.h:1020
PrioritizedChoice(std::vector< std::shared_ptr< Ope > > &&opes)
Definition peglib.h:1022
size_t parse_core(const char *s, size_t n, SemanticValues &vs, Context &c, std::any &dt) const override
Definition peglib.h:3029
void accept(Visitor &v) override
Definition peglib.h:3099
Recovery(const std::shared_ptr< Ope > &ope)
Definition peglib.h:1532
std::shared_ptr< Ope > ope_
Definition peglib.h:1539
const std::string name_
Definition peglib.h:1461
Definition * rule_
Definition peglib.h:1467
std::shared_ptr< Ope > get_core_operator() const
Definition peglib.h:2917
const char * s_
Definition peglib.h:1462
void accept(Visitor &v) override
Definition peglib.h:3095
size_t parse_core(const char *s, size_t n, SemanticValues &vs, Context &c, std::any &dt) const override
Definition peglib.h:2877
const bool is_macro_
Definition peglib.h:1464
const std::vector< std::shared_ptr< Ope > > args_
Definition peglib.h:1465
size_t iarg_
Definition peglib.h:1468
const Grammar & grammar_
Definition peglib.h:1460
Reference(const Grammar &grammar, const std::string &name, const char *s, bool is_macro, const std::vector< std::shared_ptr< Ope > > &args)
Definition peglib.h:1448
static std::shared_ptr< Repetition > zom(const std::shared_ptr< Ope > &ope)
Definition peglib.h:1119
static std::shared_ptr< Repetition > opt(const std::shared_ptr< Ope > &ope)
Definition peglib.h:1129
bool is_zom() const
Definition peglib.h:1115
Repetition(const std::shared_ptr< Ope > &ope, size_t min, size_t max)
Definition peglib.h:1072
std::shared_ptr< Ope > ope_
Definition peglib.h:1133
size_t max_
Definition peglib.h:1135
void accept(Visitor &v) override
Definition peglib.h:3080
size_t parse_core(const char *s, size_t n, SemanticValues &vs, Context &c, std::any &dt) const override
Definition peglib.h:1075
size_t min_
Definition peglib.h:1134
static std::shared_ptr< Repetition > oom(const std::shared_ptr< Ope > &ope)
Definition peglib.h:1124
Sequence(std::vector< std::shared_ptr< Ope > > &&opes)
Definition peglib.h:993
size_t parse_core(const char *s, size_t n, SemanticValues &vs, Context &c, std::any &dt) const override
Definition peglib.h:995
std::vector< std::shared_ptr< Ope > > opes_
Definition peglib.h:1011
void accept(Visitor &v) override
Definition peglib.h:3078
Sequence(const Args &...args)
Definition peglib.h:990
Sequence(const std::vector< std::shared_ptr< Ope > > &opes)
Definition peglib.h:992
void accept(Visitor &v) override
Definition peglib.h:3090
size_t parse_core(const char *s, size_t n, SemanticValues &vs, Context &c, std::any &dt) const override
Definition peglib.h:2746
std::shared_ptr< Ope > ope_
Definition peglib.h:1370
TokenBoundary(const std::shared_ptr< Ope > &ope)
Definition peglib.h:1363
std::map< std::string, Info, std::less<> > dic_
Definition peglib.h:446
size_t match(const char *text, size_t text_len, size_t &id) const
Definition peglib.h:401
Trie(const std::vector< std::string > &items, bool ignore_case)
Definition peglib.h:380
std::string to_lower(std::string s) const
Definition peglib.h:431
size_t size() const
Definition peglib.h:428
bool ignore_case_
Definition peglib.h:448
std::function< size_t(const char *s, size_t n, SemanticValues &vs, std::any &dt)> fn_
Definition peglib.h:1403
void accept(Visitor &v) override
Definition peglib.h:3092
User(Parser fn)
Definition peglib.h:1394
size_t parse_core(const char *s, size_t n, SemanticValues &vs, Context &, std::any &dt) const override
Definition peglib.h:1395
WeakHolder(const std::shared_ptr< Ope > &ope)
Definition peglib.h:1408
void accept(Visitor &v) override
Definition peglib.h:3093
size_t parse_core(const char *s, size_t n, SemanticValues &vs, Context &c, std::any &dt) const override
Definition peglib.h:1410
std::weak_ptr< Ope > weak_
Definition peglib.h:1419
std::shared_ptr< Ope > ope_
Definition peglib.h:1485
size_t parse_core(const char *s, size_t n, SemanticValues &vs, Context &c, std::any &dt) const override
Definition peglib.h:1475
void accept(Visitor &v) override
Definition peglib.h:3096
Whitespace(const std::shared_ptr< Ope > &ope)
Definition peglib.h:1473
parser(const char *s, size_t n, std::string_view start={})
Definition peglib.h:4560
Log log_
Definition peglib.h:4775
bool enablePackratParsing_
Definition peglib.h:4774
parser(const char *s, size_t n, const Rules &rules, std::string_view start={})
Definition peglib.h:4555
bool parse_n(const char *s, size_t n, std::any &dt, T &val, const char *path=nullptr) const
Definition peglib.h:4634
std::string start_
Definition peglib.h:4773
const Grammar & get_grammar() const
Definition peglib.h:4693
std::shared_ptr< Grammar > grammar_
Definition peglib.h:4772
bool parse_n(const char *s, size_t n, std::any &dt, const char *path=nullptr) const
Definition peglib.h:4612
void set_logger(Log log)
Definition peglib.h:4749
bool load_grammar(std::string_view sv, std::string_view start={})
Definition peglib.h:4599
void enable_packrat_parsing()
Definition peglib.h:4702
parser & enable_ast()
Definition peglib.h:4736
parser(std::string_view sv, const Rules &rules, std::string_view start={})
Definition peglib.h:4563
void disable_eoi_check()
Definition peglib.h:4695
bool load_grammar(const char *s, size_t n, std::string_view start={})
Definition peglib.h:4590
std::shared_ptr< T > optimize_ast(std::shared_ptr< T > ast, bool opt_mode=true) const
Definition peglib.h:4744
void set_verbose_trace(bool verbose_trace)
Definition peglib.h:4729
const Definition & operator[](const char *s) const
Definition peglib.h:4691
bool parse_n(const char *s, size_t n, T &val, const char *path=nullptr) const
Definition peglib.h:4623
bool parse(std::string_view sv, std::any &dt, const char *path=nullptr) const
Definition peglib.h:4648
parser(std::string_view sv, std::string_view start={})
Definition peglib.h:4566
void set_logger(std::function< void(size_t line, size_t col, const std::string &msg)> log)
Definition peglib.h:4751
bool load_grammar(const char *s, size_t n, const Rules &rules, std::string_view start={})
Definition peglib.h:4581
void enable_trace(TracerEnter tracer_enter, TracerLeave tracer_leave)
Definition peglib.h:4709
bool parse(std::string_view sv, std::any &dt, T &val, const char *path=nullptr) const
Definition peglib.h:4659
bool post_process(const char *s, size_t n, Definition::Result &r) const
Definition peglib.h:4759
std::vector< std::string > get_no_ast_opt_rules() const
Definition peglib.h:4764
bool load_grammar(std::string_view sv, const Rules &rules, std::string_view start={})
Definition peglib.h:4594
bool parse(std::string_view sv, T &val, const char *path=nullptr) const
Definition peglib.h:4654
Definition & operator[](const char *s)
Definition peglib.h:4689
bool parse_n(const char *s, size_t n, const char *path=nullptr) const
Definition peglib.h:4603
bool parse(std::string_view sv, const char *path=nullptr) const
Definition peglib.h:4644
void enable_trace(TracerEnter tracer_enter, TracerLeave tracer_leave, TracerStartOrEnd tracer_start, TracerStartOrEnd tracer_end)
Definition peglib.h:4717
Definition filter_string.h:27
std::string escape_characters(const char *s, size_t n)
Definition peglib.h:213
static const char * WORD_DEFINITION_NAME
Definition peglib.h:2200
const char * u8(const T *s)
Definition peglib.h:205
std::shared_ptr< Ope > ref(const Grammar &grammar, const std::string &name, const char *s, bool is_macro, const std::vector< std::shared_ptr< Ope > > &args)
Definition peglib.h:1658
size_t encode_codepoint(char32_t cp, char *buff)
Definition peglib.h:106
std::shared_ptr< Ope > cut()
Definition peglib.h:1683
std::shared_ptr< Ope > tok(const std::shared_ptr< Ope > &ope)
Definition peglib.h:1643
std::shared_ptr< Ope > csc(const std::shared_ptr< Ope > &ope)
Definition peglib.h:1634
bool decode_codepoint(const char *s8, size_t l, size_t &bytes, char32_t &cp)
Definition peglib.h:143
size_t codepoint_count(const char *s8, size_t l)
Definition peglib.h:98
std::shared_ptr< Ope > apd(const std::shared_ptr< Ope > &ope)
Definition peglib.h:1587
std::pair< int, size_t > parse_octal_number(const char *s, size_t n, size_t i)
Definition peglib.h:270
std::u32string decode(const char *s8, size_t l)
Definition peglib.h:192
std::pair< size_t, size_t > line_info(const char *start, const char *cur)
Definition peglib.h:458
std::shared_ptr< Ope > rec(const std::shared_ptr< Ope > &ope)
Definition peglib.h:1679
std::function< size_t(const char *s, size_t n, SemanticValues &vs, std::any &dt)> Parser
Definition peglib.h:1389
std::shared_ptr< Ope > cls(const std::string &s)
Definition peglib.h:1608
std::shared_ptr< Ope > wsp(const std::shared_ptr< Ope > &ope)
Definition peglib.h:1664
bool fail(size_t len)
Definition peglib.h:671
T token_to_number_(std::string_view sv)
Definition peglib.h:358
std::shared_ptr< Ope > pre(const std::shared_ptr< Ope > &atom, const std::shared_ptr< Ope > &binop, const PrecedenceClimbing::BinOpeInfo &info, const Definition &rule)
Definition peglib.h:1672
bool is_digit(char c, int &v)
Definition peglib.h:251
std::shared_ptr< Ope > dic(const std::vector< std::string > &v, bool ignore_case)
Definition peglib.h:1595
std::shared_ptr< Ope > liti(std::string &&s)
Definition peglib.h:1604
std::shared_ptr< Ope > ign(const std::shared_ptr< Ope > &ope)
Definition peglib.h:1647
std::unordered_map< std::string, Definition > Grammar
Definition peglib.h:1444
std::function< void( const Ope &ope, const char *s, size_t n, const SemanticValues &vs, const Context &c, const std::any &dt, size_t, std::any &trace_data)> TracerLeave
Definition peglib.h:757
size_t codepoint_length(const char *s8, size_t l)
Definition peglib.h:82
size_t parse_literal(const char *s, size_t n, SemanticValues &vs, Context &c, std::any &dt, const std::string &lit, std::once_flag &init_is_word, bool &is_word, bool ignore_case)
Definition peglib.h:2487
static const char * WHITESPACE_DEFINITION_NAME
Definition peglib.h:2199
std::string resolve_escape_sequence(const char *s, size_t n)
Definition peglib.h:281
std::shared_ptr< Ope > lit(std::string &&s)
Definition peglib.h:1600
std::shared_ptr< Ope > chr(char dt)
Definition peglib.h:1628
std::shared_ptr< Ope > cho4label_(Args &&...args)
Definition peglib.h:1565
std::shared_ptr< Ope > dot()
Definition peglib.h:1632
std::function< void( const Ope &name, const char *s, size_t n, const SemanticValues &vs, const Context &c, const std::any &dt, std::any &trace_data)> TracerEnter
Definition peglib.h:753
std::unordered_map< std::string, std::shared_ptr< Ope > > Rules
Definition peglib.h:3297
void enable_profiling(parser &parser, std::ostream &os)
Definition peglib.h:4842
bool is_hex(char c, int &v)
Definition peglib.h:237
std::string ast_to_s(const std::shared_ptr< T > &ptr, std::function< std::string(const T &ast, int level)> fn=nullptr)
Definition peglib.h:4345
std::function< void(size_t line, size_t col, const std::string &msg, const std::string &rule)> Log
Definition peglib.h:676
std::shared_ptr< Ope > opt(const std::shared_ptr< Ope > &ope)
Definition peglib.h:1578
std::function< void(std::any &trace_data)> TracerStartOrEnd
Definition peglib.h:761
std::shared_ptr< Ope > oom(const std::shared_ptr< Ope > &ope)
Definition peglib.h:1574
std::shared_ptr< Ope > cho(Args &&...args)
Definition peglib.h:1560
std::shared_ptr< Ope > rep(const std::shared_ptr< Ope > &ope, size_t min, size_t max)
Definition peglib.h:1582
constexpr unsigned int str2tag_core(const char *s, size_t l, unsigned int h)
Definition peglib.h:479
std::shared_ptr< Ope > npd(const std::shared_ptr< Ope > &ope)
Definition peglib.h:1591
void ast_to_s_core(const std::shared_ptr< T > &ptr, std::string &s, int level, std::function< std::string(const T &ast, int level)> fn)
Definition peglib.h:4319
constexpr unsigned int str2tag(std::string_view sv)
Definition peglib.h:486
std::shared_ptr< Ope > seq(Args &&...args)
Definition peglib.h:1556
bool success(size_t len)
Definition peglib.h:669
std::shared_ptr< Ope > ncls(const std::string &s)
Definition peglib.h:1618
std::pair< int, size_t > parse_hex_number(const char *s, size_t n, size_t i)
Definition peglib.h:259
std::shared_ptr< Ope > cap(const std::shared_ptr< Ope > &ope, Capture::MatchAction ma)
Definition peglib.h:1638
void add_ast_action(Definition &rule)
Definition peglib.h:4392
AstBase< EmptyType > Ast
Definition filter_string.h:30
static const char * RECOVER_DEFINITION_NAME
Definition peglib.h:2201
std::any call(F fn, Args &&...args)
Definition peglib.h:613
std::shared_ptr< Ope > zom(const std::shared_ptr< Ope > &ope)
Definition peglib.h:1570
std::shared_ptr< Ope > bkr(std::string &&name)
Definition peglib.h:1668
std::shared_ptr< Ope > usr(std::function< size_t(const char *s, size_t n, SemanticValues &vs, std::any &dt)> fn)
Definition peglib.h:1652
void enable_tracing(parser &parser, std::ostream &os)
Definition peglib.h:4782
#define CPPPEGLIB_HEURISTIC_ERROR_TOKEN_MAX_CHAR_COUNT
Definition peglib.h:15
Definition clipboard_testing.h:11
void visit(Recovery &ope) override
Definition peglib.h:1777
void visit(Ignore &ope) override
Definition peglib.h:1771
void visit(Capture &ope) override
Definition peglib.h:1769
void visit(Repetition &ope) override
Definition peglib.h:1765
void visit(WeakHolder &ope) override
Definition peglib.h:1772
void visit(TokenBoundary &ope) override
Definition peglib.h:1770
void visit(NotPredicate &ope) override
Definition peglib.h:1767
void visit(PrioritizedChoice &ope) override
Definition peglib.h:1760
void visit(AndPredicate &ope) override
Definition peglib.h:1766
std::unordered_map< void *, size_t > ids
Definition peglib.h:1779
void visit(Sequence &ope) override
Definition peglib.h:1755
void visit(CaptureScope &ope) override
Definition peglib.h:1768
void visit(Whitespace &ope) override
Definition peglib.h:1775
const size_t column
Definition peglib.h:4289
AstBase(const AstBase &ast, const char *original_name, size_t position=0, size_t length=0, size_t original_choice_count=0, size_t original_choice=0)
Definition peglib.h:4276
std::weak_ptr< AstBase< EmptyType > > parent
Definition peglib.h:4306
AstBase(const char *path, size_t line, size_t column, const char *name, const std::string_view &token, size_t position=0, size_t length=0, size_t choice_count=0, size_t choice=0)
Definition peglib.h:4267
const std::string name
Definition peglib.h:4291
T token_to_number() const
Definition peglib.h:4313
const bool is_token
Definition peglib.h:4302
const size_t line
Definition peglib.h:4288
size_t length
Definition peglib.h:4293
const unsigned int original_tag
Definition peglib.h:4300
const size_t choice
Definition peglib.h:4295
size_t position
Definition peglib.h:4292
const size_t original_choice_count
Definition peglib.h:4297
const std::string_view token
Definition peglib.h:4303
std::vector< std::shared_ptr< AstBase< EmptyType > > > nodes
Definition peglib.h:4305
const size_t choice_count
Definition peglib.h:4294
const size_t original_choice
Definition peglib.h:4298
const std::string path
Definition peglib.h:4287
std::string token_to_string() const
Definition peglib.h:4308
AstBase(const char *path, size_t line, size_t column, const char *name, const std::vector< std::shared_ptr< AstBase > > &nodes, size_t position=0, size_t length=0, size_t choice_count=0, size_t choice=0)
Definition peglib.h:4257
const unsigned int tag
Definition peglib.h:4299
const std::string original_name
Definition peglib.h:4296
const bool mode_
Definition peglib.h:4385
const std::vector< std::string > rules_
Definition peglib.h:4386
std::shared_ptr< T > optimize(std::shared_ptr< T > original, std::shared_ptr< T > parent=nullptr)
Definition peglib.h:4357
AstOptimizer(bool mode, const std::vector< std::string > &rules={})
Definition peglib.h:4353
bool ret
Definition peglib.h:2209
size_t len
Definition peglib.h:2211
ErrorInfo error_info
Definition peglib.h:2212
bool recovered
Definition peglib.h:2210
void visit(Repetition &ope) override
Definition peglib.h:2002
void visit(PrioritizedChoice &ope) override
Definition peglib.h:1996
void visit(Capture &ope) override
Definition peglib.h:2018
void visit(Whitespace &ope) override
Definition peglib.h:2024
void visit(TokenBoundary &ope) override
Definition peglib.h:2019
DetectInfiniteLoop(std::vector< std::pair< const char *, std::string > > &refs, std::unordered_map< std::string, bool > &has_error_cache)
Definition peglib.h:1986
void visit(Holder &ope) override
Definition peglib.h:2022
void visit(Recovery &ope) override
Definition peglib.h:2026
void visit(NotPredicate &ope) override
Definition peglib.h:2016
std::unordered_map< std::string, bool > & has_error_cache_
Definition peglib.h:2034
void visit(WeakHolder &ope) override
Definition peglib.h:2021
void visit(Sequence &ope) override
Definition peglib.h:1990
bool has_error
Definition peglib.h:2028
void visit(Ignore &ope) override
Definition peglib.h:2020
void visit(AndPredicate &ope) override
Definition peglib.h:2015
std::vector< std::pair< const char *, std::string > > & refs_
Definition peglib.h:2033
void visit(CaptureScope &ope) override
Definition peglib.h:2017
const char * error_s
Definition peglib.h:2029
void visit(PrecedenceClimbing &ope) override
Definition peglib.h:2025
DetectInfiniteLoop(const char *s, const std::string &name, std::vector< std::pair< const char *, std::string > > &refs, std::unordered_map< std::string, bool > &has_error_cache)
Definition peglib.h:1979
std::string error_name
Definition peglib.h:2030
void visit(TokenBoundary &ope) override
Definition peglib.h:1906
bool done_
Definition peglib.h:1923
void visit(PrecedenceClimbing &ope) override
Definition peglib.h:1914
void visit(AnyCharacter &) override
Definition peglib.h:1903
void visit(Sequence &ope) override
Definition peglib.h:1867
void visit(AndPredicate &ope) override
Definition peglib.h:1891
void visit(PrioritizedChoice &ope) override
Definition peglib.h:1878
void visit(BackReference &) override
Definition peglib.h:1913
const char * error_s
Definition peglib.h:1918
void visit(Ignore &ope) override
Definition peglib.h:1907
void visit(Repetition &ope) override
Definition peglib.h:1887
void visit(WeakHolder &ope) override
Definition peglib.h:1909
std::unordered_set< std::string > refs_
Definition peglib.h:1922
void visit(Cut &) override
Definition peglib.h:1916
void visit(CaptureScope &ope) override
Definition peglib.h:1904
void visit(Character &) override
Definition peglib.h:1902
std::string name_
Definition peglib.h:1921
void visit(Recovery &ope) override
Definition peglib.h:1915
void visit(LiteralString &ope) override
Definition peglib.h:1900
void visit(CharacterClass &) override
Definition peglib.h:1901
DetectLeftRecursion(const std::string &name)
Definition peglib.h:1865
void visit(Whitespace &ope) override
Definition peglib.h:1912
void visit(Dictionary &) override
Definition peglib.h:1899
void visit(NotPredicate &ope) override
Definition peglib.h:1895
void visit(Capture &ope) override
Definition peglib.h:1905
void visit(Holder &ope) override
Definition peglib.h:1910
void visit(User &) override
Definition peglib.h:1908
std::vector< std::pair< const char *, const Definition * > > expected_tokens
Definition peglib.h:686
std::string replace_all(std::string str, const std::string &from, const std::string &to) const
Definition peglib.h:737
void clear()
Definition peglib.h:693
const char * message_pos
Definition peglib.h:687
int cast_char(char c) const
Definition peglib.h:710
std::string heuristic_error_token(const char *s, size_t n, const char *pos) const
Definition peglib.h:712
const char * last_output_pos
Definition peglib.h:690
void output_log(const Log &log, const char *s, size_t n)
Definition peglib.h:2554
bool keep_previous_token
Definition peglib.h:691
void add(const char *error_literal, const Definition *error_rule)
Definition peglib.h:700
std::string message
Definition peglib.h:688
std::string label
Definition peglib.h:689
const char * error_pos
Definition peglib.h:685
void visit(LiteralString &ope) override
Definition peglib.h:1846
static const char * token(Ope &ope)
Definition peglib.h:1852
void visit(TokenBoundary &ope) override
Definition peglib.h:1847
const char * token_
Definition peglib.h:1859
void visit(Ignore &ope) override
Definition peglib.h:1848
void visit(Recovery &ope) override
Definition peglib.h:1850
const std::vector< std::string > & params_
Definition peglib.h:2193
void visit(Repetition &ope) override
Definition peglib.h:2135
void visit(WeakHolder &ope) override
Definition peglib.h:2172
void visit(Character &ope) override
Definition peglib.h:2154
void visit(CharacterClass &ope) override
Definition peglib.h:2151
void visit(PrioritizedChoice &ope) override
Definition peglib.h:2127
void visit(Ignore &ope) override
Definition peglib.h:2168
void visit(Cut &ope) override
Definition peglib.h:2187
void visit(AnyCharacter &ope) override
Definition peglib.h:2155
void visit(LiteralString &ope) override
Definition peglib.h:2148
void visit(Recovery &ope) override
Definition peglib.h:2183
void visit(Holder &ope) override
Definition peglib.h:2173
const std::vector< std::shared_ptr< Ope > > & args_
Definition peglib.h:2192
void visit(Dictionary &ope) override
Definition peglib.h:2147
void visit(Whitespace &ope) override
Definition peglib.h:2175
void visit(TokenBoundary &ope) override
Definition peglib.h:2164
std::shared_ptr< Ope > found_ope
Definition peglib.h:2189
void visit(NotPredicate &ope) override
Definition peglib.h:2143
FindReference(const std::vector< std::shared_ptr< Ope > > &args, const std::vector< std::string > ¶ms)
Definition peglib.h:2115
void visit(Sequence &ope) override
Definition peglib.h:2119
void visit(AndPredicate &ope) override
Definition peglib.h:2139
void visit(Capture &ope) override
Definition peglib.h:2160
void visit(CaptureScope &ope) override
Definition peglib.h:2156
void visit(PrecedenceClimbing &ope) override
Definition peglib.h:2179
void visit(Capture &ope) override
Definition peglib.h:1953
std::string error_name
Definition peglib.h:1965
void visit(Sequence &ope) override
Definition peglib.h:3157
void visit(WeakHolder &ope) override
Definition peglib.h:1956
bool is_empty
Definition peglib.h:1963
void visit(Repetition &ope) override
Definition peglib.h:1940
const char * error_s
Definition peglib.h:1964
void visit(NotPredicate &) override
Definition peglib.h:1948
std::vector< std::pair< const char *, std::string > > & refs_
Definition peglib.h:1972
void visit(LiteralString &ope) override
Definition peglib.h:1949
void visit(AndPredicate &) override
Definition peglib.h:1947
void visit(CaptureScope &ope) override
Definition peglib.h:1952
void visit(Whitespace &ope) override
Definition peglib.h:1959
std::unordered_map< std::string, bool > & has_error_cache_
Definition peglib.h:1973
void visit(PrecedenceClimbing &ope) override
Definition peglib.h:1960
void visit(Holder &ope) override
Definition peglib.h:1957
void set_error()
Definition peglib.h:1968
void visit(Recovery &ope) override
Definition peglib.h:1961
void visit(TokenBoundary &ope) override
Definition peglib.h:1954
void visit(PrioritizedChoice &ope) override
Definition peglib.h:1934
void visit(Ignore &ope) override
Definition peglib.h:1955
HasEmptyElement(std::vector< std::pair< const char *, std::string > > &refs, std::unordered_map< std::string, bool > &has_error_cache)
Definition peglib.h:1929
void visit(Dictionary &) override
Definition peglib.h:1792
void visit(LiteralString &) override
Definition peglib.h:1793
bool result_
Definition peglib.h:1802
void visit(PrioritizedChoice &ope) override
Definition peglib.h:1785
static bool check(Ope &ope)
Definition peglib.h:1795
void visit(Ignore &ope) override
Definition peglib.h:2099
const std::vector< std::string > & params_
Definition peglib.h:2109
void visit(Capture &ope) override
Definition peglib.h:2097
void visit(TokenBoundary &ope) override
Definition peglib.h:2098
void visit(PrecedenceClimbing &ope) override
Definition peglib.h:2104
void visit(AndPredicate &ope) override
Definition peglib.h:2094
Grammar & grammar_
Definition peglib.h:2108
void visit(Recovery &ope) override
Definition peglib.h:2105
void visit(Whitespace &ope) override
Definition peglib.h:2103
void visit(Holder &ope) override
Definition peglib.h:2101
void visit(WeakHolder &ope) override
Definition peglib.h:2100
LinkReferences(Grammar &grammar, const std::vector< std::string > ¶ms)
Definition peglib.h:2080
void visit(NotPredicate &ope) override
Definition peglib.h:2095
void visit(Sequence &ope) override
Definition peglib.h:2083
void visit(CaptureScope &ope) override
Definition peglib.h:2096
void visit(PrioritizedChoice &ope) override
Definition peglib.h:2088
void visit(Repetition &ope) override
Definition peglib.h:2093
virtual void visit(WeakHolder &)
Definition peglib.h:1705
virtual void visit(TokenBoundary &)
Definition peglib.h:1702
virtual void visit(Repetition &)
Definition peglib.h:1692
virtual void visit(Dictionary &)
Definition peglib.h:1695
virtual void visit(Character &)
Definition peglib.h:1698
virtual ~Visitor()
Definition peglib.h:1689
virtual void visit(AndPredicate &)
Definition peglib.h:1693
virtual void visit(LiteralString &)
Definition peglib.h:1696
virtual void visit(Reference &)
Definition peglib.h:1707
virtual void visit(CharacterClass &)
Definition peglib.h:1697
virtual void visit(PrioritizedChoice &)
Definition peglib.h:1691
virtual void visit(Ignore &)
Definition peglib.h:1703
virtual void visit(PrecedenceClimbing &)
Definition peglib.h:1710
virtual void visit(CaptureScope &)
Definition peglib.h:1700
virtual void visit(Sequence &)
Definition peglib.h:1690
virtual void visit(Holder &)
Definition peglib.h:1706
virtual void visit(Capture &)
Definition peglib.h:1701
virtual void visit(NotPredicate &)
Definition peglib.h:1694
virtual void visit(BackReference &)
Definition peglib.h:1709
virtual void visit(Cut &)
Definition peglib.h:1712
virtual void visit(AnyCharacter &)
Definition peglib.h:1699
virtual void visit(Whitespace &)
Definition peglib.h:1708
virtual void visit(Recovery &)
Definition peglib.h:1711
virtual void visit(User &)
Definition peglib.h:1704
Data()
Definition peglib.h:3361
std::vector< std::pair< std::string, const char * > > duplicates_of_definition
Definition peglib.h:3350
bool enablePackratParsing
Definition peglib.h:3359
std::map< std::string, std::vector< Instruction > > instructions
Definition peglib.h:3353
std::string start
Definition peglib.h:3347
std::vector< std::pair< std::string, const char * > > duplicates_of_instruction
Definition peglib.h:3352
const char * start_pos
Definition peglib.h:3348
std::set< std::string_view > captures_in_current_definition
Definition peglib.h:3358
std::vector< std::pair< std::string, const char * > > undefined_back_references
Definition peglib.h:3355
std::shared_ptr< Grammar > grammar
Definition peglib.h:3346
std::vector< std::set< std::string_view > > captures_stack
Definition peglib.h:3356
std::any data
Definition peglib.h:3341
std::string type
Definition peglib.h:3340
std::string_view sv
Definition peglib.h:3342
std::shared_ptr< Grammar > grammar
Definition peglib.h:3302
bool enablePackratParsing
Definition peglib.h:3304
std::string start
Definition peglib.h:3303
void visit(Sequence &ope) override
Definition peglib.h:2044
void visit(Whitespace &ope) override
Definition peglib.h:2064
std::unordered_set< std::string > referenced
Definition peglib.h:2070
std::unordered_map< std::string, const char * > error_s
Definition peglib.h:2068
void visit(Holder &ope) override
Definition peglib.h:2062
const std::vector< std::string > & params_
Definition peglib.h:2074
void visit(PrecedenceClimbing &ope) override
Definition peglib.h:2065
void visit(Repetition &ope) override
Definition peglib.h:2054
void visit(WeakHolder &ope) override
Definition peglib.h:2061
void visit(Recovery &ope) override
Definition peglib.h:2066
void visit(NotPredicate &ope) override
Definition peglib.h:2056
std::unordered_map< std::string, std::string > error_message
Definition peglib.h:2069
void visit(TokenBoundary &ope) override
Definition peglib.h:2059
ReferenceChecker(const Grammar &grammar, const std::vector< std::string > ¶ms)
Definition peglib.h:2040
void visit(Capture &ope) override
Definition peglib.h:2058
void visit(PrioritizedChoice &ope) override
Definition peglib.h:2049
void visit(Ignore &ope) override
Definition peglib.h:2060
const Grammar & grammar_
Definition peglib.h:2073
void visit(AndPredicate &ope) override
Definition peglib.h:2055
void visit(CaptureScope &ope) override
Definition peglib.h:2057
std::pair< size_t, size_t > line_info() const
Definition peglib.h:2549
std::string token_to_string(size_t id=0) const
Definition peglib.h:538
void append(SemanticValues &chvs)
Definition peglib.h:558
std::vector< std::string_view > tokens
Definition peglib.h:529
size_t choice_
Definition peglib.h:606
Context * c_
Definition peglib.h:603
std::string name_
Definition peglib.h:607
friend class Holder
Definition peglib.h:600
friend class Sequence
Definition peglib.h:597
std::string_view token(size_t id=0) const
Definition peglib.h:531
std::string_view sv() const
Definition peglib.h:512
std::string_view sv_
Definition peglib.h:604
size_t choice() const
Definition peglib.h:526
T token_to_number() const
Definition peglib.h:542
const char * ss
Definition peglib.h:509
size_t choice_count_
Definition peglib.h:605
friend class Dictionary
Definition peglib.h:596
size_t choice_count() const
Definition peglib.h:523
std::vector< T > transform(size_t beg=0, size_t end=static_cast< size_t >(-1)) const
Definition peglib.h:548
const char * path
Definition peglib.h:508
std::vector< unsigned int > tags
Definition peglib.h:517
const std::string & name() const
Definition peglib.h:515
friend class Repetition
Definition peglib.h:599
friend class PrecedenceClimbing
Definition peglib.h:601
SemanticValues(Context *c)
Definition peglib.h:505
friend class Context
Definition peglib.h:595
friend class PrioritizedChoice
Definition peglib.h:598
void visit(Ignore &ope) override
Definition peglib.h:1822
void visit(Holder &ope) override
Definition peglib.h:1824
void visit(PrecedenceClimbing &ope) override
Definition peglib.h:1827
bool has_rule_
Definition peglib.h:1840
void visit(Sequence &ope) override
Definition peglib.h:1808
void visit(PrioritizedChoice &ope) override
Definition peglib.h:1813
void visit(Whitespace &ope) override
Definition peglib.h:1826
void visit(Capture &ope) override
Definition peglib.h:1820
void visit(Recovery &ope) override
Definition peglib.h:1828
bool has_token_boundary_
Definition peglib.h:1839
void visit(CaptureScope &ope) override
Definition peglib.h:1819
void visit(TokenBoundary &) override
Definition peglib.h:1821
void visit(WeakHolder &) override
Definition peglib.h:1823
void visit(Repetition &ope) override
Definition peglib.h:1818
static bool is_token(Ope &ope)
Definition peglib.h:1830
void visit(Recovery &) override
Definition peglib.h:1739
void visit(Cut &) override
Definition peglib.h:1740
void visit(Holder &ope) override
Definition peglib.h:1734
void visit(User &) override
Definition peglib.h:1732
void visit(NotPredicate &) override
Definition peglib.h:1722
void visit(TokenBoundary &) override
Definition peglib.h:1730
void visit(LiteralString &) override
Definition peglib.h:1724
void visit(AnyCharacter &) override
Definition peglib.h:1727
void visit(Whitespace &) override
Definition peglib.h:1736
void visit(WeakHolder &) override
Definition peglib.h:1733
void visit(Repetition &) override
Definition peglib.h:1720
void visit(Character &) override
Definition peglib.h:1726
void visit(CharacterClass &) override
Definition peglib.h:1725
void visit(Reference &) override
Definition peglib.h:1735
static std::string get(Ope &ope)
Definition peglib.h:1742
const char * name_
Definition peglib.h:1749
void visit(Capture &) override
Definition peglib.h:1729
void visit(CaptureScope &) override
Definition peglib.h:1728
void visit(PrecedenceClimbing &) override
Definition peglib.h:1738
void visit(Sequence &) override
Definition peglib.h:1718
void visit(PrioritizedChoice &) override
Definition peglib.h:1719
void visit(Ignore &) override
Definition peglib.h:1731
void visit(AndPredicate &) override
Definition peglib.h:1721
void visit(BackReference &) override
Definition peglib.h:1737
void visit(Dictionary &) override
Definition peglib.h:1723
bool match
Definition peglib.h:440
size_t id
Definition peglib.h:441
bool done
Definition peglib.h:439
bool execute_on_destruction
Definition peglib.h:75
scope_exit(scope_exit &&rhs)
Definition peglib.h:57
EF exit_function
Definition peglib.h:74
~scope_exit()
Definition peglib.h:63
scope_exit(EF &&f)
Definition peglib.h:54
scope_exit(const scope_exit &)=delete
void operator=(const scope_exit &)=delete
scope_exit & operator=(scope_exit &&)=delete
void release()
Definition peglib.h:67