forked from syndicate-lang/preserves
235 lines
6.9 KiB
Rust
235 lines
6.9 KiB
Rust
use crate::value::Writer;
|
|
use crate::value::suspendable::Suspendable;
|
|
use crate::value::writer::CompoundWriter;
|
|
|
|
use num::bigint::BigInt;
|
|
|
|
use std::io;
|
|
|
|
use super::super::boundary as B;
|
|
|
|
#[derive(Clone, Copy)]
|
|
pub enum CommaStyle {
|
|
None,
|
|
Separating,
|
|
Terminating,
|
|
}
|
|
|
|
pub struct TextWriter<W: io::Write>(Suspendable<W>, CommaStyle);
|
|
|
|
impl std::default::Default for CommaStyle {
|
|
fn default() -> Self {
|
|
CommaStyle::None
|
|
}
|
|
}
|
|
|
|
impl<W: io::Write> TextWriter<W> {
|
|
pub fn new(w: W) -> Self {
|
|
TextWriter(Suspendable::new(w), CommaStyle::default())
|
|
}
|
|
|
|
pub fn suspend(&mut self) -> Self {
|
|
TextWriter(self.0.suspend(), self.1)
|
|
}
|
|
|
|
pub fn resume(&mut self, other: Self) {
|
|
self.0.resume(other.0)
|
|
}
|
|
|
|
pub fn write_stringlike_char_fallback<F>(
|
|
&mut self,
|
|
c: char,
|
|
f: F,
|
|
) -> io::Result<()> where
|
|
F: FnOnce(&mut W, char) -> io::Result<()>
|
|
{
|
|
match c {
|
|
'\\' => write!(self.0, "\\\\"),
|
|
'\x08' => write!(self.0, "\\b"),
|
|
'\x0c' => write!(self.0, "\\f"),
|
|
'\x0a' => write!(self.0, "\\n"),
|
|
'\x0d' => write!(self.0, "\\r"),
|
|
'\x09' => write!(self.0, "\\t"),
|
|
_ => f(&mut self.0, c),
|
|
}
|
|
}
|
|
|
|
pub fn write_stringlike_char(&mut self, c: char) -> io::Result<()> {
|
|
self.write_stringlike_char_fallback(c, |w, c| write!(w, "{}", c))
|
|
}
|
|
}
|
|
|
|
impl<W: io::Write> CompoundWriter for TextWriter<W> {
|
|
fn boundary(&mut self, b: &B::Type) -> io::Result<()> {
|
|
match (b.closing.as_ref(), b.opening.as_ref()) {
|
|
(None, Some(B::Item::Annotation)) =>
|
|
write!(self.0, "@"),
|
|
(Some(_), Some(B::Item::Annotation)) =>
|
|
write!(self.0, " @"),
|
|
(Some(B::Item::Annotation), Some(B::Item::AnnotatedValue)) =>
|
|
write!(self.0, " "),
|
|
|
|
(Some(B::Item::DictionaryKey), Some(B::Item::DictionaryValue)) =>
|
|
write!(self.0, ": "),
|
|
|
|
(Some(B::Item::RecordLabel), Some(B::Item::RecordField)) |
|
|
(Some(B::Item::RecordField), Some(B::Item::RecordField)) =>
|
|
write!(self.0, " "),
|
|
|
|
(Some(B::Item::DictionaryValue), Some(B::Item::DictionaryKey)) |
|
|
(Some(B::Item::SequenceValue), Some(B::Item::SequenceValue)) |
|
|
(Some(B::Item::SetValue), Some(B::Item::SetValue)) =>
|
|
match self.1 {
|
|
CommaStyle::Separating | CommaStyle::Terminating =>
|
|
write!(self.0, ", "),
|
|
CommaStyle::None =>
|
|
write!(self.0, " "),
|
|
},
|
|
|
|
(Some(B::Item::DictionaryValue), None) |
|
|
(Some(B::Item::SequenceValue), None) |
|
|
(Some(B::Item::SetValue), None) =>
|
|
match self.1 {
|
|
CommaStyle::Terminating =>
|
|
write!(self.0, ","),
|
|
CommaStyle::Separating | CommaStyle::None =>
|
|
Ok(()),
|
|
},
|
|
|
|
_ =>
|
|
Ok(())
|
|
}
|
|
}
|
|
}
|
|
|
|
macro_rules! simple_writer_method {
|
|
($n:ident, $argty:ty) =>
|
|
(fn $n (&mut self, v: $argty) -> io::Result<()> {
|
|
write!(self.0, "{}", v)
|
|
});
|
|
}
|
|
|
|
impl<W: io::Write> Writer for TextWriter<W> {
|
|
type AnnWriter = Self;
|
|
type RecWriter = Self;
|
|
type SeqWriter = Self;
|
|
type SetWriter = Self;
|
|
type DictWriter = Self;
|
|
type EmbeddedWriter = Self;
|
|
|
|
fn start_annotations(&mut self) -> io::Result<Self::AnnWriter> {
|
|
Ok(self.suspend())
|
|
}
|
|
|
|
fn end_annotations(&mut self, ann: Self::AnnWriter) -> io::Result<()> {
|
|
self.resume(ann);
|
|
Ok(())
|
|
}
|
|
|
|
fn write_bool(&mut self, v: bool) -> io::Result<()> {
|
|
write!(self.0, "{}", if v { "#t" } else { "#f" })
|
|
}
|
|
|
|
fn write_f32(&mut self, v: f32) -> io::Result<()> {
|
|
write!(self.0, "{:e}f", v)
|
|
}
|
|
|
|
fn write_f64(&mut self, v: f64) -> io::Result<()> {
|
|
write!(self.0, "{:e}", v)
|
|
}
|
|
|
|
simple_writer_method!(write_i8, i8);
|
|
simple_writer_method!(write_u8, u8);
|
|
simple_writer_method!(write_i16, i16);
|
|
simple_writer_method!(write_u16, u16);
|
|
simple_writer_method!(write_i32, i32);
|
|
simple_writer_method!(write_u32, u32);
|
|
simple_writer_method!(write_i64, i64);
|
|
simple_writer_method!(write_u64, u64);
|
|
simple_writer_method!(write_i128, i128);
|
|
simple_writer_method!(write_u128, u128);
|
|
simple_writer_method!(write_int, &BigInt);
|
|
|
|
fn write_string(&mut self, v: &str) -> io::Result<()> {
|
|
write!(self.0, "\"")?;
|
|
for c in v.chars() {
|
|
match c {
|
|
'"' => write!(self.0, "\\\"")?,
|
|
_ => self.write_stringlike_char(c)?,
|
|
}
|
|
}
|
|
write!(self.0, "\"")
|
|
}
|
|
|
|
fn write_bytes(&mut self, v: &[u8]) -> io::Result<()> {
|
|
write!(self.0, "#[{}]", base64::encode_config(v, base64::STANDARD_NO_PAD))
|
|
}
|
|
|
|
fn write_symbol(&mut self, v: &str) -> io::Result<()> {
|
|
// FIXME: This regular expression is conservatively correct, but Anglo-chauvinistic.
|
|
let re = regex::Regex::new("^[a-zA-Z~!$%^&*?_=+/.][-a-zA-Z~!$%^&*?_=+/.0-9]*$").unwrap();
|
|
if re.is_match(v) {
|
|
write!(self.0, "{}", v)
|
|
} else {
|
|
write!(self.0, "|")?;
|
|
for c in v.chars() {
|
|
match c {
|
|
'|' => write!(self.0, "\\|")?,
|
|
_ => self.write_stringlike_char(c)?,
|
|
}
|
|
}
|
|
write!(self.0, "|")
|
|
}
|
|
}
|
|
|
|
fn start_record(&mut self, _field_count: Option<usize>) -> io::Result<Self::RecWriter> {
|
|
write!(self.0, "<")?;
|
|
Ok(self.suspend())
|
|
}
|
|
|
|
fn end_record(&mut self, rec: Self::RecWriter) -> io::Result<()> {
|
|
self.resume(rec);
|
|
write!(self.0, ">")
|
|
}
|
|
|
|
fn start_sequence(&mut self, _item_count: Option<usize>) -> io::Result<Self::SeqWriter> {
|
|
write!(self.0, "[")?;
|
|
Ok(self.suspend())
|
|
}
|
|
|
|
fn end_sequence(&mut self, seq: Self::SeqWriter) -> io::Result<()> {
|
|
self.resume(seq);
|
|
write!(self.0, "]")
|
|
}
|
|
|
|
fn start_set(&mut self, _item_count: Option<usize>) -> io::Result<Self::SetWriter> {
|
|
write!(self.0, "#{{")?;
|
|
Ok(self.suspend())
|
|
}
|
|
|
|
fn end_set(&mut self, set: Self::SetWriter) -> io::Result<()> {
|
|
self.resume(set);
|
|
write!(self.0, "}}")
|
|
}
|
|
|
|
fn start_dictionary(&mut self, _entry_count: Option<usize>) -> io::Result<Self::DictWriter> {
|
|
write!(self.0, "{{")?;
|
|
Ok(self.suspend())
|
|
}
|
|
|
|
fn end_dictionary(&mut self, dict: Self::DictWriter) -> io::Result<()> {
|
|
self.resume(dict);
|
|
write!(self.0, "}}")
|
|
}
|
|
|
|
fn start_embedded(&mut self) -> io::Result<Self::EmbeddedWriter> {
|
|
write!(self.0, "#!")?;
|
|
Ok(self.suspend())
|
|
}
|
|
|
|
fn end_embedded(&mut self, ptr: Self::EmbeddedWriter) -> io::Result<()> {
|
|
self.resume(ptr);
|
|
Ok(())
|
|
}
|
|
}
|