Skip to content

Commit 22f2d87

Browse files
committed
Remove constant values from ast tree
1 parent 3a82579 commit 22f2d87

File tree

7 files changed

+110
-178
lines changed

7 files changed

+110
-178
lines changed

enderpy/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ fn tokenize() -> Result<()> {
106106

107107
fn parse(file: &PathBuf) -> Result<()> {
108108
let source = fs::read_to_string(file).into_diagnostic()?;
109-
let file_path = file.to_str().unwrap_or("");
110109
let mut parser = Parser::new(&source);
111110
let ast = parser.parse();
112111
println!("{:#?}", ast);

parser/src/ast.rs

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ pub struct Node {
1313
pub end: u32,
1414
}
1515

16+
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] // #[serde(tag = "type")]
17+
pub struct TextRange {
18+
pub start: u32,
19+
pub end: u32,
20+
}
21+
1622
impl fmt::Display for Node {
1723
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1824
write!(f, "({}, {})", self.start, self.end)
@@ -342,45 +348,26 @@ pub struct Constant {
342348
pub value: ConstantValue,
343349
}
344350

345-
#[derive(Clone, PartialEq)]
351+
#[derive(Clone, PartialEq, Debug)]
346352
pub enum ConstantValue {
347353
None,
348354
Ellipsis,
349-
Bool(bool),
350-
Str(String),
351-
Bytes(Vec<u8>),
352-
Tuple(Vec<Constant>),
355+
Bool,
356+
Str,
357+
Bytes,
358+
Tuple,
353359
// Numbers are string because we don't care about the value rn.
354-
Int(String),
355-
Float(String),
356-
Complex { real: String, imaginary: String },
360+
Int,
361+
Float,
362+
Complex,
357363
}
358-
impl fmt::Debug for ConstantValue {
359-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
360-
match self {
361-
ConstantValue::None => write!(f, "None"),
362-
ConstantValue::Ellipsis => write!(f, "..."),
363-
ConstantValue::Bool(b) => write!(f, "{}", b),
364-
ConstantValue::Str(s) => {
365-
if s.starts_with("r\"") || s.starts_with("R\"") {
366-
let mut s = s.chars().skip(1).collect::<String>();
367-
s = s.chars().take(s.len() - 1).collect::<String>();
368-
369-
s = s.chars().skip(1).collect::<String>();
370-
371-
return write!(f, "{:?}", s);
372-
}
373-
374-
write!(f, "\"{}\"", s)
375-
}
376-
ConstantValue::Bytes(b) => write!(f, "{:?}", b),
377-
ConstantValue::Tuple(t) => write!(f, "{:?}", t),
378-
ConstantValue::Int(i) => write!(f, "{}", i),
379-
ConstantValue::Float(fl) => write!(f, "{}", fl),
380-
ConstantValue::Complex { real, imaginary } => write!(f, "{}+{}j", real, imaginary),
381-
}
364+
365+
impl Constant {
366+
pub fn get_value<'a>(&self, source: &'a str) -> &'a str {
367+
return &source[self.node.start as usize..self.node.end as usize];
382368
}
383369
}
370+
384371
#[derive(Debug, Clone)]
385372
pub struct List {
386373
pub node: Node,

parser/src/parser/compat.rs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -287,33 +287,11 @@ impl AsPythonCompat for Name {
287287
impl AsPythonCompat for Constant {
288288
fn as_python_compat(&self, parser: &Parser) -> Value {
289289
json_python_compat_node!("Constant", self, parser, {
290-
"value": self.value.as_python_compat(parser),
290+
"value": json!(self.get_value(parser.source)),
291291
})
292292
}
293293
}
294294

295-
impl AsPythonCompat for ConstantValue {
296-
fn as_python_compat(&self, parser: &Parser) -> Value {
297-
match self {
298-
ConstantValue::None => json!(null),
299-
ConstantValue::Ellipsis => json!("..."),
300-
ConstantValue::Bool(v) => json!(v),
301-
ConstantValue::Str(v) => json!(v),
302-
ConstantValue::Bytes(v) => json!(v),
303-
ConstantValue::Tuple(v) => json!(v
304-
.iter()
305-
.map(|cons| cons.as_python_compat(parser))
306-
.collect::<Vec<_>>()),
307-
ConstantValue::Int(v) => Value::Number(Number::from_str(v).unwrap()),
308-
ConstantValue::Float(v) => Value::Number(Number::from_str(v).unwrap()),
309-
ConstantValue::Complex {
310-
real: _real,
311-
imaginary,
312-
} => json!(imaginary),
313-
}
314-
}
315-
}
316-
317295
impl AsPythonCompat for List {
318296
fn as_python_compat(&self, parser: &Parser) -> Value {
319297
json_python_compat_node!("List", self, parser, {

parser/src/parser/mod.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,22 @@ pub fn concat_string_exprs(lhs: Expression, rhs: Expression) -> Result<Expressio
6464
end: rhs.node.end,
6565
};
6666
let concatnated_string = match (lhs.value, rhs.value) {
67-
(ConstantValue::Str(lhs_val), ConstantValue::Str(rhs_val)) => {
67+
(ConstantValue::Str, ConstantValue::Str) => {
6868
Expression::Constant(Box::new(Constant {
6969
node,
70-
value: ConstantValue::Str(lhs_val + &rhs_val),
70+
value: ConstantValue::Str,
7171
}))
7272
}
73-
(ConstantValue::Bytes(mut lhs), ConstantValue::Bytes(rhs)) => {
74-
lhs.append(&mut rhs.clone());
73+
(ConstantValue::Bytes, ConstantValue::Bytes) => {
7574
Expression::Constant(Box::new(Constant {
7675
node,
77-
value: ConstantValue::Bytes(lhs),
76+
value: ConstantValue::Bytes,
7877
}))
7978
}
80-
(ConstantValue::Bytes(_lhs), _) => {
79+
(ConstantValue::Bytes, _) => {
8180
panic!("Cannot concat bytes and string");
8281
}
83-
(_, ConstantValue::Bytes(_rhs)) => {
82+
(_, ConstantValue::Bytes) => {
8483
panic!("Can only concat bytes with other bytes");
8584
}
8685
_ => panic!("Cannot concat string"),
@@ -101,13 +100,13 @@ pub fn concat_string_exprs(lhs: Expression, rhs: Expression) -> Result<Expressio
101100
(Expression::JoinedStr(fstring_lhs), Expression::Constant(const_rhs)) => {
102101
let mut values = fstring_lhs.values;
103102
match const_rhs.value {
104-
ConstantValue::Str(rhs_val) => {
103+
ConstantValue::Str => {
105104
values.push(Expression::Constant(Box::new(Constant {
106105
node: const_rhs.node,
107-
value: ConstantValue::Str(rhs_val),
106+
value: ConstantValue::Str,
108107
})));
109108
}
110-
ConstantValue::Bytes(_) => {
109+
ConstantValue::Bytes => {
111110
panic!("Cannot concat string and bytes");
112111
}
113112
_ => panic!("Cannot concat string"),
@@ -122,11 +121,11 @@ pub fn concat_string_exprs(lhs: Expression, rhs: Expression) -> Result<Expressio
122121
}
123122
(Expression::Constant(const_lhs), Expression::JoinedStr(fstring_rhs)) => {
124123
let const_expr = match const_lhs.value {
125-
ConstantValue::Str(rhs_val) => Expression::Constant(Box::new(Constant {
124+
ConstantValue::Str => Expression::Constant(Box::new(Constant {
126125
node: const_lhs.node,
127-
value: ConstantValue::Str(rhs_val),
126+
value: ConstantValue::Str,
128127
})),
129-
ConstantValue::Bytes(_) => {
128+
ConstantValue::Bytes => {
130129
panic!("Cannot concat string and bytes");
131130
}
132131
_ => panic!("Cannot concat string"),

0 commit comments

Comments
 (0)