From d02ee1ed7bf3457a617c01018690047a88c1620e Mon Sep 17 00:00:00 2001 From: "Dylan R. Johnston" Date: Tue, 7 May 2024 15:21:40 +0800 Subject: [PATCH 1/9] Fixed std::fmt::Display for abort expressions --- src/parser/ast.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser/ast.rs b/src/parser/ast.rs index 35e7495f29..bca6ae8982 100644 --- a/src/parser/ast.rs +++ b/src/parser/ast.rs @@ -1196,7 +1196,7 @@ impl fmt::Display for Abort { &self .message .as_ref() - .map_or_else(|| "abort".to_owned(), |m| format!("abort: {m}")), + .map_or_else(|| "abort".to_owned(), |m| format!("abort {m}")), ) } } From 45428f07f6fcd1f1fb58846479cb91eda24bc5b8 Mon Sep 17 00:00:00 2001 From: "Dylan R. Johnston" Date: Tue, 7 May 2024 16:45:04 +0800 Subject: [PATCH 2/9] Fix std::fmt::Display for Query --- src/parser/ast.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/parser/ast.rs b/src/parser/ast.rs index bca6ae8982..8e83b514c3 100644 --- a/src/parser/ast.rs +++ b/src/parser/ast.rs @@ -933,7 +933,7 @@ pub struct Query { impl fmt::Display for Query { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}{}", self.target, self.path) + write!(f, "{}.{}", self.target, self.path) } } @@ -958,7 +958,7 @@ impl fmt::Display for QueryTarget { match self { Internal(v) => v.fmt(f), External(prefix) => match prefix { - PathPrefix::Event => write!(f, "."), + PathPrefix::Event => write!(f, ""), PathPrefix::Metadata => write!(f, "&"), }, FunctionCall(v) => v.fmt(f), From 220ef896206cd627a82427854ec62fbc15232df3 Mon Sep 17 00:00:00 2001 From: "Dylan R. Johnston" Date: Tue, 7 May 2024 16:45:25 +0800 Subject: [PATCH 3/9] Fix std::fmt::Display for Infailable function calls --- src/parser/ast.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/parser/ast.rs b/src/parser/ast.rs index 8e83b514c3..6a510f11e9 100644 --- a/src/parser/ast.rs +++ b/src/parser/ast.rs @@ -1002,6 +1002,9 @@ pub struct FunctionCall { impl fmt::Display for FunctionCall { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.ident.fmt(f)?; + if self.abort_on_error { + f.write_str("!")?; + } f.write_str("(")?; let mut iter = self.arguments.iter().peekable(); From 9206835b4a510321f944f0e17a1b90946bf99af8 Mon Sep 17 00:00:00 2001 From: "Dylan R. Johnston" Date: Tue, 21 May 2024 11:26:01 +0800 Subject: [PATCH 4/9] Fix float formatting --- src/parser/ast.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser/ast.rs b/src/parser/ast.rs index 6a510f11e9..f373b37ffd 100644 --- a/src/parser/ast.rs +++ b/src/parser/ast.rs @@ -358,7 +358,7 @@ impl fmt::Display for Literal { String(v) => write!(f, r#""{v}""#), RawString(v) => write!(f, "s'{v}'"), Integer(v) => v.fmt(f), - Float(v) => v.fmt(f), + Float(v) => write!(f, "{:?}", v.as_ref()), Boolean(v) => v.fmt(f), Regex(v) => write!(f, "r'{v}'"), Timestamp(v) => write!(f, "t'{v}'"), From 8aa7d85b41f6fab4e320a9ae06851b4e357dea39 Mon Sep 17 00:00:00 2001 From: "Dylan R. Johnston" Date: Fri, 31 May 2024 15:22:46 +0800 Subject: [PATCH 5/9] Support nested indentation --- src/parser/ast.rs | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/src/parser/ast.rs b/src/parser/ast.rs index f373b37ffd..2254af8a14 100644 --- a/src/parser/ast.rs +++ b/src/parser/ast.rs @@ -1,6 +1,6 @@ use std::{ collections::BTreeMap, - fmt, + fmt::{self, Write}, hash::{Hash, Hasher}, iter::IntoIterator, ops::Deref, @@ -318,7 +318,7 @@ impl AsRef for Ident { impl fmt::Display for Ident { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.0.fmt(f) + f.write_str(&self.0) } } @@ -359,7 +359,7 @@ impl fmt::Display for Literal { RawString(v) => write!(f, "s'{v}'"), Integer(v) => v.fmt(f), Float(v) => write!(f, "{:?}", v.as_ref()), - Boolean(v) => v.fmt(f), + Boolean(v) => write!(f, "{v}"), Regex(v) => write!(f, "r'{v}'"), Timestamp(v) => write!(f, "t'{v}'"), Null => f.write_str("null"), @@ -442,14 +442,19 @@ impl fmt::Display for Block { let mut iter = self.0.iter().peekable(); while let Some(expr) = iter.next() { - f.write_str("\t")?; + (0..f.width().unwrap_or_default()).try_for_each(|_| f.write_char(' '))?; expr.fmt(f)?; if iter.peek().is_some() { f.write_str("\n")?; } } - f.write_str("\n}") + f.write_str("\n")?; + f.write_fmt(format_args!( + "{:>width$}", + "}", + width = f.width().unwrap_or(4) - 3 + )) } } @@ -602,14 +607,16 @@ impl fmt::Debug for IfStatement { impl fmt::Display for IfStatement { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let width = f.width().unwrap_or_default() + 4; + f.write_str("if ")?; self.predicate.fmt(f)?; f.write_str(" ")?; - self.if_node.fmt(f)?; + f.write_fmt(format_args!("{:width$}", self.if_node,))?; if let Some(alt) = &self.else_node { - f.write_str(" else")?; - alt.fmt(f)?; + f.write_str(" else ")?; + f.write_fmt(format_args!("{:width$}", alt))?; } Ok(()) @@ -830,9 +837,12 @@ impl fmt::Display for Assignment { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { use Assignment::{Infallible, Single}; + let width = f.width().unwrap_or_default(); match self { - Single { target, op, expr } => write!(f, "{target} {op} {expr}"), - Infallible { ok, err, op, expr } => write!(f, "{ok}, {err} {op} {expr}"), + Single { target, op, expr } => f.write_fmt(format_args!("{target} {op} {expr:width$}")), + Infallible { ok, err, op, expr } => { + f.write_fmt(format_args!("{ok}, {err} {op} {expr:width$}")) + } } } } @@ -1019,7 +1029,6 @@ impl fmt::Display for FunctionCall { f.write_str(")")?; if let Some(closure) = &self.closure { - f.write_str(" ")?; closure.fmt(f)?; } @@ -1095,7 +1104,7 @@ pub struct FunctionClosure { impl fmt::Display for FunctionClosure { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str("-> |")?; + f.write_str(" -> |")?; let mut iter = self.variables.iter().peekable(); while let Some(var) = iter.next() { @@ -1106,18 +1115,10 @@ impl fmt::Display for FunctionClosure { } } - f.write_str("| {\n")?; - - let mut iter = self.block.0.iter().peekable(); - while let Some(expr) = iter.next() { - f.write_str("\t")?; - expr.fmt(f)?; - if iter.peek().is_some() { - f.write_str("\n")?; - } - } + f.write_str("| ")?; - f.write_str("\n}") + let width = f.width().unwrap_or_default() + 4; + f.write_fmt(format_args!("{:width$}", self.block)) } } From 12356c6768efc1986a318f90cebf0953915445f0 Mon Sep 17 00:00:00 2001 From: "Dylan R. Johnston" Date: Wed, 19 Jun 2024 12:07:04 +0800 Subject: [PATCH 6/9] Fix indentation of inline blocks --- src/parser/ast.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/parser/ast.rs b/src/parser/ast.rs index 2254af8a14..dbaf1fade7 100644 --- a/src/parser/ast.rs +++ b/src/parser/ast.rs @@ -318,7 +318,7 @@ impl AsRef for Ident { impl fmt::Display for Ident { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(&self.0) + write!(f, "{}", &self.0) } } @@ -357,7 +357,7 @@ impl fmt::Display for Literal { match self { String(v) => write!(f, r#""{v}""#), RawString(v) => write!(f, "s'{v}'"), - Integer(v) => v.fmt(f), + Integer(v) => write!(f, "{v}"), Float(v) => write!(f, "{:?}", v.as_ref()), Boolean(v) => write!(f, "{v}"), Regex(v) => write!(f, "r'{v}'"), @@ -389,9 +389,11 @@ impl fmt::Display for Container { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { use Container::{Array, Block, Group, Object}; + let width = f.width().unwrap_or_default() + 4; + match self { Group(v) => v.fmt(f), - Block(v) => v.fmt(f), + Block(v) => f.write_fmt(format_args!("{:width$}", v)), Array(v) => v.fmt(f), Object(v) => v.fmt(f), } @@ -607,10 +609,10 @@ impl fmt::Debug for IfStatement { impl fmt::Display for IfStatement { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let width = f.width().unwrap_or_default() + 4; - f.write_str("if ")?; self.predicate.fmt(f)?; + + let width = f.width().unwrap_or_default() + 4; f.write_str(" ")?; f.write_fmt(format_args!("{:width$}", self.if_node,))?; @@ -682,7 +684,13 @@ pub struct Op(pub Box>, pub Node, pub Box>); impl fmt::Display for Op { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{} {} {}", self.0, self.1, self.2) + f.write_fmt(format_args!( + "{:width$} {:width$} {:width$}", + self.0, + self.1, + self.2, + width = f.width().unwrap_or_default() + )) } } @@ -713,7 +721,7 @@ pub enum Opcode { impl fmt::Display for Opcode { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.as_str().fmt(f) + write!(f, "{}", self.as_str()) } } From c8e0fb532aa100d70a04c9a7e3eab26fd5944fd1 Mon Sep 17 00:00:00 2001 From: "Dylan R. Johnston" Date: Thu, 20 Jun 2024 16:24:21 +0800 Subject: [PATCH 7/9] Fix formatting of array indexing --- src/parser/ast.rs | 11 +++++++++-- src/path/owned.rs | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/parser/ast.rs b/src/parser/ast.rs index dbaf1fade7..29dc7a63e1 100644 --- a/src/parser/ast.rs +++ b/src/parser/ast.rs @@ -951,7 +951,14 @@ pub struct Query { impl fmt::Display for Query { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}.{}", self.target, self.path) + let target = format!("{}", self.target); + let path = format!("{}", self.path); + + if target == "." && path.starts_with(".") { + write!(f, "{path}") + } else { + write!(f, "{target}{path}") + } } } @@ -976,7 +983,7 @@ impl fmt::Display for QueryTarget { match self { Internal(v) => v.fmt(f), External(prefix) => match prefix { - PathPrefix::Event => write!(f, ""), + PathPrefix::Event => write!(f, "."), PathPrefix::Metadata => write!(f, "&"), }, FunctionCall(v) => v.fmt(f), diff --git a/src/path/owned.rs b/src/path/owned.rs index bc7e57e84b..78a537f073 100644 --- a/src/path/owned.rs +++ b/src/path/owned.rs @@ -296,7 +296,7 @@ impl From<&OwnedValuePath> for String { for (i, segment) in owned.segments.iter().enumerate() { match segment { OwnedSegment::Field(field) => { - serialize_field(&mut output, field.as_ref(), (i != 0).then_some(".")) + serialize_field(&mut output, field.as_ref(), Some(".")) } OwnedSegment::Index(index) => { write!(output, "[{index}]").expect("Could not write to string") From 89f8ccf8f78372274d8a41fcf283280840d6f70b Mon Sep 17 00:00:00 2001 From: "Dylan R. Johnston" Date: Wed, 3 Jul 2024 13:39:27 +0800 Subject: [PATCH 8/9] Fix identation within unary negation --- src/parser/ast.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/parser/ast.rs b/src/parser/ast.rs index 29dc7a63e1..701462aa12 100644 --- a/src/parser/ast.rs +++ b/src/parser/ast.rs @@ -1190,7 +1190,8 @@ impl Not { impl fmt::Display for Not { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "!{}", self.1) + let width = f.width().unwrap_or_default(); + write!(f, "!{:width$}", self.1) } } From b4a194b730479263c47b3227e05230de81bb353f Mon Sep 17 00:00:00 2001 From: "Dylan R. Johnston" Date: Thu, 4 Jul 2024 15:39:50 +0800 Subject: [PATCH 9/9] Re-escape string literals --- src/parser/template_string.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/parser/template_string.rs b/src/parser/template_string.rs index 8f55b553c9..3a949547cd 100644 --- a/src/parser/template_string.rs +++ b/src/parser/template_string.rs @@ -13,7 +13,12 @@ pub enum StringSegment { impl fmt::Display for StringSegment { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - StringSegment::Literal(s, _) => write!(f, "{s}"), + StringSegment::Literal(s, _) => { + let s = format!("{s:?}"); + let len = s.len(); + + write!(f, "{}", &s.as_str()[1..len - 1]) + } StringSegment::Template(s, _) => write!(f, "{{{{ {s} }}}}"), } }