Skip to content

Repository files navigation

textX Playground

This project represents a playground for textX language. Users can define a new language by specifying its grammar using textX. They can also create a program that should follow the grammar of the defined language.

Some parts of the code are taken from the following projects:

The project is deployed and you can create and validate your new language using textX at textx.github.io/textx-playground.

Functionalities

This is a web application with only one page, containing a header and two code editors.

One editor is used for specifying language grammar and the second for defining a model (program) using newly defined language. Each editor contains the status bar, which has one of the following states:

  • loading - until textX server is started
  • error - shows error in grammar/model with message and position (row, column)
  • valid - grammar/model is valid

The header contains textX logo, toggler between light and dark theme, and links to documentation and GitHub repository.

Grammar and model content can be shared via link by clicking the share icon in the top right corner. Their content is shortened using LZ compression and embedded into the URL link as a query parameter. When this link is opened, the editors' content is restored from the query parameter.

What the editors show when the playground opens (src/utils/playgroundContent.ts):

  1. a share link: ?grammar=…&model=…
  2. a named example: ?example=<slug>, where the slug is the example name in lowercase with dashes, e.g. ?example=robot, ?example=state-machine, ?example=hello-world. Unknown names are ignored.
  3. your last work, which is saved in the browser (localStorage) while you type
  4. the Robot example

Opening a share link or ?example= does not overwrite your saved work. Saving starts with the first change, and at that point the query string is removed from the address bar so that a reload keeps your edits. Examples → Start from scratch empties both editors; two empty editors are not restored, so the next visit starts from the Robot example again.

Picking another example (or Start from scratch) asks for confirmation only when the editors hold your own changes, so untouched examples can be browsed freely (src/utils/pristineContent.ts). Editing something and changing it back counts as untouched. Restored work keeps track of whether it had been edited, so an example you only looked at does not ask after a reload either. If you arrive through a share link or ?example= while edited work from an earlier visit is saved, browsing examples leaves that work alone until you edit something.

The book icon in the header (Cheatsheet) opens a grammar cheatsheet next to the editors: an annotated example rule, then the grammar syntax in short sections, each linking to its part of the grammar docs, with a search box. Its content lives in src/utils/cheatsheet.ts. The playground remembers whether the panel was open.

Basic syntax highlighting is implemented. Grammar and model keywords and comments are highlighted.

Code suggestions appear while typing, or explicitly with Ctrl+Space. The grammar editor suggests textX syntax, built-in types, and locally declared rule names. The model editor suggests literal tokens from the current valid grammar. Suggestions prefer the parser's expected syntax at the cursor; when an earlier error or a permissive regex prevents this, items marked Grammar vocabulary provide general suggestions filtered by the typed prefix. Accepting a word replaces its current token.

An invalid or empty grammar temporarily disables model suggestions. Completion does not resolve model-object references, imported files, or arbitrary regex values, and does not provide snippets. Parser probing is best-effort rather than full error recovery. Automatic suggestions are disabled inside tokenized comments and strings; Ctrl+Space can still request them explicitly.

Development

After cloning the repo and installing dependencies with npm i command, the project is started by running:

npm run dev -- --port 3000

The project is built with:

npm run build

Completion regression tests use the same textX and pygls versions as the worker:

python -m pip install -r tests/requirements.txt
python -m unittest discover -s tests -v

Deployment

The app is deployed to two places from main:

  • GitHub Pages (.github/workflows/deploy.yml), served under /textx-playground/. This is the default base.
  • Cloudflare at https://play.textx.org (.github/workflows/cloudflare.yml), built with VITE_BASE=/ and deployed with wrangler deploy (config in wrangler.jsonc). The workflow needs the CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID repository secrets; pull requests only build.

To build for the Cloudflare base locally:

VITE_BASE=/ npm run build

public/_headers (used by Cloudflare only) sets long-lived caching for hashed assets and a temporary X-Robots-Tag: noindex that should be removed at public launch.

Releases

Packages

Used by

Contributors

Languages