acir/parser/
span.rs

1//! Minimal span types used by the ACIR text parser.
2//!
3//! These mirror a small slice of the public API of `noirc_span` (`Span`, `Position`,
4//! `Spanned`). We intentionally do not depend on `noirc_span` because `acir` is published
5//! to crates.io as part of the ACVM release set, while `noirc_span` is a compiler crate
6//! that is not. Pulling it in would either block the `acir` release or force every
7//! transitive compiler crate onto crates.io. Since these types are only used internally
8//! by the parser, an inline copy of just what the parser needs is the cheaper option.
9
10pub(super) type Position = u32;
11
12#[derive(PartialEq, Eq, Debug, Copy, Clone, Default)]
13pub(crate) struct Span {
14    start: u32,
15    end: u32,
16}
17
18impl Span {
19    pub(super) fn inclusive(start: u32, end: u32) -> Span {
20        Span { start, end: end + 1 }
21    }
22
23    pub(super) fn single_char(position: u32) -> Span {
24        Span::inclusive(position, position)
25    }
26
27    pub(crate) fn start(&self) -> u32 {
28        self.start
29    }
30
31    pub(crate) fn end(&self) -> u32 {
32        self.end
33    }
34}
35
36#[derive(Debug, Clone)]
37pub(super) struct Spanned<T> {
38    pub(super) contents: T,
39    span: Span,
40}
41
42impl<T> Spanned<T> {
43    pub(super) fn from_position(start: Position, end: Position, contents: T) -> Spanned<T> {
44        Spanned { span: Span::inclusive(start, end), contents }
45    }
46
47    pub(super) fn from(t_span: Span, contents: T) -> Spanned<T> {
48        Spanned { span: t_span, contents }
49    }
50
51    pub(super) fn span(&self) -> Span {
52        self.span
53    }
54}