tree-gen
C++ code generator for tree structures
tree-gen.hpp
Go to the documentation of this file.
1 
614 #ifndef _TREE_GEN_HPP_INCLUDED_
615 #define _TREE_GEN_HPP_INCLUDED_
616 
617 #include <string>
618 #include <sstream>
619 #include <algorithm>
620 #include <iterator>
621 #include <vector>
622 #include <list>
623 #include <map>
624 #include <memory>
625 #include <cctype>
626 
630 namespace tree_gen {
631 
635 enum EdgeType {
636 
641 
646 
651 
656 
661 
666 
671 
672 };
673 
674 struct Node;
675 
679 struct Field {
680 
685 
689  std::shared_ptr<Node> node_type;
690 
696  std::string prim_type;
697 
704  std::string py_prim_type;
705 
710  std::string py_multi_type;
711 
715  std::string name;
716 
720  std::string doc;
721 
728 };
729 
733 struct Node {
734 
738  std::string snake_case_name;
739 
743  std::string title_case_name;
744 
748  std::string doc;
749 
753  std::shared_ptr<Node> parent;
754 
758  std::vector<std::weak_ptr<Node>> derived;
759 
763  std::vector<Field> fields;
764 
768  std::list<std::string> order;
769 
774 
778  std::vector<Field> all_fields() const;
779 
780 };
781 
785 using Nodes = std::vector<std::shared_ptr<Node>>;
786 
791 std::string replace_all(std::string str, const std::string& from, const std::string& to);
792 
796 class NodeBuilder {
797 public:
798 
802  std::shared_ptr<Node> node;
803 
807  NodeBuilder(const std::string &name, const std::string &doc="");
808 
812  NodeBuilder *derive_from(std::shared_ptr<Node> parent);
813 
817  NodeBuilder *with_child(
818  EdgeType type,
819  const std::string &node_name,
820  const std::string &name,
821  const std::string &doc = ""
822  );
823 
827  NodeBuilder *with_prim(
828  const std::string &prim,
829  const std::string &name,
830  const std::string &doc = "",
831  EdgeType type = Prim
832  );
833 
838  NodeBuilder *with_order(std::list<std::string> &&order);
839 
843  NodeBuilder *mark_error();
844 
845 };
846 
851 private:
852 
856  std::map<std::string, std::shared_ptr<NodeBuilder>> builders;
857 
858 public:
859 
863  std::string source_doc;
864 
868  std::string header_doc;
869 
874  std::string header_fname;
875 
879  std::string python_doc;
880 
884  std::vector<std::string> includes;
885 
889  std::vector<std::string> src_includes;
890 
894  std::vector<std::string> python_includes;
895 
899  std::string namespace_doc;
900 
904  std::vector<std::string> namespaces;
905 
909  std::string tree_namespace;
910 
914  std::string support_namespace;
915 
919  std::string initialize_function;
920 
925  std::string serialize_fn;
926 
931  std::string py_serialize_fn;
932 
936  std::string deserialize_fn;
937 
942  std::string py_deserialize_fn;
943 
949  std::string source_location;
950 
955 
959  void set_source_doc(const std::string &doc);
960 
964  void set_header_doc(const std::string &doc);
965 
969  void set_header_fname(const std::string &fname);
970 
974  void set_python_doc(const std::string &doc);
975 
979  void set_tree_namespace(const std::string &name_space);
980 
984  void set_support_namespace(const std::string &name_space);
985 
989  void set_initialize_function(const std::string &init_fn);
990 
994  void set_serdes_functions(const std::string &ser_fn, const std::string &des_fn);
995 
999  void set_source_location(const std::string &ident);
1000 
1004  void add_include(const std::string &include);
1005 
1009  void add_src_include(const std::string &include);
1010 
1014  void add_python_include(const std::string &include);
1015 
1019  void add_namespace(const std::string &name_space, const std::string &doc = "");
1020 
1024  void add_node(std::shared_ptr<NodeBuilder> &node_builder);
1025 
1029  void build();
1030 
1031 };
1032 
1033 } // namespace tree_gen
1034 
1035 #endif
Exactly one node.
Definition: tree-gen.hpp:645
std::string support_namespace
The namespace to take support stuff like tree::cbor from.
Definition: tree-gen.hpp:914
std::vector< std::shared_ptr< Node > > Nodes
List of nodes.
Definition: tree-gen.hpp:785
Represents a type of AST node.
Definition: tree-gen.hpp:733
Struct containing everything needed for a complete specification.
Definition: tree-gen.hpp:850
std::vector< std::string > src_includes
The include statements to stick at the top of the source file.
Definition: tree-gen.hpp:889
std::string py_multi_type
The primitive Multi* type name in the Python world, if any (depends on type and ext_type).
Definition: tree-gen.hpp:710
Zero or one nodes.
Definition: tree-gen.hpp:640
std::vector< std::weak_ptr< Node > > derived
Node types derived from this one.
Definition: tree-gen.hpp:758
std::string doc
Class member documentation.
Definition: tree-gen.hpp:720
Namespace for the tree-gen program.
std::vector< std::string > includes
The include statements to stick at the top of the header file.
Definition: tree-gen.hpp:884
std::string namespace_doc
Namespace documentation.
Definition: tree-gen.hpp:899
std::string tree_namespace
The namespace to take the tree base types from.
Definition: tree-gen.hpp:909
std::string header_fname
Explicit filename for the header, in case it will not end up in the same directory that the source is...
Definition: tree-gen.hpp:874
std::vector< std::string > namespaces
The C++ namespaces to use.
Definition: tree-gen.hpp:904
std::string initialize_function
The initialization function to use for default values of members.
Definition: tree-gen.hpp:919
std::string title_case_name
Name in TitleCase.
Definition: tree-gen.hpp:743
std::string header_doc
Header file documentation.
Definition: tree-gen.hpp:868
Link to zero or one nodes elsewhere in the tree.
Definition: tree-gen.hpp:660
std::string prim_type
The primitive type name, if any (depends on type).
Definition: tree-gen.hpp:696
EdgeType
Types of edges between nodes and primitives.
Definition: tree-gen.hpp:635
Primitive type.
Definition: tree-gen.hpp:670
std::string source_location
Annotation object used for source location info, or empty if source locations are not used or are not...
Definition: tree-gen.hpp:949
std::string py_prim_type
The primitive type name, if any (depends on type).
Definition: tree-gen.hpp:704
std::string doc
Class documentation.
Definition: tree-gen.hpp:748
std::shared_ptr< Node > node_type
The field type, if any (depends on type).
Definition: tree-gen.hpp:689
EdgeType ext_type
External node type.
Definition: tree-gen.hpp:727
Represents a field.
Definition: tree-gen.hpp:679
One or more nodes.
Definition: tree-gen.hpp:655
std::string replace_all(std::string str, const std::string &from, const std::string &to)
Convenience method for replacing all occurrences of a substring in a string with another string...
Definition: tree-gen.cpp:48
Convenience class for constructing a node.
Definition: tree-gen.hpp:796
std::string serialize_fn
The serialization function to use when serializing primitives.
Definition: tree-gen.hpp:925
bool is_error_marker
Whether this node represents a recovered parse error.
Definition: tree-gen.hpp:773
std::vector< std::string > python_includes
The include statements to stick at the top of the Python file.
Definition: tree-gen.hpp:894
std::vector< Field > fields
Child nodes.
Definition: tree-gen.hpp:763
EdgeType type
The edge type for the field.
Definition: tree-gen.hpp:684
std::list< std::string > order
Optional override for field order as returned by all_fields().
Definition: tree-gen.hpp:768
std::string source_doc
Source file documentation.
Definition: tree-gen.hpp:863
std::string python_doc
Python file documentation.
Definition: tree-gen.hpp:879
std::string snake_case_name
Name in snake_case.
Definition: tree-gen.hpp:738
std::string py_deserialize_fn
Deserialization function to use when serializing primitives in the Python domain, using ...
Definition: tree-gen.hpp:942
std::string deserialize_fn
The serialization function to use when deserializing primitives.
Definition: tree-gen.hpp:936
Zero or more nodes.
Definition: tree-gen.hpp:650
std::string name
Class member name.
Definition: tree-gen.hpp:715
std::shared_ptr< Node > parent
The node type this is derived from, if any.
Definition: tree-gen.hpp:753
std::shared_ptr< Node > node
The node being constructed.
Definition: tree-gen.hpp:802
Nodes nodes
All the nodes.
Definition: tree-gen.hpp:954
std::string py_serialize_fn
Serialization function to use when serializing primitives in the Python domain, using ...
Definition: tree-gen.hpp:931
Link to exactly one node elsewhere in the tree.
Definition: tree-gen.hpp:665