Package: textX
textX is a meta-language for building Domain-Specific Languages (DSLs) in
Python. It is inspired byXtext.In a nutshell, textX will help you build your textual language in an easy way.
You can invent your own language or build a support for already existing textual
language or file format.From a single language description (grammar), textX will build a parser and a
meta-model (a.k.a. abstract syntax) for the language. See the docs for the
details.textX follows the syntax and semantics of Xtext butdiffers in some
placesand is
implemented 100% in Python usingArpeggioPEG parser - no grammar ambiguities,
unlimited lookahead, interpreter style of work.Quick introHere is a complete example that shows the definition of a simple DSL for
drawing. We also show how to define a custom class, interpret models and search
for instances of a particular type.fromtextximportmetamodel_from_str,get_children_of_typegrammar="""Model: commands*=DrawCommand;DrawCommand: MoveCommand | ShapeCommand;ShapeCommand: LineTo | Circle;MoveCommand: MoveTo | MoveBy;MoveTo: 'move' 'to' position=Point;MoveBy: 'move' 'by' vector=Point;Circle: 'circle' radius=INT;LineTo: 'line' 'to' point=Point;Point: x=INT ',' y=INT;"""# We will provide our class for Point.# Classes for other rules will be dynamically generated.classPoint:def__init__(self,parent,x,y):self.parent=parentself.x=xself.y=ydef__str__(self):return"{},{}".format(self.x,self.y)def__add__(self,other):returnPoint(self.parent,self.x+other.x,self.y+other.y)# Create meta-model from the grammar. Provide `Point` class to be used for# the rule `Point` from the grammar.mm=metamodel_from_str(grammar,classes=[Point])model_str="""move to 5, 10line to 10, 10line to 20, 20move by 5, -7circle 10line to 10, 10"""# Meta-model knows how to parse and instantiate models.model=mm.model_from_str(model_str)# At this point model is a plain Python object graph with instances of# dynamically created classes and attributes following the grammar.defcname(o):returno.__class__.__name__# Let's interpret the modelposition=Point(None,0,0)forcommandinmodel.commands:ifcname(command)=='MoveTo':print('Moving to position',command.position)position=command.positionelifcname(command)=='MoveBy':position=position+command.vectorprint('Moving by',command.vector,'to a new position',position)elifcname(command)=='Circle':print('Drawing circle at',position,'with radius',command.radius)else:print('Drawing line from',position,'to',command.point)position=command.pointprint('End position is',position)# Output:# Moving to position 5,10# Drawing line from 5,10 to 10,10# Drawing line from 10,10 to 20,20# Moving by 5,-7 to a new position 25,13# Drawing circle at 25,13 with radius 10# Drawing line from 25,13 to 10,10# Collect all points starting from the root of the modelpoints=get_children_of_type("Point",model)forpointinpoints:print('Point:{}'.format(point))# Output:# Point: 5,10# Point: 10,10# Point: 20,20# Point: 5,-7# Point: 10,10Video tutorialsIntroduction to textXImplementing Martin Fowler's State Machine DSL in textXDocs and tutorialsThe full documentation with tutorials is available athttp://textx.github.io/textX/stable/Support in IDE/editorsProjects that are currently in progress are:textX-LS- support for Language Server
Protocol and VS Code for any textX based language. This project is about to
supersede the following projects:textX-languageserver-
Language Server Protocol support for textX languagestextX-extensions- syntax
highlighting, code outlineviewX- creating visualizers
for textX languagesIf you are a vim editor user check
outsupport for vim.For emacs there istextx-modewhich is
also available inMELPA.You can also check
outtextX-ninja project. It is
currently unmaintained.Discussion and helpFor general questions, suggestions, and feature requests please useGitHub
Discussions.For issues please useGitHub issue
tracker.Citing textXIf you are using textX in your research project we would be very grateful if you
cite our paper:Dejanović I., Vaderna R., Milosavljević G., Vuković Ž. (2017).TextX: A Python
tool for Domain-Specific Languages
implementation.
Knowledge-Based Systems, 115, 1-4.LicenseMITPython versionsTested for 3.8+