-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
655 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,7 @@ mod tests; | |
|
||
mod error; | ||
mod parse; | ||
mod parse_extern; | ||
mod parse_fn; | ||
mod parse_impl; | ||
mod parse_mod; | ||
|
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,99 @@ | ||
use crate::parse_impl::parse_impl_body; | ||
use crate::parse_utils::{consume_ident, parse_any_ident, parse_ident, parse_punct, TokenIter}; | ||
use crate::{Attribute, ExternBlock, ExternCrate, VisMarker}; | ||
use proc_macro2::{Delimiter, TokenTree}; | ||
|
||
pub(crate) fn parse_extern_crate( | ||
tokens: &mut TokenIter, | ||
attributes: Vec<Attribute>, | ||
vis_marker: Option<VisMarker>, | ||
) -> ExternCrate { | ||
consume_extern_crate(tokens, attributes, vis_marker).expect("cannot parse extern crate") | ||
} | ||
|
||
fn consume_extern_crate( | ||
tokens: &mut TokenIter, | ||
attributes: Vec<Attribute>, | ||
vis_marker: Option<VisMarker>, | ||
) -> Option<ExternCrate> { | ||
let tk_extern = consume_ident(tokens, "extern")?; | ||
let tk_crate = consume_ident(tokens, "crate")?; | ||
|
||
let name = parse_any_ident(tokens, "extern crate"); | ||
let tk_as = consume_ident(tokens, "as"); | ||
|
||
let alias; | ||
let tk_underscore; | ||
if tk_as.is_some() { | ||
alias = Some(parse_any_ident(tokens, "extern crate: alias")); | ||
if alias.is_none() { | ||
tk_underscore = Some(parse_ident(tokens, "_", "extern crate")); | ||
} else { | ||
tk_underscore = None; | ||
} | ||
} else { | ||
alias = None; | ||
tk_underscore = None; | ||
} | ||
|
||
let tk_semicolon = parse_punct(tokens, ';', "extern crate"); | ||
|
||
Some(ExternCrate { | ||
attributes, | ||
vis_marker, | ||
tk_extern, | ||
tk_crate, | ||
name, | ||
tk_as, | ||
alias, | ||
tk_underscore, | ||
tk_semicolon, | ||
}) | ||
} | ||
|
||
pub(crate) fn parse_extern_block( | ||
tokens: &mut TokenIter, | ||
attributes: Vec<Attribute>, | ||
vis_marker: Option<VisMarker>, | ||
) -> ExternBlock { | ||
consume_extern_block(tokens, attributes, vis_marker).expect("cannot parse extern block") | ||
} | ||
|
||
fn consume_extern_block( | ||
tokens: &mut TokenIter, | ||
attributes: Vec<Attribute>, | ||
vis_marker: Option<VisMarker>, | ||
) -> Option<ExternBlock> { | ||
let tk_unsafe = consume_ident(tokens, "unsafe"); | ||
let tk_extern = consume_ident(tokens, "extern")?; | ||
|
||
let extern_abi = match tokens.peek() { | ||
Some(TokenTree::Literal(lit)) => { | ||
let lit = Some(lit.clone()); | ||
tokens.next(); | ||
lit | ||
} | ||
_ => None, | ||
}; | ||
|
||
let (tk_braces, inner_attributes, body_items) = match tokens.next() { | ||
Some(TokenTree::Group(group)) if group.delimiter() == Delimiter::Brace => { | ||
parse_impl_body(group, true) | ||
} | ||
_ => { | ||
// Only here we know that it's not an extern crate or extern block, so try other options on call-site (fn). | ||
return None; | ||
} | ||
}; | ||
|
||
Some(ExternBlock { | ||
attributes, | ||
vis_marker, | ||
tk_unsafe, | ||
tk_extern, | ||
extern_abi, | ||
tk_braces, | ||
inner_attributes, | ||
body_items, | ||
}) | ||
} |
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
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,22 @@ | ||
--- | ||
source: src/tests.rs | ||
expression: unsafe_extern | ||
--- | ||
ExternBlock( | ||
ExternBlock { | ||
attributes: [], | ||
vis_marker: None, | ||
tk_unsafe: Some( | ||
Ident( | ||
unsafe, | ||
), | ||
), | ||
tk_extern: Ident( | ||
extern, | ||
), | ||
extern_abi: None, | ||
tk_braces: {}, | ||
inner_attributes: [], | ||
body_items: [], | ||
}, | ||
) |
Oops, something went wrong.