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
use std::collections::VecDeque;
use std::fmt::Display;
use crate::parser::ast::attributes::AttributeGroup;
use crate::parser::ast::identifiers::SimpleIdentifier;
use crate::parser::error::ParseError;
use crate::parser::state::stream::TokenStream;
pub mod stream;
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum NamespaceType {
Braced,
Unbraced,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Scope {
Namespace(SimpleIdentifier),
BracedNamespace(Option<SimpleIdentifier>),
}
#[derive(Debug)]
pub struct State<'a> {
pub stack: VecDeque<Scope>,
pub stream: &'a mut TokenStream<'a>,
pub attributes: Vec<AttributeGroup>,
pub namespace_type: Option<NamespaceType>,
pub errors: Vec<ParseError>,
}
impl<'a> State<'a> {
pub fn new(tokens: &'a mut TokenStream<'a>) -> Self {
Self {
stack: VecDeque::with_capacity(32),
stream: tokens,
namespace_type: None,
attributes: vec![],
errors: vec![],
}
}
pub fn attribute(&mut self, attr: AttributeGroup) {
self.attributes.push(attr);
}
pub fn get_attributes(&mut self) -> Vec<AttributeGroup> {
let mut attributes = vec![];
std::mem::swap(&mut self.attributes, &mut attributes);
attributes
}
pub fn record(&mut self, error: ParseError) {
self.errors.push(error);
}
pub fn namespace_type(&self) -> Option<&NamespaceType> {
self.namespace_type.as_ref()
}
pub fn namespace(&self) -> Option<&Scope> {
self.stack.iter().next()
}
pub fn named<T: Display + ?Sized>(&self, name: &T) -> String {
match self.namespace() {
Some(Scope::Namespace(n)) | Some(Scope::BracedNamespace(Some(n))) => {
format!("{}\\{}", n, name)
}
_ => name.to_string(),
}
}
pub fn enter(&mut self, scope: Scope) {
match &scope {
Scope::Namespace(_) => {
self.namespace_type = Some(NamespaceType::Unbraced);
}
Scope::BracedNamespace(_) => {
self.namespace_type = Some(NamespaceType::Braced);
}
}
self.stack.push_back(scope);
}
pub fn exit(&mut self) {
self.stack.pop_back();
}
}