1pub mod ast;
2pub(crate) mod compound;
3pub(crate) mod expressions;
4pub mod parser;
5pub(crate) mod pre_parsing_hooks;
6use std::{iter::Cloned, slice::Iter};
7
8use nom::Needed;
9pub use parser::parse;
10pub use pre_parsing_hooks::pre_parse;
11use tokenizer::tokens::CommentedToken;
12pub(crate) mod program;
13pub(crate) mod token_parsers;
14pub(crate) mod whitespace;
15
16#[derive(Debug, Clone, PartialEq)]
17pub struct Input<'a, 'b: 'a>(pub &'b [&'a CommentedToken<'a>]);
18
19impl<'a, 'b> std::ops::Deref for Input<'a, 'b> {
20 type Target = &'b [&'a CommentedToken<'a>];
21
22 fn deref(&self) -> &Self::Target {
23 &self.0
24 }
25}
26
27impl<'a> nom::Input for Input<'a, '_> {
28 type Item = &'a CommentedToken<'a>;
29 type Iter = Cloned<Iter<'a, &'a CommentedToken<'a>>>;
30 type IterIndices = std::iter::Enumerate<Self::Iter>;
31
32 fn input_len(&self) -> usize {
33 self.0.len()
34 }
35
36 fn take(&self, index: usize) -> Self {
37 Input(&self.0[0..index])
38 }
39
40 fn take_from(&self, index: usize) -> Self {
41 Input(&self.0[index..])
42 }
43
44 fn take_split(&self, index: usize) -> (Self, Self) {
45 let (prefix, suffix) = self.0.split_at(index);
46 (Input(suffix), Input(prefix))
47 }
48
49 fn position<P>(&self, predicate: P) -> Option<usize>
50 where
51 P: Fn(Self::Item) -> bool,
52 {
53 self.0.iter().position(|b| predicate(b))
54 }
55
56 fn iter_elements(&self) -> Self::Iter {
57 self.0.iter().cloned()
58 }
59
60 fn iter_indices(&self) -> Self::IterIndices {
61 self.iter_elements().enumerate()
62 }
63
64 fn slice_index(&self, count: usize) -> Result<usize, Needed> {
65 if self.0.len() >= count {
66 Ok(count)
67 } else {
68 Err(Needed::new(count - self.0.len()))
69 }
70 }
71}
72
73impl std::fmt::Display for Input<'_, '_> {
74 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75 match self.0.split_first() {
76 None => Ok(()),
77 Some((first, rest)) => {
78 f.write_fmt(format_args!("{:?}", first.token))?;
79 for token in rest {
80 write!(f, " {:?}", token.token)?;
81 }
82 Ok(())
83 }
84 }
85 }
86}
87
88pub(crate) struct InputForDisplay<'a>(&'a [&'a CommentedToken<'a>]);
89
90impl std::fmt::Display for InputForDisplay<'_> {
91 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92 match self.0.split_first() {
93 None => Ok(()),
94 Some((first, rest)) => {
95 f.write_fmt(format_args!("{}", first))?;
96 for token in rest.iter().take(2) {
97 write!(f, " {}", token)?;
98 }
99 Ok(())
100 }
101 }
102 }
103}