1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use std::fmt;
/// Defines the type of a given value
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub enum ValueType {
/// String values
String,
/// Non-precise signed integer (defaults to 64-bit)
Signed,
/// 8-bit signed integer
I8,
/// 16-bit signed integer
I16,
/// 32-bit signed integer
I32,
/// 64-bit signed integer
I64,
/// 128-bit signed integer
I128,
/// Non-precise unsigned integer (defaults to 64-bit)
Unsigned,
/// 8-bit unsigned integer
U8,
/// 16-bit unsigned integer
U16,
/// 32-bit unsigned integer
U32,
/// 64-bit unsigned integer
U64,
/// 128-bit unsigned integer
U128,
/// Non-precise floating point (defaults to 64-bit)
Float,
/// 32-bit floating point
F32,
/// 64-bit floating point
F64,
/// Byte Array (Vec<u8>)
Bytes,
/// Boolean
Bool,
/// Semantic Version
Version,
/// Semantic Version Requirement
Require,
/// Macro string
Macro,
/// Label identifier
Label,
/// :Symbol identifier
Symbol,
/// Null
Null,
/// Array
Array(Vec<Self>),
/// Table
Table(IndexMap<String, Self>),
}
impl ValueType {
/// Checks if the right type can be assigned to the left when a type requirement
/// is not specified.
///
/// This function implements the type compatibility rules for BarkML, determining
/// whether a value of one type can be assigned to a variable of another type.
///
/// # Arguments
///
/// * `right` - The type of the value being assigned
///
/// # Returns
///
/// `true` if the assignment is valid, `false` otherwise
pub fn can_assign(&self, right: &Self) -> bool {
use ValueType::*;
match (self, right) {
// Exact matches are always valid
(left, right) if left == right => true,
// String types are only compatible with other strings
(String, String) => true,
// Signed integer compatibility
(Signed, I8 | I16 | I32 | I64 | I128 | Signed) => true,
(Signed, U8 | U16 | U32) => true, // Small unsigned can fit in signed
(I64, Signed | I8 | I16 | I32 | I64) => true,
(I64, U8 | U16 | U32) => true, // Small unsigned can fit in i64
// Unsigned integer compatibility
(Unsigned, U8 | U16 | U32 | U64 | U128 | Unsigned) => true,
(U64, Unsigned | U8 | U16 | U32 | U64) => true,
// Float compatibility
(Float, F32 | F64 | Float) => true,
(F64, F64 | Float) => true,
// No other implicit conversions allowed
_ => false,
}
}
/// Determines if this type is a numeric type
pub const fn is_numeric(&self) -> bool {
matches!(
self,
Self::Signed
| Self::Unsigned
| Self::I8
| Self::I16
| Self::I32
| Self::I64
| Self::I128
| Self::U8
| Self::U16
| Self::U32
| Self::U64
| Self::U128
| Self::Float
| Self::F32
| Self::F64
)
}
/// Determines if this type is an integer type
pub const fn is_integer(&self) -> bool {
matches!(
self,
Self::Signed
| Self::Unsigned
| Self::I8
| Self::I16
| Self::I32
| Self::I64
| Self::I128
| Self::U8
| Self::U16
| Self::U32
| Self::U64
| Self::U128
)
}
/// Determines if this type is a floating point type
pub const fn is_float(&self) -> bool {
matches!(self, Self::Float | Self::F32 | Self::F64)
}
/// Determines if this type is a compound type (array or table)
pub const fn is_compound(&self) -> bool {
matches!(self, Self::Array(_) | Self::Table(_))
}
/// Determines if this type is a primitive type (not compound)
pub const fn is_primitive(&self) -> bool {
!self.is_compound()
}
/// Returns the size in bytes for fixed-size types, None for variable-size types
pub const fn size_hint(&self) -> Option<usize> {
match self {
Self::I8 | Self::U8 => Some(1),
Self::I16 | Self::U16 => Some(2),
Self::I32 | Self::U32 | Self::F32 => Some(4),
Self::I64 | Self::U64 | Self::F64 | Self::Signed | Self::Unsigned | Self::Float => {
Some(8)
}
Self::I128 | Self::U128 => Some(16),
Self::Bool => Some(1),
_ => None, // Variable size types
}
}
/// Returns the category of this type for grouping purposes
pub const fn category(&self) -> TypeCategory {
match self {
Self::String => TypeCategory::Text,
Self::Signed | Self::I8 | Self::I16 | Self::I32 | Self::I64 | Self::I128 => {
TypeCategory::SignedInteger
}
Self::Unsigned | Self::U8 | Self::U16 | Self::U32 | Self::U64 | Self::U128 => {
TypeCategory::UnsignedInteger
}
Self::Float | Self::F32 | Self::F64 => TypeCategory::Float,
Self::Bytes => TypeCategory::Binary,
Self::Bool => TypeCategory::Boolean,
Self::Version | Self::Require => TypeCategory::Version,
Self::Macro => TypeCategory::Macro,
Self::Label | Self::Symbol => TypeCategory::Identifier,
Self::Null => TypeCategory::Null,
Self::Array(_) => TypeCategory::Collection,
Self::Table(_) => TypeCategory::Collection,
}
}
}
/// Categories for grouping related types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TypeCategory {
Text,
SignedInteger,
UnsignedInteger,
Float,
Binary,
Boolean,
Version,
Macro,
Identifier,
Null,
Collection,
}
impl fmt::Display for ValueType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::String => f.write_str("string"),
Self::Signed => f.write_str("int"),
Self::I8 => f.write_str("i8"),
Self::I16 => f.write_str("i16"),
Self::I32 => f.write_str("i32"),
Self::I64 => f.write_str("i64"),
Self::I128 => f.write_str("i128"),
Self::Unsigned => f.write_str("uint"),
Self::U8 => f.write_str("u8"),
Self::U16 => f.write_str("u16"),
Self::U32 => f.write_str("u32"),
Self::U64 => f.write_str("u64"),
Self::U128 => f.write_str("u128"),
Self::Float => f.write_str("float"),
Self::F32 => f.write_str("f32"),
Self::F64 => f.write_str("f64"),
Self::Bytes => f.write_str("bytes"),
Self::Bool => f.write_str("bool"),
Self::Version => f.write_str("version"),
Self::Require => f.write_str("require"),
Self::Macro => f.write_str("macro"),
Self::Label => f.write_str("label"),
Self::Symbol => f.write_str("symbol"),
Self::Null => f.write_str("null"),
Self::Array(children) => {
if children.is_empty() {
f.write_str("[]")
} else {
write!(
f,
"[{}]",
children
.iter()
.map(|x| x.to_string())
.collect::<Vec<_>>()
.join(", ")
)
}
}
Self::Table(children) => {
if children.is_empty() {
f.write_str("{}")
} else {
write!(
f,
"{{ {} }}",
children
.iter()
.map(|(k, v)| format!("{k}: {v}"))
.collect::<Vec<_>>()
.join(", ")
)
}
}
}
}
}
/// Represents the type of a statement in the BarkML language
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
pub enum StatementType {
/// Control statement ($identifier = value)
/// Stores the expected type of the value
Control(ValueType),
/// Assignment statement (identifier = value)
/// Stores the expected type of the value
Assignment(ValueType),
/// Block statement (identifier labels { statements })
/// Stores the types of labels and contents
Block {
/// Types of the label values
labels: Vec<ValueType>,
/// Types of the contained statements
contents: IndexMap<String, Self>,
},
/// Section statement ([identifier] statements)
/// Stores the types of contained statements
Section(IndexMap<String, Self>),
/// Module statement (top-level container)
/// Stores the types of contained statements
Module(IndexMap<String, Self>),
}
impl StatementType {
/// Returns true if this statement type can contain child statements
pub const fn is_container(&self) -> bool {
matches!(
self,
Self::Block { .. } | Self::Section(_) | Self::Module(_)
)
}
/// Returns true if this statement type represents a value assignment
pub const fn is_assignment(&self) -> bool {
matches!(self, Self::Control(_) | Self::Assignment(_))
}
/// Gets the value type for assignment statements, None for containers
pub fn value_type(&self) -> Option<&ValueType> {
match self {
Self::Control(vt) | Self::Assignment(vt) => Some(vt),
_ => None,
}
}
/// Gets the child statement types for container statements
pub fn child_types(&self) -> Option<&IndexMap<String, Self>> {
match self {
Self::Block { contents, .. } | Self::Section(contents) | Self::Module(contents) => {
Some(contents)
}
_ => None,
}
}
}
/// Stores the metadata associated with a value or statement
///
/// Metadata provides additional information about AST nodes, including source location,
/// comments, and labels. This information is useful for error reporting, documentation
/// generation, and preserving the original structure of the code.
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize)]
pub struct Metadata {
/// Source location information
pub location: Location,
/// Optional comment associated with the node (from # or /* */ comments)
pub comment: Option<String>,
/// Optional label associated with the node (from !label syntax)
pub label: Option<String>,
}
impl Metadata {
/// Creates a new Metadata instance with the given location
pub const fn new(location: Location) -> Self {
Self {
location,
comment: None,
label: None,
}
}
/// Creates a new Metadata instance with location, comment, and label
pub const fn with_details(
location: Location,
comment: Option<String>,
label: Option<String>,
) -> Self {
Self {
location,
comment,
label,
}
}
/// Returns true if this metadata has a comment
pub const fn has_comment(&self) -> bool {
self.comment.is_some()
}
/// Returns true if this metadata has a label
pub const fn has_label(&self) -> bool {
self.label.is_some()
}
/// Returns true if this metadata has any additional information beyond location
pub const fn has_annotations(&self) -> bool {
self.has_comment() || self.has_label()
}
}
/// Represents a location in the source code
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash, Deserialize, Serialize)]
pub struct Location {
/// Module name where the token was found
pub module: Option<String>,
/// Line number (0-based)
pub line: usize,
/// Column number (0-based)
pub column: usize,
/// Original source text for the token
pub source_text: Option<String>,
/// Length of the token in characters
pub length: usize,
/// File path where the token was found
pub file_path: Option<String>,
}
impl Location {
/// Create a new location with basic information
pub const fn new(line: usize, column: usize) -> Self {
Self {
module: None,
line,
column,
source_text: None,
length: 0,
file_path: None,
}
}
/// Create a new location with full information
pub const fn with_details(
module: Option<String>,
line: usize,
column: usize,
source_text: Option<String>,
length: usize,
file_path: Option<String>,
) -> Self {
Self {
module,
line,
column,
source_text,
length,
file_path,
}
}
/// Set the module name
pub fn set_module(&mut self, module: &str) {
self.module = Some(module.to_string());
}
/// Set the source text
pub fn set_source_text(&mut self, text: &str) {
self.source_text = Some(text.to_string());
self.length = text.len();
}
/// Set the file path
pub fn set_file_path(&mut self, path: &str) {
self.file_path = Some(path.to_string());
}
/// Get the human-readable position (1-based for display)
pub const fn position(&self) -> (usize, usize) {
(self.line + 1, self.column + 1)
}
/// Get the source context for error reporting
///
/// Returns the source text if available, or an empty string if not.
/// This is useful for providing context in error messages.
pub fn context(&self) -> String {
self.source_text.clone().unwrap_or_default()
}
/// Get a formatted source context with line numbers for error reporting
///
/// Returns a formatted string with line numbers and the source text,
/// highlighting the current line if possible.
pub fn formatted_context(&self) -> String {
let Some(source) = &self.source_text else {
return String::new();
};
let lines: Vec<&str> = source.lines().collect();
let line_num = self.line;
// Determine the range of lines to show (up to 2 lines before and after)
let start_line = line_num.saturating_sub(2);
let end_line = std::cmp::min(line_num + 3, lines.len());
let mut result = String::new();
for (offset, line_content) in lines
.iter()
.enumerate()
.skip(start_line)
.take(end_line - start_line)
{
let i = offset;
let line_display = i + 1; // 1-based line numbers for display
if i == line_num {
// Highlight the current line
result.push_str(&format!("-> {}: {}\n", line_display, line_content));
// Add caret pointing to the column
let spaces = 3 + line_display.to_string().len() + 2 + self.column;
result.push_str(&format!("{}^\n", " ".repeat(spaces)));
} else {
result.push_str(&format!(" {}: {}\n", line_display, line_content));
}
}
result
}
/// Returns true if this location has complete information
pub fn is_complete(&self) -> bool {
self.module.is_some() && self.file_path.is_some() && self.source_text.is_some()
}
/// Create a new location that spans from this location to another
pub fn span_to(&self, other: &Location) -> Location {
let start_line = std::cmp::min(self.line, other.line);
let _end_line = std::cmp::max(self.line, other.line);
let start_col = if self.line == other.line {
std::cmp::min(self.column, other.column)
} else if self.line < other.line {
self.column
} else {
other.column
};
let length = if self.line == other.line {
(std::cmp::max(self.column + self.length, other.column + other.length))
.saturating_sub(start_col)
} else {
// Multi-line span - approximate
self.length + other.length
};
Location {
module: self.module.clone().or_else(|| other.module.clone()),
line: start_line,
column: start_col,
source_text: self
.source_text
.clone()
.or_else(|| other.source_text.clone()),
length,
file_path: self.file_path.clone().or_else(|| other.file_path.clone()),
}
}
}
impl fmt::Display for Location {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (line, column) = self.position();
match (&self.module, &self.file_path) {
(Some(module), Some(file_path)) => {
write!(f, "[{}@{}:{}:{}]", module, file_path, line, column)
}
(Some(module), None) => {
write!(f, "[{}@{}:{}]", module, line, column)
}
(None, Some(file_path)) => {
write!(f, "[{}:{}:{}]", file_path, line, column)
}
(None, None) => {
write!(f, "[{}:{}]", line, column)
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_value_type_can_assign() {
// Test exact matches
assert!(ValueType::String.can_assign(&ValueType::String));
assert!(ValueType::I32.can_assign(&ValueType::I32));
// Test signed integer compatibility
assert!(ValueType::Signed.can_assign(&ValueType::I8));
assert!(ValueType::Signed.can_assign(&ValueType::U8));
assert!(ValueType::I64.can_assign(&ValueType::I32));
// Test unsigned integer compatibility
assert!(ValueType::Unsigned.can_assign(&ValueType::U8));
assert!(ValueType::U64.can_assign(&ValueType::U32));
// Test float compatibility
assert!(ValueType::Float.can_assign(&ValueType::F32));
assert!(ValueType::F64.can_assign(&ValueType::Float));
// Test incompatible types
assert!(!ValueType::String.can_assign(&ValueType::I32));
assert!(!ValueType::I32.can_assign(&ValueType::String));
assert!(!ValueType::U64.can_assign(&ValueType::I64));
}
#[test]
fn test_value_type_categories() {
assert!(ValueType::I32.is_numeric());
assert!(ValueType::F64.is_numeric());
assert!(!ValueType::String.is_numeric());
assert!(ValueType::I32.is_integer());
assert!(!ValueType::F64.is_integer());
assert!(ValueType::F64.is_float());
assert!(!ValueType::I32.is_float());
assert!(ValueType::Array(vec![]).is_compound());
assert!(!ValueType::String.is_compound());
}
#[test]
fn test_location_span() {
let loc1 = Location::new(0, 5);
let loc2 = Location::new(0, 10);
let span = loc1.span_to(&loc2);
assert_eq!(span.line, 0);
assert_eq!(span.column, 5);
}
}