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;
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.
94inline String recordGramRemoveEscapes(const String& in) { return tableGramRemoveEscapes(in); }
95
96// A function to remove quotes from a quoted string.
97inline String recordGramRemoveQuotes(const String& in) { return tableGramRemoveQuotes(in); }
98
99// </group>
100
101// <summary>
102// Helper class for values in RecordGram
103// </summary>
104
105// <use visibility=local>
106
107// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
108// </reviewed>
109
110// <synopsis>
111// A record selection command is lexically analyzed via flex.
112// An object of this class is used to hold a value (like a name
113// or a literal) for later use in the parser code.
114// </synopsis>
115
117 public:
118 Int type; // # i=Int, f=Double, c=DComplex, s=String r=Regex
119 String str; // # string literal; table name; field name; unit
120 Bool bval; // # bool literal
121 Int64 ival; // # integer literal
122 Double dval[2]; // # Double/DComplex literal
123};
124
125// <summary>
126// Select-class for flex/bison scanner/parser for RecordGram
127// </summary>
128
129// <use visibility=local>
130
131// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
132// </reviewed>
133
134// <prerequisite>
135// # Classes you should understand before using this one.
136// <li> RecordGram.l and .y (flex and bison grammar)
137// </prerequisite>
138
139// <synopsis>
140// This class is needed for the actions in the flex scanner
141// and bison parser.
142// This stores the information by constructing RecordGram objects
143// as needed and storing them in a List.
144//
145// An expression can be given as a string and parsed by the <src>parse</src>
146// function.
147// The grammar used is as much as possible the same as that for the
148// WHERE clause in TaQL (see Note 199).
149// It is possible to set the TaQL style to use by setting
150// <src>theirTaQLStyle</src> before calling the parse functions.
151// A better way is to define the style with the 'USING STYLE' part of
152// the command (similar to TaQL).
153// </synopsis>
154
155// <motivation>
156// It is necessary to be able to give a record select command in ASCII.
157// It is used by the ACSIS people.
158// </motivation>
159
160// # <todo asof="$DATE:$">
161// # A List of bugs, limitations, extensions or planned refinements.
162// # </todo>
163
165 public:
166 // Define the types of tokens in the grammar.
167 enum Token { Node, Val, Elem, Set };
168
169 // Convert an expression string to an expression tree.
170 // The expression will operate on a series of Record objects.
171 // The given record is needed to know the type of the fields used in
172 // the expression.
173 // # The record will be put into the static variable to be used by
174 // # the other functions.
175 static TableExprNode parse(const RecordInterface& record, const String& expression);
176
177 // Convert an expression string to an expression tree.
178 // The expression will operate on the given table.
179 // # The record will be put into the static variable to be used by
180 // # the other functions.
181 static TableExprNode parse(const Table& table, const String& expression);
182
183 // Evaluate an expression to the given type.
184 // The expression can contain variables; their names and values must be
185 // defined in the record.
186 // For double values it is possible to specify the desired unit.
187 // If the expression is a scalar value, the expr2Array functions will
188 // return an array with length 1.
189 // <group>
190 static Bool expr2Bool(const String& expr, const Record& vars = Record());
191 static Int64 expr2Int(const String& expr, const Record& vars = Record());
192 static double expr2Double(const String& expr, const Record& vars = Record(),
193 const String& unit = String());
194 static DComplex expr2Complex(const String& expr, const Record& vars = Record());
195 static String expr2String(const String& expr, const Record& vars = Record());
196 static MVTime expr2Date(const String& expr, const Record& vars = Record());
197 static Array<Bool> expr2ArrayBool(const String& expr, const Record& vars = Record());
198 static Array<Int64> expr2ArrayInt(const String& expr, const Record& vars = Record());
199 static Array<double> expr2ArrayDouble(const String& expr, const Record& vars = Record(),
200 const String& unit = String());
201 static Array<DComplex> expr2ArrayComplex(const String& expr, const Record& vars = Record());
202 static Array<String> expr2ArrayString(const String& expr, const Record& vars = Record());
203 static Array<MVTime> expr2ArrayDate(const String& expr, const Record& vars = Record());
204 // </group>
205
206 // Create a TableExprNode from a literal.
208
209 // Find the field name and create a TableExprNode from it.
210 // To be called only by the yy parser (under theirMutex).
212
213 // Handle a function.
214 // To be called only by the yy parser (under theirMutex).
215 static TableExprNode handleFunc(const String& name, const TableExprNodeSet& arguments);
216
217 // Handle a regex.
219
220 // Set the final node pointer.
221 static void setNodePtr(TableExprNode* nodePtr) { theirNodePtr = nodePtr; }
222
223 // Define the global TaQLStyle to use.
224 // By default it is glish style.
226
227 // Add a token to the list of tokens to be deleted
228 // for the possible tokens in the RecordGram.yy union.
229 // The addToken() functions are to be called only by the yy parser (under theirMutex).
230 static void addToken(TableExprNode* ptr);
231 static void addToken(RecordGramVal* ptr);
232 static void addToken(TableExprNodeSet* ptr);
234
235 // Delete a token and remove from the list.
236 // The deleteToken() functions are to be called only by the yy parser (under theirMutex).
237 static void deleteToken(TableExprNode* ptr);
238 static void deleteToken(RecordGramVal* ptr);
239 static void deleteToken(TableExprNodeSet* ptr);
241
242 private:
243 // Delete all tokens not deleted yet.
244 static void deleteTokenStorage();
245
246 // Do the conversion of an expression string to an expression tree.
247 static TableExprNode doParse(const String& expression);
248
249 // Add a token to the list of tokens to be deleted.
250 static void addToken(void* ptr, Token type) { theirTokens[ptr] = type; }
251 // Remove a token from the list of tokens to be deleted.
252 static void removeToken(void* ptr) { theirTokens.erase(ptr); }
253
254 static std::map<void*, Token> theirTokens;
256 static const Table* theirTabPtr;
258 static std::mutex theirMutex;
259};
260
261} // namespace casacore
262
263#endif
Helper class for values in RecordGram.
Definition RecordGram.h:116
Select-class for flex/bison scanner/parser for RecordGram.
Definition RecordGram.h:164
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:258
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:256
static TableExprNode parse(const RecordInterface &record, const String &expression)
Convert an expression string to an expression tree.
static TableExprNode * theirNodePtr
Definition RecordGram.h:257
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:225
static void addToken(void *ptr, Token type)
Add a token to the list of tokens to be deleted.
Definition RecordGram.h:250
static void setNodePtr(TableExprNode *nodePtr)
Set the final node pointer.
Definition RecordGram.h:221
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:167
static std::map< void *, Token > theirTokens
Definition RecordGram.h:254
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:252
static Array< double > expr2ArrayDouble(const String &expr, const Record &vars=Record(), const String &unit=String())
static const RecordInterface * theirRecPtr
Definition RecordGram.h:255
static void addToken(TableExprNodeSetElem *ptr)
String: the storage and methods of handling collections of characters.
Definition String.h:355
Class to hold the table expression nodes for an element in a set.
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size).
Definition aipsxtype.h:36
String name() const
Return the name of the field.
TableExprNode regex(const TableExprNode &node)
Functions for regular expression matching and pattern matching.
Definition ExprNode.h:1302
RecordInterface()
The default constructor creates an empty record with a variable structure.
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:97