-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.rs
153 lines (126 loc) · 3.88 KB
/
random.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
use crate::preproc::{Preproc, PreprocVerdict};
use std::fmt::Write;
use rand::Rng;
#[derive(Default)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// match `random(from, to)`
enum State {
#[default]
NotStarted,
/// `random`
WordRandom,
/// `(`
Open,
/// `-`
FromMinus,
FromNumber,
/// `,` or `:`
Comma,
/// `-`
ToMinus,
ToNumber,
/// `)`
Close,
Err,
}
impl State {
fn state_upd(self, token: &str) -> Self {
if token.chars().all(|c| c.is_whitespace()) {
return match self {
Self::FromMinus | Self::ToMinus => Self::Err,
_ => self,
}
}
let is_num = token.chars().all(|c| c.is_ascii_digit());
match self {
Self::NotStarted if token == "random" => Self::WordRandom,
Self::WordRandom if token == "(" => Self::Open,
Self::Open if token == "-" => Self::FromMinus,
Self::Open | Self::FromMinus if is_num => Self::FromNumber,
Self::FromNumber if token == "," || token == ":" || token == ";" => Self::Comma,
Self::Comma if token == "-" => Self::ToMinus,
Self::Comma | Self::ToMinus if is_num => Self::ToNumber,
Self::ToNumber if token == ")" => Self::Close,
_ => Self::Err,
}
}
#[inline]
fn is_err(self) -> bool {
matches!(self, Self::Err)
}
#[inline]
fn is_ended(self) -> bool {
matches!(self, Self::Close)
}
}
#[derive(Default, Debug)]
pub enum Mode {
#[default]
Std,
HeaderClass,
Simple,
}
#[derive(Default)]
pub struct __InnerPreproc {
from: i32,
to: i32,
state: State,
mode: Mode, // if we need trait then just rename into `ignore_mdoe`
}
impl __InnerPreproc {
pub fn simple_mode_on(&mut self) {
self.mode = Mode::Simple
}
pub fn header_mode_on(&mut self) {
self.mode = Mode::HeaderClass;
}
}
impl Preproc for __InnerPreproc {
fn close(&mut self, _: &mut String, _: ()) {
self.reset()
}
fn reset(&mut self) {
self.state = State::NotStarted
}
fn action(&mut self, output: &mut String, _: &str, _: ()) {
let mut rng = rand::thread_rng();
let from = self.from.min(self.to);
let to = self.from.max(self.to);
let rand_num = rng.gen_range(from..=to);
let _ = match self.mode {
Mode::Std => write!(output, "<span class=\"P-rand\" title=\"{from} ≤ x ≤ {to}\">{rand_num}</span>"),
Mode::HeaderClass => write!(output, "<span class=\"H-rand\" title=\"{from} ≤ x ≤ {to}\">{rand_num}</span>"),
Mode::Simple => write!(output, "{rand_num}"),
};
}
fn state_upd_str(&mut self, token: &str) -> PreprocVerdict {
let state = self.state.state_upd(token);
if state.is_err() {
return PreprocVerdict::No
}
if self.state != state {
if state == State::FromNumber {
let minus = self.state == State::FromMinus;
let Ok(x) = token.parse::<i32>() else { return PreprocVerdict::No };
self.from = if minus { -x } else { x };
}
if state == State::ToNumber {
let minus = self.state == State::ToMinus;
let Ok(x) = token.parse::<i32>() else { return PreprocVerdict::No };
self.to = if minus { -x } else { x };
}
self.state = state;
}
PreprocVerdict::new(self.state.is_ended())
}
}
pub type RandomPreproc = crate::preproc::generic::SingleCmdPreproc<__InnerPreproc>;
impl RandomPreproc {
#[allow(unused)]
pub fn simple_mode_on(&mut self) {
self.inner_mut().simple_mode_on()
}
pub fn header_mode_on(&mut self) {
self.inner_mut().header_mode_on()
}
}