Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2f20164

Browse files
committed
Return error in the parsers
1 parent ab2cd7f commit 2f20164

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1214
-720
lines changed

src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::lexer::errors::LexerError;
77
use crate::lexer::lexer::Location;
88
use crate::lexer::source::FileId;
99

10-
#[derive(Debug, Clone, Copy)]
10+
#[derive(Debug, Clone, Copy, Default)]
1111
pub struct Span {
1212
pub file: Option<FileId>,
1313
pub start: Location,

src/lexer/extra.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// http://opensource.org/licenses/MIT>, at your option. This file may not be
44
// copied, modified, or distributed except according to those terms.
55

6+
use crate::errors::Span;
67
use crate::lexer::{TLexer, Token};
78

89
#[derive(Clone, Debug)]
@@ -20,6 +21,10 @@ impl TLexer for SavedLexer {
2021
Token::Eof
2122
}
2223
}
24+
25+
fn span(&self) -> Span {
26+
Span::default()
27+
}
2328
}
2429

2530
impl SavedLexer {
@@ -54,6 +59,14 @@ impl<'l1, 'l2> TLexer for CombinedLexers<'l1, 'l2> {
5459
eprintln!("TOK: {:?}", tok);
5560
tok
5661
}
62+
63+
fn span(&self) -> Span {
64+
if self.state {
65+
Span::default()
66+
} else {
67+
self.second.span()
68+
}
69+
}
5770
}
5871

5972
impl<'l1, 'l2> CombinedLexers<'l1, 'l2> {

src/lexer/lexer.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ pub enum Token {
470470
MSUnaligned,
471471
}
472472

473-
#[derive(Clone, Debug, Copy)]
473+
#[derive(Clone, Debug, Copy, Default)]
474474
pub struct Location {
475475
pub pos: usize,
476476
pub line: u32,
@@ -536,6 +536,8 @@ pub trait TLexer {
536536
stole.push(tok);
537537
}
538538
}
539+
540+
fn span(&self) -> Span;
539541
}
540542

541543
pub struct Lexer<'a, PC: PreprocContext> {
@@ -558,6 +560,14 @@ impl<'a, PC: PreprocContext> TLexer for Lexer<'a, PC> {
558560
}
559561
}
560562
}
563+
564+
fn span(&self) -> Span {
565+
Span {
566+
file: self.buf.get_source_id(),
567+
start: self.start,
568+
end: self.location(),
569+
}
570+
}
561571
}
562572

563573
macro_rules! get_operator {
@@ -1039,14 +1049,6 @@ impl<'a, PC: PreprocContext> Lexer<'a, PC> {
10391049
}
10401050
}
10411051

1042-
pub(super) fn span(&self) -> Span {
1043-
Span {
1044-
file: self.buf.get_source_id(),
1045-
start: self.start,
1046-
end: self.location(),
1047-
}
1048-
}
1049-
10501052
pub fn next_token(&mut self) -> Token {
10511053
loop {
10521054
self.start = self.location();

src/lexer/preprocessor/include.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::path::{Path, PathBuf};
99

1010
use crate::lexer::buffer::BufferData;
1111
use crate::lexer::errors::LexerError;
12-
use crate::lexer::lexer::Lexer;
12+
use crate::lexer::lexer::{Lexer, TLexer};
1313
use crate::lexer::preprocessor::PreprocContext;
1414
use crate::lexer::source::{FileId, SourceMutex};
1515

src/lexer/preprocessor/preprocessor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use super::context::{IfKind, IfState, PreprocContext};
1010
use super::macros::{Action, Macro, MacroFunction, MacroObject, MacroType};
1111
use crate::lexer::buffer::FileInfo;
1212
use crate::lexer::errors::LexerError;
13-
use crate::lexer::lexer::{Lexer, Token};
13+
use crate::lexer::lexer::{Lexer, TLexer, Token};
1414
use crate::lexer::string::StringType;
1515

1616
#[derive(Clone, Debug, Copy, PartialEq, PartialOrd)]

src/parser/attributes.rs

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use termcolor::StandardStreamLock;
88

99
use crate::lexer::{TLexer, Token};
1010
use crate::parser::dump::Dump;
11+
use crate::parser::errors::ParserError;
1112
use crate::parser::Context;
12-
use crate::{dump_fields, dump_start};
1313

1414
#[derive(Clone, Debug, PartialEq)]
1515
pub struct Attribute {
@@ -66,7 +66,7 @@ impl<'a, L: TLexer> UsingParser<'a, L> {
6666
Self { lexer }
6767
}
6868

69-
fn parse(self, _context: &mut Context) -> (Option<Token>, Option<String>) {
69+
fn parse(self, _context: &mut Context) -> Result<(Option<Token>, Option<String>), ParserError> {
7070
let tok = self.lexer.next_useful();
7171
if tok == Token::Using {
7272
let tok = self.lexer.next_useful();
@@ -75,17 +75,23 @@ impl<'a, L: TLexer> UsingParser<'a, L> {
7575
let tok = self.lexer.next_useful();
7676
match tok {
7777
Token::Colon => {
78-
return (None, ns);
78+
return Ok((None, ns));
7979
}
8080
_ => {
81-
unreachable!("Invalid token in attributes: {:?}", tok);
81+
return Err(ParserError::InvalidTokenInAttrs {
82+
sp: self.lexer.span(),
83+
tok,
84+
});
8285
}
8386
}
8487
} else {
85-
unreachable!("Invalid token in attributes: {:?}", tok);
88+
return Err(ParserError::InvalidTokenInAttrs {
89+
sp: self.lexer.span(),
90+
tok,
91+
});
8692
}
8793
}
88-
(Some(tok), None)
94+
Ok((Some(tok), None))
8995
}
9096
}
9197

@@ -102,10 +108,10 @@ impl<'a, L: TLexer> ArgumentParser<'a, L> {
102108
self,
103109
tok: Option<Token>,
104110
_context: &mut Context,
105-
) -> (Option<Token>, Option<AttributeArg>) {
111+
) -> Result<(Option<Token>, Option<AttributeArg>), ParserError> {
106112
let tok = tok.unwrap_or_else(|| self.lexer.next_useful());
107113
if tok != Token::LeftParen {
108-
return (Some(tok), None);
114+
return Ok((Some(tok), None));
109115
}
110116

111117
let mut arg = AttributeArg::default();
@@ -122,9 +128,12 @@ impl<'a, L: TLexer> ArgumentParser<'a, L> {
122128
Token::RightParen => {
123129
if paren_count == 1 {
124130
if brack_count != 0 || brace_count != 0 {
125-
unreachable!("Unbalanced attribute");
131+
return Err(ParserError::UnbalancedAttr {
132+
sp: self.lexer.span(),
133+
tok,
134+
});
126135
} else {
127-
return (None, Some(arg));
136+
return Ok((None, Some(arg)));
128137
}
129138
} else {
130139
paren_count -= 1;
@@ -135,7 +144,10 @@ impl<'a, L: TLexer> ArgumentParser<'a, L> {
135144
}
136145
Token::RightBrack => {
137146
if brack_count == 0 {
138-
unreachable!("Unbalanced attribute");
147+
return Err(ParserError::UnbalancedAttr {
148+
sp: self.lexer.span(),
149+
tok,
150+
});
139151
} else {
140152
brack_count -= 1;
141153
}
@@ -145,13 +157,18 @@ impl<'a, L: TLexer> ArgumentParser<'a, L> {
145157
}
146158
Token::RightBrace => {
147159
if brace_count == 0 {
148-
unreachable!("Unbalanced attribute");
160+
return Err(ParserError::UnbalancedAttr {
161+
sp: self.lexer.span(),
162+
tok,
163+
});
149164
} else {
150165
brace_count -= 1;
151166
}
152167
}
153168
Token::Eof => {
154-
unreachable!("Wrong attribute");
169+
return Err(ParserError::UnexpectedEof {
170+
sp: self.lexer.span(),
171+
});
155172
}
156173
t => {
157174
arg.tokens.push(t);
@@ -174,25 +191,29 @@ impl<'a, L: TLexer> NameParser<'a, L> {
174191
self,
175192
tok: Token,
176193
_context: &mut Context,
177-
) -> (Option<Token>, (Option<String>, String)) {
194+
) -> Result<(Option<Token>, (Option<String>, String)), ParserError> {
178195
match tok {
179196
Token::Identifier(id) => {
180197
let tk = self.lexer.next_useful();
181198
if tk == Token::ColonColon {
182199
let ns = Some(id);
183200
let tk = self.lexer.next_useful();
184201
if let Token::Identifier(id) = tk {
185-
(None, (ns, id))
202+
Ok((None, (ns, id)))
186203
} else {
187-
unreachable!("Invalid token in attributes: {:?}", tk);
204+
Err(ParserError::InvalidTokenInAttrs {
205+
sp: self.lexer.span(),
206+
tok: tk,
207+
})
188208
}
189209
} else {
190-
(Some(tk), (None, id))
210+
Ok((Some(tk), (None, id)))
191211
}
192212
}
193-
_ => {
194-
unreachable!("Invalid token in attributes: {:?}", tok);
195-
}
213+
_ => Err(ParserError::InvalidTokenInAttrs {
214+
sp: self.lexer.span(),
215+
tok,
216+
}),
196217
}
197218
}
198219
}
@@ -211,7 +232,7 @@ impl<'a, L: TLexer> AttributeParser<'a, L> {
211232
attributes: &mut Attributes,
212233
tok: Option<Token>,
213234
context: &mut Context,
214-
) -> (Option<Token>, bool) {
235+
) -> Result<(Option<Token>, bool), ParserError> {
215236
// [[ attribute-list ]]
216237
// [[ using attribute-namespace : attribute-list ]]
217238
//
@@ -223,21 +244,21 @@ impl<'a, L: TLexer> AttributeParser<'a, L> {
223244

224245
let tok = tok.unwrap_or_else(|| self.lexer.next_useful());
225246
if tok != Token::DoubleLeftBrack {
226-
return (Some(tok), false);
247+
return Ok((Some(tok), false));
227248
}
228249

229250
let up = UsingParser::new(self.lexer);
230-
let (tok, default_ns) = up.parse(context);
251+
let (tok, default_ns) = up.parse(context)?;
231252
let has_using = default_ns.is_some();
232253

233254
let mut tok = tok.unwrap_or_else(|| self.lexer.next_useful());
234255

235256
loop {
236257
let np = NameParser::new(self.lexer);
237-
let (tk, (namespace, id)) = np.parse(tok, context);
258+
let (tk, (namespace, id)) = np.parse(tok, context)?;
238259

239260
let ap = ArgumentParser::new(self.lexer);
240-
let (tk, arg) = ap.parse(tk, context);
261+
let (tk, arg) = ap.parse(tk, context)?;
241262

242263
attributes.push(Attribute {
243264
namespace: namespace.or_else(|| default_ns.clone()),
@@ -250,10 +271,13 @@ impl<'a, L: TLexer> AttributeParser<'a, L> {
250271
match tok {
251272
Token::Comma => {}
252273
Token::DoubleRightBrack => {
253-
return (None, true);
274+
return Ok((None, true));
254275
}
255276
_ => {
256-
unreachable!("Invalid token in attributes: {:?}", tok);
277+
return Err(ParserError::InvalidTokenInAttrs {
278+
sp: self.lexer.span(),
279+
tok,
280+
});
257281
}
258282
}
259283

@@ -275,14 +299,14 @@ impl<'a, L: TLexer> AttributesParser<'a, L> {
275299
self,
276300
tok: Option<Token>,
277301
context: &mut Context,
278-
) -> (Option<Token>, Option<Attributes>) {
302+
) -> Result<(Option<Token>, Option<Attributes>), ParserError> {
279303
let mut attributes = Vec::new();
280304
let mut tok = tok;
281305
let mut has_attributes = false;
282306

283307
loop {
284308
let ap = AttributeParser::new(self.lexer);
285-
let (tk, has_attr) = ap.parse(&mut attributes, tok, context);
309+
let (tk, has_attr) = ap.parse(&mut attributes, tok, context)?;
286310
tok = tk;
287311
has_attributes |= has_attr;
288312

@@ -291,11 +315,11 @@ impl<'a, L: TLexer> AttributesParser<'a, L> {
291315
}
292316
}
293317

294-
if has_attributes {
318+
Ok(if has_attributes {
295319
(tok, Some(attributes))
296320
} else {
297321
(tok, None)
298-
}
322+
})
299323
}
300324
}
301325

@@ -311,7 +335,7 @@ mod tests {
311335
let mut l = Lexer::<DefaultContext>::new(b"[[noreturn]]");
312336
let p = AttributesParser::new(&mut l);
313337
let mut context = Context::default();
314-
let (_, a) = p.parse(None, &mut context);
338+
let (_, a) = p.parse(None, &mut context).unwrap();
315339

316340
assert_eq!(
317341
a.unwrap(),
@@ -329,7 +353,7 @@ mod tests {
329353
let mut l = Lexer::<DefaultContext>::new(b"[[gnu::unused]]");
330354
let p = AttributesParser::new(&mut l);
331355
let mut context = Context::default();
332-
let (_, a) = p.parse(None, &mut context);
356+
let (_, a) = p.parse(None, &mut context).unwrap();
333357

334358
assert_eq!(
335359
a.unwrap(),
@@ -347,7 +371,7 @@ mod tests {
347371
let mut l = Lexer::<DefaultContext>::new(b"[[deprecated(\"because\")]]");
348372
let p = AttributesParser::new(&mut l);
349373
let mut context = Context::default();
350-
let (_, a) = p.parse(None, &mut context);
374+
let (_, a) = p.parse(None, &mut context).unwrap();
351375

352376
assert_eq!(
353377
a.unwrap(),
@@ -367,7 +391,7 @@ mod tests {
367391
let mut l = Lexer::<DefaultContext>::new(b"[[using CC: opt(1), debug]]");
368392
let p = AttributesParser::new(&mut l);
369393
let mut context = Context::default();
370-
let (_, a) = p.parse(None, &mut context);
394+
let (_, a) = p.parse(None, &mut context).unwrap();
371395

372396
assert_eq!(
373397
a.unwrap(),
@@ -395,7 +419,7 @@ mod tests {
395419
let mut l = Lexer::<DefaultContext>::new(b"[[A]] [[B]] [[C]]");
396420
let p = AttributesParser::new(&mut l);
397421
let mut context = Context::default();
398-
let (_, a) = p.parse(None, &mut context);
422+
let (_, a) = p.parse(None, &mut context).unwrap();
399423

400424
assert_eq!(
401425
a.unwrap(),

0 commit comments

Comments
 (0)