rascal
rascal copied to clipboard
Leading and trailing layout
Following the tutor's recipe, I have added a layout rule to my grammar to allow for whitespace and single-line comments starting with //
. Unfortunately, the parser does not seem to handle leading an trailing layout. I managed to allow for leading and trailing whitespace (by using #start[...]
), but I can't seem to get leading and trailing comments to work.
My layout is defined as follows:
lexical WhitespaceComment
= [\ \t\n\r]
| @category="Comment" "//" ![\n]* $
;
layout Layout = WhitespaceComment* !>> [\ \t\n\r/];
Am I missing something, or is this a bug? Thanks a lot!
Hi Bas. Thanks for asking. In what particular way does your example fail? Does the parser throw an Ambiguity(..)
exception or a ParseError
exception?
By the way, the code you wrote looks good to me, so something else is causing the trouble. Let's find out what it is.
Maybe you could share the whole module (or a test module derived from it)? There might be some interaction with other productions or imports.
Thanks for the replies. Interestingly, it seems that I'm not getting any ParseError
s or Ambiguity
s anymore. However, leading and trailing comments do not get highlighted properly, except if they are in between syntax. For example, in the following source, the // test
on line 2 is highlighted, but the // test
on line 4 is not:
a -> s <str> . s -> a <int> . s -> b <int> . a -> b <int> . b -> a (ok . b -> s (ok . b -> s <str> . s -> b <date> . end),
// test
quit . b -> s (quit . end))
// test
Here is my grammar/Grammar.rsc
:
module grammar::Grammar
lexical Id = [a-zA-Z][a-zA-Z0-9_]* !>> [a-zA-Z0-9_] \ "end";
lexical Primitive = [a-z]+ !>> [a-z];
lexical Label = [0-9a-zA-Z]+ !>> [0-9a-zA-Z];
lexical RecVar = [A-Z][0-9]* !>> [0-9];
lexical WhitespaceComment
= [\ \t\n\r]
| @category="Comment" "//" ![\n]* $
;
layout Layout = WhitespaceComment* !>> [\ \t\n\r/];
start syntax GlobalType
= message: Exchange exc "\<" Typs typs "\>" "." GlobalType cont
| choice: Exchange exc "(" {Choice ","}+ choices ")"
| recDef: "mu" RecVar var "." GlobalType cont
| recCall: RecVar var
| end: "end"
;
syntax Exchange = exc: Id from "-\>" Id to;
syntax Typs = typs: {Typ ","}+;
syntax Typ
= typ: Primitive
| array: "array" "[" Typs "]"
;
syntax Choice = choice: Label label "." GlobalType cont;
And here is my plugin.rsc
:
module Plugin
import util::IDE;
import ParseTree;
import grammar::Grammar;
private str GT_NAME = "GlobalType";
private str GT_EXT = "gt";
Tree parser(str x, loc l) {
return parse(#start[GlobalType], x, l).top;
}
public void registerGlobalTypes() {
registerLanguage(GT_NAME, GT_EXT, parser);
}
Try moving the parser function into the module with the grammar.
@basvdheuvel if the layout non-terminal is ambiguous itself, the highlighter will choose one of the alternatives and highlight accordingly. This can look quite arbitrary but it should be deterministic, so the same input should have the same highlighting, but adding a space or a newline could suddently flip the interpretation.
- maybe we should highlight ambiguity "red" again for the sake of usability?
- if this is what you see indeed, then there is still an ambiguity to resolve. I'm ready to help if you get stuck.