Skip to content

Commit c3736b4

Browse files
authored
Merge pull request #11 from mkantor/plz-in-errors
Use plz notation in error messages
2 parents 9c0e2e6 + 7c6dbe0 commit c3736b4

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/language/semantics/key-path.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import { either } from '../../adts.js'
12
import type { Atom, Molecule } from '../parsing.js'
3+
import { unparse } from '../unparsing.js'
4+
import { prettyPlz } from '../unparsing/pretty-plz.js'
25

36
export const functionParameter = Symbol('functionParameter')
47
export const functionReturn = Symbol('functionReturn')
@@ -15,7 +18,14 @@ export type KeyPath = readonly (
1518
)[]
1619

1720
export const stringifyKeyPathForEndUser = (keyPath: KeyPath): string =>
18-
JSON.stringify(keyPath)
21+
either.match(
22+
// TODO: Use single-line plz notation.
23+
unparse(keyPathToMolecule(keyPath), prettyPlz),
24+
{
25+
right: stringifiedOutput => stringifiedOutput,
26+
left: error => `(unserializable key path: ${error.message})`,
27+
},
28+
)
1929

2030
export const keyPathToMolecule = (keyPath: KeyPath): Molecule =>
2131
Object.fromEntries(

src/language/semantics/semantic-graph.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import type {
66
} from '../errors.js'
77
import type { Atom, Molecule } from '../parsing.js'
88
import type { Canonicalized } from '../parsing/syntax-tree.js'
9+
import { unparse } from '../unparsing.js'
10+
import { prettyPlz } from '../unparsing/pretty-plz.js'
911
import { serializeFunctionNode, type FunctionNode } from './function-node.js'
1012
import { stringifyKeyPathForEndUser, type KeyPath } from './key-path.js'
1113
import {
@@ -174,7 +176,17 @@ export const serialize = (
174176

175177
export const stringifySemanticGraphForEndUser = (
176178
graph: SemanticGraph,
177-
): string => JSON.stringify(serialize(graph))
179+
): string =>
180+
either.match(
181+
either.flatMap(serialize(graph), output =>
182+
// TODO: Use single-line plz notation.
183+
unparse(output, prettyPlz),
184+
),
185+
{
186+
right: stringifiedOutput => stringifiedOutput,
187+
left: error => `(unserializable value: ${error.message})`,
188+
},
189+
)
178190

179191
export const isSemanticGraph = (
180192
value:

0 commit comments

Comments
 (0)