Commit 177f4b79 authored by Mark OLESEN's avatar Mark OLESEN Committed by Andrew Heather
Browse files

ENH: use direct scanning mode when lexing #eval expressions

parent 42308ea1
......@@ -2,7 +2,7 @@
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: Any |
| \\ / A nd | Web: www.OpenFOAM.com |
| \\ / A nd | Website: www.openfoam.com |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
FoamFile
......@@ -43,7 +43,9 @@ cosh(0.1) #eval{cosh(0.1)};
sqrt100 #eval{((sqrt(100)))};
random #eval{ floor(1000*rand()) };
float 12.345;
ceil #eval{ ceil($float) };
floor #eval{ floor($float) };
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
/*--------------------------------*- C++ -*----------------------------------*\
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......
......@@ -2,8 +2,10 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......
......@@ -2,8 +2,10 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -54,6 +56,7 @@ namespace stringOps
// - misc: floor, ceil, round, rand, rand(seed)
//
// \note The rand() function returns a uniform scalar on [0-1] interval
// and uses a constant seed
scalar toScalar
(
const std::string& s,
......
/*--------------------------------*- C++ -*----------------------------------*\
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......
......@@ -2,8 +2,10 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -56,23 +58,11 @@ class parseDriver
:
public genericRagelLemonDriver
{
protected:
// Protected Data
// Private Data
//- The result
scalar value_;
// Protected Member Functions
// No copy copy construct
parseDriver(const parseDriver&) = delete;
// No copy assignment
void operator=(const parseDriver&) = delete;
public:
ClassName("evalStringToScalar::driver");
......
......@@ -3,8 +3,10 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -51,72 +53,43 @@ Description
%token_prefix TOK_
// Terminals
%token_type {Foam::scalar}
// Non-terminals
%type exp {Foam::scalar}
%left PLUS MINUS.
%left TIMES DIVIDE.
%left NEGATE.
%start_symbol evaluate
evaluate ::= exp(a).
{
driver->setValue(a);
}
exp(lhs) ::= NUMBER(a).
{
lhs = a;
}
exp(lhs) ::= MINUS exp(a). // Could add [NEGATE] precedence
{
lhs = -a;
}
exp(lhs) ::= NUMBER(a). { lhs = a; }
exp(lhs) ::= MINUS exp(a). [NEGATE] { lhs = -a; }
exp(lhs) ::= exp(a) PLUS exp(b).
{
lhs = a + b;
}
// Operations
exp(lhs) ::= exp(a) MINUS exp(b).
{
lhs = a - b;
}
exp(lhs) ::= exp(a) PLUS exp(b). { lhs = a + b; }
exp(lhs) ::= exp(a) MINUS exp(b). { lhs = a - b; }
exp(lhs) ::= exp(a) TIMES exp(b). { lhs = a * b; }
exp(lhs) ::= exp(a) DIVIDE exp(b). { lhs = a / b; }
exp(lhs) ::= LPAREN exp(a) RPAREN. { lhs = a; }
exp(lhs) ::= exp(a) TIMES exp(b).
{
lhs = a * b;
}
exp(lhs) ::= exp(a) DIVIDE exp(b).
{
lhs = a / b;
}
// Constants
exp(lhs) ::= LPAREN exp(a) RPAREN.
{
lhs = a;
}
exp(lhs) ::= PI LPAREN RPAREN. { lhs = Foam::constant::mathematical::pi; }
exp(lhs) ::= DEG_TO_RAD LPAREN RPAREN. { lhs = Foam::degToRad(); }
exp(lhs) ::= RAD_TO_DEG LPAREN RPAREN. { lhs = Foam::radToDeg(); }
// Functions
exp(lhs) ::= PI LPAREN RPAREN.
{
lhs = Foam::constant::mathematical::pi;
}
exp(lhs) ::= DEG_TO_RAD LPAREN RPAREN.
{
lhs = Foam::degToRad();
}
exp(lhs) ::= RAD_TO_DEG LPAREN RPAREN.
{
lhs = Foam::radToDeg();
}
exp(lhs) ::= DEG_TO_RAD LPAREN exp(a) RPAREN.
{
lhs = Foam::degToRad(a);
......@@ -297,6 +270,27 @@ void Foam::parsing::evalStringToScalar::parser::parse
}
Foam::word Foam::parsing::evalStringToScalar::parser::nameOfToken
(
int tokenId
) const
{
#ifndef NDEBUG
if
(
tokenId > 0
&& unsigned(tokenId) < (sizeof(yyTokenName) / sizeof(char*))
)
{
return yyTokenName[tokenId];
}
return "<invalid>";
#else
return word();
#endif
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End of %code
......
/*--------------------------------*- C++ -*----------------------------------*\
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -80,6 +82,9 @@ public:
//- Push token/value to parser
void parse(int tokenId, Foam::scalar val);
//- The text name corresponding to the tokenId
word nameOfToken(int tokenId) const;
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
/*--------------------------------*- C++ -*----------------------------------*\
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......
......@@ -2,8 +2,10 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -32,13 +34,11 @@ Description
#include "evalStringToScalarLemonParser.h"
#include "evalStringToScalarParser.H"
#include "error.H"
#include "macros.H"
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
#pragma GCC diagnostic ignored "-Wold-style-cast"
#ifndef FULLDEBUG
#define NDEBUG
#endif
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
......@@ -62,7 +62,7 @@ int Foam::parsing::evalStringToScalar::scanner::debug = 0;
#define TOKEN_OF(T) TOK_##T
#define EMIT_TOKEN(T) \
driver.parsePosition() = (ts-buf); \
DebugInfo<< "TOKEN_" #T << " at " << driver.parsePosition() << nl; \
DebugInfo<< STRINGIFY(T) << ": " << driver.parsePosition() << nl; \
parser_->parse(TOKEN_OF(T), 0); \
driver.parsePosition() = (p-buf);
......@@ -86,64 +86,75 @@ int Foam::parsing::evalStringToScalar::scanner::debug = 0;
}
else
{
// Catch range errors
// Range error
driver.reportFatal("Error parsing number");
}
driver.parsePosition() = (p-buf);
}
action emit_ident {
driver.parsePosition() = (ts-buf);
const word ident = word::validate(ts, te);
driver.reportFatal("Unknown function/type: " + ident);
driver.parsePosition() = (p-buf);
}
decimal = ((digit* '.' digit+) | (digit+ '.'?)) ;
number = (digit+ | decimal) ([Ee][\-+]? digit+)? ;
lfunc = space* '('; # Require functions to have '('
operators = (
'(' @{ EMIT_TOKEN(LPAREN); }
| ')' @{ EMIT_TOKEN(RPAREN); }
| '+' @{ EMIT_TOKEN(PLUS); }
| '-' @{ EMIT_TOKEN(MINUS); }
| '*' @{ EMIT_TOKEN(TIMES); }
| '/' @{ EMIT_TOKEN(DIVIDE); }
| ',' @{ EMIT_TOKEN(COMMA); }
);
functions = (
'pi' lfunc @{ fhold; EMIT_TOKEN(PI); }
| 'degToRad' lfunc @{ fhold; EMIT_TOKEN(DEG_TO_RAD); }
| 'radToDeg' lfunc @{ fhold; EMIT_TOKEN(RAD_TO_DEG); }
| 'exp' lfunc @{ fhold; EMIT_TOKEN(EXP); }
| 'log' lfunc @{ fhold; EMIT_TOKEN(LOG); }
| 'log10' lfunc @{ fhold; EMIT_TOKEN(LOG10); }
| 'pow' lfunc @{ fhold; EMIT_TOKEN(POW); }
| 'sqr' lfunc @{ fhold; EMIT_TOKEN(SQR); }
| 'sqrt' lfunc @{ fhold; EMIT_TOKEN(SQRT); }
| 'cbrt' lfunc @{ fhold; EMIT_TOKEN(CBRT); }
| 'sin' lfunc @{ fhold; EMIT_TOKEN(SIN); }
| 'cos' lfunc @{ fhold; EMIT_TOKEN(COS); }
| 'tan' lfunc @{ fhold; EMIT_TOKEN(TAN); }
| 'asin' lfunc @{ fhold; EMIT_TOKEN(ASIN); }
| 'acos' lfunc @{ fhold; EMIT_TOKEN(ACOS); }
| 'atan' lfunc @{ fhold; EMIT_TOKEN(ATAN); }
| 'atan2' lfunc @{ fhold; EMIT_TOKEN(ATAN2); }
| 'hypot' lfunc @{ fhold; EMIT_TOKEN(HYPOT); }
| 'sinh' lfunc @{ fhold; EMIT_TOKEN(SINH); }
| 'cosh' lfunc @{ fhold; EMIT_TOKEN(COSH); }
| 'tanh' lfunc @{ fhold; EMIT_TOKEN(TANH); }
| 'min' lfunc @{ fhold; EMIT_TOKEN(MIN); }
| 'max' lfunc @{ fhold; EMIT_TOKEN(MAX); }
| 'mag' lfunc @{ fhold; EMIT_TOKEN(MAG); }
| 'magSqr' lfunc @{ fhold; EMIT_TOKEN(MAGSQR); }
| 'floor' lfunc @{ fhold; EMIT_TOKEN(FLOOR); }
| 'ceil' lfunc @{ fhold; EMIT_TOKEN(CEIL); }
| 'round' lfunc @{ fhold; EMIT_TOKEN(ROUND); }
| 'rand' lfunc @{ fhold; EMIT_TOKEN(RAND); }
);
ident = ((alpha|'_') . ((alnum|'_')**)) ;
## The scanner
main := |*
space*;
number => emit_number;
operators;
functions;
## operators
'(' => { EMIT_TOKEN(LPAREN); };
')' => { EMIT_TOKEN(RPAREN); };
'+' => { EMIT_TOKEN(PLUS); };
'-' => { EMIT_TOKEN(MINUS); };
'*' => { EMIT_TOKEN(TIMES); };
'/' => { EMIT_TOKEN(DIVIDE); };
',' => { EMIT_TOKEN(COMMA); };
## Regular functions
'pi' => { EMIT_TOKEN(PI); };
'degToRad' => { EMIT_TOKEN(DEG_TO_RAD); };
'radToDeg' => { EMIT_TOKEN(RAD_TO_DEG); };
'exp' => { EMIT_TOKEN(EXP); };
'log' => { EMIT_TOKEN(LOG); };
'log10' => { EMIT_TOKEN(LOG10); };
'pow' => { EMIT_TOKEN(POW); };
'sqr' => { EMIT_TOKEN(SQR); };
'sqrt' => { EMIT_TOKEN(SQRT); };
'cbrt' => { EMIT_TOKEN(CBRT); };
'sin' => { EMIT_TOKEN(SIN); };
'cos' => { EMIT_TOKEN(COS); };
'tan' => { EMIT_TOKEN(TAN); };
'asin' => { EMIT_TOKEN(ASIN); };
'acos' => { EMIT_TOKEN(ACOS); };
'atan' => { EMIT_TOKEN(ATAN); };
'atan2' => { EMIT_TOKEN(ATAN2); };
'hypot' => { EMIT_TOKEN(HYPOT); };
'sinh' => { EMIT_TOKEN(SINH); };
'cosh' => { EMIT_TOKEN(COSH); };
'tanh' => { EMIT_TOKEN(TANH); };
'min' => { EMIT_TOKEN(MIN); };
'max' => { EMIT_TOKEN(MAX); };
'mag' => { EMIT_TOKEN(MAG); };
'magSqr' => { EMIT_TOKEN(MAGSQR); };
'floor' => { EMIT_TOKEN(FLOOR); };
'ceil' => { EMIT_TOKEN(CEIL); };
'round' => { EMIT_TOKEN(ROUND); };
'rand' => { fhold; EMIT_TOKEN(RAND); };
## Catch-all for identifiers/errors
ident => emit_ident;
space*;
*|;
}%%
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment