r4d 1.2.0

Text oriented macro processor
Documentation
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use std::array::IntoIter;
use std::iter::FromIterator;
use std::collections::HashMap;
use crate::{AuthType, RadError};
use crate::utils::Utils;
use crate::arg_parser::ArgParser;
use crate::Processor;

type KeywordMacType = fn(&str, usize,&mut Processor) -> Result<Option<String>, RadError>;

#[derive(Clone)]
pub struct KeywordMacro {
    macros : HashMap<String, KeywordMacType>,
}

impl KeywordMacro {
    pub fn new() -> Self {
        let map = HashMap::from_iter(IntoIter::new([
            ("bind".to_owned(),    KeywordMacro::bind_depre       as KeywordMacType),
            ("declare".to_owned(), KeywordMacro::declare          as KeywordMacType),
            ("fassert".to_owned(), KeywordMacro::assert_fail      as KeywordMacType),
            ("foreach".to_owned(), KeywordMacro::foreach          as KeywordMacType),
            ("forloop".to_owned(), KeywordMacro::forloop          as KeywordMacType),
            ("global".to_owned(),  KeywordMacro::global_depre     as KeywordMacType),
            ("if".to_owned(),      KeywordMacro::if_cond          as KeywordMacType),
            ("ifdef".to_owned(),   KeywordMacro::ifdef            as KeywordMacType),
            ("ifdefel".to_owned(), KeywordMacro::ifdefel          as KeywordMacType),
            ("ifelse".to_owned(),  KeywordMacro::ifelse           as KeywordMacType),
            ("ifenv".to_owned(),   KeywordMacro::ifenv            as KeywordMacType),
            ("ifenvel".to_owned(), KeywordMacro::ifenvel          as KeywordMacType),
            ("let".to_owned(),     KeywordMacro::bind_to_local    as KeywordMacType),
            ("pause".to_owned(),   KeywordMacro::pause            as KeywordMacType),
            ("repl".to_owned(),    KeywordMacro::replace          as KeywordMacType),
            ("static".to_owned(),  KeywordMacro::define_static    as KeywordMacType),
        ]));
        Self {
            macros: map,
        }
    }

    /// Get Function pointer from map
    pub fn get(&self, name: &str) -> Option<&KeywordMacType> {
        if let Some(mac) = self.macros.get(name) {
            Some(mac)
        } else {
            None
        }
    }

    /// Check if map contains the name
    pub fn contains(&self, name: &str) -> bool {
        self.macros.contains_key(name)
    }

    // ----------
    // Keyword Macros start

    /// Pause every macro expansion
    ///
    /// Only other pause call is evaluated
    ///
    /// # Usage
    /// 
    /// $pause(true)
    /// $pause(false)
    fn pause(args: &str, level: usize,processor : &mut Processor) -> Result<Option<String>, RadError> {
        if let Some(args) = ArgParser::new().args_with_len(args, 1, true) {
            let arg = &processor.parse_chunk_args(level, "", &args[0])?;

            if let Ok(value) =Utils::is_arg_true(arg) {
                if value {
                    processor.state.paused = true;
                } else {
                    processor.state.paused = false;
                }
                Ok(None)
            } 
            // Failed to evaluate
            else {
                Err(RadError::InvalidArgument(format!("Pause requires either true/false or zero/nonzero integer, but given \"{}\"", arg)))
            }
        } else {
            Err(RadError::InvalidArgument("Pause requires an argument".to_owned()))
        }
    }

    /// Loop around given values and substitute iterators  with the value
    ///
    /// # Usage 
    ///
    /// $foreach(\*a,b,c*\,$:)
    fn foreach(args: &str, level: usize,processor: &mut Processor) -> Result<Option<String>, RadError> {
        if let Some(args) = ArgParser::new().args_with_len(args, 2, true) {
            let mut sums = String::new();
            let loopable = &processor.parse_chunk_args(level, "", &args[0])?;

            for value in loopable.split(',') {
                let result = processor.parse_chunk_args(level, "", &args[1].replace("$:", value))?;
                sums.push_str(&result);
            }
            Ok(Some(sums))
        } else {
            Err(RadError::InvalidArgument("Foreach requires two argument".to_owned()))
        }
    }

    /// For loop around given min, max value and finally substitue iterators with value
    ///
    /// # Usage
    ///
    /// $forloop(1,5,$:)
    fn forloop(args: &str, level: usize, processor: &mut Processor) -> Result<Option<String>, RadError> {
        if let Some(args) = ArgParser::new().args_with_len(args, 3, true) {
            let mut sums = String::new();

            let min_src = processor.parse_chunk_args(level, "",&Utils::trim(&args[0]))?;
            let max_src = processor.parse_chunk_args(level, "",&Utils::trim(&args[1]))?;

            let min: usize; 
            let max: usize; 
            if let Ok(num) = min_src.parse::<usize>() {
                min = num;
            } else { 
                return Err(RadError::InvalidArgument(format!("Forloop's min value should be non zero positive integer but given {}", &args[0]))); 
            }
            if let Ok(num) = max_src.parse::<usize>() {
                max = num
            } else { 
                return Err(RadError::InvalidArgument(format!("Forloop's min value should be non zero positive integer gut given \"{}\"", &args[1]))); 
            }
            
            for value in min..=max {
                let result = processor.parse_chunk_args(level, "", &args[2].replace("$:", &value.to_string()))?;
                sums.push_str(&result);
            }

            Ok(Some(sums))
        } else {
            Err(RadError::InvalidArgument("Forloop requires two argument".to_owned()))
        }
    }

    /// Print content according to given condition
    ///
    /// # Usage 
    ///
    /// $if(evaluation, ifstate)
    fn if_cond(args: &str,level:usize,processor: &mut Processor) -> Result<Option<String>, RadError> {
        if let Some(args) = ArgParser::new().args_with_len(args, 2, true) {
            let boolean = &processor.parse_chunk_args(level,"",&args[0])?;

            // Given condition is true
            let cond = Utils::is_arg_true(&Utils::trim(boolean));
            if let Ok(cond) = cond {
                if cond { 
                    let if_expr = processor.parse_chunk_args(level,"",&args[1])?;
                    return Ok(Some(if_expr)); 
                }
            } else {
                return Err(RadError::InvalidArgument(format!("If requires either true/false or zero/nonzero integer but given \"{}\"", boolean)))
            }

            Ok(None)
        } else {
            Err(RadError::InvalidArgument("if requires two arguments".to_owned()))
        }
    }

    /// Print content according to given condition
    ///
    /// # Usage 
    ///
    /// $ifelse(evaluation, \*ifstate*\, \*elsestate*\)
    fn ifelse(args: &str, level: usize,processor: &mut Processor) -> Result<Option<String>, RadError> {
        if let Some(args) = ArgParser::new().args_with_len(args, 3, true) {
            let boolean = &processor.parse_chunk_args(level,"",&args[0])?;

            // Given condition is true
            let cond = Utils::is_arg_true(&Utils::trim(boolean));
            if let Ok(cond) = cond {
                if cond { 
                    let if_expr = processor.parse_chunk_args(level,"",&args[1])?;
                    return Ok(Some(if_expr)); 
                }
            } else {
                return Err(RadError::InvalidArgument(format!("Ifelse requires either true/false or zero/nonzero integer but given \"{}\"", boolean)))
            }

            // Else state
            let else_expr = processor.parse_chunk_args(level, "", &args[2])?;
            return Ok(Some(else_expr));
        } else {
            Err(RadError::InvalidArgument("ifelse requires three argument".to_owned()))
        }
    }

    /// If macro exists, then execute expresion
    ///
    /// # Usage
    ///
    /// $ifdef(macro_name, expr)
    fn ifdef(args: &str, level: usize,processor: &mut Processor) -> Result<Option<String>, RadError> {
        if let Some(args) = ArgParser::new().args_with_len(args, 2, true) {
            let name = processor.parse_chunk_args(level, "",&Utils::trim(&args[0]))?;
            let map = processor.get_map();

            let boolean = map.contains(&name);
            // Return true or false by the definition
            if boolean { 
                let if_expr = processor.parse_chunk_args(level,"",&args[1])?;
                return Ok(Some(if_expr)); 
            }
            Ok(None)
        } else {
            Err(RadError::InvalidArgument("ifdef requires two arguments".to_owned()))
        }
    }

    /// If macro exists, then execute expresion else exectue another
    ///
    /// # Usage
    ///
    /// $ifdefelse(macro_name,expr,expr2)
    fn ifdefel(args: &str, level: usize,processor: &mut Processor) -> Result<Option<String>, RadError> {
        if let Some(args) = ArgParser::new().args_with_len(args, 3, true) {
            let name = processor.parse_chunk_args(level, "",&Utils::trim(&args[0]))?;
            let map = processor.get_map();

            let boolean = map.contains(&name);
            // Return true or false by the definition
            if boolean { 
                let if_expr = processor.parse_chunk_args(level,"",&args[1])?;
                return Ok(Some(if_expr)); 
            } else {
                let else_expr = processor.parse_chunk_args(level,"",&args[2])?;
                return Ok(Some(else_expr)); 
            }
        } else {
            Err(RadError::InvalidArgument("ifdefel requires three arguments".to_owned()))
        }
    }

    /// If env exists, then execute expresion
    ///
    /// # Usage
    ///
    /// $ifenv(env_name, expr)
    fn ifenv(args: &str, level: usize,processor: &mut Processor) -> Result<Option<String>, RadError> {
        if !Utils::is_granted("ifenv", AuthType::ENV,processor)? {
            return Ok(None);
        }
        if let Some(args) = ArgParser::new().args_with_len(args, 2, true) {
            let name = processor.parse_chunk_args(level, "",&Utils::trim(&args[0]))?;

            let boolean = if let Ok(_) = std::env::var(name) {
                true
            } else {
                false
            };

            // Return true or false by the definition
            if boolean { 
                let if_expr = processor.parse_chunk_args(level,"",&args[1])?;
                return Ok(Some(if_expr)); 
            }
            Ok(None)
        } else {
            Err(RadError::InvalidArgument("ifenv requires two arguments".to_owned()))
        }
    }

    /// If env exists, then execute expresion else execute another
    ///
    /// # Usage
    ///
    /// $ifenvel(env_name,expr,expr2)
    fn ifenvel(args: &str, level: usize,processor: &mut Processor) -> Result<Option<String>, RadError> {
        if !Utils::is_granted("ifenvel", AuthType::ENV,processor)? {
            return Ok(None);
        }
        if let Some(args) = ArgParser::new().args_with_len(args, 3, true) {
            let name = processor.parse_chunk_args(level, "",&Utils::trim(&args[0]))?;

            let boolean = if let Ok(_) = std::env::var(name) {
                true
            } else {
                false
            };

            // Return true or false by the definition
            if boolean { 
                let if_expr = processor.parse_chunk_args(level,"",&args[1])?;
                return Ok(Some(if_expr)); 
            } else {
                let else_expr = processor.parse_chunk_args(level,"",&args[2])?;
                return Ok(Some(else_expr)); 
            }
        } else {
            Err(RadError::InvalidArgument("ifenvel requires three arguments".to_owned()))
        }
    }

    /// Replace value
    ///
    /// # Usage
    ///
    /// $repl(macro,value)
    fn replace(args: &str, level: usize,processor: &mut Processor) -> Result<Option<String>, RadError> {
        if let Some(args) = ArgParser::new().args_with_len(args, 2, true) {
            let name = processor.parse_chunk_args(level, "",&Utils::trim(&args[0]))?;
            let target = args[1].as_str();
            if !processor.get_map().replace(&name, target) {
                return Err(RadError::InvalidArgument(format!("{} doesn't exist, thus cannot replace it's content", name)))
            }
            Ok(None)
        } else {
            Err(RadError::InvalidArgument("Replace requires two arguments".to_owned()))
        }
    }

    /// Assert fail
    ///
    /// # Usage
    ///
    /// $fassert(abc,abc)
    fn assert_fail(args: &str, level: usize,processor: &mut Processor) -> Result<Option<String>, RadError> {
        let result = processor.parse_chunk_args(level, "", args);
        if let Err(_) = result {
            processor.track_assertion(true)?;
            Ok(None)
        } else {
            processor.track_assertion(false)?;
            Err(RadError::AssertFail)
        }
    }

    #[deprecated(since = "1.2", note = "Bind is deprecated and will be removed in 2.0")]
    fn bind_depre(args: &str, level:usize, processor: &mut Processor) -> Result<Option<String>, RadError> {
        processor.log_warning("Bind is deprecated and will be removed in 2.0 version. Use let instead.")?;
        Self::bind_to_local(args, level, processor)
    }

    /// Declare an empty macros
    ///
    /// # Usage
    ///
    /// $declare(n1,n2,n3)
    fn declare(args: &str, level:usize, processor: &mut Processor) -> Result<Option<String>, RadError> {
        let names = processor.parse_chunk_args(level, "",&Utils::trim(args))?;
        // TODO Create empty macro rules
        let custom_rules = names
            .split(',')
            .map(|name| { (Utils::trim(name),"","") } )
            .collect::<Vec<(String,&str,&str)>>();

        // Check overriding. Wanr or yield error
        for (name,_,_) in custom_rules.iter() {
            if processor.get_map().contains(&name) {
                if processor.state.strict {
                    return Err(RadError::InvalidMacroName(format!("Declaring a macro with a name already existing : \"{}\"", name)))
                } else {
                    processor.log_warning(&format!("Declaring a macro with a name already existing : \"{}\"", name))?;
                }
            }
        }

        // Add custom rules
        processor.add_custom_rules(custom_rules)?;
        Ok(None)
    }

    /// Declare a local macro
    ///
    /// Local macro gets deleted after macro execution
    ///
    /// # Usage
    ///
    /// $let(name,value)
    fn bind_to_local(args: &str, level:usize, processor: &mut Processor) -> Result<Option<String>, RadError> {
        if let Some(args) = ArgParser::new().args_with_len(args, 2, true) {
            let name = processor.parse_chunk_args(level, "",&Utils::trim(&args[0]))?;
            let value = processor.parse_chunk_args(level, "",&Utils::trim(&args[1]))?;
            // Let shadows varaible so it is ok to have existing name
            // TODO
            // I'm not so sure if Level 1 is fine for all cases?
            processor.get_map().new_local(1, &name, &value);
            Ok(None)
        } else {
            Err(RadError::InvalidArgument("Let requires two argument".to_owned()))
        }
    }

    /// Global macro (Deprecated)
    ///
    /// This is technically same with static
    /// This macro will be completely removed in 2.0
    #[deprecated(since = "1.2", note = "Global is deprecated and will be removed in 2.0")]
    fn global_depre(args: &str, level: usize, processor: &mut Processor) -> Result<Option<String>, RadError> {
        processor.log_warning("Global is deprecated and will be removed in 2.0 version. Use static instead.")?;
        Self::define_static(args,level,processor)
    }

    /// Define a static macro
    ///
    /// # Usage
    ///
    /// $static(name,value)
    fn define_static(args: &str, level : usize, processor: &mut Processor) -> Result<Option<String>, RadError> {
        if let Some(args) = ArgParser::new().args_with_len(args, 2, true) {
            let name = processor.parse_chunk_args(level, "",&Utils::trim(&args[0]))?;
            let value = processor.parse_chunk_args(level, "",&Utils::trim(&args[1]))?;
            // Macro name already exists
            if processor.get_map().contains(&name) {
                // Strict mode prevents overriding
                // Return error
                if processor.state.strict {
                    return Err(RadError::InvalidMacroName(format!("Creating a static macro with a name already existing : \"{}\"", name)));
                } else {
                    // Its warn-able anyway
                    processor.log_warning(&format!("Creating a static macro with a name already existing : \"{}\"", name))?;
                }
            }
            processor.add_custom_rules(vec![(&name,"",&value)])?;
            Ok(None)
        } else {
            Err(RadError::InvalidArgument("Static requires two argument".to_owned()))
        }
    }

    // Keyword macros end
    // ----------
}