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
//! Module dedicated to [Instanciation] parsing.
use super::common::{
parse_configuration_assignations, parse_generics, parse_parameters_assignations,
};
use super::parameter::Parameter;
use super::word::{Kind, Word};
use super::{CommentsAnnotations, Generic, PositionnedString};
use crate::ScriptError;
use core::slice::Windows;
use std::collections::HashMap;
/// Structure describing a textual instanciation.
///
/// This match the conceptual syntax of calling a model or treatment.
/// It owns a name, a type (treatment or model type, not [data type](super::Type)), and list of [parameters](super::Parameter).
#[derive(Clone, Debug)]
pub struct Instanciation {
pub annotations: Option<CommentsAnnotations>,
pub name: PositionnedString,
pub r#type: PositionnedString,
pub generics: Vec<Generic>,
pub configuration: Vec<Parameter>,
pub parameters: Vec<Parameter>,
}
impl Instanciation {
/// Build an instanciation by parsing words.
///
/// * `iter`: Iterator over words list, next() being expected to be the name.
///
pub fn build(
annotations: Option<CommentsAnnotations>,
mut iter: &mut Windows<Word>,
global_annotations: &mut HashMap<Word, CommentsAnnotations>,
) -> Result<Self, ScriptError> {
let name: PositionnedString = iter
.next()
.map(|s| &s[0])
.ok_or_else(|| ScriptError::end_of_script(62))
.and_then(|w| {
if w.kind != Some(Kind::Name) {
Err(ScriptError::word(63, w.clone(), &[Kind::Name]))
} else {
Ok(w.into())
}
})?;
match iter.next().map(|s| &s[0]) {
Some(w) if w.kind == Some(Kind::Colon) => {
Self::build_from_type(annotations, name.clone(), &mut iter, global_annotations)
}
Some(w) if w.kind == Some(Kind::OpeningBracket) => Self::build_from_generics(
annotations,
name.clone(),
name,
&mut iter,
global_annotations,
),
Some(w) if w.kind == Some(Kind::OpeningBracket) => Self::build_from_configuration(
annotations,
name.clone(),
name,
Vec::new(),
&mut iter,
global_annotations,
),
Some(w) if w.kind == Some(Kind::OpeningParenthesis) => Self::build_from_parameters(
annotations,
name.clone(),
name,
Vec::new(),
Vec::new(),
&mut iter,
global_annotations,
),
Some(w) => {
return Err(ScriptError::word(
64,
w.clone(),
&[
Kind::Colon,
Kind::OpeningChevron,
Kind::OpeningBracket,
Kind::OpeningParenthesis,
],
))
}
None => return Err(ScriptError::end_of_script(65)),
}
}
/// Build an instanciation by parsing words, starting when its type is expected.
///
/// * `name`: The name already parsed for the `Instanciation` (its accuracy is under responsibility of the caller).
/// * `iter`: Iterator over words list, next() being expected to be the type name.
///
pub fn build_from_type(
annotations: Option<CommentsAnnotations>,
name: PositionnedString,
mut iter: &mut Windows<Word>,
global_annotations: &mut HashMap<Word, CommentsAnnotations>,
) -> Result<Self, ScriptError> {
let r#type = iter
.next()
.map(|s| &s[0])
.ok_or_else(|| ScriptError::end_of_script(66))
.and_then(|w| {
if w.kind != Some(Kind::Name) {
Err(ScriptError::word(67, w.clone(), &[Kind::Name]))
} else {
Ok(w.into())
}
})?;
match iter.next().map(|s| &s[0]) {
Some(w) if w.kind == Some(Kind::OpeningChevron) => Self::build_from_generics(
annotations,
name.clone(),
r#type,
&mut iter,
global_annotations,
),
Some(w) if w.kind == Some(Kind::OpeningBracket) => Self::build_from_configuration(
annotations,
name.clone(),
r#type,
Vec::new(),
&mut iter,
global_annotations,
),
Some(w) if w.kind == Some(Kind::OpeningParenthesis) => Self::build_from_parameters(
annotations,
name.clone(),
r#type,
Vec::new(),
Vec::new(),
&mut iter,
global_annotations,
),
Some(w) => {
return Err(ScriptError::word(
68,
w.clone(),
&[
Kind::OpeningChevron,
Kind::OpeningBracket,
Kind::OpeningParenthesis,
],
))
}
None => return Err(ScriptError::end_of_script(69)),
}
}
/// Build an instanciation by parsing words, starting when generics [Generic] is expected.
///
/// * `name`: The name already parsed for the `Instanciation` (its accuracy is under responsibility of the caller).
/// * `iter`: Iterator over words list, next() being expected to be about [Generic].
///
pub fn build_from_generics(
annotations: Option<CommentsAnnotations>,
name: PositionnedString,
r#type: PositionnedString,
mut iter: &mut Windows<Word>,
global_annotations: &mut HashMap<Word, CommentsAnnotations>,
) -> Result<Self, ScriptError> {
let generics = parse_generics(&mut iter, global_annotations)?;
// We expect configuration or parameters in any cases.
match iter.next().map(|s| &s[0]) {
Some(w) if w.kind == Some(Kind::OpeningBracket) => Self::build_from_configuration(
annotations,
name,
r#type,
generics,
&mut iter,
global_annotations,
),
Some(w) if w.kind == Some(Kind::OpeningParenthesis) => Self::build_from_parameters(
annotations,
name,
r#type,
generics,
Vec::new(),
&mut iter,
global_annotations,
),
Some(w) => Err(ScriptError::word(
167,
w.clone(),
&[Kind::OpeningBracket, Kind::OpeningParenthesis],
)),
None => Err(ScriptError::end_of_script(166)),
}
}
/// Build an instanciation by parsing words, starting when configuration [Parameter] is expected.
///
/// * `name`: The name already parsed for the `Instanciation` (its accuracy is under responsibility of the caller).
/// * `iter`: Iterator over words list, next() being expected to be about [Parameter].
///
pub fn build_from_configuration(
annotations: Option<CommentsAnnotations>,
name: PositionnedString,
r#type: PositionnedString,
generics: Vec<Generic>,
mut iter: &mut Windows<Word>,
global_annotations: &mut HashMap<Word, CommentsAnnotations>,
) -> Result<Self, ScriptError> {
let configuration = parse_configuration_assignations(&mut iter, global_annotations)?;
// We expect parameters in any cases.
iter.next()
.map(|s| &s[0])
.ok_or_else(|| ScriptError::end_of_script(70))
.and_then(|w| {
if w.kind != Some(Kind::OpeningParenthesis) {
Err(ScriptError::word(
71,
w.clone(),
&[Kind::OpeningParenthesis],
))
} else {
Ok(())
}
})?;
Self::build_from_parameters(
annotations,
name,
r#type,
generics,
configuration,
&mut iter,
global_annotations,
)
}
/// Build an instanciation by parsing words, starting when [Parameter] is expected.
///
/// * `name`: The name already parsed for the `Instanciation` (its accuracy is under responsibility of the caller).
/// * `iter`: Iterator over words list, next() being expected to be about [Parameter].
///
pub fn build_from_parameters(
annotations: Option<CommentsAnnotations>,
name: PositionnedString,
r#type: PositionnedString,
generics: Vec<Generic>,
configuration: Vec<Parameter>,
mut iter: &mut Windows<Word>,
global_annotations: &mut HashMap<Word, CommentsAnnotations>,
) -> Result<Self, ScriptError> {
let parameters = parse_parameters_assignations(&mut iter, global_annotations)?;
Ok(Self {
annotations,
name,
r#type,
generics,
configuration,
parameters,
})
}
}