Skip to content

Commit ac30617

Browse files
committed
Replace FileRange with ByteRange in surface
1 parent d797ddd commit ac30617

File tree

11 files changed

+439
-347
lines changed

11 files changed

+439
-347
lines changed

fathom/src/core.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::fmt;
44

55
use crate::env::{Index, Level};
6+
use crate::files::FileId;
67
use crate::source::{Span, StringId};
78

89
pub mod binary;
@@ -12,6 +13,7 @@ pub mod semantics;
1213

1314
/// Modules
1415
pub struct Module<'arena> {
16+
pub file_id: FileId,
1517
pub items: &'arena [Item<'arena>],
1618
}
1719

fathom/src/core/pretty.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111
//! use codespan_reporting::term::termcolor::{BufferedStandardStream, ColorChoice};
1212
//! use fathom::core::pretty::Context;
1313
//! use fathom::core::Module;
14+
//! use fathom::files::FileId;
1415
//! use fathom::source::StringInterner;
1516
//!
1617
//! // These are created for demonstration
1718
//! let interner = RefCell::new(StringInterner::new());
18-
//! let module = Module { items: &[] };
19+
//! let module = Module {
20+
//! file_id: FileId::default(),
21+
//! items: &[],
22+
//! };
1923
//!
2024
//! let pp = Context::new(&interner);
2125
//! let doc = pp.module(&module);

fathom/src/driver.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use codespan_reporting::term::termcolor::{BufferedStandardStream, ColorChoice, W
88

99
use crate::core::binary::{self, BufferError, ReadError};
1010
use crate::files::{FileId, Files};
11-
use crate::source::{FileRange, Span, StringInterner};
11+
use crate::source::{ByteRange, Span, StringInterner};
1212
use crate::surface::{self, elaboration};
1313
use crate::{core, BUG_REPORT_URL};
1414

@@ -192,7 +192,7 @@ impl<'surface, 'core> Driver<'surface, 'core> {
192192
}
193193

194194
pub fn elaborate_and_emit_module(&mut self, file_id: FileId, pretty_core: bool) -> Status {
195-
let mut context = elaboration::Context::new(&self.interner, &self.core_scope);
195+
let mut context = elaboration::Context::new(file_id, &self.interner, &self.core_scope);
196196

197197
let surface_module = self.parse_module(file_id);
198198
let module = context.elab_module(&self.core_scope, &surface_module, &mut |m| {
@@ -217,7 +217,7 @@ impl<'surface, 'core> Driver<'surface, 'core> {
217217
}
218218

219219
pub fn elaborate_and_emit_term(&mut self, file_id: FileId) -> Status {
220-
let mut context = elaboration::Context::new(&self.interner, &self.core_scope);
220+
let mut context = elaboration::Context::new(file_id, &self.interner, &self.core_scope);
221221

222222
// Parse and elaborate the term
223223
let surface_term = self.parse_term(file_id);
@@ -241,7 +241,7 @@ impl<'surface, 'core> Driver<'surface, 'core> {
241241
}
242242

243243
pub fn normalise_and_emit_term(&mut self, file_id: FileId) -> Status {
244-
let mut context = elaboration::Context::new(&self.interner, &self.core_scope);
244+
let mut context = elaboration::Context::new(file_id, &self.interner, &self.core_scope);
245245

246246
// Parse and elaborate the term
247247
let surface_term = self.parse_term(file_id);
@@ -277,7 +277,8 @@ impl<'surface, 'core> Driver<'surface, 'core> {
277277

278278
let initial_buffer = binary::Buffer::from(buffer_data);
279279
let mut binary_context = binary::Context::new(initial_buffer);
280-
let mut elab_context = elaboration::Context::new(&self.interner, &self.core_scope);
280+
let mut elab_context =
281+
elaboration::Context::new(format_file_id, &self.interner, &self.core_scope);
281282

282283
// Parse and elaborate a module if one was provided
283284
if let Some(file_id) = module_file_id {
@@ -294,9 +295,14 @@ impl<'surface, 'core> Driver<'surface, 'core> {
294295
// will need to be revisited if we need to support multiple modules, but
295296
// it works for now!
296297
let surface_format = self.parse_term(format_file_id);
297-
let format = elab_context.elab_format(&self.core_scope, &surface_format, &mut |m| {
298-
self.emit_diagnostic(m.to_diagnostic(&self.interner));
299-
});
298+
let format = elab_context.elab_format(
299+
&self.core_scope,
300+
format_file_id,
301+
&surface_format,
302+
&mut |m| {
303+
self.emit_diagnostic(m.to_diagnostic(&self.interner));
304+
},
305+
);
300306

301307
// Return early if we’ve seen any errors, unless `allow_errors` is enabled
302308
if *self.seen_errors.borrow() && !self.allow_errors {
@@ -330,20 +336,20 @@ impl<'surface, 'core> Driver<'surface, 'core> {
330336
Status::Ok
331337
}
332338

333-
fn parse_module(&'surface self, file_id: FileId) -> surface::Module<'surface, FileRange> {
339+
fn parse_module(&'surface self, file_id: FileId) -> surface::Module<'surface, ByteRange> {
334340
let source = self.files.get(file_id).unwrap().source();
335341
let (module, messages) =
336342
surface::Module::parse(&self.interner, &self.surface_scope, file_id, source);
337-
self.emit_diagnostics(messages.into_iter().map(|m| m.to_diagnostic()));
343+
self.emit_diagnostics(messages.into_iter().map(|m| m.to_diagnostic(file_id)));
338344

339345
module
340346
}
341347

342-
fn parse_term(&'surface self, file_id: FileId) -> surface::Term<'surface, FileRange> {
348+
fn parse_term(&'surface self, file_id: FileId) -> surface::Term<'surface, ByteRange> {
343349
let source = self.files.get(file_id).unwrap().source();
344350
let (term, messages) =
345351
surface::Term::parse(&self.interner, &self.surface_scope, file_id, source);
346-
self.emit_diagnostics(messages.into_iter().map(move |m| m.to_diagnostic()));
352+
self.emit_diagnostics(messages.into_iter().map(move |m| m.to_diagnostic(file_id)));
347353

348354
term
349355
}

fathom/src/files.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ use codespan_reporting::files::{Error, SimpleFile};
1414
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
1515
pub struct FileId(NonZeroU32);
1616

17+
impl Default for FileId {
18+
fn default() -> Self {
19+
Self(NonZeroU32::new(1).unwrap())
20+
}
21+
}
22+
1723
impl fmt::Display for FileId {
1824
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1925
fmt::Display::fmt(&self.0, f)

fathom/src/source.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
//! Types related to source files.
22
3-
use std::{
4-
fmt,
5-
ops::{Deref, DerefMut, Range},
6-
};
3+
use std::fmt;
4+
use std::ops::{Deref, DerefMut, Range};
75

86
use crate::files::FileId;
97

@@ -237,47 +235,48 @@ pub type BytePos = u32;
237235
#[derive(Copy, Clone)]
238236
pub struct FileRange {
239237
file_id: FileId,
240-
start: BytePos,
241-
end: BytePos,
238+
byte_range: ByteRange,
242239
}
243240

244241
impl fmt::Debug for FileRange {
245242
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
246243
write!(
247244
f,
248245
"FileRange({}, {}..{})",
249-
self.file_id, self.start, self.end
246+
self.file_id, self.byte_range.start, self.byte_range.end
250247
)
251248
}
252249
}
253250

254251
impl FileRange {
255-
pub const fn new(file_id: FileId, start: BytePos, end: BytePos) -> FileRange {
252+
pub const fn new(file_id: FileId, byte_range: ByteRange) -> FileRange {
256253
FileRange {
257254
file_id,
258-
start,
259-
end,
255+
byte_range,
260256
}
261257
}
262258

263259
pub fn file_id(&self) -> FileId {
264260
self.file_id
265261
}
266262

263+
pub const fn byte_range(&self) -> ByteRange {
264+
self.byte_range
265+
}
266+
267267
pub const fn start(&self) -> BytePos {
268-
self.start
268+
self.byte_range.start
269269
}
270270

271271
pub const fn end(&self) -> BytePos {
272-
self.end
272+
self.byte_range.end
273273
}
274274

275275
pub fn merge(&self, other: &FileRange) -> Option<FileRange> {
276276
if self.file_id == other.file_id {
277277
Some(FileRange::new(
278278
self.file_id,
279-
self.start.min(other.start),
280-
self.end.max(other.end),
279+
self.byte_range.merge(other.byte_range),
281280
))
282281
} else {
283282
None
@@ -287,7 +286,7 @@ impl FileRange {
287286

288287
impl From<FileRange> for Range<usize> {
289288
fn from(range: FileRange) -> Self {
290-
(range.start as usize)..(range.end as usize)
289+
range.byte_range.into()
291290
}
292291
}
293292

@@ -321,6 +320,12 @@ impl ByteRange {
321320
}
322321
}
323322

323+
impl From<ByteRange> for Range<usize> {
324+
fn from(range: ByteRange) -> Self {
325+
(range.start as usize)..(range.end as usize)
326+
}
327+
}
328+
324329
#[cfg(test)]
325330
mod tests {
326331
use super::*;

0 commit comments

Comments
 (0)