zscript_parser/
lib.rs

1pub mod parser;
2pub mod tokenizer;
3
4pub mod ast;
5pub mod hir;
6pub mod ir_common;
7
8pub mod err;
9pub mod filesystem;
10pub mod interner;
11pub mod parser_manager;
12
13#[cfg(feature = "serialize")]
14use serde::Serialize;
15
16#[cfg_attr(feature = "serialize", derive(Serialize))]
17#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
18pub struct Span {
19    file: filesystem::FileIndex,
20    start: usize,
21    end: usize,
22}
23
24impl Span {
25    #[inline(always)]
26    fn combine(self, other: Span) -> Span {
27        // note - this function expects the spans to come
28        // from the same file
29        // it doesn't actually check this though
30        Span {
31            start: self.start.min(other.start),
32            end: self.end.max(other.end),
33            file: self.file,
34        }
35    }
36
37    pub fn get_file(&self) -> filesystem::FileIndex {
38        self.file
39    }
40    pub fn get_start(&self) -> usize {
41        self.start
42    }
43    pub fn get_end(&self) -> usize {
44        self.end
45    }
46}
47
48fn get_lines(source: &str) -> Vec<std::ops::Range<usize>> {
49    let source_ptr = source.as_ptr() as usize;
50    source
51        .split('\n')
52        .map(|l| {
53            let l_ptr = l.as_ptr() as usize;
54            let offset = l_ptr - source_ptr;
55            offset..(offset + l.len())
56        })
57        .collect()
58}