CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
DataFormatter.cc
Go to the documentation of this file.
1 #include <vector>
2 #include <string>
3 #include "casm/external/boost.hh"
5 
6 namespace CASM {
7  //****************************************************************************************
8 
9  std::string::const_iterator end_of_literal(std::string::const_iterator it, std::string::const_iterator end_it) {
10 
11  for(++it; it != end_it; ++it) {
12  if((*it) == '\'')
13  return ++it;
14  }
15  return it;
16  }
17 
18  //****************************************************************************************
19  /*
20  * break 'input' string into a list of format tags and their (option) arguments
21  */
22  void split_formatter_expression(const std::string &input_expr,
23  std::vector<std::string> &tag_names,
24  std::vector<std::string> &sub_exprs) {
25  std::string::const_iterator it(input_expr.cbegin()), it_end(input_expr.cend()), t_it1, t_it2;
26  while(it != it_end) {
27  while(it != it_end && (isspace(*it) || boost::is_any_of(",#")(*it)))
28  ++it;
29  if(it == it_end)
30  break;
31  // Identified a formatter tag, save starting iterator
32  t_it1 = it;
33  if((*it) == '\'') {
34  it = end_of_literal(it, it_end);
35  t_it2 = it;
36  --t_it2;
37  if(t_it2 == t_it1 || (*t_it2) != '\'') {
38  throw std::runtime_error("Mismatched quotation marks in expression:\n \"" + input_expr + "\"\n");
39  }
40  }
41  else {
42  // find end of formatter tag
43  while(it != it_end && !(isspace(*it) || (*it) == ',') && (*it) != '(' && (*it) != ')')
44  ++it;
45 
46  if(*it == ')')
47  throw std::runtime_error("Mismatched parentheses in expression:\n \"" + input_expr + "\"\n");
48  }
49  // push_back formatter tag, and push_back an empty string for its optional arguments
50  tag_names.push_back(std::string(t_it1, it));
51  sub_exprs.push_back(std::string());
52 
53 
54  // no argument, we've reached beginning of new tag, so continue
55  if(it == it_end || (*it) != '(')
56  continue;
57 
58  // from here on, we're parsing arguments:
59  while(it != it_end && isspace(*(++it)));//skipspace
60 
61  if(*it == ',')
62  throw std::runtime_error("Stray comma in expression:\n \"" + input_expr + "\"\n");
63 
64  // start of argument
65  t_it1 = it;
66 
67  // stop at end of string, or if we hit the non-nested closing paren
68  Index paren_depth(0);
69  while(it != it_end && !((*it) == ')' && paren_depth == 0)) {
70  if((*it) == '(')
71  ++paren_depth;
72  else if((*it) == ')')
73  --paren_depth;
74 
75  ++it;
76  }
77 
78  if(it == it_end)
79  throw std::runtime_error("Mismatched parentheses in expression:\n \"" + input_expr + "\"\n");
80 
81  t_it2 = it;
82  while(isspace(*(--t_it2)));//remove trailing space
83 
84  if(*t_it2 == ',')
85  throw std::runtime_error("Stray comma in expression:\n \"" + input_expr + "\"\n");
86 
87  sub_exprs.back() = std::string(t_it1, ++t_it2);
88 
89  ++it;
90  }
91 
92  }
93 }
void split_formatter_expression(const std::string &input_expr, std::vector< std::string > &tag_names, std::vector< std::string > &sub_exprs)
Main CASM namespace.
Definition: complete.cpp:8
std::string::const_iterator end_of_literal(std::string::const_iterator it, std::string::const_iterator end_it)
Definition: DataFormatter.cc:9
EigenIndex Index
For long integer indexing: