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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#![doc(html_root_url = "https://docs.rs/mo/0.1.0")]
//! # Usage
//!
//! mo come in two flavors: binary or crate (library)
//!
//! For a basic/medium/advanced usage of the binary/library, please see the [Readme](https://github.com/yuulive/mo).
//!
//! ## Frequently Asked Questions? (FAQ)
//!
//! See the [Readme](https://github.com/yuulive/mo).
//!
//! ## I just want to see how this code works
//!
//! Please see first the Readme. Well if you really want to see the code, go ahead
//!
use aho_corasick::AhoCorasick;
use indicatif::{ProgressBar, ProgressStyle};
use pest::error::{Error, ErrorVariant, InputLocation};
use pest_meta::ast::Rule as AstRule;
use pest_meta::parser::{self, Rule};
use pest_meta::{optimizer, validator};
use pest_vm::Vm;
use rayon::prelude::*;
use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use std::path::Path;

pub mod config;
mod generator;

// Re-exports
pub use pest;
pub use pest_meta;

use crate::config::*;
use crate::generator::*;

/// Compile a grammar string and creates a `HashMap` with rules found as keys and their components (AST) as entries
///
/// In this step, the grammar is validated with the pest reference grammar, and the built-in rules are replaced for
/// their equivalents
/// ```
/// use mo::compile_grammar;
///
/// // Grammar string
/// let mut grammar = r#"
///         language = {"Rust" | "Python" | "Go" | "Java" | "PHP" | "Haskell"}
///         one = {"1"}
///         daysNumber = {one ~ " day" | !one ~ ASCII_NONZERO_DIGIT ~ " days"}
///         sentence = {"I have been programming in " ~ language ~ " for " ~ daysNumber ~ "."}
///     "#;
///
/// // Generate the ast
/// let grammar_ast = compile_grammar(grammar.to_string());
///
/// println!("{:?}", grammar_ast);
/// ```
pub fn compile_grammar(grammar: String) -> Result<Grammar, Vec<HashMap<String, String>>> {
    // Replace builtin pest rules for their equivalents
    let grammar = replace_builtin_rules(&grammar).unwrap();

    // Print grammar after replaces builtin rules
    // println!("{}", grammar.clone());

    let result = parser::parse(Rule::grammar_rules, &grammar).map_err(|error| {
        error.renamed_rules(|rule| match *rule {
            Rule::grammar_rule => "rule".to_owned(),
            Rule::_push => "push".to_owned(),
            Rule::assignment_operator => "`=`".to_owned(),
            Rule::silent_modifier => "`_`".to_owned(),
            Rule::atomic_modifier => "`@`".to_owned(),
            Rule::compound_atomic_modifier => "`$`".to_owned(),
            Rule::non_atomic_modifier => "`!`".to_owned(),
            Rule::opening_brace => "`{`".to_owned(),
            Rule::closing_brace => "`}`".to_owned(),
            Rule::opening_paren => "`(`".to_owned(),
            Rule::positive_predicate_operator => "`&`".to_owned(),
            Rule::negative_predicate_operator => "`!`".to_owned(),
            Rule::sequence_operator => "`&`".to_owned(),
            Rule::choice_operator => "`|`".to_owned(),
            Rule::optional_operator => "`?`".to_owned(),
            Rule::repeat_operator => "`*`".to_owned(),
            Rule::repeat_once_operator => "`+`".to_owned(),
            Rule::comma => "`,`".to_owned(),
            Rule::closing_paren => "`)`".to_owned(),
            Rule::quote => "`\"`".to_owned(),
            Rule::insensitive_string => "`^`".to_owned(),
            Rule::range_operator => "`..`".to_owned(),
            Rule::single_quote => "`'`".to_owned(),
            other_rule => format!("{:?}", other_rule),
        })
    });

    let pairs = match result {
        Ok(pairs) => pairs,
        Err(error) => {
            // add_rules_to_select(vec![]);
            return Err(vec![convert_error(error, &grammar)]);
        }
    };

    if let Err(errors) = validator::validate_pairs(pairs.clone()) {
        // add_rules_to_select(vec![]);
        return Err(errors
            .into_iter()
            .map(|e| convert_error(e, &grammar))
            .collect());
    }

    let ast = match parser::consume_rules(pairs) {
        Ok(ast) => ast,
        Err(errors) => {
            // add_rules_to_select(vec![]);
            return Err(errors
                .into_iter()
                .map(|e| convert_error(e, &grammar))
                .collect());
        }
    };

    let hashmap_ast_rules: HashMap<String, AstRule> = ast
        .iter()
        .map(|rule| (rule.name.to_string(), rule.clone()))
        .collect();

    Ok(Grammar {
        rules: hashmap_ast_rules,
    })
}

/// Generate a number of examples with the grammar,start rule and config provided
///
/// ```
/// use mo::config::GeneratorConfig;
/// use mo::parallel_generate_examples;
///
/// // Default configuration for the generator
/// let mut config: GeneratorConfig = Default::default();
///
/// // Grammar string
/// let mut grammar = r#"
///         language = {"Rust" | "Python" | "Go" | "Java" | "PHP" | "Haskell"}
///         one = {"1"}
///         daysNumber = {one ~ " day" | !one ~ ASCII_NONZERO_DIGIT ~ " days"}
///         sentence = {"I have been programming in " ~ language ~ " for " ~ daysNumber ~ "."}
///     "#;
///
/// // Generate the examples
/// let results = parallel_generate_examples(
///             grammar.to_string(),        // The grammar
///             5,                          // Quantity of examples
///             "sentence".to_string(),    // Start rule
///             &config,                    // Config of the generator
///             false,                      // Print progress
///             false,                      // Print in stdout, false return a vector with the examples
///         );
///
/// println!("{:?}", results);
/// ```
///
pub fn parallel_generate_examples(
    grammar_string: String,
    quantity: u32,
    start: String,
    config: &GeneratorConfig,
    print_progress: bool,
    print_stdout: bool,
) -> Vec<Result<String, String>> {
    let input_data = InputData::new(grammar_string);
    let mut vec = vec![];

    // This mode is for avoid printing the examples, nothing special
    // Nobody wants to generate examples and then discard all of them... right?
    if config.benchmark_mode {
        // Print examples as they are generated
        (1..quantity + 1).into_par_iter().for_each(|_| {
            // This isn't optimized by the compiler... right?
            let _r = generator::generate_example(input_data.clone(), start.clone(), config);
        });
    } else if print_stdout {
        // Print examples as they are generated
        (1..quantity + 1).into_par_iter().for_each(|i| {
            let r = generator::generate_example(input_data.clone(), start.clone(), config);
            if print_progress {
                println!("Example #{} generated:\r\n{}", i, r.unwrap());
            } else {
                println!("{}", r.unwrap());
            }
        });
    } else {
        vec = Vec::with_capacity(quantity as usize);
        vec.par_extend((1..quantity + 1).into_par_iter().map(|i| {
            let r = generator::generate_example(input_data.clone(), start.clone(), config);
            if print_progress {
                println!("Example #{} generated", i);
            }
            r
        }));
    }

    vec
}

// pub fn gen_random_html_and_save(examples: u32,)
// where>
//     S: AsRef<Path>,
// Creación de ejemplos recibiendo una gramática, numero de ejemplos, regla de inicio, path de guardado, nombre de archivos en formato "example-{}.ext"
/// Generate and save a number of examples with the grammar,start rule and config provided
///
/// ```ignore
/// # // This doc_test is ignored because have side effects (the files)
/// use std::path::Path;
/// use mo::config::GeneratorConfig;
/// use mo::parallel_generate_save_examples;
///
/// // Default configuration for the generator
/// let mut config: GeneratorConfig = Default::default();
///
/// // Grammar string
/// let mut grammar = r#"
///         language = {"Rust" | "Python" | "Go" | "Java" | "PHP" | "Haskell"}
///         one = {"1"}
///         daysNumber = {one ~ " day" | !one ~ ASCII_NONZERO_DIGIT ~ " days"}
///         sentence = {"I have been programming in " ~ language ~ " for " ~ daysNumber ~ "."}
///     "#;
///
/// // Folder to save the examples
/// let path = Path::new("./my-examples/");
///
/// let template_name = "relevant-example-{}.txt".to_string();
///
/// // Generate and save the examples
/// let results = parallel_generate_save_examples(
///             grammar.to_string(),       // The grammar
///             5,                         // Quantity of examples
///             "sentence".to_string(),   // Start rule
///             path,                      // The folder to save the examples
///             template_name,             // The name of the files saved
///             &config,                   // Config of the generator
///         );
///
/// ```
pub fn parallel_generate_save_examples<S>(
    grammar_string: String,
    quantity: u32,
    start: String,
    path: S,
    name_format: String,
    config: &GeneratorConfig,
) where
    S: AsRef<Path>,
{
    // use env_logger;
    // env_logger::init();

    let path_cloned = path.as_ref();

    // Creación de la barra de progreso
    let progress_bar = ProgressBar::new(quantity.into());
    progress_bar.set_style(
        ProgressStyle::default_bar()
            .template(
                "[{elapsed_precise}] {wide_bar} {pos:>3}/{len:3} {msg} {percent}% {eta_precise}",
            )
            .progress_chars("██░"),
    );

    // Forzar el pintado inicial de la barra
    progress_bar.tick();

    let input_data = InputData::new(grammar_string);

    (0..quantity).into_par_iter().for_each(|i| {
        // Generar el ejemplo
        let r = generator::generate_example(input_data.clone(), start.clone(), config);

        match r {
            Ok(example) => {
                let new_path = path_cloned.join(name_format.replace("{}", &i.to_string()));
                // println!("for {:?}", new_path);

                // Guardar el archivo
                let mut f = File::create(new_path).expect("Unable to create file");
                f.write_all(example.as_bytes())
                    .expect("Unable to write data");

                // Modificando la barra de progreso
                progress_bar.inc(1);
                // println!(
                //     "HTML {} completado y guardado, total esperado {}",
                //     i, examples
                // );
            }
            Err(error) => {
                println!("{}", error);
            }
        }
    });

    // Terminando la barra de progreso
    progress_bar.finish();
}

// Parsea `input` usando la gramática `grammar`, iniciando el parseo desde `rule`
// retorna Ok si es exitoso el parseo, Err si no es posible parsear
// Es usado en términos generales como shorcut en los tests para validar si una cadena generada, puede ser parseada por la misma gramatica que la genero
/// Parse input with the provided grammar and start rule returns `Ok` if the parse is sucessfull, `Err` otherwise
///
/// It's used for validate the examples generated with the original grammar
///
/// ```
/// use mo::config::GeneratorConfig;
/// use mo::{compile_grammar, parse_input, parallel_generate_examples};
///
/// // Default configuration for the generator
/// let mut config: GeneratorConfig = Default::default();
///
/// // Grammar string
/// let mut grammar = r#"
///         language = {"Rust" | "Python" | "Go" | "Java" | "PHP" | "Haskell"}
///         one = {"1"}
///         daysNumber = {one ~ " day" | !one ~ ASCII_NONZERO_DIGIT ~ " days"}
///         sentence = {"I have been programming in " ~ language ~ " for " ~ daysNumber ~ "."}
///     "#;
///
/// // Generate the ast
/// let grammar_ast = compile_grammar(grammar.to_string());
///
/// // Generate the examples
/// let results = parallel_generate_examples(
///             grammar.to_string(),        // The grammar
///             1,                          // Quantity of examples
///             "sentences".to_string(),    // Start rule
///             &config,                    // Config of the generator
///             false,                      // Print progress
///             false,                      // Print in stdout, false return a vector with the examples
///         );
///
/// let one_example = results[0].as_ref().unwrap();
///
/// // Validate the generated example with the grammar
/// let validate = parse_input(grammar_ast.unwrap(), "sentence".to_string(), one_example.to_string());
///
/// println!("{:?}", validate);
/// ```
///
pub fn parse_input<'a>(grammar: Grammar, rule: String, input: String) -> Result<(), String> {
    // Es necesario entregar una copia entera de las reglas al vm
    let vm = Vm::new(optimizer::optimize(
        grammar.rules.values().map(|r| r.clone()).collect(),
    ));

    parse_input_with_vm(vm, rule, input)
}

/// Parsea `input` usando la máquina `Vm`, iniciando el parseo desde `rule`
/// retorna Ok si es exitoso el parseo, Err si no es posible parsear
fn parse_input_with_vm<'a>(vm: Vm, rule: String, input: String) -> Result<(), String> {
    match vm.parse(&rule, &input) {
        Ok(_pairs) => {
            // let lines: Vec<_> = pairs.map(|pair| format_pair(pair, 0, true)).collect();
            // let lines = lines.join("\n");

            // output.set_value(&format!("{}", lines));
            Ok(())
        }
        Err(error) => {
            // output.set_value(&format!("{}", error.renamed_rules(|r| r.to_string())))
            // FIXME: Eliminar el string para usar un tipo de error más "Rustacean"
            Err(format!("{}", error.renamed_rules(|r| r.to_string())))
        }
    }
    // }
}

fn convert_error(error: Error<Rule>, grammar: &str) -> HashMap<String, String> {
    let message = match error.variant {
        ErrorVariant::CustomError { message } => message,
        _ => unreachable!(),
    };

    match error.location {
        InputLocation::Pos(pos) => {
            let mut map = HashMap::new();

            map.insert("from".to_owned(), line_col(pos, grammar));
            map.insert("to".to_owned(), line_col(pos, grammar));
            map.insert("message".to_owned(), format!("{}", message));

            map
        }
        InputLocation::Span((start, end)) => {
            let mut map = HashMap::new();

            map.insert("from".to_owned(), line_col(start, grammar));
            map.insert("to".to_owned(), line_col(end, grammar));
            map.insert("message".to_owned(), format!("{}", message));

            map
        }
    }
}

fn line_col(pos: usize, input: &str) -> String {
    let (line, col) = {
        let mut pos = pos;
        // Position's pos is always a UTF-8 border.
        let slice = &input[..pos];
        let mut chars = slice.chars().peekable();

        let mut line_col = (1, 1);

        while pos != 0 {
            match chars.next() {
                Some('\r') => {
                    if let Some(&'\n') = chars.peek() {
                        chars.next();

                        if pos == 1 {
                            pos -= 1;
                        } else {
                            pos -= 2;
                        }

                        line_col = (line_col.0 + 1, 1);
                    } else {
                        pos -= 1;
                        line_col = (line_col.0, line_col.1 + 1);
                    }
                }
                Some('\n') => {
                    pos -= 1;
                    line_col = (line_col.0 + 1, 1);
                }
                Some(c) => {
                    pos -= c.len_utf8();
                    line_col = (line_col.0, line_col.1 + 1);
                }
                None => unreachable!(),
            }
        }

        line_col
    };

    format!("({}, {})", line - 1, col - 1)
}

/// Replace builtin pest rules for their equivalents
///
/// For example in a grammar like this:
/// ```text
/// small_number = ASCII_DIGIT{3}
/// ```
///
/// the replaced equivalent will be this:
/// ASCII_DIGIT
/// ```text
/// small_number = ('0'..'9'){3}
/// ```
///
/// **Note:** currently only the ASCII rules are replaced
///
/// For the list of equivalent rules see https://pest.rs/book/grammars/built-ins.html
fn replace_builtin_rules(grammar: &String) -> Result<String, std::io::Error> {
    //TODO: Add the Unicode rules from https://pest.rs/book/grammars/built-ins.html

    let patterns = &[
        "ANY",
        "ASCII_DIGIT",
        "ASCII_NONZERO_DIGIT",
        "ASCII_BIN_DIGIT",
        "ASCII_OCT_DIGIT",
        "ASCII_HEX_DIGIT",
        "ASCII_ALPHA_LOWER",
        "ASCII_ALPHA_UPPER",
        "ASCII_ALPHANUMERIC",
        "NEWLINE",
    ];

    // Parentheses are kept to facilitate things like ASCII_ALPHA{1,5}
    let replace_with = &[
        "('\u{00}'..'\u{10FFFF}')",
        "('0'..'9')",
        "('1'..'9')",
        "('0'..'1')",
        "('0'..'7')",
        "('0'..'9' | 'a'..'f' | 'A'..'F')",
        "('a'..'z')",
        "('A'..'Z')",
        "('0'..'9' | 'a'..'z' | 'A'..'Z')",
        r#"("\n" | "\r\n" | "\r")"#,
    ];

    // Replace all strings in a single pass
    let mut wtr = vec![];
    let ac = AhoCorasick::new(patterns);
    ac.stream_replace_all(grammar.as_bytes(), &mut wtr, replace_with)?;

    // println!("{:?}", wtr);
    let mut s = match String::from_utf8(wtr) {
        Ok(v) => v,
        Err(e) => panic!("Invalid UTF-8 sequence: {}", e),
    };

    // ASCII_ALPHA it is replaced last because it has conflict with ASCII_ALPHA_LOWER y ASCII_ALPHA_UPPER
    // because the word "ASCII_ALPHA" is shorter
    s = s.replace("ASCII_ALPHA", "('a'..'z' | 'A'..'Z')");

    // println!("result: {}", s);
    Ok(s)
}