Qore Programming Language 1.19.2
Loading...
Searching...
No Matches
QoreParseHashNode.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 QoreParseHashNode.h
4
5 Qore Programming Language
6
7 Copyright (C) 2003 - 2023 Qore Technologies, s.r.o.
8
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the "Software"),
11 to deal in the Software without restriction, including without limitation
12 the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26
27 Note that the Qore library is released under a choice of three open-source
28 licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29 information.
30*/
31
32#ifndef _QORE_QOREPARSEHASHNODE_H
33
34#define _QORE_QOREPARSEHASHNODE_H
35
36#include <qore/Qore.h>
37
38#include <vector>
39#include <map>
40#include <string>
41
42DLLLOCAL QoreValue copy_value_and_resolve_lvar_refs(const QoreValue& n, ExceptionSink* xsink);
43
44class QoreParseHashNode : public ParseNode {
45public:
46 typedef std::vector<QoreValue> nvec_t;
47 typedef std::vector<const QoreTypeInfo*> tvec_t;
48
49 DLLLOCAL QoreParseHashNode(const QoreProgramLocation* loc, bool curly = false)
50 : ParseNode(loc, NT_PARSE_HASH, true), curly(curly) {
51 }
52
53 // to resolve local vars in a background expression before evaluation
54 DLLLOCAL QoreParseHashNode(const QoreParseHashNode& old, ExceptionSink* xsink) :
55 ParseNode(old.loc, NT_PARSE_HASH, true),
56 lvec(old.lvec),
57 kmap(old.kmap),
58 vtype(old.vtype),
59 typeInfo(old.typeInfo),
60 curly(old.curly) {
61 keys.reserve(old.keys.size());
62 values.reserve(old.values.size());
63 for (auto& i : old.keys) {
64 keys.push_back(copy_value_and_resolve_lvar_refs(i, xsink));
65 if (*xsink)
66 return;
67 }
68 for (auto& i : old.values) {
69 values.push_back(copy_value_and_resolve_lvar_refs(i, xsink));
70 if (*xsink)
71 return;
72 }
73 }
74
75 DLLLOCAL ~QoreParseHashNode() {
76 assert(keys.size() == values.size());
77 for (size_t i = 0; i < keys.size(); ++i) {
78 keys[i].discard(nullptr);
79 values[i].discard(nullptr);
80 }
81 keys.clear();
82 values.clear();
83 }
84
85 DLLLOCAL void add(QoreValue n, QoreValue v, const QoreProgramLocation* loc) {
86 keys.push_back(n);
87 values.push_back(v);
88 lvec.push_back(loc);
89
90 if (!n || n.isValue()) {
92 checkDup(loc, key->c_str());
93 }
94 }
95
96 DLLLOCAL size_t size() {
97 return keys.size();
98 }
99
100 // used when converting to the hash map operator
101 DLLLOCAL QoreValue takeFirstKey() {
102 assert(keys.size() == 1);
103 QoreValue rv = keys[0];
104 keys[0].clear();
105 return rv;
106 }
107
108 // used when converting to the hash map operator
109 DLLLOCAL QoreValue takeFirstValue() {
110 assert(values.size() == 1);
111 QoreValue rv = values[0];
112 values[0].clear();
113 return rv;
114 }
115
116 DLLLOCAL void setCurly() {
117 assert(!curly);
118 curly = true;
119 }
120
121 DLLLOCAL bool isCurly() const {
122 return curly;
123 }
124
125 DLLLOCAL void finalizeBlock(int start_line, int end_line);
126
127 DLLLOCAL const nvec_t& getKeys() const{
128 return keys;
129 }
130
131 DLLLOCAL const nvec_t& getValues() const {
132 return values;
133 }
134
135 DLLLOCAL const tvec_t& getValueTypes() const {
136 return vtypes;
137 }
138
139 DLLLOCAL virtual int getAsString(QoreString& str, int foff, ExceptionSink* xsink) const;
140
141 DLLLOCAL virtual QoreString* getAsString(bool& del, int foff, ExceptionSink* xsink) const;
142
143 DLLLOCAL virtual const char* getTypeName() const;
144
145 DLLLOCAL virtual int parseInitImpl(QoreValue& val, QoreParseContext& parse_context);
146
147 DLLLOCAL bool hasParseError() const {
148 return parse_error;
149 }
150
151protected:
152 typedef std::map<std::string, bool> kmap_t;
153 typedef std::vector<const QoreProgramLocation*> lvec_t;
154 nvec_t keys, values;
155 tvec_t vtypes;
156 lvec_t lvec;
157 // to detect duplicate values, only stored during parsing
158 kmap_t kmap;
159 // common value type, if any
160 const QoreTypeInfo* vtype = nullptr;
161 // node type info (derivative of hash)
162 const QoreTypeInfo* typeInfo = nullptr;
163 // flag for a hash expression in curly brackets for the hash version of the map operator
164 bool curly;
165 // error happened in parsing
166 bool parse_error = false;
167
168 DLLLOCAL virtual const QoreTypeInfo* getTypeInfo() const {
169 return typeInfo;
170 }
171
172 DLLLOCAL static void doDuplicateWarning(const QoreProgramLocation* newoc1, const char* key);
173
174 DLLLOCAL void checkDup(const QoreProgramLocation* loc, const char* key) {
175 std::string kstr(key);
176 kmap_t::iterator i = kmap.lower_bound(kstr);
177 if (i == kmap.end() || i->first != kstr)
178 kmap.insert(i, kmap_t::value_type(kstr, false));
179 else if (!i->second) {
180 doDuplicateWarning(loc, key);
181 i->second = true;
182 }
183 }
184
185 DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const;
186};
187
188#endif
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:50
DLLEXPORT bool isValue() const
returns true if the object holds a value, false if it holds an expression
DLLEXPORT void clear()
unconditionally set the QoreValue to QoreNothingNode (does not dereference any possible contained Abs...
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:93
this class is used to safely manage calls to AbstractQoreNode::getStringRepresentation() when a simpl...
Definition: QoreStringNode.h:320
const qore_type_t NT_PARSE_HASH
type value for QoreParseHashNode
Definition: node_types.h:83
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:276