/* $Id$ */ /* * Copyright (C) 2006 Richard Braun * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _MTXLANGUAGE_H #define _MTXLANGUAGE_H #include #include #include "mtx.h" #include "mtxsymbol.h" /** * \def MTXLANGUAGE_VALUES_INDEX * \brief Index of the lexical group of values in lexical groups array. */ #define MTXLANGUAGE_VALUES_INDEX 0 /** * \def MTXLANGUAGE_OPERATORS_INDEX * \brief Index of the lexical group of operators in lexical groups array. */ #define MTXLANGUAGE_OPERATORS_INDEX 1 /** * \def MTXLANGUAGE_OPERATORS_MAX_LENGTH * \brief Maximum length of the string representation of an operator. */ #define MTXLANGUAGE_OPERATORS_MAX_LENGTH 2 /** * \def MTXLANGUAGE_ANS_VARIABLE * \brief name of the automatic variable \a ans */ #define MTXLANGUAGE_ANS_VARIABLE "ans" /** * Main language object used as interface between user and engine. */ class MtxLanguage { private: /** * Vector of known variables. * * @see getSymbols() */ std::vector symbols; /** * Return the lexical group of the given character. * * Throw an exception if the character isn't registered. */ size_t findGroup(char c) const; /** * Check a lexical element. * * Throw an exception if the lexical element is invalid. */ void checkLexicalElement(const std::string &lexical_element) const; /** * Lexical analysis. * * Return a vector of the lexical elements of the given expression. An * exception is thrown if an invalid character is present. */ mtx_expression_t parseLexicalElements(const std::string &expression) const; /** * Compile a set of lexical elements into expressions. * * Parse the given lexical elements and compile them in a vector of * expressions. An MtxException exception is thrown if an error occurs. */ std::vector parseExpressions(const mtx_expression_t &lexical_elements); /** * Release the memory referenced by the pointers in the given vector. */ void freeExpressions(std::vector &expressions); /** * Array of groups of lexical elements. E.g. + and = are in the same * group. */ static const char *lexical_groups[]; public: /** * Default constructor. */ MtxLanguage(); /** * Parse the given expression into a set of results. * * This is the main interface between the user and the engine. * The caller should catch MtxException exceptions. */ std::vector parse(const std::string &expression); /** * Set a named variable. */ void setSymbol(const MtxSymbol &symbol); /** * Get a variable from its name. */ MtxSymbol getSymbol(const std::string &name) const; /** * Return a copy of the vector of symbols. * * Mainly used to list all registered variables. * * @see symbols. */ std::vector getSymbols() const; }; #endif /* _MTXLANGUAGE_H */