Skip to content

Commit 661f6a3

Browse files
authored
feat: added basic support for if statements (#121)
1 parent 8b08567 commit 661f6a3

File tree

5 files changed

+506
-14
lines changed

5 files changed

+506
-14
lines changed

crates/deno_task_shell/src/grammar.pest

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ OPERATOR = _{
107107
}
108108

109109
// Reserved words
110-
If = { "if" }
111-
Then = { "then" }
110+
If = _{ "if" }
111+
Then = _{ "then" }
112112
Else = { "else" }
113113
Elif = { "elif" }
114-
Fi = { "fi" }
114+
Fi = _{ "fi" }
115115
Do = { "do" }
116116
Done = { "done" }
117117
Case = { "case" }
@@ -147,7 +147,6 @@ command = !{
147147
}
148148

149149
compound_command = {
150-
double_square_bracket |
151150
brace_group |
152151
subshell |
153152
for_clause |
@@ -167,8 +166,6 @@ for_clause = {
167166
do_group
168167
}
169168

170-
double_square_bracket = !{ "[[" ~ compound_list ~ "]]" }
171-
172169
case_clause = !{
173170
Case ~ UNQUOTED_PENDING_WORD ~ linebreak ~
174171
linebreak ~ In ~ linebreak ~
@@ -197,14 +194,55 @@ pattern = !{
197194
}
198195

199196
if_clause = !{
200-
If ~ compound_list ~
201-
linebreak ~ Then ~ linebreak ~ compound_list ~ linebreak ~
197+
If ~ conditional_expression ~
198+
linebreak ~ Then ~ linebreak ~ complete_command ~ linebreak ~
202199
else_part? ~ linebreak ~ Fi
203200
}
204201

205202
else_part = !{
206-
Elif ~ compound_list ~ Then ~ linebreak ~ else_part |
207-
Else ~ linebreak ~ compound_list
203+
Elif ~ conditional_expression ~ Then ~ complete_command ~ linebreak ~ else_part? |
204+
Else ~ linebreak ~ complete_command
205+
}
206+
207+
conditional_expression = !{
208+
("[[" ~ (unary_conditional_expression | binary_conditional_expression | UNQUOTED_PENDING_WORD) ~ "]]") |
209+
("[" ~ (unary_conditional_expression | binary_conditional_expression | UNQUOTED_PENDING_WORD) ~ "]") |
210+
("test" ~ (unary_conditional_expression | binary_conditional_expression | UNQUOTED_PENDING_WORD))
211+
}
212+
213+
unary_conditional_expression = !{
214+
file_conditional_op ~ FILE_NAME_PENDING_WORD |
215+
variable_conditional_op ~ VARIABLE |
216+
string_conditional_op ~ UNQUOTED_PENDING_WORD
217+
}
218+
219+
file_conditional_op = !{
220+
"-a" | "-b" | "-c" | "-d" | "-e" | "-f" | "-g" | "-h" | "-k" |
221+
"-p" | "-r" | "-s" | "-u" | "-w" | "-x" | "-G" | "-L" |
222+
"-N" | "-O" | "-S"
223+
}
224+
225+
variable_conditional_op = !{
226+
"-v" | "-R"
227+
}
228+
229+
string_conditional_op = !{
230+
"-n" | "-z"
231+
}
232+
233+
binary_conditional_expression = !{
234+
UNQUOTED_PENDING_WORD ~ (
235+
binary_string_conditional_op |
236+
binary_arithmetic_conditional_op
237+
) ~ UNQUOTED_PENDING_WORD
238+
}
239+
240+
binary_string_conditional_op = !{
241+
"==" | "=" | "!=" | "<" | ">"
242+
}
243+
244+
binary_arithmetic_conditional_op = !{
245+
"-eq" | "-ne" | "-lt" | "-le" | "-gt" | "-ge"
208246
}
209247

210248
while_clause = !{ While ~ compound_list ~ do_group }

0 commit comments

Comments
 (0)