arithy / packcc

A parser generator for C
Other
348 stars 29 forks source link

value passthrough #58

Closed 0alfa closed 7 months ago

0alfa commented 2 years ago

is it possible to have something like this

expression <- x:(a / b / c / d) { $$ = x; }

instead of this

expression <- a { $$ = a; } / b { $$ = b; } / c { $$ = c; } / d { $$ = d; }

?

arithy commented 2 years ago

Let me rewrite your expressions more precisely. The rule definition way you want: expression <- x:( rule_a / rule_b / rule_c / rule_d ) { $$ = x; } The currently valid definition way: expression <- a:rule_a { $$ = a; } / b:rule_b { $$ = b; } / c:rule_c { $$ = c; } / d:rule_d { $$ = d; }

If the syntax is limited to the manner below: variable : ( rulename / rulename / ... )

it may be possible to realize this, but I cannot promise to do it soon. I'll try to implement it whenever I have time.

0alfa commented 2 years ago

this macro works for now

#define PT                                                                     \
  pcc_value_refer_table_t _x = __pcc_in->data.leaf.values;                     \
  i(_x.len, I(_x.buf[i] && *_x.buf[i], NODE *n = ((NODE *)*_x.buf[i]);         \
              __ = *_x.buf[i]))

value <- (a:funclit / a:term) { PT; }

expands to

static void pcc_action_value_0(pcc_context_t *__pcc_ctx, pcc_thunk_t *__pcc_in,
                               pcc_value_t *__pcc_out) {
  pcc_value_refer_table_t _x = __pcc_in->data.leaf.values;
  for (size_t i = 0; i < _x.len; i++) {
    if (_x.buf[i] && *_x.buf[i]) {
      NODE *n = ((NODE *)*_x.buf[i]);
      (*__pcc_out) = *_x.buf[i];
    };
  };
}
awrc commented 12 months ago

I've found that this works

expression <- (x:a / x:b / x:c / x:d) { $$ = x; }

arithy commented 7 months ago

@awrc , thank you for the helpful comment! @0alfa , please use the syntax above. I'd like to close this issue as "not planned".