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