whitehole 0.8.0

A simple, fast, intuitive parser combinator framework for Rust.
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
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
514
//! Manage the [`State`](Parser::state), [`Heap`](Parser::heap)
//! and the [parsing progress](Parser::instant).
//!
//! # Build the Parser
//!
//! See [`Builder`].
//!
//! # Parse and Peek
//!
//! You can use [`Parser::next`] to try to yield the next [`Output`].
//!
//! If you just want to peek the next output without updating the parser,
//! you can use [`Parser::peek`] instead.
//!
//! ```
//! use whitehole::{combinator::eat, parser::Parser};
//!
//! let mut parser = Parser::builder()
//!   .entry(eat("123"))
//!   .build("123");
//!
//! // peek will clone the state
//! let (output, state) = parser.peek();
//! ```
//!
//! # Iter
//!
//! [`Parser`] implements [`Iterator`] so you can use it in a for-loop
//! or with any iterator methods.
//!
//! ```
//! use whitehole::{combinator::eat, parser::Parser};
//!
//! let factory = || {
//!   Parser::builder()
//!     .entry(eat("123"))
//!     .build("123123123")
//! };
//!
//! // for-loop
//! let mut parser = factory();
//! for o in &mut parser {
//!   assert_eq!(o.digested, 3);
//! }
//!
//! // iterator methods
//! let mut parser = factory();
//! for (_, o) in (&mut parser).enumerate() {
//!   assert_eq!(o.digested, 3);
//! }
//! ```
//!
//! # Progress
//!
//! ## Instant
//!
//! [`Parser`] uses [`Instant`] to manage the parsing progress.
//! You can use [`Parser::instant`] to access the current progress.
//!
//! ```
//! use whitehole::{combinator::eat, parser::Parser};
//!
//! let mut parser = Parser::builder()
//!  .entry(eat("123"))
//!  .build("123123");
//!
//! assert_eq!(parser.instant.text(), "123123");
//! assert_eq!(parser.instant.rest(), "123123");
//! assert_eq!(parser.instant.digested(), 0);
//!
//! parser.next();
//! assert_eq!(parser.instant.rest(), "123");
//! assert_eq!(parser.instant.digested(), 3);
//!
//! parser.next();
//! assert_eq!(parser.instant.rest(), "");
//! assert_eq!(parser.instant.digested(), 6);
//! ```
//!
//! [`Parser::instant`] is public, you can mutate it directly if needed.
//! Be ware to keep the `State` and `Heap` in sync with the progress.
//!
//! If you want to parse an other text, use [`Parser::reload`] or [`Parser::reload_with`] instead,
//! these methods will reset the instant to default and restore `State` if needed.
//!
//! ## External Digestion
//!
//! You can update [`Parser::instant`] to digest from outside of the parser.
//! e.g. in error handling or recovery.
//!
//! Be ware to keep the `State` and `Heap` in sync with the progress.
//!
//! ```
//! use whitehole::{combinator::eat, parser::Parser};
//!
//! let mut parser = Parser::builder()
//!   .entry(eat("123"))
//!   .build("a123");
//!
//! assert!(parser.next().is_none());
//!
//! // enter "panic mode", digest the next char from outside
//! let next_len = parser.instant.rest().chars().next().unwrap().len_utf8();
//! unsafe { parser.instant.digest_unchecked(next_len) };
//! assert_eq!(parser.instant.rest(), "123");
//!
//! // now we can try to yield again
//! assert!(parser.next().is_some());
//! ```
//!
//! ## Snapshots
//!
//! [`Parser`] is clone-able when your entry action, `State` and `Heap` are all clone-able.
//!
//! However, if you do have a `Heap` with heap allocation, cloning the parser might be expensive.
//! In this case, you can use [`Parser::snapshot`] and [`Parser::restore`]
//! to save and restore [`Parser::instant`] and [`Parser::state`].
//!
//! ```
//! use whitehole::{combinator::eat, parser::{Parser, Snapshot}};
//!
//! let mut parser = Parser::builder()
//!   .entry(eat("123"))
//!   .build("123");
//!
//! let snapshot = parser.snapshot();
//! parser.next();
//! assert_eq!(parser.instant.digested(), 3);
//!
//! parser.restore(snapshot);
//! assert_eq!(parser.instant.digested(), 0);
//! ```
//!
//! It's like [`Parser::peek`], but you can save as many snapshots as you want.
//!
//! # State and Heap
//!
//! Parser will manage [`Parser::state`] which is accessible by actions
//! so that you can make the parser stateful.
//!
//! For non-state data, you can use [`Parser::heap`] which is also accessible by actions.
//! You can use the heap to pass data to actions or store data that is generated by actions.
//!
//! See [`Parser::state`] and [`Parser::heap`] for more information.

mod builder;
mod snapshot;

pub use builder::*;
pub use snapshot::*;

use crate::{
  action::{Action, Input, Output},
  combinator::Take,
  digest::Digest,
  instant::Instant,
};
use std::{ops::RangeFrom, slice::SliceIndex};

/// Manage the [`State`](Parser::state), [`Heap`](Parser::heap)
/// and the [parsing progress](Parser::instant).
///
/// See the [module-level documentation](self) for more.
#[derive(Debug)]
pub struct Parser<'text, T: Action> {
  /// The state of a stateful parser.
  ///
  /// With this, you can construct stateful parsers,
  /// while [`Action`]s remain stateless.
  ///
  /// All vars that control the flow of the parsing should be stored here.
  /// This should be small and cheap to clone (maybe just a bunch of integers or booleans).
  /// If a var only represents a resource (e.g. a chunk of memory, a channel, etc),
  /// it should be stored in [`Self::heap`].
  ///
  /// This is public. You can mutate this directly if needed.
  ///
  /// You can access this in [`Action`]s via [`Input::state`]
  /// and [`Accepted::state`](crate::combinator::Accepted::state).
  pub state: T::State,

  /// The reusable heap.
  ///
  /// With this, you can reuse allocated memory
  /// across actions and parsings.
  ///
  /// All vars that doesn't count as a part of [`Self::state`] should be stored here.
  /// If a var is used to control the flow of the parsing,
  /// it should be treated as a state and stored in [`Self::state`].
  /// If a var only represents a resource (e.g. a chunk of memory, a channel, etc),
  /// it should be stored here.
  ///
  /// This is public. You can mutate this directly if needed.
  ///
  /// You can access this in [`Action`]s via [`Input::heap`]
  /// and [`Accepted::heap`](crate::combinator::Accepted::heap).
  pub heap: T::Heap,

  /// The progress of the parser. You can mutate this directly if needed.
  ///
  /// See [`Instant`].
  pub instant: Instant<&'text T::Text>,

  /// The entry action.
  pub entry: T,
}

impl<T: Action<State: Clone, Heap: Clone> + Clone> Clone for Parser<'_, T> {
  fn clone(&self) -> Self {
    Parser {
      state: self.state.clone(),
      heap: self.heap.clone(),
      instant: self.instant.clone(),
      entry: self.entry.clone(),
    }
  }
}

impl Parser<'static, Take> {
  /// Create a parser builder with default settings.
  #[inline]
  pub const fn builder() -> Builder<()> {
    Builder::new()
  }
}

impl<'text, T: Action> Parser<'text, T> {
  /// Consume self, return a new instance with the same action and a new text.
  ///
  /// [`Self::instant`] and [`Self::state`] will be reset to default.
  /// [`Self::heap`] won't change.
  #[inline]
  pub fn reload(self, text: &T::Text) -> Parser<T>
  where
    T::State: Default,
  {
    self.reload_with(T::State::default(), text)
  }

  /// Consume self, return a new instance with the same action, a new text and an optional new state.
  ///
  /// If the state is not provided, current [`Self::state`] will be kept.
  /// [`Self::instant`] will be reset to default.
  /// [`Self::heap`] won't change.
  #[inline]
  pub fn reload_with(self, state: impl Into<Option<T::State>>, text: &T::Text) -> Parser<T> {
    Parser {
      entry: self.entry,
      heap: self.heap,
      state: state.into().unwrap_or(self.state),
      instant: Instant::new(text),
    }
  }

  /// Take a snapshot of the current [`Self::state`] and [`Self::instant`].
  #[inline]
  pub fn snapshot(&self) -> Snapshot<&'text T::Text, T::State>
  where
    T::State: Clone,
  {
    Snapshot {
      state: self.state.clone(),
      instant: self.instant.clone(),
    }
  }

  /// Restore [`Self::state`] and [`Self::instant`] from a [`Snapshot`].
  #[inline]
  pub fn restore(&mut self, snapshot: Snapshot<&'text T::Text, T::State>) {
    self.state = snapshot.state;
    self.instant = snapshot.instant;
  }

  /// Try to yield the next [`Output`] without updating [`Self::instant`] and [`Self::state`].
  /// [`Self::state`] will be cloned and returned.
  /// Return [`None`] if the action rejects.
  #[inline]
  pub fn peek(&mut self) -> (Option<Output<T::Value>>, T::State)
  where
    T::State: Clone,
  {
    let mut tmp_state = self.state.clone();
    (
      self.entry.exec(Input {
        instant: &self.instant,
        state: &mut tmp_state,
        heap: &mut self.heap,
      }),
      tmp_state,
    )
  }
}

impl<T: Action<Text: Digest>> Iterator for Parser<'_, T>
where
  RangeFrom<usize>: SliceIndex<T::Text, Output = T::Text>,
{
  type Item = Output<T::Value>;

  #[inline]
  fn next(&mut self) -> Option<Self::Item> {
    self
      .entry
      .exec(Input {
        instant: &self.instant,
        state: &mut self.state,
        heap: &mut self.heap,
      })
      .inspect(|output| unsafe { self.instant.digest_unchecked(output.digested) })
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::contextual;
  use std::rc::Rc;

  #[test]
  fn parser_builder() {
    contextual!(i32, i32);

    let parser = Parser::builder()
      .state(123)
      .heap(123)
      .entry(eat("123"))
      .build("123");
    assert_eq!(parser.state, 123);
    assert_eq!(parser.heap, 123);
    assert_eq!(parser.instant.text(), "123");
  }

  #[test]
  fn parser_clone() {
    contextual!(i32, i32);

    let parser = Parser {
      state: 123,
      heap: 123,
      instant: Instant::new("123"),
      entry: Rc::new(eat("123")),
    }
    .clone();
    assert_eq!(parser.state, 123);
    assert_eq!(parser.heap, 123);
  }

  #[test]
  fn parser_getters() {
    contextual!(i32, i32);

    let parser = Parser {
      state: 123,
      heap: 123,
      instant: Instant::new("123"),
      entry: eat("123"),
    };
    assert_eq!(
      parser
        .entry
        .exec(Input {
          instant: &Instant::new("123"),
          state: &mut 0,
          heap: &mut 0
        })
        .unwrap()
        .digested,
      3
    );
    assert_eq!(parser.instant.digested(), 0);
  }

  #[test]
  fn parser_reload() {
    contextual!(i32, i32);

    let mut parser = Parser {
      state: 123,
      heap: 123,
      instant: Instant::new("123"),
      entry: eat("123"),
    };
    parser.next();
    assert_eq!(parser.instant.digested(), 3);
    assert_eq!(parser.instant.rest(), "");
    let parser = parser.reload("456");
    assert_eq!(parser.instant.text(), "456");
    assert_eq!(parser.instant.rest(), "456");
    assert_eq!(parser.instant.digested(), 0);
    assert_eq!(parser.state, 0);
    assert_eq!(parser.heap, 123);
  }

  #[test]
  fn parser_reload_with() {
    contextual!(i32, i32);

    let mut parser = Parser {
      state: 123,
      heap: 123,
      instant: Instant::new("123"),
      entry: eat("123"),
    };
    parser.next();
    assert_eq!(parser.instant.digested(), 3);
    assert_eq!(parser.instant.rest(), "");
    let parser = parser.reload_with(None, "456");
    assert_eq!(parser.instant.text(), "456");
    assert_eq!(parser.instant.rest(), "456");
    assert_eq!(parser.instant.digested(), 0);
    assert_eq!(parser.state, 123);
    assert_eq!(parser.heap, 123);
  }

  #[test]
  fn parser_snapshot_restore() {
    contextual!(i32, i32);

    let mut parser = Parser {
      state: 123,
      heap: 123,
      instant: Instant::new("123"),
      entry: eat("123"),
    };
    parser.next();
    let snapshot = parser.snapshot();
    assert_eq!(snapshot.state, 123);
    assert_eq!(snapshot.instant.text(), "123");
    assert_eq!(snapshot.instant.digested(), 3);
    assert_eq!(snapshot.instant.rest(), "");

    let mut parser = Parser {
      state: 0,
      heap: 123,
      instant: Instant::new("123"),
      entry: eat("123"),
    };
    parser.restore(snapshot);
    assert_eq!(parser.state, 123);
    assert_eq!(parser.instant.text(), "123");
    assert_eq!(parser.instant.digested(), 3);
    assert_eq!(parser.instant.rest(), "");
  }

  #[test]
  fn parser_parse() {
    contextual!(i32, i32);

    let mut parser = Parser {
      state: 123,
      heap: 123,
      instant: Instant::new("123"),
      entry: eat("123"),
    };
    let output = parser.next().unwrap();
    assert_eq!(output.digested, 3);
    assert_eq!(output.value, ());
    assert_eq!(parser.instant.digested(), 3);
    assert_eq!(parser.instant.rest(), "");
    assert!(parser.next().is_none());
  }

  #[test]
  fn parser_peek() {
    contextual!(i32, i32);

    let mut parser = Parser {
      state: 123,
      heap: 123,
      instant: Instant::new("123"),
      entry: eat("123"),
    };
    let (output, state) = parser.peek();
    let output = output.unwrap();
    assert_eq!(state, 123);
    assert_eq!(output.digested, 3);
    assert_eq!(output.value, ());
    assert_eq!(parser.instant.digested(), 0);
    assert_eq!(parser.instant.rest(), "123");
    assert!(parser.next().is_some());
  }

  #[test]
  fn parser_iterator_in_for_loop() {
    contextual!(i32, i32);

    let mut parser = Parser {
      state: 123,
      heap: 123,
      instant: Instant::new("123123123"),
      entry: eat("123"),
    };
    for o in &mut parser {
      assert_eq!(o.digested, 3);
    }
    assert_eq!(parser.instant.digested(), 9);
  }

  #[test]
  fn parser_iterator_with_iter_methods() {
    contextual!(i32, i32);

    let mut parser = Parser {
      state: 123,
      heap: 123,
      instant: Instant::new("123123123"),
      entry: eat("123"),
    };
    for (_, o) in (&mut parser).enumerate() {
      assert_eq!(o.digested, 3);
    }
    assert_eq!(parser.instant.digested(), 9);
  }
}