Skip to content

Unify parsing: shared infrastructure, trees with spans, evaluation kept separate #300

Done

Description

@leftibot

Unify parsing: shared infrastructure, trees with spans, evaluation kept separate

The validator reads a dozen languages: CMake, Meson, Make, shell, Dockerfile, GitHub Actions expressions, GitLab CI, Cabal, tox, JSON/TOML/XML, and tool settings. Each reader was written on its own as the need arose, so each solves the same problems its own way. Some build a real syntax tree; others flatten the file, or evaluate while they parse. This issue is about giving parsing one consistent design, so readers reuse work, behave the same at their limits, report positions the same way, and are testable layer by layer.

Where things stand

Reader Produces Structure
Meson (meson/syntax.py) Literal, Name, Array, Dictionary, Call, Index, Operation, Assignment, If; evaluation is a separate pass full tree (tokens have lines; expression nodes mostly don't)
CMake (cmake/syntax.py) Invocation(name, arguments, line) and Block branches; arguments keep their kind command-level tree; arguments stay strings
Cabal (haskell/cabal.py) Section(kind, name, line, fields, sections) tree
Make (make.py) Makefile(rules, variables, includes), built by regex line by line, applying +=/:= while reading; conditionals not represented semantic model; loses information
Dockerfile (docker/dockerfile.py) flat Instruction(line, keyword, arguments) flat
Shell (facts/shell.py) flat ShellCommand(argv, env, line); &&/||, pipelines, subshells, if/for headers and substitutions are split apart or dropped none, and every CI step goes through it
GitHub Actions expressions, CMake if() conditions, generator expressions parsers that evaluate as they go, straight to a value or Condition none
JSON/TOML/XML (text.check_json, ecosystems/data.py) well-formed or not, with an error position none (by design)

Duplication and inconsistency, measured

  • Five syntax-error types: JsonSyntaxError, CMakeSyntaxError, MesonSyntaxError, CabalSyntaxError, and facts.wellformed.ParseError. They carry different fields and are turned into Unknown/unreadable facts in different ways.

  • Separate tokenizers in CMake, Meson, GitHub expressions and JSON. Four line-continuation implementations (Make, shell, Dockerfile, facts/tools.py), each with its own comment and escape handling.

  • Nesting limits that disagree (ci/gitlab.py, cmake/evaluation.py, ci/github_actions.py, javascript/analysis.py, ecosystems/data.py), each behaving differently at the limit. The mutation run shows none of them is tested at the boundary:

    Limit Value
    GitLab _DEPTH 10
    CMake MOST_NESTED 24
    GitHub reusable-workflow _CALL_DEPTH 4
    tsconfig extends hard-coded depth < 4
    data files _DEEP
  • 18 modules call shlex directly (action inputs, Dart, Clojure aliases, scanners, audits, CMake and Meson re-joining, …). They bypass facts/shell, so they handle quoting, substitutions and comments differently from every CI run: step.

  • At least seven places expand variable references, each with its own unknown handling: CMake ${} (cmake/values.py), Make $(), shell $VAR, tox {}, GitLab $VAR, GitHub ${{ }}, facts/expansion.

  • At least five places follow includes, each with its own cycle and depth handling:

    • CMake include/add_subdirectory;
    • Meson subdir;
    • Make include;
    • GitLab include/extends/!reference;
    • tsconfig extends;
    • GitHub reusable workflows and composite actions.
  • Positions: Origin is a path plus a structured place (jobs.test.steps[3].run), with no line or column. Readers know lines (CMake, Make, Dockerfile, Cabal) or nothing (shell, expressions). Only ParseError has a column.

  • Shared already, and working well: facts/conditions (the Condition algebra used by CMake, Meson and generator expressions), facts/wellformed (Parser, size limits, decoded) and facts/expansion (CommandExpander, Script). The design should grow from these.

Principles

  1. Parse to a tree; evaluate separately. The syntax layer is pure (text → tree with spans) and never consults facts. Evaluation (variables, conditions, includes) walks the tree. Each layer gets its own tests: golden trees for syntax, value tables for evaluation. Mutation survivors then point at one layer.
  2. One position model. A Span(line, column, end_line, end_column) on every node, and an optional span on Origin. Messages, --explain and parse errors all say path:line:column the same way.
  3. One error model. A single SyntaxError base (from facts.wellformed.ParseError) with a position and a reason, and one rule for turning it into an Unknown or unreadable fact. Recoverable errors (skip a statement, keep going) are distinguished from fatal ones.
  4. A shared lexing kit. A regex-table tokenizer that yields tokens with spans, plus shared helpers for line continuations (with a configurable escape character), comments and quoted strings with escapes. It is iterative, never recursive, for untrusted input, as check_json already does.
  5. One limits policy. A Limits value covers file size, nesting depth, include depth, expansion depth and call depth, with documented defaults. Reaching a limit always yields an Unknown that names the limit, never a silent cut-off. One shared test helper builds a chain at the limit and one past it for every limited reader.
  6. One include follower. It owns cycle detection, depth limiting, unreadable files and provenance (which file pulled in which). It is used by every reader that follows includes.
  7. One template/expansion engine. A Template node (literal parts and references) with a per-language reference syntax. Values are known, conditional (via facts/conditions) or unknown. One rule decides when an unexpanded reference makes a command opaque.
  8. The shell reader is the hub. Every embedded command line (action inputs, tox commands, Make recipes, npm scripts, Leiningen aliases, Meson/CMake re-joins) goes through facts/shell, never shlex.split directly.
  9. Pluggable backends. Because readers produce trees behind an interface, a reader's syntax layer can later be swapped, for example for a tree-sitter grammar (Future option: tree-sitter grammars as an optional parsing backend #299), without touching evaluation.

Incremental plan

Each step preserves behaviour, keeps the corpus (python -m corpus) unchanged unless it fixes a known bug, and is done test first.

  1. Span, Origin positions and a common SyntaxError base. Migrate the five error types.

  2. Limits and the boundary test helper. Move the depth constants into it and test each limit at N and N+1.

  3. Route every shlex caller through facts/shell (18 modules).

  4. A shell syntax tree:

    • nodes for commands, pipelines, lists (&&/||/;), subshells, if/for/while/case, assignments, redirections, and command and arithmetic substitutions, each with a span;
    • derive today's ShellCommand stream from it.

    This also gives the enforcement facts Score a project's tooling, not just pass/warn/fail #295 needs (|| true, set +e), and fixes the class of bugs behind A custom shell: wrapper (msys2 {0}, script -q -e -c "bash {0}") makes the whole step one argument of the wrapper program #194 and An echo X=… >> $GITHUB_ENV inside a shell if branch is taken as unconditional, and later steps with if: env.X != … are dropped silently #284.

  5. A Makefile tree: assignments, rules, conditionals (ifeq/ifdef/else/endif) and include as nodes, with expansion in an evaluation pass. Conditionals become Conditions, as in CMake.

  6. Split GitHub Actions expressions and CMake if() conditions into parse and evaluate. Both are small.

  7. The shared include follower and the Template engine. Migrate readers one at a time.

  8. Dockerfile instructions with parsed flags (--from=, --mount=) and heredocs as nodes.

  9. Revisit Future option: tree-sitter grammars as an optional parsing backend #299 per reader, once the tree interface exists.

Related

Not in scope

  • A single universal grammar or parser generator. Each language keeps a hand-written or reference parser suited to it.
  • New required dependencies.
  • Behaviour changes beyond known bugs. Each step is shown neutral by the corpus diff.

Acceptance

  • Every reader except the well-formedness-only ones produces a tree with spans, and its evaluation is a separate, separately tested pass.
  • One error type, one Limits policy (each limit tested at the boundary), one include follower, one expansion engine, and no direct shlex use outside facts/shell.
  • Messages and --explain give path:line:column wherever the source position is known.
  • A mutation run shows the reader survivors reduced, and the corpus diff is empty apart from documented fixes.

🤖 Generated with Claude Code

https://claude.ai/code/session_01ByTXNqDWdM7HzezrFtEvQt

Activity

  1. added
    enhancement
    area: architecture
    on Nov 19, 2026
  2. leftibot commented on Nov 23, 2026

    @leftibot
    CollaboratorAuthor

    Done, apart from the two items held back (below). Each step landed test first, with the corpus diff checked.

    Step PR
    1. One Span, one ParseError base #344
    2. One limits policy, each limit tested at the boundary #345
    3. Embedded command lines split in one place (no direct shlex) #346
    4. Shell scripts parse to a syntax tree #349, with #351 (no exponential slowdown or stack overflow on deep nesting)
    5. Makefiles parse to a tree; conditionals are Conditions #355, #360
    6. GitHub expressions, CMake if() and generator expressions parse to trees, evaluated separately #347, #348, #368
    7. One include follower, one template engine #357, #358
    8. Dockerfile instructions with flags, heredocs and spans #356
    Positions: origins read path:line:column (Make, CMake, Meson, Cabal, shell scripts, CI YAML, YAML settings) #362

    Related fixes along the way: #350 (Cabal braces), #354 (a YAML escape past the last character).

    By design, not unified:

    • CMake keeps its own include stack and its own reference expansion. Its limit counts calls along with files, and its references nest and hold per build.
    • Meson reads each meson.build once, from a directory tree that can't cycle.

    Not done:

    Follow-ups:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: architectureenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions