Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the pretty printer correctly escape field names #1410

Merged
merged 2 commits into from
Jun 29, 2023
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
5 changes: 5 additions & 0 deletions cli/tests/snapshot/inputs/pretty/field_escaping.ncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# capture = 'stdout'
# command = ['pprint-ast']
{
"this needs \\\"escaping\"\nvery much" = true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: cli/tests/snapshot/main.rs
expression: out
---
{ "this needs \\\"escaping\"\nvery much" = true, }
16 changes: 1 addition & 15 deletions core/src/identifier.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
//! Define the type of an identifier.
use once_cell::sync::Lazy;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Debug};
use std::hash::Hash;

use crate::{parser::lexer::KEYWORDS, position::TermPos, term::string::NickelString};
use crate::{position::TermPos, term::string::NickelString};

simple_counter::generate_counter!(GeneratedCounter, usize);
static INTERNER: Lazy<interner::Interner> = Lazy::new(interner::Interner::new);
static QUOTING_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new("^_?[a-zA-Z][_a-zA-Z0-9-]*$").unwrap());

#[derive(Clone, Copy, Deserialize, Serialize)]
#[serde(into = "String", from = "String")]
Expand Down Expand Up @@ -53,18 +51,6 @@ impl Ident {
INTERNER.lookup(self.symbol)
}

/// Return the string representation of this identifier, and add enclosing double quotes if the
/// label isn't a valid identifier according to the parser, for example if it contains a
/// special character like a space.
pub fn label_quoted(&self) -> String {
let label = self.label();
if QUOTING_REGEX.is_match(label) && !KEYWORDS.contains(&label) {
String::from(label)
} else {
format!("\"{label}\"")
}
}

pub fn into_label(self) -> String {
self.label().to_owned()
}
Expand Down
47 changes: 31 additions & 16 deletions core/src/pretty.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use crate::destructuring::{self, RecordPattern};
use crate::identifier::Ident;

use crate::parser::lexer::KEYWORDS;
use crate::term::{
record::{Field, FieldMetadata},
*,
};
use crate::types::*;

use malachite::num::{basic::traits::Zero, conversion::traits::ToSci};
use once_cell::sync::Lazy;
pub use pretty::{DocAllocator, DocBuilder, Pretty};
use regex::Regex;

Expand Down Expand Up @@ -42,6 +44,28 @@ fn sorted_map<K: Ord, V>(m: &'_ IndexMap<K, V>) -> Vec<(&'_ K, &'_ V)> {
ret
}

/// Escape a string to make it suitable for placing between quotes in Nickel
fn escape(s: &str) -> String {
s.replace('\\', "\\\\")
.replace("%{", "\\%{")
.replace('\"', "\\\"")
.replace('\n', "\\n")
}

static QUOTING_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new("^_?[a-zA-Z][_a-zA-Z0-9-]*$").unwrap());

/// Return the string representation of an identifier, and add enclosing double quotes if the
/// label isn't a valid identifier according to the parser, for example if it contains a
/// special character like a space.
fn ident_quoted(ident: &Ident) -> String {
let label = ident.label();
if QUOTING_REGEX.is_match(label) && !KEYWORDS.contains(&label) {
String::from(label)
} else {
format!("\"{}\"", escape(label))
}
}

impl<'a, A: Clone + 'a> NickelAllocatorExt<'a, A> for pretty::BoxAllocator {}

trait NickelAllocatorExt<'a, A: 'a>: DocAllocator<'a, A> + Sized
Expand All @@ -52,12 +76,7 @@ where
/// Escape the special characters in a string, including the newline character, so that it can
/// be enclosed by double quotes a be a valid Nickel string.
fn escaped_string(&'a self, s: &str) -> DocBuilder<'a, Self, A> {
let s = s
.replace('\\', "\\\\")
.replace("%{", "\\%{")
.replace('\"', "\\\"")
.replace('\n', "\\n");
self.text(s)
self.text(escape(s))
}

/// Print string chunks, either in the single line or multiline style.
Expand Down Expand Up @@ -177,7 +196,7 @@ where
}

fn field(&'a self, id: &Ident, field: &Field, with_doc: bool) -> DocBuilder<'a, Self, A> {
self.text(id.label_quoted())
self.text(ident_quoted(id))
.append(self.field_body(field, with_doc))
}

Expand Down Expand Up @@ -295,9 +314,7 @@ where
BoolNot() => allocator.text("!"),
BoolAnd() => allocator.space().append(allocator.text("&&")),
BoolOr() => allocator.space().append(allocator.text("||")),
StaticAccess(id) => allocator
.text(".")
.append(allocator.text(id.label_quoted())),
StaticAccess(id) => allocator.text(".").append(allocator.text(ident_quoted(id))),
Embed(id) => allocator
.text("%embed%")
.append(allocator.space())
Expand Down Expand Up @@ -584,9 +601,7 @@ where
.group(),
},
Var(id) => allocator.as_string(id),
Enum(id) => allocator
.text("'")
.append(allocator.text(id.label_quoted())),
Enum(id) => allocator.text("'").append(allocator.text(ident_quoted(id))),
Record(record) => allocator
.line()
.append(allocator.fields(&record.fields, true))
Expand Down Expand Up @@ -618,7 +633,7 @@ where
sorted_map(cases).iter().map(|&(id, t)| {
allocator
.text("'")
.append(allocator.text(id.label_quoted()))
.append(allocator.text(ident_quoted(id)))
.append(allocator.space())
.append(allocator.text("=>"))
.append(allocator.space())
Expand Down Expand Up @@ -738,7 +753,7 @@ where
EnumRowsF::Extend { row, tail } => {
let builder = allocator
.text("'")
.append(allocator.text(row.label_quoted()));
.append(allocator.text(ident_quoted(&row)));
let builder = if let EnumRowsF::Extend { .. } = tail.0 {
builder
.append(allocator.text(","))
Expand Down Expand Up @@ -777,7 +792,7 @@ where
tail,
} => {
let builder = allocator
.text(id.label_quoted())
.text(ident_quoted(&id))
.append(allocator.text(":"))
.append(allocator.space())
.append(types.pretty(allocator));
Expand Down