subcomponent 0.1.0

A components orchestrator
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
/*
 * Copyright (c) 2016-2017 Jean Guyomarc'h
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

extern crate std;
use std::error::Error as FmtError;
use compiler::lexer;
use compiler::lexer::LexemeKind;
use self::Error::*;
use std::rc::Rc;
use std::ops::Deref;
use std::cell::RefCell;
use config;
use fetcher;
use hook;


#[derive(Debug,PartialEq)]
pub enum Error {
    InvalidSequence,
    ExpectedIdentifier,
    TooManyBraces,
    NotEnoughBraces,
    InvalidPropertyName,
    InvalidPropertyType,
    NoSubcomponents,
    RuntimeError,
    NoFetchMethod,
    MissingRequiredProperty,
    ConflictingProperties,
    InvalidUnpackMethod,
    ReservedHook,
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.description())
    }
}

impl std::error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            InvalidSequence => "Invalid sequence of tokens",
            ExpectedIdentifier => "An identifier was expected",
            TooManyBraces => "Brace '{' does not have a matching closing brace '}'",
            NotEnoughBraces => "Brace '}' does not have a matching opening brace '{'",
            InvalidPropertyName => "Invalid property name",
            InvalidPropertyType => "Invalid property type",
            NoSubcomponents => "At least one subcomponent is required",
            RuntimeError => "An unknown runtime error occurred",
            NoFetchMethod => "No fetch method have been defined",
            MissingRequiredProperty => "A required property is not implemented",
            ConflictingProperties => "Two or more properties are in conflict",
            InvalidUnpackMethod => "Invalid unpack method",
            ReservedHook => "Collision between a user hook and a reserved command",
        }
    }
}

pub enum PropertyValue<'a> {
    StringValue(&'a String),
    BooleanValue(bool),
    StringListValue(&'a Vec<String>),

    EmptyValue,
}

impl<'a> std::fmt::Display for PropertyValue<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match *self {
            PropertyValue::StringValue(val) => write!(f, "{}", val),
            PropertyValue::BooleanValue(ref val) => write!(f, "{}", val),
            PropertyValue::StringListValue(val) => {
                let mut text = String::new();
                for elem in val.iter() {
                    text.push_str(elem);
                    text.push(' ');
                }
                write!(f, "{}", text)
            },
            PropertyValue::EmptyValue => write!(f, "None"),
        }
    }
}

pub trait Property {
    fn value_get(&self) -> PropertyValue;
}

pub struct Identifier {
    name: String,
}

impl Property for () {
    fn value_get(&self) -> PropertyValue {
        PropertyValue::EmptyValue
    }
}

impl Property for Identifier {
    fn value_get(&self) -> PropertyValue {
        PropertyValue::StringValue(&self.name)
    }
}

impl Property for Vec<String> {
    fn value_get(&self) -> PropertyValue {
        PropertyValue::StringListValue(self)
    }
}

impl Property for String {
    fn value_get(&self) -> PropertyValue {
        PropertyValue::StringValue(self)
    }
}

impl Property for bool {
    fn value_get(&self) -> PropertyValue {
        PropertyValue::BooleanValue(*self)
    }
}


enum Sequence {
    Property,
    BlockStart,
    BlockEnd,
    StringList,
    Invalid,
}


impl Identifier {
    pub fn new(name: String) -> Identifier {
        Identifier {
            name: name,
        }
    }
}

pub struct Parser {
    cfg: Rc<config::Config>,
    components: Vec<RefCell<config::Component>>,
}


impl Parser {

    pub fn components_get(&self) -> &Vec<RefCell<config::Component>> {
        &self.components
    }

    pub fn config_get(&self) -> &config::Config {
        &self.cfg
    }

    pub fn new() -> Parser {
        Parser {
            cfg: Rc::new(config::Config::new()),
            components: Vec::new(),
        }
    }

    fn name_prop_get(component: &str, cfg: &config::Config) -> Result<String, Error> {
        /*
         * Get the "name" property. It is a mandatory String.
         */
        let key = format!("/subcomponents/{}/name", component);
        if let Some(prop) = cfg.get(&key) {
            match prop.deref().value_get() {
                PropertyValue::StringValue(val) => { return Ok(val.clone()); },
                _ => { return Err(Error::InvalidPropertyType); },
            }
        }
        Err(Error::MissingRequiredProperty)
    }

    fn path_prop_get(component: &str, cfg: &config::Config) -> Result<String, Error> {
        /*
         * Get the "path" property. It is a mandatory String.
         */
        let key = format!("/subcomponents/{}/path", component);
        if let Some(prop) = cfg.get(&key) {
            match prop.deref().value_get() {
                PropertyValue::StringValue(val) => { return Ok(val.clone()); },
                _ => { return Err(Error::InvalidPropertyType); },
            }
        }
        Err(Error::MissingRequiredProperty)
    }

    fn depends_prop_get(component: &str, cfg: &config::Config) -> Result<Vec<String>, Error> {
        let key = format!("/subcomponents/{}/depends", component);
        if let Some(prop) = cfg.get(&key) {
           match prop.deref().value_get() {
              PropertyValue::StringValue(val) => { return Ok(vec![val.clone()]); },
              PropertyValue::StringListValue(val) => { return Ok(val.clone()); },
              _ => {
                 error!("Property 'depends' of component '{}' shall be a list \
                        of components",
                        component);
                 return Err(Error::InvalidPropertyType);
              }
           }
        }
        Ok(Vec::new())
    }

    fn parse_hook_exec(component: &str, cfg: &config::Config, hook: &str) -> Result<String, Error> {
       let key = format!("/subcomponents/{}/{}/exec", component, hook);
       if let Some(prop) = cfg.get(&key) {
          match prop.deref().value_get() {
             PropertyValue::StringValue(val) => { return Ok(val.clone()); },
             _ => { return Err(Error::InvalidPropertyType); }
          }
       }
        Err(Error::MissingRequiredProperty)
    }
    fn parse_hook_args(component: &str, cfg: &config::Config, hook: &str) -> Result<Option<Vec<String>>, Error> {
       let key = format!("/subcomponents/{}/{}/args", component, hook);
       if let Some(prop) = cfg.get(&key) {
          match prop.deref().value_get() {
             PropertyValue::StringValue(val) => { return Ok(Some(vec![val.clone()])); },
             PropertyValue::StringListValue(val) => { return Ok(Some(val.clone())); },
             _ => { return Err(Error::InvalidPropertyType); }
          }
       }
       Ok(None)
    }

    fn parse_hook_cwd(component: &str, cfg: &config::Config, hook: &str) -> Result<Option<String>, Error> {
       let key = format!("/subcomponents/{}/{}/cwd", component, hook);
       if let Some(prop) = cfg.get(&key) {
          match prop.deref().value_get() {
             PropertyValue::StringValue(val) => { return Ok(Some(val.clone())); },
             _ => { return Err(Error::InvalidPropertyType); }
          }
       }
       Ok(None)
    }

    fn parse_component(component: &str, cfg: &config::Config) -> Result<config::Component, Error> {
        let name = Parser::name_prop_get(component, cfg)?;
        let path = Parser::path_prop_get(component, cfg)?;
        let dependencies = Parser::depends_prop_get(component, cfg)?;
        let hooks_names = cfg.get_hooks_for_component(component)?;
        let mut hooks = Vec::new();

        for hook_ref in hooks_names {
           let exec = Parser::parse_hook_exec(component, cfg, &hook_ref)?;
           let args = Parser::parse_hook_args(component, cfg, &hook_ref)?;
           let cwd = match Parser::parse_hook_cwd(component, cfg, &hook_ref)? {
              Some(cwd_arg) => { cwd_arg },
              None => { path.clone() },
           };
           let hook = hook::Hook::new(hook_ref, exec, cwd, args);
           hooks.push(hook);
        }

        Ok(config::Component::new(String::from(component), name, path, dependencies, hooks))
    }

    fn collect_components(&mut self) -> Result<(), Error> {

        let components = self.cfg.collect_components()?;

        /*
         * Parse all components nodes and get their base handles.
         */
        for component in &components {
            let comp = Parser::parse_component(component, &self.cfg)?;
            self.components.push(RefCell::new(comp));
        }
        Ok(())
    }

    fn token_value_check(&self, tok: &LexemeKind) -> bool {
        match *tok {
            LexemeKind::Identifier | LexemeKind::String => true,
            _ => false,
        }
    }
    fn tokens_list_items_check(&self, tok_left: &LexemeKind, tok_right: &LexemeKind) -> bool {
        if tok_right == &LexemeKind::ListSeparator {
            if self.token_value_check(tok_left) {
                return true;
            }
        }
        false
    }

    fn sequence_get(&self, tokens: &[lexer::Lexeme]) -> (Sequence, usize) {
        let tokens_count = tokens.len();

        macro_rules! tok {
            ($x:expr) => {{
                let kind = tokens[$x].kind_get();
                kind
            }}
        }

        /*
         * XXX This code needs to be rewritten when slice matching
         * will be integrated to stable rust.
         */

        if tokens_count >= 1 {
            let seq = [tok!(0)];
            let comp = [&LexemeKind::BlockEnd];
            if seq == comp {
                return (Sequence::BlockEnd, 1);
            }
        }

        /*
         * We have four valid possibilites:
         * - a property value, for 4 tokens
         * - a property list, for N tokens, N >= 6
         * - a block declaration, for 2 tokens
         * - a block ending, for 1 token
         *
         * At least 4 tokens does not imply it must be a property. If 4 tokens
         * do not give us a property, we will have to check for the start of
         * block too, as well as the end of block.
         */

        if tokens_count >= 6 {
            /*
             * At least 6 tokens: we can expect a list of at least size 2.
             */
            let seq_pre = [tok!(0), tok!(1), tok!(2), tok!(3)];

            /*
             * XXX We are handling a STRING LIST only!!
             */
            let comp = [
                &LexemeKind::Identifier, &LexemeKind::PropertyEnd,
                &LexemeKind::String, &LexemeKind::ListSeparator,
            ];
            if seq_pre == comp {
                let mut i = 2;
                while i + 2 <= tokens_count {
                    let left = tokens[i].kind_get();
                    let right = tokens[i + 1].kind_get();
                    if self.tokens_list_items_check(left, right) {
                        /* Still in the list, do nothing, keep going! */
                    } else if right == &LexemeKind::StatementEnd {
                        /* Okay, we have a string list */
                        return (Sequence::StringList, i + 2);
                    } else {
                        /* Ill-formed list, stop trying */
                        break;
                    }

                    i += 2;
                }
            }
        }

        if tokens_count >= 4 {
            /*
             * At least 4 tokens: we can expect a property
             */
            let seq = [tok!(0), tok!(1), tok!(3)];
            let comp = [
                &LexemeKind::Identifier, &LexemeKind::PropertyEnd,
                &LexemeKind::StatementEnd,
            ];
            if (seq == comp) && self.token_value_check(tok!(2)) {
                return (Sequence::Property, 4);
            }
        }

        if tokens_count >= 2 {
            let seq = [tok!(0), tok!(1)];
            let comp = [&LexemeKind::Identifier, &LexemeKind::BlockStart];
            if seq == comp {
                return (Sequence::BlockStart, 2);
            }
        }

        /*
         * We found nothing earlier, so we just have an invalid sequence
         */
        error!("[{}] syntax error near l{}:c{}",
               tokens[0].file, tokens[0].line, tokens[0].column);
        (Sequence::Invalid, 0)
    }

    pub fn parse(&mut self, lexer: &lexer::Lexer) -> Result<(), Error> {
        let mut iter = 0;
        let mut blocks_match = 0;
        let tokens = lexer.tokens_get();
        let iter_max = tokens.len();

        /*
         * Convenient macro to access the config as a mutable reference
         */
        macro_rules! config_get {
            () => {{
                if let Some(config) = Rc::get_mut(&mut self.cfg) {
                    config
                } else {
                    error!("Failed to get a mutable reference on the config");
                    return Err(Error::RuntimeError);
                }
            }}
        }

        /*
         * Convenience to extract a token from the lexer, and
         * throw an error if we can't get enough.
         */
        macro_rules! token_extract {
            ($x:expr) => {{
                match lexer.token_extract(iter + $x) {
                    Some(val) => val,
                    None => {
                        error!("Missing value or identifier");
                        return Err(Error::ExpectedIdentifier);
                    }
                }
            }}
        }

        /*
         * `pkey` will track the parent property. It is the property
         * of the group that is currently filled.
         */
        let mut pkey = String::from("/");

        while iter < iter_max {
            let (seq, count) = self.sequence_get(&tokens[iter..]);
            match seq {
                Sequence::BlockStart => {
                    blocks_match += 1;
                    let tok = token_extract!(0);

                    /*
                     * Start a new group, change the `pkey`.
                     */
                    pkey.push_str(tok);
                    pkey.push_str("/");


                    let config = config_get!();
                    config.set(pkey.clone(), Box::new(()));
                },
                Sequence::BlockEnd => {
                    blocks_match -= 1;

                    /*
                     * End of a group, undo what was added to the `pkey` when
                     * we opened the group.
                     */

                    /*
                     * Don't forget to remove the trailing '/' in case we met two
                     * successive block ends.
                     */
                    if pkey.chars().last().unwrap() == '/' {
                       pkey.pop();
                    }
                    if let Some(index) = pkey.rfind('/') {
                        /* Remove the last characters from 'index' to the end
                         * of the string. */
                        for _iter in index..pkey.len()-1 {
                            pkey.pop();
                        }
                    }
                },
                Sequence::StringList => {
                    let prop_name = token_extract!(0);
                    /* elements is the arguments count, minus:
                     * - the property name
                     * - the end of property (:)
                     * - the end of statement (;)
                     * Then, we end up with count being always odd, because a
                     * comma separates exactly two arguments.
                     * So we divide this remainder by 2, and then add one, to
                     * take the round up the divsion by 2.
                     */
                    let elements = ((count - 3) / 2) + 1;
                    let mut list = Vec::new();

                    /*
                     * Go through the elements of the stringlist and extract
                     * one token over two.
                     */
                    for i in 0..elements {
                        if let Some(tok) = lexer.token_extract(iter + ((i + 1) * 2)) {
                            list.push(String::from(tok));
                        }
                    }

                    /*
                     * Okay, we have a property. Let's make a copy of the
                     * `pkey`, and complete the copy the property name.
                     */
                    let mut key = pkey.clone();
                    key.push_str(prop_name);

                    trace!("Parsed property list \"{}\" -> {}", key, list.value_get());
                    if self.cfg.get(&key).is_some() {
                        warn!("Overriding property {}", key);
                    }
                    let config = config_get!();
                    config.set(key, Box::new(list));
                },
                Sequence::Property => {
                    let prop_tok = token_extract!(0);
                    let val_tok = token_extract!(2);
                    let val = String::from(val_tok);
                    let data: Box<Property>;

                    /*
                     * Okay, we have a property. Let's make a copy of the
                     * `pkey`, and complete the copy the property name.
                     */
                    let mut key = pkey.clone();
                    key.push_str(prop_tok);

                    match *tokens[iter + 2].kind_get() {
                        LexemeKind::String => {
                            data = Box::new(val);
                        },
                        LexemeKind::Identifier => {
                            match val_tok {
                                /*
                                 * FIXME The lexer should be able to tell when
                                 * an identifier is a boolean.
                                 */
                                "true" => {
                                    data = Box::new(true);
                                },
                                "false" => {
                                    data = Box::new(false);
                                },
                                _ => {
                                    data = Box::new(Identifier::new(val));
                                }
                            }
                        },
                        _ => {
                            /* Should not happen (already checked) */
                            return Err(Error::InvalidSequence);
                        }
                    }

                    /*
                     * We are done with our property. We can insert it in the
                     * global config. Warn if it already existed.
                     */
                    trace!("Parsed property \"{}\" -> {}", key, data.value_get());
                    if self.cfg.get(&key).is_some() {
                        warn!("Overriding property {}", key);
                    }
                    let config = config_get!();
                    config.set(key, data);
                },
                Sequence::Invalid => {
                    error!("Invalid sequence of tokens. Expected a propery or a group declaration.");
                    return Err(Error::InvalidSequence);
                },
            }
            /*
             * We have consumed `count` tokens
             */
            iter += count;
        }

        /*
         * Safety check. If we have unbalanced braces, it is a good time to
         * gracefully fail
         */
        if blocks_match != 0 {
            error!("Unbalanced number of braces.");
            if blocks_match > 0 {
                return Err(Error::TooManyBraces);
            } else {
                return Err(Error::NotEnoughBraces);
            }
        }

        /*
         * Collect all components
         */
        try!(self.collect_components());

        /*
         * We must have at least one component
         */
        if self.components.is_empty() {
            return Err(NoSubcomponents);
        }

        /*
         * Fetch methods are clients of the parser. Now that we are done with
         * the basics of the file, parse the fetch methods.
         */
        try!(fetcher::parse(self));

        self.cfg.check_unused()
    }
}

/*============================================================================*
 *                                    Tests                                   *
 *============================================================================*/

#[cfg(test)]
mod tests {
    extern crate std;
    use compiler;
    use compiler::parser::Error;

    fn test_file_get(name: &str, ext: &str) -> std::path::PathBuf {
        /*
         * Parser tests are in tests/parser/<NAME>.<EXT>
         */
        let mut path = std::path::PathBuf::new();
        path.push("tests");
        path.push("parser");
        path.push(name);
        path.set_extension(ext);
        path
    }
    fn test_source_get(name: &str) -> std::path::PathBuf {
        test_file_get(name, "sub")
    }

    fn test_check_run(parser_test: &str, expected: Option<Error>) {
        let source = test_source_get(parser_test);
        let source_path = source.as_path();
        let mut lexer = compiler::lexer::new();

        /* Lex the input source path */
        match lexer.lex(source_path) {
            Ok(_) => {
                let mut parser = compiler::parser::Parser::new();
                let err = parser.parse(&lexer).err();
                println!("Parser error: {:?}. Expected: {:?}.", err, expected);
                assert!(err == expected);
            }
            Err(err) => {
                println!("*** Lexer error: {}", err);
                assert!(false);
            }
        }
    }

    #[test] fn parse_valid_01() { test_check_run("ok-01", None); }
    #[test] fn parse_valid_02() { test_check_run("ok-02", None); }
    #[test] fn parse_valid_03() { test_check_run("ok-03", None); }
    #[test] fn parse_valid_04() { test_check_run("ok-04", None); }
    #[test] fn parse_valid_05() { test_check_run("ok-05", None); }
    #[test] fn parse_valid_06() { test_check_run("ok-06", None); }

    #[test] fn parse_invalid_01()
    { test_check_run("fail-01", Some(Error::NoSubcomponents)); }

    #[test] fn parse_invalid_02()
    { test_check_run("fail-02", Some(Error::NoSubcomponents)); }

    #[test] fn parse_invalid_03()
    { test_check_run("fail-03", Some(Error::MissingRequiredProperty)); }

    #[test] fn parse_invalid_04()
    { test_check_run("fail-04", Some(Error::MissingRequiredProperty)); }

    #[test] fn parse_invalid_05()
    { test_check_run("fail-05", Some(Error::ConflictingProperties)); }

    #[test] fn parse_invalid_06()
    { test_check_run("fail-06", Some(Error::TooManyBraces)); }

    #[test] fn parse_invalid_07()
    { test_check_run("fail-07", Some(Error::NotEnoughBraces)); }

    #[test] fn parse_invalid_08()
    { test_check_run("fail-08", Some(Error::InvalidPropertyName)); }

    #[test] fn parse_invalid_10()
    { test_check_run("fail-10", Some(Error::ReservedHook)); }

    #[test] fn parse_invalid_11()
    { test_check_run("fail-11", Some(Error::MissingRequiredProperty)); }
}