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
use ;
use ;
/// Object-safe methods for turning `self` into `dyn Any`.
///
/// These are blanket-implemented for all [`Sized`] types.
// ----------------------------------------------------------------------------
/// A type that represents a generous parse tree.
///
/// The first parser pass is "generous" in the sense that it accepts a
/// significant superset of the Welly language. If you parse a valid program,
/// the generous parse tree will have the correct structure. If you parse a
/// nearly valid program, you will hopefully nonetheless get a parse tree that
/// is close to the one intended. This helps with reporting helpful errors.
///
/// There are many types that implement `Tree`. It would be inconvenient to
/// define an `enum` that can contain any of them. Instead, we use `dyn Tree`.
/// You can use the methods of `dyn Tree` to match its actual type:
///
/// ```
/// use welly_parser::{Tree};
///
/// // Invent a new kind of `Tree`.
/// #[derive(Debug)]
/// struct Fruit(&'static str);
/// impl Tree for Fruit {}
///
/// // An example `Fruit` wrapped as a `dyn Tree`.
/// let tree: Box<dyn Tree> = Box::new(Fruit("Apple"));
///
/// // Test whether `tree` is a `Fruit`.
/// let is_fruit: bool = tree.is::<Fruit>();
/// println!("{}", is_fruit);
///
/// // Borrow the `Fruit`.
/// let borrowed_fruit: Option<&Fruit> = tree.downcast_ref::<Fruit>();
/// println!("{}", borrowed_fruit.expect("Not a Fruit").0);
///
/// // Move the `Fruit`.
/// let owned_fruit: Result<Box<Fruit>, Box<dyn Tree>> = tree.downcast::<Fruit>();
/// println!("{}", owned_fruit.expect("Not a Fruit").0);
/// ```
// ----------------------------------------------------------------------------
/// Represents the end of the source code.
///
/// Parsers must return this [`Tree`] unchanged. It must never be incorporated
/// into a larger `Tree`.
;