Skip to content

Commit

Permalink
Main function of type built-in
Browse files Browse the repository at this point in the history
  • Loading branch information
magicant committed Mar 7, 2024
1 parent 6a106a2 commit df592e6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
17 changes: 14 additions & 3 deletions yash-builtin/src/command/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use super::Invoke;
use super::Search;
use crate::common::syntax::parse_arguments;
use crate::common::syntax::Mode;
use crate::common::syntax::OptionOccurrence;
use crate::common::syntax::OptionSpec;
use crate::common::syntax::ParseError;
use thiserror::Error;
Expand Down Expand Up @@ -55,9 +56,13 @@ const OPTION_SPECS: &[OptionSpec] = &[
OptionSpec::new().short('V').long("verbose-identify"),
];

pub fn parse(env: &Env, args: Vec<Field>) -> Result<Command, Error> {
let (options, operands) = parse_arguments(OPTION_SPECS, Mode::with_env(env), args)?;

/// Interprets the parsed command line arguments
///
/// This function converts the result of [`parse_arguments`] into a `Command`.
pub fn interpret(
options: Vec<OptionOccurrence<'_>>,
operands: Vec<Field>,
) -> Result<Command, Error> {
// Interpret options
let mut standard_path = false;
let mut verbose_identify = None;
Expand Down Expand Up @@ -89,6 +94,12 @@ pub fn parse(env: &Env, args: Vec<Field>) -> Result<Command, Error> {
}
}

/// Parses command line arguments of the `command` built-in
pub fn parse(env: &Env, args: Vec<Field>) -> Result<Command, Error> {
let (options, operands) = parse_arguments(OPTION_SPECS, Mode::with_env(env), args)?;
interpret(options, operands)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
34 changes: 31 additions & 3 deletions yash-builtin/src/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,40 @@
//!
//! [`command`]: crate::command
use crate::command::syntax::interpret;
use crate::command::Command;
use crate::common::report_error;
use crate::common::syntax::parse_arguments;
use crate::common::syntax::Mode;
use crate::common::syntax::OptionOccurrence;
use crate::common::syntax::OptionSpec;
use yash_env::semantics::Field;
use yash_env::Env;
use yash_syntax::source::Location;

const OPTION_SPECS: &[OptionSpec] = &[
// TODO: Non-standard options
];

fn parse(env: &mut Env, args: Vec<Field>) -> Result<Command, crate::command::syntax::Error> {
let (mut options, operands) = parse_arguments(OPTION_SPECS, Mode::with_env(env), args)?;
let spec = OptionSpec::new().short('V').long("verbose-identify");
let location = env.stack.current_builtin().map_or_else(
|| Location::dummy(""),
|builtin| builtin.name.origin.clone(),
);
options.push(OptionOccurrence {
spec: &spec,
location,
argument: None,
});
interpret(options, operands)
}

/// Entry point of the `type` built-in
pub async fn main(env: &mut Env, args: Vec<Field>) -> crate::Result {
_ = env;
_ = args;
todo!()
match parse(env, args) {
Ok(command) => command.execute(env).await,
Err(error) => report_error(env, &error).await,
}
}

0 comments on commit df592e6

Please sign in to comment.