syn_solidity/yul/expr/
mod.rs

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
use crate::{Lit, Spanned, YulPath};
use proc_macro2::Span;
use std::fmt;
use syn::{
    parse::{discouraged::Speculative, Parse, ParseStream, Result},
    token::Paren,
};

mod fn_call;
pub use fn_call::{YulFnCall, YulFnType};

/// A Yul expression.
///
/// Solidity Reference:
/// <https://docs.soliditylang.org/en/latest/grammar.html#a4.SolidityParser.yulExpression>
#[derive(Clone)]
pub enum YulExpr {
    Path(YulPath),
    Call(YulFnCall),
    Literal(Lit),
}

impl Parse for YulExpr {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        if input.peek2(Paren) {
            return input.parse().map(Self::Call);
        }

        let speculative_parse = input.fork();

        if let Ok(lit) = speculative_parse.parse::<Lit>() {
            input.advance_to(&speculative_parse);
            return Ok(Self::Literal(lit));
        }

        input.parse().map(Self::Path)
    }
}

impl Spanned for YulExpr {
    fn span(&self) -> Span {
        match self {
            Self::Path(path) => path.span(),
            Self::Call(call) => call.span(),
            Self::Literal(lit) => lit.span(),
        }
    }

    fn set_span(&mut self, span: Span) {
        match self {
            Self::Path(path) => path.set_span(span),
            Self::Call(call) => call.set_span(span),
            Self::Literal(lit) => lit.set_span(span),
        }
    }
}

impl fmt::Debug for YulExpr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("YulExpr::")?;
        match self {
            Self::Path(path) => path.fmt(f),
            Self::Call(call) => call.fmt(f),
            Self::Literal(lit) => lit.fmt(f),
        }
    }
}