Search code examples
rascal

How can you define alternative literals in a syntax definition?


I am attempting to define a syntax to parse data definitions in COBOL and had a particular definition for picture clauses like this:

syntax PictureClause =  pic: "PIC" PictureStringType  PictureStringLen ("VALUE"|"VALUES") ValueSpec

My matching ADT for this syntax was as so:

data PictureClause = pic(str pictype, PictureStringLen plen, str valuespec);

However, I noticed that it seems as if the implode function was attempting to match the parenthesized statement to the second str parameter, instead of ignoring it like the "PIC" string literal. However, this syntax definition worked as expected:

syntax PictureClause =  pic: "PIC" PictureStringType  PictureStringLen "VALUE" ValueSpec
                   |pic: "PIC" PictureStringType PictureStringLen "VALUES" ValueSpec;

As the title states, how can I define alternatives in a single statement for literals that I do not want in my ADT in a syntax definition? I can see that alternatives are possible, but I'm wondering if there is a more concise way of defining it, in the spirit of my first attempt


Solution

  • I seem to recall that the current version of implode treats alternatives as nodes and does not flatten them, even if the alternatives are merely literals. Your definition is perfect, nevertheless.

    It's a relatively simple feature request imho, if you have time to register it on GitHub.

    Another option is to not implode at all and use concrete syntax