| 193 | } |
| 194 | |
| 195 | fn include( |
| 196 | &mut self, |
| 197 | path: &str, |
| 198 | path_span: Span, |
| 199 | begin: usize, |
| 200 | stdlib: &StandardLibrary, |
| 201 | ) -> Result<(), Diagnostic> { |
| 202 | let mut buffer = vec![]; |
| 203 | |
| 204 | let (owner, mut path) = if let Some(path) = path.strip_prefix("std/") { |
| 205 | (Owner::StandardLibrary, stdlib.path.join(path)) |
| 206 | } else { |
| 207 | (Owner::Local, self.path.parent().unwrap().join(path)) |
| 208 | }; |
| 209 | let mut path_with_extension = path.clone(); |
| 210 | path_with_extension.set_extension("gs"); |
| 211 | if !path_with_extension.is_file() && path.is_dir() { |
| 212 | let file_name = path.file_name().unwrap().to_owned(); |
| 213 | path.push(file_name); |
| 214 | } |
| 215 | path.set_extension("gs"); |
| 216 | let mut file = File::open(&path).map_err(|error| Diagnostic { |
| 217 | kind: DiagnosticKind::IOError(error), |
| 218 | span: path_span, |
| 219 | })?; |
| 220 | file.read_to_end(&mut buffer).unwrap(); |
| 221 | self.text.splice(begin..begin, buffer.iter().cloned()); |
| 222 | |
| 223 | // split current include into two parts |
| 224 | |
| 225 | let current_include = self.includes.remove(self.current_include); |
| 226 | |
| 227 | // buffer before the include stmt |
| 228 | let top_unit_range = current_include.unit_range.start..begin; |
| 229 | self.includes.insert( |
| 230 | self.current_include, |
| 231 | Include { |
| 232 | unit_range: top_unit_range.clone(), |
| 233 | source_range: current_include.source_range.start |
| 234 | ..(current_include.source_range.start + top_unit_range.len()), |
| 235 | path: current_include.path.clone(), |
| 236 | owner: current_include.owner, |
| 237 | }, |
| 238 | ); |
| 239 | |
| 240 | // insert a new include in the middle |
| 241 | self.includes.insert( |
| 242 | self.current_include + 1, |
| 243 | Include { |
| 244 | unit_range: begin..begin + buffer.len(), |
| 245 | source_range: 0..buffer.len(), |
| 246 | path, |
| 247 | owner, |
| 248 | }, |
| 249 | ); |
| 250 | |
| 251 | // buffer after the include stmt |
| 252 | let bottom_unit_range = begin..current_include.unit_range.end; |