surrealdb/syn/
common.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
144
145
/// A human readable location inside a string.
///
/// Locations are 1 indexed, the first character on the first line being on line 1 column 1.
#[derive(Clone, Copy, Debug)]
pub struct Location {
	pub line: usize,
	/// In chars.
	pub column: usize,
}

impl Location {
	/// Returns the location of the start of substring in the larger input string.
	///
	/// Assumption: substr must be a subslice of input.
	pub fn of_in(substr: &str, input: &str) -> Self {
		// Bytes of input before substr.
		let offset = (substr.as_ptr() as usize)
			.checked_sub(input.as_ptr() as usize)
			.expect("tried to find location of substring in unrelated string");
		// Bytes of input prior to line being iteratated.
		let mut bytes_prior = 0;
		for (line_idx, line) in input.split('\n').enumerate() {
			// +1 for the '\n'
			let bytes_so_far = bytes_prior + line.len() + 1;
			if bytes_so_far > offset {
				// found line.
				let line_offset = offset - bytes_prior;
				let column = line[..line_offset].chars().count();
				// +1 because line and column are 1 index.
				return Self {
					line: line_idx + 1,
					column: column + 1,
				};
			}
			bytes_prior = bytes_so_far;
		}
		unreachable!()
	}

	#[cfg(feature = "experimental_parser")]
	pub fn of_span_start(source: &str, span: Span) -> Self {
		// Bytes of input before substr.
		let offset = span.offset as usize;
		// Bytes of input prior to line being iteratated.
		let mut bytes_prior = 0;
		for (line_idx, line) in source.split('\n').enumerate() {
			// +1 for the '\n'
			let bytes_so_far = bytes_prior + line.len() + 1;
			if bytes_so_far > offset {
				// found line.
				let line_offset = offset - bytes_prior;
				let column = line[..line_offset].chars().count();
				// +1 because line and column are 1 index.
				return Self {
					line: line_idx + 1,
					column: column + 1,
				};
			}
			bytes_prior = bytes_so_far;
		}
		unreachable!()
	}

	#[cfg(feature = "experimental_parser")]
	pub fn of_span_end(source: &str, span: Span) -> Self {
		// Bytes of input before substr.
		let offset = span.offset as usize + span.len as usize;
		// Bytes of input prior to line being iteratated.
		let mut bytes_prior = 0;
		for (line_idx, line) in source.split('\n').enumerate() {
			// +1 for the '\n'
			let bytes_so_far = bytes_prior + line.len() + 1;
			if bytes_so_far > offset {
				// found line.
				let line_offset = offset - bytes_prior;
				let column = line[..line_offset].chars().count();
				// +1 because line and column are 1 index.
				return Self {
					line: line_idx + 1,
					column: column + 1,
				};
			}
			bytes_prior = bytes_so_far;
		}
		unreachable!()
	}

	#[cfg(feature = "experimental_parser")]
	pub fn range_of_span(source: &str, span: Span) -> Range<Self> {
		// Bytes of input before substr.
		let offset = span.offset as usize;
		let end = offset + span.len as usize;

		// Bytes of input prior to line being iteratated.
		let mut bytes_prior = 0;
		let mut iterator = source.split('\n').enumerate();
		let start = loop {
			let Some((line_idx, line)) = iterator.next() else {
				panic!("tried to find location of span not belonging to string");
			};
			// +1 for the '\n'
			let bytes_so_far = bytes_prior + line.len() + 1;
			if bytes_so_far > offset {
				// found line.
				let line_offset = offset - bytes_prior;
				let column = line[..line_offset].chars().count();
				// +1 because line and column are 1 index.
				if bytes_so_far > end {
					// end is on the same line, finish immediatly.
					let line_offset = end - bytes_prior;
					let end_column = line[..line_offset].chars().count();
					return Self {
						line: line_idx + 1,
						column: column + 1,
					}..Self {
						line: line_idx + 1,
						column: end_column + 1,
					};
				} else {
					break Self {
						line: line_idx + 1,
						column: column + 1,
					};
				}
			}
			bytes_prior = bytes_so_far;
		};

		loop {
			let Some((line_idx, line)) = iterator.next() else {
				panic!("tried to find location of span not belonging to string");
			};
			// +1 for the '\n'
			let bytes_so_far = bytes_prior + line.len() + 1;
			if bytes_so_far > end {
				let line_offset = end - bytes_prior;
				let column = line[..line_offset].chars().count();
				return start..Self {
					line: line_idx + 1,
					column: column + 1,
				};
			}
		}
	}
}