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-1335  USA
17 */
18 
19 #pragma once
20 
21 #include "../grn.h"
22 
23 #include "ts_buf.h"
24 #include "ts_expr.h"
25 #include "ts_expr_node.h"
26 #include "ts_op.h"
27 #include "ts_str.h"
28 #include "ts_types.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 typedef struct {
35   grn_obj *src_table;  /* The source table of a bridge (no ref. count). */
36   grn_obj *dest_table; /* The destination table of a bridge. */
37   size_t n_nodes;      /* The stack depth (position) of a bridge. */
38 } grn_ts_expr_bridge;
39 
40 typedef struct {
41   grn_obj *table;              /* Associated table. */
42   grn_obj *curr_table;         /* Current table (no ref. count). */
43   grn_ts_expr_node **nodes;    /* Node stack. */
44   size_t n_nodes;              /* Number of nodes (stack depth). */
45   size_t max_n_nodes;          /* Maximum number of nodes (stack capacity). */
46   grn_ts_expr_bridge *bridges; /* Bridges to subexpressions. */
47   size_t n_bridges;            /* Number of bridges (subexpression depth). */
48   size_t max_n_bridges;        /* Max. number (capacity) of bridges. */
49 } grn_ts_expr_builder;
50 
51 /* grn_ts_expr_builder_open() creates an expression builder. */
52 grn_rc grn_ts_expr_builder_open(grn_ctx *ctx, grn_obj *table,
53                                 grn_ts_expr_builder **builder);
54 
55 /* grn_ts_expr_builder_close() destroys an expression builder. */
56 grn_rc grn_ts_expr_builder_close(grn_ctx *ctx, grn_ts_expr_builder *builder);
57 
58 /* grn_ts_expr_builder_complete() completes an expression. */
59 grn_rc grn_ts_expr_builder_complete(grn_ctx *ctx, grn_ts_expr_builder *builder,
60                                     grn_ts_expr **expr);
61 
62 /* grn_ts_expr_builder_clear() clears the internal states. */
63 grn_rc grn_ts_expr_builder_clear(grn_ctx *ctx, grn_ts_expr_builder *builder);
64 
65 /* grn_ts_expr_builder_push_name() pushes a named object. */
66 grn_rc grn_ts_expr_builder_push_name(grn_ctx *ctx,
67                                      grn_ts_expr_builder *builder,
68                                      grn_ts_str name);
69 
70 /*
71  * grn_ts_expr_builder_push_obj() pushes an object.
72  *
73  * Acceptable objects are as follows:
74  * - Consts
75  *  - GRN_BULK: GRN_DB_*.
76  *  - GRN_UVECTOR: GRN_DB_* except GRN_DB_[SHORT/LONG_]TEXT.
77  *  - GRN_VECTOR: GRN_DB_[SHORT/LONG_]TEXT.
78  * - Columns
79  *  - GRN_ACCESSOR: _id, _score, _key, _value, and columns.
80  *  - GRN_COLUMN_FIX_SIZE: GRN_DB_* except GRN_DB_[SHORT/LONG_]TEXT.
81  *  - GRN_COLUMN_VAR_SIZE: GRN_DB_[SHORT/LONG_]TEXT.
82  */
83 grn_rc grn_ts_expr_builder_push_obj(grn_ctx *ctx, grn_ts_expr_builder *builder,
84                                     grn_obj *obj);
85 
86 /* grn_ts_expr_builder_push_id() pushes "_id". */
87 grn_rc grn_ts_expr_builder_push_id(grn_ctx *ctx, grn_ts_expr_builder *builder);
88 
89 /* grn_ts_expr_builder_push_score() pushes "_score". */
90 grn_rc grn_ts_expr_builder_push_score(grn_ctx *ctx,
91                                       grn_ts_expr_builder *builder);
92 
93 /* grn_ts_expr_builder_push_key() pushes "_key". */
94 grn_rc grn_ts_expr_builder_push_key(grn_ctx *ctx,
95                                     grn_ts_expr_builder *builder);
96 
97 /* grn_ts_expr_builder_push_value() pushes "_value". */
98 grn_rc grn_ts_expr_builder_push_value(grn_ctx *ctx,
99                                       grn_ts_expr_builder *builder);
100 
101 /* grn_ts_expr_builder_push_const() pushes a const. */
102 grn_rc grn_ts_expr_builder_push_const(grn_ctx *ctx,
103                                       grn_ts_expr_builder *builder,
104                                       grn_ts_data_kind kind,
105                                       grn_ts_data_type type,
106                                       grn_ts_any value);
107 
108 /* grn_ts_expr_builder_push_column() pushes a column. */
109 grn_rc grn_ts_expr_builder_push_column(grn_ctx *ctx,
110                                        grn_ts_expr_builder *builder,
111                                        grn_obj *column);
112 
113 /* grn_ts_expr_builder_push_op() pushes an operator. */
114 grn_rc grn_ts_expr_builder_push_op(grn_ctx *ctx, grn_ts_expr_builder *builder,
115                                    grn_ts_op_type op_type);
116 
117 /* grn_ts_expr_builder_begin_subexpr() begins a subexpression. */
118 grn_rc grn_ts_expr_builder_begin_subexpr(grn_ctx *ctx,
119                                          grn_ts_expr_builder *builder);
120 
121 /* grn_ts_expr_builder_end_subexpr() ends a subexpression. */
122 grn_rc grn_ts_expr_builder_end_subexpr(grn_ctx *ctx,
123                                        grn_ts_expr_builder *builder);
124 
125 #ifdef __cplusplus
126 }
127 #endif
128 
129