mod comments;
mod emit;
#[cfg(test)]
mod tests;
use bock_lexer::Lexer;
use bock_parser::Parser;
use bock_source::SourceFile;
pub use emit::Formatter;
#[derive(Debug)]
pub struct FormatResult {
pub output: String,
pub changed: bool,
}
#[must_use]
pub fn format_source(source: &str, filename: &str) -> FormatResult {
let file = SourceFile::new(
bock_errors::FileId(0),
std::path::PathBuf::from(filename),
source.to_string(),
);
let mut lexer = Lexer::new(&file);
let tokens = lexer.tokenize();
if lexer.diagnostics().has_errors() {
return FormatResult {
output: source.to_string(),
changed: false,
};
}
let mut parser = Parser::new(tokens, &file);
let module = parser.parse_module();
if parser.diagnostics().has_errors() {
return FormatResult {
output: source.to_string(),
changed: false,
};
}
let comments = comments::extract_comments(source);
let mut formatter = Formatter::new(&comments, source);
formatter.format_module(&module);
let output = formatter.finish();
let changed = output != source;
FormatResult { output, changed }
}