1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
//# TableGram.cc: Grammar for table command lines
//# Copyright (C) 1993,1994,1995,1997,1999,2001,2003
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This library is free software; you can redistribute it and/or modify it
//# under the terms of the GNU Library General Public License as published by
//# the Free Software Foundation; either version 2 of the License, or (at your
//# option) any later version.
//#
//# This library is distributed in the hope that it will be useful, but WITHOUT
//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
//# License for more details.
//#
//# You should have received a copy of the GNU Library General Public License
//# along with this library; if not, write to the Free Software Foundation,
//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
//#
//# Correspondence concerning AIPS++ should be addressed as follows:
//# Internet email: aips2-request@nrao.edu.
//# Postal address: AIPS++ Project Office
//# National Radio Astronomy Observatory
//# 520 Edgemont Road
//# Charlottesville, VA 22903-2475 USA
//#
//# $Id$
// TableGram; grammar for table command lines
// This file includes the output files of bison and flex for
// parsing command lines operating on tables.
#include <casacore/tables/TaQL/TableGram.h>
#include <casacore/tables/TaQL/TaQLNode.h>
#include <casacore/tables/TaQL/TaQLNodeDer.h>
#include <casacore/tables/Tables/TableError.h>
#include <casacore/casa/Utilities/MUString.h>
#include <casacore/casa/Quanta/MVAngle.h>
#include <stack>
//# stdlib.h is needed for bison 1.28 and needs to be included here
//# (before the flex/bison files).
//# Bison defines WHERE, which is also defined in LogOrigin.h (which
//# is included in auto-template mode).
//# So undefine WHERE first.
#undef WHERE
#include <casacore/casa/stdlib.h>
//# Let clang and gcc ignore some warnings in bison/flex code.
//# Undef YY_NULL because flex can redefine it slightly differently (giving a warning).
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic ignored "-Wdeprecated-register"
#pragma GCC diagnostic ignored "-Wsign-compare"
#include "TableGram.ycc" // bison output
#ifdef YY_NULL
# undef YY_NULL
#endif
#include "TableGram.lcc" // flex output
#pragma GCC diagnostic pop
// Define the yywrap function for flex.
int TableGramwrap()
{
return 1;
}
namespace casacore { //# NAMESPACE CASACORE - BEGIN
//# Declare a file global pointer to a char* for the input string.
static const char* strpTableGram = 0;
static Int posTableGram = 0;
// Define a class to delete the yy_buffer in case of an exception.
class TableGramState {
public:
TableGramState (YY_BUFFER_STATE state)
: itsState (state)
{}
~TableGramState()
{ clear(); }
void clear()
{ if (itsState) {
TableGram_delete_buffer(itsState);
itsState=0;
}
}
YY_BUFFER_STATE state() const
{ return itsState; }
private:
TableGramState (const TableGramState&);
TableGramState& operator= (const TableGramState&);
YY_BUFFER_STATE itsState; //# this is a pointer to yy_buffer_state
};
//# Parse the command.
int tableGramParseCommand (const String& command)
{
// Save current state for re-entrancy.
int sav_yy_start = yy_start;
const char* savStrpTableGram = strpTableGram;
Int savPosTableGram= posTableGram;
YY_BUFFER_STATE sav_state = YY_CURRENT_BUFFER;
// Create a new state buffer for new expression.
TableGramState next (TableGram_create_buffer (TableGramin, YY_BUF_SIZE));
TableGram_switch_to_buffer (next.state());
yy_start = 1;
strpTableGram = command.chars(); // get pointer to command string
posTableGram = 0; // initialize string position
int sts = TableGramparse(); // parse command string
// The current state has to be deleted before switching back to previous.
next.clear();
// Restore previous state.
yy_start = sav_yy_start;
strpTableGram = savStrpTableGram;
posTableGram= savPosTableGram;
TableGram_switch_to_buffer (sav_state);
return sts;
}
//# Give the string position.
Int& tableGramPosition()
{
return posTableGram;
}
//# Get the next input characters for flex.
int tableGramInput (char* buf, int max_size)
{
int nr=0;
while (*strpTableGram != 0) {
if (nr >= max_size) {
break; // get max. max_size char.
}
buf[nr++] = *strpTableGram++;
}
return nr;
}
void TableGramerror (const char*)
{
throw TableGramError (tableGramPosition(), String(TableGramtext));
}
String tableGramRemoveEscapes (const String& in)
{
String out;
int leng = in.length();
for (int i=0; i<leng; i++) {
if (in[i] == '\\') {
i++;
}
out += in[i];
}
return out;
}
String tableGramRemoveEscapesQuotes (const String& in)
{
String out;
char quote = 0;
int leng = in.length();
for (int i=0; i<leng; i++) {
if (quote) {
if (in[i] == quote) {
quote = 0;
} else {
out += in[i];
}
} else if (in[i] == '"' || in[i] == '\'') {
quote = in[i];
} else {
if (in[i] == '\\') {
i++;
}
out += in[i];
}
}
return out;
}
String tableGramRemoveQuotes (const String& in)
{
//# A string is formed as "..."'...''...' etc.
//# All ... parts will be extracted and concatenated into an output string.
String out;
String str = in;
int leng = str.length();
int pos = 0;
while (pos < leng) {
//# Find next occurrence of leading ' or ""
int inx = str.index (str[pos], pos+1);
if (inx < 0) {
throw (TableError ("ill-formed quoted string: " + str));
}
out += str.at (pos+1, inx-pos-1); // add substring
pos = inx+1;
}
return out;
}
Double tableGramParseTime (const String& in)
{
Quantity res;
//# Skip a possible leading / which acts as an escape character.
String val(in);
if (val.length() > 0 && val[0] == '/') {
val = val.after(0);
}
if (! MVAngle::read (res, val)) {
throw (TableError ("invalid time/pos string " + val));
}
return MVAngle(res).radian();
}
MVTime tableGramParseDateTime (const String& in)
{
MUString str (in);
Quantity res;
if (! MVTime::read (res, str)) {
throw (TableError ("invalid date string " + in));
}
return MVTime(res);
}
} //# NAMESPACE CASACORE - END
|