Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split binding rules into multiple parts and process built-ins with a subset #1174

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/codegen/language/definition/src/model/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Language {
pub name: Identifier,

pub documentation_dir: PathBuf,
pub binding_rules_file: PathBuf,
pub binding_rules_dir: PathBuf,
pub file_extension: Option<String>,

pub root_item: Identifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
codegen_language_macros::compile!(Language(
name = Foo,
documentation_dir = "foo/bar",
binding_rules_file = "bindings/rules.msgb",
binding_rules_dir = "bindings",
root_item = Bar,
leading_trivia = Sequence([]),
trailing_trivia = Sequence([]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
codegen_language_macros::compile!(Language(
name = Foo,
documentation_dir = "foo/bar",
binding_rules_file = "bindings/rules.msgb",
binding_rules_dir = "bindings",
root_item = Bar1,
leading_trivia = Sequence([]),
trailing_trivia = Sequence([]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
codegen_language_macros::compile!(Language(
name = Foo,
documentation_dir = "foo/bar",
binding_rules_file = "bindings/rules.msgb",
binding_rules_dir = "bindings",
root_item = Bar,
leading_trivia = Sequence([]),
trailing_trivia = Sequence([]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
codegen_language_macros::compile!(Language(
name = Foo,
documentation_dir = "foo/bar",
binding_rules_file = "bindings/rules.msgb",
binding_rules_dir = "bindings",
root_item = Bar,
leading_trivia = Sequence([]),
trailing_trivia = Sequence([]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
codegen_language_macros::compile!(Language(
name = Foo,
documentation_dir = "foo/bar",
binding_rules_file = "bindings/rules.msgb",
binding_rules_dir = "bindings",
root_item = Bar,
leading_trivia = Sequence([]),
trailing_trivia = Sequence([]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
codegen_language_macros::compile!(Language(
name = Foo,
documentation_dir = "foo/bar",
binding_rules_file = "bindings/rules.msgb",
binding_rules_dir = "bindings",
root_item = Bar,
leading_trivia = Sequence([]),
trailing_trivia = Sequence([]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
codegen_language_macros::compile!(Language(
name = Foo,
documentation_dir = "foo/bar",
binding_rules_file = "bindings/rules.msgb",
binding_rules_dir = "bindings",
root_item = Bar,
leading_trivia = Sequence([]),
trailing_trivia = Sequence([]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
codegen_language_macros::compile!(Language(
name = Foo,
documentation_dir = "foo/bar",
binding_rules_file = "bindings/rules.msgb",
binding_rules_dir = "bindings",
root_item = Bar,
leading_trivia = Sequence([]),
trailing_trivia = Sequence([]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
codegen_language_macros::compile!(Language(
name = Foo,
documentation_dir = "foo/bar",
binding_rules_file = "bindings/rules.msgb",
binding_rules_dir = "bindings",
root_item = Bar,
leading_trivia = Sequence([]),
trailing_trivia = Sequence([]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
codegen_language_macros::compile!(Language(
name = Foo,
documentation_dir = "foo/bar",
binding_rules_file = "bindings/rules.msgb",
binding_rules_dir = "bindings",
root_item = Bar1,
leading_trivia = Sequence([]),
trailing_trivia = Sequence([]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
codegen_language_macros::compile!(Language(
name = Foo,
documentation_dir = "foo/bar",
binding_rules_file = "bindings/rules.msgb",
binding_rules_dir = "bindings",
root_item = Bar1,
leading_trivia = Sequence([]),
trailing_trivia = Sequence([]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
codegen_language_macros::compile!(Language(
name = Foo,
documentation_dir = "foo/bar",
binding_rules_file = "bindings/rules.msgb",
binding_rules_dir = "bindings",
root_item = Bar,
leading_trivia = Sequence([]),
trailing_trivia = Sequence([]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
codegen_language_macros::compile!(Language(
name = Foo,
documentation_dir = "foo/bar",
binding_rules_file = "bindings/rules.msgb",
binding_rules_dir = "bindings",
root_item = One,
leading_trivia = Sequence([]),
trailing_trivia = Sequence([]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
codegen_language_macros::compile!(Language(
name = Foo,
documentation_dir = "foo/bar",
binding_rules_file = "bindings/rules.msgb",
binding_rules_dir = "bindings",
root_item = One,
leading_trivia = Sequence([]),
trailing_trivia = Sequence([]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
codegen_language_macros::compile!(Language(
name = Foo,
documentation_dir = "foo/bar",
binding_rules_file = "bindings/rules.msgb",
binding_rules_dir = "bindings",
root_item = Bar,
leading_trivia = Sequence([]),
trailing_trivia = Sequence([]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
codegen_language_macros::compile!(Language(
name = Foo,
documentation_dir = "foo/bar",
binding_rules_file = "bindings/rules.msgb",
binding_rules_dir = "bindings",
root_item = Bar,
leading_trivia = Sequence([]),
trailing_trivia = Sequence([]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
codegen_language_macros::compile!(Language(
name = Foo,
documentation_dir = "foo/bar",
binding_rules_file = "bindings/rules.msgb",
binding_rules_dir = "bindings",
root_item = Bar,
leading_trivia = Sequence([]),
trailing_trivia = Sequence([]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
codegen_language_macros::compile!(Language(
name = Foo,
documentation_dir = "foo/bar",
binding_rules_file = "bindings/rules.msgb",
binding_rules_dir = "bindings",
root_item = One,
leading_trivia = Sequence([]),
trailing_trivia = Sequence([]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
codegen_language_macros::compile!(Language(
name = Foo,
documentation_dir = "foo/bar",
binding_rules_file = "bindings/rules.msgb",
binding_rules_dir = "bindings",
root_item = One,
leading_trivia = Sequence([]),
trailing_trivia = Sequence([]),
Expand Down
4 changes: 2 additions & 2 deletions crates/codegen/language/tests/src/pass/tiny_language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use semver::Version;
codegen_language_macros::compile!(Language(
name = Tiny,
documentation_dir = "tiny/docs",
binding_rules_file = "tiny/bindings/rules.msgb",
binding_rules_dir = "tiny/bindings",
file_extension = ".tiny",
root_item = Foo,
leading_trivia = Sequence([]),
Expand Down Expand Up @@ -50,7 +50,7 @@ fn definition() {
Language {
name: "Tiny".into(),
documentation_dir: Path::repo_path("tiny/docs"),
binding_rules_file: Path::repo_path("tiny/bindings/rules.msgb"),
binding_rules_dir: Path::repo_path("tiny/bindings"),
file_extension: Some(".tiny".into()),
root_item: "Foo".into(),
leading_trivia: TriviaParser::Sequence { parsers: [].into() },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::parser::ParserInitializationError;
#[allow(clippy::needless_pass_by_value)]
pub fn add_built_ins(
_binding_graph: &mut BindingGraph,
_version: Version,
_version: &Version,
) -> Result<(), ParserInitializationError> {
unreachable!("Built-ins are Solidity-specific")
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#[allow(clippy::needless_raw_string_hashes)]
#[allow(dead_code)] // TODO(#982): use to create the graph
pub const BINDING_RULES_SOURCE: &str = r#####"
{{ model.bindings.binding_rules_source }}
pub const USER_BINDING_RULES_SOURCE: &str = r#####"
{{ model.bindings.user_binding_rules_source }}
"#####;

#[allow(clippy::needless_raw_string_hashes)]
pub const SYSTEM_BINDING_RULES_SOURCE: &str = r#####"
{{ model.bindings.system_binding_rules_source }}
"#####;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions crates/codegen/runtime/cargo/crate/src/runtime/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,29 @@ pub enum BindingGraphInitializationError {
ParserInitialization(#[from] ParserInitializationError),
}

#[allow(clippy::needless_pass_by_value)]
pub fn create_with_resolver(
version: Version,
resolver: Rc<dyn PathResolver<KindTypes>>,
) -> Result<BindingGraph, ParserInitializationError> {
let mut binding_graph = BindingGraph::create(
version.clone(),
binding_rules::BINDING_RULES_SOURCE,
binding_rules::USER_BINDING_RULES_SOURCE,
binding_rules::SYSTEM_BINDING_RULES_SOURCE,
resolver,
);

crate::extensions::bindings::add_built_ins(&mut binding_graph, version)?;
crate::extensions::bindings::add_built_ins(&mut binding_graph, &version)?;

Ok(binding_graph)
}

#[cfg(feature = "__private_testing_utils")]
pub fn get_binding_rules() -> &'static str {
binding_rules::BINDING_RULES_SOURCE
pub fn get_user_binding_rules() -> &'static str {
binding_rules::USER_BINDING_RULES_SOURCE
}

#[cfg(feature = "__private_testing_utils")]
pub fn get_system_binding_rules() -> &'static str {
binding_rules::SYSTEM_BINDING_RULES_SOURCE
}
30 changes: 23 additions & 7 deletions crates/codegen/runtime/generator/src/bindings/mod.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
use std::collections::BTreeSet;
use std::path::Path;

use anyhow::Result;
use codegen_language_definition::model;
use infra_utils::codegen::CodegenFileSystem;
use infra_utils::paths::PathExtensions;
use semver::Version;
use serde::Serialize;

#[derive(Default, Serialize)]
pub struct BindingsModel {
binding_rules_source: String,
user_binding_rules_source: String,
system_binding_rules_source: String,
built_ins_versions: BTreeSet<Version>,
file_extension: String,
}

impl BindingsModel {
pub fn from_language(language: &model::Language) -> Result<Self> {
// We use `CodegenFileSystem` here to ensure the rules are rebuilt if the rules file changes
let binding_rules_dir = language.binding_rules_file.unwrap_parent();
let mut fs = CodegenFileSystem::new(binding_rules_dir)?;
let binding_rules_source = fs.read_file(&language.binding_rules_file)?;
let binding_rules_dir = &language.binding_rules_dir;
let user_binding_rules_source = assemble_rules(binding_rules_dir, "user-rules.parts")?;
let system_binding_rules_source = assemble_rules(binding_rules_dir, "system-rules.parts")?;
let built_ins_versions = language.collect_built_ins_versions();
let file_extension = language.file_extension.clone().unwrap_or_default();

Ok(Self {
binding_rules_source,
user_binding_rules_source,
system_binding_rules_source,
built_ins_versions,
file_extension,
})
}
}

// Builds a rules file by concatenating the file parts listed in the given `parts_file`
fn assemble_rules(rules_dir: &Path, parts_file: &str) -> Result<String> {
// We use `CodegenFileSystem` here to ensure the rules are rebuilt if the rules file changes
let mut fs = CodegenFileSystem::new(rules_dir)?;
let parts_contents = fs.read_file(rules_dir.join(parts_file))?;
let mut parts = Vec::new();
for part_name in parts_contents.lines() {
if part_name.is_empty() {
continue;
}
parts.push(fs.read_file(rules_dir.join(part_name))?);
}
Ok(parts.join("\n"))
}
2 changes: 1 addition & 1 deletion crates/metaslang/bindings/generated/public_api.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading