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
//! Module dedicated to [Parameter] parsing.
use super::r#type::Type;
use super::value::Value;
use super::word::*;
use super::CommentsAnnotations;
use super::PositionnedString;
use crate::ScriptError;
use core::slice::Windows;
use std::collections::HashMap;
/// Structure describing a textual parameter.
///
/// It owns a name, and optional [Type] and/or [Value]. There is no logical dependency between them at this point.
#[derive(Clone, Debug)]
pub struct Parameter {
pub annotations: Option<CommentsAnnotations>,
pub name: PositionnedString,
pub variability: Option<PositionnedString>,
pub r#type: Option<Type>,
pub value: Option<Value>,
}
impl Parameter {
/// Build a parameter by parsing words, starting when name is expected.
///
/// * `variability_or_name`: The variability or name already parsed for the `Parameter` (its accuracy is under responsibility of the caller).
/// * `iter`: Iterator over words list, next() being expected to be about [Type].
///
pub fn build_from_name(
annotations: Option<CommentsAnnotations>,
variability_or_name: PositionnedString,
mut iter: &mut Windows<Word>,
global_annotations: &mut HashMap<Word, CommentsAnnotations>,
) -> Result<Self, ScriptError> {
if variability_or_name.string == "var" || variability_or_name.string == "const" {
match iter.next().map(|s| &s[0]) {
Some(w) if w.kind == Some(Kind::Name) => {
iter.next()
.map(|s| &s[0])
.ok_or_else(|| ScriptError::end_of_script(55))
.and_then(|w| {
if w.kind != Some(Kind::Colon) {
Err(ScriptError::word(56, w.clone(), &[Kind::Colon]))
} else {
Ok(())
}
})?;
Self::build_from_type(
annotations,
Some(variability_or_name),
w.into(),
&mut iter,
global_annotations,
)
}
Some(w) => return Err(ScriptError::word(57, w.clone(), &[Kind::Name])),
None => return Err(ScriptError::end_of_script(58)),
}
} else {
iter.next()
.map(|s| &s[0])
.ok_or_else(|| ScriptError::end_of_script(59))
.and_then(|w| {
if w.kind != Some(Kind::Colon) {
Err(ScriptError::word(60, w.clone(), &[Kind::Colon]))
} else {
Ok(())
}
})?;
Self::build_from_type(
annotations,
None,
variability_or_name,
&mut iter,
global_annotations,
)
}
}
/// Build a parameter by parsing words, starting when named [Type] is expected.
///
/// * `variability`: The variability already parsed for the `Parameter` (its accuracy is under responsibility of the caller).
/// * `name`: The name already parsed for the `Parameter` (its accuracy is under responsibility of the caller).
/// * `iter`: Iterator over words list, next() being expected to be about [Type].
///
pub fn build_from_type(
annotations: Option<CommentsAnnotations>,
variability: Option<PositionnedString>,
name: PositionnedString,
mut iter: &mut Windows<Word>,
global_annotations: &mut HashMap<Word, CommentsAnnotations>,
) -> Result<Self, ScriptError> {
let (r#type, possible_equal) = Type::build(&mut iter, global_annotations)?;
match possible_equal.kind {
Some(Kind::Equal) => {
// We discard the equal sign.
iter.next();
let value = Value::build_from_first_item(&mut iter, global_annotations)?;
Ok(Self {
annotations,
name,
variability,
r#type: Some(r#type),
value: Some(value),
})
}
_ => Ok(Self {
annotations,
name,
variability,
r#type: Some(r#type),
value: None,
}),
}
}
/// Build a parameter by parsing words, starting when a [Value] is expected.
///
/// * `name`: The name already parsed for the `Parameter` (its accuracy is under responsibility of the caller).
/// * `iter`: Iterator over words list, next() being expected to be about [Value].
///
pub fn build_from_value(
annotations: Option<CommentsAnnotations>,
name: PositionnedString,
iter: &mut Windows<Word>,
global_annotations: &mut HashMap<Word, CommentsAnnotations>,
) -> Result<Self, ScriptError> {
let value = Value::build_from_first_item(iter, global_annotations)?;
Ok(Self {
annotations,
name,
variability: None,
r#type: None,
value: Some(value),
})
}
}