@@ -432,6 +432,23 @@ inline std::pair<size_t, size_t> line_info(const char* start, const char* cur) {
432
432
return std::make_pair (no, col);
433
433
}
434
434
435
+ /*
436
+ * String tag
437
+ */
438
+ #ifndef PEGLIB_NO_CONSTEXPR_SUPPORT
439
+ inline constexpr unsigned int str2tag (const char * str, int h = 0 ) {
440
+ return !str[h] ? 5381 : (str2tag (str, h + 1 ) * 33 ) ^ static_cast <unsigned char >(str[h]);
441
+ }
442
+
443
+ namespace udl {
444
+
445
+ inline constexpr unsigned int operator " " _(const char * s, size_t ) {
446
+ return str2tag (s);
447
+ }
448
+
449
+ }
450
+ #endif
451
+
435
452
/*
436
453
* Semantic values
437
454
*/
@@ -452,6 +469,10 @@ struct SemanticValues : protected std::vector<any>
452
469
// Definition name
453
470
const std::string& name () const { return name_; }
454
471
472
+ #ifndef PEGLIB_NO_CONSTEXPR_SUPPORT
473
+ std::vector<unsigned int > tags;
474
+ #endif
475
+
455
476
// Line number and column at which the matched string is
456
477
std::pair<size_t , size_t > line_info () const {
457
478
return peg::line_info (ss, s_);
@@ -860,6 +881,7 @@ class Context
860
881
auto & sv = *value_stack[value_stack_size++];
861
882
if (!sv.empty ()) {
862
883
sv.clear ();
884
+ sv.tags .clear ();
863
885
}
864
886
sv.reset ();
865
887
sv.path = path;
@@ -961,6 +983,7 @@ class Sequence : public Ope
961
983
i += len;
962
984
}
963
985
sv.insert (sv.end (), chldsv.begin (), chldsv.end ());
986
+ sv.tags .insert (sv.tags .end (), chldsv.tags .begin (), chldsv.tags .end ());
964
987
sv.s_ = chldsv.c_str ();
965
988
sv.n_ = chldsv.length ();
966
989
sv.tokens .insert (sv.tokens .end (), chldsv.tokens .begin (), chldsv.tokens .end ());
@@ -1010,6 +1033,7 @@ class PrioritizedChoice : public Ope
1010
1033
auto len = rule.parse (s, n, chldsv, c, dt);
1011
1034
if (success (len)) {
1012
1035
sv.insert (sv.end (), chldsv.begin (), chldsv.end ());
1036
+ sv.tags .insert (sv.tags .end (), chldsv.tags .begin (), chldsv.tags .end ());
1013
1037
sv.s_ = chldsv.c_str ();
1014
1038
sv.n_ = chldsv.length ();
1015
1039
sv.choice_count_ = opes_.size ();
@@ -1056,6 +1080,7 @@ class ZeroOrMore : public Ope
1056
1080
} else {
1057
1081
if (sv.size () != save_sv_size) {
1058
1082
sv.erase (sv.begin () + static_cast <std::ptrdiff_t >(save_sv_size));
1083
+ sv.tags .erase (sv.tags .begin () + static_cast <std::ptrdiff_t >(save_sv_size));
1059
1084
}
1060
1085
if (sv.tokens .size () != save_tok_size) {
1061
1086
sv.tokens .erase (sv.tokens .begin () + static_cast <std::ptrdiff_t >(save_tok_size));
@@ -1114,6 +1139,7 @@ class OneOrMore : public Ope
1114
1139
} else {
1115
1140
if (sv.size () != save_sv_size) {
1116
1141
sv.erase (sv.begin () + static_cast <std::ptrdiff_t >(save_sv_size));
1142
+ sv.tags .erase (sv.tags .begin () + static_cast <std::ptrdiff_t >(save_sv_size));
1117
1143
}
1118
1144
if (sv.tokens .size () != save_tok_size) {
1119
1145
sv.tokens .erase (sv.tokens .begin () + static_cast <std::ptrdiff_t >(save_tok_size));
@@ -1155,6 +1181,7 @@ class Option : public Ope
1155
1181
} else {
1156
1182
if (sv.size () != save_sv_size) {
1157
1183
sv.erase (sv.begin () + static_cast <std::ptrdiff_t >(save_sv_size));
1184
+ sv.tags .erase (sv.tags .begin () + static_cast <std::ptrdiff_t >(save_sv_size));
1158
1185
}
1159
1186
if (sv.tokens .size () != save_tok_size) {
1160
1187
sv.tokens .erase (sv.tokens .begin () + static_cast <std::ptrdiff_t >(save_tok_size));
@@ -2278,6 +2305,7 @@ inline size_t Holder::parse(const char* s, size_t n, SemanticValues& sv, Context
2278
2305
if (success (len)) {
2279
2306
if (!outer_->ignoreSemanticValue ) {
2280
2307
sv.emplace_back (val);
2308
+ sv.tags .emplace_back (str2tag (outer_->name .c_str ()));
2281
2309
}
2282
2310
} else {
2283
2311
if (outer_->error_message ) {
@@ -2957,20 +2985,6 @@ class ParserGenerator
2957
2985
* AST
2958
2986
*---------------------------------------------------------------------------*/
2959
2987
2960
- const int AstDefaultTag = -1 ;
2961
-
2962
- #ifndef PEGLIB_NO_CONSTEXPR_SUPPORT
2963
- inline constexpr unsigned int str2tag (const char * str, int h = 0 ) {
2964
- return !str[h] ? 5381 : (str2tag (str, h + 1 ) * 33 ) ^ static_cast <unsigned char >(str[h]);
2965
- }
2966
-
2967
- namespace udl {
2968
- inline constexpr unsigned int operator " " _(const char * s, size_t ) {
2969
- return str2tag (s);
2970
- }
2971
- }
2972
- #endif
2973
-
2974
2988
template <typename Annotation>
2975
2989
struct AstBase : public Annotation
2976
2990
{
0 commit comments