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 <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.
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());
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.
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
casacore::RecordGram::expr2ArrayString
static Array< String > expr2ArrayString(const String &expr, const Record &vars=Record())
casacore::RecordGram::setNodePtr
static void setNodePtr(TableExprNode *nodePtr)
Set the final node pointer.
Definition: RecordGram.h:244
Complexfwd_global_functions_Complexfwd::casacore::DComplex
std::complex< Double > DComplex
Definition: Complexfwd.h:50
casacore::MVTime
Definition: MVTime.h:269
casacore::RecordGram
Select-class for flex/bison scanner/parser for RecordGram.
Definition: RecordGram.h:177
casacore::RecordGram::addToken
static void addToken(TableExprNode *ptr)
Add a token to the list of tokens to be deleted for the possible tokens in the RecordGram....
casacore::RecordGram::expr2ArrayComplex
static Array< DComplex > expr2ArrayComplex(const String &expr, const Record &vars=Record())
casacore::RecordGram::expr2ArrayDate
static Array< MVTime > expr2ArrayDate(const String &expr, const Record &vars=Record())
casacore::RecordGram::expr2Int
static Int64 expr2Int(const String &expr, const Record &vars=Record())
casacore::RecordGram::deleteToken
static void deleteToken(TableExprNodeSet *ptr)
casacore::RecordGram_global_functions_RecordGramFunctions::recordGramParseCommand
int recordGramParseCommand(const String &command)
Declare the bison parser (is implemented by bison command).
casacore::RecordGram_global_functions_RecordGramFunctions
Definition: RecordGram.h:81
casacore::RecordGram::expr2String
static String expr2String(const String &expr, const Record &vars=Record())
casacore::RecordGram::theirRecPtr
static const RecordInterface * theirRecPtr
Definition: RecordGram.h:281
casacore::RecordGram::theirTaQLStyle
static TaQLStyle theirTaQLStyle
Define the global TaQLStyle to use.
Definition: RecordGram.h:249
casacore::RecordGram_global_functions_RecordGramFunctions::recordGramInput
int recordGramInput(char *buf, int max_size)
Declare the input routine for flex/bison.
casacore::Table
Definition: Table.h:154
casacore::RecordGram::Elem
@ Elem
Definition: RecordGram.h:180
casacore::Double
double Double
Definition: aipstype.h:55
casacore::RecordGram::addToken
static void addToken(RecordGramVal *ptr)
casacore::RecordGram_global_functions_RecordGramFunctions::RecordGramerror
void RecordGramerror(const char *)
The yyerror function for the parser.
casacore::RecordGram::deleteToken
static void deleteToken(RecordGramVal *ptr)
casacore::RecordGram::Token
Token
Define the types of tokens in the grammar.
Definition: RecordGram.h:180
casacore::RecordGram::deleteToken
static void deleteToken(TableExprNode *ptr)
Delete a token and remove from the list.
casacore::RecordGram::parse
static TableExprNode parse(const RecordInterface &record, const String &expression)
Convert an expression string to an expression tree.
casacore::RecordGramVal::str
String str
Definition: RecordGram.h:127
casacore::regex
TableExprNode regex(const TableExprNode &node)
Functions for regular expression matching and pattern matching.
Definition: ExprNode.h:1440
casacore::RecordGram::expr2ArrayInt
static Array< Int64 > expr2ArrayInt(const String &expr, const Record &vars=Record())
casacore::RecordGram::theirTokens
static std::map< void *, Token > theirTokens
Definition: RecordGram.h:280
casacore::RecordGram::handleField
static TableExprNode handleField(const String &name)
Find the field name and create a TableExprNode from it.
casacore::RecordGram::addToken
static void addToken(void *ptr, Token type)
Add a token to the list of tokens to be deleted.
Definition: RecordGram.h:274
casacore::RecordGram::Set
@ Set
Definition: RecordGram.h:180
casacore::Int
int Int
Definition: aipstype.h:50
casacore
this file contains all the compiler specific defines
Definition: mainpage.dox:28
casacore::RecordGram::Node
@ Node
Definition: RecordGram.h:180
casacore::RecordGram::handleRegex
static TableExprNode handleRegex(const TableExprNode &left, const String &regex)
Handle a regex.
casacore::RecordGram::doParse
static TableExprNode doParse(const String &expression)
Do the conversion of an expression string to an expression tree.
casacore::RecordGram::deleteToken
static void deleteToken(TableExprNodeSetElem *ptr)
casacore::TableExprNodeSet
Class to hold multiple table expression nodes.
Definition: ExprNodeSet.h:312
casacore::RecordGram_global_functions_RecordGramFunctions::recordGramPosition
Int & recordGramPosition()
Give the current position in the string.
casacore::RecordGram::removeToken
static void removeToken(void *ptr)
Remove a token from the list of tokens to be deleted.
Definition: RecordGram.h:277
casacore::TableExprNode
Definition: ExprNode.h:156
casacore::RecordGram::handleFunc
static TableExprNode handleFunc(const String &name, const TableExprNodeSet &arguments)
Handle a function.
casacore::RecordGram::expr2Bool
static Bool expr2Bool(const String &expr, const Record &vars=Record())
Evaluate an expression to the given type.
casacore::TaQLStyle
Definition: TaQLStyle.h:65
casacore::RecordGram::expr2ArrayBool
static Array< Bool > expr2ArrayBool(const String &expr, const Record &vars=Record())
casacore::RecordGram::Val
@ Val
Definition: RecordGram.h:180
casacore::RecordInterface
Definition: RecordInterface.h:145
casacore::RecordGram::theirTabPtr
static const Table * theirTabPtr
Definition: RecordGram.h:282
casacore::Int64
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition: aipsxtype.h:38
casacore::RecordGramVal
Helper class for values in RecordGram.
Definition: RecordGram.h:124
casacore::RecordGram::deleteTokenStorage
static void deleteTokenStorage()
Delete all tokens not deleted yet.
casacore::RecordGram::addToken
static void addToken(TableExprNodeSetElem *ptr)
casacore::Array< Bool >
casacore::RecordGramVal::type
Int type
Definition: RecordGram.h:126
casacore::RecordGram::theirMutex
static Mutex theirMutex
Definition: RecordGram.h:284
casacore::String
String: the storage and methods of handling collections of characters.
Definition: String.h:223
casacore::Mutex
Definition: Mutex.h:59
casacore::Bool
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
casacore::RecordGram::parse
static TableExprNode parse(const Table &table, const String &expression)
Convert an expression string to an expression tree.
casacore::RecordGramVal::bval
Bool bval
Definition: RecordGram.h:128
casacore::Record
Definition: Record.h:181
casacore::RecordGram::expr2ArrayDouble
static Array< double > expr2ArrayDouble(const String &expr, const Record &vars=Record(), const String &unit=String())
casacore::RecordGram::expr2Complex
static DComplex expr2Complex(const String &expr, const Record &vars=Record())
casacore::RecordGramVal::ival
Int64 ival
Definition: RecordGram.h:129
casacore::RecordGramVal::dval
Double dval[2]
Definition: RecordGram.h:130
casacore::RecordGram::addToken
static void addToken(TableExprNodeSet *ptr)
casacore::TableExprNodeSetElem
Definition: ExprNodeSet.h:95
casacore::RecordGram_global_functions_RecordGramFunctions::recordGramRemoveQuotes
String recordGramRemoveQuotes(const String &in)
A function to remove quotes from a quoted string.
Definition: RecordGram.h:101
casacore::RecordGram::expr2Double
static double expr2Double(const String &expr, const Record &vars=Record(), const String &unit=String())
casacore::RecordGram::theirNodePtr
static TableExprNode * theirNodePtr
Definition: RecordGram.h:283
casacore::RecordGram::handleLiteral
static TableExprNode handleLiteral(RecordGramVal *)
Create a TableExprNode from a literal.
casacore::RecordGram::expr2Date
static MVTime expr2Date(const String &expr, const Record &vars=Record())
casacore::RecordGram_global_functions_RecordGramFunctions::recordGramRemoveEscapes
String recordGramRemoveEscapes(const String &in)
A function to remove escaped characters.
Definition: RecordGram.h:97