1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
20 #include "CQLParser.h"
27 condition = ModelCondition::PREDICATE_EQ;
30 std::vector<std::string> CCQLParser::tokenize(IN const std::string &input)
32 std::vector<std::string> temp;
36 std::vector<std::string> tokens_temp;
37 std::string arrage_query = input;
38 arrage_query = check_Predicate(arrage_query);
39 for (unsigned int i = 0 ; i < temp.size(); i++)
43 tokens_temp = getTokens(arrage_query, temp.at(i));
45 for (unsigned int j = 0 ; j < tokens_temp.size() ; j++)
47 arrage_query += tokens_temp.at(j) + " ";
48 //cout << "element = " << tokens_temp.at(j) << endl;
54 void CCQLParser::parse(IN std::string input, OUT Token *root)
56 std::vector<std::string> tokens = tokenize(input);
57 bool flag;//get,sub,if
58 bool value_flag = false;
60 for (unsigned int i = 0 ; i < tokens.size() ; i++)
62 if ( tolower(tokens.at(i)) == "get" || tolower(tokens.at(i)) == "subscribe"
63 || tolower(tokens.at(i)) == "if")
67 if (i != tokens.size() - 1 && tolower(tokens.at(i)) == "subscribe"
68 && tolower(tokens.at(i + 1)) == "if")
71 temp.name = tokens.at(i);
72 root->child_token.push_back(temp);
73 temp.name = tokens.at(i + 1);
76 //temp1.type = Command;
77 //temp1.name = tokens.at(i+1);
90 temp.name = tokens.at(i);
91 root->child_token.push_back(temp);
94 temp.name = tokens.at(i);
95 if (tokens.at(i) == "if")
110 if (value_flag == true)
116 if ( i >= tokens.size() || tolower(tokens.at(i) ) == "if")
122 //enum condition {EQ,GTE,GT,LT,LTE,NEQ};
124 temp1.name = tokens.at(i);
127 if (tokens.at(i) == "=" || tokens.at(i) == "==")
129 temp1.condition = ModelCondition::PREDICATE_EQ;
134 else if (tokens.at(i) == ">=")
136 temp1.condition = ModelCondition::PREDICATE_GTE;
140 else if (tokens.at(i) == ">")
142 temp1.condition = ModelCondition::PREDICATE_GT;
146 else if (tokens.at(i) == "<")
148 temp1.condition = ModelCondition::PREDICATE_LT;
152 else if (tokens.at(i) == "<=")
154 temp1.condition = ModelCondition::PREDICATE_LTE;
158 else if (tokens.at(i) == "!=")
160 temp1.condition = ModelCondition::PREDICATE_NEQ;
166 temp1.type = Context;
173 if (tolower(tokens.at(i)) == "and" || tolower(tokens.at(i)) == "or")
178 split(tokens.at(i), &temp1, flag );//false -> Property
179 temp.child_token.push_back(temp1);
183 split(tokens.at(i), &temp1, flag , tokens.at(i + 1), tokens.at(i + 2)); //false -> Property
185 temp.child_token.push_back(temp1);
189 root->child_token.push_back(temp);
194 std::string CCQLParser::tolower(IN std::string str)
196 for (unsigned int i = 0 ; i < str.size() ; i++)
198 if (str[i] <= 'Z' && str[i] >= 'A')
207 std::vector<std::string> CCQLParser::getTokens(IN const std::string &str,
208 IN const std::string &delimiters)
210 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
211 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
212 std::vector<std::string> tokens;
214 while (std::string::npos != pos || std::string::npos != lastPos)
216 tokens.push_back(str.substr(lastPos, pos - lastPos));
217 lastPos = str.find_first_not_of(delimiters, pos);
218 pos = str.find_first_of(delimiters, lastPos);
224 void CCQLParser::check_index(IN std::string input, OUT Token *token)
226 std::vector<std::string> tokens = getTokens(input, "[");
228 if (tokens.size() == 2)
230 if (tolower(tokens.at(1)) == "all]")
232 token->name = tokens.at(0);
233 token->number = INDEX_ALL;
236 else if (tolower(tokens.at(1)) == "this]")
238 token->name = tokens.at(0);
239 token->number = INDEX_THIS;
244 int num = atoi(tokens.at(1).c_str());
245 token->name = tokens.at(0);
251 void CCQLParser::split(IN std::string input, IN Token *root, bool flag, IN std::string arg1,
254 std::vector<std::string> tokens = getTokens(input, ".");
255 Token *temp_token = root;
257 for (unsigned int j = 0 ; j < tokens.size() ; j++)
261 temp_token->name = tokens.at(j);
262 check_index(tokens.at(j), temp_token);
266 if (j == tokens.size() - 1 && flag == false)
268 ModelProperty property;
269 property.propertyName = tokens.at(j);
273 std::vector<std::string> tokens_temp = getTokens(arg2, "\"");
275 property.propertyValue = tokens_temp.at(0);
276 property.propertyType = ModelProperty::TYPE_TEXT;
278 temp_token->model_property = property;
280 //root->child_token.push_back(temp1);
283 else if (check_number(arg2))
285 //temp1->number = atof(arg2.c_str());
287 // 0 text /1 integer /2 real
288 if (check_number(arg2) == TYPEINTEGER)
290 property.propertyType = ModelProperty::TYPE_INTEGER;
292 else if (check_number(arg2) == TYPEREAL)
294 property.propertyType = ModelProperty::TYPE_REAL;
296 property.propertyValue = arg2;
298 temp_token->model_property = property;
304 temp1.type = Context;
305 temp1.name = tokens.at(j);
306 check_index(tokens.at(j), &temp1);
307 temp_token->child_token.push_back(temp1);
308 temp_token = &(temp_token->child_token.back());
315 int CCQLParser::check_number(IN std::string &str)
317 //int flag = 0; // 0 text /1 integer /2 real
320 for (unsigned int i = 0 ; i < str.size(); i++)
326 else if (isdigit(str[i]))
350 std::string CCQLParser::check_Predicate(IN std::string input)
352 std::string temp = "";
353 for (unsigned int i = 0 ; i < input.size() ; i++)
364 if (input[i - 1] != '=' && input[i - 1] != '!' && input[i - 1] != '>' && input[i - 1] != '<'
365 && input[i - 1] != ' ')
374 if (input[i - 1] != ' ')
384 if (input[i - 1] == '=' || input[i - 1] == '<' || input[i - 1] == '>')
396 bool CCQLParser::check_grammer(IN Token *token)
398 if (token->child_token.size() == 1 && tolower(token->child_token.at(0).name) == "get")
400 if (token->child_token.at(0).child_token.size() > 0)
410 else if (token->child_token.size() == 2)
412 if (token->child_token.at(1).child_token.size() > 0)
414 if (tolower(token->child_token.at(0).name) == "subscribe")
418 else if (tolower(token->child_token.at(0).name) == "get")
420 if (token->child_token.at(0).child_token.size() > 0)