casacore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TaQLNode.h
Go to the documentation of this file.
1 //# TaQLNode.h: Envelope class for a node in the raw TaQL parse tree
2 //# Copyright (C) 2005
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 #ifndef TABLES_TAQLNODE_H
29 #define TABLES_TAQLNODE_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
35 
36 #include <iostream>
37 #include <mutex>
38 #include <vector>
39 #include <memory>
40 
41 namespace casacore { //# NAMESPACE CASACORE - BEGIN
42 
43 //# Forward Declaration.
44 class AipsIO;
45 class TaQLNodeVisitor;
46 class TaQLMultiNode;
47 class TaQLConstNodeRep;
48 class TaQLRegexNodeRep;
49 class TaQLMultiNodeRep;
50 class TaQLQueryNodeRep;
51 
52 // <summary>
53 // Envelope class for a node in the raw TaQL parse tree.
54 // </summary>
55 
56 // <use visibility=local>
57 
58 // <reviewed reviewer="" date="" tests="tTaQLNode">
59 // </reviewed>
60 
61 // <prerequisite>
62 //# Classes you should understand before using this one.
63 // <li> <linkto group=TableGram.h#TableGramFunctions>TableGram</linkto>
64 // <li> Note 199 describing
65 // <a href="../notes/199.html">
66 // TaQL</a>
67 // </prerequisite>
68 
69 // <synopsis>
70 // The result of parsing a TaQL command is stored in TaQLNode objects.
71 // Each part of the command can have its own specialized
72 // <linkto class=TaQLNodeRep>TaQLNodeRep</linkto> object, which forms
73 // the letter in the TaQLNode envelope.
74 // <br>The actual scanning/parsing of the command is done using flex/bison
75 // as defined in the TableGram files.
76 // </synopsis>
77 
78 // <motivation>
79 // The letter-envelope idiom (counted pointer) makes if much easier
80 // to keep track of memory, especially in the case of exceptions.
81 // </motivation>
82 
83 class TaQLNode
84 {
85 public:
86  // Default constructor.
88  {}
89 
90  // Construct for given letter. It takes over the pointer.
92  { itsRep.reset (rep); }
93 
94  // Copy constructor (reference semantics).
95  TaQLNode (const TaQLNode& that)
96  { itsRep = that.itsRep; }
97 
98  // Assignment (reference semantics).
100  { if (this != &that) {
101  itsRep = that.itsRep;
102  }
103  return *this;
104  }
105 
106  // Get the TaQL style.
107  const TaQLStyle& style() const
108  { return itsRep->style(); }
109 
110  // Destructor deletes the letter if no more references.
112  {}
113 
114  // Parse a TaQL command and return the result.
115  // An exception is thrown in case of parse errors.
116  // The parse tree is deleted by function clearNodeCreated.
117  static TaQLNode parse (const String& command);
118 
119  // Does the envelope contain a letter?
120  Bool isValid() const
121  { return Bool(itsRep); }
122 
123  // Return the type of letter.
124  char nodeType() const
125  { return itsRep->nodeType(); }
126 
127  // Get read access to the letter.
128  const TaQLNodeRep* getRep() const
129  { return itsRep.get(); }
130 
131  // Let the visitor visit the node.
132  // If no node, return an empty result.
134  { return (itsRep ? itsRep->visit (visitor) : TaQLNodeResult()); }
135 
136  // Print the node (recursively) in the given stream.
137  void show (std::ostream& os) const
138  { if (itsRep) itsRep->show (os); }
139 
140  // Save and restore the entire parse tree.
141  // <group>
142  void save (AipsIO& aio) const;
143  static TaQLNode restore (AipsIO& aio);
144  // </group>
145 
146 protected:
147  std::shared_ptr<TaQLNodeRep> itsRep;
148 
149 private:
150  // Delete all nodes that were created by the parser.
151  static void clearNodesCreated();
152 
153 public:
154  // Helper functions for save/restore of tree.
155  // <group>
156  void saveNode (AipsIO& aio) const;
157  static TaQLNode restoreNode (AipsIO& aio);
158  static TaQLMultiNode restoreMultiNode (AipsIO& aio);
159  // </group>
160 
161  // The object getting the final tree.
163  // A list of objects created by the parser and deleted at the end.
164  static std::vector<TaQLNode*> theirNodesCreated;
165  // Keep the TaQL style to use.
167  // Use a mutex to guard the statics.
168  static std::mutex theirMutex;
169 };
170 
171 
172 // <summary>
173 // Envelope class for a node containing a constant value.
174 // </summary>
175 // <use visibility=local>
176 // <reviewed reviewer="" date="" tests="tTaQLNode">
177 // </reviewed>
178 // <synopsis>
179 // This is a specialization of the envelope class
180 // <linkto class=TaQLNode>TaQLNode</linkto> for a node containing
181 // a constant value.
182 // </synopsis>
183 class TaQLConstNode: public TaQLNode
184 {
185 public:
186  explicit TaQLConstNode (TaQLConstNodeRep* rep);
187  void setIsTableName();
188  const String& getString() const;
189 private:
191 };
192 
193 
194 // <summary>
195 // Envelope class for a node containing a constant regex value.
196 // </summary>
197 // <use visibility=local>
198 // <reviewed reviewer="" date="" tests="tTaQLNode">
199 // </reviewed>
200 // <synopsis>
201 // This is a specialization of the envelope class
202 // <linkto class=TaQLNode>TaQLNode</linkto> for a node containing
203 // a constant regex or pattern value.
204 // </synopsis>
205 class TaQLRegexNode: public TaQLNode
206 {
207 public:
208  explicit TaQLRegexNode (TaQLRegexNodeRep* rep);
209  const String& getString() const;
210  Bool caseInsensitive() const;
211  Bool negate() const;
212 private:
214 };
215 
216 
217 // <summary>
218 // Envelope class for a node containing a list of nodes.
219 // </summary>
220 // <use visibility=local>
221 // <reviewed reviewer="" date="" tests="tTaQLNode">
222 // </reviewed>
223 // <synopsis>
224 // This is a specialization of the envelope class
225 // <linkto class=TaQLNode>TaQLNode</linkto> for a node containing
226 // a list of nodes.
227 // </synopsis>
228 class TaQLMultiNode: public TaQLNode
229 {
230 public:
231  TaQLMultiNode();
232  explicit TaQLMultiNode (Bool isSetOrArray);
234  void add (const TaQLNode& node);
235  void add (TaQLNodeRep* noderep);
236  void setIsSetOrArray();
237  void setPPFix (const String& prefix, const String& postfix);
238  void setSeparator (const String& sep);
239  void setSeparator (uInt incr, const String& sep);
241  { return itsNRep; }
242 private:
244 };
245 
246 
247 // <summary>
248 // Envelope class for a node containing a selection command.
249 // </summary>
250 // <use visibility=local>
251 // <reviewed reviewer="" date="" tests="tTaQLNode">
252 // </reviewed>
253 // <synopsis>
254 // This is a specialization of the envelope class
255 // <linkto class=TaQLNode>TaQLNode</linkto> for a node containing
256 // a selection command.
257 // </synopsis>
258 class TaQLQueryNode: public TaQLNode
259 {
260 public:
262  void setBrackets();
263  void setNoExecute();
264  void setFromExecute();
265 private:
267 };
268 
269 
270 } //# NAMESPACE CASACORE - END
271 
272 #endif
char nodeType() const
Return the type of letter.
Definition: TaQLNode.h:124
Raw TaQL parse tree node defining a selection command.
Definition: TaQLNodeDer.h:743
TaQLMultiNodeRep * itsNRep
Definition: TaQLNode.h:243
TaQLQueryNodeRep * itsNRep
Definition: TaQLNode.h:266
Envelope class for a node containing a selection command.
Definition: TaQLNode.h:258
static std::mutex theirMutex
Use a mutex to guard the statics.
Definition: TaQLNode.h:168
static TaQLStyle theirStyle
Keep the TaQL style to use.
Definition: TaQLNode.h:166
static TaQLMultiNode restoreMultiNode(AipsIO &aio)
void add(const TaQLNode &node)
static TaQLNode parse(const String &command)
Parse a TaQL command and return the result.
AipsIO is the object persistency mechanism of Casacore.
Definition: AipsIO.h:168
TaQLRegexNode(TaQLRegexNodeRep *rep)
const TaQLStyle & style() const
Get the TaQL style.
Definition: TaQLNode.h:107
TaQLConstNode(TaQLConstNodeRep *rep)
void setSeparator(const String &sep)
Bool isValid() const
Does the envelope contain a letter?
Definition: TaQLNode.h:120
Raw TaQL parse tree node defining a constant value.
Definition: TaQLNodeDer.h:61
static TaQLNode restore(AipsIO &aio)
void show(std::ostream &os) const
Print the node (recursively) in the given stream.
Definition: TaQLNode.h:137
Raw TaQL parse tree node defining a list of nodes.
Definition: TaQLNodeDer.h:247
void setPPFix(const String &prefix, const String &postfix)
static TaQLNode restoreNode(AipsIO &aio)
static TaQLNode theirNode
The object getting the final tree.
Definition: TaQLNode.h:162
Envelope class for a node containing a list of nodes.
Definition: TaQLNode.h:228
TaQLNode(const TaQLNode &that)
Copy constructor (reference semantics).
Definition: TaQLNode.h:95
Envelope class for a node containing a constant regex value.
Definition: TaQLNode.h:205
Raw TaQL parse tree node defining a constant regex value.
Definition: TaQLNodeDer.h:118
const String & getString() const
Class with static members defining the TaQL style.
Definition: TaQLStyle.h:64
Bool caseInsensitive() const
TaQLNode(TaQLNodeRep *rep)
Construct for given letter.
Definition: TaQLNode.h:91
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
~TaQLNode()
Destructor deletes the letter if no more references.
Definition: TaQLNode.h:111
TaQLNode & operator=(const TaQLNode &that)
Assignment (reference semantics).
Definition: TaQLNode.h:99
std::shared_ptr< TaQLNodeRep > itsRep
Definition: TaQLNode.h:147
const String & getString() const
TaQLRegexNodeRep * itsNRep
Definition: TaQLNode.h:213
Envelope class for a node containing a constant value.
Definition: TaQLNode.h:183
Envelope class to hold the result of a visit to the node tree.
void save(AipsIO &aio) const
Save and restore the entire parse tree.
static void clearNodesCreated()
Delete all nodes that were created by the parser.
static std::vector< TaQLNode * > theirNodesCreated
A list of objects created by the parser and deleted at the end.
Definition: TaQLNode.h:164
TaQLNode()
Default constructor.
Definition: TaQLNode.h:87
String: the storage and methods of handling collections of characters.
Definition: String.h:225
void saveNode(AipsIO &aio) const
Helper functions for save/restore of tree.
const TaQLNodeRep * getRep() const
Get read access to the letter.
Definition: TaQLNode.h:128
Envelope class for a node in the raw TaQL parse tree.
Definition: TaQLNode.h:83
Class to visit the nodes in the raw TaQL parse tree.
TaQLQueryNode(TaQLQueryNodeRep *rep)
TaQLNodeResult visit(TaQLNodeVisitor &visitor) const
Let the visitor visit the node.
Definition: TaQLNode.h:133
TaQLConstNodeRep * itsNRep
Definition: TaQLNode.h:190
const TaQLMultiNodeRep * getMultiRep() const
Definition: TaQLNode.h:240
Representation of a node in the raw TaQL parse tree.
Definition: TaQLNodeRep.h:76
unsigned int uInt
Definition: aipstype.h:51