Skip to content

Commit 181fb99

Browse files
committed
parser: hacky escape hatch for pretty printing resolved symbols
1 parent 09b1947 commit 181fb99

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

parser/src/idempotency.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,21 @@ impl Display for Expr<'_> {
240240

241241
impl Display for Atom<'_> {
242242
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
243-
<Self as Debug>::fmt(self, f)
243+
if let Self::Variable(var) = self {
244+
<_ as Display>::fmt(var, f)
245+
} else {
246+
<_ as Debug>::fmt(self, f)
247+
}
244248
}
245249
}
246250

247251
impl Display for Variable<'_> {
248252
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
249-
<Self as Debug>::fmt(self, f)
253+
if let Self::User(ident) = self {
254+
<_ as Display>::fmt(ident, f)
255+
} else {
256+
<_ as Debug>::fmt(self, f)
257+
}
250258
}
251259
}
252260

parser/src/sexpr.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
// For the full copyright and license information, please view the LICENSE
44
// files that was distributed with this source code.
55

6-
use std::fmt::{Debug, Display, Formatter, Result};
6+
use std::{
7+
fmt::{Debug, Display, Formatter, Result},
8+
ptr,
9+
};
710

811
use crate::ast::{
912
Atom, Body, Identifier, Place, Redirection, SimpleStatement, Statement, Variable,
@@ -230,7 +233,18 @@ impl Debug for Identifier<'_> {
230233

231234
impl Display for Identifier<'_> {
232235
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
233-
<&str as Display>::fmt(&self.literal, f)
236+
// HACK: to make sure we resolve qualifications like awk after symbol
237+
// resolution, we check whether the source code has them together.
238+
if ptr::eq(
239+
self.namespace
240+
.as_ptr()
241+
.wrapping_add(self.namespace.len() + 2),
242+
self.literal.as_ptr(),
243+
) {
244+
<_ as Debug>::fmt(self, f)
245+
} else {
246+
<_ as Display>::fmt(&self.literal, f)
247+
}
234248
}
235249
}
236250

0 commit comments

Comments
 (0)