This repository has been archived by the owner on Jul 29, 2024. It is now read-only.
-
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
fc9e0a5
commit 0f37868
Showing
18 changed files
with
98 additions
and
101 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 |
---|---|---|
@@ -1,45 +1,44 @@ | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn; | ||
|
||
/// Auto-implement `FromStr` for AST node | ||
/// | ||
/// AST node must implement `Parse` trait | ||
#[proc_macro_derive(AST)] | ||
pub fn ast_macro_derive(input: TokenStream) -> TokenStream { | ||
let ast = syn::parse(input).unwrap(); | ||
let ast = syn::parse(input).unwrap(); | ||
|
||
impl_ast_macro(&ast) | ||
impl_ast_macro(&ast) | ||
} | ||
|
||
fn impl_ast_macro(ast: &syn::DeriveInput) -> TokenStream { | ||
let name = &ast.ident; | ||
let gen = quote! { | ||
impl std::str::FromStr for #name { | ||
type Err = <#name as Parse>::Err; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
let mut context = crate::syntax::Context::new( | ||
crate::syntax::FullSourceLexer::new(s) | ||
); | ||
|
||
context.lexer.skip_spaces(); | ||
|
||
let res = #name::parse(&mut context); | ||
|
||
let token = context.lexer.skip_spaces().next(); | ||
if token.is_some() { | ||
return Err( | ||
crate::syntax::error::ExtraToken { | ||
token: token.unwrap(), | ||
at: context.lexer.span().into() | ||
}.into() | ||
); | ||
} | ||
res | ||
} | ||
} | ||
}; | ||
|
||
gen.into() | ||
} | ||
let name = &ast.ident; | ||
let gen = quote! { | ||
impl std::str::FromStr for #name { | ||
type Err = <#name as Parse>::Err; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
let mut context = crate::syntax::Context::new( | ||
crate::syntax::FullSourceLexer::new(s) | ||
); | ||
|
||
context.lexer.skip_spaces(); | ||
|
||
let res = #name::parse(&mut context); | ||
|
||
let token = context.lexer.skip_spaces().next(); | ||
if token.is_some() { | ||
return Err( | ||
crate::syntax::error::ExtraToken { | ||
token: token.unwrap(), | ||
at: context.lexer.span().into() | ||
}.into() | ||
); | ||
} | ||
res | ||
} | ||
} | ||
}; | ||
|
||
gen.into() | ||
} |
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
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
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
Oops, something went wrong.