Skip to content

Commit

Permalink
Move Ident::label_quoted into pretty as a freestanding function
Browse files Browse the repository at this point in the history
  • Loading branch information
vkleen committed Jun 28, 2023
1 parent b92d48f commit 78a10ed
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
18 changes: 1 addition & 17 deletions core/src/identifier.rs
Original file line number Diff line number Diff line change
@@ -1,17 +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, pretty::escape, 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 @@ -55,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!("\"{}\"", escape(label))
}
}

pub fn into_label(self) -> String {
self.label().to_owned()
}
Expand Down
35 changes: 24 additions & 11 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,13 +44,28 @@ fn sorted_map<K: Ord, V>(m: &'_ IndexMap<K, V>) -> Vec<(&'_ K, &'_ V)> {
ret
}

pub fn escape(s: &str) -> String {
/// 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 Down Expand Up @@ -179,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 @@ -297,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 @@ -586,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 @@ -620,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 @@ -740,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 @@ -779,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

0 comments on commit 78a10ed

Please sign in to comment.