-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed error_callback attr, added to error
`#[logos(error = SomeType)]` `#[logos(error_callback = callback)]` is now expressed as `#[logos(error(SomeType, callback))]`
- Loading branch information
1 parent
a0a358e
commit 582cfcc
Showing
5 changed files
with
130 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use proc_macro2::{Span, TokenStream}; | ||
use syn::spanned::Spanned; | ||
use syn::Ident; | ||
|
||
use crate::leaf::Callback; | ||
use crate::parser::nested::NestedValue; | ||
use crate::parser::Parser; | ||
use crate::util::MaybeVoid; | ||
|
||
pub struct ErrorType { | ||
pub ty: TokenStream, | ||
pub callback: Option<Callback>, | ||
} | ||
|
||
impl ErrorType { | ||
pub fn new(ty: TokenStream) -> Self { | ||
Self { ty, callback: None } | ||
} | ||
|
||
pub fn named_attr(&mut self, name: Ident, value: NestedValue, parser: &mut Parser) { | ||
match (name.to_string().as_str(), value) { | ||
("callback", NestedValue::Assign(tokens)) => { | ||
let span = tokens.span(); | ||
let callback = match parser.parse_callback(tokens) { | ||
Some(callback) => callback, | ||
None => { | ||
parser.err("Not a valid callback", span); | ||
return; | ||
} | ||
}; | ||
|
||
if let Some(previous) = self.callback.replace(callback) { | ||
parser | ||
.err( | ||
"Callback has been already set", | ||
span.join(name.span()).unwrap(), | ||
) | ||
.err("Previous callback set here", previous.span()); | ||
} | ||
} | ||
("callback", _) => { | ||
parser.err("Expected: callback = ...", name.span()); | ||
} | ||
(unknown, _) => { | ||
parser.err( | ||
format!( | ||
"\ | ||
Unknown nested attribute: {}\n\ | ||
\n\ | ||
Expected one of: callback\ | ||
", | ||
unknown | ||
), | ||
name.span(), | ||
); | ||
} | ||
} | ||
} | ||
|
||
pub fn unwrap(opt: Option<Self>) -> (MaybeVoid, Option<Callback>) { | ||
if let Some(Self { ty, callback }) = opt { | ||
(MaybeVoid::Some(ty), callback) | ||
} else { | ||
(MaybeVoid::Void, None) | ||
} | ||
} | ||
|
||
pub fn span(&self) -> Span { | ||
self.ty.span() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters