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.9KB

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