gbf_core/decompiler/ast/visitors/emit_context.rs
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
#![deny(missing_docs)]
use thiserror::Error;
/// Represents an error that occurred while converting an AST node.
#[derive(Debug, Error)]
pub enum EmitError {}
/// Represents the verbosity mode in which the AST should be emitted.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EmitVerbosity {
/// Emit the AST in a format that is readable by humans.
Pretty,
/// Emit the AST in a format that is readable by the parser. This format ensures that no comments or extra whitespace is emitted.
Minified,
/// Debug mode, which emits the AST in a format that is useful for debugging.
Debug,
}
/// Represents the formatting indentation style for blocks of code.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IndentStyle {
/// Allman style indentation.
Allman,
/// K&R style indentation.
KAndR,
}
/// Contains the emitting context for the AST.
#[derive(Debug, Clone, Copy)]
pub struct EmitContext {
/// The current indentation level.
pub indent: usize,
/// The number of spaces to indent by.
pub indent_step: usize,
/// Whether to format numbers in hexadecimal.
pub format_number_hex: bool,
/// The mode in which to emit the AST.
pub verbosity: EmitVerbosity,
/// The style of indentation to use.
pub indent_style: IndentStyle,
/// The root of the expression tree.
pub expr_root: bool,
/// If we should include SSA versions in the emitted code.
pub include_ssa_versions: bool,
}
impl EmitContext {
/// Allow temporarily changing the EmitContext for a block of code.
///
/// # Arguments
/// - `f` - The function to call with the new EmitContext.
///
/// # Returns
/// The EmitContext after the function has been called.
///
/// # Example
/// ```
/// use gbf_core::decompiler::ast::visitors::emit_context::EmitContext;
///
/// let mut context = EmitContext::default();
/// let body_context = context.scoped(|ctx| ctx.with_indent());
/// ```
pub fn scoped<F>(&mut self, action: F) -> Self
where
F: FnOnce(&mut Self) -> EmitContext,
{
action(self)
}
/// Returns a new EmitContext with the indent increased by the indent step.
///
/// # Returns
/// A new EmitContext with the indent increased by the indent step.
///
/// # Example
/// ```
/// use gbf_core::decompiler::ast::visitors::emit_context::EmitContext;
///
/// let mut context = EmitContext::default();
/// let body_context = context.with_indent();
/// ```
pub fn with_indent(&self) -> EmitContext {
let mut new_context = *self;
new_context.indent += self.indent_step;
new_context
}
/// Returns a new EmitContext with expr_root set to the given value.
///
/// # Arguments
/// - `expr_root` - The value to set expr_root to.
///
/// # Returns
/// A new EmitContext with expr_root set to the given value.
///
/// # Example
/// ```
/// use gbf_core::decompiler::ast::visitors::emit_context::EmitContext;
///
/// let mut context = EmitContext::default();
/// let body_context = context.with_expr_root(true);
/// ```
pub fn with_expr_root(&self, expr_root: bool) -> EmitContext {
let mut new_context = *self;
new_context.expr_root = expr_root;
new_context
}
/// Creates a builder for `EmitContext`.
///
/// # Returns
/// A new instance of `EmitContextBuilder`.
pub fn builder() -> EmitContextBuilder {
EmitContextBuilder::default()
}
}
/// Builder for `EmitContext` to provide a fluent API for customization.
#[derive(Debug, Clone)]
pub struct EmitContextBuilder {
indent: usize,
indent_step: usize,
format_number_hex: bool,
verbosity: EmitVerbosity,
indent_style: IndentStyle,
expr_root: bool,
include_ssa_versions: bool,
}
impl EmitContextBuilder {
/// Sets the initial indentation level.
pub fn indent(mut self, indent: usize) -> Self {
self.indent = indent;
self
}
/// Sets the number of spaces per indentation step.
pub fn indent_step(mut self, indent_step: usize) -> Self {
self.indent_step = indent_step;
self
}
/// Configures whether to format numbers in hexadecimal.
pub fn format_number_hex(mut self, format_number_hex: bool) -> Self {
self.format_number_hex = format_number_hex;
self
}
/// Sets the verbosity mode.
pub fn verbosity(mut self, verbosity: EmitVerbosity) -> Self {
self.verbosity = verbosity;
self
}
/// Sets the indentation style.
pub fn indent_style(mut self, indent_style: IndentStyle) -> Self {
self.indent_style = indent_style;
self
}
/// Sets the `expr_root` flag.
pub fn expr_root(mut self, expr_root: bool) -> Self {
self.expr_root = expr_root;
self
}
/// Sets the `include_ssa_versions` flag.
pub fn include_ssa_versions(mut self, include_ssa_versions: bool) -> Self {
self.include_ssa_versions = include_ssa_versions;
self
}
/// Builds the `EmitContext` with the specified parameters.
pub fn build(self) -> EmitContext {
EmitContext {
indent: self.indent,
indent_step: self.indent_step,
format_number_hex: self.format_number_hex,
verbosity: self.verbosity,
indent_style: self.indent_style,
expr_root: self.expr_root,
include_ssa_versions: self.include_ssa_versions,
}
}
}
// == Other Implementations for EmitContext ==
impl Default for EmitContextBuilder {
fn default() -> Self {
Self {
indent: 0,
indent_step: 4,
format_number_hex: false,
verbosity: EmitVerbosity::Pretty,
indent_style: IndentStyle::Allman,
expr_root: true,
include_ssa_versions: false,
}
}
}
impl Default for EmitContext {
fn default() -> Self {
EmitContextBuilder::default().build()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_emit_context_with_indent() {
let context = EmitContext::default();
let new_context = context.with_indent();
assert_eq!(new_context.indent, 4);
}
#[test]
fn test_emit_context_scoped() {
let mut context = EmitContext::default();
let body_context = context.scoped(|ctx| ctx.with_indent());
assert_eq!(body_context.indent, 4);
}
#[test]
fn test_builder_default_values() {
let builder = EmitContext::builder();
let context = builder.build();
assert_eq!(context.indent, 0);
assert_eq!(context.indent_step, 4);
assert!(!context.format_number_hex);
assert_eq!(context.verbosity, EmitVerbosity::Pretty);
assert_eq!(context.indent_style, IndentStyle::Allman);
}
#[test]
fn test_builder_custom_values() {
let context = EmitContext::builder()
.indent(2)
.indent_step(8)
.format_number_hex(true)
.verbosity(EmitVerbosity::Debug)
.indent_style(IndentStyle::KAndR)
.expr_root(true)
.include_ssa_versions(true)
.build();
assert_eq!(context.indent, 2);
assert_eq!(context.indent_step, 8);
assert!(context.format_number_hex);
assert_eq!(context.verbosity, EmitVerbosity::Debug);
assert_eq!(context.indent_style, IndentStyle::KAndR);
assert!(context.expr_root);
assert!(context.include_ssa_versions);
}
}