spec_syntax.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #ifndef SPEC_SYNTAX__H
  2. #define SPEC_SYNTAX__H
  3. /**
  4. * Copyright (C) 2011 Anders Sundman <anders@4zm.org>
  5. *
  6. * This file is part of mfterm.
  7. *
  8. * mfterm is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * mfterm is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with mfterm. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <stdio.h>
  22. typedef enum {
  23. COMPOSITE_TYPE,
  24. BYTE_TYPE,
  25. BIT_TYPE,
  26. } type_category_t;
  27. typedef enum {
  28. PARTIAL_DECL,
  29. COMPLETE_DECL,
  30. } type_decl_status_t;
  31. typedef struct type_t type_t;
  32. typedef struct field_t field_t;
  33. typedef struct field_list_t field_list_t;
  34. typedef struct composite_type_extras_t composite_type_extras_t;
  35. typedef struct type_table_t type_table_t;
  36. typedef struct instance_t instance_t;
  37. typedef struct instance_list_t instance_list_t;
  38. // Parse the input file and set up type_root and instance_root.
  39. // Return 0 on success.
  40. int spec_import(FILE* input);
  41. /**
  42. * A struct representing a data type in the specification
  43. * language. There are two primitive types, Bit and Bytte. All other
  44. * types are user defined and use the composite_extras field to
  45. * express the type details.
  46. *
  47. * The primitive types are allocated statically (constants), while all
  48. * the composite types are allocated dynamically.
  49. */
  50. struct type_t {
  51. type_category_t type_category;
  52. composite_type_extras_t* composite_extras;
  53. };
  54. // The primitive type instances
  55. extern type_t byte_type;
  56. extern type_t bit_type;
  57. /**
  58. * A composite type is made up of an ordered list of fields. A field
  59. * is a named use of another type as an array. A field array of length
  60. * 1 can be treated without the array syntax in the language; but is
  61. * represented like all other fields.
  62. */
  63. struct field_t {
  64. char* name; // Field name
  65. type_t* type;
  66. size_t length;
  67. };
  68. /**
  69. * Type representing the ordered list of fields in a composite type.
  70. */
  71. struct field_list_t {
  72. field_t* field;
  73. field_list_t* next_;
  74. };
  75. /**
  76. * This structure represents the content of a user defined type. It
  77. * holds the name and the fields of the type.
  78. *
  79. * The type also has a flag to indicate if it has been fully
  80. * declared. The specification language allows the use of types before
  81. * they have been declared. Once the complete specification has been
  82. * parsed, all types must be declared.
  83. */
  84. struct composite_type_extras_t {
  85. char* name; // Type name
  86. type_decl_status_t decl_status;
  87. field_list_t* fields; // All fields of the type or NULL.
  88. };
  89. // Allocate and return a composite type instance. The type will assume
  90. // ownership of the heap allocated name.
  91. type_t* make_composite_type(char* name);
  92. // Free a composite type. This function will also free it's fields.
  93. void free_composite_type(type_t* t);
  94. // Allocate a new field with the given parameters. Anonymous '-'
  95. // filler fields use NULL as name. The field will assume ownership of
  96. // the heap allocated name.
  97. field_t* make_field(char* name, type_t* type, size_t length);
  98. // Free the memory used by a field.
  99. void free_field(field_t* field);
  100. // Add a field to an existing list of fields. The order of fields is
  101. // significant and this function will append the field to the end of
  102. // the field_list.
  103. field_list_t* append_field(field_list_t* field_list, field_t* field);
  104. // Search the field list for a field with the given name
  105. field_t* get_field(field_list_t* field_list, const char* name);
  106. /**
  107. * A 'table' of all the types in the language. This is part of the
  108. * output from the parsing process. The table is actually a list and
  109. * operations are typically O(n^2); but since there will probably
  110. * never be more than 50 types, this should be ok.
  111. */
  112. struct type_table_t {
  113. type_t* type;
  114. type_table_t* next_;
  115. };
  116. // The global instance of the type table. If there isn't any, the
  117. // variable will be NULL. All the type table operations (tt_) operate
  118. // on this global variable.
  119. extern type_table_t* type_table;
  120. // The root type of the type hierarchy
  121. extern type_t* type_root;
  122. // Clear the type table - freeing the memory used by the table and by
  123. // all the types.
  124. void tt_clear();
  125. // Add a type to the type table.
  126. type_t* tt_add_type(type_t* t);
  127. // Search the type table for a type with the given name. The first
  128. // type found will be returned. If no type is found, NULL is returned.
  129. type_t* tt_get_type(const char* type_name);
  130. // Check if there are any partially declared types in the type
  131. // table. Return a pointer to the first incomplete type or NULL if
  132. // none exists.
  133. type_t* tt_contains_partial_types();
  134. /**
  135. * Type representing instances of types in the spec language. The same
  136. * type can be instantiated several times in different spec types and
  137. * fields. The instances map agains type fields and thus contains a
  138. * length field.
  139. *
  140. * The size field is inclusive of the instance and all it's child
  141. * instances. The bit size field will be < 8; larger bit fields in the
  142. * type spec will be included in the byte field.
  143. */
  144. struct instance_t {
  145. size_t offset_bytes;
  146. size_t offset_bits;
  147. size_t size_bytes;
  148. size_t size_bits;
  149. field_t* field;
  150. instance_list_t* fields;
  151. };
  152. /**
  153. * Type representing the ordered list of instance fields in a
  154. * composite type instance.
  155. */
  156. struct instance_list_t {
  157. instance_t* instance;
  158. instance_list_t* next_;
  159. };
  160. // The global variable representing the root instance; it is an
  161. // instanciation of the '.' type.
  162. extern instance_t* instance_root;
  163. // Create an instance tree matching the type tree starting at
  164. // root_type. The global instance tree is constructed with root_type '.'.
  165. instance_t* make_instance_tree(type_t* root_type);
  166. // Clear the global instance tree. Free it and set instance_tree NULL
  167. void clear_instance_tree();
  168. // Print a representation of the instance hierarchy
  169. void print_instance_tree();
  170. /**
  171. * Get the child instance with a given name. Only look to children,
  172. * not grand children. If no child with the given name exists, return
  173. * null.
  174. */
  175. instance_t* get_instance_child(instance_t* inst, const char* name);
  176. /**
  177. * Like get_instance_child(inst, name), but name does not have to be
  178. * null terminated. Instead the length of the name string is given by
  179. * the last argument.
  180. */
  181. instance_t* get_instance_child_n(instance_t* inst, const char* name, size_t nlen);
  182. /**
  183. * Parse a specification path of the form '.fu.bar.baz' and return the
  184. * instance pointed to by baz. In case the path doesn't point to a
  185. * loaded instance, return NULL. */
  186. instance_t* parse_spec_path(const char* path);
  187. /**
  188. * Parse the path to produce a parent section and an instance that
  189. * points to the head of the parent.
  190. *
  191. * The format is .fu.bar.ba(z). Where .fu.bar.ba is the path, fu, bar
  192. * and baz are nested fields. The function should return parent_end
  193. * pointing into path to the point after the last '.', i.e. to the 'b'
  194. * in the last 'ba'. parent_inst will point to bar.
  195. *
  196. * The function returns 0 on success.
  197. */
  198. int parse_partial_spec_path(const char* path,
  199. const char** parent_end,
  200. instance_t** parent_inst);
  201. // Return the number of fields the instance has. 0 if inst is NULL.
  202. int instance_fields_count(instance_t* inst);
  203. #endif