struct parse_context {
struct sl_pp_context *context;
- const struct sl_pp_token_info *input;
struct parse_dict dict;
+ struct sl_pp_token_info *tokens;
+ unsigned int tokens_read;
+ unsigned int tokens_cap;
+
unsigned char *out_buf;
unsigned int out_cap;
unsigned int parsing_builtin;
char error[256];
+ int process_error;
};
static void
_error(struct parse_context *ctx,
- char *msg)
+ const char *msg)
{
if (ctx->error[0] == '\0') {
strcpy(ctx->error, msg);
}
+static const struct sl_pp_token_info *
+_fetch_token(struct parse_context *ctx,
+ unsigned int pos)
+{
+ if (ctx->process_error) {
+ return NULL;
+ }
+
+ while (pos >= ctx->tokens_read) {
+ if (ctx->tokens_read == ctx->tokens_cap) {
+ ctx->tokens_cap += 1024;
+ ctx->tokens = realloc(ctx->tokens,
+ ctx->tokens_cap * sizeof(struct sl_pp_token_info));
+ if (!ctx->tokens) {
+ _error(ctx, "out of memory");
+ ctx->process_error = 1;
+ return NULL;
+ }
+ }
+ if (sl_pp_process_get(ctx->context, &ctx->tokens[ctx->tokens_read])) {
+ _error(ctx, sl_pp_context_error_message(ctx->context));
+ ctx->process_error = 1;
+ return NULL;
+ }
+ switch (ctx->tokens[ctx->tokens_read].token) {
+ case SL_PP_COMMA:
+ case SL_PP_SEMICOLON:
+ case SL_PP_LBRACE:
+ case SL_PP_RBRACE:
+ case SL_PP_LPAREN:
+ case SL_PP_RPAREN:
+ case SL_PP_LBRACKET:
+ case SL_PP_RBRACKET:
+ case SL_PP_DOT:
+ case SL_PP_INCREMENT:
+ case SL_PP_ADDASSIGN:
+ case SL_PP_PLUS:
+ case SL_PP_DECREMENT:
+ case SL_PP_SUBASSIGN:
+ case SL_PP_MINUS:
+ case SL_PP_BITNOT:
+ case SL_PP_NOTEQUAL:
+ case SL_PP_NOT:
+ case SL_PP_MULASSIGN:
+ case SL_PP_STAR:
+ case SL_PP_DIVASSIGN:
+ case SL_PP_SLASH:
+ case SL_PP_MODASSIGN:
+ case SL_PP_MODULO:
+ case SL_PP_LSHIFTASSIGN:
+ case SL_PP_LSHIFT:
+ case SL_PP_LESSEQUAL:
+ case SL_PP_LESS:
+ case SL_PP_RSHIFTASSIGN:
+ case SL_PP_RSHIFT:
+ case SL_PP_GREATEREQUAL:
+ case SL_PP_GREATER:
+ case SL_PP_EQUAL:
+ case SL_PP_ASSIGN:
+ case SL_PP_AND:
+ case SL_PP_BITANDASSIGN:
+ case SL_PP_BITAND:
+ case SL_PP_XOR:
+ case SL_PP_BITXORASSIGN:
+ case SL_PP_BITXOR:
+ case SL_PP_OR:
+ case SL_PP_BITORASSIGN:
+ case SL_PP_BITOR:
+ case SL_PP_QUESTION:
+ case SL_PP_COLON:
+ case SL_PP_IDENTIFIER:
+ case SL_PP_UINT:
+ case SL_PP_FLOAT:
+ case SL_PP_EOF:
+ ctx->tokens_read++;
+ break;
+ }
+ }
+ return &ctx->tokens[pos];
+}
+
+
static int
_parse_token(struct parse_context *ctx,
enum sl_pp_token token,
struct parse_state *ps)
{
- if (ctx->input[ps->in].token == token) {
+ const struct sl_pp_token_info *input = _fetch_token(ctx, ps->in);
+
+ if (input && input->token == token) {
ps->in++;
return 0;
}
int id,
struct parse_state *ps)
{
- if (ctx->input[ps->in].token == SL_PP_IDENTIFIER &&
- ctx->input[ps->in].data.identifier == id) {
+ const struct sl_pp_token_info *input = _fetch_token(ctx, ps->in);
+
+ if (input && input->token == SL_PP_IDENTIFIER && input->data.identifier == id) {
ps->in++;
return 0;
}
_parse_identifier(struct parse_context *ctx,
struct parse_state *ps)
{
- if (ctx->input[ps->in].token == SL_PP_IDENTIFIER) {
- const char *cstr = sl_pp_context_cstr(ctx->context, ctx->input[ps->in].data.identifier);
+ const struct sl_pp_token_info *input = _fetch_token(ctx, ps->in);
+
+ if (input && input->token == SL_PP_IDENTIFIER) {
+ const char *cstr = sl_pp_context_cstr(ctx->context, input->data.identifier);
do {
_emit(ctx, &ps->out, *cstr);
_parse_float(struct parse_context *ctx,
struct parse_state *ps)
{
- if (ctx->input[ps->in].token == SL_PP_FLOAT) {
- const char *cstr = sl_pp_context_cstr(ctx->context, ctx->input[ps->in].data._float);
+ const struct sl_pp_token_info *input = _fetch_token(ctx, ps->in);
+
+ if (input && input->token == SL_PP_FLOAT) {
+ const char *cstr = sl_pp_context_cstr(ctx->context, input->data._float);
_emit(ctx, &ps->out, 1);
do {
_parse_uint(struct parse_context *ctx,
struct parse_state *ps)
{
- if (ctx->input[ps->in].token == SL_PP_UINT) {
- const char *cstr = sl_pp_context_cstr(ctx->context, ctx->input[ps->in].data._uint);
+ const struct sl_pp_token_info *input = _fetch_token(ctx, ps->in);
+
+ if (input && input->token == SL_PP_UINT) {
+ const char *cstr = sl_pp_context_cstr(ctx->context, input->data._uint);
_emit(ctx, &ps->out, 1);
do {
struct parse_state *ps)
{
struct parse_state p = *ps;
+ const struct sl_pp_token_info *input = _fetch_token(ctx, p.in);
unsigned int e = _emit(ctx, &p.out, 0);
int id;
- if (ctx->input[p.in].token != SL_PP_IDENTIFIER) {
+ if (!input || input->token != SL_PP_IDENTIFIER) {
return -1;
}
- id = ctx->input[p.in].data.identifier;
+ id = input->data.identifier;
if (id == ctx->dict._const) {
_update(ctx, e, TYPE_QUALIFIER_CONST);
{
struct parse_state p = *ps;
unsigned int e = _emit(ctx, &p.out, 0);
+ const struct sl_pp_token_info *input;
int id;
if (_parse_struct_specifier(ctx, &p) == 0) {
return 0;
}
- if (ctx->input[p.in].token != SL_PP_IDENTIFIER) {
+ input = _fetch_token(ctx, p.in);
+ if (!input || input->token != SL_PP_IDENTIFIER) {
return -1;
}
- id = ctx->input[p.in].data.identifier;
+ id = input->data.identifier;
if (id == ctx->dict._void) {
_update(ctx, e, TYPE_SPECIFIER_VOID);
_parse_precision(struct parse_context *ctx,
struct parse_state *ps)
{
+ const struct sl_pp_token_info *input = _fetch_token(ctx, ps->in);
int id;
unsigned int precision;
- if (ctx->input[ps->in].token != SL_PP_IDENTIFIER) {
+ if (!input || input->token != SL_PP_IDENTIFIER) {
return -1;
}
- id = ctx->input[ps->in].data.identifier;
+ id = input->data.identifier;
if (id == ctx->dict.lowp) {
precision = PRECISION_LOW;
_parse_prectype(struct parse_context *ctx,
struct parse_state *ps)
{
+ const struct sl_pp_token_info *input = _fetch_token(ctx, ps->in);
int id;
unsigned int type;
- if (ctx->input[ps->in].token != SL_PP_IDENTIFIER) {
+ if (!input || input->token != SL_PP_IDENTIFIER) {
return -1;
}
- id = ctx->input[ps->in].data.identifier;
+ id = input->data.identifier;
if (id == ctx->dict._int) {
type = TYPE_SPECIFIER_INT;
int
sl_cl_compile(struct sl_pp_context *context,
- const struct sl_pp_token_info *input,
unsigned int shader_type,
unsigned int parsing_builtin,
unsigned char **output,
struct parse_state ps;
ctx.context = context;
- ctx.input = input;
ADD_NAME_STR(ctx, _void, "void");
ADD_NAME_STR(ctx, _float, "float");
ctx.parsing_builtin = 1;
ctx.error[0] = '\0';
+ ctx.process_error = 0;
+
+ ctx.tokens_cap = 1024;
+ ctx.tokens_read = 0;
+ ctx.tokens = malloc(ctx.tokens_cap * sizeof(struct sl_pp_token_info));
+ if (!ctx.tokens) {
+ strncpy(error, "out of memory", cberror);
+ return -1;
+ }
ps.in = 0;
ps.out = 0;
if (_parse_translation_unit(&ctx, &ps)) {
strncpy(error, ctx.error, cberror);
+ free(ctx.tokens);
return -1;
}
*output = ctx.out_buf;
*cboutput = ps.out;
+ free(ctx.tokens);
return 0;
}
int
sl_cl_compile(struct sl_pp_context *context,
- const struct sl_pp_token_info *input,
unsigned int shader_type,
unsigned int parsing_builtin,
unsigned char **output,
sl_pp_purify_state_init(&context->pure, input, options);
+ memset(&context->process_state, 0, sizeof(context->process_state));
+
return context;
}
sl_pp_macro_free(context->macro);
free(context->getc_buf);
sl_pp_token_buffer_destroy(&context->tokens);
+ free(context->process_state.out);
free(context);
}
}
#include "sl_pp_dict.h"
#include "sl_pp_macro.h"
+#include "sl_pp_process.h"
#include "sl_pp_purify.h"
#include "sl_pp_token_util.h"
unsigned int getc_buf_capacity;
struct sl_pp_token_buffer tokens;
+
+ struct sl_pp_process_state process_state;
};
#endif /* SL_PP_CONTEXT_H */
#include <stdlib.h>
#include <string.h>
+#include "sl_pp_context.h"
#include "sl_pp_process.h"
#include "sl_pp_public.h"
#include <stdlib.h>
#include <string.h>
+#include "sl_pp_context.h"
#include "sl_pp_process.h"
#include "sl_pp_public.h"
#include <stdlib.h>
#include <string.h>
+#include "sl_pp_context.h"
#include "sl_pp_process.h"
#include "sl_pp_public.h"
#include <stdlib.h>
#include <string.h>
+#include "sl_pp_context.h"
#include "sl_pp_public.h"
#include "sl_pp_process.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include "sl_pp_context.h"
#include "sl_pp_public.h"
#include "sl_pp_macro.h"
#include "sl_pp_process.h"
#include <stdlib.h>
#include <string.h>
+#include "sl_pp_context.h"
#include "sl_pp_process.h"
#include <stdlib.h>
#include <string.h>
+#include "sl_pp_context.h"
#include "sl_pp_process.h"
#include "sl_pp_public.h"
}
int
-sl_pp_process(struct sl_pp_context *context,
- struct sl_pp_token_info **output)
+sl_pp_process_get(struct sl_pp_context *context,
+ struct sl_pp_token_info *output)
{
- struct sl_pp_process_state state;
- int found_eof = 0;
-
- memset(&state, 0, sizeof(state));
-
- if (context->line > 1) {
- struct sl_pp_token_info ti;
-
- ti.token = SL_PP_LINE;
- ti.data.line.lineno = context->line - 1;
- ti.data.line.fileno = context->file;
- if (sl_pp_process_out(&state, &ti)) {
- strcpy(context->error_msg, "out of memory");
- return -1;
- }
+ if (!context->process_state.out) {
+ if (context->line > 1) {
+ struct sl_pp_token_info ti;
+
+ ti.token = SL_PP_LINE;
+ ti.data.line.lineno = context->line - 1;
+ ti.data.line.fileno = context->file;
+ if (sl_pp_process_out(&context->process_state, &ti)) {
+ strcpy(context->error_msg, "out of memory");
+ return -1;
+ }
- ti.token = SL_PP_NEWLINE;
- if (sl_pp_process_out(&state, &ti)) {
- strcpy(context->error_msg, "out of memory");
- return -1;
+ ti.token = SL_PP_NEWLINE;
+ if (sl_pp_process_out(&context->process_state, &ti)) {
+ strcpy(context->error_msg, "out of memory");
+ return -1;
+ }
}
}
- while (!found_eof) {
+ for (;;) {
struct sl_pp_token_info input;
+ int found_eof = 0;
+
+ if (context->process_state.out_len) {
+ *output = context->process_state.out[0];
+
+ if (context->process_state.out_len > 1) {
+ unsigned int i;
+
+ for (i = 1; i < context->process_state.out_len; i++) {
+ context->process_state.out[i - 1] = context->process_state.out[i];
+ }
+ }
+ context->process_state.out_len--;
+
+ return 0;
+ }
if (sl_pp_token_buffer_skip_white(&context->tokens, &input)) {
return -1;
int found_eol = 0;
struct sl_pp_token_info endof;
struct sl_pp_token_peek peek;
- int result;
+ int result = 0;
/* Directive name. */
name = input.data.identifier;
sl_pp_process_error(context, peek.tokens, 0, peek.size - 1);
result = -1;
} else if (name == context->dict.extension) {
- result = sl_pp_process_extension(context, peek.tokens, 0, peek.size - 1, &state);
+ result = sl_pp_process_extension(context, peek.tokens, 0, peek.size - 1, &context->process_state);
} else if (name == context->dict.line) {
struct sl_pp_token_buffer buffer;
result = sl_pp_token_peek_to_buffer(&peek, &buffer);
if (result == 0) {
- result = sl_pp_process_line(context, &buffer, &state);
+ result = sl_pp_process_line(context, &buffer, &context->process_state);
sl_pp_token_buffer_destroy(&buffer);
}
} else if (name == context->dict.pragma) {
- result = sl_pp_process_pragma(context, peek.tokens, 0, peek.size - 1, &state);
+ result = sl_pp_process_pragma(context, peek.tokens, 0, peek.size - 1, &context->process_state);
} else if (name == context->dict.undef) {
result = sl_pp_process_undef(context, peek.tokens, 0, peek.size - 1);
} else {
return result;
}
- if (sl_pp_process_out(&state, &endof)) {
+ if (sl_pp_process_out(&context->process_state, &endof)) {
strcpy(context->error_msg, "out of memory");
return -1;
}
case SL_PP_NEWLINE:
/* Empty directive. */
- if (sl_pp_process_out(&state, &input)) {
+ if (sl_pp_process_out(&context->process_state, &input)) {
strcpy(context->error_msg, "out of memory");
return -1;
}
case SL_PP_EOF:
/* Empty directive. */
- if (sl_pp_process_out(&state, &input)) {
+ if (sl_pp_process_out(&context->process_state, &input)) {
strcpy(context->error_msg, "out of memory");
return -1;
}
case SL_PP_NEWLINE:
/* Preserve newline just for the sake of line numbering. */
- if (sl_pp_process_out(&state, &input)) {
+ if (sl_pp_process_out(&context->process_state, &input)) {
strcpy(context->error_msg, "out of memory");
return -1;
}
break;
case SL_PP_EOF:
- if (sl_pp_process_out(&state, &input)) {
+ if (sl_pp_process_out(&context->process_state, &input)) {
strcpy(context->error_msg, "out of memory");
return -1;
}
case SL_PP_IDENTIFIER:
sl_pp_token_buffer_unget(&context->tokens, &input);
- if (sl_pp_macro_expand(context, &context->tokens, NULL, &state,
+ if (sl_pp_macro_expand(context, &context->tokens, NULL, &context->process_state,
context->if_value ? sl_pp_macro_expand_normal : sl_pp_macro_expand_mute)) {
return -1;
}
default:
if (context->if_value) {
- if (sl_pp_process_out(&state, &input)) {
+ if (sl_pp_process_out(&context->process_state, &input)) {
strcpy(context->error_msg, "out of memory");
return -1;
}
}
}
}
- }
- if (context->if_ptr != SL_PP_MAX_IF_NESTING) {
- strcpy(context->error_msg, "expected `#endif' directive");
- return -1;
+ if (found_eof) {
+ if (context->if_ptr != SL_PP_MAX_IF_NESTING) {
+ strcpy(context->error_msg, "expected `#endif' directive");
+ return -1;
+ }
+ }
}
+}
- *output = state.out;
- return 0;
+int
+sl_pp_process(struct sl_pp_context *context,
+ struct sl_pp_token_info **output)
+{
+ struct sl_pp_process_state state;
+
+ memset(&state, 0, sizeof(state));
+ for (;;) {
+ struct sl_pp_token_info input;
+
+ if (sl_pp_process_get(context, &input)) {
+ free(state.out);
+ return -1;
+ }
+ if (sl_pp_process_out(&state, &input)) {
+ free(state.out);
+ return -1;
+ }
+ if (input.token == SL_PP_EOF) {
+ *output = state.out;
+ return 0;
+ }
+ }
}
#ifndef SL_PP_PROCESS_H
#define SL_PP_PROCESS_H
-#include "sl_pp_context.h"
#include "sl_pp_macro.h"
#include "sl_pp_token.h"
+struct sl_pp_context;
+
struct sl_pp_process_state {
struct sl_pp_token_info *out;
unsigned int out_len;
sl_pp_version(struct sl_pp_context *context,
unsigned int *version);
+int
+sl_pp_process_get(struct sl_pp_context *context,
+ struct sl_pp_token_info *output);
+
int
sl_pp_process(struct sl_pp_context *context,
struct sl_pp_token_info **output);