JUK1
ElementsRegexBuilder.cpp
Go to the documentation of this file.
2 // indentifier is the sequence of letters representing the component
3 // simplifiedMatching:
4 // a string detailing how many, and the order of size_ts, and Values, and strings, and when options start
5 // startAnchor "^"
6 // endAnchor "$"
7 //
8 // n = int/size_t
9 // w = word (works for floats etc)
10 // ? = everything after is optional
11 // c = char
12 // s = space
13 //
14 // Capacitor example:
15 // ( "C", "n n w" )
16 // Resistor example:
17 // ( "R", "n n w ? w" )
18 //
19 //
20 
21 std::regex
22 generateRegex( std::string indentifier, std::string simplifiedMatching,
23  bool startAnchor,
24  bool endAnchor ) {
25  constexpr char spaceRegex[] = R"(\s)";
26  constexpr char startAnchorRegex[] = R"(^)";
27  constexpr char endAnchorRegex[] = R"(\s?$)";
28  constexpr char optionalSpaceRegex[] = R"(\s?)";
29 
30  constexpr char charRegex[] = R"((.))";
31  constexpr char size_tRegex[] = R"((\d+?))";
32  constexpr char emptyWordRegex[] = R"((.*?))";
33  constexpr char wordRegex[] = R"((.+?))";
34  constexpr char optionalCharRegex[] = R"((?:\s(.))?)";
35  constexpr char optionalWordRegex[] = R"((?:\s(.+?))?)";
36  constexpr char optionalSize_tRegex[] = R"((?:\s(\d+?))?)";
37 
38  std::string built( "" );
39 
40  if ( startAnchor ) {
41  built += startAnchorRegex;
42  }
43 
44  built += indentifier + emptyWordRegex;
45 
46  bool option = false;
47  for ( char letter : simplifiedMatching ) {
48  switch( letter ) {
49  case 'n':
50  if ( option ) {
51  built += optionalSize_tRegex;
52  } else {
53  built += spaceRegex;
54  built += size_tRegex;
55  }
56  break;
57  case 'w':
58  if ( option ) {
59  built += optionalWordRegex;
60  } else {
61  built += spaceRegex;
62  built += wordRegex;
63  }
64  break;
65  case 'c':
66  if ( option ) {
67  built += optionalCharRegex;
68  } else {
69  built += spaceRegex;
70  built += charRegex;
71  }
72  break;
73  case 's':
74  if ( option ) {
75  built += optionalSpaceRegex;
76  } else {
77  built += spaceRegex;
78  }
79  break;
80  case '?':
81  option = true;
82  break;;
83  case ' ':
84  break;
85  default:
86  break;
87  }
88  }
89 
90  if ( endAnchor ) {
91  built += endAnchorRegex;
92  }
93 
94  return std::regex( built.c_str() );
95 }
std::regex generateRegex(std::string indentifier, std::string simplifiedMatching, bool startAnchor, bool endAnchor)
a helper function to aid in the construction of regexes for parsing netlist files