Skip to content

Commit 2a0af83

Browse files
committed
Improve error diagnostics
1 parent 84f0d4a commit 2a0af83

6 files changed

Lines changed: 87 additions & 1 deletion

File tree

include/iris/x4/core/action.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
#include <iris/x4/core/context.hpp>
1818
#include <iris/x4/core/action_context.hpp>
1919

20-
#include <ranges>
20+
#include <ranges> // subrange
2121
#include <iterator>
2222
#include <concepts>
2323
#include <type_traits>
2424
#include <utility>
25+
#include <format>
2526

2627
namespace iris::x4 {
2728

@@ -109,6 +110,11 @@ struct action : proxy_parser<Subject, action<Subject, ActionF>>
109110

110111
constexpr void operator[](auto const&) const = delete; // You can't add semantic action for semantic action
111112

113+
[[nodiscard]] constexpr std::string get_x4_info() const
114+
{
115+
return std::format("{}[f]", get_info<Subject>{}(this->subject));
116+
}
117+
112118
private:
113119
// Semantic action with no parameter: `p[([] { /* ... */ })]`
114120
template<class Context, X4Attribute Attr>

include/iris/x4/core/skip_over.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,25 @@ enum struct builtin_skipper_kind : char
4242
space,
4343
};
4444

45+
namespace detail {
46+
47+
template<builtin_skipper_kind Kind>
48+
struct builtin_skipper_traits;
49+
50+
template<>
51+
struct builtin_skipper_traits<builtin_skipper_kind::blank>
52+
{
53+
static constexpr char const* name = "blank";
54+
};
55+
56+
template<>
57+
struct builtin_skipper_traits<builtin_skipper_kind::space>
58+
{
59+
static constexpr char const* name = "space";
60+
};
61+
62+
} // detail
63+
4564
template<std::forward_iterator It, std::sentinel_for<It> Se, class Context>
4665
requires X4Subject<get_context_plain_t<contexts::skipper, Context>>
4766
constexpr void skip_over(It& first, Se const& last, Context const& ctx)

include/iris/x4/directive/skip.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <iris/x4/char/char_class_tags.hpp>
1919

20+
#include <format>
2021
#include <concepts>
2122
#include <iterator>
2223
#include <type_traits>
@@ -49,6 +50,15 @@ struct skip_directive : proxy_parser<Subject, skip_directive<Subject, Skipper>>
4950
return this->subject.parse(first, last, x4::replace_first_context<contexts::skipper>(ctx, skipper_), attr);
5051
}
5152

53+
[[nodiscard]] constexpr std::string get_x4_info() const
54+
{
55+
return std::format(
56+
"skip({})[{}]",
57+
get_info<Skipper>{}(this->skipper_),
58+
get_info<Subject>{}(this->subject)
59+
);
60+
}
61+
5262
private:
5363
template<class Context>
5464
using context_t = std::remove_cvref_t<decltype(
@@ -98,6 +108,15 @@ struct builtin_skip_directive : proxy_parser<Subject, builtin_skip_directive<Kin
98108
/* constexpr */ builtin_skipper_kind skipper_kind = Kind;
99109
return this->subject.parse(first, last, x4::replace_first_context<contexts::skipper>(ctx, skipper_kind), attr);
100110
}
111+
112+
[[nodiscard]] constexpr std::string get_x4_info() const
113+
{
114+
return std::format(
115+
"skip({})[{}]",
116+
detail::builtin_skipper_traits<Kind>::name,
117+
get_info<Subject>{}(this->subject)
118+
);
119+
}
101120
};
102121

103122

include/iris/x4/operator/alternative.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <iris/rvariant/rvariant.hpp>
2323

24+
#include <format>
2425
#include <concepts>
2526
#include <iterator>
2627
#include <type_traits>
@@ -153,6 +154,15 @@ struct alternative : binary_parser<Left, Right, alternative<Left, Right>>
153154
}
154155
return false; // `attr` is untouched
155156
}
157+
158+
[[nodiscard]] constexpr std::string get_x4_info() const
159+
{
160+
return std::format(
161+
"{} | {}",
162+
get_info<Left>{}(this->left),
163+
get_info<Right>{}(this->right)
164+
);
165+
}
156166
};
157167

158168
template<X4Subject Left, X4Subject Right>

include/iris/x4/operator/list.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <iris/x4/traits/container_traits.hpp>
2121

22+
#include <format>
2223
#include <iterator>
2324
#include <type_traits>
2425
#include <utility>
@@ -63,6 +64,15 @@ struct list : binary_parser<Left, Right, list<Left, Right>>
6364
return true;
6465
}
6566
}
67+
68+
[[nodiscard]] constexpr std::string get_x4_info() const
69+
{
70+
return std::format(
71+
"({} % {})",
72+
get_info<Left>{}(this->left),
73+
get_info<Right>{}(this->right)
74+
);
75+
}
6676
};
6777

6878
template<X4Subject Left, X4Subject Right>

include/iris/x4/operator/sequence.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@
2020
#include <iris/x4/directive/expect.hpp>
2121

2222
#include <iris/alloy/tuple.hpp>
23+
#include <iris/type_traits.hpp>
2324

25+
#include <format>
2426
#include <concepts>
2527
#include <iterator>
2628
#include <type_traits>
2729
#include <utility>
2830

2931
namespace iris::x4 {
3032

33+
template<class Subject>
34+
struct expect_directive;
35+
3136
template<class Left, class Right>
3237
struct sequence : binary_parser<Left, Right, sequence<Left, Right>>
3338
{
@@ -74,6 +79,23 @@ struct sequence : binary_parser<Left, Right, sequence<Left, Right>>
7479
{
7580
return detail::parse_sequence(*this, first, last, ctx, attr);
7681
}
82+
83+
[[nodiscard]] constexpr std::string get_x4_info() const
84+
{
85+
if constexpr (iris::is_ttp_specialization_of_v<Right, expect_directive>) {
86+
return std::format(
87+
"{} > {}",
88+
get_info<Left>{}(this->left),
89+
get_info<typename Right::subject_type>{}(this->right.subject)
90+
);
91+
} else {
92+
return std::format(
93+
"{} >> {}",
94+
get_info<Left>{}(this->left),
95+
get_info<Right>{}(this->right)
96+
);
97+
}
98+
}
7799
};
78100

79101
template<X4Subject Left, X4Subject Right>

0 commit comments

Comments
 (0)