Refactor PrettyPrint.
This commit is contained in:
parent
0d2db659ca
commit
8a6e4277a7
@ -6,7 +6,7 @@ mod type_gen;
|
|||||||
|
|
||||||
use crate::build_fn::make_build_fn;
|
use crate::build_fn::make_build_fn;
|
||||||
use crate::deserialize::deserialize_yaml_spec;
|
use crate::deserialize::deserialize_yaml_spec;
|
||||||
// use crate::pretty_print::make_pretty_print_impl;
|
use crate::pretty_print::make_pretty_print_impl;
|
||||||
use crate::type_gen::make_type;
|
use crate::type_gen::make_type;
|
||||||
use proc_macro2::TokenStream;
|
use proc_macro2::TokenStream;
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
@ -117,24 +117,27 @@ fn generate_node_file(build_specs: &[BuildSpec]) -> AstGeneratedFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn generate_pretty_print_file(build_specs: &[BuildSpec]) -> AstGeneratedFile {
|
fn generate_pretty_print_file(build_specs: &[BuildSpec]) -> AstGeneratedFile {
|
||||||
// let impls = build_specs
|
let impls = build_specs
|
||||||
// .iter()
|
.iter()
|
||||||
// .map(|build_spec| {
|
.map(|build_spec| {
|
||||||
// let stream = make_pretty_print_impl(build_spec);
|
let maybe_stream = make_pretty_print_impl(build_spec);
|
||||||
// debug_built_spec(build_spec, &stream);
|
if let Some(stream) = &maybe_stream {
|
||||||
// stream
|
debug_built_spec(build_spec, &stream);
|
||||||
// })
|
}
|
||||||
// .collect::<Vec<_>>();
|
maybe_stream
|
||||||
//
|
})
|
||||||
// let combined = quote! {
|
.filter(Option::is_some)
|
||||||
// use crate::ast::node::*;
|
.map(Option::unwrap)
|
||||||
// #(#impls)*
|
.collect::<Vec<_>>();
|
||||||
// };
|
|
||||||
// AstGeneratedFile {
|
let combined = quote! {
|
||||||
// name: String::from("pretty_print.rs"),
|
use crate::ast::node::*;
|
||||||
// contents: token_stream_to_string(combined),
|
#(#impls)*
|
||||||
// }
|
};
|
||||||
todo!()
|
AstGeneratedFile {
|
||||||
|
name: String::from("pretty_print.rs"),
|
||||||
|
contents: token_stream_to_string(combined),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_build_specs(yaml: &str) -> Vec<BuildSpec> {
|
pub fn get_build_specs(yaml: &str) -> Vec<BuildSpec> {
|
||||||
@ -145,6 +148,6 @@ pub fn generate_files(build_specs: &[BuildSpec]) -> Vec<AstGeneratedFile> {
|
|||||||
vec![
|
vec![
|
||||||
generate_build_file(build_specs),
|
generate_build_file(build_specs),
|
||||||
generate_node_file(build_specs),
|
generate_node_file(build_specs),
|
||||||
// generate_pretty_print_file(build_specs),
|
generate_pretty_print_file(build_specs),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,260 +1,214 @@
|
|||||||
// use crate::spec::leaf_enum_spec::LeafEnumBuildSpec;
|
use crate::spec::leaf_enum_spec::LeafEnumBuildSpec;
|
||||||
// use crate::spec::leaf_struct_spec::{LeafStructBuildSpec, LeafStructMemberKind};
|
use crate::spec::leaf_struct_spec::{LeafStructBuildSpec, LeafStructMemberKind};
|
||||||
// use crate::spec::polymorphic_type_spec::PolymorphicTypeBuildSpec;
|
use crate::spec::polymorphic_type_spec::PolymorphicTypeBuildSpec;
|
||||||
// use crate::spec::production_spec::ProductionBuildSpec;
|
use crate::spec::struct_spec::{MemberChildBuild, StructChild, StructSpec, VecChildBuild};
|
||||||
// use crate::spec::struct_spec::StructSpec;
|
use crate::spec::tree_enum_spec::{EnumRuleChildKind, TreeEnumBuildSpec};
|
||||||
// use crate::spec::tree_enum_spec::{EnumRuleChildKind, TreeEnumBuildSpec};
|
use crate::spec::BuildSpec;
|
||||||
// use crate::spec::{
|
use convert_case::{Case, Casing};
|
||||||
// AlternativeChild, BuildSpec, MemberChildToBuild, PolymorphicBuildBuildSpec,
|
use proc_macro2::TokenStream;
|
||||||
// PolymorphicEnumBuildSpec, StructChildSpec, VecChildToBuild,
|
use quote::{format_ident, quote};
|
||||||
// };
|
|
||||||
// use convert_case::{Case, Casing};
|
fn make_polymorphic_type_p2_impl(spec: &PolymorphicTypeBuildSpec) -> TokenStream {
|
||||||
// use proc_macro2::TokenStream;
|
let type_ident = format_ident!("{}", spec.name());
|
||||||
// use quote::{format_ident, quote};
|
let child_matchers = spec
|
||||||
//
|
.variants()
|
||||||
// fn make_production_p2_impl(_spec: &ProductionBuildSpec) -> TokenStream {
|
.map(|member| {
|
||||||
// quote! {}
|
let enum_member_ident = format_ident!("{}", member.name());
|
||||||
// }
|
let inner_name = format_ident!("{}", member.inner_kind().to_case(Case::Snake));
|
||||||
//
|
quote! {
|
||||||
// fn make_polymorphic_enum_p2_impl(_spec: &PolymorphicEnumBuildSpec) -> TokenStream {
|
#type_ident::#enum_member_ident(#inner_name) => #inner_name.pretty_print(writer)
|
||||||
// quote! {}
|
}
|
||||||
// }
|
})
|
||||||
//
|
.collect::<Vec<_>>();
|
||||||
// fn make_polymorphic_build_p2_impl(spec: &PolymorphicBuildBuildSpec) -> TokenStream {
|
|
||||||
// let (_, build) = spec.primary_alternative();
|
quote! {
|
||||||
// let type_ident = format_ident!("{}", spec.name());
|
impl PrettyPrint for #type_ident {
|
||||||
// let name_str = spec.name();
|
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
|
||||||
//
|
match self {
|
||||||
// let child_statements = build
|
#(#child_matchers,)*
|
||||||
// .children()
|
}
|
||||||
// .map(|child| match child {
|
}
|
||||||
// AlternativeChild::Skip => None,
|
}
|
||||||
// AlternativeChild::Build(build) => {
|
}
|
||||||
// let child_ident = format_ident!("{}", build.name());
|
}
|
||||||
// Some(quote! {
|
|
||||||
// self.#child_ident().pretty_print(writer)?
|
fn make_leaf_enum_p2_impl(spec: &LeafEnumBuildSpec) -> TokenStream {
|
||||||
// })
|
let type_ident = format_ident!("{}", spec.build());
|
||||||
// }
|
let child_matchers = spec
|
||||||
// })
|
.rules()
|
||||||
// .filter(Option::is_some)
|
.map(|rule| {
|
||||||
// .map(Option::unwrap)
|
let enum_variant_ident = format_ident!("{}", rule);
|
||||||
// .collect::<Vec<TokenStream>>();
|
let name_str = rule;
|
||||||
//
|
quote! {
|
||||||
// quote! {
|
#type_ident::#enum_variant_ident => writer.writeln_indented(#name_str)
|
||||||
// impl PrettyPrint for #type_ident {
|
}
|
||||||
// fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
|
})
|
||||||
// writer.writeln_indented(#name_str);
|
.collect::<Vec<_>>();
|
||||||
// writer.increase_indent();
|
|
||||||
// #(#child_statements;)*
|
quote! {
|
||||||
// writer.decrease_indent();
|
impl PrettyPrint for #type_ident {
|
||||||
// Ok(())
|
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
|
||||||
// }
|
match self {
|
||||||
// }
|
#(#child_matchers,)*
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
//
|
}
|
||||||
// fn make_polymorphic_type_p2_impl(spec: &PolymorphicTypeBuildSpec) -> TokenStream {
|
}
|
||||||
// let type_ident = format_ident!("{}", spec.name());
|
}
|
||||||
// let child_matchers = spec
|
|
||||||
// .variants()
|
fn make_tree_enum_p2_impl(spec: &TreeEnumBuildSpec) -> TokenStream {
|
||||||
// .map(|member| {
|
let type_ident = format_ident!("{}", spec.build());
|
||||||
// let enum_member_ident = format_ident!("{}", member.name());
|
let type_str = spec.build();
|
||||||
// let inner_name = format_ident!("{}", member.inner_kind().to_case(Case::Snake));
|
|
||||||
// quote! {
|
let child_matchers = spec
|
||||||
// #type_ident::#enum_member_ident(#inner_name) => #inner_name.pretty_print(writer)
|
.rules()
|
||||||
// }
|
.map(|rule| {
|
||||||
// })
|
let enum_variant_ident = format_ident!("{}", rule.rule());
|
||||||
// .collect::<Vec<_>>();
|
if let Some(child) = rule.child() {
|
||||||
//
|
match child.kind() {
|
||||||
// quote! {
|
EnumRuleChildKind::Node(node_child) => {
|
||||||
// impl PrettyPrint for #type_ident {
|
let child_name_ident =
|
||||||
// fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
|
format_ident!("{}", node_child.node_kind().to_case(Case::Snake));
|
||||||
// match self {
|
Some(quote! {
|
||||||
// #(#child_matchers,)*
|
#type_ident::#enum_variant_ident(#child_name_ident) => {
|
||||||
// }
|
#child_name_ident.pretty_print(writer)?;
|
||||||
// }
|
}
|
||||||
// }
|
})
|
||||||
// }
|
}
|
||||||
// }
|
_ => None,
|
||||||
//
|
}
|
||||||
// fn make_leaf_enum_p2_impl(spec: &LeafEnumBuildSpec) -> TokenStream {
|
} else {
|
||||||
// let type_ident = format_ident!("{}", spec.build());
|
let variant_str = rule.rule();
|
||||||
// let child_matchers = spec
|
Some(quote! {
|
||||||
// .rules()
|
#type_ident::#enum_variant_ident => writer.writeln_indented(#variant_str)?
|
||||||
// .map(|rule| {
|
})
|
||||||
// let enum_variant_ident = format_ident!("{}", rule.rule());
|
}
|
||||||
// let name_str = rule.rule();
|
})
|
||||||
// quote! {
|
.filter(Option::is_some)
|
||||||
// #type_ident::#enum_variant_ident => writer.writeln_indented(#name_str)
|
.map(Option::unwrap)
|
||||||
// }
|
.collect::<Vec<_>>();
|
||||||
// })
|
|
||||||
// .collect::<Vec<_>>();
|
quote! {
|
||||||
//
|
impl PrettyPrint for #type_ident {
|
||||||
// quote! {
|
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
|
||||||
// impl PrettyPrint for #type_ident {
|
writer.writeln_indented(#type_str)?;
|
||||||
// fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
|
writer.increase_indent();
|
||||||
// match self {
|
match self {
|
||||||
// #(#child_matchers,)*
|
#(#child_matchers,)*
|
||||||
// }
|
_ => {}
|
||||||
// }
|
}
|
||||||
// }
|
writer.decrease_indent();
|
||||||
// }
|
Ok(())
|
||||||
// }
|
}
|
||||||
//
|
}
|
||||||
// fn make_enum_p2_impl(spec: &TreeEnumBuildSpec) -> TokenStream {
|
}
|
||||||
// let type_ident = format_ident!("{}", spec.build());
|
}
|
||||||
// let type_str = spec.build();
|
|
||||||
//
|
fn make_leaf_struct_p2_impl(leaf_struct_build_spec: &LeafStructBuildSpec) -> TokenStream {
|
||||||
// let child_matchers = spec
|
let type_ident = format_ident!("{}", leaf_struct_build_spec.build());
|
||||||
// .rules()
|
let member_formatters = leaf_struct_build_spec
|
||||||
// .map(|rule| {
|
.members()
|
||||||
// let enum_variant_ident = format_ident!("{}", rule.rule());
|
.map(|member| match member.kind() {
|
||||||
// if let Some(child) = rule.child() {
|
LeafStructMemberKind::String => Some("{}"),
|
||||||
// match child.kind() {
|
})
|
||||||
// EnumRuleChildKind::Node(node_child) => {
|
.filter(Option::is_some)
|
||||||
// let child_name_ident =
|
.map(Option::unwrap)
|
||||||
// format_ident!("{}", node_child.build().to_case(Case::Snake));
|
.collect::<Vec<_>>()
|
||||||
// Some(quote! {
|
.join(", ");
|
||||||
// #type_ident::#enum_variant_ident(#child_name_ident) => {
|
|
||||||
// #child_name_ident.pretty_print(writer)?;
|
let format_string = format!("{}({})", leaf_struct_build_spec.build(), member_formatters);
|
||||||
// }
|
|
||||||
// })
|
let members = leaf_struct_build_spec
|
||||||
// }
|
.members()
|
||||||
// _ => None,
|
.map(|member| {
|
||||||
// }
|
let member_ident = format_ident!("{}", member.name());
|
||||||
// } else {
|
quote! {
|
||||||
// let variant_str = rule.rule();
|
self.#member_ident()
|
||||||
// Some(quote! {
|
}
|
||||||
// #type_ident::#enum_variant_ident => writer.writeln_indented(#variant_str)?
|
})
|
||||||
// })
|
.collect::<Vec<_>>();
|
||||||
// }
|
|
||||||
// })
|
quote! {
|
||||||
// .filter(Option::is_some)
|
impl PrettyPrint for #type_ident {
|
||||||
// .map(Option::unwrap)
|
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
|
||||||
// .collect::<Vec<_>>();
|
writer.writeln_indented(&format!(#format_string, #(#members),*))
|
||||||
//
|
}
|
||||||
// quote! {
|
}
|
||||||
// impl PrettyPrint for #type_ident {
|
}
|
||||||
// fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
|
}
|
||||||
// writer.writeln_indented(#type_str)?;
|
|
||||||
// writer.increase_indent();
|
fn make_struct_p2_impl(struct_build_spec: &StructSpec) -> TokenStream {
|
||||||
// match self {
|
let child_print_statements = struct_build_spec
|
||||||
// #(#child_matchers,)*
|
.children()
|
||||||
// _ => {}
|
.map(|child| match child {
|
||||||
// }
|
StructChild::SkipChild(_) => None,
|
||||||
// writer.decrease_indent();
|
StructChild::VecChild(vec_child) => match vec_child.build() {
|
||||||
// Ok(())
|
VecChildBuild::Node(_) => {
|
||||||
// }
|
let child_ident = format_ident!("{}", vec_child.name());
|
||||||
// }
|
Some(quote! {
|
||||||
// }
|
for child in self.#child_ident() {
|
||||||
// }
|
child.pretty_print(writer)?;
|
||||||
//
|
}
|
||||||
// fn make_leaf_struct_p2_impl(leaf_struct_build_spec: &LeafStructBuildSpec) -> TokenStream {
|
})
|
||||||
// let type_ident = format_ident!("{}", leaf_struct_build_spec.build());
|
}
|
||||||
// let member_formatters = leaf_struct_build_spec
|
VecChildBuild::String(_) => None,
|
||||||
// .members()
|
},
|
||||||
// .map(|member| match member.kind() {
|
StructChild::MemberChild(member_child) => match member_child.build() {
|
||||||
// LeafStructMemberKind::String => Some("{}"),
|
MemberChildBuild::Node(_) => {
|
||||||
// })
|
let child_ident = format_ident!("{}", member_child.name());
|
||||||
// .filter(Option::is_some)
|
if member_child.optional() {
|
||||||
// .map(Option::unwrap)
|
Some(quote! {
|
||||||
// .collect::<Vec<_>>()
|
if let Some(child) = self.#child_ident() {
|
||||||
// .join(", ");
|
child.pretty_print(writer)?;
|
||||||
//
|
}
|
||||||
// let format_string = format!("{}({})", leaf_struct_build_spec.build(), member_formatters);
|
})
|
||||||
//
|
} else {
|
||||||
// let members = leaf_struct_build_spec
|
Some(quote! {
|
||||||
// .members()
|
self.#child_ident().pretty_print(writer)?;
|
||||||
// .map(|member| {
|
})
|
||||||
// let member_ident = format_ident!("{}", member.name());
|
}
|
||||||
// quote! {
|
}
|
||||||
// self.#member_ident()
|
MemberChildBuild::Boolean(_) => {
|
||||||
// }
|
let format_string = format!("{}({})", member_child.name(), "{}");
|
||||||
// })
|
let child_ident = format_ident!("{}", member_child.name());
|
||||||
// .collect::<Vec<_>>();
|
Some(quote! {
|
||||||
//
|
writer.writeln_indented(&format!(#format_string, self.#child_ident()))?;
|
||||||
// quote! {
|
})
|
||||||
// impl PrettyPrint for #type_ident {
|
}
|
||||||
// fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
|
},
|
||||||
// writer.writeln_indented(&format!(#format_string, #(#members),*))
|
})
|
||||||
// }
|
.filter(Option::is_some)
|
||||||
// }
|
.map(Option::unwrap)
|
||||||
// }
|
.collect::<Vec<_>>();
|
||||||
// }
|
|
||||||
//
|
let type_ident = format_ident!("{}", struct_build_spec.build());
|
||||||
// fn make_struct_p2_impl(struct_build_spec: &StructSpec) -> TokenStream {
|
let type_string = struct_build_spec.build();
|
||||||
// let child_print_statements = struct_build_spec
|
|
||||||
// .children()
|
quote! {
|
||||||
// .map(|child| match child {
|
impl PrettyPrint for #type_ident {
|
||||||
// StructChildSpec::SkipChild(_) => None,
|
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
|
||||||
// StructChildSpec::VecChild(vec_child) => match vec_child.build() {
|
writer.writeln_indented(#type_string)?;
|
||||||
// VecChildToBuild::Node(_) => {
|
writer.increase_indent();
|
||||||
// let child_ident = format_ident!("{}", vec_child.name());
|
#(#child_print_statements)*
|
||||||
// Some(quote! {
|
writer.decrease_indent();
|
||||||
// for child in self.#child_ident() {
|
Ok(())
|
||||||
// child.pretty_print(writer)?;
|
}
|
||||||
// }
|
}
|
||||||
// })
|
}
|
||||||
// }
|
}
|
||||||
// VecChildToBuild::String => None,
|
|
||||||
// },
|
pub fn make_pretty_print_impl(build_spec: &BuildSpec) -> Option<TokenStream> {
|
||||||
// StructChildSpec::MemberChild(member_child) => match member_child.build() {
|
match build_spec {
|
||||||
// MemberChildToBuild::Node(node_member_child) => {
|
BuildSpec::Struct(struct_spec) => Some(make_struct_p2_impl(struct_spec)),
|
||||||
// let child_ident = format_ident!("{}", member_child.name());
|
BuildSpec::LeafStruct(leaf_struct) => Some(make_leaf_struct_p2_impl(leaf_struct)),
|
||||||
// if node_member_child.optional() {
|
BuildSpec::Enum(enum_spec) => Some(make_tree_enum_p2_impl(enum_spec)),
|
||||||
// Some(quote! {
|
BuildSpec::LeafEnum(leaf_enum) => Some(make_leaf_enum_p2_impl(leaf_enum)),
|
||||||
// if let Some(child) = self.#child_ident() {
|
BuildSpec::Production(_) => None,
|
||||||
// child.pretty_print(writer)?;
|
BuildSpec::NodeProduction(_) => None,
|
||||||
// }
|
BuildSpec::PolymorphicType(polymorphic_type) => {
|
||||||
// })
|
Some(make_polymorphic_type_p2_impl(polymorphic_type))
|
||||||
// } else {
|
}
|
||||||
// Some(quote! {
|
BuildSpec::PolymorphicPassThrough(_) => None,
|
||||||
// self.#child_ident().pretty_print(writer)?;
|
BuildSpec::PolymorphicEnumLoop(_) => None,
|
||||||
// })
|
}
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
// MemberChildToBuild::Boolean(boolean_member_child) => {
|
|
||||||
// let format_string = format!("{}({})", boolean_member_child.name(), "{}");
|
|
||||||
// let child_ident = format_ident!("{}", boolean_member_child.name());
|
|
||||||
// Some(quote! {
|
|
||||||
// writer.writeln_indented(&format!(#format_string, self.#child_ident()))?;
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
// },
|
|
||||||
// })
|
|
||||||
// .filter(Option::is_some)
|
|
||||||
// .map(Option::unwrap)
|
|
||||||
// .collect::<Vec<_>>();
|
|
||||||
//
|
|
||||||
// let type_ident = format_ident!("{}", struct_build_spec.build());
|
|
||||||
// let type_string = struct_build_spec.build();
|
|
||||||
//
|
|
||||||
// quote! {
|
|
||||||
// impl PrettyPrint for #type_ident {
|
|
||||||
// fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
|
|
||||||
// writer.writeln_indented(#type_string)?;
|
|
||||||
// writer.increase_indent();
|
|
||||||
// #(#child_print_statements)*
|
|
||||||
// writer.decrease_indent();
|
|
||||||
// Ok(())
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// pub fn make_pretty_print_impl(build_spec: &BuildSpec) -> TokenStream {
|
|
||||||
// match build_spec {
|
|
||||||
// BuildSpec::Struct(struct_spec) => make_struct_p2_impl(struct_spec),
|
|
||||||
// BuildSpec::LeafStruct(leaf_struct) => make_leaf_struct_p2_impl(leaf_struct),
|
|
||||||
// BuildSpec::Enum(enum_spec) => make_enum_p2_impl(enum_spec),
|
|
||||||
// BuildSpec::LeafEnum(leaf_enum) => make_leaf_enum_p2_impl(leaf_enum),
|
|
||||||
// BuildSpec::PolymorphicType(polymorphic) => make_polymorphic_type_p2_impl(polymorphic),
|
|
||||||
// BuildSpec::PolymorphicBuild(polymorphic_build) => {
|
|
||||||
// make_polymorphic_build_p2_impl(polymorphic_build)
|
|
||||||
// }
|
|
||||||
// BuildSpec::PolymorphicEnum(polymorphic_enum) => {
|
|
||||||
// make_polymorphic_enum_p2_impl(polymorphic_enum)
|
|
||||||
// }
|
|
||||||
// BuildSpec::Production(production) => make_production_p2_impl(production),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user