pax-language 0.39.1

Pax language parser
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
#[cfg(feature = "parser")]
use computable::Computable;
use pax_runtime_api::PaxValue;
#[cfg(feature = "parser")]
use pest::{
    iterators::{Pair, Pairs},
    pratt_parser::PrattParser,
};
#[cfg(feature = "parser")]
use property_resolution::IdentifierResolver;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
#[cfg(feature = "parser")]
use std::rc::Rc;

#[cfg(feature = "parser")]
use crate::{deserializer::from_pax_ast, get_pax_pratt_parser, parse_pax_pairs, Rule};

pub(crate) mod computable;
pub mod property_resolution;
#[cfg(test)]
mod tests;

#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
/// PAXEL expression AST node.
pub enum PaxExpression {
    Primary(Box<PaxPrimary>),
    Prefix(Box<PaxPrefix>),
    Infix(Box<PaxInfix>),
    Postfix(Box<PaxPostfix>),
    Ternary(Box<PaxTernary>),
    NullCoalesce(Box<PaxNullCoalesce>),
}

impl Default for PaxExpression {
    fn default() -> Self {
        Self::Primary(Box::new(PaxPrimary::default()))
    }
}

impl PaxExpression {
    /// Construct a prefix expression.
    pub fn prefix(operator: impl Into<String>, rhs: PaxExpression) -> Self {
        Self::Prefix(Box::new(PaxPrefix {
            operator: PaxOperator::new(operator),
            rhs: Box::new(rhs),
        }))
    }

    /// Construct an infix expression.
    pub fn infix(lhs: PaxExpression, operator: impl Into<String>, rhs: PaxExpression) -> Self {
        Self::Infix(Box::new(PaxInfix {
            operator: PaxOperator::new(operator),
            lhs: Box::new(lhs),
            rhs: Box::new(rhs),
        }))
    }

    /// Construct a postfix expression.
    pub fn postfix(lhs: PaxExpression, operator: impl Into<String>) -> Self {
        Self::Postfix(Box::new(PaxPostfix {
            operator: PaxOperator::new(operator),
            lhs: Box::new(lhs),
        }))
    }

    /// Construct a ternary expression.
    pub fn ternary(
        condition: PaxExpression,
        then_branch: PaxExpression,
        else_branch: PaxExpression,
    ) -> Self {
        Self::Ternary(Box::new(PaxTernary {
            condition: Box::new(condition),
            then_branch: Box::new(then_branch),
            else_branch: Box::new(else_branch),
        }))
    }

    /// Construct a null-coalescing expression.
    pub fn null_coalesce(lhs: PaxExpression, rhs: PaxExpression) -> Self {
        Self::NullCoalesce(Box::new(PaxNullCoalesce {
            lhs: Box::new(lhs),
            rhs: Box::new(rhs),
        }))
    }
}

impl PaxExpression {
    // Mirror parsing.rs (including the outer grammar) when displaying ASTs
    // constructed without explicit Grouped nodes, e.g. by cartridge generation.
    fn precedence(&self) -> u8 {
        match self {
            Self::Ternary(_) => 1,
            Self::NullCoalesce(_) => 2,
            Self::Infix(i) => match i.operator.name.as_str() {
                "||" => 3,
                "&&" => 4,
                "==" | "!=" | "<" | "<=" | ">" | ">=" => 5,
                "+" | "-" => 6,
                "*" | "/" | "%%" => 7,
                "^" => 8,
                _ => 0,
            },
            Self::Prefix(p) if p.operator.name == "-" => 9,
            Self::Prefix(_) => 10,
            Self::Postfix(_) | Self::Primary(_) => 11,
        }
    }

    fn fmt_operand(&self, f: &mut std::fmt::Formatter<'_>, minimum: u8) -> std::fmt::Result {
        if self.precedence() < minimum {
            write!(f, "({self})")
        } else {
            write!(f, "{self}")
        }
    }
}

impl Display for PaxExpression {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PaxExpression::Primary(p) => write!(f, "{}", p),
            PaxExpression::Prefix(p) => {
                write!(f, "{}", p.operator.name)?;
                p.rhs.fmt_operand(f, self.precedence())
            }
            PaxExpression::Infix(i) => {
                let right_associative = i.operator.name == "^";
                i.lhs
                    .fmt_operand(f, self.precedence() + u8::from(right_associative))?;
                write!(f, " {} ", i.operator.name)?;
                i.rhs
                    .fmt_operand(f, self.precedence() + u8::from(!right_associative))
            }
            PaxExpression::Postfix(p) => write!(f, "{}{}", p.lhs, p.operator.name),
            PaxExpression::Ternary(t) => {
                t.condition.fmt_operand(f, self.precedence() + 1)?;
                write!(f, " ? {} : {}", t.then_branch, t.else_branch)
            }
            PaxExpression::NullCoalesce(n) => {
                n.lhs.fmt_operand(f, self.precedence() + 1)?;
                write!(f, " ?? ")?;
                n.rhs.fmt_operand(f, self.precedence())
            }
        }
    }
}

#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
/// Primary expression forms: literals, symbols, object/list/tuple literals, calls, and ranges.
pub enum PaxPrimary {
    Literal(PaxValue),
    Grouped(Box<PaxExpression>, Option<PaxUnit>),
    Identifier(PaxIdentifier, Vec<PaxAccessor>),
    Object(Vec<(String, PaxExpression)>),
    FunctionOrEnum(String, String, Vec<PaxExpression>),
    Range(PaxExpression, PaxExpression),
    Tuple(Vec<PaxExpression>),
    List(Vec<PaxExpression>),
}

impl Display for PaxPrimary {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PaxPrimary::Literal(l) => write!(f, "{}", l),
            PaxPrimary::Grouped(e, u) => {
                if let Some(u) = u {
                    write!(
                        f,
                        "({}){}",
                        e,
                        match u {
                            PaxUnit::Percent => "%",
                            PaxUnit::Pixels => "px",
                            PaxUnit::Radians => "rad",
                            PaxUnit::Degrees => "deg",
                            PaxUnit::Milliseconds => "ms",
                            PaxUnit::Seconds => "s",
                            PaxUnit::Frames => "f",
                        }
                    )
                } else {
                    write!(f, "({})", e)
                }
            }
            PaxPrimary::Identifier(i, a) => {
                write!(f, "{}", i.name)?;
                for accessor in a {
                    match accessor {
                        PaxAccessor::Tuple(i) => write!(f, ".{}", i)?,
                        PaxAccessor::List(e) => write!(f, "[{}]", e)?,
                        PaxAccessor::Struct(s) => write!(f, ".{}", s)?,
                    }
                }
                Ok(())
            }
            PaxPrimary::Object(o) => {
                write!(f, "{{")?;
                let mut o = o.iter().collect::<Vec<_>>();
                o.sort_by(|a, b| a.0.cmp(&b.0));
                for (i, (key, val)) in o.iter().enumerate() {
                    write!(f, "{}: {}", key, val)?;
                    if i != o.len() - 1 {
                        write!(f, ", ")?;
                    }
                }
                write!(f, "}}")?;
                Ok(())
            }
            PaxPrimary::FunctionOrEnum(name, e, a) => {
                if (name == "Fill" || name == "Stroke") && e == "__layer" && a.len() == 1 {
                    return write!(f, "{} {}", name, a[0]);
                }
                if name == "Color" {
                    write!(f, "{}", e)?;
                } else {
                    write!(f, "{}::{}", name, e)?;
                }
                if !a.is_empty() {
                    write!(f, "(")?;
                    for (i, arg) in a.iter().enumerate() {
                        write!(f, "{}", arg)?;
                        if i != a.len() - 1 {
                            write!(f, ", ")?;
                        }
                    }
                    write!(f, ")")?;
                }
                Ok(())
            }
            PaxPrimary::Range(s, e) => write!(f, "{}..{}", s, e),
            PaxPrimary::Tuple(t) => {
                write!(f, "(")?;
                for (i, e) in t.iter().enumerate() {
                    write!(f, "{}", e)?;
                    if i != t.len() - 1 {
                        write!(f, ", ")?;
                    }
                }
                write!(f, ")")?;
                Ok(())
            }
            PaxPrimary::List(l) => {
                write!(f, "[")?;
                for (i, e) in l.iter().enumerate() {
                    write!(f, "{}", e)?;
                    if i != l.len() - 1 {
                        write!(f, ", ")?;
                    }
                }
                write!(f, "]")?;
                Ok(())
            }
        }
    }
}

impl Default for PaxPrimary {
    fn default() -> Self {
        Self::Literal(PaxValue::default())
    }
}

#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
/// Unit suffix attached to a grouped numeric expression.
pub enum PaxUnit {
    Percent,
    Pixels,
    Radians,
    Degrees,
    Milliseconds,
    Seconds,
    Frames,
}

#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
/// Access path applied after an identifier, such as `.field`, `.0`, or `[index]`.
pub enum PaxAccessor {
    Tuple(usize),
    List(PaxExpression),
    Struct(String),
}

#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
/// Prefix operation such as numeric negation or boolean not.
pub struct PaxPrefix {
    operator: PaxOperator,
    rhs: Box<PaxExpression>,
}

impl PaxPrefix {
    /// Name of the prefix operator.
    pub fn operator_name(&self) -> &str {
        &self.operator.name
    }

    /// Right-hand expression.
    pub fn rhs(&self) -> &PaxExpression {
        &self.rhs
    }
}

#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
/// Binary infix operation with left and right expression operands.
pub struct PaxInfix {
    operator: PaxOperator,
    lhs: Box<PaxExpression>,
    rhs: Box<PaxExpression>,
}

impl PaxInfix {
    /// Name of the infix operator.
    pub fn operator_name(&self) -> &str {
        &self.operator.name
    }

    /// Left-hand expression.
    pub fn lhs(&self) -> &PaxExpression {
        &self.lhs
    }

    /// Right-hand expression.
    pub fn rhs(&self) -> &PaxExpression {
        &self.rhs
    }
}

#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
/// Postfix operation node.
pub struct PaxPostfix {
    operator: PaxOperator,
    lhs: Box<PaxExpression>,
}

impl PaxPostfix {
    /// Name of the postfix operator.
    pub fn operator_name(&self) -> &str {
        &self.operator.name
    }

    /// Left-hand expression.
    pub fn lhs(&self) -> &PaxExpression {
        &self.lhs
    }
}

#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
/// Conditional expression with a boolean condition and selected true/false branch.
pub struct PaxTernary {
    condition: Box<PaxExpression>,
    then_branch: Box<PaxExpression>,
    else_branch: Box<PaxExpression>,
}

#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
/// Short-circuiting fallback expression. `Some(value) ?? fallback` evaluates to
/// `value`, `None ?? fallback` evaluates the fallback, and non-option left
/// operands pass through unchanged.
pub struct PaxNullCoalesce {
    lhs: Box<PaxExpression>,
    rhs: Box<PaxExpression>,
}

impl PaxTernary {
    /// Condition expression.
    pub fn condition(&self) -> &PaxExpression {
        &self.condition
    }

    /// Expression evaluated when the condition is true.
    pub fn then_branch(&self) -> &PaxExpression {
        &self.then_branch
    }

    /// Expression evaluated when the condition is false.
    pub fn else_branch(&self) -> &PaxExpression {
        &self.else_branch
    }
}

impl PaxNullCoalesce {
    /// Left-hand expression.
    pub fn lhs(&self) -> &PaxExpression {
        &self.lhs
    }

    /// Right-hand fallback expression.
    pub fn rhs(&self) -> &PaxExpression {
        &self.rhs
    }
}

#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
/// Parsed operator token, stored by display name.
pub struct PaxOperator {
    name: String,
}

impl PaxOperator {
    /// Construct an operator from its source spelling.
    pub fn new(name: impl Into<String>) -> Self {
        Self { name: name.into() }
    }

    /// Source spelling for this operator.
    pub fn name(&self) -> &str {
        &self.name
    }
}

#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
/// Symbol reference in a PAXEL expression.
pub struct PaxIdentifier {
    pub name: String,
}

impl Display for PaxIdentifier {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.name)
    }
}

impl PaxIdentifier {
    /// Construct an identifier from its source spelling.
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
        }
    }
}

/// Parse a pax expression into a computable AST
#[cfg(feature = "parser")]
pub fn parse_pax_expression(expr: &str) -> Result<PaxExpression, String> {
    let parsed_expr = parse_pax_pairs(Rule::expression_body, expr)
        .map_err(|e| format!("Failed to parse expression: {}", e))?;
    let pratt_parser = get_pax_pratt_parser();
    recurse_pratt_parse(parsed_expr, &pratt_parser)
}

/// Parse an already-produced pest pair into a PAXEL expression AST.
#[cfg(feature = "parser")]
pub fn parse_pax_expression_from_pair(expr: Pair<Rule>) -> Result<PaxExpression, String> {
    let pratt_parser = get_pax_pratt_parser();
    recurse_pratt_parse(Pairs::single(expr), &pratt_parser)
}

#[cfg(feature = "parser")]
fn recurse_pratt_parse(
    expr: Pairs<Rule>,
    pratt_parser: &PrattParser<Rule>,
) -> Result<PaxExpression, String> {
    pratt_parser
        .map_primary(move |primary| match primary.as_rule() {
            Rule::xo_literal => {
                let inner = primary.into_inner().next().unwrap();
                match inner.as_rule() {
                    Rule::literal_value => {
                        let pax_value = from_pax_ast(inner)
                            .map_err(|e| format!("Failed to parse literal value: {}", e))?;
                        let value = PaxPrimary::Literal(pax_value);
                        let exp = PaxExpression::Primary(Box::new(value));
                        Ok(exp)
                    }
                    Rule::literal_tuple_access => {
                        let mut inner = inner.into_inner();
                        let ident = inner.next().unwrap().as_str().trim().to_string();
                        let index = inner.next().unwrap().as_str().parse::<usize>().unwrap();
                        let value = PaxPrimary::Identifier(
                            PaxIdentifier { name: ident },
                            vec![PaxAccessor::Tuple(index)],
                        );
                        let exp = PaxExpression::Primary(Box::new(value));
                        Ok(exp)
                    }
                    Rule::literal_list_access => {
                        let mut inner = inner.into_inner();
                        let ident = inner.next().unwrap().as_str().trim().to_string();
                        let index = inner.next().unwrap().as_str();
                        let index = parse_pax_expression(index)?;
                        let value = PaxPrimary::Identifier(
                            PaxIdentifier { name: ident },
                            vec![PaxAccessor::List(index)],
                        );
                        let exp = PaxExpression::Primary(Box::new(value));
                        Ok(exp)
                    }
                    _ => {
                        return Err(format!("Unexpected rule: {:?}", inner.as_rule()));
                    }
                }
            }
            Rule::expression_body | Rule::expression_binary => {
                recurse_pratt_parse(primary.into_inner(), pratt_parser)
            }
            Rule::expression_coalesce => {
                let mut inner = primary.into_inner();
                let lhs = recurse_pratt_parse(Pairs::single(inner.next().unwrap()), pratt_parser)?;
                if inner.next().is_some() {
                    let rhs =
                        recurse_pratt_parse(Pairs::single(inner.next().unwrap()), pratt_parser)?;
                    Ok(PaxExpression::NullCoalesce(Box::new(PaxNullCoalesce {
                        lhs: Box::new(lhs),
                        rhs: Box::new(rhs),
                    })))
                } else {
                    Ok(lhs)
                }
            }
            Rule::expression_ternary => {
                let mut inner = primary.into_inner();
                let condition =
                    recurse_pratt_parse(Pairs::single(inner.next().unwrap()), pratt_parser)?;
                if inner.next().is_some() {
                    let then_branch =
                        recurse_pratt_parse(Pairs::single(inner.next().unwrap()), pratt_parser)?;
                    inner.next();
                    let else_branch =
                        recurse_pratt_parse(Pairs::single(inner.next().unwrap()), pratt_parser)?;
                    Ok(PaxExpression::Ternary(Box::new(PaxTernary {
                        condition: Box::new(condition),
                        then_branch: Box::new(then_branch),
                        else_branch: Box::new(else_branch),
                    })))
                } else {
                    Ok(condition)
                }
            }
            Rule::expression_grouped => {
                let mut inner = primary.clone().into_inner();
                let expr = inner.next().unwrap();
                let expr_val = recurse_pratt_parse(expr.into_inner(), pratt_parser)?;
                let ret: Result<PaxExpression, String> = if let Some(unit) = inner.next() {
                    let unit = unit.as_str().trim().trim_start_matches(')');
                    match unit {
                        "%" => Ok(PaxExpression::Primary(Box::new(PaxPrimary::Grouped(
                            Box::new(expr_val),
                            Some(PaxUnit::Percent),
                        )))),
                        "px" => Ok(PaxExpression::Primary(Box::new(PaxPrimary::Grouped(
                            Box::new(expr_val),
                            Some(PaxUnit::Pixels),
                        )))),
                        "rad" => Ok(PaxExpression::Primary(Box::new(PaxPrimary::Grouped(
                            Box::new(expr_val),
                            Some(PaxUnit::Radians),
                        )))),
                        "deg" => Ok(PaxExpression::Primary(Box::new(PaxPrimary::Grouped(
                            Box::new(expr_val),
                            Some(PaxUnit::Degrees),
                        )))),
                        "ms" => Ok(PaxExpression::Primary(Box::new(PaxPrimary::Grouped(
                            Box::new(expr_val),
                            Some(PaxUnit::Milliseconds),
                        )))),
                        "s" => Ok(PaxExpression::Primary(Box::new(PaxPrimary::Grouped(
                            Box::new(expr_val),
                            Some(PaxUnit::Seconds),
                        )))),
                        "f" => Ok(PaxExpression::Primary(Box::new(PaxPrimary::Grouped(
                            Box::new(expr_val),
                            Some(PaxUnit::Frames),
                        )))),
                        _ => Err(format!("Unsupported unit: {}", unit)),
                    }
                } else {
                    Ok(PaxExpression::Primary(Box::new(PaxPrimary::Grouped(
                        Box::new(expr_val),
                        None,
                    ))))
                };
                ret
            }
            Rule::xo_enum_or_function_call => {
                let mut inner = primary.into_inner();
                while inner.len() > 3 {
                    inner.next();
                }
                let scope = inner.next().unwrap().as_str().trim().to_string();
                let function_name = inner.next().unwrap().as_str().trim().to_string();

                let args = if let Some(args) = inner.next() {
                    args.into_inner()
                        .map(|a| recurse_pratt_parse(a.into_inner(), pratt_parser))
                        .collect::<Result<Vec<PaxExpression>, String>>()?
                } else {
                    vec![]
                };
                let exp = PaxExpression::Primary(Box::new(PaxPrimary::FunctionOrEnum(
                    scope,
                    function_name,
                    args,
                )));
                Ok(exp)
            }
            Rule::xo_color_space_func => {
                let func = primary.as_str().trim().split("(").next().unwrap();
                let inner = primary.into_inner();
                let args = inner
                    .map(|a| recurse_pratt_parse(a.into_inner(), pratt_parser))
                    .collect::<Result<Vec<PaxExpression>, String>>()?;
                let exp = PaxExpression::Primary(Box::new(PaxPrimary::FunctionOrEnum(
                    "Color".to_string(),
                    func.to_string(),
                    args,
                )));
                Ok(exp)
            }
            Rule::xo_object => {
                let mut inner = primary.into_inner();
                let type_name = if inner
                    .peek()
                    .is_some_and(|v| v.as_rule() == Rule::identifier)
                {
                    Some(inner.next().unwrap().as_str().to_string())
                } else {
                    None
                };
                let mut obj = vec![];
                for pair in inner {
                    let mut pair = pair.into_inner();
                    // settings_key = { identifier ~ (":" | "=") }
                    let key = pair
                        .next()
                        .unwrap()
                        .as_str()
                        .trim()
                        .trim_end_matches(':')
                        .trim_end_matches('=')
                        .to_string();
                    let value = pair.next().unwrap();
                    let value = recurse_pratt_parse(value.into_inner(), pratt_parser)?;
                    obj.push((key, value));
                }
                let value = if let Some(name) =
                    type_name.filter(|name| name == "Fill" || name == "Stroke")
                {
                    PaxPrimary::FunctionOrEnum(
                        name,
                        "__layer".into(),
                        vec![PaxExpression::Primary(Box::new(PaxPrimary::Object(obj)))],
                    )
                } else {
                    PaxPrimary::Object(obj)
                };
                let exp = PaxExpression::Primary(Box::new(value));
                Ok(exp)
            }
            Rule::xo_range => {
                let mut inner = primary.into_inner();
                let start_rule = Pairs::single(inner.next().unwrap());
                let start = recurse_pratt_parse(start_rule, pratt_parser)?;
                // xo_range_exclusive
                inner.next();
                let end_rule = Pairs::single(inner.next().unwrap());
                let end = recurse_pratt_parse(end_rule, pratt_parser)?;
                let value = PaxPrimary::Range(start, end);
                let exp = PaxExpression::Primary(Box::new(value));
                Ok(exp)
            }
            Rule::xo_tuple => {
                let inner = primary.into_inner();
                let tuple = inner
                    .map(|e| recurse_pratt_parse(e.into_inner(), pratt_parser))
                    .collect::<Result<Vec<PaxExpression>, String>>()?;
                let value = PaxPrimary::Tuple(tuple);
                let exp = PaxExpression::Primary(Box::new(value));
                Ok(exp)
            }
            Rule::xo_list => {
                let inner = primary.into_inner();
                let list = inner
                    .map(|e| recurse_pratt_parse(e.into_inner(), pratt_parser))
                    .collect::<Result<Vec<PaxExpression>, String>>()?;
                let value = PaxPrimary::List(list);
                let exp = PaxExpression::Primary(Box::new(value));
                Ok(exp)
            }
            Rule::xo_symbol => {
                let builtins_prefix = if primary.as_str().starts_with("$") {
                    "$"
                } else {
                    ""
                }
                .to_string();

                let mut symbols = primary.into_inner();

                // skip self or this
                let peek = symbols.peek().unwrap();
                if peek.as_str().trim() == "self" || peek.as_str().trim() == "this" {
                    symbols.next();
                }

                let identifier = builtins_prefix + symbols.next().unwrap().as_str().trim();
                let mut accessors = vec![];
                for symbol in symbols {
                    let accessor = match symbol.as_rule() {
                        // list access
                        Rule::expression_body => {
                            let expr = recurse_pratt_parse(symbol.into_inner(), pratt_parser)?;
                            PaxAccessor::List(expr)
                        }
                        // field access .field
                        Rule::identifier => {
                            let field = symbol.as_str().trim().to_string();
                            PaxAccessor::Struct(field)
                        }
                        _ => {
                            return Err(format!("Unexpected rule: {:?}", symbol.as_rule()));
                        }
                    };
                    accessors.push(accessor);
                }
                let value = PaxPrimary::Identifier(PaxIdentifier { name: identifier }, accessors);
                let exp = PaxExpression::Primary(Box::new(value));
                Ok(exp)
            }
            _ => {
                return Err(format!(
                    "Unexpected rule: {:?}, str: {} ",
                    primary.as_rule(),
                    primary.as_str()
                ));
            }
        })
        .map_prefix(|op, rhs| match op.as_rule() {
            _ => {
                let a = PaxOperator {
                    name: op.as_str().trim().to_string(),
                };
                let r = rhs?;
                let exp = PaxExpression::Prefix(Box::new(PaxPrefix {
                    operator: a,
                    rhs: Box::new(r),
                }));
                Ok(exp)
            }
        })
        .map_postfix(|lhs, op| match op.as_rule() {
            _ => {
                let a = PaxOperator {
                    name: op.as_str().trim().to_string(),
                };
                let l = lhs?;
                let exp = PaxExpression::Postfix(Box::new(PaxPostfix {
                    operator: a,
                    lhs: Box::new(l),
                }));
                Ok(exp)
            }
        })
        .map_infix(|lhs, op, rhs| match op.as_rule() {
            _ => {
                let a = PaxOperator {
                    name: op.as_str().trim().to_string(),
                };
                let l = lhs?;
                let r = rhs?;
                let exp = PaxExpression::Infix(Box::new(PaxInfix {
                    operator: a,
                    lhs: Box::new(l),
                    rhs: Box::new(r),
                }));
                Ok(exp)
            }
        })
        .parse(expr)
}

/// Compute a pax expression to a PaxValue
#[cfg(feature = "parser")]
pub fn compute_paxel(expr: &str, idr: Rc<dyn IdentifierResolver>) -> Result<PaxValue, String> {
    let expr = parse_pax_expression(expr)?;
    expr.compute(idr)
}