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
// ATTRIBUTES.rs
// by Lut99
//
// Created:
// 08 Dec 2023, 11:35:48
// Last edited:
// 12 Dec 2023, 16:35:31
// Auto updated?
// Yes
//
// Description:
//! Implements a traversal that processes [`Stmt::Attribute`]s and
//! [`Stmt::AttributeInner`]s into attribute annotations on other
//! statement nodes.
//
use brane_dsl::ast::{Attribute, Block, Node as _, Program, Stmt};
use crate::errors::AstError;
use crate::warnings::AstWarning;
pub use crate::warnings::AttributesWarning as Warning;
/***** TESTS *****/
#[cfg(test)]
mod tests {
use brane_dsl::ParserOptions;
use brane_shr::utilities::{create_data_index, create_package_index, test_on_dsl_files};
use specifications::data::DataIndex;
use specifications::package::PackageIndex;
use super::super::print::dsl;
use super::*;
use crate::{CompileResult, CompileStage, compile_program_to};
/// Tests the traversal by generating symbol tables for every file.
#[test]
fn test_attributes() {
test_on_dsl_files("BraneScript", |path, code| {
// Start by the name to always know which file this is
println!("{}", (0..80).map(|_| '-').collect::<String>());
println!("File '{}' gave us:", path.display());
// Load the package index
let pindex: PackageIndex = create_package_index();
let dindex: DataIndex = create_data_index();
let program: Program = match compile_program_to(code.as_bytes(), &pindex, &dindex, &ParserOptions::bscript(), CompileStage::Attributes) {
CompileResult::Program(p, warns) => {
// Print warnings if any
for w in warns {
w.prettyprint(path.to_string_lossy(), &code);
}
p
},
CompileResult::Eof(err) => {
// Print the error
err.prettyprint(path.to_string_lossy(), &code);
panic!("Failed to process attributes (see output above)");
},
CompileResult::Err(errs) => {
// Print the errors
for e in errs {
e.prettyprint(path.to_string_lossy(), &code);
}
panic!("Failed to process attributes (see output above)");
},
_ => {
unreachable!();
},
};
// Now print the symbol tables for prettyness
dsl::do_traversal(program, std::io::stdout()).unwrap();
println!("{}\n\n", (0..80).map(|_| '-').collect::<String>());
});
}
}
/***** TRAVERSAL FUNCTIONS *****/
/// Traverses a block to process annotation statements.
///
/// # Arguments
/// - `block`: The [`Block`] to traverse.
/// - `prev_attrs`: The attributes returned by the previous statement if it was a [`Stmt::Attribute`], or else an empty vector.
/// - `warns`: A list to keep track of warnings that occur.
fn pass_block(block: &mut Block, prev_attrs: Vec<Attribute>, warns: &mut Vec<Warning>) {
let Block { stmts, table: _, ret_type: _, attrs, range: _ } = block;
// Set the attributes passed from the previous one
attrs.extend(prev_attrs);
// Simply pass its statements
let mut next_attrs: Vec<Attribute> = vec![];
for s in stmts.iter_mut() {
// Pass it with this block's attribute list as parent, though
next_attrs = pass_stmt(s, attrs, next_attrs, warns);
}
// Warn about final attributes not found
for attr in next_attrs {
warns.push(Warning::UnmatchedAttribute { range: attr.range().clone() });
}
// Retain only non-attribute statements
stmts.retain(|s| !matches!(s, Stmt::Attribute(_)) && !matches!(s, Stmt::AttributeInner(_)));
}
/// Traverses a statement to process annotation statements.
///
/// # Arguments
/// - `stmt`: The [`Stmt`] to traverse.
/// - `parent_attrs`: A list of the parent attributes, updated when we find a [`Stmt::AttributeInner`].
/// - `prev_attrs`: The attributes returned by the previous statement if it was a [`Stmt::Attribute`], or else an empty vector.
/// - `warns`: A list to keep track of warnings that occur.
///
/// # Returns
/// The attributes returned by this statement if it was a [`Stmt::Attribute`], or else an empty vector.
fn pass_stmt(stmt: &mut Stmt, parent_attrs: &mut Vec<Attribute>, mut prev_attrs: Vec<Attribute>, warns: &mut Vec<Warning>) -> Vec<Attribute> {
// Match on the statement
use Stmt::*;
match stmt {
Attribute(attr) => {
// Add the attributes as next one
prev_attrs.push(attr.clone());
prev_attrs
},
AttributeInner(attr) => {
// Add the attributes to the parent
parent_attrs.push(attr.clone());
vec![]
},
Block { block } => {
pass_block(block, prev_attrs, warns);
vec![]
},
Import { name: _, version: _, st_funcs: _, st_classes: _, attrs, range: _ } => {
// Set the previous attributes
attrs.extend(prev_attrs);
vec![]
},
FuncDef { ident: _, params: _, code, st_entry: _, attrs, range: _ } => {
// Set the previous attributes
attrs.extend(prev_attrs);
// Traverse the body
// Note: we pass empty because the attributes to the class have already been given
pass_block(code, vec![], warns);
vec![]
},
ClassDef { ident: _, props: _, methods, st_entry: _, symbol_table: _, attrs, range: _ } => {
// Set the previous attributes
attrs.extend(prev_attrs);
// Traverse the methods
for method in methods {
// Note: we pass empty because the attributes to the class have already been given
pass_stmt(method, parent_attrs, vec![], warns);
}
vec![]
},
Return { expr: _, data_type: _, output: _, attrs, range: _ } => {
attrs.extend(prev_attrs);
vec![]
},
If { cond: _, consequent, alternative, attrs, range: _ } => {
attrs.extend(prev_attrs);
// Pass the blocks
pass_block(consequent, vec![], warns);
if let Some(alternative) = alternative {
// Note: we pass empty because the attributes to the class have already been given
pass_block(alternative, vec![], warns);
}
vec![]
},
For { initializer: _, condition: _, increment: _, consequent, attrs, range: _ } => {
attrs.extend(prev_attrs);
// Note: we pass empty because the attributes to the class have already been given
pass_block(consequent, vec![], warns);
vec![]
},
While { condition: _, consequent, attrs, range: _ } => {
attrs.extend(prev_attrs);
// Note: we pass empty because the attributes to the class have already been given
pass_block(consequent, vec![], warns);
vec![]
},
Parallel { result: _, blocks, merge: _, st_entry: _, attrs, range: _ } => {
attrs.extend(prev_attrs);
for block in blocks {
// Note: we pass empty because the attributes to the class have already been given
pass_block(block, vec![], warns);
}
vec![]
},
LetAssign { name: _, value: _, st_entry: _, attrs, range: _ } => {
attrs.extend(prev_attrs);
vec![]
},
Assign { name: _, value: _, st_entry: _, attrs, range: _ } => {
attrs.extend(prev_attrs);
vec![]
},
Expr { expr: _, data_type: _, attrs, range: _ } => {
attrs.extend(prev_attrs);
vec![]
},
Empty {} => vec![],
}
}
/***** LIBRARY *****/
/// Processes annotation statements into annotations on the other statements in the AST.
///
/// The goal of this traversal is to get rid of [`Stmt::Attribute`] and [`Stmt::AttributeInner`] occurrances, populating the `attrs`-field in various [`Stmt`] variants.
///
/// # Arguments
/// - `root`: The root node of the tree on which this compiler pass will be done.
///
/// # Returns
/// The same nodes as went in, but now with annotation statements translated to annotations on structs.
///
/// # Errors
/// This pass may throw multiple `AstError::AttributesError`s if the user made mistakes with their variable references.
pub fn do_traversal(mut root: Program, warnings: &mut Vec<AstWarning>) -> Result<Program, Vec<AstError>> {
// Traverse the tree, doin' all the work
let mut warns: Vec<Warning> = vec![];
pass_block(&mut root.block, vec![], &mut warns);
// Process the warnings
warnings.extend(warns.into_iter().map(AstWarning::AttributesWarning));
// Returns the errors
Ok(root)
}