swamp_script_node/
lib.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/script
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5
6use std::fmt;
7use std::fmt::{Debug, Formatter};
8
9#[derive(Clone, Eq, PartialEq, Default, Hash)]
10pub struct Node {
11    pub span: Span,
12}
13
14impl Debug for Node {
15    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
16        if self.span.file_id == 0xffff {
17            write!(f, "<{}:{}>", self.span.offset, self.span.length)
18        } else {
19            write!(
20                f,
21                "<{}:{} ({})>",
22                self.span.offset, self.span.length, self.span.file_id
23            )
24        }
25    }
26}
27
28pub type FileId = u16;
29
30#[derive(PartialEq, Eq, Hash, Default, Clone)]
31pub struct Span {
32    pub file_id: FileId,
33    pub offset: u32,
34    pub length: u16,
35}
36
37impl Span {
38    pub fn dummy() -> Self {
39        Span {
40            offset: 0,
41            length: 0,
42            file_id: 0xffff,
43        }
44    }
45
46    // Helper method to get the end position
47    pub fn end(&self) -> u32 {
48        self.offset + self.length as u32
49    }
50}
51
52impl Debug for Span {
53    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
54        write!(f, "<{}:{} ({})>", self.offset, self.length, self.file_id)
55    }
56}
57
58impl Node {
59    pub fn new_unknown() -> Self {
60        Self {
61            span: Span {
62                file_id: 0xffff,
63                offset: 0,
64                length: 0,
65            },
66        }
67    }
68}