casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RecordGram.h
Go to the documentation of this file.
1 //# RecordGram.h: Grammar for record command lines
2 //# Copyright (C) 2000
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_RECORDGRAM_H
29 #define TABLES_RECORDGRAM_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
39 #include <casacore/casa/OS/Mutex.h>
40 #include <map>
41 
42 namespace casacore { //# NAMESPACE CASACORE - BEGIN
43 
44 //# Forward Declarations
45 class TableExprNode;
46 class TableExprNodeSet;
47 class TableExprNodeSetElem;
48 class Table;
49 
50 // <summary>
51 // Global functions for flex/bison scanner/parser for RecordGram
52 // </summary>
53 
54 // <use visibility=local>
55 
56 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
57 // </reviewed>
58 
59 // <prerequisite>
60 //# Classes you should understand before using this one.
61 // <li> RecordGram.l and .y (flex and bison grammar)
62 // </prerequisite>
63 
64 // <synopsis>
65 // Global functions are needed to define the input of the flex scanner
66 // and to start the bison parser.
67 // The input is taken from a string.
68 // </synopsis>
69 
70 // <motivation>
71 // It is necessary to be able to give a record select command in ASCII.
72 // This can be used in a CLI or in the record browser to get a subset
73 // of a record or to sort a record.
74 // </motivation>
75 
76 // <todo asof="$DATE:$">
77 //# A List of bugs, limitations, extensions or planned refinements.
78 // </todo>
79 
80 // <group name=RecordGramFunctions>
81 
82 // Declare the bison parser (is implemented by bison command).
83 int recordGramParseCommand (const String& command);
84 
85 // The yyerror function for the parser.
86 // It throws an exception with the current token.
87 void RecordGramerror (const char*);
88 
89 // Give the current position in the string.
90 // This can be used when parse errors occur.
91 Int& recordGramPosition();
92 
93 // Declare the input routine for flex/bison.
94 int recordGramInput (char* buf, int max_size);
95 
96 // A function to remove escaped characters.
98  { return tableGramRemoveEscapes (in); }
99 
100 // A function to remove quotes from a quoted string.
102  { return tableGramRemoveQuotes (in); }
103 
104 // </group>
105 
106 
107 
108 // <summary>
109 // Helper class for values in RecordGram
110 // </summary>
111 
112 // <use visibility=local>
113 
114 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
115 // </reviewed>
116 
117 // <synopsis>
118 // A record selection command is lexically analyzed via flex.
119 // An object of this class is used to hold a value (like a name
120 // or a literal) for later use in the parser code.
121 // </synopsis>
122 
124 {
125 public:
126  Int type; //# i=Int, f=Double, c=DComplex, s=String r=Regex
127  String str; //# string literal; table name; field name; unit
128  Bool bval; //# bool literal
129  Int64 ival; //# integer literal
130  Double dval[2]; //# Double/DComplex literal
131 };
132 
133 
134 
135 
136 // <summary>
137 // Select-class for flex/bison scanner/parser for RecordGram
138 // </summary>
139 
140 // <use visibility=local>
141 
142 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
143 // </reviewed>
144 
145 // <prerequisite>
146 //# Classes you should understand before using this one.
147 // <li> RecordGram.l and .y (flex and bison grammar)
148 // </prerequisite>
149 
150 // <synopsis>
151 // This class is needed for the the actions in the flex scanner
152 // and bison parser.
153 // This stores the information by constructing RecordGram objects
154 // as needed and storing them in a List.
155 //
156 // An expression can be given as a string and parsed by the <src>parse</src>
157 // function.
158 // The grammar used is as much as possible the same as that for the
159 // WHERE clause in TaQL (see Note 199).
160 // It is possible to set the TaQL style to use by setting
161 // <src>theirTaQLStyle</src> before calling the parse functions.
162 // A better way is to define the style with the 'USING STYLE' part of
163 // the command (similar to TaQL).
164 // </synopsis>
165 
166 // <motivation>
167 // It is necessary to be able to give a record select command in ASCII.
168 // It is used by the ACSIS people.
169 // </motivation>
170 
171 //# <todo asof="$DATE:$">
172 //# A List of bugs, limitations, extensions or planned refinements.
173 //# </todo>
174 
175 
177 {
178 public:
179  // Define the types of tokens in the grammar.
180  enum Token {Node, Val, Elem, Set};
181 
182  // Convert an expression string to an expression tree.
183  // The expression will operate on a series of Record objects.
184  // The given record is needed to know the type of the fields used in
185  // the expression.
186  //# The record will be put into the static variable to be used by
187  //# the other functions.
188  static TableExprNode parse (const RecordInterface& record,
189  const String& expression);
190 
191  // Convert an expression string to an expression tree.
192  // The expression will operate on the given table.
193  //# The record will be put into the static variable to be used by
194  //# the other functions.
195  static TableExprNode parse (const Table& table,
196  const String& expression);
197 
198  // Evaluate an expression to the given type.
199  // The expression can contain variables; their names and values must be
200  // defined in the record.
201  // For double values it is possible to specify the desired unit.
202  // If the expression is a scalar value, the expr2Array functions will
203  // return an array with length 1.
204  // <group>
205  static Bool expr2Bool (const String& expr, const Record& vars=Record());
206  static Int64 expr2Int (const String& expr, const Record& vars=Record());
207  static double expr2Double (const String& expr, const Record& vars=Record(),
208  const String& unit=String());
209  static DComplex expr2Complex (const String& expr, const Record& vars=Record());
210  static String expr2String (const String& expr, const Record& vars=Record());
211  static MVTime expr2Date (const String& expr, const Record& vars=Record());
212  static Array<Bool> expr2ArrayBool (const String& expr,
213  const Record& vars=Record());
214  static Array<Int64> expr2ArrayInt (const String& expr,
215  const Record& vars=Record());
216  static Array<double> expr2ArrayDouble (const String& expr,
217  const Record& vars=Record(),
218  const String& unit=String());
219  static Array<DComplex> expr2ArrayComplex (const String& expr,
220  const Record& vars=Record());
221  static Array<String> expr2ArrayString (const String& expr,
222  const Record& vars=Record());
223  static Array<MVTime> expr2ArrayDate (const String& expr,
224  const Record& vars=Record());
225  // </group>
226 
227  // Create a TableExprNode from a literal.
229 
230  // Find the field name and create a TableExprNode from it.
231  // To be called only by the yy parser (under theirMutex).
232  static TableExprNode handleField (const String& name);
233 
234  // Handle a function.
235  // To be called only by the yy parser (under theirMutex).
236  static TableExprNode handleFunc (const String& name,
237  const TableExprNodeSet& arguments);
238 
239  // Handle a regex.
240  static TableExprNode handleRegex (const TableExprNode& left,
241  const String& regex);
242 
243  // Set the final node pointer.
244  static void setNodePtr (TableExprNode* nodePtr)
245  { theirNodePtr = nodePtr; }
246 
247  // Define the global TaQLStyle to use.
248  // By default it is glish style.
250 
251  // Add a token to the list of tokens to be deleted
252  // for the possible tokens in the RecordGram.yy union.
253  // The addToken() functions are to be called only by the yy parser (under theirMutex).
254  static void addToken (TableExprNode* ptr);
255  static void addToken (RecordGramVal* ptr);
256  static void addToken (TableExprNodeSet* ptr);
257  static void addToken (TableExprNodeSetElem* ptr);
258 
259  // Delete a token and remove from the list.
260  // The deleteToken() functions are to be called only by the yy parser (under theirMutex).
261  static void deleteToken (TableExprNode* ptr);
262  static void deleteToken (RecordGramVal* ptr);
263  static void deleteToken (TableExprNodeSet* ptr);
264  static void deleteToken (TableExprNodeSetElem* ptr);
265 
266 private:
267  // Delete all tokens not deleted yet.
268  static void deleteTokenStorage();
269 
270  // Do the conversion of an expression string to an expression tree.
271  static TableExprNode doParse (const String& expression);
272 
273  // Add a token to the list of tokens to be deleted.
274  static void addToken (void* ptr, Token type)
275  { theirTokens[ptr] = type; }
276  // Remove a token from the list of tokens to be deleted.
277  static void removeToken (void* ptr)
278  { theirTokens.erase (ptr); }
279 
280  static std::map<void*, Token> theirTokens;
282  static const Table* theirTabPtr;
285 };
286 
287 
288 
289 } //# NAMESPACE CASACORE - END
290 
291 #endif
static const RecordInterface * theirRecPtr
Definition: RecordGram.h:281
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition: aipsxtype.h:38
int Int
Definition: aipstype.h:50
static Mutex theirMutex
Definition: RecordGram.h:284
static DComplex expr2Complex(const String &expr, const Record &vars=Record())
static TableExprNode * theirNodePtr
Definition: RecordGram.h:283
static TableExprNode handleField(const String &name)
Find the field name and create a TableExprNode from it.
Main interface class to a read/write table.
Definition: Table.h:153
static void addToken(void *ptr, Token type)
Add a token to the list of tokens to be deleted.
Definition: RecordGram.h:274
static TableExprNode handleFunc(const String &name, const TableExprNodeSet &arguments)
Handle a function.
Class to hold multiple table expression nodes.
Definition: ExprNodeSet.h:311
Handle class for a table column expression tree.
Definition: ExprNode.h:155
String recordGramRemoveQuotes(const String &in)
A function to remove quotes from a quoted string.
Definition: RecordGram.h:101
static String expr2String(const String &expr, const Record &vars=Record())
Helper class for values in RecordGram.
Definition: RecordGram.h:123
virtual casacore::String type() const
Implements RegionShape::type.
Definition: RegionShapes.h:548
static const Table * theirTabPtr
Definition: RecordGram.h:282
static void removeToken(void *ptr)
Remove a token from the list of tokens to be deleted.
Definition: RecordGram.h:277
ABSTRACT CLASSES Abstract class for colors Any implementation of color should be able to provide a hexadecimal form of the if a human readable name(i.e."black").In many places throughout the plotter
static void deleteTokenStorage()
Delete all tokens not deleted yet.
static TableExprNode handleLiteral(RecordGramVal *)
Create a TableExprNode from a literal.
static TableExprNode doParse(const String &expression)
Do the conversion of an expression string to an expression tree.
static std::map< void *, Token > theirTokens
Definition: RecordGram.h:280
static void addToken(TableExprNode *ptr)
Add a token to the list of tokens to be deleted for the possible tokens in the RecordGram.yy union.
static Bool expr2Bool(const String &expr, const Record &vars=Record())
Evaluate an expression to the given type.
static TableExprNode handleRegex(const TableExprNode &left, const String &regex)
Handle a regex.
Class to hold the table expression nodes for an element in a set.
Definition: ExprNodeSet.h:94
double Double
Definition: aipstype.h:55
static Array< String > expr2ArrayString(const String &expr, const Record &vars=Record())
static MVTime expr2Date(const String &expr, const Record &vars=Record())
Class with static members defining the TaQL style.
Definition: TaQLStyle.h:64
static double expr2Double(const String &expr, const Record &vars=Record(), const String &unit=String())
static Int64 expr2Int(const String &expr, const Record &vars=Record())
A hierarchical collection of named fields of various types.
Definition: Record.h:180
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
static void setNodePtr(TableExprNode *nodePtr)
Set the final node pointer.
Definition: RecordGram.h:244
static TableExprNode parse(const RecordInterface &record, const String &expression)
Convert an expression string to an expression tree.
Select-class for flex/bison scanner/parser for RecordGram.
Definition: RecordGram.h:176
String recordGramRemoveEscapes(const String &in)
A function to remove escaped characters.
Definition: RecordGram.h:97
Wrapper around a pthreads mutex.
Definition: Mutex.h:58
static Array< double > expr2ArrayDouble(const String &expr, const Record &vars=Record(), const String &unit=String())
Token
Define the types of tokens in the grammar.
Definition: RecordGram.h:180
TableExprNode regex(const TableExprNode &node)
Functions for regular expression matching and pattern matching.
Definition: ExprNode.h:1440
static Array< Int64 > expr2ArrayInt(const String &expr, const Record &vars=Record())
String: the storage and methods of handling collections of characters.
Definition: String.h:223
static Array< DComplex > expr2ArrayComplex(const String &expr, const Record &vars=Record())
Abstract base class for Record classes.
Class to handle date/time type conversions and I/O.
Definition: MVTime.h:269
static TaQLStyle theirTaQLStyle
Define the global TaQLStyle to use.
Definition: RecordGram.h:249
static Array< MVTime > expr2ArrayDate(const String &expr, const Record &vars=Record())
static Array< Bool > expr2ArrayBool(const String &expr, const Record &vars=Record())
static void deleteToken(TableExprNode *ptr)
Delete a token and remove from the list.
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42