Click here to Skip to main content
15,893,668 members
Articles / Programming Languages / C#

An extensible math expression parser with plug-ins

Rate me:
Please Sign up or sign in to vote.
4.92/5 (147 votes)
13 Mar 2008CPOL51 min read 1.4M   29K   364  
Design and code for an extensible, maintainable, robust, and easy to use math parser.
#ifndef _MTPARSEREXCEPTION_INCLUDED
#define _MTPARSEREXCEPTION_INCLUDED

#include "UnicodeANSIDefs.h"
#include "MTException.h"

// Definition exception
// This exception will be thrown if there is a problem during the definition step
struct SMTDefExcepData
{
	int id;				// exception id
	MTSTRING itemName;	// item that cannot be defined
	MTSTRING conflictItemName;	// item in conflict
	MTSTRING description;
};


// Parsing exception
// This exception will be thrown if there is a problem during the parsing step
struct SMTParsingExcepData
{	
	int id;				// exception id
	int pos;			// character position in the expression
	MTSTRING itemName;	// item name 	
	int param1, param2;	// extra exception parameters
	MTSTRING helpString;// operator or function help string
	MTSTRING description;
};

typedef MTExceptions<SMTDefExcepData> MTDefException;
typedef MTExceptions<SMTParsingExcepData> MTParsingException;


// List of all definition exception ids
enum EMTDefExceptionID
{
	
	// the argument separator character and the decimal point character are the same
	e_MTDefExcep_SyntaxArgDecConflict,

	// the argument separator character and the begin/end variable name character are the same
	e_MTDefExcep_SyntaxArgVarConflict,

	// the decimal point character and the begin/end variable name character are the same
	e_MTDefExcep_SyntaxDecVarConflict,

	// an operator with the same name and the same number of arguments is already defined
	e_MTDefExcep_OpAlreadyDefined,

	// null operator name
	e_MTDefExcep_OpNameNull,

	// There is a character in the operator name that is the same as a syntax element.	
	e_MTDefExcep_OpNameSyntaxConflict,

	// The operator precedence is incorrect; must be below function precedence.
	e_MTDefExcep_OpPrecedence,
	
	// a function with the same name and the same number of arguments is already defined
	e_MTDefExcep_FuncAlreadyDefined,

	// null function name
	e_MTDefExcep_FuncNameNull,

	// There is a character in the function name that is the same as a syntax element.	
	e_MTDefExcep_FuncNameSyntaxConflict,

	// The function name contains an operator symbol
	e_MTDefExcep_FuncNameOpConflict,

	// a variable with the same name is already defined
	e_MTDefExcep_VarAlreadyDefined,

	// null variable name
	e_MTDefExcep_VarNameNull,

	// a variable name cannot contains only numbers
	e_MTDefExcep_VarNameOnlyNum,	

	// The variable name is the same as a defined constant name
	e_MTDefExcep_VarNameConstConflict,

	// The variable name contains variable delimiters:
	// variable names can contain all syntax elements but variable delimiters
	e_MTDefExcep_VarNameDelimConflict,

	// Cannot find the specified variable.  
	e_MTDefExcep_VariableNotFound,

	// a constant with the same name is already defined
	e_MTDefExcep_ConstAlreadyDefined,

	// null constant name
	e_MTDefExcep_ConstNameNull,

	// a constant name cannot contains only numbers
	e_MTDefExcep_ConstNameOnlyNum,

	// There is a character in the constant name that is the same as a syntax element.	
	e_MTDefExcep_ConstNameSyntaxConflict,

	// The constant name contains an operator symbol
	e_MTDefExcep_ConstNameOpConflict,

	// The constant name is the same as a defined variable name
	e_MTDefExcep_ConstNameVarConflict


};

// List of all parsing exception ids
enum EMTParsingExceptionID
{
	// an operator has been detected but the syntax is incorrect 
	e_MTParsingExcep_InvalidOpSyntax,
	
	// can't use the begin variable name character when already in a variable name 
	e_MTParsingExcep_UnexpectedBeginVarName,

	// can't use the end variable name character without the begin variable name character 
	e_MTParsingExcep_UnexpectedEndVarName,

	// an operator appears at an unexpected position
	e_MTParsingExcep_UnexpectedOp,

	// missing opening bracket
	e_MTParsingExcep_MissingOpenBracket,
	
	// invalid use of an argument separator: can only be used inside a function
	e_MTParsingExcep_InvalidArgSeparator,

	// missing closing bracket
	e_MTParsingExcep_MissingCloseBracket,

	// no function that takes that number of arguments has been found
	e_MTParsingExcep_OverloadedFuncNotFound,

	// generic invalid syntax error
	e_MTParsingExcep_InvalidSyntax,

	// missing operator: an operator was expected
	e_MTParsingExcep_MissingOp,

	// a function has been detected but there is no defined function with
	// this name
	e_MTParsingExcep_UndefinedFunc,	

	// a variable has been detected but there is no defined variable with
	// this name	
	e_MTParsingExcep_UndefinedVar,
	
	// An argument separator used with no value.
	// example: fct(,,d,)
	e_MTParsingExcep_UselessArgSeparator,
	
	// unexpected error or error that shouldn't be shown to the user
	e_MTParsingExcep_InternalError

};

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Canada Canada
Software Engineer working at a fun and smart startup company

Comments and Discussions