@@ -205,19 +205,70 @@ auto make_scope_exit(EF&& exit_function) -> scope_exit<EF> {
205
205
* PEG
206
206
*---------------------------------------------------------------------------*/
207
207
208
+ /*
209
+ * Line information utility function
210
+ */
211
+ inline std::pair<size_t , size_t > line_info (const char * start, const char * cur) {
212
+ auto p = start;
213
+ auto col_ptr = p;
214
+ auto no = 1 ;
215
+
216
+ while (p < cur) {
217
+ if (*p == ' \n ' ) {
218
+ no++;
219
+ col_ptr = p + 1 ;
220
+ }
221
+ p++;
222
+ }
223
+
224
+ auto col = p - col_ptr + 1 ;
225
+
226
+ return std::make_pair (no, col);
227
+ }
228
+
208
229
/*
209
230
* Semantic values
210
231
*/
211
232
struct SemanticValues : protected std ::vector<any>
212
233
{
234
+ // Input text
213
235
const char * path;
214
236
const char * ss;
237
+
238
+ // Matched string
215
239
const char * c_str () const { return s_; }
216
240
size_t length () const { return n_; }
241
+
242
+ std::string str () const {
243
+ return std::string (s_, n_);
244
+ }
245
+
246
+ // Line number and column at which the matched string is
247
+ std::pair<size_t , size_t > line_info () const {
248
+ return peg::line_info (ss, s_);
249
+ }
250
+
251
+ // Choice number (0 based index)
217
252
size_t choice () const { return choice_; }
218
253
254
+ // Tokens
219
255
std::vector<std::pair<const char *, size_t >> tokens;
220
256
257
+ std::string token (size_t id = 0 ) const {
258
+ if (!tokens.empty ()) {
259
+ assert (id < tokens.size ());
260
+ const auto & tok = tokens[id];
261
+ return std::string (tok.first , tok.second );
262
+ }
263
+ return std::string (s_, n_);
264
+ }
265
+
266
+ // Transform the semantic value vector to another vector
267
+ template <typename T>
268
+ auto transform (size_t beg = 0 , size_t end = static_cast <size_t >(-1 )) const -> vector<T> {
269
+ return this ->transform (beg, end, [](const any& v) { return v.get <T>(); });
270
+ }
271
+
221
272
SemanticValues () : s_(nullptr ), n_(0 ), choice_(0 ) {}
222
273
223
274
using std::vector<any>::iterator;
@@ -243,24 +294,6 @@ struct SemanticValues : protected std::vector<any>
243
294
using std::vector<any>::emplace;
244
295
using std::vector<any>::emplace_back;
245
296
246
- std::string str () const {
247
- return std::string (s_, n_);
248
- }
249
-
250
- std::string token (size_t id = 0 ) const {
251
- if (!tokens.empty ()) {
252
- assert (id < tokens.size ());
253
- const auto & tok = tokens[id];
254
- return std::string (tok.first , tok.second );
255
- }
256
- return std::string (s_, n_);
257
- }
258
-
259
- template <typename T>
260
- auto transform (size_t beg = 0 , size_t end = static_cast <size_t >(-1 )) const -> vector<T> {
261
- return this ->transform (beg, end, [](const any& v) { return v.get <T>(); });
262
- }
263
-
264
297
private:
265
298
friend class Context ;
266
299
friend class PrioritizedChoice ;
@@ -1595,29 +1628,9 @@ inline std::shared_ptr<Ope> wsp(const std::shared_ptr<Ope>& ope) {
1595
1628
* PEG parser generator
1596
1629
*---------------------------------------------------------------------------*/
1597
1630
1598
- inline std::pair<size_t , size_t > line_info (const char * start, const char * cur) {
1599
- auto p = start;
1600
- auto col_ptr = p;
1601
- auto no = 1 ;
1602
-
1603
- while (p < cur) {
1604
- if (*p == ' \n ' ) {
1605
- no++;
1606
- col_ptr = p + 1 ;
1607
- }
1608
- p++;
1609
- }
1610
-
1611
- auto col = p - col_ptr + 1 ;
1612
-
1613
- return std::make_pair (no, col);
1614
- }
1615
-
1616
1631
typedef std::unordered_map<std::string, Definition> Grammar;
1617
1632
typedef std::function<void (size_t , size_t , const std::string&)> Log;
1618
1633
1619
- // typedef std::unordered_map<std::string, std::shared_ptr<Ope>> Rules;
1620
-
1621
1634
class ParserGenerator
1622
1635
{
1623
1636
public:
0 commit comments