Search code examples
rascal

Problem with parsing file ending on a newline


It seems a bit like a trivial question, but I am stuck on parsing the end of file EOF using my own island grammar. I am using the new VScode extension btw.

I've mostly been using the examples from the basic recipes and have a simple grammar with the following layout rules:

layout Whitespace = [\t-\n\r\ ]*;
lexical IntegerLiteral = [0-9]+ !>> [0-9];
lexical Comment = "%%" ![\n]* $;

Using this, and some rules it parses some simple files, but will give a parse error anytime a file ends in a newline. (newlines in between lines are no problem).

Am is missing something obvious?

Thanks!


Solution

  • It sounds a bit like your grammar is missing a start nonterminal. All grammar rules get whitespace in between their constituent symbols but not at the start or the end.

    A start nonterminal is the exception:

    start syntax Islands = Island+;
    
    Islands parseIslands(loc input)
        = parse(#start[Islands], input).top;
    

    Passing the start nonterminal to parse will allow the file to start and end with whitespace, and using the .top field you can ignore that whitespace from the parse tree again by projecting out the middle Islands tree.