Search code examples
rascal

How to implode an embedded choice of alternative symbols?


I have this syntax definition

syntax RuleData
= rule_data: ID+ RulePart '-\>' (Command|RulePart)+ Message? Newlines
;

Which for the most part doesn't cause me an issue, the only problem is that I'm not sure how to implode (Command|RulePart)+, I looked through the Rascal docs but I didn't find anything on how to define "Union" types.

This is what my ADT looks like currently

data RULEDATA
= rule_data(list[str] prefix, list[RULEPART] left, list[???] right, list[str] message, str)
;

The ??? is the bit where it could either be a RulePart (which, for the sake of simplicity, is a list[str]) or a Command (which is a str).


Solution

  • Turns out I was overcomplicating the whole thing. Instead of trying to have a union of types I simply added an additional construction to RULEPART that could accommodate COMMAND. I think I might have been also initially confused about some of the issues because I had bugs in other parts of the code that I was misinterpreting as being caused by this problem.

    data RULEDATA
        = rule_data(list[str] prefix, list[RULEPART] left, list[RULEPART] right, list[str] message, str)
        ;
    
    data RULEPART
        = part(list[RULECONTENT] contents)
        | command(str command)
        ;