-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathstmt.rs
182 lines (172 loc) Β· 5.14 KB
/
stmt.rs
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
use heraclitus_compiler::prelude::*;
use itertools::Itertools;
use crate::docs::module::DocumentationModule;
use crate::utils::metadata::{ParserMetadata, TranslateMetadata};
use crate::modules::expression::expr::{Expr, ExprType};
use crate::translate::module::TranslateModule;
use crate::modules::variable::{
init::VariableInit,
set::VariableSet,
};
use crate::modules::command::modifier::CommandModifier;
use crate::handle_types;
use crate::modules::condition::{
ifchain::IfChain,
ifcond::IfCondition,
};
use crate::modules::shorthand::{
add::ShorthandAdd,
sub::ShorthandSub,
mul::ShorthandMul,
div::ShorthandDiv,
modulo::ShorthandModulo,
};
use crate::modules::loops::{
infinite_loop::InfiniteLoop,
iter_loop::IterLoop,
break_stmt::Break,
continue_stmt::Continue,
};
use crate::modules::function::{
declaration::FunctionDeclaration,
ret::Return,
fail::Fail,
};
use crate::modules::imports::import::Import;
use crate::modules::main::Main;
use crate::modules::builtin::{
echo::Echo,
mv::Mv,
cd::Cd,
exit::Exit,
};
use super::comment_doc::CommentDoc;
use super::comment::Comment;
#[derive(Debug, Clone)]
pub enum StatementType {
Expr(Expr),
VariableInit(VariableInit),
VariableSet(VariableSet),
IfCondition(IfCondition),
IfChain(IfChain),
ShorthandAdd(ShorthandAdd),
ShorthandSub(ShorthandSub),
ShorthandMul(ShorthandMul),
ShorthandDiv(ShorthandDiv),
ShorthandModulo(ShorthandModulo),
InfiniteLoop(InfiniteLoop),
IterLoop(IterLoop),
Break(Break),
Continue(Continue),
FunctionDeclaration(FunctionDeclaration),
Return(Return),
Fail(Fail),
Import(Import),
Main(Main),
Cd(Cd),
Echo(Echo),
Mv(Mv),
Exit(Exit),
CommandModifier(CommandModifier),
Comment(Comment),
CommentDoc(CommentDoc),
}
#[derive(Debug, Clone)]
pub struct Statement {
pub value: Option<StatementType>
}
impl Statement {
handle_types!(StatementType, [
// Imports
Import,
// Functions
FunctionDeclaration, Main, Return, Fail,
// Loops
InfiniteLoop, IterLoop, Break, Continue,
// Conditions
IfChain, IfCondition,
// Variables
VariableInit, VariableSet,
// Short hand
ShorthandAdd, ShorthandSub,
ShorthandMul, ShorthandDiv,
ShorthandModulo,
// Command
CommandModifier, Echo, Mv, Cd, Exit,
// Comment doc
CommentDoc, Comment,
// Expression
Expr
]);
// Get result out of the provided module and save it in the internal state
fn get<M,S>(&mut self, meta: &mut M, mut module: S, cb: impl Fn(S) -> StatementType) -> SyntaxResult
where
M: Metadata,
S: SyntaxModule<M>
{
match syntax(meta, &mut module) {
Ok(()) => {
self.value = Some(cb(module));
Ok(())
}
Err(details) => Err(details)
}
}
pub fn get_docs_item_name(&self) -> Option<String> {
match &self.value {
Some(StatementType::FunctionDeclaration(inner)) => Some(inner.name.clone()),
_ => None,
}
}
}
impl SyntaxModule<ParserMetadata> for Statement {
syntax_name!("Statement");
fn new() -> Self {
Statement {
value: None
}
}
fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
let mut error = None;
let statements = self.get_modules();
for statement in statements {
// Try to parse the statement
match self.parse_match(meta, statement) {
Ok(()) => return Ok(()),
Err(failure) => {
match failure {
Failure::Loud(err) => return Err(Failure::Loud(err)),
Failure::Quiet(err) => error = Some(err)
}
}
}
}
Err(Failure::Quiet(error.unwrap()))
}
}
impl TranslateModule for Statement {
fn translate(&self, meta: &mut TranslateMetadata) -> String {
// Translate the staxtement
let statement = self.value.as_ref().unwrap();
// This is a workaround that handles $(...) which cannot be used as a statement
let translated = match statement {
StatementType::Expr(expr) => match &expr.value {
Some(ExprType::Command(cmd)) => cmd.translate_command_statement(meta),
_ => format!("echo {} > /dev/null 2>&1", self.translate_match(meta, statement))
},
_ => self.translate_match(meta, statement)
};
// Get all the required supplemental statements
let indentation = meta.gen_indent();
let statements = meta.stmt_queue.drain(..).map(|st| indentation.clone() + st.trim_end_matches(';') + ";\n").join("");
// Return all the statements
statements + &indentation + &translated
}
}
impl DocumentationModule for Statement {
fn document(&self, meta: &ParserMetadata) -> String {
// Document the statement
let documented = self.document_match(meta, self.value.as_ref().unwrap());
documented
}
}