Skip to content

Commit

Permalink
convert proc macro to "#[proc_macro]"/"proc macros 1.2"
Browse files Browse the repository at this point in the history
  • Loading branch information
chpio committed Mar 9, 2017
1 parent 1e0965f commit 72a9b51
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 69 deletions.
4 changes: 2 additions & 2 deletions vtree_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ syn = "0.11.6"
synom = "0.11.0"

[lib]
crate-type = ["dylib"]
plugin = true
proc-macro = true
crate-type = ["proc-macro"]
14 changes: 3 additions & 11 deletions vtree_macros/src/generator.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use syntax::parse::{ParseSess, filemap_to_tts};
use syntax::tokenstream::TokenStream;
use regex::{Regex, Captures};
use syn::Ident;
use quote::Tokens;
use NodeChildType;
use ParsedData;
use Node;
use parser::{ParsedData, NodeChildType, Node};
use std::iter::once;

fn to_snake_case(s: &str) -> String {
Expand Down Expand Up @@ -552,7 +548,7 @@ fn gen_all_nodes_from_group_impls<'a>(pd: &'a ParsedData) -> impl Iterator<Item
})
}

pub fn generate_defs(pd: ParsedData) -> TokenStream {
pub fn generate_defs(pd: ParsedData) -> String {
let all_nodes_ident = Ident::new("AllNodes");
let node_defs = gen_node_defs(&pd);
let group_defs = pd.group_name_to_nodes
Expand All @@ -575,9 +571,5 @@ pub fn generate_defs(pd: ParsedData) -> TokenStream {
#(#all_nodes_from_group_impls)*
};
println!("{}", defs);
let source_str = defs.as_str();
let sess = ParseSess::new();
let filemap =
sess.codemap().new_filemap("<procmacro_lex>".to_string(), None, source_str.to_owned());
filemap_to_tts(&sess, filemap).into_iter().collect()
defs.into_string()
}
61 changes: 9 additions & 52 deletions vtree_macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,71 +1,28 @@
#![feature(plugin_registrar, rustc_private)]
#![feature(plugin_registrar)]
#![feature(conservative_impl_trait)]
#![feature(proc_macro)]
#![recursion_limit = "128"]

#[macro_use]
extern crate quote;
extern crate syntax;
extern crate syntax_pos;
extern crate rustc_plugin;
#[macro_use]
extern crate lazy_static;
extern crate regex;
extern crate syn;
#[macro_use]
extern crate synom;
extern crate proc_macro;

mod parser;
mod generator;

use parser::parse_nodes;
use generator::generate_defs;
use proc_macro::TokenStream;

use syntax::ext::base::{ExtCtxt, ProcMacro};
use syntax::symbol::Symbol;
use syntax_pos::Span;
use rustc_plugin::Registry;
use syntax::ext::base::SyntaxExtension;
use syntax::tokenstream::TokenStream;
use std::collections::HashMap;

#[derive(Debug, Clone)]
pub enum NodeChildType {
Single,
Optional,
Multi,
}

#[derive(Debug, Clone)]
pub struct NodeChild {
name: syn::Ident,
group: syn::Ident,
child_type: NodeChildType,
}

#[derive(Debug, Clone)]
pub struct Node {
name: syn::Ident,
params_type: Option<syn::Path>,
fields: Vec<NodeChild>,
}

#[derive(Debug, Clone)]
pub struct ParsedData {
nodes: Vec<Node>,
group_name_to_nodes: HashMap<syn::Ident, Vec<Node>>,
}

struct MacroDefineNodes;
impl ProcMacro for MacroDefineNodes {
fn expand<'ctx>(&self, _: &'ctx mut ExtCtxt, _: Span, ts: TokenStream) -> TokenStream {
let input = ts.to_string();
let pd = parse_nodes(&input);
generate_defs(pd)
}
}

#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
reg.register_syntax_extension(Symbol::intern("define_nodes"),
SyntaxExtension::ProcMacro(Box::new(MacroDefineNodes)));
#[proc_macro]
pub fn define_nodes(input: TokenStream) -> TokenStream {
let input = input.to_string();
let pd = parse_nodes(&input);
generate_defs(pd).parse().unwrap()
}
31 changes: 27 additions & 4 deletions vtree_macros/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
use syn::parse::{ident, path};
use syn::{Ident, Path};
use std::collections::HashMap;
use NodeChildType;
use Node;
use NodeChild;
use ParsedData;

#[derive(Debug, Clone)]
pub enum NodeChildType {
Single,
Optional,
Multi,
}

#[derive(Debug, Clone)]
pub struct NodeChild {
pub name: Ident,
pub group: Ident,
pub child_type: NodeChildType,
}

#[derive(Debug, Clone)]
pub struct Node {
pub name: Ident,
pub params_type: Option<Path>,
pub fields: Vec<NodeChild>,
}

#[derive(Debug, Clone)]
pub struct ParsedData {
pub nodes: Vec<Node>,
pub group_name_to_nodes: HashMap<Ident, Vec<Node>>,
}

named!(parse -> Vec<(Ident, Option<Path>, Option<Ident>, Vec<(Ident, NodeChildType, Ident)>)>,
terminated_list!(punct!(","), tuple!(
Expand Down

0 comments on commit 72a9b51

Please sign in to comment.