greenwood

Hex.pmHex DocsApache 2.0JavaScript CompatibleErlang Compatible

A generic trivia-preserving concrete syntax tree (CST) library for Gleam.

Overview

Greenwood provides an immutable concrete syntax tree parameterized by node and token kind, with associated trivia and structural transformation primitives. Greenwood syntax trees are format-agnostic and parsers supply their own kind types.

Installation

gleam add greenwood@1

Further documentation can be found at https://greenwood.hexdocs.pm.

A Deeper Dive

Function Groups

There are six types of functions in the Greenwood interface:

Each function and type is marked with its interface group in the documentation.

Trivia

In syntax tree terminology, "trivia" refers to source text that has no semantic meaning to the language but matters to humans: whitespace, comments, blank lines, and sometimes preprocessor directives. A pure AST discards trivia entirely, but a CST must track it.

Greenwood implements a hybrid concrete syntax tree supporting Roslyn-style Trivia annotations (attached trivia) or Rowan-style green tree child tokens (inline trivia), depending on the choices made by the parser.

Cursor (Zipper)

The cursor API implements a Huet zipper (Gérard Huet, "The Zipper", Journal of Functional Programming 7(5):549–554, 1997). It provides a focused view into the tree — you can navigate down into children, edit the focused node, and reconstruct the full tree by moving back up. This avoids rebuilding the entire tree from the root for localized edits.

Core Types

Libraries Using Greenwood

COMING SOON

Roadmap

Future capabilities may include tree diffing (compare two trees and produce a minimal edit script).

Semantic Versioning

greenwood follows Semantic Versioning 2.0.