-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separate
slang_solidity_cli
into a separate crate (#1079)
and update documentation everywhere.
- Loading branch information
1 parent
7870dc9
commit 43b389e
Showing
36 changed files
with
734 additions
and
396 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": minor | ||
--- | ||
|
||
Move the Rust CLI into a separate `slang_solidity_cli` crate. |
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
17 changes: 0 additions & 17 deletions
17
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 |
---|---|---|
@@ -1,18 +1 @@ | ||
use thiserror::Error; | ||
|
||
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), | ||
} |
85 changes: 46 additions & 39 deletions
85
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 |
---|---|---|
@@ -1,52 +1,59 @@ | ||
use std::fs; | ||
use std::path::PathBuf; | ||
|
||
use clap::Parser; | ||
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| { | ||
#[derive(Parser, Debug)] | ||
pub struct ParseCommand { | ||
/// File path to the source file to parse | ||
file_path: PathBuf, | ||
|
||
/// The language version to use for parsing | ||
#[arg(short, long)] | ||
version: Version, | ||
|
||
/// Print the concrete syntax tree as JSON | ||
#[clap(long)] | ||
json: bool, | ||
} | ||
|
||
impl ParseCommand { | ||
pub fn execute(self) { | ||
let Self { | ||
file_path, | ||
version, | ||
json, | ||
} = self; | ||
|
||
let file_path = file_path | ||
.canonicalize() | ||
.unwrap_or_else(|_| panic!("File not found: {file_path:?}")); | ||
|
||
let input = fs::read_to_string(&file_path).unwrap(); | ||
let language = Language::new(version).unwrap(); | ||
let parse_output = language.parse(Language::ROOT_KIND, &input); | ||
|
||
if !parse_output.is_valid() { | ||
const COLOR: bool = true; | ||
|
||
let report = parse_output | ||
.errors() | ||
.iter() | ||
.map(|error| diagnostic::render(error, file_path.to_str().unwrap(), &input, COLOR)) | ||
.collect::<Vec<_>>() | ||
.join("\n"); | ||
|
||
panic!("Parse failed:\n{report}"); | ||
} | ||
|
||
if json { | ||
let root_node = output.tree(); | ||
let json = serde_json::to_string_pretty(&root_node).expect("JSON serialization failed"); | ||
let json = serde_json::to_string_pretty(&parse_output.tree()).unwrap(); | ||
|
||
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 |
---|---|---|
@@ -1,42 +1,31 @@ | ||
use std::process::ExitCode; | ||
mod commands; | ||
|
||
use clap::Subcommand; | ||
use semver::Version; | ||
use clap::{Parser, Subcommand}; | ||
|
||
pub mod commands; | ||
use crate::cli::commands::parse::ParseCommand; | ||
|
||
#[derive(Parser, Debug)] | ||
#[command(next_line_help = true)] | ||
#[command(author, about)] | ||
struct Cli { | ||
#[command(subcommand)] | ||
command: Commands, | ||
} | ||
|
||
#[derive(Subcommand, Debug)] | ||
pub enum Commands { | ||
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, | ||
Parse(ParseCommand), | ||
} | ||
|
||
/// Print the concrete syntax tree as JSON | ||
#[clap(long)] | ||
json: bool, | ||
}, | ||
pub fn execute() { | ||
match Cli::parse().command { | ||
Commands::Parse(command) => command.execute(), | ||
}; | ||
} | ||
|
||
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), | ||
}; | ||
match command_result { | ||
Ok(()) => ExitCode::SUCCESS, | ||
Err(error) => { | ||
eprintln!("{error}"); | ||
ExitCode::FAILURE | ||
} | ||
} | ||
} | ||
#[test] | ||
fn verify_clap_cli() { | ||
// Catches problems earlier in the development cycle: | ||
<Cli as clap::CommandFactory>::command().debug_assert(); | ||
} |
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
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
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
Oops, something went wrong.