OR-Tools  9.0
parser_util.h
Go to the documentation of this file.
1 // Copyright 2010-2021 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 // Struct and utility functions used by the code in parser.yy
15 // Included in parser.tab.hh.
16 
17 #ifndef OR_TOOLS_FLATZINC_PARSER_UTIL_H_
18 #define OR_TOOLS_FLATZINC_PARSER_UTIL_H_
19 
20 #include <cmath>
21 #include <cstdint>
22 
23 #include "absl/container/flat_hash_map.h"
24 #include "ortools/base/map_util.h"
25 #include "ortools/flatzinc/model.h"
26 
27 namespace operations_research {
28 namespace fz {
29 // This is the context used during parsing.
30 struct ParserContext {
31  absl::flat_hash_map<std::string, int64_t> integer_map;
32  absl::flat_hash_map<std::string, std::vector<int64_t>> integer_array_map;
33  absl::flat_hash_map<std::string, double> float_map;
34  absl::flat_hash_map<std::string, std::vector<double>> float_array_map;
35  absl::flat_hash_map<std::string, IntegerVariable*> variable_map;
36  absl::flat_hash_map<std::string, std::vector<IntegerVariable*>>
38  absl::flat_hash_map<std::string, Domain> domain_map;
39  absl::flat_hash_map<std::string, std::vector<Domain>> domain_array_map;
40 };
41 
42 // An optional reference to a variable, or an integer value, used in
43 // assignments during the declaration of a variable, or a variable
44 // array.
47  VariableRefOrValue result;
48  result.variable = nullptr;
49  result.value = 0;
50  result.defined = false;
51  return result;
52  }
54  VariableRefOrValue result;
55  result.variable = var;
56  result.value = 0;
57  result.defined = true;
58  return result;
59  }
60  static VariableRefOrValue Value(int64_t value) {
61  VariableRefOrValue result;
62  result.variable = nullptr;
63  result.value = value;
64  result.defined = true;
65  return result;
66  }
67 
69  int64_t value;
70  bool defined;
71 };
72 
74  std::vector<IntegerVariable*> variables;
75  std::vector<int64_t> values;
76 
77  void PushBack(const VariableRefOrValue& v) {
78  CHECK(v.defined);
79  variables.push_back(v.variable);
80  values.push_back(v.value);
81  }
82 
83  int Size() const { return values.size(); }
84 };
85 
86 // Class needed to pass information from the lexer to the parser.
87 // TODO(user): Use std::unique_ptr<vector< >> to ease memory management.
88 struct LexerInfo {
89  int64_t integer_value;
90  double double_value;
91  std::string string_value;
93  std::vector<Domain>* domains;
94  std::vector<int64_t>* integers;
95  std::vector<double>* doubles;
97  std::vector<Argument>* args;
99  std::vector<Annotation>* annotations;
102 };
103 
104 // If the argument is an integer, return it as int64_t. Otherwise, die.
105 int64_t ConvertAsIntegerOrDie(double d);
106 } // namespace fz
107 } // namespace operations_research
108 #endif // OR_TOOLS_FLATZINC_PARSER_UTIL_H_
#define CHECK(condition)
Definition: base/logging.h:498
IntVar * var
Definition: expr_array.cc:1874
int64_t ConvertAsIntegerOrDie(double d)
Definition: parser_util.cc:65
Collection of objects used to extend the Constraint Solver library.
std::vector< Argument > * args
Definition: parser_util.h:97
std::vector< Domain > * domains
Definition: parser_util.h:93
VariableRefOrValueArray * var_or_value_array
Definition: parser_util.h:101
std::vector< double > * doubles
Definition: parser_util.h:95
std::vector< Annotation > * annotations
Definition: parser_util.h:99
std::vector< int64_t > * integers
Definition: parser_util.h:94
absl::flat_hash_map< std::string, int64_t > integer_map
Definition: parser_util.h:31
absl::flat_hash_map< std::string, double > float_map
Definition: parser_util.h:33
absl::flat_hash_map< std::string, std::vector< IntegerVariable * > > variable_array_map
Definition: parser_util.h:37
absl::flat_hash_map< std::string, IntegerVariable * > variable_map
Definition: parser_util.h:35
absl::flat_hash_map< std::string, std::vector< Domain > > domain_array_map
Definition: parser_util.h:39
absl::flat_hash_map< std::string, std::vector< int64_t > > integer_array_map
Definition: parser_util.h:32
absl::flat_hash_map< std::string, std::vector< double > > float_array_map
Definition: parser_util.h:34
absl::flat_hash_map< std::string, Domain > domain_map
Definition: parser_util.h:38
std::vector< IntegerVariable * > variables
Definition: parser_util.h:74
void PushBack(const VariableRefOrValue &v)
Definition: parser_util.h:77
static VariableRefOrValue VariableRef(IntegerVariable *var)
Definition: parser_util.h:53
static VariableRefOrValue Undefined()
Definition: parser_util.h:46
static VariableRefOrValue Value(int64_t value)
Definition: parser_util.h:60