///|
/// Source location in a CUE document (1-based line and column, 0-based char offset).
#derive(Eq, Debug)
pub(all) struct Position {
offset : Int32
line : Int32
column : Int32
}
///|
pub fn Position::new(offset : Int32, line : Int32, column : Int32) -> Position {
{ offset, line, column, }
}
///|
pub fn Position::zero() -> Position {
{ offset: 0, line: 1, column: 1, }
}
///|
pub impl Show for Position with fn output(self, logger) {
logger.write_string("\{self.line}:\{self.column}")
}
///|
/// Half-open source span `[start, end)`.
#derive(Eq, Debug)
pub(all) struct Span {
start : Position
end : Position
}
///|
pub fn Span::new(start : Position, end : Position) -> Span {
{ start, end, }
}
///|
pub fn Span::zero() -> Span {
{ start: Position::zero(), end: Position::zero(), }
}
///|
pub fn Span::merge(self : Span, other : Span) -> Span {
let start = if self.start.offset <= other.start.offset {
self.start
} else {
other.start
}
let end = if self.end.offset >= other.end.offset {
self.end
} else {
other.end
}
{ start, end, }
}
///|
pub impl Show for Span with fn output(self, logger) {
logger.write_string("\{self.start}-\{self.end}")
}
///|
/// Category of a CUE diagnostic error.
#derive(Eq, Debug)
pub(all) enum ErrorKind {
LexError
ParseError
EvalError
ConflictError
ClosedStructError
IncompleteError
CycleError
BoundError
RegexError
JsonError
YamlError
ExplicitBottom
}
///|
pub impl Show for ErrorKind with fn output(self, logger) {
let s = match self {
LexError => "lex error"
ParseError => "parse error"
EvalError => "eval error"
ConflictError => "conflict"
ClosedStructError => "closed struct"
IncompleteError => "incomplete"
CycleError => "cycle"
BoundError => "bound error"
RegexError => "regex error"
JsonError => "json error"
YamlError => "yaml error"
ExplicitBottom => "explicit bottom"
}
logger.write_string(s)
}
///|
/// Structured CUE diagnostic carrying kind, message, field path, and optional source span.
#derive(Eq, Debug)
pub(all) suberror CueError {
CueError(
kind~ : ErrorKind,
message~ : String,
path~ : Array[String],
span~ : Span?,
)
}
///|
pub fn CueError::new(
kind : ErrorKind,
message : String,
path~ : Array[String] = [],
span~ : Span? = None,
) -> CueError {
CueError(kind~, message~, path~, span~)
}
///|
pub fn CueError::with_path_prefix(
self : CueError,
segment : String,
) -> CueError {
match self {
CueError(kind~, message~, path~, span~) => {
let next : Array[String] = [segment]
for seg in path {
next.push(seg)
}
CueError(kind~, message~, path=next, span~)
}
}
}
///|
pub fn CueError::format_path(path : Array[String]) -> String {
if path.is_empty() {
return ""
}
let buf = StringBuilder()
for i = 0; i < path.length(); i = i + 1 {
let seg = path[i]
if i > 0 && !seg.has_prefix("[") {
buf.write_char('.')
}
buf.write_string(seg)
}
buf.to_string()
}
///|
pub impl Show for CueError with fn output(self, logger) {
match self {
CueError(kind~, message~, path~, span~) => {
if !path.is_empty() {
logger.write_string(CueError::format_path(path))
logger.write_string(": ")
}
logger.write_string("\{kind}: \{message}")
match span {
Some(sp) => logger.write_string(" (at \{sp.start})")
None => ()
}
}
}
}
///|
pub fn CueError::to_string(self : CueError) -> String {
Show::to_string(self)
}