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
use delegate_attr::delegate;
use serde::{Deserialize, Serialize};

pub type Atom = crate::crulst::CrulzAtom;

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum GroupType {
    Strict,
    Loose,
    Dissolving,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum ASTNode {
    NullNode,

    /// Constant: is_non_space, data
    Constant(bool, Atom),

    /// Argument: indirection (= (count of '$'s) - 1), index
    /// no given index means something like '$$.'
    Argument {
        indirection: usize,
        index: Option<usize>,
    },

    /// Grouped: type, elems
    /// loose groups are created while replacing patterns
    Grouped(GroupType, Vec<ASTNode>),

    /// CmdEval: cmd, args
    CmdEval(Vec<ASTNode>, CmdEvalArgs),
}

use ASTNode::*;
pub type VAN = Vec<ASTNode>;

impl std::default::Default for ASTNode {
    #[inline(always)]
    fn default() -> Self {
        NullNode
    }
}

impl ASTNode {
    #[inline(always)]
    pub(crate) fn is_space(&self) -> bool {
        match self {
            NullNode | Constant(false, _) => true,
            _ => false,
        }
    }

    #[inline(always)]
    pub(crate) fn as_constant(&self) -> Option<&Atom> {
        match self {
            Constant(_, ref x) => Some(x),
            _ => None,
        }
    }

    pub(crate) fn conv_to_constant(&self) -> Option<Atom> {
        match self {
            ASTNode::Constant(_, x) => Some(x.clone()),
            ASTNode::Grouped(gt, x) if *gt != GroupType::Strict => {
                let mut impc = x.iter().map(ASTNode::conv_to_constant);
                if x.len() == 1 {
                    impc.next().unwrap()
                } else {
                    impc.try_fold(String::new(), |acc, i| i.as_ref().map(|j| acc + j))
                        .map(<_>::into)
                }
            }
            _ => None,
        }
    }
}

pub trait LiftAST {
    type LiftT: LiftAST;

    // lift the AST one level up (ASTNode -> VAN || VAN -> ASTNode),
    // used as helper for MangleAST::simplify_inplace and others
    // to convert to the appropriate datatype
    fn lift_ast(self) -> Self::LiftT;
}

impl LiftAST for ASTNode {
    type LiftT = VAN;

    #[inline(always)]
    fn lift_ast(self) -> Self::LiftT {
        vec![self]
    }
}

impl LiftAST for VAN {
    type LiftT = ASTNode;

    #[inline(always)]
    fn lift_ast(self) -> Self::LiftT {
        ASTNode::Grouped(GroupType::Dissolving, self)
    }
}

#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct CmdEvalArgs(pub VAN);

impl std::iter::IntoIterator for CmdEvalArgs {
    type Item = ASTNode;
    type IntoIter = std::vec::IntoIter<ASTNode>;

    #[inline(always)]
    fn into_iter(self) -> std::vec::IntoIter<ASTNode> {
        self.0.into_iter()
    }
}

impl std::iter::FromIterator<ASTNode> for CmdEvalArgs {
    #[inline(always)]
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = ASTNode>,
    {
        CmdEvalArgs(Vec::from_iter(iter))
    }
}

impl CmdEvalArgs {
    /// constructs `CmdEvalArgs` from a `VAN` with white-space as arguments delimiter
    pub fn from_wsdelim(args: VAN) -> Self {
        use crate::mangle_ast::MangleAST;
        use itertools::Itertools;
        args.into_iter()
            .group_by(ASTNode::is_space)
            .into_iter()
            .filter(|x| !x.0)
            .map(|x| x.1.collect::<VAN>().lift_ast().simplify())
            .collect()
    }
}

#[delegate(self.0)]
impl CmdEvalArgs {
    pub fn iter(&self) -> std::slice::Iter<ASTNode> { }
    pub fn iter_mut(&mut self) -> std::slice::IterMut<ASTNode> { }
    pub fn len(&self) -> usize { }
    pub fn is_empty(&self) -> bool { }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_args2unspaced() {
        use ASTNode::*;
        assert_eq!(
            CmdEvalArgs::from_wsdelim(vec![
                Constant(true, "a".into()),
                Constant(false, "a".into()),
                Constant(true, "a".into()),
                Constant(true, "a".into()),
                Constant(false, "a".into())
            ]),
            CmdEvalArgs(vec![
                Constant(true, "a".into()),
                Constant(true, "aa".into())
            ])
        );
    }
}