1
1
use std:: { borrow:: Cow , num:: ParseIntError , ops:: Range } ;
2
2
3
3
use oxcr_protocol:: {
4
- aott,
4
+ aott:: {
5
+ self ,
6
+ text:: { self , CharError } ,
7
+ } ,
5
8
miette:: { self , SourceSpan } ,
6
9
ser:: any_of,
7
10
thiserror,
@@ -11,7 +14,7 @@ use oxcr_protocol::{
11
14
#[ derive( miette:: Diagnostic , thiserror:: Error , Debug ) ]
12
15
pub enum ParseError {
13
16
#[ error( "expected {expected}, found {found}" ) ]
14
- #[ diagnostic( code( cli :: expected) ) ]
17
+ #[ diagnostic( code( aott :: error :: expected) ) ]
15
18
Expected {
16
19
expected : Expectation ,
17
20
found : char ,
@@ -22,7 +25,7 @@ pub enum ParseError {
22
25
} ,
23
26
#[ error( "unexpected end of input{}" , . expected. as_ref( ) . map( |expectation| format!( ", expected {expectation}" ) ) . unwrap_or_else( String :: new) ) ]
24
27
#[ diagnostic(
25
- code( cli :: unexpected_eof) ,
28
+ code( aott :: error :: unexpected_eof) ,
26
29
help( "try giving it more input next time, I guess?" )
27
30
) ]
28
31
UnexpectedEof {
@@ -58,6 +61,36 @@ pub enum ParseError {
58
61
#[ source]
59
62
error : ParseIntError ,
60
63
} ,
64
+ #[ error( "filter failed in {location}, while checking {token}" ) ]
65
+ #[ diagnostic( code( aott:: error:: filter_failed) ) ]
66
+ FilterFailed {
67
+ #[ label = "this is the token that didn't pass" ]
68
+ at : SourceSpan ,
69
+ location : & ' static core:: panic:: Location < ' static > ,
70
+ token : char ,
71
+ } ,
72
+ #[ error( "expected keyword {keyword}" ) ]
73
+ #[ diagnostic( code( aott:: text:: error:: expected_keyword) ) ]
74
+ ExpectedKeyword {
75
+ #[ label = "the keyword here is {found}" ]
76
+ at : SourceSpan ,
77
+ keyword : String ,
78
+ found : String ,
79
+ } ,
80
+ #[ error( "expected digit of radix {radix}" ) ]
81
+ #[ diagnostic( code( aott:: text:: error:: expected_digit) ) ]
82
+ ExpectedDigit {
83
+ #[ label = "here" ]
84
+ at : SourceSpan ,
85
+ radix : u32 ,
86
+ found : char ,
87
+ } ,
88
+ #[ error( "expected identifier character (a-zA-Z or _), but found {found}" ) ]
89
+ ExpectedIdent {
90
+ #[ label = "here" ]
91
+ at : SourceSpan ,
92
+ found : char ,
93
+ } ,
61
94
}
62
95
63
96
#[ derive( Debug , thiserror:: Error ) ]
@@ -73,10 +106,8 @@ pub enum Expectation {
73
106
}
74
107
75
108
impl < ' a > aott:: error:: Error < & ' a str > for ParseError {
76
- type Span = Range < usize > ;
77
-
78
109
fn unexpected_eof (
79
- span : Self :: Span ,
110
+ span : Range < usize > ,
80
111
expected : Option < Vec < <& ' a str as aott:: prelude:: InputType >:: Token > > ,
81
112
) -> Self {
82
113
Self :: UnexpectedEof {
@@ -86,25 +117,62 @@ impl<'a> aott::error::Error<&'a str> for ParseError {
86
117
}
87
118
}
88
119
89
- fn expected_token_found (
90
- span : Self :: Span ,
91
- expected : Vec < char > ,
92
- found : aott:: MaybeRef < ' _ , char > ,
93
- ) -> Self {
120
+ fn expected_token_found ( span : Range < usize > , expected : Vec < char > , found : char ) -> Self {
94
121
Self :: Expected {
95
122
expected : Expectation :: AnyOf ( expected) ,
96
- found : found . into_clone ( ) ,
123
+ found,
97
124
at : span. into ( ) ,
98
125
help : None ,
99
126
}
100
127
}
101
128
102
- fn expected_eof_found ( span : Self :: Span , found : aott :: MaybeRef < ' _ , char > ) -> Self {
129
+ fn expected_eof_found ( span : Range < usize > , found : char ) -> Self {
103
130
Self :: Expected {
104
131
expected : Expectation :: EndOfInput ,
105
- found : found . into_clone ( ) ,
132
+ found,
106
133
at : span. into ( ) ,
107
134
help : None ,
108
135
}
109
136
}
137
+
138
+ fn filter_failed (
139
+ span : Range < usize > ,
140
+ location : & ' static core:: panic:: Location < ' static > ,
141
+ token : <& ' a str as aott:: prelude:: InputType >:: Token ,
142
+ ) -> Self {
143
+ Self :: FilterFailed {
144
+ at : span. into ( ) ,
145
+ location,
146
+ token,
147
+ }
148
+ }
149
+ }
150
+
151
+ impl CharError < char > for ParseError {
152
+ fn expected_digit ( span : Range < usize > , radix : u32 , got : char ) -> Self {
153
+ Self :: ExpectedDigit {
154
+ at : span. into ( ) ,
155
+ radix,
156
+ found : got,
157
+ }
158
+ }
159
+
160
+ fn expected_ident_char ( span : Range < usize > , got : char ) -> Self {
161
+ Self :: ExpectedIdent {
162
+ at : span. into ( ) ,
163
+ found : got,
164
+ }
165
+ }
166
+
167
+ fn expected_keyword < ' a , ' b : ' a > (
168
+ span : Range < usize > ,
169
+ keyword : & ' b <char as text:: Char >:: Str ,
170
+ actual : & ' a <char as text:: Char >:: Str ,
171
+ ) -> Self {
172
+ Self :: ExpectedKeyword {
173
+ at : span. into ( ) ,
174
+ keyword : keyword. to_owned ( ) ,
175
+ found : actual. to_owned ( ) ,
176
+ }
177
+ }
110
178
}
0 commit comments