parsec-html-css 0.1.0

CSS Syntax Level 3 for Parsec: tokenizer, parser, An+B and serialization, tested against css-parsing-tests.
Documentation
#!/usr/bin/env node
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 James Conway
//
// Writes src/values/generated.rs from data/webref/css.json, the CSS
// definitions w3c/webref scrapes from the specs (MIT; see data/webref):
// every property's grammar, initial value, inheritance and longhands, and
// the grammars of the types and functions they refer to. Run from
// crates/parsec-css after updating the JSON:
//
//   node scripts/properties.js && cargo fmt
'use strict';

const fs = require('fs');
const path = require('path');

const root = path.join(__dirname, '..');
const css = JSON.parse(fs.readFileSync(path.join(root, 'data/webref/css.json'), 'utf8'));
const commit = fs.readFileSync(path.join(root, 'data/webref/COMMIT'), 'utf8').trim();

const str = (s) => JSON.stringify(s);
const opt = (s) => (s === undefined ? 'None' : `Some(${str(s)})`);

// Where webref's grammar is behind its spec, or a spec's prose restricts
// what the grammar says, the grammar as it should read. Each with where it
// comes from.
const PROPERTY_PATCHES = {
  // CSS Overflow 4 §5.1 refers to the property; webref has a type that
  // isn't defined.
  'line-clamp': { syntax: "none | [ <integer [1,∞]> || auto || <'block-ellipsis'> ] -webkit-legacy?" },
  // CSS Multi-column 2 §4.1; webref merges it with CSS Sizing 3's
  // mention and loses the rest.
  'column-width': {
    syntax: 'auto | <length [0,∞]>',
    initial: 'auto',
    inherited: 'no',
    href: 'https://drafts.csswg.org/css-multicol-2/#cw',
  },
};
const TYPE_PATCHES = {
  // Scroll Animations 1 §3.1 lists the range names in prose.
  'timeline-range-name': 'cover | contain | entry | exit | entry-crossing | exit-crossing | scroll',
  // CSS UI 4 §5.1.1: "a limited version of image-set()".
  'url-set': '<image-set()>',
  // CSS Color 5 §7: light-dark() takes two images where an image goes.
  'image': '<url> | <image()> | <image-set()> | <cross-fade()> | <element()> | <gradient> | <light-dark-image>',
  // CSS Transitions 1 §2.2: a duration can't be negative, and it's the
  // first time in a transition.
  'single-transition': '[ none | <single-transition-property> ] || <time [0s,∞]> || <easing-function> || <time> || <transition-behavior-value>',
};

const props = [...css.properties].sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
const lines = [];
lines.push('// SPDX-License-Identifier: Apache-2.0');
lines.push('// Copyright 2026 James Conway');
lines.push('');
lines.push(`// Generated by scripts/properties.js from data/webref/css.json (${commit}).`);
lines.push('// Don\'t edit by hand.');
lines.push('');
lines.push('use super::properties::PropertyData;');
lines.push('');
lines.push('pub(crate) static PROPERTIES: &[PropertyData] = &[');
for (const raw of props) {
  const p = { ...raw, ...(PROPERTY_PATCHES[raw.name] || {}) };
  const inherited = p.inherited === 'yes' ? 'Some(true)' : p.inherited === 'no' ? 'Some(false)' : 'None';
  lines.push('    PropertyData {');
  lines.push(`        name: ${str(p.name)},`);
  lines.push(`        syntax: ${opt(p.syntax)},`);
  lines.push(`        initial: ${opt(p.initial)},`);
  lines.push(`        inherited: ${inherited},`);
  lines.push(`        longhands: &[${(p.longhands || []).map(str).join(', ')}],`);
  lines.push(`        alias_of: ${opt(p.legacyAliasOf)},`);
  lines.push(`        href: ${str(p.href)},`);
  lines.push('    },');
}
lines.push('];');
lines.push('');

// Where a spec's prose restricts a grammar further than the grammar says,
// and webref carries only the grammar, the restriction written in: Filter
// Effects 1 §11, "negative values are not allowed" for each of these.
const PATCHES = {
  'blur': 'blur( <length [0,∞]>? )',
  'brightness': 'brightness( [ <number [0,∞]> | <percentage [0,∞]> ]? )',
  'contrast': 'contrast( [ <number [0,∞]> | <percentage [0,∞]> ]? )',
  'grayscale': 'grayscale( [ <number [0,∞]> | <percentage [0,∞]> ]? )',
  'invert': 'invert( [ <number [0,∞]> | <percentage [0,∞]> ]? )',
  'opacity': 'opacity( [ <number [0,∞]> | <percentage [0,∞]> ]? )',
  'saturate': 'saturate( [ <number [0,∞]> | <percentage [0,∞]> ]? )',
  'sepia': 'sepia( [ <number [0,∞]> | <percentage [0,∞]> ]? )',
  'drop-shadow': 'drop-shadow( [ <color>? && [ <length>{2} <length [0,∞]>? ] ] )',
  // CSS Shapes 1 §3.1: a circle's radius is one value, "two
  // <length-percentage> values are invalid"; percentages resolve against
  // the reference box, and an ellipse's radii may each be either.
  'circle': 'circle( [ <radial-extent> | <length-percentage [0,∞]> ]? [ at <position> ]? )',
  'ellipse': 'ellipse( [ <radial-extent> | <length-percentage [0,∞]> ]{0,2} [ at <position> ]? )',
  // A corner's rounding is a radius, which can't be negative.
  'polygon': "polygon( <'fill-rule'>? [ round <length [0,∞]> ]? , [<length-percentage> <length-percentage>]# )",
};

// Types and functions by name, sorted for binary search. Where a name is
// defined more than once for different contexts (`for`), the first
// definition without one wins.
function table(name, items, key) {
  const seen = new Map();
  for (const t of items) {
    if (!t.syntax && !(name === 'TYPES' && TYPE_PATCHES[key(t)])) continue;
    const k = key(t);
    if (!seen.has(k) || (seen.get(k).for && !t.for)) seen.set(k, t);
  }
  const keys = [...seen.keys()].sort();
  lines.push(`pub(crate) static ${name}: &[(&str, &str)] = &[`);
  for (const k of keys) {
    const syntax = (name === 'FUNCTIONS' && PATCHES[k]) || (name === 'TYPES' && TYPE_PATCHES[k]) || seen.get(k).syntax;
    lines.push(`    (${str(k)}, ${str(syntax)}),`);
  }
  lines.push('];');
  lines.push('');
}
table('TYPES', css.types, (t) => t.name);
table('FUNCTIONS', css.functions, (t) => t.name.replace(/\(\)$/, ''));

fs.writeFileSync(path.join(root, 'src/values/generated.rs'), lines.join('\n'));
console.error(`${props.length} properties`);