1extern crate alloc;
2use alloc::{ vec::Vec };
3use crate::parser::{Error, Position};
4use core::fmt::Debug;
5
6#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
7pub struct SimplePosition {
8 pub index: u32,
9 pub line: u32,
10 pub column: u32,
11}
12
13impl SimplePosition {
14 pub fn next(&self, c: char) -> Self {
15 let new_line = c == '\n';
16 Self {
17 index: self.index + 1,
18 line: if new_line { self.line + 1 } else { self.line },
19 column: if new_line { 0 } else { self.column + 1 },
20 }
21 }
22}
23
24impl Position for SimplePosition {
25 fn index(&self) -> u32 {
26 self.index
27 }
28
29 fn line(&self) -> u32 {
30 self.line
31 }
32
33 fn column(&self) -> u32 {
34 self.column
35 }
36}
37
38impl core::ops::Sub<Self> for SimplePosition {
39 type Output = i32;
40
41 fn sub(self, rhs: SimplePosition) -> Self::Output {
42 if self.index > rhs.index {
43 (self.index - rhs.index) as i32
44 } else {
45 -((rhs.index - self.index) as i32)
46 }
47 }
48}
49
50#[derive(Debug, PartialEq, Eq)]
51pub struct SimpleError {
52 pub reasons: Vec<(Option<SimplePosition>, &'static str)>,
53}
54
55impl Error for SimpleError {
56 type Position = SimplePosition;
57
58 fn reasons(&self) -> &[(Option<Self::Position>, &'static str)] {
59 &self.reasons[..]
60 }
61
62 fn add_reason(self, position: Option<Self::Position>, reason: &'static str) -> Self {
63 let mut reasons = self.reasons;
64 reasons.push((position, reason));
65 Self { reasons }
66 }
67
68 fn plain_str(reason: &'static str) -> Self {
69 let mut reasons = Vec::new();
70 reasons.push((None, reason));
71 SimpleError { reasons }
72 }
73}