set() can now reference other named token sets#74
Open
superbobry wants to merge 1 commit intoinspirer:mainfrom
Open
set() can now reference other named token sets#74superbobry wants to merge 1 commit intoinspirer:mainfrom
superbobry wants to merge 1 commit intoinspirer:mainfrom
Conversation
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');
inspirer
reviewed
Nov 24, 2023
| 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") |
Owner
There was a problem hiding this comment.
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] |
Owner
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I originally wanted this feature to use new syntax, e.g.
so that
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,