Coder Social home page Coder Social logo

asuivelentine / textx Goto Github PK

View Code? Open in Web Editor NEW

This project forked from textx/textx

0.0 1.0 0.0 9.05 MB

Domain-Specific Languages and parsers in Python made easy http://textx.github.io/textX/

License: MIT License

Python 99.49% AMPL 0.02% Limbo 0.16% Brainfuck 0.02% C 0.02% Shell 0.28%

textx's Introduction

image

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 import metamodel_from_str, get_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 = get_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

image

Implementing Martin Fowler's State Machine DSL in textX

image

Docs and tutorials

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

Support in IDE/editors

Discussion and help

For general questions and help please use StackOverflow. Just make sure to tag your question with the textx tag.

For issues, suggestions and feature request please use GitHub issue tracker.

License

MIT

Python versions

Tested for 2.7, 3.4+

textx's People

Contributors

alensuljkanovic avatar aluriak avatar borismarin avatar danixeee avatar dkrikun avatar goto40 avatar igordejanovic avatar renatav avatar simkimsia avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.