prattle/node.rs
1// node.rs - MIT License
2// MIT License
3// Copyright (c) 2018 Tyler Laing (ZerothLaw)
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23//! # Node enum
24//!
25//! This is a general purpose enum construct for representing parse trees.
26//!
27//! In the most general case, you have simple nodes (representing concrete constructs
28//! like identifiers or numbers), and composite nodes, which has a single root token
29//! (for example an operator), and zero-to-many child nodes.
30//!
31//! It should be fairly easy to take an AST built with this type into something more
32//! specialized for your language. The idea is that the parser needs to know
33//! what type is being returned.
34//!
35// TODO: Make this a trait and let users manage ASTNode definition and construction?
36
37use std::fmt::{Display, Error, Formatter};
38
39use token::Token;
40
41#[derive(Debug, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
42pub enum Node<T: Token> {
43 Simple(T),
44 Composite {
45 token: T,
46 children: Vec<Node<T>>
47 }
48}
49
50impl<T: Token> Display for Node<T> {
51 fn fmt(&self, f: &mut Formatter) -> Result<(), Error>{
52 write!(f,
53 "{}",
54 match self {
55 Node::Simple(ref t) => format!("Simple({})", t),
56 Node::Composite{
57 token: ref t,
58 children: ref childs
59 } => format!("Composite(token: {}, children: {:?})", t, childs )
60 }
61 )
62 }
63}
64
65#[cfg(test)]
66mod test {
67 use super::*;
68 //Catch Send/Sync changes
69 #[test]
70 fn test_node_send() {
71 fn assert_send<T: Send>() {}
72 assert_send::<Node<String>>();
73 }
74
75 #[test]
76 fn test_node_sync() {
77 fn assert_sync<T: Sync>() {}
78 assert_sync::<Node<String>>();
79 }
80}