Files
openfoam/src/OpenFOAM/include/m4/lemon/parser-methods.m4
Mark Olesen e6697edb52 ENH: robuster lemon parsing
- previously simply reused the scan token, which works fine for
  non-nested tokenizations but becomes too fragile with nesting.

  Now changed to use tagged unions that can be copied about
  and still retain some rudimentary knowledge of their types,
  which can be manually triggered with a destroy() call.

- provide an 'identifier' non-terminal as an additional catch
  to avoid potential leakage on parsing failure.

- adjust lemon rules and infrastructure:

  - use %token to predefine standard tokens.
    Will reduce some noise on the generated headers by retaining the
    order on the initial token names.

  - Define BIT_NOT, internal token rename NOT -> LNOT

- handle non-terminal vector values.
  Support vector::x, vector::y and vector::z constants

- permit fieldExpr access to time().
  Probably not usable or useful for an '#eval' expression,
  but useful for a Function1.

- provisioning for hooks into function calls. Establishes token
  names for next commit(s).
2021-11-26 12:24:35 +01:00

83 lines
2.3 KiB
Plaintext

divert(-1)dnl
#-----------------------------------*- m4 -*-----------------------------------
# ========= |
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
# \\ / O peration |
# \\ / A nd | www.openfoam.com
# \\/ M anipulation |
#------------------------------------------------------------------------------
# Copyright (C) 2019 OpenCFD Ltd.
#------------------------------------------------------------------------------
# License
# This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
#
# Description
# Various "boilerplate" parser methods (C++)
#
# Requires
# IOmanip.H
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# parser_code_static_methods(clsName)
#
# Description
# Emit common parser static methods
# `tokenName`
# `printTokenNames`
# `printRules`
#
# Note
# Array access uses `*(array + i)` to avoid [] square brackets,
# which may be used for m4 quoting, unless we switched back to `' !!
#
# Example
# parser_code_static_methods(Foam::expressions::fieldExpr::parser)
#
#------------------------------------------------------------------------------
define([parser_code_static_methods],
[dnl
Foam::word $1::tokenName(int i)
{
#ifndef NDEBUG
if (i > 0 && unsigned(i) < (sizeof(yyTokenName) / sizeof(char*)))
{
return *(yyTokenName + i);
}
return "<invalid>";
#else
return "";
#endif
}
void $1::printTokenNames(Ostream& os)
{
#ifndef NDEBUG
const unsigned nElem(sizeof(yyTokenName) / sizeof(char*));
for (unsigned i = 1; i < nElem; ++i) // start = 1 (skip termination token)
{
os << *(yyTokenName + i) << nl;
}
#endif
}
void $1::printRules(Ostream& os)
{
#ifndef NDEBUG
const unsigned nElem(sizeof(yyRuleName) / sizeof(char*));
// Easy way to count number of digits
const unsigned width(std::to_string(nElem).length());
for (unsigned i = 0; i < nElem; ++i)
{
os << setw(width) << i << ": " << *(yyRuleName + i) << nl;
}
#endif
}]
)
#------------------------------------------------------------------------------
divert(0)dnl