Create a Diagnostic quen the main .60 file can't be loaded

This commit is contained in:
Olivier Goffart 2021-03-15 11:42:35 +01:00
parent 2ae5b8413a
commit cae88f0823
5 changed files with 24 additions and 11 deletions

View file

@ -93,9 +93,6 @@ pub enum CompileError {
/// Cannot read environment variable CARGO_MANIFEST_DIR or OUT_DIR. The build script need to be run via cargo. /// Cannot read environment variable CARGO_MANIFEST_DIR or OUT_DIR. The build script need to be run via cargo.
#[error("Cannot read environment variable CARGO_MANIFEST_DIR or OUT_DIR. The build script need to be run via cargo.")] #[error("Cannot read environment variable CARGO_MANIFEST_DIR or OUT_DIR. The build script need to be run via cargo.")]
NotRunViaCargo, NotRunViaCargo,
/// Cannot load the input .60 file
#[error("Cannot load the .60 file: {0}")]
LoadError(std::io::Error),
/// Parse error. The error are printed in the stderr, and also are in the vector /// Parse error. The error are printed in the stderr, and also are in the vector
#[error("{0:?}")] #[error("{0:?}")]
CompileError(Vec<String>), CompileError(Vec<String>),
@ -183,8 +180,7 @@ pub fn compile_with_config(
.join(path.as_ref()); .join(path.as_ref());
let mut diag = BuildDiagnostics::default(); let mut diag = BuildDiagnostics::default();
let syntax_node = sixtyfps_compilerlib::parser::parse_file(&path, &mut diag) let syntax_node = sixtyfps_compilerlib::parser::parse_file(&path, &mut diag);
.map_err(CompileError::LoadError)?;
if diag.has_error() { if diag.has_error() {
let vec = diag.to_string_vec(); let vec = diag.to_string_vec();
@ -200,6 +196,8 @@ pub fn compile_with_config(
} }
}; };
let syntax_node = syntax_node.expect("diags contained no compilation errors");
// 'spin_on' is ok here because the compiler in single threaded and does not block if there is no blocking future // 'spin_on' is ok here because the compiler in single threaded and does not block if there is no blocking future
let (doc, mut diag) = spin_on::spin_on(sixtyfps_compilerlib::compile_syntax_node( let (doc, mut diag) = spin_on::spin_on(sixtyfps_compilerlib::compile_syntax_node(
syntax_node, syntax_node,

View file

@ -329,7 +329,7 @@ pub fn sixtyfps(stream: TokenStream) -> TokenStream {
let source_file = if let Some(cargo_manifest) = std::env::var_os("CARGO_MANIFEST_DIR") { let source_file = if let Some(cargo_manifest) = std::env::var_os("CARGO_MANIFEST_DIR") {
let mut path: std::path::PathBuf = cargo_manifest.into(); let mut path: std::path::PathBuf = cargo_manifest.into();
path.push("Cargo.toml"); path.push("Cargo.toml");
Some(diagnostics::SourceFileInner::from_path(path)) Some(diagnostics::SourceFileInner::from_path_only(path))
} else { } else {
None None
}; };

View file

@ -85,7 +85,8 @@ impl SourceFileInner {
&self.path &self.path
} }
pub fn from_path(path: PathBuf) -> Rc<Self> { /// Create a SourceFile that has just a path, but no contents
pub fn from_path_only(path: PathBuf) -> Rc<Self> {
Rc::new(Self { path, ..Default::default() }) Rc::new(Self { path, ..Default::default() })
} }
@ -106,6 +107,17 @@ impl SourceFileInner {
pub type SourceFile = Rc<SourceFileInner>; pub type SourceFile = Rc<SourceFileInner>;
pub fn load_from_path(path: &Path) -> Result<String, Diagnostic> {
std::fs::read_to_string(path).map_err(|err| Diagnostic {
message: format!("Could not load {}: {}", path.display(), err),
span: SourceLocation {
source_file: Some(SourceFileInner::from_path_only(path.to_owned())),
span: Default::default(),
},
level: DiagnosticLevel::Error,
})
}
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct SourceLocation { pub struct SourceLocation {
pub source_file: Option<SourceFile>, pub source_file: Option<SourceFile>,

View file

@ -837,9 +837,11 @@ pub fn parse(
pub fn parse_file<P: AsRef<std::path::Path>>( pub fn parse_file<P: AsRef<std::path::Path>>(
path: P, path: P,
build_diagnostics: &mut BuildDiagnostics, build_diagnostics: &mut BuildDiagnostics,
) -> std::io::Result<SyntaxNodeWithSourceFile> { ) -> Option<SyntaxNodeWithSourceFile> {
let source = std::fs::read_to_string(&path)?; let source = crate::diagnostics::load_from_path(path.as_ref())
Ok(parse(source, Some(path.as_ref()), build_diagnostics)) .map_err(|d| build_diagnostics.push_internal_error(d))
.ok()?;
Some(parse(source, Some(path.as_ref()), build_diagnostics))
} }
pub fn parse_tokens(tokens: Vec<Token>, diags: &mut BuildDiagnostics) -> SyntaxNode { pub fn parse_tokens(tokens: Vec<Token>, diags: &mut BuildDiagnostics) -> SyntaxNode {

View file

@ -43,7 +43,7 @@ struct Cli {
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
let args = Cli::from_args(); let args = Cli::from_args();
let mut diag = BuildDiagnostics::default(); let mut diag = BuildDiagnostics::default();
let syntax_node = parser::parse_file(&args.path, &mut diag)?; let syntax_node = parser::parse_file(&args.path, &mut diag);
//println!("{:#?}", syntax_node); //println!("{:#?}", syntax_node);
if diag.has_error() { if diag.has_error() {
diag.print(); diag.print();
@ -51,6 +51,7 @@ fn main() -> std::io::Result<()> {
} }
let mut compiler_config = CompilerConfiguration::new(args.format); let mut compiler_config = CompilerConfiguration::new(args.format);
compiler_config.include_paths = args.include_paths; compiler_config.include_paths = args.include_paths;
let syntax_node = syntax_node.expect("diags contained no compilation errors");
let (doc, diag) = spin_on::spin_on(compile_syntax_node(syntax_node, diag, compiler_config)); let (doc, diag) = spin_on::spin_on(compile_syntax_node(syntax_node, diag, compiler_config));
let mut diag = diag.check_and_exit_on_error(); let mut diag = diag.check_and_exit_on_error();