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
int if_value;
char error_msg[SL_PP_MAX_ERROR_MSG];
+
+ unsigned int line;
};
void
#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;) {
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;
}
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;
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) {
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")) {
if (sl_pp_process_out(&state, &endof)) {
return -1;
}
+ context->line++;
}
break;
if (sl_pp_process_out(&state, &input[i])) {
return -1;
}
+ context->line++;
i++;
break;
if (sl_pp_process_out(&state, &input[i])) {
return -1;
}
+ context->line++;
i++;
found_eol = 1;
break;
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,
SL_PP_EXTENSION_WARN,
SL_PP_EXTENSION_DISABLE,
+ SL_PP_LINE,
+
SL_PP_EOF
};
char other;
int pragma;
int extension;
+ unsigned int line;
};
struct sl_pp_token_info {
unsigned int *tokens_eaten)
{
unsigned int i = 0;
+ unsigned int line = context->line;
/* Default values if `#version' is not present. */
*version = 110;
/* 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;
break;
case SL_PP_NEWLINE:
+ line++;
+ /* pass thru */
case SL_PP_EOF:
i++;
*tokens_eaten = i;
+ context->line = line;
found_end = 1;
break;