spec_parser.y 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * Copyright (C) 2011 Anders Sundman <anders@4zm.org>
  3. *
  4. * This file is part of mfterm.
  5. *
  6. * mfterm is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * mfterm is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with mfterm. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. %{
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdarg.h>
  23. #include "spec_syntax.h"
  24. #include "util.h"
  25. /* #define YYDEBUG 1 */
  26. /* sp_debug = 1; */
  27. #define YYERROR_VERBOSE 1
  28. struct YYLTYPE;
  29. int sp_lex(void);
  30. void sp_error(const char* s, ...);
  31. void sp_lerror(struct YYLTYPE loc, const char* s, ...);
  32. %}
  33. %union {
  34. type_t* type_t_ptr;
  35. field_t* field_t_ptr;
  36. field_list_t* field_list_t_ptr;
  37. char* string;
  38. int integer;
  39. }
  40. %token BYTE "Byte"
  41. %token BIT "Bit"
  42. %token <string> IDENTIFIER "name"
  43. %token <string> DEC_NUM "dec-number"
  44. %token <string> HEX_NUM "hex-number"
  45. %token end_of_file 0 "end-of-file"
  46. %type <type_t_ptr> data_type
  47. %type <type_t_ptr> primitive_data_type
  48. %type <type_t_ptr> named_composite_type_decl
  49. %type <field_list_t_ptr> composite_type_decl
  50. %type <field_t_ptr> field_decl
  51. %type <field_list_t_ptr> field_decl_list
  52. %type <integer> number
  53. %destructor { free($$); } IDENTIFIER DEC_NUM HEX_NUM <string>
  54. %destructor { free_field($$);
  55. } field_decl <field_t_ptr>
  56. %destructor { free_field($$->field); free($$);
  57. } composite_type_decl field_decl_list <field_list_t_ptr>
  58. %destructor { if ($$ && $$->composite_extras)
  59. free_composite_type($$);
  60. } named_composite_type_decl data_type <type_t_ptr>
  61. %% /* Grammar rules and actions follow. */
  62. input
  63. : /* empty */ { }
  64. | input named_composite_type_decl { (void) $2; }
  65. | input '.' composite_type_decl {
  66. if (tt_get_type(".")) {
  67. // Error - the root type has allready been defined
  68. sp_lerror(@1, "Root type '.' allready defined.");
  69. YYERROR; // abort and initiate error recovery
  70. }
  71. // Create the type (named '.')
  72. type_t* t = make_composite_type(strdup("."));
  73. t->composite_extras->fields = $3;
  74. t->composite_extras->decl_status = COMPLETE_DECL;
  75. tt_add_type(t);
  76. type_root = t;
  77. }
  78. ;
  79. named_composite_type_decl
  80. : IDENTIFIER composite_type_decl {
  81. type_t* t = tt_get_type($1);
  82. if (t && t->composite_extras->decl_status == COMPLETE_DECL) {
  83. // Error - the type has been defined once before
  84. sp_lerror(@1, "Re-definition of type '%s'", $1);
  85. $$ = NULL;
  86. YYERROR; // abort and initiate error recovery
  87. }
  88. // If it's the first time we see the type, create it
  89. if(t == NULL) {
  90. t = make_composite_type($1);
  91. tt_add_type(t);
  92. }
  93. t->composite_extras->fields = $2;
  94. t->composite_extras->decl_status = COMPLETE_DECL;
  95. $$ = t;
  96. }
  97. ;
  98. composite_type_decl
  99. : '{' field_decl_list '}' {
  100. $$ = $2;
  101. }
  102. ;
  103. field_decl_list
  104. : /* empty */ {
  105. $$ = NULL;
  106. }
  107. | field_decl_list field_decl {
  108. if ($1 == NULL) {
  109. $$ = append_field(NULL, $2);
  110. }
  111. else {
  112. if ($2 == NULL) {
  113. $$ = NULL;
  114. }
  115. else if ($2->name == NULL || get_field($1, $2->name) == NULL) {
  116. // If it doesn't exist, then all is well. Add it.
  117. $$ = append_field($1, $2);
  118. }
  119. else {
  120. // If it allready exists, we have a semantic error.
  121. sp_lerror(@2, "A field with the name '%s' is allready defined.",
  122. $2->name);
  123. $$ = $1;
  124. YYERROR; // abort and initiate error recovery
  125. }
  126. }
  127. }
  128. | field_decl_list error {
  129. $$ = $1;
  130. yyclearin;
  131. YYERROR;
  132. }
  133. ;
  134. field_decl
  135. : data_type IDENTIFIER {
  136. $$ = make_field($2, $1, 1);
  137. }
  138. | data_type '-' {
  139. $$ = make_field(NULL, $1, 1); // Anonymous
  140. }
  141. | data_type '[' number ']' IDENTIFIER {
  142. $$ = make_field($5, $1, $3);
  143. }
  144. | data_type '[' number ']' '-' {
  145. $$ = make_field(NULL, $1, $3);
  146. }
  147. // Error handling
  148. | data_type error {
  149. (void) $1; $$ = NULL; YYERROR;
  150. }
  151. | data_type '[' number ']' error {
  152. (void) $1; $$ = NULL; YYERROR;
  153. }
  154. | data_type '[' error ']' IDENTIFIER {
  155. (void) $1; (void) $5; $$ = NULL; YYERROR;
  156. }
  157. | data_type '[' error ']' '-' {
  158. (void) $1; $$ = NULL; YYERROR;
  159. }
  160. | data_type '[' error ']' error {
  161. (void) $1; $$ = NULL; YYERROR;
  162. }
  163. ;
  164. data_type
  165. : composite_type_decl {
  166. type_t* t;
  167. t = make_composite_type(NULL); // Anonymous
  168. tt_add_type(t);
  169. t->composite_extras->fields = $1;
  170. t->composite_extras->decl_status = COMPLETE_DECL;
  171. $$ = t;
  172. }
  173. | primitive_data_type { $$ = $1; }
  174. | IDENTIFIER {
  175. type_t* t = tt_get_type($1);
  176. if (t) {
  177. $$ = t; // Re-discovered a known type
  178. }
  179. else {
  180. // This is a new type name - decl will hopefully come later.
  181. t = make_composite_type($1);
  182. t->composite_extras->decl_status = PARTIAL_DECL;
  183. tt_add_type(t);
  184. $$ = t;
  185. }
  186. }
  187. ;
  188. primitive_data_type
  189. : BYTE { $$ = &byte_type; }
  190. | BIT { $$ = &bit_type; }
  191. ;
  192. number
  193. : DEC_NUM { $$ = strtol($1, NULL, 10); free($1); }
  194. | HEX_NUM { $$ = strtol($1, NULL, 16); free($1); }
  195. ;
  196. %%
  197. void sp_lerror(struct YYLTYPE t, const char* s, ...) {
  198. va_list ap;
  199. va_start(ap, s);
  200. if(t.first_line) {
  201. if (t.last_line == t.first_line)
  202. fprintf(stderr, "Error:%d:%d: ", t.first_line, t.first_column);
  203. else
  204. fprintf(stderr, "Error:%d-%d: ", t.first_line, t.last_line);
  205. }
  206. vfprintf(stderr, s, ap);
  207. fprintf(stderr, "\n");
  208. }