Week 2: Building a DSML with Xtext
Session goal
Today, we will focus on textual domain-specific modelling languages
Copyright By PowCoder代写 加微信 powcoder
– Learn how to declaratively describe the structure of a language
– Use the Xtext language workbench to generate a complete IDE for our language
– Start to explore more advanced concepts such as scoping and Ctrl-Space code completion
Style of learning
– Active work: I will introduce a topic, you will try it out on your machines
– Pair work: Two students to share a computer during the active-work sessions
– Prepared work: building on the work you have done in preparation for the session
24/01/2020 (c) King’s College London 2
What’s a language workbench?
24/01/2020 (c) King’s College London 3
What are the key steps in developing a new DSL with Xtext?
24/01/2020 (c) King’s College London 4
Demo: a simple Turtles language
Step 1: Creating a new Xtext project
When starting Eclipse, you might find it is started in the modelling perspective. You need to open the Java perspective:
24/01/2020 (c) King’s College London 6
Step 1: Creating a new Xtext project
24/01/2020 (c) King’s College London 7
Step 1: Creating a new Xtext project
24/01/2020 (c) King’s College London 7
Step 1: Creating a new Xtext project
24/01/2020 (c) King’s College London 7
Step 1: Creating a new Xtext project
24/01/2020 (c) King’s College London 8
Step 1: Creating a new Xtext project
24/01/2020 (c) King’s College London 8
Use a qualified name for the project Will be the base name for all Eclipse plugins generated for your language.
Step 1: Creating a new Xtext project
24/01/2020 (c) King’s College London 8
Build on project name and add an identifier that could be a Java class name.
Step 1: Creating a new Xtext project
24/01/2020 (c) King’s College London 8
Files with this extension will contain code in your new language.
Step 1: Creating a new Xtext project
Generate the new projects
24/01/2020 (c) King’s College London
Step 1: Creating a new Xtext project … some time later
24/01/2020 (c) King’s College London 9
Step 1: Creating a new Xtext project … some time later
24/01/2020 (c) King’s College London 9
Main language project. Don’t worry about the others for now
Step 1: Creating a new Xtext project … some time later
24/01/2020 (c) King’s College London 9
Step 1: Creating a new Xtext project … some time later
24/01/2020 (c) King’s College London 9
Grammar file already opened. This is where we define our language.
In your pairs
– Open Eclipse
– Create a new Xtext plugin for your turtles language – Choose a sensible language name and file extension
You have 10 minutes
24/01/2020 (c) King’s College London 10
Step 2: Write a basic grammar
Basic structure of a grammar rule:
24/01/2020 (c) King’s College London 12
Step 2: Write a basic grammar
Basic structure of a grammar rule:
Keyword starting a language definition
24/01/2020 (c) King’s College London 12
Step 2: Write a basic grammar
Basic structure of a grammar rule:
Language name
24/01/2020 (c) King’s College London 12
Step 2: Write a basic grammar
Basic structure of a grammar rule:
Import some standard definitions (comments etc)
24/01/2020 (c) King’s College London 12
Step 2: Write a basic grammar
Basic structure of a grammar rule:
Language definition rules
24/01/2020 (c) King’s College London
Step 2: Write a basic grammar
Basic structure of a grammar rule:
Start rule: every model fits this rule
24/01/2020 (c) King’s College London 12
Step 2: Write a basic grammar
Basic structure of a grammar rule:
24/01/2020 (c) King’s College London 12
Step 2: Write a basic grammar
Basic structure of a grammar rule:
24/01/2020 (c) King’s College London 12
Step 2: Write a basic grammar
Basic structure of a grammar rule:
Expected text
24/01/2020 (c) King’s College London 12
Step 2: Write a basic grammar
Basic structure of a grammar rule:
24/01/2020 (c) King’s College London
Call out to separate rule
Step 2: Write a basic grammar
Basic structure of a grammar rule:
Assign result of rule call (see next week)
24/01/2020 (c) King’s College London 12
Step 2: Write a basic grammar
Things you can do in a rule
RuleName Expect to find anything that fits the description in rule RuleName at this position
<...>? Indicate that some part of a rule is optional
<...>+ Indicate that some part of a rule can occur arbitrarily often (but at least once)
Identifier=RuleName Indicate that whatever matched RuleName should be captured in a variable called Identifier; use += if this is inside a * or + clause.
A full introduction to EBNF for grammar specification and issues of correctly specifying grammars for LR parsers can be found in the literature.
Expect to find this exact string at this point in the model
Group sub-parts of a rule; useful for indicating repetitions etc of larger parts of the rule
Indicate that some part of a rule can occur arbitrarily often (including never)
<...A> | <...B>
Indicate to expect something matching either <...A> or <...B>
24/01/2020 (c) King’s College London 13
Step 2: Write a basic grammar
Types of rules
24/01/2020 (c) King’s College London 14
Step 2: Write a basic grammar
Types of rules
Start rule: every model fits this rule
24/01/2020 (c) King’s College London 14
Step 2: Write a basic grammar
Types of rules
“Normal” rule
24/01/2020 (c) King’s College London 14
Step 2: Write a basic grammar
Types of rules
Enumeration rule: defines a set of alternative bits of text.
24/01/2020 (c) King’s College London 14
Step 3: Test drive your language
1. Generate full language implementation (need Internet access the first time)
24/01/2020 (c) King’s College London 15
Step 3: Test drive your language
1. Generate full language implementation (need Internet access the first time)
2. Run a second Eclipse instance for testing
24/01/2020 (c) King’s College London 15
Step 3: Test drive your language
1. Generate full language implementation (need Internet access the first time)
2. Run a second Eclipse instance for testing
3. Create a Java project and a file with the
right extension
24/01/2020 (c) King’s College London 15
In your pairs
– Change your turtle language’s grammar so that you can – Issue move commands
– Issue turning commands
– Have an arbitrary sequence of such commands
– Hint: You can use pre-defined terminal INT where you want to allow integers – Test your language with a simple example
You have 10 minutes
Download last step as tag initial_project_creation from the Turtle DSML repository linked from KEATS
24/01/2020 (c) King’s College London 16
Testing your language with XPECT
Let’s write a jUnit test for our language
– We’ll use the Xpect framework
– Write tests directly in our new language
24/01/2020 (c) King’s College London 18
Testing your language with XPECT
Let’s write a jUnit test for our language
– We’ll use the Xpect framework
– Write tests directly in our new language
1. In our new Eclipse instance, create a “plugin” project
– A special kind of Java project
24/01/2020 (c) King’s College London 18
Testing your language with XPECT
Let’s write a jUnit test for our language
– We’ll use the Xpect framework
– Write tests directly in our new language
1. In our new Eclipse instance, create a “plugin” project
– A special kind of Java project
2. Add Xpect and our language project to the dependencies
of the new project
24/01/2020 (c) King’s College London 18
Testing your language with XPECT
Let’s write a jUnit test for our language
– We’ll use the Xpect framework
– Write tests directly in our new language
1. In our new Eclipse instance, create a “plugin” project
– A special kind of Java project
2. Add Xpect and our language project to the dependencies
of the new project
3. Create an empty jUnit test class
24/01/2020 (c) King’s College London 18
Testing your language with XPECT
Let’s write a jUnit test for our language
– We’ll use the Xpect framework
– Write tests directly in our new language
1. In our new Eclipse instance, create a “plugin” project
– A special kind of Java project
2. Add Xpect and our language project to the dependencies
of the new project
3. Create an empty jUnit test class
4. Create a file with extension .ggame.xt (replace ggame with
the extension for your language) write your test example
24/01/2020 (c) King’s College London 18
Testing your language with XPECT
Let’s write a jUnit test for our language
– We’ll use the Xpect framework
– Write tests directly in our new language
1. In our new Eclipse instance, create a “plugin” project
– A special kind of Java project
2. Add Xpect and our language project to the dependencies
of the new project
3. Create an empty jUnit test class
4. Create a file with extension .ggame.xt (replace ggame with
the extension for your language) write your test example 5. Run the test directly from the .dt file
24/01/2020 (c) King’s College London 18
In your pairs
– Write a test for your Turtle language and run it
– Extend your test to include a loop construct
– Run the test again – it should fail
– Update your language definition to add the loop construct
– Regenerate the language infrastructure and restart the runtime Eclipse instance – Run the test again
You have 15 minutes
Download last step as tag DSML_v1.0
from the Turtle DSML repository linked from KEATS
24/01/2020 (c) King’s College London 19
Cross references
Referencing things by name is essential in textual languages
– Variable definitions and variable usage – Method declarations and method calls -…
Xtext has special support for this
– In the grammar, we can say that another part of the model can be cross-referenced by putting the rule name in angular brackets […]
– The rule should contain a variable called name that is assigned an ID value
24/01/2020 (c) King’s College London 21
Cross references
Referencing things by name is essential in textual languages
– Variable definitions and variable usage – Method declarations and method calls -…
Xtext has special support for this
– In the grammar, we can say that another part of the model can be cross-referenced by putting the rule name in angular brackets […]
– The rule should contain a variable called name that is assigned an ID value
24/01/2020 (c) King’s College London 21
Cross references
Referencing things by name is essential in textual languages
– Variable definitions and variable usage – Method declarations and method calls -…
Xtext has special support for this
– In the grammar, we can say that another part of the model can be cross-referenced by putting
the rule name in angular brackets […]
– The rule should contain a variable called name that is assigned an ID value
Greetings are identified by their name
24/01/2020 (c) King’s College London 21
Cross references
Referencing things by name is essential in textual languages
– Variable definitions and variable usage – Method declarations and method calls -…
Xtext has special support for this
– In the grammar, we can say that another part of the model can be cross-referenced by putting the rule name in angular brackets […]
– The rule should contain a variable called name that is assigned an ID value
We can use the name of a previously defined Greeting in a Good Bye statement.
24/01/2020 (c) King’s College London 21
Cross references
From a cross reference, Xtext generates additional support:
24/01/2020 (c) King’s College London 22
Cross references
From a cross reference, Xtext generates additional support:
Error markers when an undefined name is provided.
24/01/2020 (c) King’s College London 22
Cross references
From a cross reference, Xtext generates additional support:
Code completion based on existing names.
24/01/2020 (c) King’s College London 22
In your pairs
– Extend your turtle language with a way of defining variables
– Variables have a name and are given an int value when defined
– Allow variables to be used wherever int values can be provided directly – Loop headers
– Move statements
– Int values should still be allowed in these places, too You have 15 minutes
Download last step as tag DSML_v2_Loop
from the Turtle DSML repository linked from KEATS
24/01/2020 (c) King’s College London 23
Testing cross references
We can test cross references in Xpect:
24/01/2020 (c) King’s College London 25
Testing cross references
We can test cross references in Xpect:
24/01/2020 (c) King’s College London 25
What can be referenced in a particular place is called a scope. Therefore, we define a scope test.
Testing cross references
We can test cross references in Xpect:
We’re interested in the scope at the location indicated by the word ‘Steffen’ below.
24/01/2020 (c) King’s College London 25
Testing cross references
We can test cross references in Xpect:
This is what we expect the scope to be: it should contain ‘Steffen’, should not contain ‘Hans’, and might contain other stuff, too.
24/01/2020 (c) King’s College London 25
Testing cross references
We can test cross references in Xpect:
We can also test that a suitable error marker is generated:
24/01/2020 (c) King’s College London 25
Testing cross references
We can test cross references in Xpect:
We can also test that a suitable error marker is generated:
State that we’re expecting so see an error here.
24/01/2020 (c) King’s College London
Testing cross references
We can test cross references in Xpect:
We can also test that a suitable error marker is generated:
The error message we’re expecting to see. Must be an exact match.
24/01/2020 (c) King’s College London 25
Testing cross references
We can test cross references in Xpect:
We can also test that a suitable error marker is generated:
24/01/2020 (c) King’s College London 25
The location where we expect the error marker to be placed.
Testing cross references
We can test cross references in Xpect:
We can also test that a suitable error marker is generated:
Note that an error expectation removes the error marker: Hans is not marked as an error above!
24/01/2020 (c) King’s College London 25
In your pairs
– Write tests for your variables and variable references
– Write tests for variables defined / used inside and outside of loops
– What do you notice?
You have 5 minutes
Download last step as tag DSML_v3_variables from the Turtle DSML repository linked from KEATS
24/01/2020 (c) King’s College London 26
Next week: A look behind the scenes
– How is the language managed internally? – Key structures in language engineering
– And using them to our advantage
– An alternative approach to language development
24/01/2020 (c) King’s College London 28
Questions?
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com