| 1 | /* -*- c-basic-offset: 2 -*- */ |
| 2 | /* |
| 3 | Copyright(C) 2015-2016 Brazil |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License version 2.1 as published by the Free Software Foundation. |
| 8 | |
| 9 | This library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with this library; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | #pragma once |
| 20 | |
| 21 | #include "ts_expr.h" |
| 22 | #include "ts_expr_builder.h" |
| 23 | #include "ts_str.h" |
| 24 | #include "ts_types.h" |
| 25 | |
| 26 | #ifdef __cplusplus |
| 27 | extern "C" { |
| 28 | #endif |
| 29 | |
| 30 | typedef enum { |
| 31 | GRN_TS_EXPR_DUMMY_TOKEN, /* No extra data. */ |
| 32 | GRN_TS_EXPR_START_TOKEN, /* No extra data. */ |
| 33 | GRN_TS_EXPR_END_TOKEN, /* No extra data. */ |
| 34 | GRN_TS_EXPR_CONST_TOKEN, /* +data_kind, content and buf. */ |
| 35 | GRN_TS_EXPR_NAME_TOKEN, /* +name. */ |
| 36 | GRN_TS_EXPR_OP_TOKEN, /* +op_type. */ |
| 37 | GRN_TS_EXPR_BRIDGE_TOKEN, /* No extra data. */ |
| 38 | GRN_TS_EXPR_BRACKET_TOKEN /* No extra data. */ |
| 39 | } grn_ts_expr_token_type; |
| 40 | |
| 41 | #define GRN_TS_EXPR_TOKEN_COMMON_MEMBERS\ |
| 42 | grn_ts_str src; /* Source string. */\ |
| 43 | grn_ts_expr_token_type type; /* Token type. */ |
| 44 | |
| 45 | typedef struct { |
| 46 | GRN_TS_EXPR_TOKEN_COMMON_MEMBERS |
| 47 | } grn_ts_expr_token; |
| 48 | |
| 49 | typedef grn_ts_expr_token grn_ts_expr_dummy_token; |
| 50 | typedef grn_ts_expr_token grn_ts_expr_start_token; |
| 51 | typedef grn_ts_expr_token grn_ts_expr_end_token; |
| 52 | |
| 53 | typedef struct { |
| 54 | GRN_TS_EXPR_TOKEN_COMMON_MEMBERS |
| 55 | grn_ts_data_kind data_kind; /* The data kind of the const. */ |
| 56 | grn_ts_any content; /* The const. */ |
| 57 | grn_ts_buf buf; /* Buffer for content.as_text. */ |
| 58 | } grn_ts_expr_const_token; |
| 59 | |
| 60 | typedef grn_ts_expr_token grn_ts_expr_name_token; |
| 61 | |
| 62 | typedef struct { |
| 63 | GRN_TS_EXPR_TOKEN_COMMON_MEMBERS |
| 64 | grn_ts_op_type op_type; /* Operator type. */ |
| 65 | } grn_ts_expr_op_token; |
| 66 | |
| 67 | typedef grn_ts_expr_token grn_ts_expr_bridge_token; |
| 68 | typedef grn_ts_expr_token grn_ts_expr_bracket_token; |
| 69 | |
| 70 | typedef struct { |
| 71 | grn_ts_expr_builder *builder; /* Builder. */ |
| 72 | grn_ts_buf str_buf; /* Buffer for a source string. */ |
| 73 | grn_ts_expr_token **tokens; /* Tokens. */ |
| 74 | size_t n_tokens; /* Number of tokens. */ |
| 75 | size_t max_n_tokens; /* Maximum number of tokens. */ |
| 76 | grn_ts_expr_dummy_token *dummy_tokens; /* Dummy tokens. */ |
| 77 | size_t n_dummy_tokens; /* Number of dummy tokens. */ |
| 78 | size_t max_n_dummy_tokens; /* Maximum number of dummy tokens. */ |
| 79 | grn_ts_expr_token **stack; /* Token stack. */ |
| 80 | size_t stack_depth; /* Token stack's current depth. */ |
| 81 | size_t stack_size; /* Token stack's capacity. */ |
| 82 | } grn_ts_expr_parser; |
| 83 | |
| 84 | /* grn_ts_expr_parser_open() creates a parser. */ |
| 85 | grn_rc grn_ts_expr_parser_open(grn_ctx *ctx, grn_obj *table, |
| 86 | grn_ts_expr_parser **parser); |
| 87 | |
| 88 | /* grn_ts_expr_parser_close() destroys a parser. */ |
| 89 | grn_rc grn_ts_expr_parser_close(grn_ctx *ctx, grn_ts_expr_parser *parser); |
| 90 | |
| 91 | /* grn_ts_expr_parser_parse() parses a string and creates an expression. */ |
| 92 | grn_rc grn_ts_expr_parser_parse(grn_ctx *ctx, grn_ts_expr_parser *parser, |
| 93 | grn_ts_str str, grn_ts_expr **expr); |
| 94 | |
| 95 | /* |
| 96 | * grn_ts_expr_parser_split() splits comma-separated strings into the first |
| 97 | * expression and the rest. |
| 98 | * Note that if `str` is empty, this function returns GRN_END_OF_DATA. |
| 99 | */ |
| 100 | grn_rc grn_ts_expr_parser_split(grn_ctx *ctx, grn_ts_expr_parser *parser, |
| 101 | grn_ts_str str, grn_ts_str *first, |
| 102 | grn_ts_str *rest); |
| 103 | |
| 104 | #ifdef __cplusplus |
| 105 | } |
| 106 | #endif |
| 107 | |
| 108 | |