Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"devDependencies": {
"@types/node": "^22.5.5",
"strip-ansi": "^7.1.0",
"typescript": "^5.6.2"
},
"dependencies": {
Expand Down
4 changes: 1 addition & 3 deletions src/language/cli/output.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import either, { type Either } from '@matt.kantor/either'
import { parseArgs } from 'util'
import { type SyntaxTree } from '../parsing/syntax-tree.js'
import { unparse, type Notation } from '../unparsing.js'
import { prettyJson } from '../unparsing/pretty-json.js'
import { prettyPlz } from '../unparsing/pretty-plz.js'
import { prettyJson, prettyPlz, unparse, type Notation } from '../unparsing.js'

export const handleOutput = async (
process: NodeJS.Process,
Expand Down
123 changes: 123 additions & 0 deletions src/language/compiling/unparsing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import either from '@matt.kantor/either'
import stripAnsi from 'strip-ansi'
import { testCases } from '../../test-utilities.test.js'
import { type Atom, type Molecule } from '../parsing.js'
import {
inlinePlz,
prettyJson,
prettyPlz,
type Notation,
} from '../unparsing.js'

const unparser = (notation: Notation) => (value: Atom | Molecule) =>
either.map(
typeof value === 'string'
? notation.atom(value)
: notation.molecule(value, notation),
stripAnsi, // terminal styling is not currently tested
)

testCases(
unparser(inlinePlz),
input => `unparsing \`${JSON.stringify(input)}\``,
)('inline plz', [
[{}, either.makeRight('{}')],
['a', either.makeRight('a')],
['Hello, world!', either.makeRight('"Hello, world!"')],
[{ 0: 'a' }, either.makeRight('{ a }')],
[{ 1: 'a' }, either.makeRight('{ 1: a }')],
[
{ 0: 'a', 1: 'b', 3: 'c', somethingElse: 'd' },
either.makeRight('{ a, b, 3: c, somethingElse: d }'),
],
[{ a: { b: { c: 'd' } } }, either.makeRight('{ a: { b: { c: d } } }')],
[
{
identity: {
0: '@function',
parameter: 'a',
body: { 0: '@lookup', 1: 'a' },
},
test: {
0: '@apply',
function: { 0: '@lookup', 1: 'identity' },
argument: 'it works!',
},
},
either.makeRight('{ identity: a => :a, test: :identity("it works!") }'),
],
])

testCases(
unparser(prettyPlz),
input => `unparsing \`${JSON.stringify(input)}\``,
)('pretty plz', [
[{}, either.makeRight('{}')],
['a', either.makeRight('a')],
['Hello, world!', either.makeRight('"Hello, world!"')],
[{ 0: 'a' }, either.makeRight('{\n a\n}')],
[{ 1: 'a' }, either.makeRight('{\n 1: a\n}')],
[
{ 0: 'a', 1: 'b', 3: 'c', somethingElse: 'd' },
either.makeRight('{\n a\n b\n 3: c\n somethingElse: d\n}'),
],
[
{ a: { b: { c: 'd' } } },
either.makeRight('{\n a: {\n b: {\n c: d\n }\n }\n}'),
],
[
{
identity: {
0: '@function',
parameter: 'a',
body: { 0: '@lookup', 1: 'a' },
},
test: {
0: '@apply',
function: { 0: '@lookup', 1: 'identity' },
argument: 'it works!',
},
},
either.makeRight(
'{\n identity: a => :a\n test: :identity("it works!")\n}',
),
],
])

testCases(
unparser(prettyJson),
input => `unparsing \`${JSON.stringify(input)}\``,
)('pretty JSON', [
[{}, either.makeRight('{}')],
['a', either.makeRight('"a"')],
['Hello, world!', either.makeRight('"Hello, world!"')],
[{ 0: 'a' }, either.makeRight('{\n "0": "a"\n}')],
[{ 1: 'a' }, either.makeRight('{\n "1": "a"\n}')],
[
{ 0: 'a', 1: 'b', 3: 'c', somethingElse: 'd' },
either.makeRight(
'{\n "0": "a",\n "1": "b",\n "3": "c",\n "somethingElse": "d"\n}',
),
],
[
{ a: { b: { c: 'd' } } },
either.makeRight('{\n "a": {\n "b": {\n "c": "d"\n }\n }\n}'),
],
[
{
identity: {
0: '@function',
parameter: 'a',
body: { 0: '@lookup', 1: 'a' },
},
test: {
0: '@apply',
function: { 0: '@lookup', 1: 'identity' },
argument: 'it works!',
},
},
either.makeRight(
'{\n "identity": {\n "0": "@function",\n "parameter": "a",\n "body": {\n "0": "@lookup",\n "1": "a"\n }\n },\n "test": {\n "0": "@apply",\n "function": {\n "0": "@lookup",\n "1": "identity"\n },\n "argument": "it works!"\n }\n}',
),
],
])
2 changes: 1 addition & 1 deletion src/language/runtime/keywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
lookupPropertyOfObjectNode,
makeUnelaboratedObjectNode,
} from '../semantics/object-node.js'
import { prettyJson } from '../unparsing/pretty-json.js'
import { prettyJson } from '../unparsing.js'

const unserializableFunction = () =>
either.makeLeft({
Expand Down
3 changes: 1 addition & 2 deletions src/language/semantics/key-path.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import either from '@matt.kantor/either'
import type { Atom, Molecule } from '../parsing.js'
import { unparse } from '../unparsing.js'
import { inlinePlz } from '../unparsing/inline-plz.js'
import { inlinePlz, unparse } from '../unparsing.js'

export type KeyPath = readonly Atom[]

Expand Down
3 changes: 1 addition & 2 deletions src/language/semantics/semantic-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ 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 { inlinePlz } from '../unparsing/inline-plz.js'
import { inlinePlz, unparse } from '../unparsing.js'
import { serializeFunctionNode, type FunctionNode } from './function-node.js'
import { stringifyKeyPathForEndUser, type KeyPath } from './key-path.js'
import {
Expand Down
5 changes: 4 additions & 1 deletion src/language/unparsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import type { UnserializableValueError } from './errors.js'
import type { Atom, Molecule } from './parsing.js'
import type { Notation } from './unparsing/unparsing-utilities.js'

export { type Notation } from './unparsing/unparsing-utilities.js'
export { inlinePlz } from './unparsing/inline-plz.js'
export { prettyJson } from './unparsing/pretty-json.js'
export { prettyPlz } from './unparsing/pretty-plz.js'
export type { Notation } from './unparsing/unparsing-utilities.js'

export const unparse = (
value: Atom | Molecule,
Expand Down
Loading
Loading