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
//! Module dedicated to [Script] parsing.
use super::annotation::Annotation;
use super::model::Model;
use super::r#use::Use;
use super::treatment::Treatment;
use super::word::{get_words, Kind, Position, Word};
use super::{CommentsAnnotations, PositionnedString};
use crate::ScriptError;
use std::collections::HashMap;
/// Structure managing and describing textual script.
///
/// It owns the whole script text, as well as parsed attributes, including [Use]s, [Annotation]s, [Model]s, and [Treatment]s.
/// There is no logical coherence involved there, only syntax analysis and parsing.
#[derive(Clone, Debug)]
pub struct Script {
pub text: String,
pub uses: Vec<Use>,
pub annotations: Vec<Annotation>,
pub models: Vec<Model>,
pub treatments: Vec<Treatment>,
}
impl Script {
/// Build script by parsing the whole content.
///
/// This is the main function of the whole [text module](super), it process the entire textual content of script and build a syntax tree.
/// It also makes a copy of `text` and keeps it by its own.
///
/// * `text`: The text of the script itself.
///
/// # Note
/// It doesn't check any logic, only syntax analysis and parsing.
///
pub fn build(text: &str) -> Result<Self, ScriptError> {
let mut uses = Vec::new();
let mut models = Vec::new();
let mut treatments = Vec::new();
let words = get_words(text);
if let Err(err_words) = words {
if let Some(err_word) = err_words.last() {
return Err(ScriptError::word(19, err_word.clone(), &[]));
} else {
return Err(ScriptError::end_of_script(20));
}
}
let mut words = words.unwrap();
// Annotation and documentation purpose: associating every annotation, attribute, comment, or documentation item
// with the nearest next word that is something else than one of those.
let mut annotated_items = HashMap::new();
let mut last_doc = None;
let mut annotations = Vec::new();
let mut comments = Vec::new();
for word in words.iter() {
if word.kind == Some(Kind::Comment) {
if word.text.starts_with("///") {
last_doc = Some(PositionnedString {
string: word.text.strip_prefix("///").unwrap().to_string(),
position: Position {
absolute_position: word.position.absolute_position + 3,
line_number: word.position.line_number,
line_position: word.position.line_position + 3,
},
});
} else if word.text.starts_with("/**") {
last_doc = Some(PositionnedString {
string: word
.text
.strip_prefix("/**")
.unwrap()
.strip_suffix("*/")
.unwrap()
.to_string(),
position: Position {
absolute_position: word.position.absolute_position + 3,
line_number: word.position.line_number,
line_position: word.position.line_position + 3,
},
});
} else if word.text.starts_with("//") {
comments.push(PositionnedString {
string: word.text.strip_prefix("//").unwrap().to_string(),
position: Position {
absolute_position: word.position.absolute_position + 2,
line_number: word.position.line_number,
line_position: word.position.line_position + 2,
},
});
} else if word.text.starts_with("/*") {
comments.push(PositionnedString {
string: word
.text
.strip_prefix("/*")
.unwrap()
.strip_suffix("*/")
.unwrap()
.to_string(),
position: Position {
absolute_position: word.position.absolute_position + 2,
line_number: word.position.line_number,
line_position: word.position.line_position + 2,
},
});
}
} else if word.kind == Some(Kind::Annotation) {
annotations.push(Annotation { text: word.into() });
} else {
annotated_items.insert(
word.clone(),
CommentsAnnotations {
doc: last_doc,
comments,
annotations,
},
);
last_doc = None;
comments = Vec::new();
annotations = Vec::new();
}
}
// Removing all comments and annotations.
words.retain(|w| w.kind != Some(Kind::Comment) && w.kind != Some(Kind::Annotation));
// Adding a last word for eof signal
words.push(Word::default());
let words = words;
let mut iter = words.windows(2);
loop {
match iter.next().map(|s| &s[0]) {
Some(w) if w.kind == Some(Kind::Name) => match w.text.as_str() {
"use" => uses.push(Use::build(&mut iter)?),
"model" => models.push(Model::build(
&mut iter,
annotated_items.remove(&w),
&mut annotated_items,
)?),
"treatment" => treatments.push(Treatment::build(
&mut iter,
annotated_items.remove(&w),
&mut annotated_items,
)?),
_ => {
return Err(ScriptError::declaration_expected(
51,
w.clone(),
&["use", "model", "treatment"],
))
}
},
Some(w) => {
return Err(ScriptError::word(
52,
w.clone(),
&[Kind::Annotation, Kind::Name],
))
}
None => break,
}
}
Ok(Self {
text: text.to_string(),
uses,
annotations,
models,
treatments,
})
}
}