diff --git a/crates/codegen/runtime/cargo/src/runtime/bindings.rs.jinja2 b/crates/codegen/runtime/cargo/src/runtime/bindings.rs.jinja2 index 68b43dc8db..6eace04490 100644 --- a/crates/codegen/runtime/cargo/src/runtime/bindings.rs.jinja2 +++ b/crates/codegen/runtime/cargo/src/runtime/bindings.rs.jinja2 @@ -1,11 +1,12 @@ use crate::bindings_support::Bindings; -use crate::language::Language; +use semver::Version; -pub fn create_for(language: Language) -> Bindings { +#[allow(clippy::needless_pass_by_value)] +pub fn create_for(version: Version) -> Bindings { {%- if model.bindings.graph_builder_path -%} - Bindings::create(language, GRAPH_BUILDER_SOURCE) + Bindings::create(version, GRAPH_BUILDER_SOURCE) {%- else -%} - _ = language; + _ = version; {%- if rendering_in_stubs -%} unreachable!("Bindings not available in stubs"); {%- else -%} @@ -15,7 +16,7 @@ pub fn create_for(language: Language) -> Bindings { } {%- if model.bindings.graph_builder_path -%} - const GRAPH_BUILDER_SOURCE: &'static str = include_str!("{{ model.bindings.graph_builder_path }}"); + const GRAPH_BUILDER_SOURCE: &str = include_str!("{{ model.bindings.graph_builder_path }}"); {%- else -%} mod supress_dependencies { use { stack_graphs as _ }; diff --git a/crates/codegen/runtime/cargo/src/runtime/bindings_support/mod.rs b/crates/codegen/runtime/cargo/src/runtime/bindings_support/mod.rs index e559c328c3..5d1c12ac6a 100644 --- a/crates/codegen/runtime/cargo/src/runtime/bindings_support/mod.rs +++ b/crates/codegen/runtime/cargo/src/runtime/bindings_support/mod.rs @@ -1,77 +1,54 @@ -pub mod stack_graph { - use metaslang_graph_builder::stack_graph; - pub use metaslang_graph_builder::stack_graph::{ - BuildError, NoCancellation, FILE_PATH_VAR, ROOT_PATH_VAR, - }; +use metaslang_graph_builder::stack_graph::{self, NoCancellation}; - use super::super::metaslang_cst::KindTypes; +use super::metaslang_cst::KindTypes; +type StackGraphLanguage = stack_graph::StackGraphLanguage; - pub type Builder<'a> = stack_graph::Builder<'a, KindTypes>; - pub type StackGraphLanguage = stack_graph::StackGraphLanguage; -} - -use std::fs; +use semver::Version; +use stack_graphs::graph::StackGraph; use std::io; -use std::path::Path; +use std::path::PathBuf; use thiserror::Error; -use stack_graphs::graph::StackGraph; -use crate::language::Language; +use crate::cursor::Cursor; use crate::graph_builder::File as GraphBuilderFile; use crate::graph_builder::Variables; -use crate::parse_error::ParseError; - -use self::stack_graph::StackGraphLanguage; #[derive(Error, Debug)] pub enum BindingsError { #[error(transparent)] Io(#[from] std::io::Error), - #[error("Parsing source file failed")] - ParseError(Vec), - #[error(transparent)] BuildError(#[from] metaslang_graph_builder::stack_graph::BuildError), } pub struct Bindings { - language: Language, + version: Version, stack_graph: StackGraph, sgl: StackGraphLanguage, } impl Bindings { #[allow(dead_code)] - pub(crate) fn create(language: Language, msgb_source: &str) -> Self { + pub(crate) fn create(version: Version, msgb_source: &str) -> Self { let graph_builder_file = GraphBuilderFile::from_str(msgb_source) .expect("Bindings stack graph builder parse error"); let sgl = StackGraphLanguage::new(graph_builder_file); let stack_graph = StackGraph::new(); Self { - language, + version, stack_graph, sgl, } } - pub fn add_file(&mut self, source_file_path: &Path) -> Result<(), BindingsError> { - let input = fs::read_to_string(source_file_path)?; - let parse_output = self.language.parse(Language::ROOT_KIND, &input); - if !parse_output.is_valid() { - return Err(BindingsError::ParseError(parse_output.errors)) - } - let tree_cursor = parse_output.create_tree_cursor(); - - let root_path = source_file_path + pub fn add_file(&mut self, file_path: &str, tree_cursor: Cursor) -> Result<(), BindingsError> { + let path = PathBuf::from(&file_path).canonicalize()?; + let root_path = path .parent() .ok_or(io::Error::from(io::ErrorKind::InvalidData))?; - let file_name = source_file_path - .file_name() - .and_then(|name| name.to_str()) - .unwrap_or(""); - let file = self.stack_graph.get_or_create_file(file_name); + let file = self.stack_graph.get_or_create_file(file_path); let mut globals = Variables::new(); // TODO: add the Language version as well to allow semantic changes between versions @@ -81,17 +58,20 @@ impl Bindings { root_path.to_str().unwrap().into(), ) .expect("failed to add ROOT_PATH variable"); + globals + .add(stack_graph::FILE_PATH_VAR.into(), file_path.into()) + .expect("failed to add FILE_PATH variable"); globals .add( - stack_graph::FILE_PATH_VAR.into(), - source_file_path.to_str().unwrap().into(), + stack_graph::VERSION_VAR.into(), + self.version.to_string().into(), ) - .expect("failed to add FILE_PATH variable"); + .expect("failed to add VERSION_VAR variable"); let mut builder = self.sgl .builder_into_stack_graph(&mut self.stack_graph, file, tree_cursor); - builder.build(&globals, &stack_graph::NoCancellation)?; + builder.build(&globals, &NoCancellation)?; Ok(()) } diff --git a/crates/codegen/runtime/cargo/src/runtime/cli/commands/bindings.rs b/crates/codegen/runtime/cargo/src/runtime/cli/commands/bindings.rs index 6f5d7e998d..afd93e7d6e 100644 --- a/crates/codegen/runtime/cargo/src/runtime/cli/commands/bindings.rs +++ b/crates/codegen/runtime/cargo/src/runtime/cli/commands/bindings.rs @@ -1,22 +1,16 @@ use semver::Version; -use std::path::PathBuf; use crate::bindings; -use crate::language::Language; -use super::CommandError; pub fn execute( file_path_string: &str, version: Version, ) -> Result<(), super::CommandError> { - let language = Language::new(version)?; - let mut bindings = bindings::create_for(language); + let mut bindings = bindings::create_for(version.clone()); + let parse_output = super::parse::parse_source_file(file_path_string, version, |_| ())?; + let tree_cursor = parse_output.create_tree_cursor(); - let file_path = PathBuf::from(&file_path_string) - .canonicalize() - .map_err(|_| CommandError::FileNotFound(file_path_string.to_string()))?; - - bindings.add_file(&file_path)?; + bindings.add_file(file_path_string, tree_cursor)?; // print_defs_and_refs(&bindings); // resolve_refs(&bindings); diff --git a/crates/codegen/runtime/cargo/src/runtime/generated/bindings.rs b/crates/codegen/runtime/cargo/src/runtime/generated/bindings.rs index 165c8a9905..456a72f342 100644 --- a/crates/codegen/runtime/cargo/src/runtime/generated/bindings.rs +++ b/crates/codegen/runtime/cargo/src/runtime/generated/bindings.rs @@ -1,10 +1,12 @@ // This file is generated automatically by infrastructure scripts. Please don't edit by hand. +use semver::Version; + use crate::bindings_support::Bindings; -use crate::language::Language; -pub fn create_for(language: Language) -> Bindings { - _ = language; +#[allow(clippy::needless_pass_by_value)] +pub fn create_for(version: Version) -> Bindings { + _ = version; unreachable!("Bindings not available in stubs"); } mod supress_dependencies { diff --git a/crates/metaslang/graph_builder/src/stack_graph/builder.rs b/crates/metaslang/graph_builder/src/stack_graph/builder.rs index 69caadfae4..90b273f2fc 100644 --- a/crates/metaslang/graph_builder/src/stack_graph/builder.rs +++ b/crates/metaslang/graph_builder/src/stack_graph/builder.rs @@ -76,6 +76,9 @@ pub const FILE_PATH_VAR: &str = "FILE_PATH"; /// Name of the variable used to pass the root path. /// If given, should be an ancestor of the file path. pub const ROOT_PATH_VAR: &str = "ROOT_PATH"; +/// Version of the language being processed, to apply different semantic rules +pub const VERSION_VAR: &str = "VERSION"; + pub struct Builder<'a, KT: KindTypes> { sgl: &'a StackGraphLanguage, diff --git a/crates/metaslang/graph_builder/src/stack_graph/mod.rs b/crates/metaslang/graph_builder/src/stack_graph/mod.rs index 50cfc3edc0..d6cb986d61 100644 --- a/crates/metaslang/graph_builder/src/stack_graph/mod.rs +++ b/crates/metaslang/graph_builder/src/stack_graph/mod.rs @@ -306,7 +306,7 @@ mod builder; mod cancellation; mod functions; -pub use builder::{BuildError, Builder, FILE_PATH_VAR, ROOT_PATH_VAR}; +pub use builder::{BuildError, Builder, FILE_PATH_VAR, ROOT_PATH_VAR, VERSION_VAR}; pub use cancellation::{CancellationFlag, NoCancellation}; /// Holds information about how to construct stack graphs for a particular language. diff --git a/crates/solidity/outputs/cargo/slang_solidity/src/generated/bindings_support/mod.rs b/crates/solidity/outputs/cargo/slang_solidity/src/generated/bindings_support/mod.rs index 2565e26f5e..6b369f725e 100644 --- a/crates/solidity/outputs/cargo/slang_solidity/src/generated/bindings_support/mod.rs +++ b/crates/solidity/outputs/cargo/slang_solidity/src/generated/bindings_support/mod.rs @@ -1,77 +1,56 @@ // This file is generated automatically by infrastructure scripts. Please don't edit by hand. -pub mod stack_graph { - use metaslang_graph_builder::stack_graph; - pub use metaslang_graph_builder::stack_graph::{ - BuildError, NoCancellation, FILE_PATH_VAR, ROOT_PATH_VAR, - }; +use metaslang_graph_builder::stack_graph::{self, NoCancellation}; - use super::super::metaslang_cst::KindTypes; +use super::metaslang_cst::KindTypes; +type StackGraphLanguage = stack_graph::StackGraphLanguage; - pub type Builder<'a> = stack_graph::Builder<'a, KindTypes>; - pub type StackGraphLanguage = stack_graph::StackGraphLanguage; -} - -use std::path::Path; -use std::{fs, io}; +use std::io; +use std::path::PathBuf; +use semver::Version; use stack_graphs::graph::StackGraph; use thiserror::Error; -use self::stack_graph::StackGraphLanguage; +use crate::cursor::Cursor; use crate::graph_builder::{File as GraphBuilderFile, Variables}; -use crate::language::Language; -use crate::parse_error::ParseError; #[derive(Error, Debug)] pub enum BindingsError { #[error(transparent)] Io(#[from] std::io::Error), - #[error("Parsing source file failed")] - ParseError(Vec), - #[error(transparent)] BuildError(#[from] metaslang_graph_builder::stack_graph::BuildError), } pub struct Bindings { - language: Language, + version: Version, stack_graph: StackGraph, sgl: StackGraphLanguage, } impl Bindings { #[allow(dead_code)] - pub(crate) fn create(language: Language, msgb_source: &str) -> Self { + pub(crate) fn create(version: Version, msgb_source: &str) -> Self { let graph_builder_file = GraphBuilderFile::from_str(msgb_source) .expect("Bindings stack graph builder parse error"); let sgl = StackGraphLanguage::new(graph_builder_file); let stack_graph = StackGraph::new(); Self { - language, + version, stack_graph, sgl, } } - pub fn add_file(&mut self, source_file_path: &Path) -> Result<(), BindingsError> { - let input = fs::read_to_string(source_file_path)?; - let parse_output = self.language.parse(Language::ROOT_KIND, &input); - if !parse_output.is_valid() { - return Err(BindingsError::ParseError(parse_output.errors)); - } - let tree_cursor = parse_output.create_tree_cursor(); - - let root_path = source_file_path + pub fn add_file(&mut self, file_path: &str, tree_cursor: Cursor) -> Result<(), BindingsError> { + let path = PathBuf::from(&file_path).canonicalize()?; + let root_path = path .parent() .ok_or(io::Error::from(io::ErrorKind::InvalidData))?; - let file_name = source_file_path - .file_name() - .and_then(|name| name.to_str()) - .unwrap_or(""); - let file = self.stack_graph.get_or_create_file(file_name); + let file = self.stack_graph.get_or_create_file(file_path); let mut globals = Variables::new(); // TODO: add the Language version as well to allow semantic changes between versions @@ -81,17 +60,20 @@ impl Bindings { root_path.to_str().unwrap().into(), ) .expect("failed to add ROOT_PATH variable"); + globals + .add(stack_graph::FILE_PATH_VAR.into(), file_path.into()) + .expect("failed to add FILE_PATH variable"); globals .add( - stack_graph::FILE_PATH_VAR.into(), - source_file_path.to_str().unwrap().into(), + stack_graph::VERSION_VAR.into(), + self.version.to_string().into(), ) - .expect("failed to add FILE_PATH variable"); + .expect("failed to add VERSION_VAR variable"); let mut builder = self.sgl .builder_into_stack_graph(&mut self.stack_graph, file, tree_cursor); - builder.build(&globals, &stack_graph::NoCancellation)?; + builder.build(&globals, &NoCancellation)?; Ok(()) } diff --git a/crates/solidity/outputs/cargo/slang_solidity/src/generated/cli/commands/bindings.rs b/crates/solidity/outputs/cargo/slang_solidity/src/generated/cli/commands/bindings.rs index d0b5d7d1a5..fcf1ba3b12 100644 --- a/crates/solidity/outputs/cargo/slang_solidity/src/generated/cli/commands/bindings.rs +++ b/crates/solidity/outputs/cargo/slang_solidity/src/generated/cli/commands/bindings.rs @@ -1,22 +1,15 @@ // This file is generated automatically by infrastructure scripts. Please don't edit by hand. -use std::path::PathBuf; - use semver::Version; -use super::CommandError; use crate::bindings; -use crate::language::Language; pub fn execute(file_path_string: &str, version: Version) -> Result<(), super::CommandError> { - let language = Language::new(version)?; - let mut bindings = bindings::create_for(language); - - let file_path = PathBuf::from(&file_path_string) - .canonicalize() - .map_err(|_| CommandError::FileNotFound(file_path_string.to_string()))?; + let mut bindings = bindings::create_for(version.clone()); + let parse_output = super::parse::parse_source_file(file_path_string, version, |_| ())?; + let tree_cursor = parse_output.create_tree_cursor(); - bindings.add_file(&file_path)?; + bindings.add_file(file_path_string, tree_cursor)?; // print_defs_and_refs(&bindings); // resolve_refs(&bindings); diff --git a/crates/solidity/outputs/cargo/slang_solidity/src/generated/generated/bindings.rs b/crates/solidity/outputs/cargo/slang_solidity/src/generated/generated/bindings.rs index 093a239f6d..51a24adec7 100644 --- a/crates/solidity/outputs/cargo/slang_solidity/src/generated/generated/bindings.rs +++ b/crates/solidity/outputs/cargo/slang_solidity/src/generated/generated/bindings.rs @@ -1,9 +1,11 @@ // This file is generated automatically by infrastructure scripts. Please don't edit by hand. +use semver::Version; + use crate::bindings_support::Bindings; -use crate::language::Language; -pub fn create_for(language: Language) -> Bindings { - Bindings::create(language, GRAPH_BUILDER_SOURCE) +#[allow(clippy::needless_pass_by_value)] +pub fn create_for(version: Version) -> Bindings { + Bindings::create(version, GRAPH_BUILDER_SOURCE) } -const GRAPH_BUILDER_SOURCE: &'static str = include_str!("../../../generated/stack-graph.msgb"); +const GRAPH_BUILDER_SOURCE: &str = include_str!("../../../generated/stack-graph.msgb"); diff --git a/crates/testlang/outputs/cargo/slang_testlang/src/generated/bindings_support/mod.rs b/crates/testlang/outputs/cargo/slang_testlang/src/generated/bindings_support/mod.rs index 2565e26f5e..6b369f725e 100644 --- a/crates/testlang/outputs/cargo/slang_testlang/src/generated/bindings_support/mod.rs +++ b/crates/testlang/outputs/cargo/slang_testlang/src/generated/bindings_support/mod.rs @@ -1,77 +1,56 @@ // This file is generated automatically by infrastructure scripts. Please don't edit by hand. -pub mod stack_graph { - use metaslang_graph_builder::stack_graph; - pub use metaslang_graph_builder::stack_graph::{ - BuildError, NoCancellation, FILE_PATH_VAR, ROOT_PATH_VAR, - }; +use metaslang_graph_builder::stack_graph::{self, NoCancellation}; - use super::super::metaslang_cst::KindTypes; +use super::metaslang_cst::KindTypes; +type StackGraphLanguage = stack_graph::StackGraphLanguage; - pub type Builder<'a> = stack_graph::Builder<'a, KindTypes>; - pub type StackGraphLanguage = stack_graph::StackGraphLanguage; -} - -use std::path::Path; -use std::{fs, io}; +use std::io; +use std::path::PathBuf; +use semver::Version; use stack_graphs::graph::StackGraph; use thiserror::Error; -use self::stack_graph::StackGraphLanguage; +use crate::cursor::Cursor; use crate::graph_builder::{File as GraphBuilderFile, Variables}; -use crate::language::Language; -use crate::parse_error::ParseError; #[derive(Error, Debug)] pub enum BindingsError { #[error(transparent)] Io(#[from] std::io::Error), - #[error("Parsing source file failed")] - ParseError(Vec), - #[error(transparent)] BuildError(#[from] metaslang_graph_builder::stack_graph::BuildError), } pub struct Bindings { - language: Language, + version: Version, stack_graph: StackGraph, sgl: StackGraphLanguage, } impl Bindings { #[allow(dead_code)] - pub(crate) fn create(language: Language, msgb_source: &str) -> Self { + pub(crate) fn create(version: Version, msgb_source: &str) -> Self { let graph_builder_file = GraphBuilderFile::from_str(msgb_source) .expect("Bindings stack graph builder parse error"); let sgl = StackGraphLanguage::new(graph_builder_file); let stack_graph = StackGraph::new(); Self { - language, + version, stack_graph, sgl, } } - pub fn add_file(&mut self, source_file_path: &Path) -> Result<(), BindingsError> { - let input = fs::read_to_string(source_file_path)?; - let parse_output = self.language.parse(Language::ROOT_KIND, &input); - if !parse_output.is_valid() { - return Err(BindingsError::ParseError(parse_output.errors)); - } - let tree_cursor = parse_output.create_tree_cursor(); - - let root_path = source_file_path + pub fn add_file(&mut self, file_path: &str, tree_cursor: Cursor) -> Result<(), BindingsError> { + let path = PathBuf::from(&file_path).canonicalize()?; + let root_path = path .parent() .ok_or(io::Error::from(io::ErrorKind::InvalidData))?; - let file_name = source_file_path - .file_name() - .and_then(|name| name.to_str()) - .unwrap_or(""); - let file = self.stack_graph.get_or_create_file(file_name); + let file = self.stack_graph.get_or_create_file(file_path); let mut globals = Variables::new(); // TODO: add the Language version as well to allow semantic changes between versions @@ -81,17 +60,20 @@ impl Bindings { root_path.to_str().unwrap().into(), ) .expect("failed to add ROOT_PATH variable"); + globals + .add(stack_graph::FILE_PATH_VAR.into(), file_path.into()) + .expect("failed to add FILE_PATH variable"); globals .add( - stack_graph::FILE_PATH_VAR.into(), - source_file_path.to_str().unwrap().into(), + stack_graph::VERSION_VAR.into(), + self.version.to_string().into(), ) - .expect("failed to add FILE_PATH variable"); + .expect("failed to add VERSION_VAR variable"); let mut builder = self.sgl .builder_into_stack_graph(&mut self.stack_graph, file, tree_cursor); - builder.build(&globals, &stack_graph::NoCancellation)?; + builder.build(&globals, &NoCancellation)?; Ok(()) } diff --git a/crates/testlang/outputs/cargo/slang_testlang/src/generated/cli/commands/bindings.rs b/crates/testlang/outputs/cargo/slang_testlang/src/generated/cli/commands/bindings.rs index d0b5d7d1a5..fcf1ba3b12 100644 --- a/crates/testlang/outputs/cargo/slang_testlang/src/generated/cli/commands/bindings.rs +++ b/crates/testlang/outputs/cargo/slang_testlang/src/generated/cli/commands/bindings.rs @@ -1,22 +1,15 @@ // This file is generated automatically by infrastructure scripts. Please don't edit by hand. -use std::path::PathBuf; - use semver::Version; -use super::CommandError; use crate::bindings; -use crate::language::Language; pub fn execute(file_path_string: &str, version: Version) -> Result<(), super::CommandError> { - let language = Language::new(version)?; - let mut bindings = bindings::create_for(language); - - let file_path = PathBuf::from(&file_path_string) - .canonicalize() - .map_err(|_| CommandError::FileNotFound(file_path_string.to_string()))?; + let mut bindings = bindings::create_for(version.clone()); + let parse_output = super::parse::parse_source_file(file_path_string, version, |_| ())?; + let tree_cursor = parse_output.create_tree_cursor(); - bindings.add_file(&file_path)?; + bindings.add_file(file_path_string, tree_cursor)?; // print_defs_and_refs(&bindings); // resolve_refs(&bindings); diff --git a/crates/testlang/outputs/cargo/slang_testlang/src/generated/generated/bindings.rs b/crates/testlang/outputs/cargo/slang_testlang/src/generated/generated/bindings.rs index d41c1778a0..8698da4554 100644 --- a/crates/testlang/outputs/cargo/slang_testlang/src/generated/generated/bindings.rs +++ b/crates/testlang/outputs/cargo/slang_testlang/src/generated/generated/bindings.rs @@ -1,10 +1,12 @@ // This file is generated automatically by infrastructure scripts. Please don't edit by hand. +use semver::Version; + use crate::bindings_support::Bindings; -use crate::language::Language; -pub fn create_for(language: Language) -> Bindings { - _ = language; +#[allow(clippy::needless_pass_by_value)] +pub fn create_for(version: Version) -> Bindings { + _ = version; unreachable!("Language does not define binding rules"); } mod supress_dependencies {