casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
RecordGram.h
Go to the documentation of this file.
00001 //# RecordGram.h: Grammar for record command lines
00002 //# Copyright (C) 2000
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: RecordGram.h 21051 2011-04-20 11:46:29Z gervandiepen $
00027 
00028 #ifndef TABLES_RECORDGRAM_H
00029 #define TABLES_RECORDGRAM_H
00030 
00031 //# Includes
00032 #include <casa/BasicSL/String.h>
00033 #include <tables/Tables/TableGram.h>
00034 #include <tables/Tables/TaQLStyle.h>
00035 #include <tables/Tables/Table.h>
00036 #include <casa/OS/Mutex.h>
00037 
00038 namespace casa { //# NAMESPACE CASA - BEGIN
00039 
00040 //# Forward Declarations
00041 class TableExprNode;
00042 class TableExprNodeSet;
00043 class RecordInterface;
00044 class Table;
00045 
00046 // <summary>
00047 // Global functions for flex/bison scanner/parser for RecordGram
00048 // </summary>
00049 
00050 // <use visibility=local>
00051 
00052 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
00053 // </reviewed>
00054 
00055 // <prerequisite>
00056 //# Classes you should understand before using this one.
00057 //  <li> RecordGram.l and .y  (flex and bison grammar)
00058 // </prerequisite>
00059 
00060 // <synopsis> 
00061 // Global functions are needed to define the input of the flex scanner
00062 // and to start the bison parser.
00063 // The input is taken from a string.
00064 // </synopsis> 
00065 
00066 // <motivation>
00067 // It is necessary to be able to give a record select command in ASCII.
00068 // This can be used in a CLI or in the record browser to get a subset
00069 // of a record or to sort a record.
00070 // </motivation>
00071 
00072 // <todo asof="$DATE:$">
00073 //# A List of bugs, limitations, extensions or planned refinements.
00074 // </todo>
00075 
00076 // <group name=RecordGramFunctions>
00077 
00078 // Declare the bison parser (is implemented by bison command).
00079 int recordGramParseCommand (const String& command);
00080 
00081 // The yyerror function for the parser.
00082 // It throws an exception with the current token.
00083 void RecordGramerror (const char*);
00084 
00085 // Give the current position in the string.
00086 // This can be used when parse errors occur.
00087 Int& recordGramPosition();
00088 
00089 // Declare the input routine for flex/bison.
00090 int recordGramInput (char* buf, int max_size);
00091 
00092 // A function to remove escaped characters.
00093 inline String recordGramRemoveEscapes (const String& in)
00094     { return tableGramRemoveEscapes (in); }
00095 
00096 // A function to remove quotes from a quoted string.
00097 inline String recordGramRemoveQuotes (const String& in)
00098     { return tableGramRemoveQuotes (in); }
00099 
00100 // </group>
00101 
00102 
00103 
00104 // <summary>
00105 // Helper class for values in RecordGram 
00106 // </summary>
00107 
00108 // <use visibility=local>
00109 
00110 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
00111 // </reviewed>
00112 
00113 // <synopsis> 
00114 // A record selection command is lexically analyzed via flex.
00115 // An object of this class is used to hold a value (like a name
00116 // or a literal) for later use in the parser code.
00117 // </synopsis> 
00118 
00119 class RecordGramVal
00120 {
00121 public:
00122     Int      type;          //# i=Int, f=Double, c=DComplex, s=String r=Regex
00123     String   str;           //# string literal; table name; field name; unit
00124     Bool     bval;          //# bool literal
00125     Int64    ival;          //# integer literal
00126     Double   dval[2];       //# Double/DComplex literal
00127 };
00128 
00129 
00130 
00131 
00132 // <summary>
00133 // Select-class for flex/bison scanner/parser for RecordGram
00134 // </summary>
00135 
00136 // <use visibility=local>
00137 
00138 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
00139 // </reviewed>
00140 
00141 // <prerequisite>
00142 //# Classes you should understand before using this one.
00143 //  <li> RecordGram.l and .y  (flex and bison grammar)
00144 // </prerequisite>
00145 
00146 // <synopsis> 
00147 // This class is needed for the the actions in the flex scanner
00148 // and bison parser.
00149 // This stores the information by constructing RecordGram objects
00150 // as needed and storing them in a List.
00151 //
00152 // An expression can be given as a string and parsed by the <src>parse</src>
00153 // function.
00154 // The grammar used is as much as possible the same as that for the
00155 // WHERE clause in TaQL (see AIPS++ note 199).
00156 // It is possible to set the TaQL style to use by setting
00157 // <src>theirTaQLStyle</src> before calling the parse functions.
00158 // A better way is to define the style with the 'USING STYLE' part of
00159 // the command (similar to TaQL).
00160 // </synopsis> 
00161 
00162 // <motivation>
00163 // It is necessary to be able to give a record select command in ASCII.
00164 // It is used by the ACSIS people.
00165 // </motivation>
00166 
00167 //# <todo asof="$DATE:$">
00168 //# A List of bugs, limitations, extensions or planned refinements.
00169 //# </todo>
00170 
00171 
00172 class RecordGram
00173 {
00174 public:
00175     // Convert an expression string to an expression tree.
00176     // The expression will operate on a series of Record objects.
00177     // The given record is needed to know the type of the fields used in
00178     // the expression.
00179     //# The record will be put into the static variable to be used by
00180     //# the other functions.
00181     static TableExprNode parse (const RecordInterface& record,
00182                                 const String& expression);
00183 
00184     // Convert an expression string to an expression tree.
00185     // The expression will operate on the given table.
00186     //# The record will be put into the static variable to be used by
00187     //# the other functions.
00188     static TableExprNode parse (const Table& table,
00189                                 const String& expression);
00190 
00191     // Create a TableExprNode from a literal.
00192     static TableExprNode handleLiteral (RecordGramVal*);
00193 
00194     // Find the field name and create a TableExprNode from it.
00195     static TableExprNode handleField (const String& name);
00196 
00197     // Handle a function.
00198     static TableExprNode handleFunc (const String& name,
00199                                      const TableExprNodeSet& arguments);
00200 
00201     // Handle a regex.
00202     static TableExprNode handleRegex (const TableExprNode& left,
00203                                       const String& regex);
00204 
00205     // Set the final node pointer.
00206     static void setNodePtr (TableExprNode* nodePtr)
00207         { theirNodePtr = nodePtr; }
00208 
00209     // Define the global TaQLStyle to use.
00210     // By default it is glish style.
00211     static TaQLStyle theirTaQLStyle;
00212 
00213 private:
00214     // Do the conversion of an expression string to an expression tree.
00215     static TableExprNode doParse (const String& expression);
00216 
00217     static const RecordInterface* theirRecPtr;
00218     static const Table*           theirTabPtr;
00219     static TableExprNode*         theirNodePtr;
00220     static Mutex                  theirMutex;
00221 };
00222 
00223 
00224 
00225 } //# NAMESPACE CASA - END
00226 
00227 #endif