From 2a2d2c464a192c1871ca30f8822d72d51f24f435 Mon Sep 17 00:00:00 2001 From: Ryuichi Ueda Date: Sat, 20 Jan 2024 14:24:02 +0900 Subject: [PATCH] Refactor --- src/elements/subword.rs | 1 - src/elements/subword/unquoted.rs | 35 ++++++++++++++++++++++++++++++++ src/elements/word.rs | 18 +++++++++------- 3 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 src/elements/subword/unquoted.rs diff --git a/src/elements/subword.rs b/src/elements/subword.rs index 6aca362e..89e3d75e 100644 --- a/src/elements/subword.rs +++ b/src/elements/subword.rs @@ -2,7 +2,6 @@ //SPDX-License-Identifier: BSD-3-Clause pub mod unquoted; -pub mod brace; use crate::{Feeder, ShellCore}; use super::subword::unquoted::UnquotedSubword; diff --git a/src/elements/subword/unquoted.rs b/src/elements/subword/unquoted.rs new file mode 100644 index 00000000..feee662e --- /dev/null +++ b/src/elements/subword/unquoted.rs @@ -0,0 +1,35 @@ +//SPDX-FileCopyrightText: 2022 Ryuichi Ueda ryuichiueda@gmail.com +//SPDX-License-Identifier: BSD-3-Clause + +use crate::{ShellCore, Feeder}; +use super::Subword; + +#[derive(Debug)] +pub struct UnquotedSubword { + pub text: String, +} + +impl Subword for UnquotedSubword { + fn get_text(&self) -> String { self.text.clone() } + fn eval(&mut self) -> Vec> { vec![vec![self.text.clone()]] } +} + +impl UnquotedSubword { + fn new() -> UnquotedSubword { + UnquotedSubword { + text: String::new(), + } + } + + pub fn parse(feeder: &mut Feeder, core: &mut ShellCore) -> Option { + let mut ans = Self::new(); + + let len = feeder.scanner_word(core); + if len == 0 { + return None; + } + + ans.text = feeder.consume(len); + Some(ans) + } +} diff --git a/src/elements/word.rs b/src/elements/word.rs index c2d8a4e6..2cbdb7d1 100644 --- a/src/elements/word.rs +++ b/src/elements/word.rs @@ -8,12 +8,14 @@ use crate::elements::subword::Subword; #[derive(Debug)] pub struct Word { pub text: String, + pub subwords: Vec>, } impl Word { fn new() -> Word { Word { text: String::new(), + subwords: vec![], } } @@ -23,13 +25,15 @@ impl Word { pub fn parse(feeder: &mut Feeder, core: &mut ShellCore) -> Option { let mut ans = Word::new(); - let arg_len = feeder.scanner_word(core); - - if arg_len > 0 { - ans.text = feeder.consume(arg_len); - Some(ans) - }else{ + while let Some(sw) = subword::parse(feeder, core) { + ans.text += &sw.get_text(); + ans.subwords.push(sw); + } + + if ans.text.len() == 0 { None + }else{ + Some(ans) } - } + } }