Skip to content

v11 Pratt parser#240

Draft
dkryaklin wants to merge 7 commits into
postcss:masterfrom
dkryaklin:v11/spec-first-playground
Draft

v11 Pratt parser#240
dkryaklin wants to merge 7 commits into
postcss:masterfrom
dkryaklin:v11/spec-first-playground

Conversation

@dkryaklin
Copy link
Copy Markdown

@dkryaklin dkryaklin commented Apr 22, 2026

What

A decoupled prototype of new calc() internals under playground/pratt/

Why

v10 uses a JISON-generated LR parser and an ad-hoc reducer. It doesn't cover min() / max() / clamp(), typed division (calc(100vw / 1px)), or calc-keyword folding (pi, e, infinity). CSS Values & Units 4 §10 gives a deterministic simplification algorithm — implementing it directly makes correctness reviewable section-by-section rather than invented-and-patched.

How

Five TS modules, each mapped one-to-one to a spec section:

  • tokenizer.ts — §10.1 tokens
  • parser.ts — §10.1 grammar (Pratt + parselet registry)
  • type.ts — §10.2 calculation types
  • simplify.ts — §10.10 simplification
  • serialize.ts — §10.12 output

169 tests (parser / tokenizer / simplify / typed / opaque / serialize / WPT crib), run via pnpm test:pratt. tsx runtime, no build step.

See ROADMAP.md for more details

@dkryaklin dkryaklin changed the title v11 spec-first playground: Pratt parser + §10.10 simplifier v11 Pratt parser Apr 24, 2026
Copy link
Copy Markdown
Collaborator

@ludofischer ludofischer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very interesting! How much is AI generated? I am asking because I had thought of trying to use AI to generate a "hand-written" parser, but never tried it.

Copy link
Copy Markdown
Collaborator

@ludofischer ludofischer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good that you are comparing against the csstools output, as I suppose they checked they're fairly spec-compliant. Are you also testing against the current postcss-calc test suite? There's a lot of code here. when do you think it's going to stabilize so I can look at it more thoroughly?

Comment thread scripts/benchmark-v10.ts

async function main(): Promise<void> {
// Dynamic imports so CJS/ESM interop works under tsx.
const v10Plugin = (await import('../src/index.js')).default as () => AcceptedPlugin;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the naming (version 10? version 3?) strange, I don't think we need dynamic import either, we can drop Node 20 compatibility if we release a major.

Comment thread package.json
"fast-check": "^4.7.0",
"jison-gho": "0.6.1-216",
"postcss": "^8.5.10",
"prettier": "^3.8.3",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can update TypeScript to the latest version. cssnano is also using it. You could even try TypeScript go.

Comment thread tsconfig.pratt.json
"target": "es2022",
"lib": ["es2022"],
"module": "esnext",
"moduleResolution": "bundler",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are there two tsconfig files?

@dkryaklin dkryaklin force-pushed the v11/spec-first-playground branch from e362c9e to 16c1db0 Compare May 5, 2026 11:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants