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

use crate::NameTest;

// https://www.w3.org/TR/1999/REC-xpath-19991116/#NT-AxisName
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum AxisName {
	/// 'ancestor'
	/// Contains the ancestors of the context node;
	/// the ancestors of the context node consist of the parent of context node and the parent's parent and so on;
	/// thus, the ancestor axis will always include the root node, unless the context node is the root node
	Ancestor,
	/// 'ancestor-or-self'
	/// Contains the context node and the ancestors of the context node;
	/// thus, the ancestor axis will always include the root node
	AncestorOrSelf,
	/// 'attribute'
	/// Contains the attributes of the context node; the axis will be empty unless the context node is an element
	Attribute,
	/// 'child'
	/// Contains the children of the context node
	Child,
	/// 'descendant'
	/// Contains the descendants of the context node;
	/// a descendant is a child or a child of a child and so on;
	/// thus the descendant axis never contains attribute or namespace nodes
	Descendant,
	/// 'descendant-or-self'
	/// Contains the context node and the descendants of the context node
	DescendantOrSelf,
	/// 'following'
	/// Contains all nodes in the same document as the context node that are after the context node in document order, excluding any descendants and excluding attribute nodes and namespace nodes
	Following,
	/// 'following-or-self'
	/// Contains all the following siblings of the context node;
	/// if the context node is an attribute node or namespace node, the following-sibling axis is empty
	FollowingSibling,
	/// 'namespace'
	/// Contains the namespace nodes of the context node;
	/// the axis will be empty unless the context node is an element
	Namespace,
	/// 'parent'
	/// Contains the parent of the context node, if there is one
	Parent,
	/// 'preceding'
	/// Contains all nodes in the same document as the context node that are before the context node in document order, excluding any ancestors and excluding attribute nodes and namespace nodes
	Preceding,
	/// 'preceding-sibling'
	/// Contains all the preceding siblings of the context node;
	/// if the context node is an attribute node or namespace node, the preceding-sibling axis is empty
	PrecedingSibling,
	/// 'self'
	/// Contains just the context node itself
	SelfAxis
}

impl AxisName {
	pub fn principal_node_type(&self) -> PrincipalNodeType {
        match *self {
            AxisName::Attribute => PrincipalNodeType::Attribute,
            AxisName::Namespace => PrincipalNodeType::Namespace,
            _ => PrincipalNodeType::Element,
        }
    }
}

// PartialEq<markup5ever::Attribute> for NameTest


#[derive(Debug, Clone, PartialEq)]
pub enum PrincipalNodeType {
	Attribute,
	Namespace,
	Element
}

#[derive(Debug, Clone, PartialEq)]
pub enum NodeType {
	Comment,
	Text,
	ProcessingInstruction(Option<String>),
	Node
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Operator {
	// OperatorName
	/// 'and'
	And,
	/// 'or'
	Or,
	/// 'mod'
	Mod,
	/// 'div'
	Div,

	// MultiplyOperator
	/// '*'
	Star,

	// Other
	/// '/'
	ForwardSlash,
	/// '//'
	DoubleForwardSlash,
	/// '|'
	Pipe,
	/// '+'
	Plus,
	/// '-'
	Minus,
	/// '='
	Equal,
	/// '!='
	DoesNotEqual,
	/// '<'
	LessThan,
	/// '<='
	LessThanOrEqual,
	/// '>'
	GreaterThan,
	/// '>='
	GreaterThanOrEqual
}


// https://www.w3.org/TR/1999/REC-xpath-19991116/#NT-ExprToken
#[derive(Debug, Clone, PartialEq)]
pub enum ExprToken {
	/// '('
	LeftParen,
	/// ')'
	RightParen,
	/// '['
	LeftBracket,
	/// ']'
	RightBracket,
	/// '.'
	Period,
	/// '..'
	ParentNode,
	/// '@'
	AtSign,
	/// ','
	Comma,
	/// '::'
	LocationStep,


	// Specializations
    Axis(AxisName),
	Number(f64),
	Literal(String),
	NameTest(NameTest),
	NodeType(NodeType),
	Operator(Operator),
	FunctionName(String),
	VariableReference(String)
}

impl ExprToken {
	pub fn is_node_type(&self) -> bool {
		matches!(self, ExprToken::NodeType(_))
	}

	pub fn is_name_test(&self) -> bool {
		matches!(self, ExprToken::NameTest(_))
	}

	pub fn is_operator(&self) -> bool {
		matches!(self, ExprToken::Operator(_))
	}

	pub fn is_axis(&self) -> bool {
		matches!(self, ExprToken::Axis(_))
	}

	pub fn is_literal(&self) -> bool {
		matches!(self, ExprToken::Literal(_))
	}

	pub fn is_number(&self) -> bool {
		matches!(self, ExprToken::Number(_))
	}

	pub fn is_function_name(&self) -> bool {
		matches!(self, ExprToken::FunctionName(_))
	}
}

macro_rules! from_impl {
	($struct:ident, $enum:ident) => {
		impl From<$struct> for ExprToken {
			fn from(this: $struct) -> ExprToken {
				ExprToken::$enum(this)
			}
		}

		impl From<&$struct> for ExprToken {
			fn from(this: &$struct) -> ExprToken {
				ExprToken::$enum(this.clone())
			}
		}

		impl From<ExprToken> for Option<$struct> {
			fn from(this: ExprToken) -> Option<$struct> {
				match this {
					ExprToken::$enum(op) => Some(op),
					_ => None
				}
			}
		}
	};
}

from_impl!(AxisName, Axis);
from_impl!(f64, Number);
from_impl!(String, Literal);
from_impl!(NameTest, NameTest);
from_impl!(NodeType, NodeType);
from_impl!(Operator, Operator);