Skip to content

Commit

Permalink
Add an option to ignore docstring errors
Browse files Browse the repository at this point in the history
This implements the feature requested in #215.
  • Loading branch information
TethysSvensson committed Jan 3, 2024
1 parent 7d36b6a commit f39eacf
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 37 deletions.
6 changes: 3 additions & 3 deletions crates/planus-cli/src/app/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -12,8 +12,8 @@ pub struct Command {
}

impl Command {
pub fn run(self, _options: super::AppOptions) -> Result<ExitCode> {
if translate_files(&self.files).is_none() {
pub fn run(self, options: super::AppOptions) -> Result<ExitCode> {
if translate_files_with_options(&self.files, options.to_converter_options()).is_none() {
Ok(ExitCode::FAILURE)
} else {
Ok(ExitCode::SUCCESS)
Expand Down
8 changes: 5 additions & 3 deletions crates/planus-cli/src/app/dot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -18,8 +18,10 @@ pub struct Command {
}

impl Command {
pub fn run(self, _options: super::AppOptions) -> Result<ExitCode> {
let Some(declarations) = translate_files(&self.files) else {
pub fn run(self, options: super::AppOptions) -> Result<ExitCode> {
let Some(declarations) =
translate_files_with_options(&self.files, options.to_converter_options())
else {
return Ok(ExitCode::FAILURE);
};

Expand Down
14 changes: 13 additions & 1 deletion crates/planus-cli/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use clap::{
Parser,
};
use color_eyre::Result;
use planus_translation::ConverterOptions;

fn clap_v3_styling() -> Styles {
Styles::styled()
Expand Down Expand Up @@ -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<ExitCode> {
Expand Down
8 changes: 5 additions & 3 deletions crates/planus-cli/src/app/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -18,8 +18,10 @@ pub struct Command {
}

impl Command {
pub fn run(self, _options: super::AppOptions) -> Result<ExitCode> {
let Some(declarations) = translate_files(&self.files) else {
pub fn run(self, options: super::AppOptions) -> Result<ExitCode> {
let Some(declarations) =
translate_files_with_options(&self.files, options.to_converter_options())
else {
return Ok(ExitCode::FAILURE);
};

Expand Down
9 changes: 7 additions & 2 deletions crates/planus-cli/src/app/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ pub struct Command {
}

impl Command {
pub fn run(self, _options: super::AppOptions) -> Result<ExitCode> {
pub fn run(self, options: super::AppOptions) -> Result<ExitCode> {
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(),
)
}
}
11 changes: 8 additions & 3 deletions crates/planus-inspector/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExitCode> {
let Some(declarations) = translate_files(&[schema_file]) else {
pub fn run_app(
schema_file: &Path,
root_type: &str,
buffer: &[u8],
converter_options: ConverterOptions,
) -> Result<ExitCode> {
let Some(declarations) = translate_files_with_options(&[schema_file], converter_options) else {
return Ok(ExitCode::FAILURE);
};

Expand Down
48 changes: 32 additions & 16 deletions crates/planus-translation/src/ast_convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)],
Expand Down Expand Up @@ -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 => {
Expand All @@ -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."),
);
}
}
}
}
Expand Down
18 changes: 16 additions & 2 deletions crates/planus-translation/src/intermediate_language/ast_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@ use codespan_reporting::diagnostic::Label;
use planus_types::ast::Schema;

use crate::{
ast_convert::ConverterOptions,
ctx::Ctx,
util::sorted_map::{sorted_map, SortedMap, SortedSet},
};

#[derive(Default)]
pub struct AstMap {
asts: SortedMap<FileId, (Schema, Vec<FileId>)>,
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];

Expand All @@ -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()
Expand Down Expand Up @@ -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];
Expand Down
14 changes: 11 additions & 3 deletions crates/planus-translation/src/intermediate_language/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@ 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;

pub mod ast_map;

pub fn translate_files(input_files: &[impl AsRef<Path>]) -> Option<Declarations> {
translate_files_with_options(input_files, Default::default())
}

pub fn translate_files_with_options(
input_files: &[impl AsRef<Path>],
converter_options: ConverterOptions,
) -> Option<Declarations> {
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 {
Expand All @@ -22,8 +29,9 @@ pub fn translate_files(input_files: &[impl AsRef<Path>]) -> Option<Declarations>
fn translate_files_with_context<P: AsRef<Path>>(
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);
Expand Down
3 changes: 2 additions & 1 deletion crates/planus-translation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path>, ignore_errors: bool) -> Option<String> {
let mut ctx = ctx::Ctx::default();
Expand Down

0 comments on commit f39eacf

Please sign in to comment.