Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
parser_util.h
Go to the documentation of this file.
1// Copyright 2010-2025 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 ORTOOLS_FLATZINC_PARSER_UTIL_H_
18#define ORTOOLS_FLATZINC_PARSER_UTIL_H_
19
20#include <cstdint>
21#include <string>
22#include <vector>
23
24#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.domain = domain;
61 result.is_domain = true;
62 result.defined = true;
63 return result;
64 }
66 VarRefOrValue result;
67 result.float_value = value;
68 result.defined = true;
69 result.is_float = true;
70 return result;
71 }
72
73 Variable* variable = nullptr;
74 int64_t value = 0;
75 double float_value = 0.0;
77 bool defined = false;
78 bool is_float = false;
79 bool is_domain = false;
80 // Indicates that the set domain is fixed.
81 bool domain_is_fixed = false;
82};
83
84// Class needed to pass information from the lexer to the parser.
85// TODO(user): Use std::unique_ptr<vector< >> to ease memory management.
86struct LexerInfo {
89 std::string string_value;
91 std::vector<Domain>* domains;
92 std::vector<int64_t>* integers;
93 std::vector<double>* doubles;
95 std::vector<Argument>* args;
97 std::vector<Annotation>* annotations;
99 std::vector<VarRefOrValue>* var_or_value_array;
100};
101
102// If the argument is an integer, return it as int64_t. Otherwise, die.
103int64_t ConvertAsIntegerOrDie(double d);
104} // namespace fz
105} // namespace operations_research
106#endif // ORTOOLS_FLATZINC_PARSER_UTIL_H_
int64_t ConvertAsIntegerOrDie(double d)
OR-Tools root namespace.
std::vector< double > * doubles
Definition parser_util.h:93
std::vector< Annotation > * annotations
Definition parser_util.h:97
std::vector< int64_t > * integers
Definition parser_util.h:92
std::vector< Domain > * domains
Definition parser_util.h:91
std::vector< Argument > * args
Definition parser_util.h:95
std::vector< VarRefOrValue > * var_or_value_array
Definition parser_util.h:99
absl::flat_hash_map< std::string, std::vector< Variable * > > variable_array_map
Definition parser_util.h:36
absl::flat_hash_map< std::string, Domain > domain_map
Definition parser_util.h:37
absl::flat_hash_map< std::string, int64_t > integer_map
Definition parser_util.h:31
absl::flat_hash_map< std::string, std::vector< double > > float_array_map
Definition parser_util.h:34
absl::flat_hash_map< std::string, Variable * > variable_map
Definition parser_util.h:35
absl::flat_hash_map< std::string, std::vector< Domain > > domain_array_map
Definition parser_util.h:38
absl::flat_hash_map< std::string, double > float_map
Definition parser_util.h:33
absl::flat_hash_map< std::string, std::vector< int64_t > > integer_array_map
Definition parser_util.h:32
static VarRefOrValue VarRef(Variable *var)
Definition parser_util.h:46
static VarRefOrValue DomainValue(Domain domain)
Definition parser_util.h:58
static VarRefOrValue FloatValue(double value)
Definition parser_util.h:65
static VarRefOrValue Value(int64_t value)
Definition parser_util.h:52