textX

4.3.0 · active · verified Fri Apr 17

textX is a meta-language for building Domain-Specific Languages (DSLs) in Python, inspired by Xtext. It allows you to define a grammar using a simple, intuitive syntax and then generates a parser and an object model for your DSL. The current version is 4.3.0, and it has a steady release cadence, primarily focusing on maintenance and Python version compatibility.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a simple grammar as a string, create a metamodel from it, and then parse an input string to build a model. It also shows basic error handling for invalid input.

from textx import metamodel_from_str

grammar = """
Model: 'Hello' name=ID;
ID: /[_a-zA-Z][a-zA-Z0-9_]*/;
"""

# Create a metamodel from the grammar string
metamodel = metamodel_from_str(grammar)

# Parse a model from an input string
input_str = "Hello World"
model = metamodel.model_from_str(input_str)

# Access the parsed model data
print(f"Parsed name: {model.name}")

# Example with an error
try:
    metamodel.model_from_str("Hi World")
except Exception as e:
    print(f"Error parsing 'Hi World': {e}")

view raw JSON →