-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathparser.rs
More file actions
213 lines (178 loc) · 5.96 KB
/
parser.rs
File metadata and controls
213 lines (178 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//SPDX-FileCopyrightText: 2024 Ryuichi Ueda ryuichiueda@gmail.com
//SPDX-License-Identifier: BSD-3-Clause
use crate::{ShellCore, Feeder};
use crate::error::parse::ParseError;
use crate::elements::subword;
use crate::elements::word::Word;
use crate::elements::command;
use super::{CondElem, ConditionalExpr};
impl ConditionalExpr {
fn eat_word(feeder: &mut Feeder, ans: &mut Self, core: &mut ShellCore) -> bool {
if feeder.starts_with("]]")
|| feeder.starts_with(")")
|| feeder.starts_with("(") {
return false;
}
match Word::parse(feeder, core, None) {
Ok(Some(w)) => {
ans.text += &w.text.clone();
ans.elements.push(CondElem::Word(w));
true
},
_ => false
}
}
fn eat_compare_op(feeder: &mut Feeder, ans: &mut Self, core: &mut ShellCore) -> String {
let len = feeder.scanner_test_compare_op(core);
if len == 0 {
return "".to_string();
}
let opt = feeder.consume(len);
ans.text += &opt.clone();
ans.elements.push(CondElem::BinaryOp(opt.clone()));
opt
}
fn eat_subwords(feeder: &mut Feeder, ans: &mut Self, core: &mut ShellCore) -> Word {
let mut word = Word::default();
while ! feeder.starts_with(" ") {
if let Ok(Some(sw)) = subword::parse(feeder, core, &None) {
ans.text += sw.get_text();
word.text += sw.get_text();
word.subwords.push(sw);
continue;
}
let len = feeder.scanner_regex_symbol();
if len == 0 {
break;
}
let symbol = feeder.consume(len);
ans.text += &symbol.clone();
word.subwords.push( From::from(&symbol) );
}
word
}
fn eat_regex(feeder: &mut Feeder, ans: &mut Self, core: &mut ShellCore) -> bool {
if ! Self::eat_blank(feeder, ans, core) {
return false;
}
let w = Self::eat_subwords(feeder, ans, core);
ans.elements.push( CondElem::Regex(w) );
true
}
fn eat_file_check_option(feeder: &mut Feeder, ans: &mut Self, core: &mut ShellCore) -> bool {
let len = feeder.scanner_test_check_option(core);
if len == 0 {
return false;
}
let opt = feeder.consume(len);
ans.text += &opt.clone();
ans.elements.push(CondElem::UnaryOp(opt));
true
}
fn eat_not_and_or(feeder: &mut Feeder, ans: &mut Self) -> bool {
if feeder.starts_with("!") {
ans.text += &feeder.consume(1);
ans.elements.push( CondElem::Not );
return true;
}
if feeder.starts_with("&&") {
ans.text += &feeder.consume(2);
ans.elements.push( CondElem::And );
return true;
}
if feeder.starts_with("||") {
ans.text += &feeder.consume(2);
ans.elements.push( CondElem::Or );
return true;
}
false
}
fn eat_paren(feeder: &mut Feeder, ans: &mut Self, core: &mut ShellCore) -> Result<bool, ParseError> {
if let Some(e) = ans.elements.last() {
match e {
CondElem::UnaryOp(_) => {
return Ok(false)
},
_ => {},
}
}
if ! feeder.starts_with("(") {
return Ok(false);
}
ans.text += &feeder.consume(1);
let expr = match Self::parse(feeder, core)? {
Some(e) => e,
None => return Ok(false),
};
if ! feeder.starts_with(")") {
return Ok(false);
}
ans.text += &expr.text.clone();
ans.elements.push( CondElem::InParen(expr) );
ans.text += &feeder.consume(1);
Ok(true)
}
fn eat_blank(feeder: &mut Feeder, ans: &mut Self, core: &mut ShellCore) -> bool {
match feeder.scanner_blank(core) {
0 => false,
n => {
ans.text += &feeder.consume(n);
true
},
}
}
pub fn parse(feeder: &mut Feeder, core: &mut ShellCore) -> Result<Option<Self>, ParseError> {
let mut ans = Self::default();
let mut read_option = true;
loop {
//Self::eat_blank(feeder, &mut ans, core)?;
command::eat_blank_lines(feeder, core, &mut ans.text)?;
if feeder.starts_with("\n"){
ans.text += &feeder.consume(1);
continue;
}
if feeder.len() == 0 {
if ! feeder.feed_additional_line(core).is_ok() {
return Ok(None);
}
continue;
}
if feeder.starts_with("]]")
|| feeder.starts_with(")") {
if ans.elements.is_empty() {
return Ok(None);
}
ans.elements.push(CondElem::And);
return Ok(Some(ans));
}
if Self::eat_paren(feeder, &mut ans, core)? {
continue;
}
match Self::eat_compare_op(feeder, &mut ans, core).as_ref() {
"" => {},
"=~" => {
match Self::eat_regex(feeder, &mut ans, core) {
false => return Ok(None),
true => continue,
}
},
_ => continue,
}
if read_option {
if Self::eat_file_check_option(feeder, &mut ans, core) {
continue;
}
}
if Self::eat_not_and_or(feeder, &mut ans) {
read_option = true;
continue;
}
if Self::eat_word(feeder, &mut ans, core) {
read_option = false;
continue;
}
break;
}
Ok(Some(ans))
}
}