krab/common/
mod.rs

1pub mod tokens;
2mod ty;
3mod value;
4
5use std::fmt::Display;
6
7pub use ty::Type;
8pub use value::*;
9
10#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
11pub enum ControlFlow {
12    Continue,
13    Break,
14    Return,
15}
16
17#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
18pub enum SpanKind {
19    Default,
20    Eof,
21}
22
23#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
24pub struct Span {
25    pub start: usize,
26    pub end: usize,
27    pub loc: usize,
28    kind: SpanKind,
29}
30
31impl Span {
32    pub const ZERO: Span = Span {
33        start: 0,
34        end: 0,
35        loc: 0,
36        kind: SpanKind::Default,
37    };
38    pub const EOF: Span = Span {
39        start: 0,
40        end: 0,
41        loc: 0,
42        kind: SpanKind::Eof,
43    };
44
45    pub const fn new(start: usize, end: usize, loc: usize) -> Span {
46        Span {
47            start,
48            end,
49            loc,
50            kind: SpanKind::Default,
51        }
52    }
53}
54
55impl Display for Span {
56    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57        match self.kind {
58            SpanKind::Default => write!(f, "line {}, pos {}", self.loc, self.start),
59            SpanKind::Eof => write!(f, "EOF"),
60        }
61    }
62}