Actually just three programming languages in a trenchcoat
at main 58 lines 1.8 kB view raw
1use crate::syntax::SyntaxError; 2 3/// Encapsulates the result of attempting to parse a Trilogy file. 4/// 5/// This is similar to but more than just a [`Result`][], as a Trilogy file may 6/// contain multiple errors or warnings. 7/// 8/// While the contents are accessible whether the parse was successful or not, 9/// if the `Parse` contains errors, then the exact structure of the contents 10/// is not defined to be well formed. 11#[derive(Clone, Debug)] 12pub struct Parse<T> { 13 pub(crate) ast: T, 14 pub(crate) warnings: Vec<SyntaxError>, 15 pub(crate) errors: Vec<SyntaxError>, 16} 17 18impl<T> Parse<T> { 19 /// A reference to the contents of this parse. 20 /// 21 /// If this `Parse` contains errors, the exact structure of these contents 22 /// are not defined to be well formed. 23 pub fn ast(&self) -> &T { 24 &self.ast 25 } 26 27 /// Consume the parse, turning it into its contents. 28 /// 29 /// If this `Parse` contains errors, the exact structure of these contents 30 /// are not defined to be well formed. 31 pub fn into_ast(self) -> T { 32 self.ast 33 } 34 35 /// Whether this `Parse` contains errors. 36 pub fn has_errors(&self) -> bool { 37 !self.errors.is_empty() 38 } 39 40 /// Whether this `Parse` contains warnings. 41 /// 42 /// Warnings do not affect the validity of the parsed contents, but may 43 /// be presented to the writer of the source code to inform them of potential 44 /// mistakes. 45 pub fn has_warnings(&self) -> bool { 46 !self.warnings.is_empty() 47 } 48 49 /// The list of warnings generated during this parse. 50 pub fn warnings(&self) -> &[SyntaxError] { 51 &self.warnings 52 } 53 54 /// The list of errors generated during this parse. 55 pub fn errors(&self) -> &[SyntaxError] { 56 &self.errors 57 } 58}