diff --git a/crates/planus-cli/src/app/check.rs b/crates/planus-cli/src/app/check.rs index 867cdaef..125d3513 100644 --- a/crates/planus-cli/src/app/check.rs +++ b/crates/planus-cli/src/app/check.rs @@ -2,7 +2,7 @@ use std::{path::PathBuf, process::ExitCode}; use clap::{Parser, ValueHint}; use color_eyre::Result; -use planus_translation::translate_files; +use planus_translation::translate_files_with_options; /// Check validity of .fbs files #[derive(Parser)] @@ -12,8 +12,8 @@ pub struct Command { } impl Command { - pub fn run(self, _options: super::AppOptions) -> Result { - if translate_files(&self.files).is_none() { + pub fn run(self, options: super::AppOptions) -> Result { + if translate_files_with_options(&self.files, options.to_converter_options()).is_none() { Ok(ExitCode::FAILURE) } else { Ok(ExitCode::SUCCESS) diff --git a/crates/planus-cli/src/app/dot.rs b/crates/planus-cli/src/app/dot.rs index 1aaaa7a3..5c4e924b 100644 --- a/crates/planus-cli/src/app/dot.rs +++ b/crates/planus-cli/src/app/dot.rs @@ -3,7 +3,7 @@ use std::{io::Write, path::PathBuf, process::ExitCode}; use clap::{Parser, ValueHint}; use color_eyre::Result; use planus_codegen::generate_dot; -use planus_translation::translate_files; +use planus_translation::translate_files_with_options; /// Generate a dot graph #[derive(Parser)] @@ -18,8 +18,10 @@ pub struct Command { } impl Command { - pub fn run(self, _options: super::AppOptions) -> Result { - let Some(declarations) = translate_files(&self.files) else { + pub fn run(self, options: super::AppOptions) -> Result { + let Some(declarations) = + translate_files_with_options(&self.files, options.to_converter_options()) + else { return Ok(ExitCode::FAILURE); }; diff --git a/crates/planus-cli/src/app/mod.rs b/crates/planus-cli/src/app/mod.rs index 96d08725..58f33ee3 100644 --- a/crates/planus-cli/src/app/mod.rs +++ b/crates/planus-cli/src/app/mod.rs @@ -12,6 +12,7 @@ use clap::{ Parser, }; use color_eyre::Result; +use planus_translation::ConverterOptions; fn clap_v3_styling() -> Styles { Styles::styled() @@ -42,7 +43,18 @@ pub enum Command { } #[derive(Default, Parser)] -pub struct AppOptions {} +pub struct AppOptions { + #[clap(long)] + pub ignore_docstring_errors: bool, +} + +impl AppOptions { + pub fn to_converter_options(&self) -> ConverterOptions { + ConverterOptions { + ignore_docstring_errors: self.ignore_docstring_errors, + } + } +} impl App { pub fn run(self) -> Result { diff --git a/crates/planus-cli/src/app/rust.rs b/crates/planus-cli/src/app/rust.rs index a180d1f6..aa43df68 100644 --- a/crates/planus-cli/src/app/rust.rs +++ b/crates/planus-cli/src/app/rust.rs @@ -3,7 +3,7 @@ use std::{io::Write, path::PathBuf, process::ExitCode}; use clap::{Parser, ValueHint}; use color_eyre::Result; use planus_codegen::generate_rust; -use planus_translation::translate_files; +use planus_translation::translate_files_with_options; /// Generate rust code #[derive(Parser)] @@ -18,8 +18,10 @@ pub struct Command { } impl Command { - pub fn run(self, _options: super::AppOptions) -> Result { - let Some(declarations) = translate_files(&self.files) else { + pub fn run(self, options: super::AppOptions) -> Result { + let Some(declarations) = + translate_files_with_options(&self.files, options.to_converter_options()) + else { return Ok(ExitCode::FAILURE); }; diff --git a/crates/planus-cli/src/app/view.rs b/crates/planus-cli/src/app/view.rs index b4ab9cf4..ea5f7f60 100644 --- a/crates/planus-cli/src/app/view.rs +++ b/crates/planus-cli/src/app/view.rs @@ -16,8 +16,13 @@ pub struct Command { } impl Command { - pub fn run(self, _options: super::AppOptions) -> Result { + pub fn run(self, options: super::AppOptions) -> Result { let buffer = std::fs::read(&self.data_file)?; - planus_inspector::app::run_app(&self.schema_file, &self.root_type, &buffer) + planus_inspector::app::run_app( + &self.schema_file, + &self.root_type, + &buffer, + options.to_converter_options(), + ) } } diff --git a/crates/planus-inspector/src/app.rs b/crates/planus-inspector/src/app.rs index a8ebfb22..8c7bb303 100644 --- a/crates/planus-inspector/src/app.rs +++ b/crates/planus-inspector/src/app.rs @@ -7,14 +7,19 @@ use crossterm::{ terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, }; use fuzzy_matcher::FuzzyMatcher; -use planus_translation::translate_files; +use planus_translation::{translate_files_with_options, ConverterOptions}; use planus_types::intermediate::{AbsolutePath, DeclarationIndex, DeclarationKind}; use tui::{backend::CrosstermBackend, Terminal}; use crate::{run_inspector, Inspector}; -pub fn run_app(schema_file: &Path, root_type: &str, buffer: &[u8]) -> Result { - let Some(declarations) = translate_files(&[schema_file]) else { +pub fn run_app( + schema_file: &Path, + root_type: &str, + buffer: &[u8], + converter_options: ConverterOptions, +) -> Result { + let Some(declarations) = translate_files_with_options(&[schema_file], converter_options) else { return Ok(ExitCode::FAILURE); }; diff --git a/crates/planus-translation/src/ast_convert.rs b/crates/planus-translation/src/ast_convert.rs index 764d016c..8e6b1b1b 100644 --- a/crates/planus-translation/src/ast_convert.rs +++ b/crates/planus-translation/src/ast_convert.rs @@ -9,19 +9,31 @@ use planus_types::{ast::*, cst}; use crate::{ctx::Ctx, error::ErrorKind, pretty_print::PrettyPrinter}; struct CstConverter<'ctx> { - pub schema: Schema, - pub ctx: &'ctx Ctx, - pub current_span: Span, - pub allow_outer_docstrings: bool, + schema: Schema, + ctx: &'ctx Ctx, + current_span: Span, + allow_outer_docstrings: bool, + converter_options: ConverterOptions, } -pub fn convert(ctx: &Ctx, file_id: FileId, schema: cst::Schema<'_>) -> Schema { +#[derive(Copy, Clone, Default, Debug)] +pub struct ConverterOptions { + pub ignore_docstring_errors: bool, +} + +pub fn convert( + ctx: &Ctx, + file_id: FileId, + schema: cst::Schema<'_>, + converter_options: ConverterOptions, +) -> Schema { let location = format!("* File `{}`", ctx.get_filename(file_id).display()); let mut converter = CstConverter { schema: Schema::new(file_id, location), ctx, current_span: schema.span, allow_outer_docstrings: true, + converter_options, }; for decl in &schema.declarations { converter.convert_declaration(decl); @@ -1199,7 +1211,7 @@ impl<'ctx> CstConverter<'ctx> { span: comment.span, value: comment.content.to_owned(), }); - } else { + } else if !self.converter_options.ignore_docstring_errors { self.emit_error( ErrorKind::FILE_ORDER, [Label::primary(self.schema.file_id, comment.span)], @@ -1229,11 +1241,13 @@ impl<'ctx> CstConverter<'ctx> { match comment.kind { CommentKind::Comment => (), CommentKind::OuterDocstring => { - self.emit_error( - ErrorKind::MISC_SEMANTIC_ERROR, - [Label::primary(self.schema.file_id, comment.span)], - Some("Doc comments are not meaningful here."), - ); + if !self.converter_options.ignore_docstring_errors { + self.emit_error( + ErrorKind::MISC_SEMANTIC_ERROR, + [Label::primary(self.schema.file_id, comment.span)], + Some("Doc comments are not meaningful here."), + ); + } self.allow_outer_docstrings = false; } CommentKind::InnerDocstring => { @@ -1243,11 +1257,13 @@ impl<'ctx> CstConverter<'ctx> { value: comment.content.to_owned(), }); } else { - self.emit_error( - ErrorKind::MISC_SEMANTIC_ERROR, - [Label::primary(self.schema.file_id, comment.span)], - Some("Inner doc comments are not meaningful here."), - ); + if !self.converter_options.ignore_docstring_errors { + self.emit_error( + ErrorKind::MISC_SEMANTIC_ERROR, + [Label::primary(self.schema.file_id, comment.span)], + Some("Inner doc comments are not meaningful here."), + ); + } } } } diff --git a/crates/planus-translation/src/intermediate_language/ast_map.rs b/crates/planus-translation/src/intermediate_language/ast_map.rs index 666a3769..89a8e56d 100644 --- a/crates/planus-translation/src/intermediate_language/ast_map.rs +++ b/crates/planus-translation/src/intermediate_language/ast_map.rs @@ -3,6 +3,7 @@ use codespan_reporting::diagnostic::Label; use planus_types::ast::Schema; use crate::{ + ast_convert::ConverterOptions, ctx::Ctx, util::sorted_map::{sorted_map, SortedMap, SortedSet}, }; @@ -10,9 +11,17 @@ use crate::{ #[derive(Default)] pub struct AstMap { asts: SortedMap)>, + converter_options: ConverterOptions, } impl AstMap { + pub fn new_with_options(converter_options: ConverterOptions) -> Self { + Self { + asts: Default::default(), + converter_options, + } + } + pub fn add_files_recursively(&mut self, ctx: &mut Ctx, file_id: FileId) { let mut queue = vec![file_id]; @@ -21,7 +30,8 @@ impl AstMap { sorted_map::Entry::Occupied(_) => continue, sorted_map::Entry::Vacant(entry) => { if let Some(cst) = ctx.parse_file(file_id) { - let ast = crate::ast_convert::convert(ctx, file_id, cst); + let ast = + crate::ast_convert::convert(ctx, file_id, cst, self.converter_options); let dependencies = ast .includes .iter() @@ -117,7 +127,11 @@ mod tests { (Schema::new(file_id2, String::new()), vec![file_id3]), ); asts.insert(file_id3, (Schema::new(file_id3, String::new()), vec![])); - let reachability = AstMap { asts }.reachability(); + let reachability = AstMap { + asts, + converter_options: Default::default(), + } + .reachability(); let mut file_id1_reach = [file_id1, file_id2, file_id3]; let mut file_id2_reach = [file_id2, file_id3]; let file_id3_reach = [file_id3]; diff --git a/crates/planus-translation/src/intermediate_language/mod.rs b/crates/planus-translation/src/intermediate_language/mod.rs index 120d16fe..2c805e43 100644 --- a/crates/planus-translation/src/intermediate_language/mod.rs +++ b/crates/planus-translation/src/intermediate_language/mod.rs @@ -2,7 +2,7 @@ use std::path::Path; use planus_types::intermediate::Declarations; -use crate::ctx::Ctx; +use crate::{ast_convert::ConverterOptions, ctx::Ctx}; pub mod checks; pub mod translation; @@ -10,8 +10,15 @@ pub mod translation; pub mod ast_map; pub fn translate_files(input_files: &[impl AsRef]) -> Option { + translate_files_with_options(input_files, Default::default()) +} + +pub fn translate_files_with_options( + input_files: &[impl AsRef], + converter_options: ConverterOptions, +) -> Option { let mut ctx = Ctx::default(); - let declarations = translate_files_with_context(&mut ctx, input_files); + let declarations = translate_files_with_context(&mut ctx, input_files, converter_options); if !ctx.has_errors() { Some(declarations) } else { @@ -22,8 +29,9 @@ pub fn translate_files(input_files: &[impl AsRef]) -> Option fn translate_files_with_context>( ctx: &mut crate::ctx::Ctx, input_files: &[P], + converter_options: ConverterOptions, ) -> Declarations { - let mut ast_map = ast_map::AstMap::default(); + let mut ast_map = ast_map::AstMap::new_with_options(converter_options); for file in input_files { if let Some(file_id) = ctx.add_file(file, []) { ast_map.add_files_recursively(ctx, file_id); diff --git a/crates/planus-translation/src/lib.rs b/crates/planus-translation/src/lib.rs index 13333f77..10c87799 100644 --- a/crates/planus-translation/src/lib.rs +++ b/crates/planus-translation/src/lib.rs @@ -19,8 +19,9 @@ mod util; use std::path::Path; +pub use ast_convert::ConverterOptions; pub use error::ErrorKind; -pub use intermediate_language::translate_files; +pub use intermediate_language::{translate_files, translate_files_with_options}; pub fn format_file(path: &impl AsRef, ignore_errors: bool) -> Option { let mut ctx = ctx::Ctx::default();