Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion compiler/syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
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.

return ret
}
if _, ok := c.resolveRefIndex(expr.Symbol()); ok {
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.

}
}
ret.Symbol, ret.Args = c.resolveRef(expr.Symbol(), nonterm)
return ret
Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions parsers/test/parser_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,15 @@ var tmRuleType = [...]uint32{
0, // QualifiedNameopt :
}

// set(MINUS | PLUS) = MINUS, PLUS
var plusMinus = []token.Type{
25, 27,
}

// set((MINUS | PLUS) | MINUSGT) = MINUS, MINUSGT, PLUS
var plusMinusArrow = []token.Type{
25, 26, 27,
}

// set(follow ERROR) =
var afterErr = []token.Type{}
5 changes: 4 additions & 1 deletion parsers/test/test.tm
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ MultiLineComment -> MultiLineComment: /\/\*/ (space)

:: parser

%generate plusMinus = set('-' | '+');
%generate plusMinusArrow = set(plusMinus | '->');

%input Test, Decl1;

Test -> Test:
Expand Down Expand Up @@ -166,7 +169,7 @@ foo_la:
;

foo_nonterm<A> :
IntegerConstant '.' expr
IntegerConstant '.' expr
| [A] IntegerConstant 'foo_' expr
;

Expand Down