Skip to content

Stringent v2: arktype-based type system and grammar compilation - #5

Closed
eralmansouri wants to merge 1 commit into
mainfrom
claude/formeddable-stringent-review-suiwqu
Closed

eralmansouri wants to merge 1 commit into
mainfrom
claude/formeddable-stringent-review-suiwqu

Conversation

@eralmansouri

Copy link
Copy Markdown
Owner

Summary

This PR implements Stringent v2, a major redesign that replaces the custom type vocabulary system with arktype as the runtime type engine, adds compile-time grammar validation, and derives associativity from pattern shape rather than explicit configuration.

Key Changes

Type System (arktype integration)

  • Constraints and result types are now arktype definitions instead of string type names. Binding names automatically become type aliases in the parser's scope.
  • Assignability-based matching: constraint validation uses arktype's extends operator (directional subtype check) and a new overlaps() function for symmetric equality-style operators.
  • Removed marker objects: sameAs() and fromBinding() are replaced with plain string references to earlier bindings in the pattern.
  • Schema validation: schemas are now arktype object defs, validated at compile time via type.validate() and at runtime by compiling the def. Identifier/path resolution uses arktype's Type.get() instead of hand-rolled traversal.
  • Scope support: createParser() accepts a scope option to extend the type vocabulary with user-defined aliases, making arktype's keyword library (string.email, number.integer, etc.) available in constraints.

Grammar Compilation (src/runtime/compile.ts)

  • New compile phase that validates node schemas at construction time and precompiles:
    • Each expr slot's constraint (classified as unconstrained, binding reference, or static arktype def)
    • Each node's resultType (passthrough, binding reference, or static def)
    • Precedence levels sorted ascending with parse mode derived from tail shape
  • Associativity is derived from pattern shape: tail at current level (rhs()) → right-associative; tail at tighter level (lhs()) or closed pattern → left-associative fold.
  • Construction errors throw immediately — typos in constraint defs or ambiguous levels are caught at parser creation, not silently ignored.

Parser Runtime (src/runtime/parser.ts)

  • Output types carried on symbol key (OUTPUT_TYPE): parsed subexpressions carry their arktype Type non-enumerably so constraint checks compose without polluting the serializable AST.
  • Unified ParseEnv: parser functions now receive a single ParseEnv object containing the compiled grammar, schema Type, and diagnostics, replacing scattered parameters.
  • Binding-reference resolution: constraints and result types that name earlier bindings resolve to those operands' parsed types at runtime.

Type-Level Engine (src/parse/index.ts)

  • Mirrored v2 semantics: associativity derives from tail shape; constraints are arktype defs with assignability matching; binding references resolve to operand types.
  • Refinement erasure: type.infer<"number > 0"> automatically reduces to number for constraint matching.
  • Scope caveat documented: defs resolve in arktype's default scope at the type level, so grammars using custom scope aliases lose literal-mode checking for those defs (use safeParse or cast).

Schema & Node Definition

  • Removed precedence: "atom" sentinel: precedence is now a plain number; the highest precedence level is the leaf level.
  • Removed associativity property: derived from pattern tail shape instead.
  • New overlapping() function: symmetric constraint for equality operators (checks type overlap, not subtype).
  • Built-in literals expanded: true, false, null, undefined added alongside number/string.
  • String escape handling: runtime tokenizer now handles \n \t \r \\ \" \' \0 \b \f \v \xHH \uHHHH escapes.

API Changes

  • createParser() no longer exposes typeNames property; type vocabulary is now implicit in the scope.
  • InferValues<TSchema> now delegates to type.infer<TSchema> instead of custom recursion.
  • Constraint validation errors now reference binding names and arktype expressions instead of type-name strings.

Testing & Documentation

  • Type-level tests (src/types.typetest.ts)

https://claude.ai/code/session_017GQrzid8mJPWM4HMb6Cf6C

eralmansouri pushed a commit that referenced this pull request Jul 7, 2026
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GQrzid8mJPWM4HMb6Cf6C
@eralmansouri
eralmansouri changed the base branch from main to claude/repo-onboarding-9o7s19 July 7, 2026 15:58
@eralmansouri
eralmansouri changed the base branch from claude/repo-onboarding-9o7s19 to main July 7, 2026 15:58
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