surrealdb_core/syn/error/
mod.rs

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
use crate::syn::token::Span;
use std::fmt::Display;

mod location;
mod mac;
mod render;
pub use location::Location;
pub(crate) use mac::{bail, syntax_error};
pub use render::{RenderedError, Snippet};

#[derive(Debug, Clone, Copy)]
pub enum MessageKind {
	Suggestion,
	Error,
}

#[derive(Debug)]
enum DiagnosticKind {
	Cause(String),
	Span {
		kind: MessageKind,
		span: Span,
		label: Option<String>,
	},
}

#[derive(Debug)]
pub struct Diagnostic {
	kind: DiagnosticKind,
	next: Option<Box<Diagnostic>>,
}

/// A parsing error.
#[derive(Debug)]
pub struct SyntaxError {
	diagnostic: Box<Diagnostic>,
	data_pending: bool,
}

impl SyntaxError {
	/// Create a new parse error.
	pub fn new<T>(message: T) -> Self
	where
		T: Display,
	{
		let diagnostic = Diagnostic {
			kind: DiagnosticKind::Cause(message.to_string()),
			next: None,
		};

		Self {
			diagnostic: Box::new(diagnostic),
			data_pending: false,
		}
	}

	/// Returns whether this error is possibly the result of missing data.
	pub fn is_data_pending(&self) -> bool {
		self.data_pending
	}

	/// Indicate that this error might be the result of missing data and could be resolved with
	/// more data.
	pub fn with_data_pending(mut self) -> Self {
		self.data_pending = true;
		self
	}

	pub fn with_span(mut self, span: Span, kind: MessageKind) -> Self {
		self.diagnostic = Box::new(Diagnostic {
			kind: DiagnosticKind::Span {
				kind,
				span,
				label: None,
			},
			next: Some(self.diagnostic),
		});
		self
	}

	pub fn with_labeled_span<T: Display>(
		mut self,
		span: Span,
		kind: MessageKind,
		label: T,
	) -> Self {
		self.diagnostic = Box::new(Diagnostic {
			kind: DiagnosticKind::Span {
				kind,
				span,
				label: Some(label.to_string()),
			},
			next: Some(self.diagnostic),
		});
		self
	}

	pub fn with_cause<T: Display>(mut self, t: T) -> Self {
		self.diagnostic = Box::new(Diagnostic {
			kind: DiagnosticKind::Cause(t.to_string()),
			next: Some(self.diagnostic),
		});
		self
	}

	pub fn render_on(&self, source: &str) -> RenderedError {
		let mut res = RenderedError {
			errors: Vec::new(),
			snippets: Vec::new(),
		};
		Self::render_on_inner(&self.diagnostic, source, &mut res);
		res
	}

	pub fn render_on_bytes(&self, source: &[u8]) -> RenderedError {
		let source = String::from_utf8_lossy(source);
		self.render_on(&source)
	}

	fn render_on_inner(diagnostic: &Diagnostic, source: &str, res: &mut RenderedError) {
		if let Some(ref x) = diagnostic.next {
			Self::render_on_inner(x, source, res);
		}

		match diagnostic.kind {
			DiagnosticKind::Cause(ref x) => res.errors.push(x.clone()),
			DiagnosticKind::Span {
				ref span,
				ref label,
				ref kind,
			} => {
				let locations = Location::range_of_span(source, *span);
				let snippet = Snippet::from_source_location_range(
					source,
					locations,
					label.as_ref().map(|x| x.as_str()),
					*kind,
				);
				res.snippets.push(snippet)
			}
		}
	}
}