Qore Programming Language  0.9.16
QoreParseHashNode.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreParseHashNode.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2018 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 
42 DLLLOCAL QoreValue copy_value_and_resolve_lvar_refs(const QoreValue& n, ExceptionSink* xsink);
43 
44 class QoreParseHashNode : public ParseNode {
45 public:
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) : ParseNode(loc, NT_PARSE_HASH, true), curly(curly) {
50  }
51 
52  // to resolve local vars in a background expression before evaluation
53  DLLLOCAL QoreParseHashNode(const QoreParseHashNode& old, ExceptionSink* xsink) :
54  ParseNode(old.loc, NT_PARSE_HASH, true),
55  lvec(old.lvec),
56  kmap(old.kmap),
57  vtype(old.vtype),
58  typeInfo(old.typeInfo),
59  curly(old.curly) {
60  keys.reserve(old.keys.size());
61  values.reserve(old.values.size());
62  for (auto& i : old.keys) {
63  keys.push_back(copy_value_and_resolve_lvar_refs(i, xsink));
64  if (*xsink)
65  return;
66  }
67  for (auto& i : old.values) {
68  values.push_back(copy_value_and_resolve_lvar_refs(i, xsink));
69  if (*xsink)
70  return;
71  }
72  }
73 
74  DLLLOCAL ~QoreParseHashNode() {
75  assert(keys.size() == values.size());
76  for (size_t i = 0; i < keys.size(); ++i) {
77  keys[i].discard(nullptr);
78  values[i].discard(nullptr);
79  }
80  keys.clear();
81  values.clear();
82  }
83 
84  DLLLOCAL void add(QoreValue n, QoreValue v, const QoreProgramLocation* loc) {
85  keys.push_back(n);
86  values.push_back(v);
87  lvec.push_back(loc);
88 
89  if (!n || n.isValue()) {
90  QoreStringValueHelper key(n);
91  checkDup(loc, key->c_str());
92  }
93  }
94 
95  DLLLOCAL size_t size() {
96  return keys.size();
97  }
98 
99  // used when converting to the hash map operator
100  DLLLOCAL QoreValue takeFirstKey() {
101  assert(keys.size() == 1);
102  QoreValue rv = keys[0];
103  keys[0].clear();
104  return rv;
105  }
106 
107  // used when converting to the hash map operator
108  DLLLOCAL QoreValue takeFirstValue() {
109  assert(values.size() == 1);
110  QoreValue rv = values[0];
111  values[0].clear();
112  return rv;
113  }
114 
115  DLLLOCAL void setCurly() {
116  assert(!curly);
117  curly = true;
118  }
119 
120  DLLLOCAL bool isCurly() const {
121  return curly;
122  }
123 
124  DLLLOCAL void finalizeBlock(int start_line, int end_line);
125 
126  DLLLOCAL const nvec_t& getKeys() const{
127  return keys;
128  }
129 
130  DLLLOCAL const nvec_t& getValues() const {
131  return values;
132  }
133 
134  DLLLOCAL const tvec_t& getValueTypes() const {
135  return vtypes;
136  }
137 
138  DLLLOCAL virtual int getAsString(QoreString& str, int foff, ExceptionSink* xsink) const;
139 
140  DLLLOCAL virtual QoreString* getAsString(bool& del, int foff, ExceptionSink* xsink) const;
141 
142  DLLLOCAL virtual const char* getTypeName() const;
143 
144  DLLLOCAL virtual void parseInitImpl(QoreValue& val, LocalVar *oflag, int pflag, int &lvids, const QoreTypeInfo *&returnTypeInfo);
145 
146 protected:
147  typedef std::map<std::string, bool> kmap_t;
148  typedef std::vector<const QoreProgramLocation*> lvec_t;
149  nvec_t keys, values;
150  tvec_t vtypes;
151  lvec_t lvec;
152  // to detect duplicate values, only stored during parsing
153  kmap_t kmap;
154  // common value type, if any
155  const QoreTypeInfo* vtype = nullptr;
156  // node type info (derivative of hash)
157  const QoreTypeInfo* typeInfo = nullptr;
158  // flag for a hash expression in curly brackets for the hash version of the map operator
159  bool curly;
160 
161  DLLLOCAL virtual const QoreTypeInfo* getTypeInfo() const {
162  return typeInfo;
163  }
164 
165  DLLLOCAL static void doDuplicateWarning(const QoreProgramLocation* newoc1, const char* key);
166 
167  DLLLOCAL void checkDup(const QoreProgramLocation* loc, const char* key) {
168  std::string kstr(key);
169  kmap_t::iterator i = kmap.lower_bound(kstr);
170  if (i == kmap.end() || i->first != kstr)
171  kmap.insert(i, kmap_t::value_type(kstr, false));
172  else if (!i->second) {
173  doDuplicateWarning(loc, key);
174  i->second = true;
175  }
176  }
177 
178  DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const;
179 };
180 
181 #endif
QoreValue
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:262
QoreStringValueHelper
this class is used to safely manage calls to AbstractQoreNode::getStringRepresentation() when a simpl...
Definition: QoreStringNode.h:302
QoreString
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:81
NT_PARSE_HASH
const qore_type_t NT_PARSE_HASH
type value for QoreParseHashNode
Definition: node_types.h:83
ExceptionSink
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
Qore.h