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