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.
- Loading branch information
1 parent
0a169d4
commit b49a4b4
Showing
8 changed files
with
1,773 additions
and
0 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,17 @@ | ||
[package] | ||
name = "unstable" | ||
version = "0.1.0" | ||
authors = ["Sergio Benitez <sb@sergio.bz>"] | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[[bin]] | ||
name = "unstable" | ||
path = "src/main.rs" | ||
|
||
[dependencies] | ||
syn = { path = "../../", features = ["full", "unstable"] } | ||
|
||
[patch.crates-io] | ||
proc-macro2 = { path = "../../../proc-macro2" } |
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,66 @@ | ||
#![feature(proc_macro)] | ||
|
||
extern crate syn; | ||
extern crate proc_macro; | ||
|
||
use proc_macro::{TokenStream, Diagnostic}; | ||
|
||
use syn::*; | ||
use syn::spanned::Spanned; | ||
use syn::synom::{Synom, Cursor, SynomBuffer}; | ||
|
||
struct Parser { | ||
buffer: Box<SynomBuffer>, | ||
cursor: Cursor<'static> | ||
} | ||
|
||
impl Parser { | ||
fn new(tokens: TokenStream) -> Parser { | ||
let buffer = Box::new(SynomBuffer::new(tokens.into())); | ||
let cursor = unsafe { | ||
let buffer: &'static SynomBuffer = ::std::mem::transmute(&*buffer); | ||
buffer.begin() | ||
}; | ||
|
||
Parser { | ||
buffer: buffer, | ||
cursor: cursor | ||
} | ||
} | ||
|
||
fn parse<T: Synom>(&mut self) -> Result<T, Diagnostic> { | ||
let (cursor, val) = T::parse(self.cursor).map_err(|e| e.into())?; | ||
self.cursor = cursor; | ||
Ok(val) | ||
} | ||
} | ||
|
||
fn eval(input: TokenStream) -> Result<TokenStream, Diagnostic> { | ||
let mut parser = Parser::new(input); | ||
|
||
let a = parser.parse::<ExprTuple>()?; | ||
parser.parse::<token::Eq>()?; | ||
let b = parser.parse::<ExprTuple>()?; | ||
|
||
let (a_len, b_len) = (a.args.len(), b.args.len()); | ||
if a_len != b_len { | ||
let diag = b.span().expect("b's span") | ||
.error(format!("expected {} element(s), got {}", a_len, b_len)) | ||
.span_note(a.span().expect("a's span"), "because of this"); | ||
|
||
return Err(diag); | ||
} | ||
|
||
Ok("println!(\"All good!\")".parse().unwrap()) | ||
} | ||
|
||
#[proc_macro] | ||
pub fn demo(input: TokenStream) -> TokenStream { | ||
match eval(input) { | ||
Ok(val) => val, | ||
Err(diag) => { | ||
diag.emit(); | ||
"".parse().unwrap() | ||
} | ||
} | ||
} |
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,7 @@ | ||
#[macro_use] | ||
extern crate unstable; | ||
|
||
pub fn main() { | ||
demo!((a, b) = (1, 2, 3)); | ||
} | ||
|
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
Oops, something went wrong.