Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Jan 20, 2024
1 parent b676f93 commit 2a2d2c4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/elements/subword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
35 changes: 35 additions & 0 deletions src/elements/subword/unquoted.rs
Original file line number Diff line number Diff line change
@@ -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<String>> { vec![vec![self.text.clone()]] }
}

impl UnquotedSubword {
fn new() -> UnquotedSubword {
UnquotedSubword {
text: String::new(),
}
}

pub fn parse(feeder: &mut Feeder, core: &mut ShellCore) -> Option<UnquotedSubword> {
let mut ans = Self::new();

let len = feeder.scanner_word(core);
if len == 0 {
return None;
}

ans.text = feeder.consume(len);
Some(ans)
}
}
18 changes: 11 additions & 7 deletions src/elements/word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ use crate::elements::subword::Subword;
#[derive(Debug)]
pub struct Word {
pub text: String,
pub subwords: Vec<Box<dyn Subword>>,
}

impl Word {
fn new() -> Word {
Word {
text: String::new(),
subwords: vec![],
}
}

Expand All @@ -23,13 +25,15 @@ impl Word {

pub fn parse(feeder: &mut Feeder, core: &mut ShellCore) -> Option<Word> {
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)
}
}
}
}

0 comments on commit 2a2d2c4

Please sign in to comment.