# File expressionparser.hxx¶

Parses strings containing expressions, returning a tree of generators

Copyright 2010 B.D.Dudson, S.Farley, M.V.Umansky, X.Q.Xu

Contact: Ben Dudson, bd512@york.ac.uk

This file is part of BOUT++.

BOUT++ is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

BOUT++ 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with BOUT++. If not, see http://www.gnu.org/licenses/.

Typedefs

using FieldGeneratorPtr = std::shared_ptr<FieldGenerator>
class FieldGenerator
#include <expressionparser.hxx>

Represents an operation which generates a value at a given (x,y,z) location, perhaps using other generators passed to clone()

Public Functions

virtual ~FieldGenerator()
virtual FieldGeneratorPtr clone(const std::list<FieldGeneratorPtr> args)

Virtual constructor. Makes a copy of this FieldGenerator, initialised with the given list of arguments. It is up to the implementations to test whether the correct number of arguments is passed.

Parameters
• args: A (possibly empty) list of arguments to the generator function

virtual double generate(double x, double y, double z, double t) = 0

Generate a value at the given coordinates (x,y,z,t) This should be deterministic, always returning the same value given the same inputs

virtual std::string str() const

Create a string representation of the generator, for debugging output.

class ExpressionParser
#include <expressionparser.hxx>

Parses expressions, turning strings into FieldGenerator objects which can be used to perform calculations.

This class is not intended to be used directly, but should be inherited to add additional functionality

Subclassed by FieldFactory

Public Functions

ExpressionParser()
virtual ~ExpressionParser()
void addGenerator(const std::string &name, FieldGeneratorPtr g)

Add a generator to the parser, which can then be recognised and used in expressions.

Parameters
• name: The name to be recognised in expressions. This should start with a letter and contain no whitespace, only alphanumeric letters and underscores.
• g: The class inheriting from FieldGenerator. When recognised in an expression, the clone() function will be called to build a tree of generators

void addBinaryOp(char sym, FieldGeneratorPtr b, int precedence)

Add a binary operator such as +,-,*,/,^

Parameters
• sym: The operator symbol. This must be a single character
• b: The FieldGenerator to use. When the symbol is recognised, b->clone() will be called with two input arguments
• precedence: The precedence of the operator, which decides which order operators are performed in. Higher precedence operations are done before low precedence operations. Binary operators already defined are +, - precedence = 10 *, / precedence = 20 ^ precedence = 30

Protected Functions

virtual FieldGeneratorPtr resolve(std::string &name) const

This will be called to resolve any unknown symbols.

FieldGeneratorPtr parseString(const std::string &input) const

Parses a given string into a tree of FieldGenerator objects.

Protected Attributes

std::string reserved_chars = "+-*/^[](){},"

Characters which cannot be used in symbols; all other allowed In addition, whitespace cannot be used Adding a binary operator adds its symbol to this string

Private Functions

FieldGeneratorPtr parseIdentifierExpr(LexInfo &lex) const
FieldGeneratorPtr parseParenExpr(LexInfo &lex) const
FieldGeneratorPtr parsePrimary(LexInfo &lex) const

Parse a primary expression, one of:

• number
• identifier
• ( … )
• [ … ]
• a unary ‘-‘, which is converted to ‘0 -‘ A ParseException is thrown if none of these is found

FieldGeneratorPtr parseBinOpRHS(LexInfo &lex, int prec, FieldGeneratorPtr lhs) const
FieldGeneratorPtr parseExpression(LexInfo &lex) const

Private Members

std::map<std::string, FieldGeneratorPtr> gen

std::map<char, std::pair<FieldGeneratorPtr, int>> bin_op

Binary operations.

struct LexInfo

Lexing info, used when splitting input into tokens.

Public Functions

LexInfo(const std::string &input, std::string reserved_chars = "")
char nextToken()

Get the next token in the string.

Public Members

signed char curtok = 0

Current token. -1 for number, -2 for string, 0 for “end of input”.

double curval

Value if a number.

std::string curident

Identifier, variable or function name.

signed char LastChar

The last character read from the string.

std::stringstream ss

Used to read values from the input string.

std::string reserved_chars

Reserved characters, not in symbols.

class FieldBinary : public FieldGenerator
#include <expressionparser.hxx>

Binary operators.

Public Functions

FieldBinary(FieldGeneratorPtr l, FieldGeneratorPtr r, char o)
FieldGeneratorPtr clone(const std::list<FieldGeneratorPtr> args)

Virtual constructor. Makes a copy of this FieldGenerator, initialised with the given list of arguments. It is up to the implementations to test whether the correct number of arguments is passed.

Parameters
• args: A (possibly empty) list of arguments to the generator function

BoutReal generate(double x, double y, double z, double t)

Generate a value at the given coordinates (x,y,z,t) This should be deterministic, always returning the same value given the same inputs

std::string str() const

Create a string representation of the generator, for debugging output.

Private Members

FieldGeneratorPtr lhs
FieldGeneratorPtr rhs
char op
class FieldValue : public FieldGenerator
#include <expressionparser.hxx>

Represent fixed values.

Public Functions

FieldValue(double val)
FieldGeneratorPtr clone(const std::list<FieldGeneratorPtr> args)

Virtual constructor. Makes a copy of this FieldGenerator, initialised with the given list of arguments. It is up to the implementations to test whether the correct number of arguments is passed.

Parameters
• args: A (possibly empty) list of arguments to the generator function

double generate(double x, double y, double z, double t)

Generate a value at the given coordinates (x,y,z,t) This should be deterministic, always returning the same value given the same inputs

std::string str() const

Create a string representation of the generator, for debugging output.

Private Members

double value
class ParseException : public std::exception

Public Functions

ParseException(const char *s, ...)
~ParseException()
const char *what() const

Protected Attributes

std::string message