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
use std::fmt::Display;
use std::fmt::Error;
use std::fmt::Formatter;
use super::text::TextContext;
use super::text::TextPosition;
use super::text::TextSpan;
use super::tokens::Token;
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum SymbolType {
Terminal,
Variable,
Virtual
}
pub const SID_NOTHING: u32 = 0;
pub const SID_EPSILON: u32 = 1;
pub const SID_DOLLAR: u32 = 2;
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct Symbol {
pub id: u32,
pub name: &'static str
}
impl Display for Symbol {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(f, "{}", self.name)
}
}
pub trait SemanticElementTrait {
fn get_position(&self) -> Option<TextPosition>;
fn get_span(&self) -> Option<TextSpan>;
fn get_context(&self) -> Option<TextContext>;
fn get_symbol(&self) -> Symbol;
fn get_value(&self) -> Option<String>;
}
pub enum SemanticElement<'a> {
Token(Token<'a>),
Terminal(Symbol),
Variable(Symbol),
Virtual(Symbol)
}
impl<'a> SemanticElementTrait for SemanticElement<'a> {
fn get_position(&self) -> Option<TextPosition> {
match self {
&SemanticElement::Token(ref token) => token.get_position(),
&SemanticElement::Terminal(ref _symbol) => None,
&SemanticElement::Variable(ref _symbol) => None,
&SemanticElement::Virtual(ref _symbol) => None
}
}
fn get_span(&self) -> Option<TextSpan> {
match self {
&SemanticElement::Token(ref token) => token.get_span(),
&SemanticElement::Terminal(ref _symbol) => None,
&SemanticElement::Variable(ref _symbol) => None,
&SemanticElement::Virtual(ref _symbol) => None
}
}
fn get_context(&self) -> Option<TextContext> {
match self {
&SemanticElement::Token(ref token) => token.get_context(),
&SemanticElement::Terminal(ref _symbol) => None,
&SemanticElement::Variable(ref _symbol) => None,
&SemanticElement::Virtual(ref _symbol) => None
}
}
fn get_symbol(&self) -> Symbol {
match self {
&SemanticElement::Token(ref token) => token.get_symbol(),
&SemanticElement::Terminal(ref symbol) => *symbol,
&SemanticElement::Variable(ref symbol) => *symbol,
&SemanticElement::Virtual(ref symbol) => *symbol
}
}
fn get_value(&self) -> Option<String> {
match self {
&SemanticElement::Token(ref token) => token.get_value(),
&SemanticElement::Terminal(ref _symbol) => None,
&SemanticElement::Variable(ref _symbol) => None,
&SemanticElement::Virtual(ref _symbol) => None
}
}
}
impl<'a> SemanticElement<'a> {
pub fn get_symbol_type(&self) -> SymbolType {
match self {
&SemanticElement::Token(ref _token) => SymbolType::Terminal,
&SemanticElement::Terminal(ref _symbol) => SymbolType::Terminal,
&SemanticElement::Variable(ref _symbol) => SymbolType::Variable,
&SemanticElement::Virtual(ref _symbol) => SymbolType::Virtual
}
}
}
pub trait SemanticBody {
fn get_element_at(&self, index: usize) -> SemanticElement;
fn length(&self) -> usize;
}
pub type SemanticAction = FnMut(Symbol, &SemanticBody);