OR-Tools  8.1
parser_util.h
Go to the documentation of this file.
1 // Copyright 2010-2018 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 
22 #include "absl/container/flat_hash_map.h"
23 #include "ortools/base/map_util.h"
24 #include "ortools/flatzinc/model.h"
25 
26 namespace operations_research {
27 namespace fz {
28 // This is the context used during parsing.
29 struct ParserContext {
30  absl::flat_hash_map<std::string, int64> integer_map;
31  absl::flat_hash_map<std::string, std::vector<int64>> integer_array_map;
32  absl::flat_hash_map<std::string, double> float_map;
33  absl::flat_hash_map<std::string, std::vector<double>> float_array_map;
34  absl::flat_hash_map<std::string, IntegerVariable*> variable_map;
35  absl::flat_hash_map<std::string, std::vector<IntegerVariable*>>
37  absl::flat_hash_map<std::string, Domain> domain_map;
38  absl::flat_hash_map<std::string, std::vector<Domain>> domain_array_map;
39 };
40 
41 // An optional reference to a variable, or an integer value, used in
42 // assignments during the declaration of a variable, or a variable
43 // array.
46  VariableRefOrValue result;
47  result.variable = nullptr;
48  result.value = 0;
49  result.defined = false;
50  return result;
51  }
53  VariableRefOrValue result;
54  result.variable = var;
55  result.value = 0;
56  result.defined = true;
57  return result;
58  }
60  VariableRefOrValue result;
61  result.variable = nullptr;
62  result.value = value;
63  result.defined = true;
64  return result;
65  }
66 
69  bool defined;
70 };
71 
73  std::vector<IntegerVariable*> variables;
74  std::vector<int64> values;
75 
76  void PushBack(const VariableRefOrValue& v) {
77  CHECK(v.defined);
78  variables.push_back(v.variable);
79  values.push_back(v.value);
80  }
81 
82  int Size() const { return values.size(); }
83 };
84 
85 // Class needed to pass information from the lexer to the parser.
86 // TODO(user): Use std::unique_ptr<vector< >> to ease memory management.
87 struct LexerInfo {
89  double double_value;
90  std::string string_value;
92  std::vector<Domain>* domains;
93  std::vector<int64>* integers;
94  std::vector<double>* doubles;
96  std::vector<Argument>* args;
98  std::vector<Annotation>* annotations;
101 };
102 
103 // If the argument is an integer, return it as int64. Otherwise, die.
104 int64 ConvertAsIntegerOrDie(double d);
105 } // namespace fz
106 } // namespace operations_research
107 #endif // OR_TOOLS_FLATZINC_PARSER_UTIL_H_
var
IntVar * var
Definition: expr_array.cc:1858
operations_research::fz::ParserContext::float_array_map
absl::flat_hash_map< std::string, std::vector< double > > float_array_map
Definition: parser_util.h:33
operations_research::fz::LexerInfo::arg
Argument arg
Definition: parser_util.h:95
map_util.h
operations_research::fz::VariableRefOrValueArray
Definition: parser_util.h:72
operations_research::fz::LexerInfo::var_or_value
VariableRefOrValue var_or_value
Definition: parser_util.h:99
operations_research::fz::VariableRefOrValueArray::PushBack
void PushBack(const VariableRefOrValue &v)
Definition: parser_util.h:76
operations_research::fz::ConvertAsIntegerOrDie
int64 ConvertAsIntegerOrDie(double d)
Definition: parser_util.cc:65
operations_research::fz::ParserContext::integer_map
absl::flat_hash_map< std::string, int64 > integer_map
Definition: parser_util.h:30
operations_research::fz::VariableRefOrValue::variable
IntegerVariable * variable
Definition: parser_util.h:67
operations_research::fz::LexerInfo::domain
Domain domain
Definition: parser_util.h:91
operations_research::fz::Annotation
Definition: flatzinc/model.h:238
model.h
operations_research::fz::LexerInfo::annotation
Annotation annotation
Definition: parser_util.h:97
operations_research
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
Definition: dense_doubly_linked_list.h:21
operations_research::fz::LexerInfo::annotations
std::vector< Annotation > * annotations
Definition: parser_util.h:98
operations_research::fz::LexerInfo::domains
std::vector< Domain > * domains
Definition: parser_util.h:92
operations_research::fz::VariableRefOrValue
Definition: parser_util.h:44
int64
int64_t int64
Definition: integral_types.h:34
operations_research::fz::ParserContext::variable_array_map
absl::flat_hash_map< std::string, std::vector< IntegerVariable * > > variable_array_map
Definition: parser_util.h:36
operations_research::fz::VariableRefOrValue::VariableRef
static VariableRefOrValue VariableRef(IntegerVariable *var)
Definition: parser_util.h:52
operations_research::fz::ParserContext
Definition: parser_util.h:29
operations_research::fz::VariableRefOrValue::defined
bool defined
Definition: parser_util.h:69
operations_research::fz::VariableRefOrValue::Undefined
static VariableRefOrValue Undefined()
Definition: parser_util.h:45
operations_research::fz::ParserContext::integer_array_map
absl::flat_hash_map< std::string, std::vector< int64 > > integer_array_map
Definition: parser_util.h:31
operations_research::fz::ParserContext::variable_map
absl::flat_hash_map< std::string, IntegerVariable * > variable_map
Definition: parser_util.h:34
operations_research::fz::LexerInfo::doubles
std::vector< double > * doubles
Definition: parser_util.h:94
operations_research::fz::Argument
Definition: flatzinc/model.h:143
operations_research::fz::VariableRefOrValue::Value
static VariableRefOrValue Value(int64 value)
Definition: parser_util.h:59
operations_research::fz::LexerInfo::args
std::vector< Argument > * args
Definition: parser_util.h:96
operations_research::fz::VariableRefOrValueArray::values
std::vector< int64 > values
Definition: parser_util.h:74
operations_research::fz::LexerInfo::double_value
double double_value
Definition: parser_util.h:89
operations_research::fz::ParserContext::domain_map
absl::flat_hash_map< std::string, Domain > domain_map
Definition: parser_util.h:37
operations_research::fz::VariableRefOrValue::value
int64 value
Definition: parser_util.h:68
operations_research::fz::ParserContext::float_map
absl::flat_hash_map< std::string, double > float_map
Definition: parser_util.h:32
operations_research::fz::LexerInfo::integer_value
int64 integer_value
Definition: parser_util.h:88
operations_research::fz::IntegerVariable
Definition: flatzinc/model.h:107
operations_research::fz::LexerInfo::string_value
std::string string_value
Definition: parser_util.h:90
operations_research::fz::VariableRefOrValueArray::variables
std::vector< IntegerVariable * > variables
Definition: parser_util.h:73
operations_research::fz::LexerInfo
Definition: parser_util.h:87
operations_research::fz::Domain
Definition: flatzinc/model.h:48
operations_research::fz::VariableRefOrValueArray::Size
int Size() const
Definition: parser_util.h:82
operations_research::fz::LexerInfo::integers
std::vector< int64 > * integers
Definition: parser_util.h:93
CHECK
#define CHECK(condition)
Definition: base/logging.h:495
operations_research::fz::LexerInfo::var_or_value_array
VariableRefOrValueArray * var_or_value_array
Definition: parser_util.h:100
operations_research::fz::ParserContext::domain_array_map
absl::flat_hash_map< std::string, std::vector< Domain > > domain_array_map
Definition: parser_util.h:38