Skip to content

Commit

Permalink
Remove source parsing from the Bindings and add version variable
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiraldez committed Jun 12, 2024
1 parent 2c41a75 commit bc33c5c
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 167 deletions.
11 changes: 6 additions & 5 deletions crates/codegen/runtime/cargo/src/runtime/bindings.rs.jinja2
Original file line number Diff line number Diff line change
@@ -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 -%}
Expand All @@ -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 _ };
Expand Down
62 changes: 21 additions & 41 deletions crates/codegen/runtime/cargo/src/runtime/bindings_support/mod.rs
Original file line number Diff line number Diff line change
@@ -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<KindTypes>;

pub type Builder<'a> = stack_graph::Builder<'a, KindTypes>;
pub type StackGraphLanguage = stack_graph::StackGraphLanguage<KindTypes>;
}

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<ParseError>),

#[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("<unknown>");
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
Expand All @@ -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(())
}
Expand Down
14 changes: 4 additions & 10 deletions crates/codegen/runtime/cargo/src/runtime/cli/commands/bindings.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down

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

3 changes: 3 additions & 0 deletions crates/metaslang/graph_builder/src/stack_graph/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<KT>,
Expand Down
2 changes: 1 addition & 1 deletion crates/metaslang/graph_builder/src/stack_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

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

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

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

Loading

0 comments on commit bc33c5c

Please sign in to comment.