forked from dtolnay/syn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prototype for support of custom keywords
First prototype for support of custom keywords with syn's new parsing API. Documentation and tests aren't present for now as I'm mainly reaching for feedback. This patch introduces a new Keyword trait, a new macro custom_keyword! and exposes the existing TokenMarker enum. The Keyword trait automatically implements the Token trait, making it possible to peek on custom keywords (this is why I had to make TokenMarker public). The custom macro generates a structure storing an Ident and implementing the Keyword and Parse traits. A function with the same name as the structure is also generated in order to use it like any predefined keyword.
- Loading branch information
Showing
4 changed files
with
90 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use buffer::Cursor; | ||
use token::Token; | ||
|
||
pub trait Keyword { | ||
fn ident() -> &'static str; | ||
|
||
fn display() -> &'static str; | ||
} | ||
|
||
impl<K: Keyword> Token for K { | ||
fn peek(cursor: Cursor) -> bool { | ||
if let Some((ident, _rest)) = cursor.ident() { | ||
ident == K::ident() | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
fn display() -> &'static str { | ||
K::display() | ||
} | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! custom_keyword { | ||
($ident:ident) => { | ||
custom_keyword_internal!({ pub(in self) } $ident); | ||
}; | ||
(pub $ident:ident) => { | ||
custom_keyword_internal!({ pub } $ident); | ||
}; | ||
(pub(crate) $ident:ident) => { | ||
custom_keyword_internal!({ pub(crate) } $ident); | ||
}; | ||
(pub(super) $ident:ident) => { | ||
custom_keyword_internal!({ pub(super) } $ident); | ||
}; | ||
(pub(self) $ident:ident) => { | ||
custom_keyword_internal!({ pub(self) } $ident); | ||
}; | ||
(pub(in $path:path) $ident:ident) => { | ||
custom_keyword_internal!({ pub(in $path) } $ident); | ||
}; | ||
} | ||
|
||
#[macro_export] | ||
#[doc(hidden)] | ||
macro_rules! custom_keyword_internal { | ||
({ $($vis:tt)* } $ident:ident) => { | ||
$($vis)* struct $ident { | ||
inner: $crate::Ident | ||
} | ||
|
||
impl $crate::parse::Keyword for $ident { | ||
fn ident() -> &'static str { | ||
stringify!($ident) | ||
} | ||
|
||
fn display() -> &'static str { | ||
concat!("`", stringify!($ident), "`") | ||
} | ||
} | ||
|
||
impl $crate::parse::Parse for $ident { | ||
fn parse(input: $crate::parse::ParseStream) -> $crate::parse::Result<$ident> { | ||
input.step(|cursor| { | ||
if let Some((ident, rest)) = cursor.ident() { | ||
if ident == stringify!($ident) { | ||
return Ok(($ident { inner: ident }, rest)); | ||
} | ||
} | ||
Err(cursor.error(concat!("expected `", stringify!($ident), "`"))) | ||
}) | ||
} | ||
} | ||
|
||
$($vis)* fn $ident(marker: $crate::parse::TokenMarker) -> $ident { | ||
match marker {} | ||
} | ||
} | ||
} |
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