-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtype_.rs
275 lines (231 loc) · 6.25 KB
/
type_.rs
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
use std::fmt;
use crate::{
common::{identifier::Identifier, token, Span, Spanned},
pretty::Pretty,
variable::Variable,
};
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
pub struct BuiltinValueType {
pub span: Option<Span>,
pub token: token::ValueType,
}
impl BuiltinValueType {
pub fn new(span: Option<Span>, token: token::ValueType) -> Self {
Self { span, token }
}
}
impl Spanned for BuiltinValueType {
fn span(&self) -> Option<Span> {
self.span
}
}
impl Pretty for BuiltinValueType {}
impl fmt::Display for BuiltinValueType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.token)
}
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct Label {
pub span: Option<Span>,
pub ident: Identifier,
}
impl Label {
pub fn new(span: Option<Span>, ident: Identifier) -> Self {
Self { span, ident }
}
}
impl Spanned for Label {
fn span(&self) -> Option<Span> {
self.span
}
}
impl Pretty for Label {}
impl fmt::Display for Label {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.ident)
}
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct ScopedLabel {
pub span: Option<Span>,
pub scope: Label,
pub name: Label,
}
impl ScopedLabel {
pub fn new(span: Option<Span>, scope: Label, name: Label) -> Self {
Self { span, scope, name }
}
}
impl Spanned for ScopedLabel {
fn span(&self) -> Option<Span> {
self.span
}
}
impl Pretty for ScopedLabel {}
impl fmt::Display for ScopedLabel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", self.scope, self.name)
}
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum NamedType {
Label(Label),
BuiltinValueType(BuiltinValueType),
}
impl Spanned for NamedType {
fn span(&self) -> Option<Span> {
match self {
Self::Label(inner) => inner.span(),
Self::BuiltinValueType(inner) => inner.span(),
}
}
}
impl fmt::Display for NamedType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Label(inner) => fmt::Display::fmt(inner, f),
Self::BuiltinValueType(inner) => fmt::Display::fmt(inner, f),
}
}
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum NamedTypeAny {
Simple(NamedType),
List(NamedTypeList),
Optional(NamedTypeOptional),
}
impl Spanned for NamedTypeAny {
fn span(&self) -> Option<Span> {
match self {
Self::Simple(named) => named.span(),
Self::List(list) => list.span(),
Self::Optional(optional) => optional.span(),
}
}
}
impl Pretty for NamedTypeAny {}
impl fmt::Display for NamedTypeAny {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Simple(inner) => fmt::Display::fmt(inner, f),
Self::List(inner) => fmt::Display::fmt(inner, f),
Self::Optional(inner) => fmt::Display::fmt(inner, f),
}
}
}
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct NamedTypeList {
pub span: Option<Span>,
pub inner: NamedType,
}
impl NamedTypeList {
pub fn new(span: Option<Span>, inner: NamedType) -> Self {
Self { span, inner }
}
}
impl Spanned for NamedTypeList {
fn span(&self) -> Option<Span> {
self.span
}
}
impl Pretty for NamedTypeList {}
impl fmt::Display for NamedTypeList {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}[]", self.inner)
}
}
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct NamedTypeOptional {
pub span: Option<Span>,
pub inner: NamedType,
}
impl NamedTypeOptional {
pub fn new(span: Option<Span>, inner: NamedType) -> Self {
Self { span, inner }
}
}
impl Spanned for NamedTypeOptional {
fn span(&self) -> Option<Span> {
self.span
}
}
impl Pretty for NamedTypeOptional {}
impl fmt::Display for NamedTypeOptional {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}?", self.inner)
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum TypeRef {
Label(Label), // person OR friendship:friend OR string
Scoped(ScopedLabel),
Variable(Variable), // $t
}
impl Spanned for TypeRef {
fn span(&self) -> Option<Span> {
match self {
Self::Label(label) => label.span(),
Self::Scoped(scoped) => scoped.span(),
Self::Variable(var) => var.span(),
}
}
}
impl Pretty for TypeRef {}
impl fmt::Display for TypeRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Label(inner) => fmt::Display::fmt(inner, f),
Self::Scoped(inner) => fmt::Display::fmt(inner, f),
Self::Variable(inner) => fmt::Display::fmt(inner, f),
}
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum TypeRefAny {
Type(TypeRef), // person, friendship:friend, or $t
List(TypeRefList), // person[], friendship:friend[], or $t[]
}
impl Pretty for TypeRefAny {}
impl Spanned for TypeRefAny {
fn span(&self) -> Option<Span> {
match self {
Self::Type(inner) => inner.span(),
Self::List(inner) => inner.span(),
}
}
}
impl fmt::Display for TypeRefAny {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Type(inner) => fmt::Display::fmt(inner, f),
Self::List(inner) => fmt::Display::fmt(inner, f),
}
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct TypeRefList {
pub span: Option<Span>,
pub inner: TypeRef,
}
impl TypeRefList {
pub fn new(span: Option<Span>, inner: TypeRef) -> Self {
Self { span, inner }
}
}
impl Spanned for TypeRefList {
fn span(&self) -> Option<Span> {
self.span
}
}
impl Pretty for TypeRefList {}
impl fmt::Display for TypeRefList {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}[]", self.inner)
}
}