Open source · MIT · Python 3.8+
Grammar in.
Python objects out.
textX is a meta-language for building Domain-Specific Languages in Python. Write one grammar and textX gives you a parser and a meta-model. Your models come back as plain Python objects, with references already resolved.
pip install textX// A tiny language for describing data entities
Model:
types*=SimpleType
entities+=Entity
;
SimpleType:
'type' name=ID
;
Entity:
'entity' name=ID '{'
properties*=Property
'}'
;
Property:
name=ID ':' type=[Type] // a reference
;
Type:
SimpleType | Entity
;
Comment:
/\/\/.*$/
;type string
type int
entity Person {
name: string
age: int
address: Address
}
entity Address {
street: string
city: string
}No parser code was written. textX builds the parser straight from the grammar, and resolves Address to the entity defined below it.
from textx import metamodel_from_file
mm = metamodel_from_file('entity.tx')
model = mm.model_from_file('person.ent')
for entity in model.entities:
print(entity.name)
for prop in entity.properties:
# prop.type is the actual Entity/SimpleType object
print(f' {prop.name}: {prop.type.name}')Person name: string age: int address: Address Address street: string city: string
- 1 · Grammar
- 2 · Model
- 3 · Python
- 4 · Meta-model
- 5 · Objects
How textX works
- grammar.txYou write the rules
- meta-modelPython classes
- parserBuilt for you
- modelObject graph
- your codeGenerate, interpret, validate
Why textX
Less parser plumbing, more language design
Inspired by Xtext, built for Python. textX stays a library, not a workbench, so it drops into any project that can pip install.
One grammar, two results
A textX grammar describes the concrete syntax and the meta-model at the same time. You get a parser and Python classes from a single file.
Models are Python objects
Parsing gives you a graph of plain Python objects, with no AST to walk. Register your own classes if you want behaviour on them.
References resolved for you
Cross-references are linked automatically. Scope providers and RREL cover the harder cases, including references across files and languages.
No code generation step
Grammars are interpreted by Arpeggio, a PEG parser. Change the grammar and run again, with no generated sources to keep in sync.
Generators & the textx CLI
Turn models into code, configs or docs with Jinja-based generators. Register languages and generators, then use them from one command.
Visualize & edit
Render grammars and models with GraphViz or PlantUML. textX-LS adds validation, completion and highlighting in VS Code.
Where it fits
Between a parser generator and a language workbench
Parser generators
ANTLR · yacc · bison
You get a parse tree, then write the AST, the linking and the semantics yourself.
textX
pip install textX
A grammar becomes a meta-model, a parser and a linked object graph. It is a Python library, so it works alongside your existing tools.
Language workbenches
Xtext · Spoofax · MPS
Powerful, but they tie you to an IDE platform and a separate toolchain.
Tooling
Your language, in your editor
textX-LS adds Language Server Protocol support to any textX language. That gives you validation, completion and live reload in VS Code. Generators produce syntax highlighting and full extensions straight from your grammar.
- ✓ VS Code via textX-LS
- ✓ Vim and Emacs modes for grammar files
- ✓ In-browser Playground, no install needed

In the wild
Used for requirements, compilers, music and science
- StrictDocTechnical documentation and requirements management.
- osxphotosExports pictures and metadata from Apple Photos on macOS.
- ppciPure Python compiler infrastructure.
- pyecoreECore (EMF) implementation in Python, with textX integration.
- cutevariantExplores genetic variations from VCF files.
- pyFliesA DSL for designing experiments in psychology.
- SilveraA DSL for microservice-based software development.
- applangTextual DSL for generating mobile applications.
Build your first language in ten minutes
The Hello World tutorial takes you from an empty file to a working parser. Then try robots, entities and state machines.