Skip to content

Commit

Permalink
Stub trim implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
magicant committed Oct 11, 2022
1 parent 862141b commit 544860e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
28 changes: 18 additions & 10 deletions yash-semantics/src/expansion/initial/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl<'a> From<&'a Param> for ParamRef<'a> {
mod name;
mod resolve;
mod switch;
mod trim;

use resolve::Resolve;
pub use switch::EmptyError;
Expand All @@ -70,24 +71,31 @@ impl ParamRef<'_> {
let mut value = resolve.into_owned();

// Switch //
if let Modifier::Switch(switch) = &self.modifier {
if let Modifier::Switch(switch) = self.modifier {
if let Some(result) = switch::apply(env, switch, name, &mut value, self.location).await
{
return result;
}
}

// TODO Check for nounset error
// TODO Trim & Subst

// Length modifier //
if *self.modifier == Modifier::Length {
// TODO Reject ${#*} and ${#@} in POSIX mode
match &mut value {
None => (),
Some(Value::Scalar(v)) => to_length(v),
Some(Value::Array(vs)) => vs.iter_mut().for_each(to_length),
// TODO Reject POSIXly unspecified combinations of name and modifier

// Other modifiers //
match self.modifier {
Modifier::None | Modifier::Switch(_) => (),

Modifier::Length => {
// TODO Reject ${#*} and ${#@} in POSIX mode
match &mut value {
None => (),
Some(Value::Scalar(v)) => to_length(v),
Some(Value::Array(vs)) => vs.iter_mut().for_each(to_length),
}
}

Modifier::Trim(trim) => trim::apply(env, trim, &mut value).await?,
// TODO Modifier::Subst
}

let mut phrase = into_phrase(value);
Expand Down
31 changes: 31 additions & 0 deletions yash-semantics/src/expansion/initial/param/trim.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// This file is part of yash, an extended POSIX shell.
// Copyright (C) 2022 WATANABE Yuki
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Parameter expansion trim semantics

use super::Env;
use super::Error;
use yash_env::variable::Value;
use yash_syntax::syntax::Trim;

/// Applies the trim modifier to the value.
pub async fn apply(
_env: &mut Env<'_>,
_trim: &Trim,
_value: &mut Option<Value>,
) -> Result<(), Error> {
Ok(()) // TODO
}

0 comments on commit 544860e

Please sign in to comment.