plotnik_lib/bytecode/
sections.rs

1//! Binary format section primitives.
2
3use super::ids::StringId;
4
5/// Range into an array: [ptr..ptr+len).
6///
7/// Dual-use depending on context:
8/// - For `TypeDef` wrappers (Optional, Array*): `ptr` is inner `TypeId`, `len` is 0.
9/// - For `TypeDef` composites (Struct, Enum): `ptr` is index into TypeMember array, `len` is count.
10#[derive(Clone, Copy, Debug, Default)]
11#[repr(C)]
12pub struct Slice {
13    pub ptr: u16,
14    pub len: u16,
15}
16
17impl Slice {
18    #[inline]
19    pub fn range(self) -> std::ops::Range<usize> {
20        let start = self.ptr as usize;
21        start..start + self.len as usize
22    }
23}
24
25/// Maps tree-sitter NodeTypeId to its string name.
26#[derive(Clone, Copy, Debug)]
27#[repr(C)]
28pub struct NodeSymbol {
29    /// Tree-sitter node type ID
30    pub(crate) id: u16,
31    /// StringId for the node kind name
32    pub(crate) name: StringId,
33}
34
35impl NodeSymbol {
36    /// Create a new node symbol.
37    pub fn new(id: u16, name: StringId) -> Self {
38        Self { id, name }
39    }
40
41    pub fn id(&self) -> u16 {
42        self.id
43    }
44    pub fn name(&self) -> StringId {
45        self.name
46    }
47}
48
49/// Maps tree-sitter NodeFieldId to its string name.
50#[derive(Clone, Copy, Debug)]
51#[repr(C)]
52pub struct FieldSymbol {
53    /// Tree-sitter field ID
54    pub(crate) id: u16,
55    /// StringId for the field name
56    pub(crate) name: StringId,
57}
58
59impl FieldSymbol {
60    /// Create a new field symbol.
61    pub fn new(id: u16, name: StringId) -> Self {
62        Self { id, name }
63    }
64
65    pub fn id(&self) -> u16 {
66        self.id
67    }
68    pub fn name(&self) -> StringId {
69        self.name
70    }
71}
72
73/// A node type ID that counts as trivia (whitespace, comments).
74#[derive(Clone, Copy, Debug)]
75#[repr(C)]
76pub struct TriviaEntry {
77    pub(crate) node_type: u16,
78}
79
80impl TriviaEntry {
81    /// Create a new trivia entry.
82    pub fn new(node_type: u16) -> Self {
83        Self { node_type }
84    }
85
86    pub fn node_type(&self) -> u16 {
87        self.node_type
88    }
89}