OR-Tools  9.1
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"
26
27namespace operations_research {
28namespace fz {
29// This is the context used during parsing.
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, Variable*> variable_map;
36 absl::flat_hash_map<std::string, std::vector<Variable*>> variable_array_map;
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.
45 static VarRefOrValue Undefined() { return VarRefOrValue(); }
47 VarRefOrValue result;
48 result.variable = var;
49 result.defined = true;
50 return result;
51 }
52 static VarRefOrValue Value(int64_t value) {
53 VarRefOrValue result;
54 result.value = value;
55 result.defined = true;
56 return result;
57 }
59 VarRefOrValue result;
60 result.float_value = value;
61 result.defined = true;
62 result.is_float = true;
63 return result;
64 }
65
66 Variable* variable = nullptr;
67 int64_t value = 0;
68 double float_value = 0.0;
69 bool defined = false;
70 bool is_float = false;
71};
72
73// Class needed to pass information from the lexer to the parser.
74// TODO(user): Use std::unique_ptr<vector< >> to ease memory management.
75struct LexerInfo {
78 std::string string_value;
80 std::vector<Domain>* domains;
81 std::vector<int64_t>* integers;
82 std::vector<double>* doubles;
84 std::vector<Argument>* args;
86 std::vector<Annotation>* annotations;
88 std::vector<VarRefOrValue>* var_or_value_array;
89};
90
91// If the argument is an integer, return it as int64_t. Otherwise, die.
92int64_t ConvertAsIntegerOrDie(double d);
93} // namespace fz
94} // namespace operations_research
95#endif // OR_TOOLS_FLATZINC_PARSER_UTIL_H_
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:84
std::vector< Domain > * domains
Definition: parser_util.h:80
std::vector< VarRefOrValue > * var_or_value_array
Definition: parser_util.h:88
std::vector< double > * doubles
Definition: parser_util.h:82
std::vector< Annotation > * annotations
Definition: parser_util.h:86
std::vector< int64_t > * integers
Definition: parser_util.h:81
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, Variable * > variable_map
Definition: parser_util.h:35
absl::flat_hash_map< std::string, std::vector< Variable * > > variable_array_map
Definition: parser_util.h:36
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_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:37
static VarRefOrValue VarRef(Variable *var)
Definition: parser_util.h:46
static VarRefOrValue Value(int64_t value)
Definition: parser_util.h:52
static VarRefOrValue Undefined()
Definition: parser_util.h:45
static VarRefOrValue FloatValue(double value)
Definition: parser_util.h:58