BayesOpt
parser.cpp
1 /*
2 -------------------------------------------------------------------------
3  This file is part of BayesOpt, an efficient C++ library for
4  Bayesian optimization.
5 
6  Copyright (C) 2011-2015 Ruben Martinez-Cantin <rmcantin@unizar.es>
7 
8  BayesOpt is free software: you can redistribute it and/or modify it
9  under the terms of the GNU Affero General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  BayesOpt is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Affero General Public License for more details.
17 
18  You should have received a copy of the GNU Affero General Public License
19  along with BayesOpt. If not, see <http://www.gnu.org/licenses/>.
20 ------------------------------------------------------------------------
21 */
22 #include <stdexcept>
23 #include <sstream>
24 #include <iostream>
25 #include "parser.hpp"
26 
27 namespace bayesopt
28 {
29  namespace utils
30  {
35  void parseExpresion(std::string input, std::string& parent,
36  std::string& child1, std::string& child2)
37  {
38  std::stringstream is(input);
39  std::stringstream os(std::stringstream::out);
40  std::stringstream os1(std::stringstream::out);
41  std::stringstream os2(std::stringstream::out);
42  char c;
43  int i = 0, j = 0;
44  while (is >> c)
45  {
46  if (i < 0) throw std::runtime_error("Error parsing expression:" + input);
47 
48  if (c == ' ') /* pass */;
49  else if (c == '(') i++;
50  else if (c == ')') i--;
51  else if ((i == 1) && (c == ',')) j++;
52  else
53  {
54  if (i == 0) os << c;
55  else if (j == 0) os1 << c;
56  else os2 << c;
57  }
58  }
59  if (i != 0) throw std::runtime_error("Error parsing expression:" + input);
60 
61  parent = os.str();
62  child1 = os1.str();
63  child2 = os2.str();
64  }
65 
70  void parseExpresion(std::string input, std::string& parent,
71  std::vector<std::string>& childs)
72  {
73  std::stringstream is(input);
74  std::stringstream os(std::stringstream::out);
75  std::stringstream os1(std::stringstream::out);
76  char c;
77  int i = 0;
78 
79  childs.clear();
80  while (is >> c)
81  {
82  if (i < 0) throw std::runtime_error("Error parsing expression:" + input);
83 
84  if (c == ' ') /* pass */;
85  else if (c == '(')
86  {
87  ++i;
88  if (i > 1) os1 << c;
89  }
90  else if ((c == ')') && (--i == 0))
91  {
92  childs.push_back(os1.str());
93  }
94  else if ((i == 1) && (c == ','))
95  {
96  childs.push_back(os1.str());
97  os1.str(std::string());
98  }
99  else
100  {
101  if (i == 0) os << c;
102  else os1 << c;
103  }
104  }
105  if (i != 0) throw std::runtime_error("Error parsing expression:" + input);
106 
107  parent = os.str();
108  }
109 
113  void split(std::string &input, char delim, std::vector<std::string> &elems){
114  std::stringstream ss(input);
115  std::string item;
116  while (std::getline(ss, item, delim)){
117  elems.push_back(item);
118  }
119  }
120  } //namespace utils
121 
122 } //namespace bayesopt
123 
Namespace of the library interface.
Definition: using.dox:1
Functions to parse strings.
void split(std::string &input, char delim, std::vector< std::string > &elems)
Splits the input string with a delimiter to extract elements.
Definition: parser.cpp:113
void parseExpresion(std::string input, std::string &parent, std::string &child1, std::string &child2)
Parse expresions of the form Parent(Child1, Child2).
Definition: parser.cpp:35