ass_core/tokenizer/state/issue.rs
1//! Tokenization issue records for error reporting.
2//!
3//! Defines [`TokenIssue`], a single diagnostic carrying location information
4//! and a severity [`IssueLevel`] for appropriate handling during lexical
5//! analysis.
6
7use super::level::IssueLevel;
8use alloc::{format, string::String};
9
10/// Tokenization issue for error reporting
11///
12/// Represents a problem encountered during tokenization with location
13/// information and severity level for appropriate handling.
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct TokenIssue<'a> {
16 /// Issue severity level
17 pub level: IssueLevel,
18
19 /// Human-readable error message
20 pub message: String,
21
22 /// Source span where issue occurred
23 pub span: &'a str,
24
25 /// Line number where issue occurred (1-based)
26 pub line: usize,
27
28 /// Column number where issue occurred (1-based)
29 pub column: usize,
30}
31
32impl<'a> TokenIssue<'a> {
33 /// Create new tokenization issue
34 ///
35 /// # Arguments
36 ///
37 /// * `level` - Severity level of the issue
38 /// * `message` - Human-readable description
39 /// * `span` - Source text span where issue occurred
40 /// * `line` - Line number (1-based)
41 /// * `column` - Column number (1-based)
42 #[must_use]
43 pub const fn new(
44 level: IssueLevel,
45 message: String,
46 span: &'a str,
47 line: usize,
48 column: usize,
49 ) -> Self {
50 Self {
51 level,
52 message,
53 span,
54 line,
55 column,
56 }
57 }
58
59 /// Create warning issue
60 #[must_use]
61 pub const fn warning(message: String, span: &'a str, line: usize, column: usize) -> Self {
62 Self::new(IssueLevel::Warning, message, span, line, column)
63 }
64
65 /// Create error issue
66 #[must_use]
67 pub const fn error(message: String, span: &'a str, line: usize, column: usize) -> Self {
68 Self::new(IssueLevel::Error, message, span, line, column)
69 }
70
71 /// Create critical issue
72 #[must_use]
73 pub const fn critical(message: String, span: &'a str, line: usize, column: usize) -> Self {
74 Self::new(IssueLevel::Critical, message, span, line, column)
75 }
76
77 /// Check if this is an error-level issue
78 #[must_use]
79 pub const fn is_error(&self) -> bool {
80 self.level.is_error()
81 }
82
83 /// Get formatted location string
84 #[must_use]
85 pub fn location_string(&self) -> String {
86 format!("{}:{}", self.line, self.column)
87 }
88
89 /// Get formatted issue string for display
90 #[must_use]
91 pub fn format_issue(&self) -> String {
92 format!(
93 "{}: {} at {}:{}",
94 self.level.as_str(),
95 self.message,
96 self.line,
97 self.column
98 )
99 }
100}