1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2024-11-14 09:45:36 +00:00
LatAnalyze/lib/AsciiParser.ypp

184 lines
4.7 KiB
Plaintext
Raw Normal View History

2014-02-20 23:52:45 +00:00
/*
2014-03-12 19:54:27 +00:00
* AsciiParser.ypp, part of LatAnalyze 3
2014-02-20 23:52:45 +00:00
*
2016-04-06 20:11:23 +01:00
* Copyright (C) 2013 - 2016 Antonin Portelli
2014-02-20 23:52:45 +00:00
*
* LatAnalyze 3 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LatAnalyze 3 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LatAnalyze 3. If not, see <http://www.gnu.org/licenses/>.
*/
%{
2014-03-13 18:51:01 +00:00
#include <LatAnalyze/Global.hpp>
#include <LatAnalyze/AsciiFile.hpp>
#include <LatAnalyze/Mat.hpp>
#include <LatAnalyze/MatSample.hpp>
2014-02-20 23:52:45 +00:00
using namespace std;
using namespace Latan;
#define TEST_FIRST(name) \
if (state->isFirst)\
{\
state->first = (name);\
state->isFirst = false;\
}
%}
%pure-parser
%name-prefix "_Ascii_"
2014-02-20 23:52:45 +00:00
%locations
%defines
%error-verbose
%parse-param { Latan::AsciiFile::AsciiParserState* state }
%initial-action {yylloc.last_column = 0;}
%lex-param { void* scanner }
%union
{
long int val_int;
double val_double;
char val_char;
char val_str[256];
2014-02-20 23:52:45 +00:00
}
%token <val_char> ERR
%token <val_double> FLOAT
%token <val_int> INT
%token <val_str> ID
%token OPEN CLOSE MAT SAMPLE RG_STATE
%type <val_str> mat matsample dsample
2014-02-20 23:52:45 +00:00
%{
int _Ascii_lex(YYSTYPE* lvalp, YYLTYPE* llocp, void* scanner);
void _Ascii_error(YYLTYPE* locp, AsciiFile::AsciiParserState* state,
const char* err)
{
stringstream buf;
buf << *state->streamName << ":" << locp->first_line << ":"\
<< locp->first_column << ": " << err;
LATAN_ERROR(Parsing, buf.str());
}
#define scanner state->scanner
%}
%%
datas:
/* empty string */
| datas data
;
data:
mat
{
TEST_FIRST($1);
(*state->data)[$1].reset(new DMat(state->dMatQueue.front()));
state->dMatQueue.pop();
}
2016-04-05 15:57:19 +01:00
| dsample
{
TEST_FIRST($1);
(*state->data)[$1].reset(new DSample(state->dSampleBuf));
}
| matsample
2014-02-20 23:52:45 +00:00
{
TEST_FIRST($1);
(*state->data)[$1].reset(new DMatSample(state->dMatSampleBuf));
}
;
mat:
OPEN MAT ID INT floats CLOSE MAT
{
2015-06-15 13:59:10 +01:00
const unsigned int nRow = state->doubleQueue.size()/$4, nCol = $4;
Index i, j, r = 0;
2014-02-20 23:52:45 +00:00
if (state->doubleQueue.size() != nRow*nCol)
{
2015-06-15 13:59:10 +01:00
LATAN_ERROR(Size, "matrix '" + *state->streamName + ":" + $3 +
2014-02-20 23:52:45 +00:00
"' has a wrong size");
}
state->dMatQueue.push(DMat(nRow, nCol));
while (!state->doubleQueue.empty())
{
j = r % nCol;
i = (r - j)/nCol;
state->dMatQueue.back()(i, j) = state->doubleQueue.front();
state->doubleQueue.pop();
r++;
}
2015-06-15 13:59:10 +01:00
strcpy($$, $3);
2014-02-20 23:52:45 +00:00
}
;
2016-04-05 15:57:19 +01:00
dsample:
OPEN SAMPLE ID INT mat CLOSE SAMPLE
{
const unsigned int nSample = $4, os = DMatSample::offset;
DMat &m = state->dMatQueue.front();
if (m.rows() != nSample + os)
{
LATAN_ERROR(Size, "double sample '" + *state->streamName + ":"
+ $3 + "' has a wrong size");
}
if (m.cols() != 1)
{
LATAN_ERROR(Size, "double sample '" + *state->streamName + ":"
+ $3 + "' is not stored as a column vector");
}
state->dSampleBuf = m.array();
state->dMatQueue.pop();
strcpy($$, $3);
}
;
matsample:
OPEN SAMPLE ID INT mat mats CLOSE SAMPLE
2014-02-20 23:52:45 +00:00
{
2015-06-15 13:59:10 +01:00
const unsigned int nSample = $4, os = DMatSample::offset;
2014-02-20 23:52:45 +00:00
2015-06-15 13:59:10 +01:00
if (state->dMatQueue.size() != nSample + os)
2014-02-20 23:52:45 +00:00
{
2016-04-05 15:57:19 +01:00
LATAN_ERROR(Size, "matrix sample '" + *state->streamName + ":"
+ $3 + "' has a wrong size");
2014-02-20 23:52:45 +00:00
}
state->dMatSampleBuf.resize(nSample);
state->dMatSampleBuf[central] = state->dMatQueue.front();
state->dMatQueue.pop();
2015-06-15 13:59:10 +01:00
for (unsigned int i = 0; i < nSample; ++i)
2014-02-20 23:52:45 +00:00
{
state->dMatSampleBuf[i] = state->dMatQueue.front();
state->dMatQueue.pop();
}
2015-06-15 13:59:10 +01:00
strcpy($$, $3);
2014-02-20 23:52:45 +00:00
}
2016-04-05 15:57:19 +01:00
;
2014-02-20 23:52:45 +00:00
mats:
mats mat
| mat
;
floats:
2015-06-15 13:59:10 +01:00
floats FLOAT {state->doubleQueue.push($2);}
| floats INT {state->doubleQueue.push(static_cast<double>($2));}
| FLOAT {state->doubleQueue.push($1);}
| INT {state->doubleQueue.push(static_cast<double>($1));}
2014-02-20 23:52:45 +00:00
;