@@ -459,11 +459,15 @@ inline constexpr unsigned int operator"" _(const char *s, size_t l) {
459
459
/*
460
460
* Semantic values
461
461
*/
462
+ class Context ;
463
+
462
464
struct SemanticValues : protected std ::vector<std::any> {
465
+ SemanticValues () = default ;
466
+ SemanticValues (Context *c): c_(c) {}
467
+
463
468
// Input text
464
469
const char *path = nullptr ;
465
470
const char *ss = nullptr ;
466
- std::function<const std::vector<size_t > &()> source_line_index;
467
471
468
472
// Matched string
469
473
std::string_view sv () const { return sv_; }
@@ -553,6 +557,9 @@ struct SemanticValues : protected std::vector<std::any> {
553
557
friend class Holder ;
554
558
friend class PrecedenceClimbing ;
555
559
560
+ const std::vector<size_t > &source_line_index () const ;
561
+
562
+ Context *c_ = nullptr ;
556
563
std::string_view sv_;
557
564
size_t choice_count_ = 0 ;
558
565
size_t choice_ = 0 ;
@@ -773,7 +780,6 @@ struct ErrorInfo {
773
780
/*
774
781
* Context
775
782
*/
776
- class Context ;
777
783
class Ope ;
778
784
class Definition ;
779
785
@@ -882,7 +888,7 @@ class Context {
882
888
SemanticValues &push () {
883
889
assert (value_stack_size <= value_stack.size ());
884
890
if (value_stack_size == value_stack.size ()) {
885
- value_stack.emplace_back (std::make_shared<SemanticValues>());
891
+ value_stack.emplace_back (std::make_shared<SemanticValues>(this ));
886
892
} else {
887
893
auto &vs = *value_stack[value_stack_size];
888
894
if (!vs.empty ()) {
@@ -898,15 +904,6 @@ class Context {
898
904
auto &vs = *value_stack[value_stack_size++];
899
905
vs.path = path;
900
906
vs.ss = s;
901
- vs.source_line_index = [&]() -> const std::vector<size_t > & {
902
- if (source_line_index.empty ()) {
903
- for (size_t pos = 0 ; pos < l; pos++) {
904
- if (s[pos] == ' \n ' ) { source_line_index.push_back (pos); }
905
- }
906
- source_line_index.push_back (l);
907
- }
908
- return source_line_index;
909
- };
910
907
911
908
return vs;
912
909
}
@@ -2469,6 +2466,19 @@ inline size_t parse_literal(const char *s, size_t n, SemanticValues &vs,
2469
2466
return i;
2470
2467
}
2471
2468
2469
+ inline const std::vector<size_t > &SemanticValues::source_line_index () const {
2470
+ if (!c_) {
2471
+ std::vector<size_t >();
2472
+ }
2473
+ if (c_->source_line_index .empty ()) {
2474
+ for (size_t pos = 0 ; pos < c_->l ; pos++) {
2475
+ if (c_->s [pos] == ' \n ' ) { c_->source_line_index .push_back (pos); }
2476
+ }
2477
+ c_->source_line_index .push_back (c_->l );
2478
+ }
2479
+ return c_->source_line_index ;
2480
+ }
2481
+
2472
2482
inline void Context::set_error_pos (const char *a_s, const char *literal) {
2473
2483
if (log) {
2474
2484
if (error_info.error_pos <= a_s) {
0 commit comments