From 7c6dbe017f03b99776f083fd99cad1f707ddca2f Mon Sep 17 00:00:00 2001 From: Matt Kantor Date: Thu, 2 Jan 2025 10:21:48 -0500 Subject: [PATCH] Use plz notation in error messages --- src/language/semantics/key-path.ts | 12 +++++++++++- src/language/semantics/semantic-graph.ts | 14 +++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/language/semantics/key-path.ts b/src/language/semantics/key-path.ts index 104b455..3ed7e33 100644 --- a/src/language/semantics/key-path.ts +++ b/src/language/semantics/key-path.ts @@ -1,4 +1,7 @@ +import { either } from '../../adts.js' import type { Atom, Molecule } from '../parsing.js' +import { unparse } from '../unparsing.js' +import { prettyPlz } from '../unparsing/pretty-plz.js' export const functionParameter = Symbol('functionParameter') export const functionReturn = Symbol('functionReturn') @@ -15,7 +18,14 @@ export type KeyPath = readonly ( )[] export const stringifyKeyPathForEndUser = (keyPath: KeyPath): string => - JSON.stringify(keyPath) + either.match( + // TODO: Use single-line plz notation. + unparse(keyPathToMolecule(keyPath), prettyPlz), + { + right: stringifiedOutput => stringifiedOutput, + left: error => `(unserializable key path: ${error.message})`, + }, + ) export const keyPathToMolecule = (keyPath: KeyPath): Molecule => Object.fromEntries( diff --git a/src/language/semantics/semantic-graph.ts b/src/language/semantics/semantic-graph.ts index 63776f0..665fccb 100644 --- a/src/language/semantics/semantic-graph.ts +++ b/src/language/semantics/semantic-graph.ts @@ -6,6 +6,8 @@ import type { } from '../errors.js' import type { Atom, Molecule } from '../parsing.js' import type { Canonicalized } from '../parsing/syntax-tree.js' +import { unparse } from '../unparsing.js' +import { prettyPlz } from '../unparsing/pretty-plz.js' import { serializeFunctionNode, type FunctionNode } from './function-node.js' import { stringifyKeyPathForEndUser, type KeyPath } from './key-path.js' import { @@ -174,7 +176,17 @@ export const serialize = ( export const stringifySemanticGraphForEndUser = ( graph: SemanticGraph, -): string => JSON.stringify(serialize(graph)) +): string => + either.match( + either.flatMap(serialize(graph), output => + // TODO: Use single-line plz notation. + unparse(output, prettyPlz), + ), + { + right: stringifiedOutput => stringifiedOutput, + left: error => `(unserializable value: ${error.message})`, + }, + ) export const isSemanticGraph = ( value: