glsl: Correctly handle line numbering.

This commit is contained in:
Michal Krol 2009-09-04 15:16:21 +02:00
parent 0d9c5eafeb
commit 4aa3222df3
8 changed files with 81 additions and 7 deletions

View File

@ -37,6 +37,7 @@ sl_pp_context_init(struct sl_pp_context *context)
context->if_ptr = SL_PP_MAX_IF_NESTING;
context->if_value = 1;
memset(context->error_msg, 0, sizeof(context->error_msg));
context->line = 1;
}
void

View File

@ -48,6 +48,8 @@ struct sl_pp_context {
int if_value;
char error_msg[SL_PP_MAX_ERROR_MSG];
unsigned int line;
};
void

View File

@ -29,16 +29,44 @@
#include "sl_pp_process.h"
static int
_parse_integer(const char *input,
unsigned int *number)
{
unsigned int n = 0;
while (*input >= '0' && *input <= '9') {
if (n * 10 < n) {
/* Overflow. */
return -1;
}
n = n * 10 + (*input++ - '0');
}
if (*input != '\0') {
/* Invalid decimal number. */
return -1;
}
*number = n;
return 0;
}
int
sl_pp_process_line(struct sl_pp_context *context,
const struct sl_pp_token_info *input,
unsigned int first,
unsigned int last)
unsigned int last,
struct sl_pp_process_state *pstate)
{
unsigned int i;
struct sl_pp_process_state state;
int line_number = -1;
int file_number = -1;
const char *str;
unsigned int line;
memset(&state, 0, sizeof(state));
for (i = first; i < last;) {
@ -89,7 +117,22 @@ sl_pp_process_line(struct sl_pp_context *context,
free(state.out);
/* TODO: Do something with line and file numbers. */
str = sl_pp_context_cstr(context, line_number);
if (_parse_integer(str, &line)) {
return -1;
}
if (context->line != line) {
struct sl_pp_token_info ti;
ti.token = SL_PP_LINE;
ti.data.line = line;
if (sl_pp_process_out(pstate, &ti)) {
return -1;
}
}
/* TODO: Do something with the file number. */
return 0;
}

View File

@ -131,14 +131,14 @@ sl_pp_macro_expand(struct sl_pp_context *context,
macro_name = input[*pi].data.identifier;
macro_str = sl_pp_context_cstr(context, macro_name);
/* TODO: Having the following built-ins hardcoded is a bit lame. */
if (!strcmp(macro_str, "__LINE__")) {
if (!mute && _out_number(context, state, 1)) {
if (!mute && _out_number(context, state, context->line)) {
return -1;
}
(*pi)++;
return 0;
}
/* TODO: Having the following built-ins hardcoded is a bit lame. */
if (!strcmp(macro_str, "__FILE__")) {
if (!mute && _out_number(context, state, 0)) {
return -1;

View File

@ -75,6 +75,21 @@ sl_pp_process(struct sl_pp_context *context,
memset(&state, 0, sizeof(state));
if (context->line > 1) {
struct sl_pp_token_info ti;
ti.token = SL_PP_LINE;
ti.data.line = context->line - 1;
if (sl_pp_process_out(&state, &ti)) {
return -1;
}
ti.token = SL_PP_NEWLINE;
if (sl_pp_process_out(&state, &ti)) {
return -1;
}
}
while (!found_eof) {
skip_whitespace(input, &i);
if (input[i].token == SL_PP_HASH) {
@ -156,7 +171,7 @@ sl_pp_process(struct sl_pp_context *context,
return -1;
}
} else if (!strcmp(name, "line")) {
if (sl_pp_process_line(context, input, first, last)) {
if (sl_pp_process_line(context, input, first, last, &state)) {
return -1;
}
} else if (!strcmp(name, "pragma")) {
@ -176,6 +191,7 @@ sl_pp_process(struct sl_pp_context *context,
if (sl_pp_process_out(&state, &endof)) {
return -1;
}
context->line++;
}
break;
@ -184,6 +200,7 @@ sl_pp_process(struct sl_pp_context *context,
if (sl_pp_process_out(&state, &input[i])) {
return -1;
}
context->line++;
i++;
break;
@ -214,6 +231,7 @@ sl_pp_process(struct sl_pp_context *context,
if (sl_pp_process_out(&state, &input[i])) {
return -1;
}
context->line++;
i++;
found_eol = 1;
break;

View File

@ -116,7 +116,8 @@ int
sl_pp_process_line(struct sl_pp_context *context,
const struct sl_pp_token_info *input,
unsigned int first,
unsigned int last);
unsigned int last,
struct sl_pp_process_state *state);
int
sl_pp_process_out(struct sl_pp_process_state *state,

View File

@ -96,6 +96,8 @@ enum sl_pp_token {
SL_PP_EXTENSION_WARN,
SL_PP_EXTENSION_DISABLE,
SL_PP_LINE,
SL_PP_EOF
};
@ -105,6 +107,7 @@ union sl_pp_token_data {
char other;
int pragma;
int extension;
unsigned int line;
};
struct sl_pp_token_info {

View File

@ -60,6 +60,7 @@ sl_pp_version(struct sl_pp_context *context,
unsigned int *tokens_eaten)
{
unsigned int i = 0;
unsigned int line = context->line;
/* Default values if `#version' is not present. */
*version = 110;
@ -77,8 +78,10 @@ sl_pp_version(struct sl_pp_context *context,
/* Skip whitespace and newlines and seek for hash. */
while (!found_hash) {
switch (input[i].token) {
case SL_PP_WHITESPACE:
case SL_PP_NEWLINE:
line++;
/* pass thru */
case SL_PP_WHITESPACE:
i++;
break;
@ -156,9 +159,12 @@ sl_pp_version(struct sl_pp_context *context,
break;
case SL_PP_NEWLINE:
line++;
/* pass thru */
case SL_PP_EOF:
i++;
*tokens_eaten = i;
context->line = line;
found_end = 1;
break;