Skip to content

Commit

Permalink
Move .msgb into the language/inputs crate and install into language/o…
Browse files Browse the repository at this point in the history
…utputs

The .msgb file is linked from the `Language` definition in order to embed the
rules file as string when compiling the output language.
  • Loading branch information
ggiraldez committed Jun 11, 2024
1 parent 4f5cc3c commit f8998cf
Show file tree
Hide file tree
Showing 21 changed files with 479 additions and 13 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ slang_solidity = { path = "crates/solidity/outputs/cargo/slang_solidity" }
slang_solidity_node_addon = { path = "crates/solidity/outputs/cargo/slang_solidity_node_addon" }
solidity_cargo_tests = { path = "crates/solidity/outputs/cargo/tests" }
solidity_language = { path = "crates/solidity/inputs/language" }
solidity_stack_graph = { path = "crates/solidity/inputs/graph" }
solidity_npm_package = { path = "crates/solidity/outputs/npm/package" }
solidity_spec = { path = "crates/solidity/outputs/spec" }
solidity_testing_sanctuary = { path = "crates/solidity/testing/sanctuary" }
Expand Down
1 change: 1 addition & 0 deletions crates/codegen/language/definition/src/model/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Language {
pub name: Identifier,

pub documentation_dir: PathBuf,
pub bindings_graph_builder_path: Option<PathBuf>,
pub root_item: Identifier,

pub leading_trivia: TriviaParser,
Expand Down
1 change: 1 addition & 0 deletions crates/codegen/language/tests/src/pass/tiny_language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ fn definition() {
Language {
name: "Tiny".into(),
documentation_dir: Path::repo_path("tiny/docs"),
bindings_graph_builder_path: None,
root_item: "Foo".into(),
leading_trivia: TriviaParser::Sequence { parsers: [].into() },
trailing_trivia: TriviaParser::Sequence { parsers: [].into() },
Expand Down
9 changes: 9 additions & 0 deletions crates/codegen/runtime/cargo/src/runtime/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ pub mod stack_graph {
pub type Builder<'a> = stack_graph::Builder<'a, KindTypes>;
pub type StackGraphLanguage = stack_graph::StackGraphLanguage<KindTypes>;
}

use metaslang_graph_builder::ParseError;

use super::bindings_builder;
use super::graph_builder::File as GraphBuilderFile;

pub fn get_stack_graph_builder() -> Result<GraphBuilderFile, ParseError> {
GraphBuilderFile::from_str(bindings_builder::GRAPH_BUILDER_SOURCE)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// TODO: we will want to disable all the bindings API if the language doesn't
// have a .msgb stack graph builder

pub const GRAPH_BUILDER_SOURCE: &str =
{%- if model.bindings.graph_builder_path -%}
include_str!("{{ model.bindings.graph_builder_path }}");
{%- else -%}
"";
{%- endif -%}

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

4 changes: 4 additions & 0 deletions crates/codegen/runtime/cargo/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,7 @@ pub mod graph_builder {

#[cfg(feature = "__graph_builder")]
pub mod bindings;

#[cfg(feature = "__graph_builder")]
#[path = "generated/bindings_buider.rs"]
mod bindings_builder;
24 changes: 23 additions & 1 deletion crates/codegen/runtime/generator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::{fs, io};

use anyhow::Result;
use anyhow::{Context, Result};
use codegen_language_definition::model::Language;
use infra_utils::cargo::CargoWorkspace;
use infra_utils::codegen::CodegenTemplates;
Expand Down Expand Up @@ -40,6 +41,27 @@ impl OutputLanguage {
templates.render_directory(model, output_dir)
}

pub fn copy_resources(&self, language: &Rc<Language>, output_dir: &Path) -> Result<()> {
if !matches!(self, Self::Cargo) {
return Ok(());
}

// Copy the bindings stack graph builder file if any
if let Some(graph_builder_path) = &language.bindings_graph_builder_path {
let graph_builder_filename = graph_builder_path.file_name().ok_or(io::Error::new(
io::ErrorKind::InvalidData,
"expected a filename",
))?;
let output_path = output_dir.join(graph_builder_filename);
fs::create_dir_all(output_dir)?;
fs::copy(graph_builder_path, &output_path).with_context(|| {
format!("Failed to copy {graph_builder_path:?} into {output_path:?}")
})?;
}

Ok(())
}

pub fn generate_stubs(&self) -> Result<()> {
let model = ModelWrapper {
rendering_in_stubs: true,
Expand Down
21 changes: 21 additions & 0 deletions crates/codegen/runtime/generator/src/model.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::BTreeSet;
use std::path::PathBuf;
use std::rc::Rc;

use codegen_language_definition::model::Language;
Expand All @@ -17,6 +18,7 @@ pub struct RuntimeModel {
parser: ParserModel,
ast: AstModel,
kinds: KindsModel,
bindings: BindingsModel,
}

impl RuntimeModel {
Expand All @@ -27,6 +29,25 @@ impl RuntimeModel {
ast: AstModel::create(language),
parser: ParserModel::from_language(language),
kinds: KindsModel::create(language),
bindings: BindingsModel::create(language),
}
}
}

#[derive(Default, Serialize)]
struct BindingsModel {
graph_builder_path: Option<PathBuf>,
}

impl BindingsModel {
fn create(language: &Rc<Language>) -> Self {
let graph_builder_path = language.bindings_graph_builder_path.as_ref().and_then(|path| {
let filename = path.file_name()?;
let path = PathBuf::from("..").join("..").join("generated").join(filename);
Some(path)
});
Self {
graph_builder_path,
}
}
}
10 changes: 0 additions & 10 deletions crates/solidity/inputs/graph/Cargo.toml

This file was deleted.

Empty file.
1 change: 1 addition & 0 deletions crates/solidity/inputs/language/src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub use solidity::SolidityDefinition;
codegen_language_macros::compile!(Language(
name = Solidity,
documentation_dir = "crates/solidity/inputs/language/docs",
bindings_graph_builder_path = "crates/solidity/inputs/language/bindings/stack-graph.msgb",
root_item = SourceUnit,
// TODO(#638): For now this is on par with the DSL v1 definition to minimize the fallout.
// We should replace this with the new definition from #629.
Expand Down
4 changes: 3 additions & 1 deletion crates/solidity/outputs/cargo/slang_solidity/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ fn main() -> Result<()> {
let language = SolidityDefinition::create();

let output_dir = CargoWorkspace::locate_source_crate("slang_solidity")?.join("src/generated");
let resources_dir = CargoWorkspace::locate_source_crate("slang_solidity")?.join("generated");

OutputLanguage::Cargo.generate_runtime(&language, &output_dir)
OutputLanguage::Cargo.generate_runtime(&language, &output_dir)?;
OutputLanguage::Cargo.copy_resources(&language, &resources_dir)
}
Loading

0 comments on commit f8998cf

Please sign in to comment.