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
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/master/LICENSE ).
//! `AST` provides a set of Syntax Tree Nodes used to store
//! the output of [`parse`] method that is used in [`test_condition`] method
//! to evaluate whether a given [`PluralCategory`] should be used.
//!
//! # Examples
//!
//! ```
//! use icu_plurals::rules::parse_condition;
//! use icu_plurals::rules::ast::*;
//!
//! let input = "i = 1";
//!
//! let ast = parse_condition(input.as_bytes())
//!     .expect("Parsing failed.");
//!
//! assert_eq!(ast, Condition(Box::new([
//!     AndCondition(Box::new([
//!         Relation {
//!             expression: Expression {
//!                 operand: Operand::I,
//!                 modulus: None,
//!             },
//!             operator: Operator::Eq,
//!             range_list: RangeList(Box::new([
//!                 RangeListItem::Value(
//!                     Value(1)
//!                 )
//!             ]))
//!         }
//!     ]))
//! ])));
//! ```
//!
//! [`PluralCategory`]: ../enum.PluralCategory.html
//! [`parse`]: ../fn.parse.html
//! [`test_condition`]: ../fn.test_condition.html
use std::ops::RangeInclusive;

/// A complete AST representation of a plural rule.
/// Comprises a vector of `AndConditions` and optionally a set of Samples.
///
/// # Examples
///
/// ```
/// use icu_plurals::rules::ast::*;
/// use icu_plurals::rules::{parse, parse_condition};
///
/// let condition = parse_condition(b"i = 5 or v = 2")
///     .expect("Parsing failed.");
///
/// let samples = Samples {
///     integer: Some(SampleList {
///         sample_ranges: Box::new([SampleRange {
///             lower_val: DecimalValue("2".to_string()),
///             upper_val: None,
///         }]),
///         ellipsis: true
///     }),
///     decimal: Some(SampleList {
///         sample_ranges: Box::new([SampleRange {
///             lower_val: DecimalValue("2.5".to_string()),
///             upper_val: None,
///         }]),
///         ellipsis: false
///     }),
/// };
///
/// let rule = Rule {
///     condition,
///     samples: Some(samples),
/// };
///
/// assert_eq!(
///     rule,
///     parse("i = 5 or v = 2 @integer 2, … @decimal 2.5".as_bytes())
///          .expect("Parsing failed")
/// )
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct Rule {
    pub condition: Condition,
    pub samples: Option<Samples>,
}

/// A complete AST representation of a plural rule's condition. Comprises a vector of `AndConditions`.
///
/// # Examples
///
/// ```
/// use icu_plurals::rules::ast::*;
/// use icu_plurals::rules::parse_condition;
///
/// let condition = Condition(Box::new([
///     AndCondition(Box::new([Relation {
///         expression: Expression {
///             operand: Operand::I,
///             modulus: None,
///         },
///         operator: Operator::Eq,
///         range_list: RangeList(Box::new([RangeListItem::Value(Value(5))])),
///     }])),
///     AndCondition(Box::new([Relation {
///         expression: Expression {
///             operand: Operand::V,
///             modulus: None,
///         },
///         operator: Operator::Eq,
///         range_list: RangeList(Box::new([RangeListItem::Value(Value(2))])),
///     }])),
/// ]));
///
/// assert_eq!(
///     condition,
///     parse_condition(b"i = 5 or v = 2")
///          .expect("Parsing failed")
/// )
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct Condition(pub Box<[AndCondition]>);

/// An incomplete AST representation of a plural rule. Comprises a vector of Relations.
///
/// # Examples
///
/// All AST nodes can be built explicitly, as seen in the example. However, due to its complexity, it is preferred to build the AST using the `parse_plural_rule` function.
///
/// ```text
/// "i = 3 and v = 0"
/// ```
///
/// Can be represented by the AST:
///
/// ```
/// use icu_plurals::rules::ast::*;
///
/// AndCondition(Box::new([
///     Relation {
///         expression: Expression {
///             operand: Operand::I,
///             modulus: None,
///         },
///         operator: Operator::Eq,
///         range_list: RangeList(Box::new([RangeListItem::Value(Value(5))])),
///     },
///     Relation {
///         expression: Expression {
///             operand: Operand::V,
///             modulus: None,
///         },
///         operator: Operator::NotEq,
///         range_list: RangeList(Box::new([RangeListItem::Value(Value(2))])),
///     },
/// ]));
///
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct AndCondition(pub Box<[Relation]>);

/// An incomplete AST representation of a plural rule. Comprises an `Expression`, an `Operator`, and a `RangeList`.
///
/// # Examples
///
/// All AST nodes can be built explicitly, as seen in the example. However, due to its complexity, it is preferred to build the AST using the `parse_plural_rule` function.
///
/// ```text
/// "i = 3"
/// ```
///
/// Can be represented by the AST:
///
/// ```
/// use icu_plurals::rules::ast::*;
///
/// Relation {
///     expression: Expression {
///         operand: Operand::I,
///         modulus: None,
///     },
///     operator: Operator::Eq,
///     range_list: RangeList(Box::new([RangeListItem::Value(Value(3))])),
/// };
///
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct Relation {
    pub expression: Expression,
    pub operator: Operator,
    pub range_list: RangeList,
}

/// An enum of Relation operators for plural rules.
///
/// Each Operator enumeration belongs to the corresponding symbolic operators:
///
/// | Enum Operator | Symbolic Operator |
/// | - | - |
/// | `Eq` | "=" |
/// | `NotEq` | "!=" |
///
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Operator {
    Eq,
    NotEq,
}

/// An incomplete AST representation of a plural rule. Comprises an Operand and an optional Modulo.
///
/// # Examples
///
/// All AST nodes can be built explicitly, as seen in the example. However, due to its complexity, it is preferred to build the AST using the `parse_plural_rule` function.
///
/// ```text
/// "i % 100"
/// ```
///
/// Can be represented by the AST:
///
/// ```
/// use icu_plurals::rules::ast::*;
///
/// Expression {
///     operand: Operand::I,
///     modulus: Some(Value(100)),
/// };
///
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct Expression {
    pub operand: Operand,
    pub modulus: Option<Value>,
}

/// An incomplete AST representation of a plural rule. Comprises a char.
///
/// # Examples
///
/// All AST nodes can be built explicitly, as seen in the example. However, due to its complexity, it is preferred to build the AST using the `parse_plural_rule` function.
///
/// ```text
/// "i"
/// ```
///
/// Can be represented by the AST:
///
/// ```
/// use icu_plurals::rules::ast::Operand;
///
/// Operand::I;
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Operand {
    /// Absolute value of input
    N,
    /// An integer value of input with the fraction part truncated off
    I,
    /// Number of visible fraction digits with trailing zeros
    V,
    /// Number of visible fraction digits without trailing zeros
    W,
    /// Visible fraction digits with trailing zeros
    F,
    /// Visible fraction digits without trailing zeros
    T,
}

/// An incomplete AST representation of a plural rule. Comprises a vector of `RangeListItems`.
///
/// # Examples
///
/// All AST nodes can be built explicitly, as seen in the example. However, due to its complexity, it is preferred to build the AST using the `parse_plural_rule` function.
///
/// ```text
/// "5, 7, 9"
/// ```
///
/// Can be represented by the AST:
///
/// ```
/// use icu_plurals::rules::ast::*;
///
/// RangeList(Box::new([
///     RangeListItem::Value(Value(5)),
///     RangeListItem::Value(Value(7)),
///     RangeListItem::Value(Value(9)),
/// ]));
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct RangeList(pub Box<[RangeListItem]>);

/// An enum of items that appear in a `RangeList`: `Range` or a `Value`.
///
/// See Range and Value for additional details.
/// A range comprises two Values: an inclusive lower and upper limit.
///
/// # Examples
///
/// ```text
/// 5
/// 11..15
/// ```
///
/// Can be represented by the AST:
///
/// ```
/// use icu_plurals::rules::ast::*;
///
/// let _ = RangeListItem::Value(Value(5));
/// let _ = RangeListItem::Range(Value(11)..=Value(15));
/// ```
#[derive(Debug, Clone, PartialEq)]
pub enum RangeListItem {
    Range(RangeInclusive<Value>),
    Value(Value),
}

/// An incomplete AST representation of a plural rule, representing one integer.
///
/// # Examples
///
/// All AST nodes can be built explicitly, as seen in the example. However, due to its complexity, it is preferred to build the AST using the `parse_plural_rule` function.
///
/// ```text
/// "99"
/// ```
///
/// Can be represented by the AST:
///
/// ```
/// use icu_plurals::rules::ast::*;
///
/// RangeListItem::Value(Value(99));
/// ```
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct Value(pub u64);

/// A sample of example values that match the given rule.
///
/// # Examples
///
/// ```text
/// @integer 2, … @decimal 2.5
/// ```
///
/// ```
/// use icu_plurals::rules::ast::*;
/// Samples {
///     integer: Some(SampleList {
///         sample_ranges: Box::new([SampleRange {
///             lower_val: DecimalValue("2".to_string()),
///             upper_val: None,
///         }]),
///         ellipsis: true
///     }),
///     decimal: Some(SampleList {
///         sample_ranges: Box::new([SampleRange {
///             lower_val: DecimalValue("2.5".to_string()),
///             upper_val: None,
///         }]),
///         ellipsis: false
///     }),
/// };
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct Samples {
    pub integer: Option<SampleList>,
    pub decimal: Option<SampleList>,
}

/// A list of values used in samples.
///
/// # Examples
///
/// ```text
/// 0.0~1.5, …
/// ```
///
/// ```
/// use icu_plurals::rules::ast::*;
/// SampleList {
///     sample_ranges: Box::new([
///         SampleRange {
///             lower_val: DecimalValue("0.0".to_string()),
///             upper_val: Some(DecimalValue("1.5".to_string())),
///         }
///     ]),
///     ellipsis: true
/// };
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct SampleList {
    pub sample_ranges: Box<[SampleRange]>,
    pub ellipsis: bool,
}

/// A value range used in samples.
///
/// # Examples
///
/// ```text
/// 0.0~1.5
/// ```
///
/// ```
/// use icu_plurals::rules::ast::*;
/// SampleRange {
///     lower_val: DecimalValue("0.0".to_string()),
///     upper_val: Some(DecimalValue("1.5".to_string())),
/// };
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct SampleRange {
    pub lower_val: DecimalValue,
    pub upper_val: Option<DecimalValue>,
}

/// A decimal value used in samples.
///
/// # Examples
///
/// ```text
/// 1.00
/// ```
///
/// ```
/// use icu_plurals::rules::ast::*;
/// DecimalValue("1.00".to_string());
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct DecimalValue(pub String);