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