Skip to content

Commit

Permalink
syntax: escape printing out Char and String types
Browse files Browse the repository at this point in the history
  • Loading branch information
clarete committed Oct 2, 2023
1 parent 98572a4 commit 55d1af5
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions langlang_syntax/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl ToString for Grammar {
output.push_str(&i.to_string());
output.push('\n');
}
output.push('\n');
for name in &self.definition_names {
let d = &self.definitions[name];
output.push_str(&d.to_string());
Expand Down Expand Up @@ -376,7 +377,7 @@ pub enum Literal {
impl ToString for Literal {
fn to_string(&self) -> StdString {
match self {
Literal::String(v) => format!("\"{}\"", v.value),
Literal::String(v) => format!("\"{}\"", v.to_string()),
Literal::Class(v) => v.to_string(),
Literal::Range(v) => format!("{}-{}", v.start, v.end),
Literal::Char(v) => v.to_string(),
Expand All @@ -397,6 +398,15 @@ impl String {
}
}

impl ToString for String {
fn to_string(&self) -> StdString {
self.value
.chars()
.flat_map(|c| c.escape_default())
.collect()
}
}

#[derive(Clone, Debug, PartialEq)]
pub struct Class {
pub span: Span,
Expand Down Expand Up @@ -449,10 +459,7 @@ impl Char {

impl ToString for Char {
fn to_string(&self) -> StdString {
match self.value {
'\n' => "\\n".to_string(),
_ => format!("{}", self.value),
}
self.value.escape_default().collect()
}
}

Expand Down

0 comments on commit 55d1af5

Please sign in to comment.