Allowing only one command perline in DSL

41 views Asked by At

In my grammar I want atmost one command per line (I dont want ';' or ':' to terminate my commands). But when I use ('\r'?'\n') to ensure one command per line, it produces an extra line gape. for example it produces:

read Ram 03DF

write Ram 05FF

JMP Add 056A

CHK Reg 08AA

this is not required. Expected should be without extra line wrap. For example:

read Ram 03DF
write Ram 05FF
JMP Add 056A
CHK Reg 08AA

the overview of what I am doing is

Model:
 (content += Elements ('\r'?'\n') )+
;
Elements:
Data | Path | Ram | Address
;
.
.
.

I am not sure how can I achieve that: Please help. thanks

1

There are 1 answers

0
Joko On BEST ANSWER

I think you should not model new lines in the grammar since it has no influence on the semantics! But in case of pretty printing a line break makes sense after each element. Your grammar fragment should look like this:

Model:
    content+=Element+;
Element:
    Data | Path | Ram | Address;
...

What you are looking for is the org.your.dsl.formatting.YourDslFormatter class. It provides a method stub to implement formatting patterns for pretty printing. There you define that a line break will be inserted after each Element. Try to start with s.th. like this:

class YourDslFormatter {
    @Inject YourDslGrammarAccess g
    override protected void configureFormatting(FormattingConfig c) {
        c.setLinewrap(1).after(g.elementRule)
    }
}

Everytime you trigger Eclipse's autoformat (ctrl + shift + F) your code will be pretty printed in the editor.