OR-Tools  9.6
parser_util.h
Go to the documentation of this file.
1 // Copyright 2010-2022 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 #include <string>
23 #include <vector>
24 
25 #include "absl/container/flat_hash_map.h"
26 #include "ortools/flatzinc/model.h"
27 
28 namespace operations_research {
29 namespace fz {
30 // This is the context used during parsing.
31 struct ParserContext {
32  absl::flat_hash_map<std::string, int64_t> integer_map;
33  absl::flat_hash_map<std::string, std::vector<int64_t>> integer_array_map;
34  absl::flat_hash_map<std::string, double> float_map;
35  absl::flat_hash_map<std::string, std::vector<double>> float_array_map;
36  absl::flat_hash_map<std::string, Variable*> variable_map;
37  absl::flat_hash_map<std::string, std::vector<Variable*>> variable_array_map;
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.
45 struct VarRefOrValue {
46  static VarRefOrValue Undefined() { return VarRefOrValue(); }
48  VarRefOrValue result;
49  result.variable = var;
50  result.defined = true;
51  return result;
52  }
53  static VarRefOrValue Value(int64_t value) {
54  VarRefOrValue result;
55  result.value = value;
56  result.defined = true;
57  return result;
58  }
59  static VarRefOrValue FloatValue(double value) {
60  VarRefOrValue result;
61  result.float_value = value;
62  result.defined = true;
63  result.is_float = true;
64  return result;
65  }
66 
67  Variable* variable = nullptr;
68  int64_t value = 0;
69  double float_value = 0.0;
70  bool defined = false;
71  bool is_float = false;
72 };
73 
74 // Class needed to pass information from the lexer to the parser.
75 // TODO(user): Use std::unique_ptr<vector< >> to ease memory management.
76 struct LexerInfo {
77  int64_t integer_value;
78  double double_value;
79  std::string string_value;
81  std::vector<Domain>* domains;
82  std::vector<int64_t>* integers;
83  std::vector<double>* doubles;
85  std::vector<Argument>* args;
87  std::vector<Annotation>* annotations;
89  std::vector<VarRefOrValue>* var_or_value_array;
90 };
91 
92 // If the argument is an integer, return it as int64_t. Otherwise, die.
93 int64_t ConvertAsIntegerOrDie(double d);
94 } // namespace fz
95 } // namespace operations_research
96 #endif // OR_TOOLS_FLATZINC_PARSER_UTIL_H_
IntVar * var
Definition: expr_array.cc:1874
int64_t ConvertAsIntegerOrDie(double d)
Definition: parser_util.cc:67
Collection of objects used to extend the Constraint Solver library.
std::vector< Argument > * args
Definition: parser_util.h:85
std::vector< Domain > * domains
Definition: parser_util.h:81
std::vector< VarRefOrValue > * var_or_value_array
Definition: parser_util.h:89
std::vector< double > * doubles
Definition: parser_util.h:83
std::vector< Annotation > * annotations
Definition: parser_util.h:87
std::vector< int64_t > * integers
Definition: parser_util.h:82
absl::flat_hash_map< std::string, int64_t > integer_map
Definition: parser_util.h:32
absl::flat_hash_map< std::string, double > float_map
Definition: parser_util.h:34
absl::flat_hash_map< std::string, Variable * > variable_map
Definition: parser_util.h:36
absl::flat_hash_map< std::string, std::vector< Variable * > > variable_array_map
Definition: parser_util.h:37
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:33
absl::flat_hash_map< std::string, std::vector< double > > float_array_map
Definition: parser_util.h:35
absl::flat_hash_map< std::string, Domain > domain_map
Definition: parser_util.h:38
static VarRefOrValue VarRef(Variable *var)
Definition: parser_util.h:47
static VarRefOrValue Value(int64_t value)
Definition: parser_util.h:53
static VarRefOrValue Undefined()
Definition: parser_util.h:46
static VarRefOrValue FloatValue(double value)
Definition: parser_util.h:59