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
//! The compiler's second stage: the token stream the lexer answers in, a
//! private tree out ([`tree`]), or the one refusal that stopped it. It reads
//! the reference's grammar with the reference's precedence, lowest first:
//!
//! ```text
//! program -> statement ( ";" statement )* ";"?
//! statement -> if | while | location "=" expression | expression
//! expression -> logical_or
//! logical_or -> logical_and ( ( "OR" | "||" ) logical_and )*
//! logical_and -> logical_not ( ( "AND" | "&&" ) logical_not )*
//! logical_not -> ( "NOT" | "!" ) logical_not | comparison
//! comparison -> addition ( relation addition | presence )?
//! relation -> ">" | "<" | ">=" | "<=" | "==" | "!=" | "===" | "!=="
//! | "in" | "contains" | "intersects"
//! presence -> "is" "not"? ( "blank" | "present" | "defined" )
//! addition -> multiplication ( ( "+" | "-" ) multiplication )*
//! multiplication -> unary ( ( "*" | "/" | "%" ) unary )*
//! unary -> ( "-" | "!" ) unary | postfix
//! postfix -> primary ( "[" expression "]" | "." name | "::" type )*
//! ```
//!
//! The binary operators associate to the left. A comparison takes one
//! operator at most, so a second is a token after a finished expression:
//! `a < b < c` is refused at the second `<`. `NOT` and a `!` at the head of a
//! `logical_not` bind looser than a comparison (`!a == b` negates the
//! comparison); a `!` anywhere else is the prefix operator of `unary`.
//!
//! # The newer operators
//!
//! `intersects` and the presence test `is [not] blank`, `is [not] present`
//! and `is [not] defined` are read by position: each only directly after a
//! complete `addition`, where a relation may stand, and so at the precedence
//! of `in` and `contains`. Each word is spelled in lower case or in capitals
//! (`IS NOT BLANK`, `INTERSECTS`), and `not` is the lexer's `NOT` in either
//! spelling. A presence test is the comparison, so it does not chain with
//! another relation, and none of the words is reserved: anywhere else each
//! is an ordinary name (`is is blank` tests the variable `is`), and a word
//! after an operand that starts no such operator is left for the caller.
//!
//! # Statements
//!
//! A statement is an assignment where an `addition` read from its start is
//! followed by `=`; that read is a probe, and when it is refused or no `=`
//! follows, the statement is read again as an expression. A left side that
//! is not an identifier under property and bracket accesses is refused at
//! the `=`. A statement ending in a closing brace needs no `;` before the
//! next. A program holds one statement at least.
//!
//! # Refusals
//!
//! A refusal is a [`ParseError`] carrying the reference's message, at the
//! position and over the span of the token it names, and a reason, which is
//! this crate's token for the refusal. The end of input is the lexer's end
//! token, a zero-width span where the source ends.
//!
//! # Depth
//!
//! One refusal is not the reference's, which follows a source as deep as it
//! nests: a source nesting more than [`DEPTH_LIMIT`] levels is refused with
//! the reason `nesting_depth_exceeded`, at the token the parse stopped on. A
//! level is counted each time the parse enters a whole expression (the
//! source's, and one inside a parenthesis, a list, an object, an index, a
//! call's arguments, a statement's condition or value), for each prefix
//! operator, a relative date's operand, a block's statements, an `else if`
//! and a statement's assignment probe. A chain of left-associative
//! operators or of postfix steps is read in a loop and costs none.
//!
//! The parse does not recurse: what is open is held on explicit stacks, so
//! the stack a parse runs on does not grow with the source, and the bound
//! exists for the walks over the tree that follow. The tree drops without
//! recursing too (see [`tree`]).
pub
use crate;
use crate::;
use ;
/// How many levels a source may nest, the whole expression counting as the
/// first. A source at exactly this depth parses. It is the bound the Ruby
/// sibling chose, and above the 300 parentheses the reference's compile
/// transcript compiles.
pub const DEPTH_LIMIT: usize = 400;
/// Parses one expression from a token stream the lexer answered.
pub
/// Parses a program, one statement or more, from a token stream the lexer
/// answered.
pub
/// The token a stream with no tokens at all reads as: the end of input at
/// the start of the source. The lexer always ends a stream with its own.
static END: Token = Token ;
/// One parse of one token stream: the tokens, the cursor and how many
/// levels are open.
/// A refusal at the start of `span` and over it.
/// One past the token's last character; a string token, which may hold a
/// line feed, carries its own.