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
//! Abstract syntax tree

use std::fmt::Display;
use std::fmt::Error as FormatError;
use std::fmt::{Formatter};
use std::collections::BTreeMap;

use super::tokenizer::Pos;
use super::errors::{Error, ErrorCollector};
use super::parser::Node as P;
use super::parser::{Node, Document};
use super::tokenizer::TokenType as T;
use self::Ast::*;
use self::NullKind::*;
use self::ScalarKind::*;
use self::Tag::*;
use options::{Options, Include, DoInclude};


/// Kind of scalar value
///
/// This is usually needed to distinguish numeric value `123` from
/// string value `"123"` (usually quire doesn't care on decode stage,
/// but it might be useful for some cases)
#[derive(Debug)]
pub enum ScalarKind {
    /// Scalar value
    Plain,
    Quoted,
}

/// Kind of null value
#[derive(Debug)]
pub enum NullKind {
    /// Implicit null value, like in `a:`, there is implicit null value of a
    Implicit,
    /// Explicit null, specified as `null`, or `~` in yaml
    Explicit,
}

/// Yaml tag
#[derive(Debug)]
pub enum Tag {
    /// Value without any tag, it's derived from the value kind
    NonSpecific,
    /// Local tag `!tag` (single exclamation mark)
    LocalTag(String),
    /// Global tag, i.e. either a prefix defined in directives or a full
    /// url tag
    ///
    /// Largely unsupported in the current code
    GlobalTag(String),
}

impl Tag {
    pub fn is_specific(&self) -> bool {
        match *self {
            NonSpecific => false,
            _ => true,
        }
    }
}

/// Yaml node
#[derive(Debug)]
pub enum Ast {
    /// Mapping node
    Map(Pos, Tag, BTreeMap<String, Ast>),
    /// Sequence node
    Seq(Pos, Tag, Vec<Ast>),
    /// Scalar node (except null)
    Scalar(Pos, Tag, ScalarKind, String),
    /// Null node
    Null(Pos, Tag, NullKind),
}

impl Display for Ast {
    fn fmt(&self, fmt: &mut Formatter) -> Result<(), FormatError> {
        match *self {
            Map(_, _, _) => write!(fmt, "Map"),
            Seq(_, _, _) => write!(fmt, "Seq"),
            Scalar(_, _, _, _) => write!(fmt, "Scalar"),
            Null(_, _, _) => write!(fmt, "Null"),
        }
    }
}

impl Ast {
    pub fn pos(&self) -> Pos {
        match *self {
            Map(ref pos, _, _) => pos.clone(),
            Seq(ref pos, _, _) => pos.clone(),
            Scalar(ref pos, _, _, _) => pos.clone(),
            Null(ref pos, _, _) => pos.clone(),
        }
    }
    pub fn tag<'x>(&'x self) -> &'x Tag {
        match *self {
            Map(_, ref tag, _) => tag,
            Seq(_, ref tag, _) => tag,
            Scalar(_, ref tag, _, _) => tag,
            Null(_, ref tag, _) => tag,
        }
    }
    pub fn with_tag(self, tag: Tag) -> Ast {
        match self {
            Map(pos, _, children) => Map(pos, tag, children),
            Seq(pos,  _, children) => Seq(pos, tag, children),
            Scalar(pos, _, style, value) => Scalar(pos, tag, style, value),
            Null(pos, _, kind) => Null(pos, tag, kind),
        }
    }
    /// A special null value that is used when node inside is errorneous
    pub fn void(pos: &Pos) -> Ast {
        Ast::Null(pos.clone(), Tag::NonSpecific, NullKind::Implicit)
    }
}

struct Context<'a, 'b: 'a> {
    options: &'a Options<'b>,
    err: &'a ErrorCollector,
}


fn pos_for_node<'x>(node: &Node<'x>) -> Pos {
    match *node {
        P::Map(_, _, _, ref tokens) => tokens[0].start.clone(),
        P::Seq(_, _, _, ref tokens) => tokens[0].start.clone(),
        P::Scalar(_, _, _, ref token) => token.start.clone(),
        P::ImplicitNull(_, _, ref pos) => pos.clone(),
        P::Alias(_, ref token, _) => token.start.clone(),
    }
}

fn parse_key<'x>(node: &Node, value: &'x Node<'x>, merge: &mut Option<&Node<'x>>)
    -> Result<String, Option<Error>>
{
     match *node {
        P::Scalar(_, _, ref key, ref tok) => {
            if tok.kind == T::PlainString && &key[..] == "<<" {
                *merge = Some(value);
                return Err(None);
            }
            Ok(key.clone())
        }
        P::ImplicitNull(_, _, _) => Ok("".to_string()),
        P::Alias(_, _, ref node) => {
            parse_key(node, value, merge)
        }
        ref node => {
            Err(Some(Error::preprocess_error(&pos_for_node(node),
                "Non scalar keys are not supported yet"
                .to_string())))
        }
    }
}

impl<'a, 'b: 'a> Context<'a, 'b> {
    fn process(&mut self, node: &'a Node<'a>) -> Ast {
        match *node {
            P::Map(ref origtag, _, _, ref tokens) => {
                let pos = tokens[0].start.clone();
                let tag = self.string_to_tag(&pos, origtag);
                let mut mapping = BTreeMap::new();
                self.merge_mapping(&mut mapping, node);

                return Map(pos, tag, mapping);
            }
            P::Seq(ref origtag, _, _, ref tokens) => {
                let pos = tokens[0].start.clone();
                let tag = self.string_to_tag(&pos, origtag);
                let mut seq = Vec::new();
                self.merge_sequence(&mut seq, node);

                return Seq(pos, tag, seq);
            }
            P::Scalar(Some("!*Include"), _anch, ref val, ref tok) => {
                return self.options.include(&tok.start,
                    &Include::File { filename: val }, self.err);
            }
            P::Scalar(Some("!*IncludeSeq"), _anch, ref val, ref tok) => {
                return self.options.include(&tok.start,
                    &Include::Sequence { pattern: val }, self.err);
            }
            P::Scalar(Some("!*IncludeMap"), _anch, ref val, ref tok) => {
                return self.options.include(&tok.start,
                    &Include::Mapping { pattern: val }, self.err);
            }
            P::Scalar(ref tag, _, ref val, ref tok) => {
                let pos = tok.start.clone();
                let tag = self.string_to_tag(&pos, tag);
                if tok.kind == T::PlainString {
                    if &val[..] == "~" || &val[..] == "null" {
                        return Ast::Null(tok.start.clone(), tag, Explicit);
                    } else {
                        return Ast::Scalar(pos, tag, Plain, val.clone());
                    }
                } else {
                    return Ast::Scalar(pos, tag, Quoted, val.clone());
                }
            }
            P::ImplicitNull(ref tag, _, ref pos) => {
                let tag = self.string_to_tag(pos, tag);
                return Ast::Null(pos.clone(), tag, Implicit);
            }
            P::Alias(_, _, ref node) => {
                return self.process(node);
            }
        }
    }

    fn merge_mapping(&mut self, target: &mut BTreeMap<String, Ast>,
        node: &'a Node<'a>)
    {
        match *node {
            P::Map(_, _, ref children, _) => {
                let mut merge = None;
                for (k, v) in children.iter() {
                    let string_key = match parse_key(k, v, &mut merge) {
                        Ok(k) => k,
                        Err(None) => continue,  // merge key
                        Err(Some(e)) => {
                            self.err.add_error(e);
                            continue;
                        }
                    };
                    let value = self.process(v);
                    if !target.contains_key(&string_key) {
                        target.insert(string_key, value);
                    }
                }
                match merge {
                    Some(node) => self.merge_mapping(target, node),
                    _ => {}
                }
            }
            P::Seq(_, _, ref lst, _) => {
                // TODO(tailhook) check and assert on tags?
                for item in lst.iter() {
                    self.merge_mapping(target, item);
                }
            }
            P::Scalar(Some("!*Include"), _anch, ref val, ref tok) => {
                self.merge_mapping_ast(target,
                    self.options.include(&tok.start,
                        &Include::File { filename: val }, self.err));
            }
            P::Alias(_, _, ref node) => {
                self.merge_mapping(target, node);
            }
            _ => {
                self.err.add_error(Error::preprocess_error(&pos_for_node(node),
                    "Value of merge key must be either mapping or \
                     list of mappings".to_string()));
            }
        }
    }
    /// This is same as merge_mapping but for asts
    ///
    /// Used for includes, may be this function can be used for everything
    /// but we don't use it perhaps for efficiency
    fn merge_mapping_ast(&mut self, target: &mut BTreeMap<String, Ast>,
        ast: Ast)
    {
        match ast {
            Map(_, _, children) => {
                for (k, v) in children.into_iter() {
                    if !target.contains_key(&k) {
                        // We don't make deep merging here, because other map
                        // is already merged
                        target.insert(k, v);
                    }
                }
            }
            Seq(_, _, lst) => {
                // TODO(tailhook) check and assert on tags?
                for item in lst.into_iter() {
                    self.merge_mapping_ast(target, item);
                }
            }
            node => {
                self.err.add_error(Error::preprocess_error(&node.pos(),
                    "Value of merge key must be either mapping or \
                     list of mappings".to_string()));
            }
        }
    }

    fn merge_sequence(&mut self, target: &mut Vec<Ast>,
        node: &'a Node<'a>)
    {
        match *node {
            P::Seq(_, _, ref children, _) => {
                for item in children.iter() {
                    match *item {
                        P::Seq(Some("!*Unpack"), _, ref children, _) => {
                            for child in children.iter() {
                                self.merge_sequence(target, child);
                            }
                        }
                        _ => {
                            let value = self.process(item);
                            target.push(value);
                        }
                    }
                }
            }
            P::Alias(_, _, ref node) => {
                self.merge_sequence(target, node);
            }
            P::Scalar(Some("!*Include"), _anch, ref val, ref tok) => {
                let ast = self.options.include(&tok.start,
                    &Include::File { filename: val }, self.err);
                // We don't make deep unpacking here, because other map
                // is already unpacked
                match ast {
                    Seq(_, _, vec) => {
                        target.extend(vec)
                    }
                    other => {
                        self.err.add_error(
                            Error::preprocess_error(&pos_for_node(node),
                                "The of !*Unpack node must be sequence"
                                .to_string()));
                        target.push(other);
                    }
                }
            }
            _ => {
                self.err.add_error(Error::preprocess_error(&pos_for_node(node),
                    "The of !*Unpack node must be sequence".to_string()));
            }
        }
    }

    fn string_to_tag(&mut self, pos: &Pos, src: &Option<&'a str>)
        -> Tag
    {
        match *src {
            Some(val) => {
                let mut pieces = val.splitn(3, '!');
                assert!(pieces.next().unwrap() == "");
                match (pieces.next().unwrap(), pieces.next()) {
                    ("", None) => {
                        self.err.add_error(Error::preprocess_error(pos,
                            "Unexpected empty tag".to_string()));
                        NonSpecific
                    }
                    (val, None) => {
                        LocalTag(val.to_string())
                    }
                    ("", Some(_)) => {
                        self.err.add_error(Error::preprocess_error(pos,
                            "Global tags are unsupported yet".to_string()));
                        NonSpecific
                    }
                    (_, Some(_)) => {
                        self.err.add_error(Error::preprocess_error(pos,
                            "Tag prefixes are unsupported yet".to_string()));
                        NonSpecific
                    }
                }
            }
            None => NonSpecific,
        }
    }
}


///  Preprocess AST
///
///  This includes:
///
///  * anchor substitution
///  * resolving merge keys `<<` and `!*Unpack`
///  * resolving includes `!*Include` and similar
///
pub fn process(opt: &Options, doc: Document, err: &ErrorCollector) -> Ast {
    let mut ctx = Context {
        options: opt,
        err: err,
    };
    return ctx.process(&doc.root);
}