Skip to main content

Meta-language for DSL implementation inspired by Xtext

Project description

https://raw.githubusercontent.com/igordejanovic/textX/master/art/textX-logo.png

PyPI Version license build-status Documentation Status

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 and is implemented 100% in Python using Arpeggio PEG parser - no grammar ambiguities, unlimited lookahead, interpreter style of work.

Quick intro

from textx.metamodel import metamodel_from_str
from textx.model import children_of_type

grammar = """
Model: shapes*=Shape;
Shape: Circle | Line;
Circle: 'circle' center=Point '/' radius=INT;
Line: 'line' start=Point '/' end=Point;
Point: x=INT ',' y=INT;
"""

mm = metamodel_from_str(grammar)

# Meta-model knows how to parse and instantiate models.
model = mm.model_from_str("""
    line 10, 10 / 20, 20
    line 14, 78 / 89, 33
    circle 14, 20/10
    line 18, 89 / 78, 65
""")

# At this point model is plain Python object graph with instances of
# dynamically created classes and attributes following the grammar.

def _(p):
    "returns coordinate of the given Point as string"
    return "{},{}".format(p.x, p.y)

for shape in model.shapes:
    if shape.__class__.__name__ == 'Circle':
        print('Circle: center={}, radius={}'
              .format(_(shape.center), shape.radius))
    else:
        print('Line: from={} to={}'.format(_(shape.start), _(shape.end)))

# Output:
# Line: from=10,10 to=20,20
# Line: from=14,78 to=89,33
# Circle: center=14,20, radius=10
# Line: from=18,89 to=78,65

# Collect all points starting from the root of the model
points = children_of_type("Point", model)
for point in points:
    print('Point: {}'.format(_(point)))

# Output:
# Point: 10,10
# Point: 20,20
# Point: 14,78
# Point: 89,33
# Point: 14,20
# Point: 18,89
# Point: 78,65

Video tutorials

Introduction to textX

https://img.youtube.com/vi/CN2IVtInapo/0.jpg

Implementing Martin Fowler’s State Machine DSL in textX

https://img.youtube.com/vi/HI14jk0JIR0/0.jpg

Docs and tutorials

The full documentation with tutorials is available at http://igordejanovic.net/textX/

License

MIT

Python versions

Tested for 2.7, 3.3+

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

textX-1.6.1.tar.gz (33.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

textX-1.6.1-py2.py3-none-any.whl (38.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file textX-1.6.1.tar.gz.

File metadata

  • Download URL: textX-1.6.1.tar.gz
  • Upload date:
  • Size: 33.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for textX-1.6.1.tar.gz
Algorithm Hash digest
SHA256 2bfce94674dd85162f03afd01e4819757de2fa500ade18d74704e6f900a8e349
MD5 4856ab1e0280a55d5ca93cf7055864bf
BLAKE2b-256 b071acc287944fae9db269ed8134cc3c00796e17c1310bbfdb942c835e0ccce1

See more details on using hashes here.

File details

Details for the file textX-1.6.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for textX-1.6.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3b8795a757e76f5fc8327aa99eabc49b033ce9e8d09689610bb102404b530663
MD5 97e1adc3839d1aef1adcf2eeb18986f8
BLAKE2b-256 e72355c11641626c41ac88b495c2f1828c149dbc5560b108aa274f6bddd03fe6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page