textX


NametextX JSON
Version 4.0.1 PyPI version JSON
download
home_pageNone
SummaryMeta-language for DSL implementation inspired by Xtext
upload_time2023-11-12 11:55:39
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8, <3.13
licenseNone
keywords parser meta-language meta-model language dsl
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ![](https://raw.githubusercontent.com/textX/textX/master/art/textX-logo.png)

[![PyPI Version](https://img.shields.io/pypi/v/textX.svg)](https://pypi.python.org/pypi/textX)
![](https://img.shields.io/pypi/l/textX.svg)
[![Build status](https://github.com/textx/textx/actions/workflows/ci-linux-ubuntu.yml/badge.svg)](https://github.com/textx/textx/actions)
[![Code test coverage](https://coveralls.io/repos/github/textX/textX/badge.svg?branch=master)](https://coveralls.io/github/textX/textX?branch=master)
[![Documentation Status](https://img.shields.io/badge/docs-latest-green.svg)](http://textx.github.io/textX/latest/)


textX is a meta-language for building Domain-Specific Languages (DSLs) in
Python. It is inspired by [Xtext].

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 but [differs in some
places](http://textx.github.io/textX/latest/about/comparison/) and is
implemented 100% in Python using [Arpeggio] PEG parser - no grammar ambiguities,
unlimited lookahead, interpreter style of work.


## Quick intro

Here 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.

```python
from textx import metamodel_from_str, get_children_of_type

grammar = """
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.
class Point:
    def __init__(self, parent, x, y):
        self.parent = parent
        self.x = x
        self.y = y

    def __str__(self):
        return "{},{}".format(self.x, self.y)

    def __add__(self, other):
        return Point(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, 10
    line to 10, 10
    line to 20, 20
    move by 5, -7
    circle 10
    line 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.

def cname(o):
    return o.__class__.__name__

# Let's interpret the model
position = Point(None, 0, 0)
for command in model.commands:
    if cname(command) == 'MoveTo':
        print('Moving to position', command.position)
        position = command.position
    elif cname(command) == 'MoveBy':
        position = position + command.vector
        print('Moving by', command.vector, 'to a new position', position)
    elif cname(command) == 'Circle':
        print('Drawing circle at', position, 'with radius', command.radius)
    else:
        print('Drawing line from', position, 'to', command.point)
        position = command.point
print('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 model
points = get_children_of_type("Point", model)
for point in points:
    print('Point: {}'.format(point))

# Output:
# Point: 5,10
# Point: 10,10
# Point: 20,20
# Point: 5,-7
# Point: 10,10
```


## Video tutorials


### Introduction to textX


[![Introduction to
textX](https://img.youtube.com/vi/CN2IVtInapo/0.jpg)](https://www.youtube.com/watch?v=CN2IVtInapo)


### Implementing Martin Fowler's State Machine DSL in textX

[![Implementing State Machine
DSL](https://img.youtube.com/vi/HI14jk0JIR0/0.jpg)](https://www.youtube.com/watch?v=HI14jk0JIR0)


## Docs and tutorials

The full documentation with tutorials is available at
http://textx.github.io/textX/stable/


# Support in IDE/editors

Projects that are currently in progress are:

- [textX-LS](https://github.com/textX/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](https://github.com/textX/textX-languageserver) -
    Language Server Protocol support for textX languages
  - [textX-extensions](https://github.com/textX/textX-extensions) - syntax
    highlighting, code outline
- [viewX](https://github.com/danielkupco/viewX-vscode) - creating visualizers
  for textX languages
  
If you are a vim editor user check
out [support for vim](https://github.com/textX/textx.vim/).

For emacs there is [textx-mode](https://github.com/textX/textx-mode) which is
also available in [MELPA](https://melpa.org/#/textx-mode).

You can also check
out [textX-ninja project](https://github.com/textX/textX-ninja). It is
currently unmaintained.


## Discussion and help

For general questions, suggestions, and feature requests please use [GitHub
Discussions](https://github.com/textX/textX/discussions).


For issues please use [GitHub issue
tracker](https://github.com/textX/textX/issues).


## Citing textX

If 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](https://www.doi.org/10.1016/j.knosys.2016.10.023).
Knowledge-Based Systems, 115, 1-4.


## License

MIT

## Python versions

Tested for 3.8+


[Arpeggio]: https://github.com/textX/Arpeggio
[Xtext]: http://www.eclipse.org/Xtext/

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "textX",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8, <3.13",
    "maintainer_email": "\"Igor R. Dejanovi\u0107\" <igor.dejanovic@gmail.com>, Pierre Bayerl <noemail@nowhere.com>",
    "keywords": "parser,meta-language,meta-model,language,DSL",
    "author": null,
    "author_email": "\"Igor R. Dejanovi\u0107\" <igor.dejanovic@gmail.com>, Pierre Bayerl <noemail@nowhere.com>",
    "download_url": "https://files.pythonhosted.org/packages/a3/cf/34d062a549170094e7f0f71d118af762aa7795257ec4eb906109c5888c1f/textx-4.0.1.tar.gz",
    "platform": null,
    "description": "![](https://raw.githubusercontent.com/textX/textX/master/art/textX-logo.png)\n\n[![PyPI Version](https://img.shields.io/pypi/v/textX.svg)](https://pypi.python.org/pypi/textX)\n![](https://img.shields.io/pypi/l/textX.svg)\n[![Build status](https://github.com/textx/textx/actions/workflows/ci-linux-ubuntu.yml/badge.svg)](https://github.com/textx/textx/actions)\n[![Code test coverage](https://coveralls.io/repos/github/textX/textX/badge.svg?branch=master)](https://coveralls.io/github/textX/textX?branch=master)\n[![Documentation Status](https://img.shields.io/badge/docs-latest-green.svg)](http://textx.github.io/textX/latest/)\n\n\ntextX is a meta-language for building Domain-Specific Languages (DSLs) in\nPython. It is inspired by [Xtext].\n\nIn a nutshell, textX will help you build your textual language in an easy way.\nYou can invent your own language or build a support for already existing textual\nlanguage or file format.\n\nFrom a single language description (grammar), textX will build a parser and a\nmeta-model (a.k.a. abstract syntax) for the language. See the docs for the\ndetails.\n\ntextX follows the syntax and semantics of Xtext but [differs in some\nplaces](http://textx.github.io/textX/latest/about/comparison/) and is\nimplemented 100% in Python using [Arpeggio] PEG parser - no grammar ambiguities,\nunlimited lookahead, interpreter style of work.\n\n\n## Quick intro\n\nHere is a complete example that shows the definition of a simple DSL for\ndrawing. We also show how to define a custom class, interpret models and search\nfor instances of a particular type.\n\n```python\nfrom textx import metamodel_from_str, get_children_of_type\n\ngrammar = \"\"\"\nModel: commands*=DrawCommand;\nDrawCommand: MoveCommand | ShapeCommand;\nShapeCommand: LineTo | Circle;\nMoveCommand: MoveTo | MoveBy;\nMoveTo: 'move' 'to' position=Point;\nMoveBy: 'move' 'by' vector=Point;\nCircle: 'circle' radius=INT;\nLineTo: 'line' 'to' point=Point;\nPoint: x=INT ',' y=INT;\n\"\"\"\n\n# We will provide our class for Point.\n# Classes for other rules will be dynamically generated.\nclass Point:\n    def __init__(self, parent, x, y):\n        self.parent = parent\n        self.x = x\n        self.y = y\n\n    def __str__(self):\n        return \"{},{}\".format(self.x, self.y)\n\n    def __add__(self, other):\n        return Point(self.parent, self.x + other.x, self.y + other.y)\n\n# Create meta-model from the grammar. Provide `Point` class to be used for\n# the rule `Point` from the grammar.\nmm = metamodel_from_str(grammar, classes=[Point])\n\nmodel_str = \"\"\"\n    move to 5, 10\n    line to 10, 10\n    line to 20, 20\n    move by 5, -7\n    circle 10\n    line to 10, 10\n\"\"\"\n\n# Meta-model knows how to parse and instantiate models.\nmodel = mm.model_from_str(model_str)\n\n# At this point model is a plain Python object graph with instances of\n# dynamically created classes and attributes following the grammar.\n\ndef cname(o):\n    return o.__class__.__name__\n\n# Let's interpret the model\nposition = Point(None, 0, 0)\nfor command in model.commands:\n    if cname(command) == 'MoveTo':\n        print('Moving to position', command.position)\n        position = command.position\n    elif cname(command) == 'MoveBy':\n        position = position + command.vector\n        print('Moving by', command.vector, 'to a new position', position)\n    elif cname(command) == 'Circle':\n        print('Drawing circle at', position, 'with radius', command.radius)\n    else:\n        print('Drawing line from', position, 'to', command.point)\n        position = command.point\nprint('End position is', position)\n\n# Output:\n# Moving to position 5,10\n# Drawing line from 5,10 to 10,10\n# Drawing line from 10,10 to 20,20\n# Moving by 5,-7 to a new position 25,13\n# Drawing circle at 25,13 with radius 10\n# Drawing line from 25,13 to 10,10\n\n# Collect all points starting from the root of the model\npoints = get_children_of_type(\"Point\", model)\nfor point in points:\n    print('Point: {}'.format(point))\n\n# Output:\n# Point: 5,10\n# Point: 10,10\n# Point: 20,20\n# Point: 5,-7\n# Point: 10,10\n```\n\n\n## Video tutorials\n\n\n### Introduction to textX\n\n\n[![Introduction to\ntextX](https://img.youtube.com/vi/CN2IVtInapo/0.jpg)](https://www.youtube.com/watch?v=CN2IVtInapo)\n\n\n### Implementing Martin Fowler's State Machine DSL in textX\n\n[![Implementing State Machine\nDSL](https://img.youtube.com/vi/HI14jk0JIR0/0.jpg)](https://www.youtube.com/watch?v=HI14jk0JIR0)\n\n\n## Docs and tutorials\n\nThe full documentation with tutorials is available at\nhttp://textx.github.io/textX/stable/\n\n\n# Support in IDE/editors\n\nProjects that are currently in progress are:\n\n- [textX-LS](https://github.com/textX/textX-LS) - support for Language Server\n  Protocol and VS Code for any textX based language. This project is about to\n  supersede the following projects:\n  - [textX-languageserver](https://github.com/textX/textX-languageserver) -\n    Language Server Protocol support for textX languages\n  - [textX-extensions](https://github.com/textX/textX-extensions) - syntax\n    highlighting, code outline\n- [viewX](https://github.com/danielkupco/viewX-vscode) - creating visualizers\n  for textX languages\n  \nIf you are a vim editor user check\nout [support for vim](https://github.com/textX/textx.vim/).\n\nFor emacs there is [textx-mode](https://github.com/textX/textx-mode) which is\nalso available in [MELPA](https://melpa.org/#/textx-mode).\n\nYou can also check\nout [textX-ninja project](https://github.com/textX/textX-ninja). It is\ncurrently unmaintained.\n\n\n## Discussion and help\n\nFor general questions, suggestions, and feature requests please use [GitHub\nDiscussions](https://github.com/textX/textX/discussions).\n\n\nFor issues please use [GitHub issue\ntracker](https://github.com/textX/textX/issues).\n\n\n## Citing textX\n\nIf you are using textX in your research project we would be very grateful if you\ncite our paper:\n\nDejanovi\u0107 I., Vaderna R., Milosavljevi\u0107 G., Vukovi\u0107 \u017d. (2017). [TextX: A Python\ntool for Domain-Specific Languages\nimplementation](https://www.doi.org/10.1016/j.knosys.2016.10.023).\nKnowledge-Based Systems, 115, 1-4.\n\n\n## License\n\nMIT\n\n## Python versions\n\nTested for 3.8+\n\n\n[Arpeggio]: https://github.com/textX/Arpeggio\n[Xtext]: http://www.eclipse.org/Xtext/\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Meta-language for DSL implementation inspired by Xtext",
    "version": "4.0.1",
    "project_urls": {
        "Changelog": "https://github.com/textX/textX/blob/master/CHANGELOG.md",
        "Homepage": "https://textx.github.io/textX/",
        "Repository": "https://github.com/textX/textX/"
    },
    "split_keywords": [
        "parser",
        "meta-language",
        "meta-model",
        "language",
        "dsl"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "f66b50122e4780cdac3f93c63f87d036706f19e040d5c0a394131b87ef5ed91c",
                "md5": "fccc7b2d2a8007a065179260ad632f97",
                "sha256": "023bdb338ec4d6f8ad4c8b58e8bc16975920a5bd379428d8b027c1a7d02a42b4"
            },
            "downloads": -1,
            "filename": "textx-4.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "fccc7b2d2a8007a065179260ad632f97",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8, <3.13",
            "size": 66809,
            "upload_time": "2023-11-12T11:55:35",
            "upload_time_iso_8601": "2023-11-12T11:55:35.562379Z",
            "url": "https://files.pythonhosted.org/packages/f6/6b/50122e4780cdac3f93c63f87d036706f19e040d5c0a394131b87ef5ed91c/textx-4.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "a3cf34d062a549170094e7f0f71d118af762aa7795257ec4eb906109c5888c1f",
                "md5": "d2120c731049ee4607ba41a648278868",
                "sha256": "84aff5c95fd2c947402fcbe83eeeddc23aabcfed3464ab84184ef193c52d831a"
            },
            "downloads": -1,
            "filename": "textx-4.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "d2120c731049ee4607ba41a648278868",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8, <3.13",
            "size": 1820595,
            "upload_time": "2023-11-12T11:55:39",
            "upload_time_iso_8601": "2023-11-12T11:55:39.587007Z",
            "url": "https://files.pythonhosted.org/packages/a3/cf/34d062a549170094e7f0f71d118af762aa7795257ec4eb906109c5888c1f/textx-4.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-12 11:55:39",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "textX",
    "github_project": "textX",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "textx"
}
        
Elapsed time: 0.14343s