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
use crate::evaluate::{evaluate_baseline_expr, Scope};
use crate::parser::{hir, hir::SyntaxType, parse_command, CallNode, Spanned};
use crate::prelude::*;
use derive_new::new;
use getset::Getters;
use indexmap::IndexMap;
use log::trace;
use serde::{Deserialize, Serialize};
use std::fmt;

#[allow(unused)]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum NamedType {
    Switch,
    Mandatory(SyntaxType),
    Optional(SyntaxType),
}

#[allow(unused)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PositionalType {
    Mandatory(String, SyntaxType),
    Optional(String, SyntaxType),
}

impl PositionalType {
    pub fn mandatory(name: &str, ty: SyntaxType) -> PositionalType {
        PositionalType::Mandatory(name.to_string(), ty)
    }

    pub fn mandatory_any(name: &str) -> PositionalType {
        PositionalType::Mandatory(name.to_string(), SyntaxType::Any)
    }

    pub fn mandatory_block(name: &str) -> PositionalType {
        PositionalType::Mandatory(name.to_string(), SyntaxType::Block)
    }

    pub fn optional(name: &str, ty: SyntaxType) -> PositionalType {
        PositionalType::Optional(name.to_string(), ty)
    }

    pub fn optional_any(name: &str) -> PositionalType {
        PositionalType::Optional(name.to_string(), SyntaxType::Any)
    }

    #[allow(unused)]
    crate fn to_coerce_hint(&self) -> Option<SyntaxType> {
        match self {
            PositionalType::Mandatory(_, SyntaxType::Block)
            | PositionalType::Optional(_, SyntaxType::Block) => Some(SyntaxType::Block),
            _ => None,
        }
    }

    crate fn name(&self) -> &str {
        match self {
            PositionalType::Mandatory(s, _) => s,
            PositionalType::Optional(s, _) => s,
        }
    }

    crate fn syntax_type(&self) -> SyntaxType {
        match *self {
            PositionalType::Mandatory(_, t) => t,
            PositionalType::Optional(_, t) => t,
        }
    }
}

#[derive(Debug, Getters, Serialize, Deserialize, Clone)]
#[get = "crate"]
pub struct CommandConfig {
    pub name: String,
    pub positional: Vec<PositionalType>,
    pub rest_positional: bool,
    pub named: IndexMap<String, NamedType>,
    pub is_filter: bool,
    pub is_sink: bool,
}

#[derive(Debug, Default, new, Serialize, Deserialize)]
pub struct Args {
    pub positional: Option<Vec<Spanned<Value>>>,
    pub named: Option<IndexMap<String, Spanned<Value>>>,
}

#[derive(new)]
pub struct DebugPositional<'a> {
    positional: &'a Option<Vec<Spanned<Value>>>,
}

impl fmt::Debug for DebugPositional<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self.positional {
            None => write!(f, "None"),
            Some(positional) => f
                .debug_list()
                .entries(positional.iter().map(|p| p.debug()))
                .finish(),
        }
    }
}

#[derive(new)]
pub struct DebugNamed<'a> {
    named: &'a Option<IndexMap<String, Spanned<Value>>>,
}

impl fmt::Debug for DebugNamed<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self.named {
            None => write!(f, "None"),
            Some(named) => f
                .debug_map()
                .entries(named.iter().map(|(k, v)| (k, v.debug())))
                .finish(),
        }
    }
}

pub struct DebugArgs<'a> {
    args: &'a Args,
}

impl fmt::Debug for DebugArgs<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut s = f.debug_struct("Args");

        s.field("positional", &DebugPositional::new(&self.args.positional));
        s.field("named", &DebugNamed::new(&self.args.named));

        s.finish()
    }
}

impl Args {
    pub fn debug(&'a self) -> DebugArgs<'a> {
        DebugArgs { args: self }
    }

    pub fn nth(&self, pos: usize) -> Option<&Spanned<Value>> {
        match &self.positional {
            None => None,
            Some(array) => array.iter().nth(pos),
        }
    }

    pub fn expect_nth(&self, pos: usize) -> Result<&Spanned<Value>, ShellError> {
        match &self.positional {
            None => Err(ShellError::unimplemented("Better error: expect_nth")),
            Some(array) => match array.iter().nth(pos) {
                None => Err(ShellError::unimplemented("Better error: expect_nth")),
                Some(item) => Ok(item),
            },
        }
    }

    pub fn len(&self) -> usize {
        match &self.positional {
            None => 0,
            Some(array) => array.len(),
        }
    }

    pub fn has(&self, name: &str) -> bool {
        match &self.named {
            None => false,
            Some(named) => named.contains_key(name),
        }
    }

    pub fn get(&self, name: &str) -> Option<&Spanned<Value>> {
        match &self.named {
            None => None,
            Some(named) => named.get(name),
        }
    }

    pub fn positional_iter(&'a self) -> PositionalIter<'a> {
        match &self.positional {
            None => PositionalIter::Empty,
            Some(v) => {
                let iter = v.iter();
                PositionalIter::Array(iter)
            }
        }
    }
}

pub enum PositionalIter<'a> {
    Empty,
    Array(std::slice::Iter<'a, Spanned<Value>>),
}

impl Iterator for PositionalIter<'a> {
    type Item = &'a Spanned<Value>;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            PositionalIter::Empty => None,
            PositionalIter::Array(iter) => iter.next(),
        }
    }
}

impl CommandConfig {
    crate fn evaluate_args(
        &self,
        call: &Spanned<CallNode>,
        registry: &dyn CommandRegistry,
        scope: &Scope,
        source: &Text,
    ) -> Result<Args, ShellError> {
        let args = parse_command(self, registry, call, source)?;

        trace!("parsed args: {:?}", args);

        evaluate_args(args, registry, scope, source)

        // let mut positional: Vec<Spanned<Value>> = vec![];
        // let mut named: IndexMap<String, Value> = IndexMap::default();

        // let mut args: Vec<TokenNode> = args.cloned().collect();

        // for (key, ty) in self.named.iter() {
        //     let index = args.iter().position(|a| a.is_flag(&key, source));

        //     match (index, ty) {
        //         (Some(i), NamedType::Switch) => {
        //             args.remove(i);
        //             named.insert(key.clone(), Value::boolean(true));
        //         }

        //         (None, NamedType::Switch) => {}

        //         (Some(i), NamedType::Optional(v)) => {
        //             args.remove(i);
        //             named.insert(key.clone(), extract_named(&mut args, i, v)?);
        //         }

        //         (None, NamedType::Optional(_)) => {}

        //         (Some(i), NamedType::Mandatory(v)) => {
        //             args.remove(i);
        //             named.insert(key.clone(), extract_named(&mut args, i, v)?);
        //         }

        //         (None, NamedType::Mandatory(_)) => {
        //             return Err(ShellError::string(&format!(
        //                 "Expected mandatory argument {}, but it was missing",
        //                 key
        //             )))
        //         }
        //     }
        // }

        // let mut args = args.into_iter();

        // for param in &self.mandatory_positional {
        //     let arg = args.next();

        //     let value = match arg {
        //         None => {
        //             return Err(ShellError::string(format!(
        //                 "expected mandatory positional argument {}",
        //                 param.name()
        //             )))
        //         }

        //         Some(arg) => param.evaluate(arg.clone(), scope, source)?,
        //     };

        //     positional.push(value);
        // }

        // if self.rest_positional {
        //     let rest: Result<Vec<Spanned<Value>>, _> = args
        //         .map(|i| evaluate_baseline_expr(&i, &Scope::empty(), source))
        //         .collect();
        //     positional.extend(rest?);
        // } else {
        //     let rest: Vec<TokenNode> = args.collect();

        //     if rest.len() > 0 {
        //         return Err(ShellError::string(&format!(
        //             "Too many arguments, extras: {:?}",
        //             rest
        //         )));
        //     }
        // }

        // Ok(Args { positional, named })
    }

    #[allow(unused)]
    crate fn signature(&self) -> String {
        format!("TODO")
    }
}

fn evaluate_args(
    args: hir::Call,
    registry: &dyn CommandRegistry,
    scope: &Scope,
    source: &Text,
) -> Result<Args, ShellError> {
    let positional: Result<Option<Vec<_>>, _> = args
        .positional()
        .as_ref()
        .map(|p| {
            p.iter()
                .map(|e| evaluate_baseline_expr(e, &(), scope, source))
                .collect()
        })
        .transpose();

    let positional = positional?;

    let named: Result<Option<IndexMap<String, Spanned<Value>>>, ShellError> = args
        .named()
        .as_ref()
        .map(|n| {
            let mut results = IndexMap::new();

            for (name, value) in n.named.iter() {
                match value {
                    hir::named::NamedValue::PresentSwitch(span) => {
                        results.insert(
                            name.clone(),
                            Spanned::from_item(Value::boolean(true), *span),
                        );
                    }
                    hir::named::NamedValue::Value(expr) => {
                        results.insert(
                            name.clone(),
                            evaluate_baseline_expr(expr, registry, scope, source)?,
                        );
                    }

                    _ => {}
                };
            }

            Ok(results)
        })
        .transpose();

    let named = named?;

    Ok(Args::new(positional, named))
}

pub trait CommandRegistry {
    fn get(&self, name: &str) -> Option<CommandConfig>;
}

impl CommandRegistry for () {
    fn get(&self, _name: &str) -> Option<CommandConfig> {
        None
    }
}