ENH: use direct scanning mode when lexing #eval expressions

This commit is contained in:
Mark Olesen
2019-11-01 19:13:50 +01:00
committed by Andrew Heather
parent 42308ea1f3
commit 177f4b79fe
11 changed files with 1363 additions and 937 deletions

View File

@ -2,7 +2,7 @@
| ========= | | | ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: Any | | \\ / O peration | Version: Any |
| \\ / A nd | Web: www.OpenFOAM.com | | \\ / A nd | Website: www.openfoam.com |
| \\/ M anipulation | | | \\/ M anipulation | |
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
FoamFile FoamFile
@ -43,7 +43,9 @@ cosh(0.1) #eval{cosh(0.1)};
sqrt100 #eval{((sqrt(100)))}; sqrt100 #eval{((sqrt(100)))};
random #eval{ floor(1000*rand()) }; float 12.345;
ceil #eval{ ceil($float) };
floor #eval{ floor($float) };
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -1,9 +1,11 @@
/*--------------------------------*- C++ -*----------------------------------*\ /*---------------------------------------------------------------------------*\
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.

View File

@ -2,8 +2,10 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.

View File

@ -2,8 +2,10 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -54,6 +56,7 @@ namespace stringOps
// - misc: floor, ceil, round, rand, rand(seed) // - misc: floor, ceil, round, rand, rand(seed)
// //
// \note The rand() function returns a uniform scalar on [0-1] interval // \note The rand() function returns a uniform scalar on [0-1] interval
// and uses a constant seed
scalar toScalar scalar toScalar
( (
const std::string& s, const std::string& s,

View File

@ -1,9 +1,11 @@
/*--------------------------------*- C++ -*----------------------------------*\ /*---------------------------------------------------------------------------*\
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.

View File

@ -2,8 +2,10 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -56,23 +58,11 @@ class parseDriver
: :
public genericRagelLemonDriver public genericRagelLemonDriver
{ {
protected: // Private Data
// Protected Data
//- The result //- The result
scalar value_; scalar value_;
// Protected Member Functions
// No copy copy construct
parseDriver(const parseDriver&) = delete;
// No copy assignment
void operator=(const parseDriver&) = delete;
public: public:
ClassName("evalStringToScalar::driver"); ClassName("evalStringToScalar::driver");

View File

@ -3,8 +3,10 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -51,72 +53,43 @@ Description
%token_prefix TOK_ %token_prefix TOK_
// Terminals
%token_type {Foam::scalar} %token_type {Foam::scalar}
// Non-terminals
%type exp {Foam::scalar} %type exp {Foam::scalar}
%left PLUS MINUS. %left PLUS MINUS.
%left TIMES DIVIDE. %left TIMES DIVIDE.
%left NEGATE. %left NEGATE.
%start_symbol evaluate
evaluate ::= exp(a). evaluate ::= exp(a).
{ {
driver->setValue(a); driver->setValue(a);
} }
exp(lhs) ::= NUMBER(a). exp(lhs) ::= NUMBER(a). { lhs = a; }
{ exp(lhs) ::= MINUS exp(a). [NEGATE] { lhs = -a; }
lhs = a;
}
exp(lhs) ::= MINUS exp(a). // Could add [NEGATE] precedence // Operations
{
lhs = -a;
}
exp(lhs) ::= exp(a) PLUS exp(b). exp(lhs) ::= exp(a) PLUS exp(b). { lhs = a + b; }
{ exp(lhs) ::= exp(a) MINUS exp(b). { lhs = a - 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) MINUS exp(b).
{
lhs = a - b;
}
exp(lhs) ::= exp(a) TIMES exp(b). // Constants
{
lhs = a * b;
}
exp(lhs) ::= exp(a) DIVIDE exp(b). exp(lhs) ::= PI LPAREN RPAREN. { lhs = Foam::constant::mathematical::pi; }
{ exp(lhs) ::= DEG_TO_RAD LPAREN RPAREN. { lhs = Foam::degToRad(); }
lhs = a / b; exp(lhs) ::= RAD_TO_DEG LPAREN RPAREN. { lhs = Foam::radToDeg(); }
}
exp(lhs) ::= LPAREN exp(a) RPAREN.
{
lhs = a;
}
// Functions // 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. exp(lhs) ::= DEG_TO_RAD LPAREN exp(a) RPAREN.
{ {
lhs = Foam::degToRad(a); 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 } // End of %code

View File

@ -1,9 +1,11 @@
/*--------------------------------*- C++ -*----------------------------------*\ /*---------------------------------------------------------------------------*\
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -80,6 +82,9 @@ public:
//- Push token/value to parser //- Push token/value to parser
void parse(int tokenId, Foam::scalar val); void parse(int tokenId, Foam::scalar val);
//- The text name corresponding to the tokenId
word nameOfToken(int tokenId) const;
}; };
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -1,9 +1,11 @@
/*--------------------------------*- C++ -*----------------------------------*\ /*---------------------------------------------------------------------------*\
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.

View File

@ -2,8 +2,10 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -32,13 +34,11 @@ Description
#include "evalStringToScalarLemonParser.h" #include "evalStringToScalarLemonParser.h"
#include "evalStringToScalarParser.H" #include "evalStringToScalarParser.H"
#include "error.H" #include "error.H"
#include "macros.H"
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough" #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
#pragma GCC diagnostic ignored "-Wold-style-cast" #pragma GCC diagnostic ignored "-Wold-style-cast"
#ifndef FULLDEBUG
#define NDEBUG
#endif
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -62,7 +62,7 @@ int Foam::parsing::evalStringToScalar::scanner::debug = 0;
#define TOKEN_OF(T) TOK_##T #define TOKEN_OF(T) TOK_##T
#define EMIT_TOKEN(T) \ #define EMIT_TOKEN(T) \
driver.parsePosition() = (ts-buf); \ driver.parsePosition() = (ts-buf); \
DebugInfo<< "TOKEN_" #T << " at " << driver.parsePosition() << nl; \ DebugInfo<< STRINGIFY(T) << ": " << driver.parsePosition() << nl; \
parser_->parse(TOKEN_OF(T), 0); \ parser_->parse(TOKEN_OF(T), 0); \
driver.parsePosition() = (p-buf); driver.parsePosition() = (p-buf);
@ -86,64 +86,75 @@ int Foam::parsing::evalStringToScalar::scanner::debug = 0;
} }
else else
{ {
// Catch range errors // Range error
driver.reportFatal("Error parsing number"); driver.reportFatal("Error parsing number");
} }
driver.parsePosition() = (p-buf); 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+ '.'?)) ; decimal = ((digit* '.' digit+) | (digit+ '.'?)) ;
number = (digit+ | decimal) ([Ee][\-+]? digit+)? ; number = (digit+ | decimal) ([Ee][\-+]? digit+)? ;
lfunc = space* '('; # Require functions to have '(' ident = ((alpha|'_') . ((alnum|'_')**)) ;
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); }
);
## The scanner
main := |* main := |*
space*; space*;
number => emit_number; 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*; space*;
*|; *|;
}%% }%%