Skip to content

Conversation

@superbobry
Copy link
Contributor

I originally wanted this feature to use new syntax, e.g.

%generate foo = set('a' | 'b');
%generate bar = foo | set('c');
%generate boo = foo & set('c');

so that

  • an identifier in set() is always a terminal/nonterminal;
  • identifier outside of set is always another named token set.

However, that turned out to be a bigger change than what I was hoping for. So, this commit allows identifiers in set() to reference both terminals/nonterminals and other named token sets. Rewriting the above example,

%generate foo = set('a' | 'b');
%generate bar = set(foo | 'c');
%generate boo = set(bar & 'c');

I originally wanted this feature to use new syntax, e.g.

    %generate foo = set('a' | 'b');
    %generate bar = foo | set('c');
    %generate boo = foo & set('c');

so that

* an identifier in set() is always a terminal/nonterminal;
* identifier outside of set is always another named token set.

However, that turned out to be a bigger change than what I was hoping for.
So, this commit allows identifiers in set() to reference both
terminals/nonterminals and other named token sets. Rewriting the above
example,

    %generate foo = set('a' | 'b');
    %generate bar = set(foo | 'c');
    %generate boo = set(bar & 'c');
name := expr.Symbol().Name()
if index, ok := c.namedSets[name.Text()]; ok {
if index >= len(c.out.Sets) {
c.Errorf(expr.TmNode(), "forward references are not allowed")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw Forward references should work just fine - the circular dependencies are handled inside syntax/set.go.

c.Errorf(expr.TmNode(), "ambigious reference")
return ret
}
return c.out.Sets[index]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works but what happens here under the hood is that the set expression you reference here is evaluated several times. I'd instead introduce a new syntax.SetOp - Reference, and store the SetIndex inside TokenSet (similar to syntax.Expr.SetIndex). Then inside syntax/set->translate function create a future set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants