From 67639eae9110f14160bcc837e094be7796511f13 Mon Sep 17 00:00:00 2001 From: Matt Kantor Date: Mon, 27 Jan 2025 10:02:27 -0500 Subject: [PATCH] Fix atom quoting in pretty-plz unparser Quotes were only being used for atoms beginning with a quote-requiring character sequence, rather than atoms containing any such sequences. --- src/language/unparsing/pretty-plz.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/language/unparsing/pretty-plz.ts b/src/language/unparsing/pretty-plz.ts index 51dbeaa..4303edb 100644 --- a/src/language/unparsing/pretty-plz.ts +++ b/src/language/unparsing/pretty-plz.ts @@ -29,10 +29,17 @@ const arrow = kleur.dim('=>') const escapeStringContents = (value: string) => value.replace('\\', '\\\\').replace('"', '\\"') -const quoteIfNecessary = (value: string) => - !either.isLeft(unquotedAtomParser(value)) - ? value - : quote.concat(escapeStringContents(value)).concat(quote) +const quoteIfNecessary = (value: string) => { + const unquotedAtomResult = unquotedAtomParser(value) + if ( + either.isLeft(unquotedAtomResult) || + unquotedAtomResult.value.remainingInput.length !== 0 + ) { + return quote.concat(escapeStringContents(value)).concat(quote) + } else { + return value + } +} const atom = (node: string): Right => either.makeRight(