1 /**************************************************************************
3 xserver-xorg-video-exynos
5 Copyright 2010 - 2011 Samsung Electronics co., Ltd. All Rights Reserved.
7 Contact: Boram Park <boram1288.park@samsung.com>
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sub license, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
17 The above copyright notice and this permission notice (including the
18 next paragraph) shall be included in all copies or substantial portions
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
24 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
25 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 **************************************************************************/
36 #include "bool_exp_tokenizer.h"
37 #include "bool_exp_parser.h"
40 typedef struct _TOKEN_DATA * TOKEN_DATA;
46 const char * last_symbol;
50 static BINARY_TREE_NODE bool_exp_parse_line (BINARY_TREE tree, TOKEN_DATA token);
55 #define process_token(t) _process_token(t, __LINE__)
56 static void _process_token (TOKEN_DATA token, int line)
58 static void process_token (TOKEN_DATA token)
63 token->last_symbol = *(token->string);
64 token->last_token = get_next_token (token->string);
65 token->symbol_len = *(token->string) - token->last_symbol;
68 while (token->last_token == BET_SPACE);
70 printf ("token : %d remained string : [%s] (line:%d)\n", token->last_token, *token->string, line);
74 static BINARY_TREE_NODE bool_exp_parse_statement (BINARY_TREE tree, TOKEN_DATA token)
76 BINARY_TREE_NODE node = NULL;
77 PARSE_DATA data = NULL;
80 printf ("%s:%d (token->last_token %d)\n", __FILE__, __LINE__, token->last_token);
83 if (token->last_token == BET_L_BR)
85 process_token (token);
87 node = bool_exp_parse_line (tree, token);
93 if (token->last_token != BET_R_BR)
97 process_token (token);
102 if (token->last_token != BET_SYMBOL)
105 node = bintree_create_node (tree);
107 data = (PARSE_DATA) bintree_get_node_data (node);
109 strncpy (data->variable_name, token->last_symbol, token->symbol_len);
110 data->variable_name[token->symbol_len] = '\0';
112 if (!strcasecmp (data->variable_name, "all"))
114 data->node_type = ALL;
115 process_token (token);
120 data->node_type = DATA;
122 process_token (token);
124 switch (token->last_token)
127 data->compare = NOT_EQ;
130 data->compare = EQUAL;
133 data->compare = LESS;
136 data->compare = LESS_EQ;
139 data->compare = GREATER;
142 data->compare = GREATER_EQ;
148 process_token (token);
150 if (token->last_token == BET_NUMBER)
152 data->value_type = INTEGER;
153 data->value.integer = atoi (token->last_symbol);
155 else if (token->last_token == BET_SYMBOL)
157 data->value_type = STRING;
158 strncpy (data->value.string, token->last_symbol, token->symbol_len);
159 data->value.string[token->symbol_len] = '\0';
166 process_token (token);
172 bintree_remove_node_recursive (node);
177 static BINARY_TREE_NODE bool_exp_parse_line (BINARY_TREE tree, TOKEN_DATA token)
179 BINARY_TREE_NODE node = NULL;
180 BINARY_TREE_NODE left = NULL;
181 BINARY_TREE_NODE right = NULL;
186 printf ("%s:%d\n", __FILE__, __LINE__);
189 node = bool_exp_parse_statement (tree, token);
192 printf ("PARSE statement error\n");
196 while (token->last_token == BET_AND)
201 process_token (token);
202 right = bool_exp_parse_statement (tree, token);
206 node = bintree_create_node (tree);
208 data = (PARSE_DATA) bintree_get_node_data (node);
209 data->node_type = AND;
210 bintree_set_left_child (node, left);
211 bintree_set_right_child (node, right);
214 if (token->last_token == BET_OR)
219 process_token (token);
220 right = bool_exp_parse_line (tree, token);
224 node = bintree_create_node (tree);
226 data = (PARSE_DATA) bintree_get_node_data (node);
227 data->node_type = OR;
228 bintree_set_left_child (node, left);
229 bintree_set_right_child (node, right);
236 bintree_remove_node_recursive (left);
240 BINARY_TREE bool_exp_parse (const char * string)
242 BINARY_TREE tree = bintree_create_tree (sizeof (struct _PARSE_DATA));
243 BINARY_TREE_NODE node;
245 struct _TOKEN_DATA token;
250 token.string = &string;
251 process_token (&token);
253 node = bool_exp_parse_line (tree, &token);
256 bintree_destroy_tree (tree);
260 bintree_set_head (tree, node);