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
/*!

  Contains data structures used to represent SMPL code.

*/

use std::path::PathBuf;

use crate::analysis::ModuleId;
use crate::ast::Module;

///
/// Represents where a SMPL module came from. Used to help gather source location info.
///
/// * `ModuleSource::File` should be used whenever checking SMPL code from a file.
///
/// * Otherwise, use `ModuleSource::Anonymous`
///
///   * `ModuleSource::Anonymous` can carry a hint (`Some(string)`) that will appear
///   within source location info
///
///   * Without a hint, source location info will contain an `anonymous`
///
#[derive(Debug, Clone)]
pub enum ModuleSource {
    Anonymous(Option<String>),
    File(PathBuf),
}

impl std::fmt::Display for ModuleSource {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            ModuleSource::Anonymous(Some(ref a)) => write!(f, "{}", a),
            ModuleSource::Anonymous(None) => write!(f, "{}", "anonymous"),
            ModuleSource::File(ref buff) => write!(f, "{}", buff.display()),
        }
    }
}

///
/// Represents an unparsed SMPL module.
///
pub struct UnparsedModule<'a> {
    pub(crate) source: ModuleSource,
    pub(crate) module: &'a str,
}

impl<'a> UnparsedModule<'a> {

    ///
    /// Symbolizes an unparsed SMPL module from a file.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the file
    /// * `data` - ENTIRE content of the file 
    ///
    pub fn file(path: PathBuf, data: &str) -> UnparsedModule {
        UnparsedModule {
            source: ModuleSource::File(path),
            module: data,
        }
    }

    ///
    /// Symbolizes an unparsed SMPL module from an unnamed source.
    ///
    /// # Arguments
    ///
    /// * `data` - ENTIRE SMPL module
    ///
    pub fn anonymous(data: &str) -> UnparsedModule {
        UnparsedModule {
            source: ModuleSource::Anonymous(None),
            module: data,
        }
    }

    ///
    /// Symbolizes an unparsed SMPL module from an hintable unnamed source.
    ///
    /// # Arguments
    ///
    /// * `hint` - Hint to where the SMPL code came from
    /// * `data` - ENTIRE SMPL module
    ///
    pub fn anonymous_hint(hint: String, data: &str) -> UnparsedModule {
        UnparsedModule {
            source: ModuleSource::Anonymous(Some(hint)),
            module: data,
        }
    }

    ///
    /// Returns a reference to a SMPL module's source data
    ///
    pub fn source(&self) -> &ModuleSource {
        &self.source
    }
}

///
/// Represents a parsed SMPL module that is syntactically correct.
///
/// Created by calling `smpl::parser::parse_module`.
/// 
pub struct ParsedModule {
    pub(crate) source: ModuleSource,
    pub(crate) module: Module,
    pub(crate) id: ModuleId,
}

impl ParsedModule {
    pub(crate) fn new(data: Module, source: ModuleSource) -> ParsedModule {
        ParsedModule {
            source: source,
            module: data,
            id: ModuleId::new(),
        }
    }

    pub fn id(&self) -> ModuleId {
        self.id
    }

    ///
    /// Returns a reference to a SMPL module's source data
    ///
    pub fn source(&self) -> &ModuleSource {
        &self.source
    }
}