-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command to build a graph from a CST and extract CLI commands into…
… the runtime library (#983) Closes #981 1. Exposes the `metaslang_graph_builder` functionality in the generated languages runtime, parameterized with the language `KindTypes`. 2. Exercise the graph builder via a test in `testlang`. 3. Extracts the CLI command from `slang_solidity` into the runtime crate to be able to easily reuse it from other generated languages. 4. Adds a new `build-graph` command to the common CLI commands to run a source file through an arbitrary `.msgb` file and build a graph. 5. Expose the language's root non-terminal kind in the public API. Items 1, 2 and 4 are only available when building with the private feature flag `__graph_builder`.
- Loading branch information
Showing
35 changed files
with
817 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@nomicfoundation/slang": patch | ||
--- | ||
|
||
Expose the language root non-terminal kind at `Language.rootKind()`. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
crates/codegen/runtime/cargo/src/runtime/cli/commands/build_graph.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use std::fs; | ||
use std::path::PathBuf; | ||
|
||
use semver::Version; | ||
|
||
use super::parse::parse_source_file; | ||
use super::CommandError; | ||
use crate::graph_builder::{ | ||
ExecutionConfig, File as GraphBuilderFile, Functions, NoCancellation, Variables, | ||
}; | ||
|
||
pub fn execute( | ||
file_path_string: &str, | ||
version: Version, | ||
msgb_path_string: &str, | ||
output_json: bool, | ||
debug: bool, | ||
) -> Result<(), CommandError> { | ||
let parse_output = parse_source_file(file_path_string, version, |_| ())?; | ||
let msgb = parse_graph_builder(msgb_path_string)?; | ||
|
||
let functions = Functions::stdlib(); | ||
let variables = Variables::new(); | ||
let mut execution_config = ExecutionConfig::new(&functions, &variables); | ||
if debug { | ||
execution_config = execution_config.debug_attributes( | ||
"_location".into(), | ||
"_variable".into(), | ||
"_match".into(), | ||
); | ||
} | ||
|
||
let tree = parse_output.create_tree_cursor(); | ||
let graph = msgb.execute(&tree, &execution_config, &NoCancellation)?; | ||
|
||
if output_json { | ||
graph.display_json(None)?; | ||
} else { | ||
print!("{}", graph.pretty_print()); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn parse_graph_builder(msgb_path_string: &str) -> Result<GraphBuilderFile, CommandError> { | ||
let msgb_path = PathBuf::from(&msgb_path_string) | ||
.canonicalize() | ||
.map_err(|_| CommandError::FileNotFound(msgb_path_string.to_string()))?; | ||
|
||
let msgb_source = fs::read_to_string(&msgb_path)?; | ||
GraphBuilderFile::from_str(&msgb_source).map_err(|parser_error| { | ||
let error_message = parser_error | ||
.display_pretty(&msgb_path, &msgb_source) | ||
.to_string(); | ||
CommandError::ParseFailed(error_message) | ||
}) | ||
} |
24 changes: 24 additions & 0 deletions
24
crates/codegen/runtime/cargo/src/runtime/cli/commands/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use thiserror::Error; | ||
|
||
#[cfg(feature = "__graph_builder")] | ||
pub mod build_graph; | ||
pub mod parse; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum CommandError { | ||
#[error("File not found: {0:?}")] | ||
FileNotFound(String), | ||
|
||
#[error(transparent)] | ||
Io(#[from] std::io::Error), | ||
|
||
#[error(transparent)] | ||
LanguageError(#[from] crate::language::Error), | ||
|
||
#[error("Parsing failed: {0}")] | ||
ParseFailed(String), | ||
|
||
#[cfg(feature = "__graph_builder")] | ||
#[error(transparent)] | ||
ExecutionFailed(#[from] crate::graph_builder::ExecutionError), | ||
} |
52 changes: 52 additions & 0 deletions
52
crates/codegen/runtime/cargo/src/runtime/cli/commands/parse.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use std::fs; | ||
use std::path::PathBuf; | ||
|
||
use semver::Version; | ||
|
||
use super::CommandError; | ||
use crate::diagnostic; | ||
use crate::language::Language; | ||
use crate::parse_output::ParseOutput; | ||
|
||
pub fn execute(file_path_string: &str, version: Version, json: bool) -> Result<(), CommandError> { | ||
parse_source_file(file_path_string, version, |output| { | ||
if json { | ||
let root_node = output.tree(); | ||
let json = serde_json::to_string_pretty(&root_node).expect("JSON serialization failed"); | ||
println!("{json}"); | ||
} | ||
}) | ||
.map(|_| ()) | ||
} | ||
|
||
pub(crate) fn parse_source_file<F>( | ||
file_path_string: &str, | ||
version: Version, | ||
run_before_checking: F, | ||
) -> Result<ParseOutput, CommandError> | ||
where | ||
F: FnOnce(&ParseOutput), | ||
{ | ||
let file_path = PathBuf::from(&file_path_string) | ||
.canonicalize() | ||
.map_err(|_| CommandError::FileNotFound(file_path_string.to_string()))?; | ||
|
||
let input = fs::read_to_string(file_path)?; | ||
let language = Language::new(version)?; | ||
let parse_output = language.parse(Language::ROOT_KIND, &input); | ||
|
||
run_before_checking(&parse_output); | ||
|
||
if parse_output.is_valid() { | ||
Ok(parse_output) | ||
} else { | ||
const COLOR: bool = true; | ||
let report = parse_output | ||
.errors() | ||
.iter() | ||
.map(|error| diagnostic::render(error, file_path_string, &input, COLOR)) | ||
.collect::<Vec<_>>() | ||
.join("\n"); | ||
Err(CommandError::ParseFailed(report)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use std::process::ExitCode; | ||
|
||
use clap::Subcommand; | ||
use semver::Version; | ||
|
||
pub mod commands; | ||
|
||
#[derive(Subcommand, Debug)] | ||
pub enum Commands { | ||
/// Parses a source file, and outputs any syntax errors, or a JSON concrete syntax tree | ||
Parse { | ||
/// File path to the source file to parse | ||
file_path: String, | ||
|
||
/// The language version to use for parsing | ||
#[arg(short, long)] | ||
version: Version, | ||
|
||
/// Print the concrete syntax tree as JSON | ||
#[clap(long)] | ||
json: bool, | ||
}, | ||
|
||
// This is only intended for internal development | ||
#[cfg(feature = "__graph_builder")] | ||
/// Parses a source file and builds a graph executing the instructions from the builder file (*.msgb) | ||
BuildGraph { | ||
/// File path to the source file to parse | ||
file_path: String, | ||
|
||
/// The language version to use for parsing | ||
#[arg(short, long)] | ||
version: Version, | ||
|
||
/// The graph buider (.msgb) file to use | ||
msgb_path: String, | ||
|
||
/// Print the graph as JSON | ||
#[clap(long)] | ||
json: bool, | ||
|
||
/// Include debug info (location, variable and match) in the built graph | ||
#[clap(long)] | ||
debug: bool, | ||
}, | ||
} | ||
|
||
impl Commands { | ||
pub fn execute(self) -> ExitCode { | ||
let command_result = match self { | ||
Commands::Parse { | ||
file_path, | ||
version, | ||
json, | ||
} => commands::parse::execute(&file_path, version, json), | ||
#[cfg(feature = "__graph_builder")] | ||
Commands::BuildGraph { | ||
file_path, | ||
version, | ||
msgb_path, | ||
json, | ||
debug, | ||
} => commands::build_graph::execute(&file_path, version, &msgb_path, json, debug), | ||
}; | ||
match command_result { | ||
Ok(()) => ExitCode::SUCCESS, | ||
Err(error) => { | ||
eprintln!("{error}"); | ||
ExitCode::FAILURE | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
crates/codegen/runtime/cargo/src/runtime/generated/language.rs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
crates/codegen/runtime/npm/src/runtime/napi-bindings/generated/index.d.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.