glsl_parser.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #ifndef GLSL_PARSER_H
  2. #define GLSL_PARSER_H
  3. #include <stdint.h>
  4. #include <stddef.h>
  5. #include <stdbool.h>
  6. #include <stdio.h>
  7. struct glsl_node {
  8. //Type of this node. These values are all members of the enum
  9. int code:16;
  10. //Number of child nodes
  11. int child_count:16;
  12. //Meta data for this node. Only uses for some nodes. The
  13. //field in this unions that should be read (if any)
  14. //is determined by 'code'
  15. union {
  16. double d;
  17. float f;
  18. int i;
  19. unsigned int ui;
  20. bool b;
  21. const char *str;
  22. } data;
  23. //
  24. // Child nodes. Extra data will be allocated past the end
  25. // of the structure to hold the child nodes.
  26. //
  27. struct glsl_node *children[];
  28. };
  29. typedef void (*glsl_parse_error_cb_t)(const char *error_str, int lineno, int start_col, int end_col);
  30. struct glsl_parse_context {
  31. struct glsl_node *root;
  32. glsl_parse_error_cb_t error_cb;
  33. void *scanner; //Opaque handle to lexer context
  34. /* Internal state of the parser's stack allocator */
  35. uint8_t *first_buffer;
  36. uint8_t *cur_buffer_start;
  37. uint8_t *cur_buffer;
  38. uint8_t *cur_buffer_end;
  39. bool error;
  40. };
  41. //
  42. // Create a new node for the AST. All values following 'code' will be
  43. // placed in the node's child array.
  44. //
  45. struct glsl_node *new_glsl_node(struct glsl_parse_context *context, int code, ...) __attribute__ ((sentinel));
  46. //
  47. // Allocate memory in the parser's stack allocator
  48. //
  49. uint8_t *glsl_parse_alloc(struct glsl_parse_context *context, size_t size, int align);
  50. //
  51. // Deallocate all memory previously allocated by glsl_parse_alloc()
  52. // The parser internally uses this allocator so any generated
  53. // AST data will become invalid when glsl_parse_dealloc() is called.
  54. //
  55. void glsl_parse_dealloc(struct glsl_parse_context *context);
  56. //
  57. // Initialize a parsing context
  58. //
  59. void glsl_parse_context_init(struct glsl_parse_context *context);
  60. //
  61. // Set the callback to invoke when a parsing error occurs
  62. //
  63. void glsl_parse_set_error_cb(struct glsl_parse_context *context, glsl_parse_error_cb_t error_cb);
  64. //
  65. // Destroy a parsing context.
  66. //
  67. void glsl_parse_context_destroy(struct glsl_parse_context *context);
  68. //
  69. // Parse the supplied file and generate an AST in context->root.
  70. //
  71. // Returns false if a parsing error occured
  72. //
  73. bool glsl_parse_file(struct glsl_parse_context *context, FILE *file);
  74. //
  75. // Parse the supplied string and generate an AST in context->root.
  76. //
  77. // Returns false if a parsing error occured
  78. //
  79. bool glsl_parse_string(struct glsl_parse_context *context, const char *str);
  80. //
  81. // Include glsl.parse.h to get the enum values that are stored in the 'code'
  82. // field of glsl_node.
  83. //
  84. #include "glsl.parser.h"
  85. #endif