This commit is contained in:
Michael Zhang 2020-02-07 01:43:24 -06:00
parent 54a61d9865
commit b316508b7f
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
2 changed files with 63 additions and 35 deletions

View file

@ -1,5 +1,7 @@
#[macro_use] extern crate quote; #[macro_use]
#[macro_use] extern crate maplit; extern crate quote;
#[macro_use]
extern crate maplit;
extern crate proc_macro; extern crate proc_macro;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
@ -7,7 +9,7 @@ use std::collections::{HashMap, HashSet};
use petgraph::{dot::Dot, graph::Graph, graphmap::DiGraphMap}; use petgraph::{dot::Dot, graph::Graph, graphmap::DiGraphMap};
use proc_macro2::{Span, TokenStream, TokenTree}; use proc_macro2::{Span, TokenStream, TokenTree};
use quote::ToTokens; use quote::ToTokens;
use syn::{Expr, ExprPath, Ident, Path, PathSegment, PathArguments, punctuated::Punctuated}; use syn::{punctuated::Punctuated, Expr, ExprPath, Ident, Path, PathArguments, PathSegment};
#[derive(Debug, PartialEq, Eq, Hash)] #[derive(Debug, PartialEq, Eq, Hash)]
enum TagLhs { enum TagLhs {
@ -28,7 +30,6 @@ enum Rsx {
Tag(Tag), Tag(Tag),
Code(Expr), Code(Expr),
Text(String), Text(String),
// For(String, String, Vec<Rsx>), // For(String, String, Vec<Rsx>),
} }
@ -71,11 +72,11 @@ impl Visitor {
model: HashMap::new(), model: HashMap::new(),
} }
} }
fn load_model(&mut self, model: &HashMap<String, String>) { fn load_model(&mut self, model: &HashMap<String, String>) {
self.model.extend(model.clone()); self.model.extend(model.clone());
} }
fn unique_name(&mut self, base: impl AsRef<str>) -> String { fn unique_name(&mut self, base: impl AsRef<str>) -> String {
// TODO: normalize the name somehow so it fits in an ident (ex. strip punct) // TODO: normalize the name somehow so it fits in an ident (ex. strip punct)
let base = base.as_ref(); let base = base.as_ref();
@ -83,7 +84,7 @@ impl Visitor {
self.idx += 1; self.idx += 1;
format!("{}_{}", base, next) format!("{}_{}", base, next)
} }
fn unique_idx(&mut self, node: DepNode) -> u32 { fn unique_idx(&mut self, node: DepNode) -> u32 {
let next = self.idx; let next = self.idx;
self.idx += 1; self.idx += 1;
@ -119,13 +120,13 @@ impl Visitor {
fn extract_model_dependencies(expr: &Expr) -> HashSet<String> { fn extract_model_dependencies(expr: &Expr) -> HashSet<String> {
let tokens = expr.to_token_stream(); let tokens = expr.to_token_stream();
let mut result = HashSet::new(); let mut result = HashSet::new();
for token in tokens.into_iter() { for token in tokens.into_iter() {
if let TokenTree::Ident(ident) = token { if let TokenTree::Ident(ident) = token {
result.insert(format!("{}", ident)); result.insert(format!("{}", ident));
} }
} }
result result
} }
@ -137,39 +138,27 @@ pub fn example(input_tokens: proc_macro::TokenStream) -> proc_macro::TokenStream
"name".into() => "String".into(), "name".into() => "String".into(),
}; };
let todomvc_datainit: HashMap<String, String> = hashmap! {
"name".into() => "\"world\".into()".into(),
};
let todomvc_dom = vec![ let todomvc_dom = vec![
Rsx::Tag(Tag { Rsx::Tag(Tag {
tag: "input".into(), tag: "input".into(),
attrs: hashmap! { attrs: hashmap! {
TagLhs::Bind("value".into()) => "name".into(), TagLhs::Bind("value".into()) => "name".into(),
}, },
..Tag::default() ..Tag::default()
}), }),
Rsx::Text("Hello, ".into()), Rsx::Text("Hello, ".into()),
Rsx::Code(Expr::Path(ExprPath { Rsx::Code(syn::parse_str::<Expr>("name").unwrap()),
attrs: vec![],
qself: None,
path: Path {
leading_colon: None,
segments: {
let mut segments = Punctuated::new();
let ident = Ident::new("name", Span::call_site());
let arguments = PathArguments::None;
segments.push(PathSegment {
ident,
arguments,
});
segments
},
},
})),
Rsx::Text("!".into()), Rsx::Text("!".into()),
]; ];
let mut visitor = Visitor::new(); let mut visitor = Visitor::new();
visitor.load_model(&todomvc_datamodel); visitor.load_model(&todomvc_datamodel);
visitor.visit(&todomvc_dom); visitor.visit(&todomvc_dom);
println!("{:?}", visitor); println!("{:?}", visitor);
println!("DOT:"); println!("DOT:");
let graph: Graph<_, _, _> = visitor.deps.0.clone().into_graph(); let graph: Graph<_, _, _> = visitor.deps.0.clone().into_graph();
@ -177,19 +166,34 @@ pub fn example(input_tokens: proc_macro::TokenStream) -> proc_macro::TokenStream
let name = format_ident!("{}", todomvc_name); let name = format_ident!("{}", todomvc_name);
let mut model = TokenStream::new(); let mut model = TokenStream::new();
let mut init = TokenStream::new();
for (name, ty) in visitor.model { for (name, ty) in visitor.model {
let name = format_ident!("{}", name); let name = format_ident!("{}", name);
// TODO: parse this into an actual expression tree for Vec<T> // TODO: parse this into an actual expression tree for Vec<T>
let ty = format_ident!("{}", ty); let ty = format_ident!("{}", ty);
model.extend(quote!{ #name : #ty , }); model.extend(quote! { #name : #ty , });
}
for (name, value) in todomvc_datainit {
let name = format_ident!("{}", name);
let value = syn::parse_str::<Expr>(&value).unwrap();
init.extend(quote! { #name : #value , });
} }
let result = quote! { let result = quote! {
struct #name { struct #name {
#model #model
} }
impl #name { impl #name {
fn new() -> Self {
#name {
#init
}
}
}
impl crate::Component for #name {
fn initialize(&self, el: &crate::Element) {
}
} }
}; };

View file

@ -1,7 +1,31 @@
#[macro_use] extern crate enterprise_compiler; #[macro_use]
extern crate enterprise_compiler;
#[macro_use]
extern crate stdweb;
use stdweb::web::{document, Element, INonElementParentNode};
trait Component {
fn initialize(&self, element: &Element);
}
example!(); example!();
fn main() { fn render<C: Component>(component: &C, id: impl AsRef<str>) {
let id = id.as_ref();
if let Some(el) = document().get_element_by_id(id) {
component.initialize(&el);
}
}
fn main() {
stdweb::initialize();
let todomvc = TodoMVC::new();
render(&todomvc, "");
let message = "Hello world!";
js! { console.log(@{message}); }
stdweb::event_loop();
} }