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
//! Small query language for pseudorandomness
//!
//! See <https://github.com/Zheoni/rng-query> for the syntax and CLI.
//!
//! Run a whole input with [`run`] or manage line by line with [`State`] and it
//! methods.
//!
//! All [`Display`] implementations of the crate *may* output ANSI color codes.
//! Use something like [anstream](https://docs.rs/anstream/) if you dont want
//! colors.

pub mod coin;
pub mod dice;
mod entry;
pub mod interval;

use std::collections::HashMap;
use std::fmt::Display;
use std::rc::Rc;
use std::str::FromStr;

use entry::BufferedEntry;
use entry::EntryData;
use rand::seq::SliceRandom;
use rand::SeedableRng;
use rand_pcg::Pcg64 as Pcg;

pub use coin::CoinResult;
pub use dice::RollResult;
pub use entry::Entry;
pub use interval::IntervalResult;

macro_rules! regex {
    ($re:literal $(,)?) => {{
        static RE: std::sync::OnceLock<regex::Regex> = std::sync::OnceLock::new();
        RE.get_or_init(|| regex::Regex::new($re).unwrap())
    }};
}
pub(crate) use regex;

/// Run a whole "program"
///
/// More than 1 query can be executed, so the result is a vec with a [`StmtOutput`]
/// for each query.
pub fn run(input: &str) -> Result<Vec<StmtOutput>, Error> {
    let mut state = State::new();
    let mut output = Vec::new();
    for line in input.lines() {
        output.extend(state.run_line(line)?);
    }
    if let Some(o) = state.eof()? {
        output.push(o);
    }
    Ok(output)
}

/// Customize the separators used
///
/// ```
/// use rng_query::Separators;
/// let sep = Separators::default();
/// assert_eq!(sep.stmt, ';'); // And `\n` always.
/// assert_eq!(sep.entry, ',');
/// assert_eq!(sep.options, '/');
/// ```
///
/// To use this, change the [`separators`](State::separators) field in [`State`].
///
/// Be careful, it can break expression parsing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Separators {
    pub stmt: char,
    pub entry: char,
    pub options: char,
}

impl Default for Separators {
    fn default() -> Self {
        Self {
            stmt: ';',
            entry: ',',
            options: '/',
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Options {
    amount: Amount,
    repeating: bool,
    eval_expr: EvalExpr,
    keep_order: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Amount {
    All,
    N(u32),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum EvalExpr {
    Auto,
    Custom(bool),
}

impl Default for Options {
    fn default() -> Self {
        Self {
            amount: Amount::N(1),
            repeating: false,
            eval_expr: EvalExpr::Auto,
            keep_order: false,
        }
    }
}

impl FromStr for Options {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let preset = match s {
            "shuffle" => Some(Options {
                amount: Amount::All,
                repeating: false,
                eval_expr: EvalExpr::Custom(false),
                keep_order: false,
            }),
            "list" => Some(Options {
                amount: Amount::All,
                repeating: false,
                eval_expr: EvalExpr::Custom(false),
                keep_order: true,
            }),
            "eval" => Some(Options {
                amount: Amount::All,
                repeating: false,
                eval_expr: EvalExpr::Custom(true),
                keep_order: true,
            }),
            _ => None,
        };
        if let Some(preset) = preset {
            return Ok(preset);
        }

        let re = regex!(r"\A(all\s|0|(?:[1-9][0-9]*))\s*([\sreEo]*)\z");
        let cap = re
            .captures(s)
            .ok_or_else(|| Error::Options(format!("Bad options: {s}")))?;
        let amount = match cap[1].trim_end() {
            "all" => Amount::All,
            n => n
                .parse::<u32>()
                .map(Amount::N)
                .map_err(|e| Error::Options(format!("Bad amount: {e}")))?,
        };

        let mut flags = cap[2]
            .chars()
            .filter(|c| !c.is_ascii_whitespace())
            .collect::<Vec<_>>();
        flags.sort();
        let all_len = flags.len();
        flags.dedup();
        let unique_len = flags.len();
        if all_len != unique_len {
            return Err(Error::Options(format!(
                "Duplicate flags: {}",
                flags.iter().collect::<String>()
            )));
        }
        let repeating = flags.contains(&'r');
        let keep_order = flags.contains(&'o');

        let eval_expr = flags.contains(&'e');
        let not_eval_expr = flags.contains(&'E');
        if eval_expr && not_eval_expr {
            return Err(Error::Options(
                "Flags 'e' and 'E' are incompatible".to_string(),
            ));
        }
        let eval_expr = if eval_expr || not_eval_expr {
            EvalExpr::Custom(eval_expr)
        } else {
            EvalExpr::Auto
        };

        Ok(Options {
            amount,
            repeating,
            eval_expr,
            keep_order,
        })
    }
}

/// Query interpreter
#[derive(Debug, Clone, PartialEq)]
pub struct State {
    stack: Vec<BufferedEntry>,
    rng: Pcg,
    entry_counter: usize,
    /// See [`Separators`]
    pub separators: Separators,
}

impl State {
    /// Create a new state
    ///
    /// Seed is autogenerated form entropy.
    pub fn new() -> Self {
        Self::from_rng(Pcg::from_entropy())
    }
    /// Create a new state with a seed
    pub fn with_seed(seed: u64) -> Self {
        Self::from_rng(Pcg::seed_from_u64(seed))
    }
    fn from_rng(rng: Pcg) -> Self {
        Self {
            stack: Vec::new(),
            rng,
            entry_counter: 0,
            separators: Separators::default(),
        }
    }
}

impl Default for State {
    fn default() -> Self {
        Self::new()
    }
}

impl State {
    /// Run a single line
    ///
    /// The line will be parsed, to add data, use [`State::add_entry`].
    ///
    /// The input should *NOT* include `\n`.
    pub fn run_line(&mut self, line: &str) -> Result<Vec<StmtOutput>, Error> {
        let sep = self.separators.stmt;
        let mut outputs = Vec::new();
        for part in line.split_inclusive(sep) {
            let options = self.run_stmt_part(part.trim_end_matches(sep))?;
            let is_end = options.is_some() || part.ends_with(sep);
            if is_end {
                let output = self.end_stmt(options.unwrap_or_default())?;
                outputs.push(output);
            }
        }
        Ok(outputs)
    }

    fn run_stmt_part(&mut self, stmt: &str) -> Result<Option<Options>, Error> {
        let (entries, options) = match stmt.split_once(self.separators.options) {
            Some((entries, options)) => (entries, Some(options)),
            None => (stmt, None),
        };
        for entry in entries.split(self.separators.entry) {
            self.add_entry(entry);
        }
        if let Some(options) = options {
            options.trim().parse::<Options>().map(Some)
        } else {
            Ok(None)
        }
    }

    /// Add an entry, without parsing it
    pub fn add_entry(&mut self, entry: &str) {
        let entry = entry.trim();
        if entry.is_empty() {
            return;
        }
        let data = EntryData::Text(Rc::from(entry));
        let id = self.entry_counter;
        self.entry_counter = self
            .entry_counter
            .checked_add(1)
            .expect("somehow you managed to get to the maximum number of entries. congrats.");
        self.stack.push(BufferedEntry { id, data });
    }

    /// Signal the interpreter the end of the input
    pub fn eof(&mut self) -> Result<Option<StmtOutput>, Error> {
        if self.stack.is_empty() {
            Ok(None)
        } else {
            self.end_stmt(Options::default()).map(Some)
        }
    }

    fn end_stmt(&mut self, options: Options) -> Result<StmtOutput, Error> {
        let eval_expr = match options.eval_expr {
            EvalExpr::Auto => self.stack.len() == 1,
            EvalExpr::Custom(r) => r,
        };
        let selected = select(&mut self.rng, &self.stack, options, eval_expr)?;

        let output = selected
            .into_iter()
            .map(|e| e.eval(&mut self.rng))
            .collect();

        self.stack.clear();
        Ok(StmtOutput(output))
    }
}

fn select(
    rng: &mut Pcg,
    entries: &[BufferedEntry],
    options: Options,
    eval_expr: bool,
) -> Result<Vec<BufferedEntry>, Error> {
    if entries.is_empty() {
        return Ok(vec![]);
    }

    let n = match options.amount {
        Amount::All => entries.len(),
        Amount::N(n) => n as usize,
    };

    let parse = |entry: &BufferedEntry| {
        if eval_expr {
            if let EntryData::Text(t) = &entry.data {
                let data = entry::parse_expr(t)?;
                return Ok(BufferedEntry { id: entry.id, data });
            }
        }
        Ok(entry.clone())
    };

    // optimization for all
    if n == entries.len() {
        let mut entries = entries.iter().map(parse).collect::<Result<Vec<_>, _>>()?;
        if !options.keep_order {
            entries.shuffle(rng);
        }
        return Ok(entries);
    }

    // general case
    let mut selected = if options.repeating {
        let mut cache = HashMap::<usize, BufferedEntry>::new();

        let mut selected = Vec::with_capacity(n);
        for _ in 0..n {
            let entry = entries.choose(rng).unwrap();
            let entry = if let Some(cached) = cache.get(&entry.id) {
                cached.clone()
            } else {
                let parsed = parse(entry)?;
                cache.insert(parsed.id, parsed.clone());
                parsed
            };
            selected.push(entry);
        }
        selected
    } else {
        entries
            .choose_multiple(rng, n)
            .map(parse)
            .collect::<Result<Vec<_>, _>>()?
    };

    if options.keep_order {
        selected.sort_unstable_by_key(|e| e.id);
    }
    Ok(selected)
}

/// Output of a query
///
/// This is a [`Vec`] of selected entries with a custom [`Display`] implementation
/// that prints each entry as a line.
///
/// Also, the [alternate modifier](std::fmt#sign0) will only print the expression output
/// and not the expression itself.
#[derive(Debug, Clone, PartialEq)]
pub struct StmtOutput(pub Vec<Entry>);

impl Display for StmtOutput {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for entry in &self.0 {
            if f.alternate() {
                writeln!(f, "{entry:#}")?;
            } else {
                writeln!(f, "{entry}")?;
            }
        }
        Ok(())
    }
}

/// Query error
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// Parsing options
    #[error("Options error: {0}")]
    Options(String),
    /// Parsing expressions
    #[error("Expression error: {0}")]
    Expr(String),
}