|
| 1 | +import kleur from 'kleur' |
| 2 | +import { either, type Either } from '../../adts.js' |
| 3 | +import type { Right } from '../../adts/either.js' |
| 4 | +import type { UnserializableValueError } from '../errors.js' |
| 5 | +import type { Atom, Molecule } from '../parsing.js' |
| 6 | +import { unquotedAtomParser } from '../parsing/atom.js' |
| 7 | +import { |
| 8 | + isSemanticGraph, |
| 9 | + readApplyExpression, |
| 10 | + readFunctionExpression, |
| 11 | + readLookupExpression, |
| 12 | + serialize, |
| 13 | + type KeyPath, |
| 14 | + type SemanticGraph, |
| 15 | +} from '../semantics.js' |
| 16 | +import { type Notation } from './unparsing-utilities.js' |
| 17 | + |
| 18 | +// TODO: Share implementation details with pretty plz notation. |
| 19 | + |
| 20 | +const dot = kleur.dim('.') |
| 21 | +const quote = kleur.dim('"') |
| 22 | +const colon = kleur.dim(':') |
| 23 | +const comma = kleur.dim(',') |
| 24 | +const openBrace = kleur.dim('{') |
| 25 | +const closeBrace = kleur.dim('}') |
| 26 | +const openParenthesis = kleur.dim('(') |
| 27 | +const closeParenthesis = kleur.dim(')') |
| 28 | +const arrow = kleur.dim('=>') |
| 29 | + |
| 30 | +const escapeStringContents = (value: string) => |
| 31 | + value.replace('\\', '\\\\').replace('"', '\\"') |
| 32 | + |
| 33 | +const quoteIfNecessary = (value: string) => |
| 34 | + !either.isLeft(unquotedAtomParser(value)) |
| 35 | + ? value |
| 36 | + : quote.concat(escapeStringContents(value)).concat(quote) |
| 37 | + |
| 38 | +const atom = (node: string): Right<string> => |
| 39 | + either.makeRight( |
| 40 | + quoteIfNecessary( |
| 41 | + /^@[^@]/.test(node) ? kleur.bold(kleur.underline(node)) : node, |
| 42 | + ), |
| 43 | + ) |
| 44 | + |
| 45 | +const molecule = ( |
| 46 | + value: Molecule, |
| 47 | +): Either<UnserializableValueError, string> => { |
| 48 | + const functionExpressionResult = readFunctionExpression(value) |
| 49 | + if (!either.isLeft(functionExpressionResult)) { |
| 50 | + return sugaredFunction( |
| 51 | + functionExpressionResult.value.parameter, |
| 52 | + functionExpressionResult.value.body, |
| 53 | + ) |
| 54 | + } else { |
| 55 | + const applyExpressionResult = readApplyExpression(value) |
| 56 | + if (!either.isLeft(applyExpressionResult)) { |
| 57 | + return sugaredApply( |
| 58 | + applyExpressionResult.value.argument, |
| 59 | + applyExpressionResult.value.function, |
| 60 | + ) |
| 61 | + } else { |
| 62 | + const lookupExpressionResult = readLookupExpression(value) |
| 63 | + if (!either.isLeft(lookupExpressionResult)) { |
| 64 | + return sugaredLookup(lookupExpressionResult.value.query) |
| 65 | + } else { |
| 66 | + return sugarFreeMolecule(value) |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +const sugaredLookup = (keyPathAsNode: Molecule | SemanticGraph) => { |
| 73 | + const keyPath = Object.entries(keyPathAsNode).reduce( |
| 74 | + (accumulator: KeyPath | 'invalid', [key, value]) => { |
| 75 | + if (accumulator === 'invalid') { |
| 76 | + return accumulator |
| 77 | + } else { |
| 78 | + if (key === String(accumulator.length) && typeof value === 'string') { |
| 79 | + return [...accumulator, value] |
| 80 | + } else { |
| 81 | + return 'invalid' |
| 82 | + } |
| 83 | + } |
| 84 | + }, |
| 85 | + [], |
| 86 | + ) |
| 87 | + |
| 88 | + if ( |
| 89 | + keyPath !== 'invalid' && |
| 90 | + Object.keys(keyPathAsNode).length === keyPath.length && |
| 91 | + keyPath.every(key => !either.isLeft(unquotedAtomParser(key))) |
| 92 | + ) { |
| 93 | + return either.makeRight(kleur.cyan(colon.concat(keyPath.join(dot)))) |
| 94 | + } else { |
| 95 | + return either.flatMap(serializeIfNeeded(keyPathAsNode), serializedKeyPath => |
| 96 | + either.map(atomOrMolecule(serializedKeyPath), keyPathAsString => |
| 97 | + kleur.cyan(colon.concat(keyPathAsString)), |
| 98 | + ), |
| 99 | + ) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +const sugaredFunction = ( |
| 104 | + parameterName: string, |
| 105 | + body: Molecule | SemanticGraph, |
| 106 | +) => |
| 107 | + either.flatMap(serializeIfNeeded(body), serializedBody => |
| 108 | + either.map(atomOrMolecule(serializedBody), bodyAsString => |
| 109 | + [kleur.cyan(parameterName), arrow, bodyAsString].join(' '), |
| 110 | + ), |
| 111 | + ) |
| 112 | + |
| 113 | +const sugaredApply = ( |
| 114 | + argument: Molecule | SemanticGraph, |
| 115 | + functionToApply: Molecule | SemanticGraph, |
| 116 | +) => |
| 117 | + either.flatMap(serializeIfNeeded(functionToApply), serializedFunction => |
| 118 | + either.flatMap( |
| 119 | + atomOrMolecule(serializedFunction), |
| 120 | + functionToApplyAsString => |
| 121 | + either.flatMap(serializeIfNeeded(argument), serializedArgument => |
| 122 | + either.map(atomOrMolecule(serializedArgument), argumentAsString => |
| 123 | + functionToApplyAsString |
| 124 | + .concat(openParenthesis) |
| 125 | + .concat(argumentAsString) |
| 126 | + .concat(closeParenthesis), |
| 127 | + ), |
| 128 | + ), |
| 129 | + ), |
| 130 | + ) |
| 131 | + |
| 132 | +const sugarFreeMolecule = (value: Molecule) => { |
| 133 | + const entries = Object.entries(value) |
| 134 | + if (entries.length === 0) { |
| 135 | + return either.makeRight(openBrace + closeBrace) |
| 136 | + } else { |
| 137 | + const keyValuePairsAsStrings: string[] = [] |
| 138 | + let ordinalPropertyKeyCounter = 0n |
| 139 | + for (const [propertyKey, propertyValue] of entries) { |
| 140 | + const valueAsStringResult = atomOrMolecule(propertyValue) |
| 141 | + if (either.isLeft(valueAsStringResult)) { |
| 142 | + return valueAsStringResult |
| 143 | + } |
| 144 | + |
| 145 | + // Omit ordinal property keys: |
| 146 | + if (propertyKey === String(ordinalPropertyKeyCounter)) { |
| 147 | + keyValuePairsAsStrings.push(valueAsStringResult.value) |
| 148 | + ordinalPropertyKeyCounter += 1n |
| 149 | + } else { |
| 150 | + keyValuePairsAsStrings.push( |
| 151 | + kleur |
| 152 | + .cyan(quoteIfNecessary(propertyKey).concat(colon)) |
| 153 | + .concat(' ') |
| 154 | + .concat(valueAsStringResult.value), |
| 155 | + ) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return either.makeRight( |
| 160 | + openBrace |
| 161 | + .concat(' ') |
| 162 | + .concat(keyValuePairsAsStrings.join(comma.concat(' '))) |
| 163 | + .concat(' ') |
| 164 | + .concat(closeBrace), |
| 165 | + ) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +const serializeIfNeeded = ( |
| 170 | + nodeOrMolecule: SemanticGraph | Molecule, |
| 171 | +): Either<UnserializableValueError, Atom | Molecule> => |
| 172 | + isSemanticGraph(nodeOrMolecule) |
| 173 | + ? serialize(nodeOrMolecule) |
| 174 | + : either.makeRight(nodeOrMolecule) |
| 175 | + |
| 176 | +const atomOrMolecule = (value: Atom | Molecule) => |
| 177 | + typeof value === 'string' ? atom(value) : molecule(value) |
| 178 | + |
| 179 | +export const inlinePlz: Notation = { atom, molecule } |
0 commit comments