/* Copyright 2005-2006 Garrett Rooney. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef TMPL_COMMON_H #define TMPL_COMMON_H #include "etl_template.h" #include "etl_variables.h" #include "lexer.h" typedef enum { ETL_NODE_PRINT, ETL_NODE_IF, ETL_NODE_UNLESS, ETL_NODE_FOR, ETL_NODE_MACRO, ETL_NODE_DEFMACRO, ETL_NODE_LITERAL, ETL_NODE_FILTER, ETL_NODE_INCLUDE } etl_template_node_type_t; typedef struct etl_template_node_t etl_template_node_t; typedef enum { ETL_EXPRESSION_VAR, ETL_EXPRESSION_NODE, ETL_EXPRESSION_INDEX, ETL_EXPRESSION_METHOD, ETL_EXPRESSION_LITERAL } etl_expression_type_t; typedef enum { ETL_EXPRESSION_OP_EQ, /* == */ ETL_EXPRESSION_OP_NEQ, /* != */ ETL_EXPRESSION_OP_LT, /* < */ ETL_EXPRESSION_OP_GT, /* > */ ETL_EXPRESSION_OP_LTEQ, /* <= */ ETL_EXPRESSION_OP_GTEQ, /* >= */ ETL_EXPRESSION_OP_MOD, /* % */ ETL_EXPRESSION_OP_PLUS, /* + */ ETL_EXPRESSION_OP_MINUS, /* - */ ETL_EXPRESSION_OP_TIMES, /* * */ ETL_EXPRESSION_OP_DIV /* / */ } etl_expression_op_t; typedef struct etl_expression_t { etl_expression_type_t type; union { /* literal variable name */ char *v; /* a node in an expression tree */ struct { etl_expression_op_t op; struct etl_expression_t *left; struct etl_expression_t *right; } *n; /* first[second] */ struct { struct etl_expression_t *first; struct etl_expression_t *second; } *idx; etl_variable_t *lit; struct { struct etl_expression_t *invocant; char *name; etl_array_t *args; } method; } contents; } etl_expression_t; typedef struct { etl_expression_t *var; etl_template_node_t *yes_task; etl_template_node_t *no_task; } etl_if_contents_t; typedef enum { ETL_FOR_VAR, ETL_FOR_RANGE } etl_for_type_t; typedef struct { etl_for_type_t type; char *var; char *var2; /* For the value when looping over k,v in a hash. */ union { etl_expression_t *arr; struct { etl_expression_t *from; etl_expression_t *to; } range; } over; etl_template_node_t *task; } etl_for_contents_t; typedef struct { etl_expression_t *var; } etl_print_contents_t; typedef struct { char *data; } etl_literal_contents_t; typedef enum { etl_template_filter_html = 0, etl_template_filter_xml = 1, etl_template_filter_url = 2 } etl_template_filter_type_t; typedef struct { etl_expression_t *var; etl_template_filter_type_t filter; } etl_filter_contents_t; typedef struct { etl_expression_t *file; } etl_include_contents_t; typedef struct { char *name; etl_array_t *arg_names; etl_array_t *arg_values; etl_template_node_t *body; int refcount; } etl_macro_contents_t; struct etl_template_node_t { etl_template_node_type_t type; /* note: contents with a size == sizeof (void *) are stored inline, * others get stored as a pointer so we can minimize the size * of the contents union. */ union { etl_if_contents_t *i; etl_for_contents_t *f; etl_print_contents_t p; etl_literal_contents_t l; etl_filter_contents_t *flt; etl_include_contents_t inc; etl_macro_contents_t *macro; } contents; etl_template_node_t *next; }; struct etl_template_t { etl_template_node_t *tree; etl_hash_t *macros; }; typedef struct list_entry_t { void *data; struct list_entry_t *next; } list_entry_t; typedef struct { etl_template_t *tmpl; etl_error_t *err; etl_hash_t *macros; struct list_entry_t *spare; } etl_template_parser_state_t; void *etl_template_parser_alloc(void *(*alloc_func)(size_t)); void etl_template_parser_parse(void *parser, int token, etl_token_t *data, etl_template_parser_state_t *state); void etl_template_parser_free(void *parser, void (*free_func)(void *)); /* Force Lemon to produce the kind of symbols I want... */ #define etl_template_parser_parseAlloc etl_template_parser_alloc #define etl_template_parser_parseFree etl_template_parser_free #define etl_template_parser_parseTokenName etl_template_parser_token_name #define etl_template_parser_parseTrace etl_template_parser_trace #endif /* TMPL_COMMON_H */