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/OS/Mutex.h>
38 #include <map>
39 
40 namespace casacore { //# NAMESPACE CASACORE - BEGIN
41 
42 //# Forward Declarations
43 class TableExprNode;
44 class TableExprNodeSet;
45 class TableExprNodeSetElem;
46 class RecordInterface;
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.
90 Int& recordGramPosition();
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  // Create a TableExprNode from a literal.
198  static TableExprNode handleLiteral (RecordGramVal*);
199 
200  // Find the field name and create a TableExprNode from it.
201  static TableExprNode handleField (const String& name);
202 
203  // Handle a function.
204  static TableExprNode handleFunc (const String& name,
205  const TableExprNodeSet& arguments);
206 
207  // Handle a regex.
208  static TableExprNode handleRegex (const TableExprNode& left,
209  const String& regex);
210 
211  // Set the final node pointer.
212  static void setNodePtr (TableExprNode* nodePtr)
213  { theirNodePtr = nodePtr; }
214 
215  // Define the global TaQLStyle to use.
216  // By default it is glish style.
218 
219  // Add a token to the list of tokens to be deleted
220  // for the possible tokens in the RecordGram.yy union.
221  static void addToken (TableExprNode* ptr);
222  static void addToken (RecordGramVal* ptr);
223  static void addToken (TableExprNodeSet* ptr);
224  static void addToken (TableExprNodeSetElem* ptr);
225  // Delete a token and remove from the list.
226  static void deleteToken (TableExprNode* ptr);
227  static void deleteToken (RecordGramVal* ptr);
228  static void deleteToken (TableExprNodeSet* ptr);
229  static void deleteToken (TableExprNodeSetElem* ptr);
230  // Delete all tokens not deleted yet.
231  static void deleteTokenStorage();
232 
233 private:
234  // Do the conversion of an expression string to an expression tree.
235  static TableExprNode doParse (const String& expression);
236 
237  // Add a token to the list of tokens to be deleted.
238  static void addToken (void* ptr, Token type)
239  { theirTokens[ptr] = type; }
240  // Remove a token from the list of tokens to be deleted.
241  static void removeToken (void* ptr)
242  { theirTokens.erase (ptr); }
243 
244  static std::map<void*, Token> theirTokens;
246  static const Table* theirTabPtr;
249 };
250 
251 
252 
253 } //# NAMESPACE CASACORE - END
254 
255 #endif
static const RecordInterface * theirRecPtr
Definition: RecordGram.h:245
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:248
static TableExprNode * theirNodePtr
Definition: RecordGram.h:247
Main interface class to a read/write table.
Definition: Table.h:149
static void addToken(void *ptr, Token type)
Add a token to the list of tokens to be deleted.
Definition: RecordGram.h:238
Class to hold multiple table expression nodes.
Definition: ExprNodeSet.h:306
Handle class for a table column expression tree.
Definition: ExprNode.h:614
String recordGramRemoveQuotes(const String &in)
A function to remove quotes from a quoted string.
Definition: RecordGram.h:100
Helper class for values in RecordGram.
Definition: RecordGram.h:122
static const Table * theirTabPtr
Definition: RecordGram.h:246
static void removeToken(void *ptr)
Remove a token from the list of tokens to be deleted.
Definition: RecordGram.h:241
static std::map< void *, Token > theirTokens
Definition: RecordGram.h:244
Class to hold the table expression nodes for an element in a set.
Definition: ExprNodeSet.h:94
double Double
Definition: aipstype.h:55
Class with static members defining the TaQL style.
Definition: TaQLStyle.h:64
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:212
Select-class for flex/bison scanner/parser for RecordGram.
Definition: RecordGram.h:175
String recordGramRemoveEscapes(const String &in)
A function to remove escaped characters.
Definition: RecordGram.h:96
Wrapper around a pthreads mutex.
Definition: Mutex.h:49
Token
Define the types of tokens in the grammar.
Definition: RecordGram.h:179
TableExprNode regex(const TableExprNode &node)
Functions for regular expression matching and pattern matching.
Definition: ExprNode.h:1647
String: the storage and methods of handling collections of characters.
Definition: String.h:223
Abstract base class for Record classes.
static TaQLStyle theirTaQLStyle
Define the global TaQLStyle to use.
Definition: RecordGram.h:217
this file contains all the compiler specific defines
Definition: mainpage.dox:28