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
//a Imports
//a LexerError
//tt LexerError
/// A trait required of an error within a Lexer - a char that does not
/// match any token parser rust return an error, and this trait
/// requires that such an error be provided
///
/// It might be nice to have this take the [Lexer] too, but then there
/// is a cycle in that Lexer::Error will in general depend on Lexer
/// which depends on Lexer::Error... This breaks code (and the compiler
/// tends to hang forever)
//a Lexer
//tt Lexer
/// The [Lexer] trait is provided by stream types that support parsing
/// into tokens.
///
/// The trait itself requires:
///
/// * a token type that the [Lexer] will produce
///
/// * a stream state (often just a byte offset) that can be tracked
/// during parsing
///
/// * an error type that suports [LexerError] so that the lexer can
/// generate a failure should a token parse fail
///
/// The [Lexer] will parse its stream provided to it by matching data in
/// the stream to tokens using parser functions. Such functions are
/// invoked with a reference to the stream being parsed, the stream
/// state, and the next character in the stream (the one pointed to by
/// the stream state).
///
/// The signature is:
///
/// ```ignore
/// fn parse(stream: &LexerOfStr<P, T, E>, pos:P, ch:char) ->
/// LexerParseResult<P, T, E>
/// ```
///
/// where
///
/// ```ignore
/// LexerParseResult<P, T, E> = Result<Option<P, T>, E>
/// ```
///
/// Parsing functions examine the character they are given, and
/// possibly more characters by accessing the stream using the provide
/// state. If they match, they return an Ok result with the token they
/// have parsed to, *and* an updated state which is *beyond* the
/// matched token.
///
/// If the parser function mismatches then it returns an Ok result of None
///
/// If the parser function hits a fatal error (for example, a stream
/// indicates a network disconnection) then it must return an Err with
/// the appropriate error (of its provided Error type).
///
/// Parser functions are provided to the [Lexer] as an array of Box dyn
/// functions, such as:
///
/// ```ignore
/// let parsers = [
/// Box::new(parse_char_fn) as BoxDynLexerParseFn<OurLexer>
/// Box::new(parse_value_fn),
/// Box::new(parse_whitespace_fn),
/// ];
/// ```
///
/// Note that the use of 'as Box...' is required, as without it type
/// inference will kick in on the Box::new() to infer parse_char_fn as
/// a precise type, whereas the more generic dyn Fn is what is required.
///
/// This trait is provided in part to group the types for a lexical
/// parser together, enabling simpler type inference and less
/// turbofish syntax in clients of the lexical analysis.
//tp LexerParseResult
/// The return value for a Lexer parse function
///
/// This *could* have been defined as:
///
/// pub type LexerParseResult<L:Lexer>
/// = Result<Option<(<L as Lexer>::State, <L as Lexer>::Token)>, <L as Lexer>::Error>;
///
/// But then clients that have their type L with a lifetime (which is common) would have a parse
/// result that must be indicated by a lifetime, where the actual result *does not*.
///
/// This causes problems for clients
pub type LexerParseResult<S, T, E> = ;
//tp LexerParseFn
/// The type of a parse function
pub type LexerParseFn<L> =
fn ;
//tp BoxDynLexerParseFn
/// The type of a parse function, when Boxed as a dyn trait
///
/// This type can be used in arrays/slices to allow a Lexer to run
/// through a list of possible token parsers such as:
///
/// ```ignore
/// let parsers = [
/// Box::new(parse_char_fn) as BoxDynLexerParseFn<OurLexer>
/// Box::new(parse_value_fn),
/// Box::new(parse_whitespace_fn),
/// ];
/// ```
///
/// Note that the use of 'as Box...' is required, as without it type
/// inference will kick in on the Box::new() to infer parse_char_fn as
/// a precise type, whereas the more generic dyn Fn is what is required.
pub type BoxDynLexerParseFn<'a, L> = ;