Hello World example¶
This is an example of very simple Hello World like language.
These are the steps to build a very basic Hello World - like language.
Write a language description in textX (file
HelloWorldModel: 'hello' to_greet+=Who[','] ; Who: name = /[^,]*/ ;
Description consists of a set of parsing rules which at the same time describe Python classes that will be dynamically created and used to instantiate objects of your model. This small example consists of two rules:
HelloWorldModelstarts with the keyword
helloafter which a one or more
Whoobject must be written separated by commas.
Whoobjects will be parsed, instantiated and stored in a
to_greetlist on a
Whoobjects consists only of its names which must be matched the regular expression rule
/[^,]*/(match non-comma zero or more times). Please see textX grammar section for more information on writing grammar rules.
At this point you can check and visualise meta-model using following command from the command line:
$ textx generate hello.tx --target dot Generating dot target from models: /home/igor/repos/textX/textX/examples/hello_world/hello.tx -> /home/igor/repos/textX/textX/examples/hello_world/hello.dot To convert to png run "dot -Tpng -O hello.dot"
You can see that for each rule from language description an appropriate Python class has been created. A BASETYPE hierarchy is built-in. Each meta-model has it.
Create some content (i.e. model) in your new language (
hello World, Solar System, Universe
Your language syntax is also described by language rules from step 1.
If we break down the text of the example model it looks like this:
We see that the whole line is a
HelloWorldModeland the parts
Solar System, and
Whoobjects. Red coloured text is syntactic noise that is needed by the parser (and programmers) to recognize the boundaries of the objects in the text.
To use your models from Python first create meta-model from textX language description (file
from textx import metamodel_from_file hello_meta = metamodel_from_file('hello.tx')
Than use meta-model to create models from textual description:
hello_model = hello_meta.model_from_file('example.hello')
example.hellowill be parsed and transformed to plain Python objects. Python classes of the created objects are those defined by the meta-model. Returned object
hello_modelwill be a reference to the root of the model, i.e. the object of class
HelloWorldModel. You can use the model as any other Python object. For example:
print("Greeting", ", ".join([to_greet.name for to_greet in hello_model.to_greet]))
You can optionally export model to
dotfile to visualize it. Run following from the command line:
$ textx generate example.hello --grammar hello.tx --target dot Generating dot target from models: /home/igor/repos/textX/textX/examples/hello_world/example.hello -> /home/igor/repos/textX/textX/examples/hello_world/example.dot To convert to png run "dot -Tpng -O example.dot"
This is an object graph automatically constructed from
We see that each
Whoobject is contained in the python attribute
to_greetof list type which is defined by the grammar.
Use your model: interpret it, generate code … It is a plain Python graph of objects with plain attributes!
Try out a complete tutorial for building a simple robot language.