Skip to content

Commit b330799

Browse files
committed
Added name in SemanticValues
1 parent 2dcdafe commit b330799

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

peglib.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,9 @@ struct SemanticValues : protected std::vector<any>
448448
return std::string(s_, n_);
449449
}
450450

451+
// Definition name
452+
const std::string& name() const { return name_; }
453+
451454
// Line number and column at which the matched string is
452455
std::pair<size_t, size_t> line_info() const {
453456
return peg::line_info(ss, s_);
@@ -512,6 +515,7 @@ struct SemanticValues : protected std::vector<any>
512515
size_t n_;
513516
size_t choice_count_;
514517
size_t choice_;
518+
std::string name_;
515519

516520
template <typename F>
517521
auto transform(F f) const -> vector<typename std::remove_const<decltype(f(any()))>::type> {
@@ -2212,6 +2216,7 @@ inline size_t Holder::parse(const char* s, size_t n, SemanticValues& sv, Context
22122216
if (success(len)) {
22132217
chldsv.s_ = s;
22142218
chldsv.n_ = len;
2219+
chldsv.name_ = outer_->name;
22152220

22162221
if (!IsPrioritizedChoice::is_prioritized_choice(*ope_)) {
22172222
chldsv.choice_count_ = 0;
@@ -2915,12 +2920,15 @@ template <typename Annotation>
29152920
struct AstBase : public Annotation
29162921
{
29172922
AstBase(const char* a_path, size_t a_line, size_t a_column,
2918-
const char* a_name, size_t a_choice_count, size_t a_choice,
2923+
const char* a_name, size_t a_position, size_t a_length,
2924+
size_t a_choice_count, size_t a_choice,
29192925
const std::vector<std::shared_ptr<AstBase>>& a_nodes)
29202926
: path(a_path ? a_path : "")
29212927
, line(a_line)
29222928
, column(a_column)
29232929
, name(a_name)
2930+
, position(a_position)
2931+
, length(a_length)
29242932
, choice_count(a_choice_count)
29252933
, choice(a_choice)
29262934
, original_name(a_name)
@@ -2935,12 +2943,15 @@ struct AstBase : public Annotation
29352943
{}
29362944

29372945
AstBase(const char* a_path, size_t a_line, size_t a_column,
2938-
const char* a_name, size_t a_choice_count, size_t a_choice,
2946+
const char* a_name, size_t a_position, size_t a_length,
2947+
size_t a_choice_count, size_t a_choice,
29392948
const std::string& a_token)
29402949
: path(a_path ? a_path : "")
29412950
, line(a_line)
29422951
, column(a_column)
29432952
, name(a_name)
2953+
, position(a_position)
2954+
, length(a_length)
29442955
, choice_count(a_choice_count)
29452956
, choice(a_choice)
29462957
, original_name(a_name)
@@ -2955,11 +2966,14 @@ struct AstBase : public Annotation
29552966
{}
29562967

29572968
AstBase(const AstBase& ast, const char* a_original_name,
2969+
size_t a_position, size_t a_length,
29582970
size_t a_original_choice_count, size_t a_original_choise)
29592971
: path(ast.path)
29602972
, line(ast.line)
29612973
, column(ast.column)
29622974
, name(ast.name)
2975+
, position(a_position)
2976+
, length(a_length)
29632977
, choice_count(ast.choice_count)
29642978
, choice(ast.choice)
29652979
, original_name(a_original_name)
@@ -2980,6 +2994,8 @@ struct AstBase : public Annotation
29802994
const size_t column;
29812995

29822996
const std::string name;
2997+
size_t position;
2998+
size_t length;
29832999
const size_t choice_count;
29843000
const size_t choice;
29853001
const std::string original_name;
@@ -3245,13 +3261,13 @@ class parser
32453261
if (rule.is_token()) {
32463262
return std::make_shared<T>(
32473263
sv.path, line.first, line.second,
3248-
name.c_str(), sv.choice_count(), sv.choice(),
3264+
name.c_str(), std::distance(sv.ss, sv.c_str()), sv.length(), sv.choice_count(), sv.choice(),
32493265
sv.token());
32503266
}
32513267

32523268
auto ast = std::make_shared<T>(
32533269
sv.path, line.first, line.second,
3254-
name.c_str(), sv.choice_count(), sv.choice(),
3270+
name.c_str(), std::distance(sv.ss, sv.c_str()), sv.length(), sv.choice_count(), sv.choice(),
32553271
sv.transform<std::shared_ptr<T>>());
32563272

32573273
for (auto node: ast->nodes) {

0 commit comments

Comments
 (0)