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(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 bool CCQLParser::parse(std::string input, Token *root)
56 std::vector<std::string> tokens = tokenize(input);
57 bool flag;//get,sub,if
58 bool value_flag = false;
59 bool isCondition = false;
61 for (unsigned int i = 0 ; i < tokens.size() ; i++)
63 if ( tolower(tokens.at(i)) == "get" || tolower(tokens.at(i)) == "subscribe"
64 || tolower(tokens.at(i)) == "if")
68 if (i != tokens.size() - 1 && tolower(tokens.at(i)) == "subscribe"
69 && tolower(tokens.at(i + 1)) == "if")
72 temp.name = tokens.at(i);
73 root->child_token.push_back(temp);
74 temp.name = tokens.at(i + 1);
77 //temp1.type = Command;
78 //temp1.name = tokens.at(i+1);
92 temp.name = tokens.at(i);
93 root->child_token.push_back(temp);
96 temp.name = tokens.at(i);
97 if (tokens.at(i) == "if")
108 SORT lastType = Command;
114 if (value_flag == true)
121 if ( i >= tokens.size() || tolower(tokens.at(i) ) == "if")
127 //enum condition {EQ,GTE,GT,LT,LTE,NEQ};
129 temp1.name = tokens.at(i);
132 if (tokens.at(i) == "=" || tokens.at(i) == "==")
134 temp1.condition = ModelCondition::PREDICATE_EQ;
139 else if (tokens.at(i) == ">=")
141 temp1.condition = ModelCondition::PREDICATE_GTE;
145 else if (tokens.at(i) == ">")
147 temp1.condition = ModelCondition::PREDICATE_GT;
151 else if (tokens.at(i) == "<")
153 temp1.condition = ModelCondition::PREDICATE_LT;
157 else if (tokens.at(i) == "<=")
159 temp1.condition = ModelCondition::PREDICATE_LTE;
163 else if (tokens.at(i) == "!=")
165 temp1.condition = ModelCondition::PREDICATE_NEQ;
171 temp1.type = Context;
173 //if current context token is reserved keyword, that return error
174 if (tolower(tokens.at(i)) == "get" || tolower(tokens.at(i)) == "subscribe")
183 if (tolower(tokens.at(i)) == "and" || tolower(tokens.at(i)) == "or")
189 if (split(tokens.at(i), &temp1, flag ) != true) //false -> Property
196 //token count not matched, return and let grammer checker detects
197 if (tokens.size() < i + 3)
202 if (split(tokens.at(i), &temp1, flag , tokens.at(i + 1),
203 tokens.at(i + 2)) != true) //false -> Property
210 if (isCondition && lastType == temp1.type)
215 lastType = temp1.type;
217 temp.child_token.push_back(temp1);
219 root->child_token.push_back(temp);
226 std::string CCQLParser::tolower(std::string str)
228 for (unsigned int i = 0 ; i < str.size() ; i++)
230 if (str[i] <= 'Z' && str[i] >= 'A')
239 std::vector<std::string> CCQLParser::getTokens(const std::string &str,
240 const std::string &delimiters)
242 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
243 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
244 std::vector<std::string> tokens;
246 while (std::string::npos != pos || std::string::npos != lastPos)
248 tokens.push_back(str.substr(lastPos, pos - lastPos));
249 lastPos = str.find_first_not_of(delimiters, pos);
250 pos = str.find_first_of(delimiters, lastPos);
253 if (tokens.size() == 0)
254 tokens.push_back("\"\"");
259 void CCQLParser::check_index(std::string input, Token *token)
261 std::vector<std::string> tokens = getTokens(input, "[");
263 if (tokens.size() == 2)
265 if (tolower(tokens.at(1)) == "all]")
267 token->name = tokens.at(0);
268 token->number = INDEX_ALL;
271 else if (tolower(tokens.at(1)) == "this]")
273 token->name = tokens.at(0);
274 token->number = INDEX_THIS;
279 int num = atoi(tokens.at(1).c_str());
280 token->name = tokens.at(0);
286 bool CCQLParser::split(std::string input, Token *root, bool flag, std::string arg1,
289 std::vector<std::string> tokens = getTokens(input, ".");
290 Token *temp_token = root;
292 for (unsigned int j = 0 ; j < tokens.size() ; j++)
296 temp_token->name = tokens.at(j);
297 check_index(tokens.at(j), temp_token);
301 if (j == tokens.size() - 1 && flag == false)
303 ModelProperty property;
304 property.propertyName = tokens.at(j);
308 std::vector<std::string> tokens_temp = getTokens(arg2, "\"");
310 property.propertyValue = tokens_temp.at(0);
311 property.propertyType = ModelProperty::TYPE_TEXT;
313 temp_token->model_property = property;
315 //root->child_token.push_back(temp1);
320 //temp1->number = atof(arg2.c_str());
322 // 0 text /1 integer /2 real
323 if (check_number(arg2) == TYPEINTEGER)
325 property.propertyType = ModelProperty::TYPE_INTEGER;
327 else if (check_number(arg2) == TYPEREAL)
329 property.propertyType = ModelProperty::TYPE_REAL;
333 //unknown type raise error
336 property.propertyValue = arg2;
338 temp_token->model_property = property;
344 temp1.type = Context;
345 temp1.name = tokens.at(j);
346 check_index(tokens.at(j), &temp1);
347 temp_token->child_token.push_back(temp1);
348 temp_token = &(temp_token->child_token.back());
356 int CCQLParser::check_number(std::string &str)
358 int flag = 0; // 0 text /1 integer /2 real
361 std::string::const_iterator it = str.begin();
362 while (it != str.end())
368 else if (isdigit(*it) == false)
382 else if (dotCount == 1)
390 std::string CCQLParser::check_Predicate(std::string input)
392 std::string temp = "";
393 for (unsigned int i = 0 ; i < input.size() ; i++)
404 if (input[i - 1] != '=' && input[i - 1] != '!' && input[i - 1] != '>' && input[i - 1] != '<'
405 && input[i - 1] != ' ')
414 if (input[i - 1] != ' ')
424 if (input[i - 1] == '=' || input[i - 1] == '<' || input[i - 1] == '>')
436 bool CCQLParser::check_grammer(Token *token)
438 if (token->child_token.size() == 1 && tolower(token->child_token.at(0).name) == "get")
440 if (token->child_token.at(0).child_token.size() > 0)
450 else if (token->child_token.size() == 2)
452 if (token->child_token.at(1).child_token.size() > 0)
454 if (tolower(token->child_token.at(0).name) == "subscribe")
458 else if (tolower(token->child_token.at(0).name) == "get")
460 if (token->child_token.at(0).child_token.size() > 0)