}
if (!context->cstr_pool) {
+ strcpy(context->error_msg, "out of memory");
return -1;
}
unsigned int if_stack[SL_PP_MAX_IF_NESTING];
unsigned int if_ptr;
- int if_value;
+ unsigned int if_value;
char error_msg[SL_PP_MAX_ERROR_MSG];
static int
-_parse_formal_args(const struct sl_pp_token_info *input,
+_parse_formal_args(struct sl_pp_context *context,
+ const struct sl_pp_token_info *input,
unsigned int *first,
unsigned int last,
struct sl_pp_macro *macro)
return 0;
}
} else {
- /* Expected either an identifier or `)'. */
+ strcpy(context->error_msg, "expected either an identifier or `)'");
return -1;
}
for (;;) {
if (*first < last && input[*first].token != SL_PP_IDENTIFIER) {
- /* Expected an identifier. */
+ strcpy(context->error_msg, "expected an identifier");
return -1;
}
*arg = malloc(sizeof(struct sl_pp_macro_formal_arg));
if (!*arg) {
+ strcpy(context->error_msg, "out of memory");
return -1;
}
(*first)++;
return 0;
} else {
- /* Expected either `,' or `)'. */
+ strcpy(context->error_msg, "expected either `,' or `)'");
return -1;
}
} else {
- /* Expected either `,' or `)'. */
+ strcpy(context->error_msg, "expected either `,' or `)'");
return -1;
}
}
+
+ /* Should not gete here. */
}
first++;
}
if (macro_name == -1) {
+ strcpy(context->error_msg, "expected an identifier");
return -1;
}
if (!macro) {
macro = sl_pp_macro_new();
if (!macro) {
+ strcpy(context->error_msg, "out of memory");
return -1;
}
*/
if (first < last && input[first].token == SL_PP_LPAREN) {
first++;
- if (_parse_formal_args(input, &first, last, macro)) {
+ if (_parse_formal_args(context, input, &first, last, macro)) {
return -1;
}
}
macro->body = malloc(sizeof(struct sl_pp_token_info) * body_len);
if (!macro->body) {
+ strcpy(context->error_msg, "out of memory");
return -1;
}
ctx->input++;
} else {
if (ctx->input->token != SL_PP_LPAREN) {
+ strcpy(ctx->context->error_msg, "expected `('");
return -1;
}
ctx->input++;
return -1;
}
if (ctx->input->token != SL_PP_RPAREN) {
+ strcpy(ctx->context->error_msg, "expected `)'");
return -1;
}
ctx->input++;
}
if (input[*pi].token != SL_PP_IDENTIFIER) {
- /* Identifier expected. */
+ strcpy(context->error_msg, "expected an identifier");
return -1;
}
if (parens) {
skip_whitespace(input, pi);
if (input[*pi].token != SL_PP_RPAREN) {
- /* `)' expected */
+ strcpy(context->error_msg, "expected `)'");
return -1;
}
(*pi)++;
return -1;
}
- return sl_pp_process_out(state, &result);
+ if (sl_pp_process_out(state, &result)) {
+ strcpy(context->error_msg, "out of memory");
+ return -1;
+ }
+
+ return 0;
}
-static int
+static unsigned int
_evaluate_if_stack(struct sl_pp_context *context)
{
unsigned int i;
int result;
if (!context->if_ptr) {
- /* #if nesting too deep. */
+ strcpy(context->error_msg, "`#if' nesting too deep");
return -1;
}
default:
if (sl_pp_process_out(&state, &input[i])) {
+ strcpy(context->error_msg, "out of memory");
free(state.out);
return -1;
}
eof.token = SL_PP_EOF;
if (sl_pp_process_out(&state, &eof)) {
+ strcpy(context->error_msg, "out of memory");
free(state.out);
return -1;
}
_parse_else(struct sl_pp_context *context)
{
if (context->if_ptr == SL_PP_MAX_IF_NESTING) {
- /* No matching #if. */
+ strcpy(context->error_msg, "no matching `#if'");
return -1;
}
/* Bit b1 indicates we already went through #else. */
if (context->if_stack[context->if_ptr] & 2) {
- /* No matching #if. */
+ strcpy(context->error_msg, "no matching `#if'");
return -1;
}
unsigned int i;
if (!context->if_ptr) {
- /* #if nesting too deep. */
+ strcpy(context->error_msg, "`#if' nesting too deep");
return -1;
}
break;
default:
- /* Expected an identifier. */
+ strcpy(context->error_msg, "expected an identifier");
return -1;
}
}
- /* Expected an identifier. */
+ strcpy(context->error_msg, "expected an identifier");
return -1;
}
unsigned int i;
if (!context->if_ptr) {
- /* #if nesting too deep. */
+ strcpy(context->error_msg, "`#if' nesting too deep");
return -1;
}
break;
default:
- /* Expected an identifier. */
+ strcpy(context->error_msg, "expected an identifier");
return -1;
}
}
- /* Expected an identifier. */
+ strcpy(context->error_msg, "expected an identifier");
return -1;
}
unsigned int last)
{
if (context->if_ptr == SL_PP_MAX_IF_NESTING) {
- /* No matching #if. */
+ strcpy(context->error_msg, "no matching `#if'");
return -1;
}
#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,
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));
default:
if (sl_pp_process_out(&state, &input[i])) {
+ strcpy(context->error_msg, "out of memory");
free(state.out);
return -1;
}
if (state.out_len > 0 && state.out[0].token == SL_PP_NUMBER) {
line_number = state.out[0].data.number;
} else {
- strcpy(context->error_msg, "expected number after `#line'");
+ strcpy(context->error_msg, "expected a number after `#line'");
free(state.out);
return -1;
}
if (state.out[1].token == SL_PP_NUMBER) {
file_number = state.out[1].data.number;
} else {
- strcpy(context->error_msg, "expected number after line number");
+ strcpy(context->error_msg, "expected a number after line number");
free(state.out);
return -1;
}
if (state.out_len > 2) {
- strcpy(context->error_msg, "expected end of line after file number");
+ strcpy(context->error_msg, "expected an end of line after file number");
free(state.out);
return -1;
}
free(state.out);
- str = sl_pp_context_cstr(context, line_number);
- if (_parse_integer(str, &line)) {
- return -1;
- }
+ line = atoi(sl_pp_context_cstr(context, line_number));
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)) {
+ strcpy(context->error_msg, "out of memory");
return -1;
}
if (file_number != -1) {
unsigned int file;
- str = sl_pp_context_cstr(context, file_number);
- if (_parse_integer(str, &file)) {
- return -1;
- }
+ file_number = atoi(sl_pp_context_cstr(context, file_number));
if (context->file != file) {
struct sl_pp_token_info ti;
ti.token = SL_PP_FILE;
ti.data.file = file;
if (sl_pp_process_out(pstate, &ti)) {
+ strcpy(context->error_msg, "out of memory");
return -1;
}
ti.token = SL_PP_NUMBER;
ti.data.number = sl_pp_context_add_unique_str(context, buf);
- return sl_pp_process_out(state, &ti);
+ if (sl_pp_process_out(state, &ti)) {
+ strcpy(context->error_msg, "out of memory");
+ return -1;
+ }
+
+ return 0;
}
int
unsigned int j;
if (input[*pi].token != SL_PP_IDENTIFIER) {
+ strcpy(context->error_msg, "expected an identifier");
return -1;
}
if (!macro) {
if (!mute) {
if (sl_pp_process_out(state, &input[*pi])) {
+ strcpy(context->error_msg, "out of memory");
return -1;
}
}
if (macro->num_args >= 0) {
skip_whitespace(input, pi);
if (input[*pi].token != SL_PP_LPAREN) {
+ strcpy(context->error_msg, "expected `('");
return -1;
}
(*pi)++;
*pmacro = sl_pp_macro_new();
if (!*pmacro) {
+ strcpy(context->error_msg, "out of memory");
return -1;
}
if (j < (unsigned int)macro->num_args - 1) {
done = 1;
} else {
+ strcpy(context->error_msg, "too many actual macro arguments");
return -1;
}
} else {
if (j == (unsigned int)macro->num_args - 1) {
done = 1;
} else {
+ strcpy(context->error_msg, "too few actual macro arguments");
return -1;
}
} else {
break;
case SL_PP_EOF:
+ strcpy(context->error_msg, "too few actual macro arguments");
return -1;
default:
(**pmacro).body = malloc(sizeof(struct sl_pp_token_info) * body_len);
if (!(**pmacro).body) {
+ strcpy(context->error_msg, "out of memory");
return -1;
}
if (macro->num_args == 0) {
skip_whitespace(input, pi);
if (input[*pi].token != SL_PP_RPAREN) {
+ strcpy(context->error_msg, "expected `)'");
return -1;
}
(*pi)++;
switch (macro->body[j].token) {
case SL_PP_NEWLINE:
if (sl_pp_process_out(state, ¯o->body[j])) {
+ strcpy(context->error_msg, "out of memory");
return -1;
}
j++;
default:
if (!mute) {
if (sl_pp_process_out(state, ¯o->body[j])) {
+ strcpy(context->error_msg, "out of memory");
return -1;
}
}
/* Ignore the tokens that follow. */
if (sl_pp_process_out(state, &out)) {
+ strcpy(context->error_msg, "out of memory");
return -1;
}
*
**************************************************************************/
+#include <stdlib.h>
#include "sl_pp_version.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_version(struct sl_pp_context *context,
const struct sl_pp_token_info *input,
break;
case SL_PP_NUMBER:
- {
- const char *num = sl_pp_context_cstr(context, input[i].data.number);
-
- if (!num) {
- return -1;
- }
- if (_parse_integer(num, version)) {
- strcpy(context->error_msg, "expected version number after `#version'");
- return -1;
- }
- i++;
- found_number = 1;
- }
+ *version = atoi(sl_pp_context_cstr(context, input[i].data.number));
+ i++;
+ found_number = 1;
break;
default: