-
Notifications
You must be signed in to change notification settings - Fork 27
set() can now reference other named token sets #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -379,6 +379,20 @@ func (c *syntaxLoader) convertSet(expr ast.SetExpression, nonterm *syntax.Nonter | |
| default: | ||
| c.Errorf(op, "operator must be one of: first, last, precede or follow") | ||
| } | ||
| } else { | ||
| // Check if the reference targets another named set. | ||
| 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") | ||
| return ret | ||
| } | ||
| if _, ok := c.resolveRefIndex(expr.Symbol()); ok { | ||
| c.Errorf(expr.TmNode(), "ambigious reference") | ||
| return ret | ||
| } | ||
| return c.out.Sets[index] | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| } | ||
| } | ||
| ret.Symbol, ret.Args = c.resolveRef(expr.Symbol(), nonterm) | ||
| return ret | ||
|
|
@@ -510,7 +524,7 @@ func (c *syntaxLoader) instantiateOpt(name string, origin ast.Symref) (int, bool | |
| return index, true | ||
| } | ||
|
|
||
| func (c *syntaxLoader) resolveRef(ref ast.Symref, nonterm *syntax.Nonterm) (int, []syntax.Arg) { | ||
| func (c *syntaxLoader) resolveRefIndex(ref ast.Symref) (int, bool) { | ||
| name := ref.Name() | ||
| text := name.Text() | ||
| index, ok := c.resolver.syms[text] | ||
|
|
@@ -523,6 +537,13 @@ func (c *syntaxLoader) resolveRef(ref ast.Symref, nonterm *syntax.Nonterm) (int, | |
| if !ok && len(text) > 3 && strings.HasSuffix(text, "opt") { | ||
| index, ok = c.instantiateOpt(text, ref) | ||
| } | ||
| return index, ok | ||
| } | ||
|
|
||
| func (c *syntaxLoader) resolveRef(ref ast.Symref, nonterm *syntax.Nonterm) (int, []syntax.Arg) { | ||
| name := ref.Name() | ||
| text := name.Text() | ||
| index, ok := c.resolveRefIndex(ref) | ||
| if !ok { | ||
| c.Errorf(name, "unresolved reference '%v'", text) | ||
| return 0, nil // == eoi | ||
|
|
||
There was a problem hiding this comment.
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.