Xtext is an open-source framework for developing programming languages and domain-specific languages. It generates parsers and abstract syntax trees from language grammars. It provides an Eclipse-based IDE with features like syntax highlighting, code completion, navigation, and refactoring support. A tutorial document outlines how to create a new Xtext project and write a sample grammar for a "Language of Entities" DSL. The grammar defines entity and field rules with terminals like ID and INT, and supports optional, repetitive, and alternative elements.
2. open-source software framework
generates parser
generates semantic model for abstract syntax tree Eclipse Modeling Framework
provides customizable Eclipse-based IDE
uses ANTLR
syntax highlighting code completion outline view
code navigation hovering
code folding
rename refactoring
Xtext Language Workbench
support for IntelliJ IDEA http://www.jetbrains.com/idea
DSL editor for Web Java servlet
https://www.eclipse.org/Xtext/
3. Creating new Xtext project
*.entities – grammar definition and runtime components that are independent of UI
*.entities.ide – UI components that are independent from Eclipse
*.entities.tests – JUnit tests independent on UI
*.entities.ui.tests – JUnit tests dependent on Eclipse UI
*.entities.ui – components related to Eclipse UI
5. Writing a grammar
may contain fieldsentity
field has name
* for any number of
repetitions, including 0
+ for any number of
repetitions, but at least 1
collection of entities of the model
collection of fields of an entity
“Language of Entities”
name of a field
common terminals
(such as ID, INT)
should contain at least 1 entitymodel
duplicates allowed
– to be fixed later
6. Writing a grammar
can extend another existing entityentity
field has type (existing entity) can be array
[notion] – to refer to an
existing instance of notion
quickfixes automatically
generated by Xtext
space is not allowed
between square brackets
no space between brackets
name – used to check existence
(notion)? – optionalflag
7. Writing a grammar
allow space
arrays should have length
Common terminals
ID
INT
STRING
ML_COMMENT
SL_COMMENT
WS
doesn’t start with a number
integer value
single/double quotes
Java-style multiline
Java-style single line
whitespace, tab, new line, …
8. Writing a grammar
field has type
for simplicity,
forget about arrays
“basic type”
“entity type”
either
or
string integer Boolean
an existing entity
use different
names for the sake
example
...|... – alternatives
(...) – grouping
9. Writing a grammar
may contain statementsentity
prints an expressionstatement
expression integer constant
string constant
Boolean constant
name of an existing field
first – all fields,
then all statements
Expression: IntConstant | StringConstant | BoolConstant | FieldRef;
IntConstant: value=INT;
StringConstant: value=STRING;
BoolConstant: value=(‘on’|’off’);
FieldRef: field=[Field];
10. Writing a grammar
first – all fields,
then all statements
assignmentstatement to an existing field