0
votes

I'm creating a DSL with Xtext which will be used for generating images. However because I'm completely unknown to Xtext, I'm stumbling upon some problems and I hope you can give me some guidance.

Eventually I want the user to use the following coding structure:

bgcolor: ffffff
bgsize: 500x500
box1:
    bgcolor: 000000
    size: 300x300
    position: 100x100
box2:
    bgcolor: eeeeee
    size: 300x300
    position: 100x100

What I came up with in Xtext is the following (I did not change anything to the MWE2):

Image : (ImageElement+=ImageElementType)*;
ImageElementType:  BgColor | BgSize | Box;


// SET BASIC TERMINAL RULES
terminal SIZE : INT 'x' INT;

terminal COLOR : COLOR_BASIC | COLOR_HEX;
terminal COLOR_BASIC : 'green' | 'red' | 'blue' | 'yellow' | 'black' | 'white' | 'orange' | 'purple' | 'pink';
terminal DIGIT : ('0'..'9'|'a'..'f'|'A'..'F');
terminal COLOR_HEX : DIGIT DIGIT DIGIT DIGIT DIGIT DIGIT;

BgColor: name='bgcolor:' value=COLOR;
BgSize: name='bgsize:' value=SIZE;  

Box: name='box:';

There are a couple of issues I need to solve but I would like to know what the best approach is to these issues.

  • How can I set boundaries to the INT's used in terminal rule SIZE.
  • How can I set that bgcolor: or bgsize: may only be used once?
  • How can I set that box1, box2, etc is constructed from the same rule Box:?
  • How can I set that the properties specified under box1: belong to that box?
  • How can I make indents mandatory?

I'm not seeking for a direct solution (would be great though ;-) ) but just some tips how to tackle these issues. Are all of these solved by editing the MWE2 workflow? I have some hard time with the documentation of Xtext.

Thanks in advance for your time and comments!

Kind regards

1

1 Answers

0
votes

How can I set that bgcolor: or bgsize: may only be used once?

Make them to attributes of ImageElementType

How can I set that box1, box2, etc is constructed from the same rule Box:?

Add attribute name to the Box

How can I set that the properties specified under box1: belong to that box?

One possibility is to add a new Property concept and add them to the Box rule

How can I make indents mandatory?

You could create a whitespace sensitive language like Python, but a better idea would be to implement the Formatter for your language and auto format the model before save.

Regarding the terminal rule SIZE and in general... In most cases, it's better to have a more generic grammar and apply your restrictions in the corresponding Language Validator or ValueConverter.

Not sure what the exact language specification is, but here is a possible start point. Leaving terminal rules as is:

Image:
    ImageElement+=ImageElementType*;

ImageElementType:
    'bgcolor' ':' bgcolor=COLOR
    'bgsize' ':' bgsize=SIZE
    boxes+=Box*;

Box:
    name=ID ':'
    properties+=Property*;

Property:
    name = KnownProperties ':' value = PropertyValue
;

PropertyValue:
    SIZE | COLOR | DIGIT
;

enum KnownProperties:
    bgcolor | size | position;

// SET BASIC TERMINAL RULES
terminal SIZE:
    INT 'x' INT;
terminal COLOR:
    COLOR_BASIC | COLOR_HEX;
terminal COLOR_BASIC:
    'green' | 'red' | 'blue' | 'yellow' | 'black' | 'white' | 'orange' | 'purple' | 'pink';
terminal DIGIT:
    ('0'..'9' | 'a'..'f' | 'A'..'F');
terminal COLOR_HEX:
    DIGIT DIGIT DIGIT DIGIT DIGIT DIGIT;