Skip to content

Commit 1ca1dea

Browse files
committed
Add sugar-free-plz output format
1 parent bab859d commit 1ca1dea

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/language/cli/output.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import either, { type Either } from '@matt.kantor/either'
22
import { parseArgs } from 'util'
33
import { type SyntaxTree } from '../parsing/syntax-tree.js'
4-
import { prettyJson, prettyPlz, unparse, type Notation } from '../unparsing.js'
4+
import {
5+
prettyJson,
6+
prettyPlz,
7+
sugarFreePrettyPlz,
8+
unparse,
9+
type Notation,
10+
} from '../unparsing.js'
511

612
export const handleOutput = async (
713
process: NodeJS.Process,
@@ -23,6 +29,8 @@ export const handleOutput = async (
2329
notation = prettyJson
2430
} else if (outputFormat === 'plz') {
2531
notation = prettyPlz
32+
} else if (outputFormat === 'sugar-free-plz') {
33+
notation = sugarFreePrettyPlz
2634
} else {
2735
throw new Error(`Unsupported output format: "${outputFormat}"`)
2836
}

src/language/unparsing.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { Notation } from './unparsing/unparsing-utilities.js'
66
export { inlinePlz } from './unparsing/inline-plz.js'
77
export { prettyJson } from './unparsing/pretty-json.js'
88
export { prettyPlz } from './unparsing/pretty-plz.js'
9+
export { sugarFreePrettyPlz } from './unparsing/sugar-free-pretty-plz.js'
910
export type { Notation } from './unparsing/unparsing-utilities.js'
1011

1112
export const unparse = (
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import either from '@matt.kantor/either'
2+
import type { Atom, Molecule } from '../parsing.js'
3+
import {
4+
closeBrace,
5+
moleculeAsKeyValuePairStrings,
6+
openBrace,
7+
unparseAtom,
8+
} from './plz-utilities.js'
9+
import { indent, type Notation } from './unparsing-utilities.js'
10+
11+
const unparseMolecule = (value: Molecule) => {
12+
if (Object.keys(value).length === 0) {
13+
return either.makeRight(openBrace + closeBrace)
14+
} else {
15+
return either.map(
16+
moleculeAsKeyValuePairStrings(value, unparseAtomOrMolecule, {
17+
ordinalKeys: 'preserve',
18+
}),
19+
keyValuePairsAsStrings =>
20+
openBrace
21+
.concat('\n')
22+
.concat(indent(2, keyValuePairsAsStrings.join('\n')))
23+
.concat('\n')
24+
.concat(closeBrace),
25+
)
26+
}
27+
}
28+
29+
const unparseAtomOrMolecule = (value: Atom | Molecule) =>
30+
typeof value === 'string' ? unparseAtom(value) : unparseMolecule(value)
31+
32+
export const sugarFreePrettyPlz: Notation = {
33+
atom: unparseAtom,
34+
molecule: unparseMolecule,
35+
}

0 commit comments

Comments
 (0)