ENH: improve stream handling of expansions (#2095)

* removed internal upper limit on word/string length for parsed input.

  - Although it has not caused many problems, no reason to retain
    these limits.
  - simplify some of the internal logic for reading string-like items.
  - localize parsers for better separation from the header

  - expose new function seekCommentEnd_Cstyle(), as useful
    handler of C-style comments

* exclude imbalanced closing ')' from word/variable

  - previously included this into the word/variable, but makes more
    sense to leave on the parser for the following token.

    Prevents content like 'vector (10 20 $zmax);' from being parsed
    as '$zmax)' instead of as '$zmax' followed by a ')'.
    No conceivable reason that the former would actually be desirable,
    but can still be obtained with brace notation: Eg, '${zmax)}'

* consistent handling of ${{ ... }} expressions

  - within a dictionary content, the following construct was
    incorrectly processed:

        value ${{2*sqrt(0.5)}};

    Complains about no dictionary/env variable "{2*sqrt(0.5)}"

    Now trap expressions directly and assign their own token type
    while reading. Later expansion can then be properly passed to
    the exprDriver (evalEntry) instead of incorrectly trying
    variable expansion.

    Does not alter the use of expressions embedded within other
    expansions. Eg, "file${{10*2}}"

* improve #eval { ... } brace slurping

  - the initial implementation of this was rudimentary and simply
    grabbed everything until the next '}'.  Now continue to grab
    content until braces are properly balanced

    Eg, the content:   value #eval{${radius}*2};

    would have previously terminated prematurely with "${radius" for
    the expression!

NOTE:
    both the ${{ expr }} parsed input and the #eval { ... } input
    discard C/C++ comments during reading to reduce intermediate
    overhead for content that will be discarded before evaluation
    anyhow.

* tighten recognition of verbatim strings and expressions.

  - parser was previously sloppy and would have accepted content such
    as "# { ..." (for example) as an verbatim string introducer.
    Now only accept parse if there are no intermediate characters
    discarded.
This commit is contained in:
Mark Olesen
2021-05-17 17:25:20 +02:00
parent efd1ac4b5f
commit 44a243a94d
8 changed files with 692 additions and 419 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2012-2016 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
Copyright (C) 2020-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -120,8 +120,8 @@ Foam::Ostream& Foam::OBJstream::writeQuoted
return *this;
}
OFstream::write(static_cast<char>(token::BEGIN_STRING));
// Output with surrounding quotes and backslash escaping
OFstream::write(static_cast<char>(token::DQUOTE));
unsigned backslash = 0;
for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
@ -138,7 +138,7 @@ Foam::Ostream& Foam::OBJstream::writeQuoted
++lineNumber_;
++backslash; // backslash escape for newline
}
else if (c == token::END_STRING)
else if (c == token::DQUOTE)
{
++backslash; // backslash escape for quote
}
@ -155,7 +155,7 @@ Foam::Ostream& Foam::OBJstream::writeQuoted
// silently drop any trailing backslashes
// they would otherwise appear like an escaped end-quote
OFstream::write(static_cast<char>(token::END_STRING));
OFstream::write(static_cast<char>(token::DQUOTE));
return *this;
}