You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

parser-wavefront-obj.y 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. %{
  2. /*#include <QtGui>*/
  3. #include <QVariant>
  4. #include <QDebug>
  5. #include "utils/vector3d.h"
  6. #include "utils/wavefrontobj.h"
  7. #include "lexer-wavefront-obj.h"
  8. // yylex is a function generated by Flex and we must tell to Bison that it is
  9. // defined in other place.
  10. extern int wavefront_objlex(void);
  11. // Bison uses the yyerror function for informing us when a parsing error has
  12. // occurred.
  13. void wavefront_objerror(WaveFrontObj* data, const char *s);
  14. %}
  15. %define api.prefix {wavefront_obj}
  16. %define api.token.prefix {TOK_}
  17. %parse-param {struct WaveFrontObj* wavefrontData}
  18. // Here we define our custom variable types.
  19. // Custom types must be of fixed size.
  20. %union {
  21. double number;
  22. int integer;
  23. char* string;
  24. Vector3D* vector3d;
  25. QList<int>* integers;
  26. }
  27. // Define the terminal expression types.
  28. %token <number> NUMBER
  29. %token <string> STRING
  30. %token SLASH
  31. %token MTLLIB
  32. %token USEMTL
  33. %token O
  34. %token V
  35. %token VT
  36. %token VN
  37. %token VP
  38. %token F
  39. %token S
  40. %destructor { free($$); } <string>
  41. %destructor { delete $$; } <vector3d>
  42. %destructor { delete $$; } <integers>
  43. // Define the non-terminal expression types.
  44. %type <vector3d> vertex
  45. %type <integer> face_vertex
  46. %type <integers> face_vertex_list
  47. %type <integers> face
  48. %%
  49. wavefront_obj: stmt_list {};
  50. stmt_list: {}
  51. | stmt_list stmt {};
  52. stmt: mtllib {}
  53. | usemtl {}
  54. | named_object {}
  55. | vertex { wavefrontData->addVertex(*$1); delete $1; }
  56. | texture_coordinate {}
  57. | vertex_normal {}
  58. | vertex_space {}
  59. | face { wavefrontData->addFace(*$1); delete $1; }
  60. | smooth {};
  61. mtllib: MTLLIB STRING {};
  62. usemtl: USEMTL STRING {};
  63. named_object: O STRING {};
  64. vertex: V NUMBER NUMBER NUMBER { $$ = new Vector3D($2, $3, $4); }
  65. | V NUMBER NUMBER NUMBER NUMBER { $$ = new Vector3D($2, $3, $4); };
  66. texture_coordinate: VT NUMBER NUMBER {}
  67. | VT NUMBER NUMBER NUMBER {};
  68. vertex_normal: VN NUMBER NUMBER NUMBER {};
  69. vertex_space: VP NUMBER NUMBER NUMBER {};
  70. face: F face_vertex_list { $$ = $2; };
  71. face_vertex_list: { $$ = new QList<int>(); }
  72. | face_vertex_list face_vertex { $1->append($2); $$ = $1;};
  73. face_vertex: NUMBER { $$ = $1; }
  74. | NUMBER SLASH NUMBER { $$ = $1; }
  75. | NUMBER SLASH NUMBER SLASH NUMBER { $$ = $1; }
  76. | NUMBER SLASH SLASH NUMBER { $$ = $1; };
  77. smooth: S NUMBER {}
  78. | S STRING {};
  79. %%
  80. void wavefront_objerror(WaveFrontObj* data, const char *s)
  81. {
  82. data->setError("Parse error: " + QString(s) + " at line " + QString::number(wavefront_objlineno) + " at " + QString(wavefront_objtext));
  83. }