OR-Tools  8.2
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"
25
26namespace operations_research {
27namespace fz {
28// This is the context used during parsing.
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
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.
87struct LexerInfo {
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.
105} // namespace fz
106} // namespace operations_research
107#endif // OR_TOOLS_FLATZINC_PARSER_UTIL_H_
#define CHECK(condition)
Definition: base/logging.h:495
IntVar * var
Definition: expr_array.cc:1858
int64_t int64
int64 ConvertAsIntegerOrDie(double d)
Definition: parser_util.cc:65
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
std::vector< int64 > * integers
Definition: parser_util.h:93
std::vector< Argument > * args
Definition: parser_util.h:96
std::vector< Domain > * domains
Definition: parser_util.h:92
VariableRefOrValue var_or_value
Definition: parser_util.h:99
VariableRefOrValueArray * var_or_value_array
Definition: parser_util.h:100
std::vector< double > * doubles
Definition: parser_util.h:94
std::vector< Annotation > * annotations
Definition: parser_util.h:98
absl::flat_hash_map< std::string, double > float_map
Definition: parser_util.h:32
absl::flat_hash_map< std::string, std::vector< IntegerVariable * > > variable_array_map
Definition: parser_util.h:36
absl::flat_hash_map< std::string, int64 > integer_map
Definition: parser_util.h:30
absl::flat_hash_map< std::string, IntegerVariable * > variable_map
Definition: parser_util.h:34
absl::flat_hash_map< std::string, std::vector< Domain > > domain_array_map
Definition: parser_util.h:38
absl::flat_hash_map< std::string, std::vector< int64 > > integer_array_map
Definition: parser_util.h:31
absl::flat_hash_map< std::string, std::vector< double > > float_array_map
Definition: parser_util.h:33
absl::flat_hash_map< std::string, Domain > domain_map
Definition: parser_util.h:37
std::vector< IntegerVariable * > variables
Definition: parser_util.h:73
void PushBack(const VariableRefOrValue &v)
Definition: parser_util.h:76
static VariableRefOrValue VariableRef(IntegerVariable *var)
Definition: parser_util.h:52
static VariableRefOrValue Value(int64 value)
Definition: parser_util.h:59
static VariableRefOrValue Undefined()
Definition: parser_util.h:45