tagua_parser/ast.rs
1// Tagua VM
2//
3//
4// New BSD License
5//
6// Copyright © 2016-2016, Ivan Enderlin.
7// All rights reserved.
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are met:
11// * Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// * Redistributions in binary form must reproduce the above copyright
14// notice, this list of conditions and the following disclaimer in the
15// documentation and/or other materials provided with the distribution.
16// * Neither the name of the Hoa nor the names of its contributors may be
17// used to endorse or promote products derived from this software without
18// specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
24// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30// POSSIBILITY OF SUCH DAMAGE.
31
32//! Structures that will constitute the Abstract Syntax Tree.
33
34/// A term.
35#[derive(Debug, PartialEq)]
36pub struct Term {
37 /// The term value.
38 pub t: Literal
39}
40
41/// An addition of two terms.
42#[derive(Debug, PartialEq)]
43pub struct Addition {
44 /// The left-hand side of the addition.
45 pub a: Term,
46 /// The right-hand side of the addition.
47 pub b: Term
48}
49
50/// A literal represents a fixed value, aka an atom.
51#[derive(Debug, PartialEq)]
52pub enum Literal {
53 /// A null value.
54 ///
55 /// # Examples
56 ///
57 /// ```
58 /// # extern crate tagua_parser;
59 /// use tagua_parser::Result;
60 /// use tagua_parser::ast::Literal;
61 /// use tagua_parser::rules::literals::literal;
62 ///
63 /// # fn main () {
64 /// assert_eq!(literal(b"null"), Result::Done(&b""[..], Literal::Null));
65 /// # }
66 /// ```
67 Null,
68
69 /// A boolean, either `true` or `false`.
70 ///
71 /// # Examples
72 ///
73 /// ```
74 /// # extern crate tagua_parser;
75 /// use tagua_parser::Result;
76 /// use tagua_parser::ast::Literal;
77 /// use tagua_parser::rules::literals::literal;
78 ///
79 /// # fn main () {
80 /// assert_eq!(literal(b"true"), Result::Done(&b""[..], Literal::Boolean(true)));
81 /// assert_eq!(literal(b"false"), Result::Done(&b""[..], Literal::Boolean(false)));
82 /// # }
83 /// ```
84 Boolean(bool),
85
86 /// An integer, for instance a binary, octal, decimal or hexadecimal number.
87 ///
88 /// # Examples
89 ///
90 /// ```
91 /// # extern crate tagua_parser;
92 /// use tagua_parser::Result;
93 /// use tagua_parser::ast::Literal;
94 /// use tagua_parser::rules::literals::literal;
95 ///
96 /// # fn main () {
97 /// let output = Result::Done(&b""[..], Literal::Integer(42i64));
98 ///
99 /// assert_eq!(literal(b"0b101010"), output);
100 /// assert_eq!(literal(b"052"), output);
101 /// assert_eq!(literal(b"42"), output);
102 /// assert_eq!(literal(b"0x2a"), output);
103 /// # }
104 /// ```
105 Integer(i64),
106
107 /// A real, for instance an exponential number.
108 ///
109 /// # Examples
110 ///
111 /// ```
112 /// # extern crate tagua_parser;
113 /// use tagua_parser::Result;
114 /// use tagua_parser::ast::Literal;
115 /// use tagua_parser::rules::literals::literal;
116 ///
117 /// # fn main () {
118 /// let output = Result::Done(&b""[..], Literal::Real(4.2f64));
119 ///
120 /// assert_eq!(literal(b"4.2"), output);
121 /// assert_eq!(literal(b".42e1"), output);
122 /// assert_eq!(literal(b"420e-2"), output);
123 /// # }
124 /// ```
125 Real(f64),
126
127 /// A string.
128 ///
129 /// # Examples
130 ///
131 /// ```
132 /// # extern crate tagua_parser;
133 /// use tagua_parser::Result;
134 /// use tagua_parser::ast::Literal;
135 /// use tagua_parser::rules::literals::literal;
136 ///
137 /// # fn main () {
138 /// assert_eq!(
139 /// literal(b"'foo\\'bar'"),
140 /// Result::Done(&b""[..], Literal::String(b"foo'bar".to_vec()))
141 /// );
142 /// # }
143 /// ```
144 String(Vec<u8>)
145}
146
147
148/// A variable.
149///
150/// # Examples
151///
152/// ```
153/// # extern crate tagua_parser;
154/// use tagua_parser::Result;
155/// use tagua_parser::ast::Variable;
156/// use tagua_parser::rules::tokens::variable;
157///
158/// # fn main () {
159/// assert_eq!(
160/// variable(b"$foo"),
161/// Result::Done(&b""[..], Variable(&b"foo"[..]))
162/// );
163/// # }
164/// ```
165/// Note that the `$` is not present.
166#[derive(Debug, PartialEq)]
167pub struct Variable<'a>(pub &'a [u8]);
168
169/// A name represents an entity name.
170#[derive(Debug, PartialEq)]
171pub enum Name<'a> {
172 /// An unqualified name, i.e. a name without a namespace, like `Bar`.
173 ///
174 /// # Examples
175 ///
176 /// ```
177 /// # extern crate tagua_parser;
178 /// use tagua_parser::Result;
179 /// use tagua_parser::ast::Name;
180 /// use tagua_parser::rules::tokens::qualified_name;
181 ///
182 /// # fn main () {
183 /// assert_eq!(
184 /// qualified_name(b"Bar"),
185 /// Result::Done(&b""[..], Name::Unqualified(&b"Bar"[..]))
186 /// );
187 /// # }
188 /// ```
189 Unqualified(&'a [u8]),
190
191 /// A qualified name, i.e. a name in a relative namespace (aliased or not),
192 /// like `Foo\Bar`.
193 ///
194 /// # Examples
195 ///
196 /// ```
197 /// # extern crate tagua_parser;
198 /// use tagua_parser::Result;
199 /// use tagua_parser::ast::Name;
200 /// use tagua_parser::rules::tokens::qualified_name;
201 ///
202 /// # fn main () {
203 /// assert_eq!(
204 /// qualified_name(b"Foo\\Bar"),
205 /// Result::Done(&b""[..], Name::Qualified(vec![&b"Foo"[..], &b"Bar"[..]]))
206 /// );
207 /// # }
208 /// ```
209 Qualified(Vec<&'a [u8]>),
210
211 /// A relative qualified name, i.e. a name in a relative namespace
212 /// restricted to the current namespace, like `namespace\Foo\Bar`.
213 ///
214 /// # Examples
215 ///
216 /// ```
217 /// # extern crate tagua_parser;
218 /// use tagua_parser::Result;
219 /// use tagua_parser::ast::Name;
220 /// use tagua_parser::rules::tokens::qualified_name;
221 ///
222 /// # fn main () {
223 /// assert_eq!(
224 /// qualified_name(b"namespace\\Foo\\Bar"),
225 /// Result::Done(&b""[..], Name::RelativeQualified(vec![&b"Foo"[..], &b"Bar"[..]]))
226 /// );
227 /// # }
228 /// ```
229 /// Note that the `namespace` part is not present.
230 RelativeQualified(Vec<&'a [u8]>),
231
232 /// A fully qualified name, i.e. a name in an absolute namespace, like
233 /// `\Foo\Bar`.
234 ///
235 /// # Examples
236 ///
237 /// ```
238 /// # extern crate tagua_parser;
239 /// use tagua_parser::Result;
240 /// use tagua_parser::ast::Name;
241 /// use tagua_parser::rules::tokens::qualified_name;
242 ///
243 /// # fn main () {
244 /// assert_eq!(
245 /// qualified_name(b"\\Foo\\Bar"),
246 /// Result::Done(&b""[..], Name::FullyQualified(vec![&b"Foo"[..], &b"Bar"[..]]))
247 /// );
248 /// # }
249 /// ```
250 /// Note that the leading `\` part is not present.
251 FullyQualified(Vec<&'a [u8]>)
252}