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
//! Parser of the `app!` macro used by the Real Time For the Masses (RTFM)
//! framework
#![deny(missing_debug_implementations)]
#![deny(missing_docs)]
#![deny(warnings)]

#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate quote;
extern crate syn;

pub mod check;
pub mod error;

mod parse;
mod util;

use std::collections::{HashMap, HashSet};

use quote::Tokens;
use syn::{Ident, Path, Ty};

use error::*;

/// A rust expression
pub type Expr = Tokens;

/// `[$($ident),*]`
pub type Resources = HashSet<Ident>;

/// `$(static $Ident: $Ty = $expr;)*`
pub type Statics = HashMap<Ident, Static>;

/// `$($Ident: { .. },)*`
pub type Tasks = HashMap<Ident, Task>;

/// `app! { .. }`
#[derive(Debug)]
pub struct App {
    /// `device: $path`
    pub device: Path,
    /// `idle: { $Idle }`
    pub idle: Option<Idle>,
    /// `init: { $Init }`
    pub init: Option<Init>,
    /// `resources: $Resources`
    pub resources: Option<Statics>,
    /// `tasks: { $Tasks }`
    pub tasks: Option<Tasks>,
    _extensible: (),
}

/// `idle: { .. }`
#[derive(Debug)]
pub struct Idle {
    /// `path: $Path`
    pub path: Option<Path>,
    /// `resources: $Resources`
    pub resources: Option<Resources>,
    _extensible: (),
}

/// `init: { .. }`
#[derive(Debug)]
pub struct Init {
    /// `path: $Path`
    pub path: Option<Path>,
    _extensible: (),
}

/// `$Ident: { .. }`
#[derive(Debug)]
pub struct Task {
    /// `enabled: $bool`
    pub enabled: Option<bool>,
    /// `path: $Path`
    pub path: Option<Path>,
    /// `priority: $u8`
    pub priority: Option<u8>,
    /// `resources: $Resources`
    pub resources: Option<Resources>,
    _extensible: (),
}

/// `static $Ident: $Ty = $Expr;`
#[derive(Debug)]
pub struct Static {
    /// `$Expr`
    pub expr: Option<Expr>,
    /// `$Ty`
    pub ty: Ty,
    _extensible: (),
}

impl App {
    /// Parses the contents of the `app! { .. }` macro
    pub fn parse(input: &str) -> Result<Self> {
        parse::app(input)
    }
}