Click here to Skip to main content
15,886,774 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.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>MTParserLib: MTParserExcepStrEng.cpp Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.4 -->
<div class="qindex"><a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="annotated.html">Class&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="namespacemembers.html">Namespace&nbsp;Members</a> | <a class="qindex" href="functions.html">Class&nbsp;Members</a> | <a class="qindex" href="globals.html">File&nbsp;Members</a></div>
<h1>MTParserExcepStrEng.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="preprocessor">#include "<a class="code" href="MTParserExcepStrEng_8h.html">MTParserExcepStrEng.h</a>"</span>
<a name="l00002"></a>00002 <span class="preprocessor">#include "<a class="code" href="MTTools_8h.html">MTTools.h</a>"</span>
<a name="l00003"></a>00003 
<a name="l00004"></a>00004 
<a name="l00005"></a>00005 
<a name="l00006"></a>00006 <a class="code" href="classMTParserExcepStrEng.html">MTParserExcepStrEng</a> MTParserExcepStrEng::m_instance = <a class="code" href="classMTParserExcepStrEng.html">MTParserExcepStrEng</a>();
<a name="l00007"></a>00007 
<a name="l00008"></a>00008 MTParserExcepStrEng::MTParserExcepStrEng()
<a name="l00009"></a>00009 {       
<a name="l00010"></a>00010         addExcep(<a class="code" href="MTParserException_8h.html#a1">MTDEFEXCEP_OpAlreadyDefined</a>, _T(<span class="stringliteral">"Operator already defined: '%itemName'"</span>));             
<a name="l00011"></a>00011         addExcep(<a class="code" href="MTParserException_8h.html#a2">MTDEFEXCEP_OpNameNull</a>, _T(<span class="stringliteral">"Operator name cannot be null"</span>));
<a name="l00012"></a>00012         addExcep(<a class="code" href="MTParserException_8h.html#a3">MTDEFEXCEP_OpNameSpace</a>, _T(<span class="stringliteral">"This operator name contains space characters: '%itemName'"</span>));              
<a name="l00013"></a>00013         addExcep(<a class="code" href="MTParserException_8h.html#a4">MTDEFEXCEP_OpNameSyntaxConflict</a>, _T(<span class="stringliteral">"Operator name '%itemName' conflicts with the syntax element character: '%conflictItemName'."</span>));           
<a name="l00014"></a>00014         addExcep(<a class="code" href="MTParserException_8h.html#a5">MTDEFEXCEP_OpPrecedence</a>, _T(<span class="stringliteral">"Operator: '%itemName'; invalid precedence."</span>));                    
<a name="l00015"></a>00015         addExcep(<a class="code" href="MTParserException_8h.html#a7">MTDEFEXCEP_FuncAlreadyDefined</a>, _T(<span class="stringliteral">"Function already defined: '%itemName'"</span>));           
<a name="l00016"></a>00016         addExcep(<a class="code" href="MTParserException_8h.html#a8">MTDEFEXCEP_FuncNameNull</a>, _T(<span class="stringliteral">"Function name cannot be null"</span>));
<a name="l00017"></a>00017         addExcep(<a class="code" href="MTParserException_8h.html#a9">MTDEFEXCEP_FuncNameSpace</a>, _T(<span class="stringliteral">"This function name contains space characters: '%itemName'"</span>));            
<a name="l00018"></a>00018         addExcep(<a class="code" href="MTParserException_8h.html#a10">MTDEFEXCEP_FuncNameSyntaxConflict</a>, _T(<span class="stringliteral">"Function name '%itemName' conflicts with the syntax element character: '%conflictItemName'"</span>));          
<a name="l00019"></a>00019         addExcep(<a class="code" href="MTParserException_8h.html#a11">MTDEFEXCEP_FuncNameOpConflict</a>, _T(<span class="stringliteral">"Function name '%itemName' conflicts with the operator '%conflictItemName'"</span>));
<a name="l00020"></a>00020         addExcep(<a class="code" href="MTParserException_8h.html#a6">MTDEFEXCEP_ItemNotFound</a>, _T(<span class="stringliteral">"Cannot find the specified item: id = %itemName"</span>));                
<a name="l00021"></a>00021         addExcep(<a class="code" href="MTParserException_8h.html#a12">MTDEFEXCEP_VarAlreadyDefined</a>, _T(<span class="stringliteral">"Variable already defined: '%itemName'"</span>));            
<a name="l00022"></a>00022         addExcep(<a class="code" href="MTParserException_8h.html#a13">MTDEFEXCEP_VarNameNull</a>, _T(<span class="stringliteral">"Variable name cannot be null"</span>));
<a name="l00023"></a>00023         addExcep(<a class="code" href="MTParserException_8h.html#a14">MTDEFEXCEP_VarNameSpace</a>, _T(<span class="stringliteral">"This variable name contains space characters: '%itemName'"</span>));             
<a name="l00024"></a>00024         addExcep(<a class="code" href="MTParserException_8h.html#a15">MTDEFEXCEP_VarNameOnlyNum</a>, _T(<span class="stringliteral">"Invalid variable name: '%itemName'.  Cannot be composed only of numbers"</span>));             
<a name="l00025"></a>00025         addExcep(<a class="code" href="MTParserException_8h.html#a16">MTDEFEXCEP_VarNameConstConflict</a>, _T(<span class="stringliteral">"Variable name '%itemName' conflicts with the constant of the same name."</span>));
<a name="l00026"></a>00026         addExcep(<a class="code" href="MTParserException_8h.html#a17">MTDEFEXCEP_VarNameSyntaxConflict</a>, _T(<span class="stringliteral">"Variable name '%itemName'  conflicts with the syntax element character: '%conflictItemName'"</span>));  
<a name="l00027"></a>00027         addExcep(<a class="code" href="MTParserException_8h.html#a18">MTDEFEXCEP_VarNameOpConflict</a>, _T(<span class="stringliteral">"Variable name '%itemName' conflicts with the operator: '%conflictItemName'"</span>));                       
<a name="l00028"></a>00028         addExcep(<a class="code" href="MTParserException_8h.html#a19">MTDEFEXCEP_ConstAlreadyDefined</a>, _T(<span class="stringliteral">"Constant already defined: '%itemName'"</span>));          
<a name="l00029"></a>00029         addExcep(<a class="code" href="MTParserException_8h.html#a20">MTDEFEXCEP_ConstNameNull</a>, _T(<span class="stringliteral">"Constant name cannot be null"</span>));
<a name="l00030"></a>00030         addExcep(<a class="code" href="MTParserException_8h.html#a21">MTDEFEXCEP_ConstNameSpace</a>, _T(<span class="stringliteral">"This constant name contains space characters: '%itemName'"</span>));           
<a name="l00031"></a>00031         addExcep(<a class="code" href="MTParserException_8h.html#a22">MTDEFEXCEP_ConstNameOnlyNum</a>, _T(<span class="stringliteral">"Invalid constant name: '%itemName'.  Cannot be composed only of numbers."</span>));          
<a name="l00032"></a>00032         addExcep(<a class="code" href="MTParserException_8h.html#a23">MTDEFEXCEP_ConstNameSyntaxConflict</a>, _T(<span class="stringliteral">"Constant name '%itemName' conflicts with the syntax element character: '%conflictItemName'"</span>));
<a name="l00033"></a>00033         addExcep(<a class="code" href="MTParserException_8h.html#a24">MTDEFEXCEP_ConstNameOpConflict</a>, _T(<span class="stringliteral">"Constant name '%itemName' conflicts with the operator '%conflictItemName'"</span>));              
<a name="l00034"></a>00034         addExcep(<a class="code" href="MTParserException_8h.html#a25">MTDEFEXCEP_ConstNameVarConflict</a>, _T(<span class="stringliteral">"Constant name '%itemName' conflicts with the variable of the same name"</span>));                
<a name="l00035"></a>00035         addExcep(<a class="code" href="MTParserException_8h.html#a26">MTDEFEXCEP_PluginVersion</a>, _T(<span class="stringliteral">"The plug-in version is incompatible with this library.  Plug-in CLSID = %itemName.  Requested version: %conflictItemName.  Plug-in version: %param1"</span>));
<a name="l00036"></a>00036         addExcep(<a class="code" href="MTParserException_8h.html#a27">MTDEFEXCEP_PluginNotFound</a>, _T(<span class="stringliteral">"The plug-in cannot be found.  Make sure the plug-in is properly registered. CLSID = %itemName"</span>));
<a name="l00037"></a>00037         addExcep(<a class="code" href="MTParserException_8h.html#a28">MTDEFEXCEP_PluginTypeLibNotFound</a>, _T(<span class="stringliteral">"The plug-in type lib file cannot be found: '%itemName'.  This file describes the plug-in interface."</span>));
<a name="l00038"></a>00038         addExcep(<a class="code" href="MTParserException_8h.html#a29">MTDEFEXCEP_PluginDefConflict</a>, _T(<span class="stringliteral">"The plug-in cannot be loaded because of a conflict with an already defined item.  Plug-in CLSID = %itemName"</span>));
<a name="l00039"></a>00039         addExcep(<a class="code" href="MTParserException_8h.html#a30">MTDEFEXCEP_MacroProtoSyntax</a>, _T(<span class="stringliteral">"The macro prototype syntax is incorrect: '%itemName'. The proper syntax is: macro(arg1, arg2, arg3...)."</span>));           
<a name="l00040"></a>00040         addExcep(<a class="code" href="MTParserException_8h.html#a31">MTPARSINGEXCEP_InvalidOpSyntax</a>, _T(<span class="stringliteral">"Invalid operator syntax: '%itemName'"</span>));           
<a name="l00041"></a>00041         addExcep(<a class="code" href="MTParserException_8h.html#a32">MTPARSINGEXCEP_InvalidFuncSyntax</a>, _T(<span class="stringliteral">"Invalid function argument syntax. Function name: '%itemName', argument %param1"</span>));               
<a name="l00042"></a>00042         addExcep(<a class="code" href="MTParserException_8h.html#a33">MTPARSINGEXCEP_UnexpectedBeginVarName</a>, _T(<span class="stringliteral">"Unexpected begin variable name character"</span>));                                        
<a name="l00043"></a>00043         addExcep(<a class="code" href="MTParserException_8h.html#a34">MTPARSINGEXCEP_UnexpectedEndVarName</a>, _T(<span class="stringliteral">"Unexpected end variable name character"</span>));                                    
<a name="l00044"></a>00044         addExcep(<a class="code" href="MTParserException_8h.html#a35">MTPARSINGEXCEP_UnexpectedOp</a>, _T(<span class="stringliteral">"Unexpected operator"</span>));               
<a name="l00045"></a>00045         addExcep(<a class="code" href="MTParserException_8h.html#a36">MTPARSINGEXCEP_MissingOpenBracket</a>, _T(<span class="stringliteral">"Missing opening bracket to macth closing bracket"</span>));                                    
<a name="l00046"></a>00046         addExcep(<a class="code" href="MTParserException_8h.html#a37">MTPARSINGEXCEP_InvalidArgSeparator</a>, _T(<span class="stringliteral">"Invalid use of an argument separator outside a function"</span>));
<a name="l00047"></a>00047         addExcep(<a class="code" href="MTParserException_8h.html#a38">MTPARSINGEXCEP_MissingCloseBracket</a>, _T(<span class="stringliteral">"Missing closing bracket to match opening bracket"</span>));
<a name="l00048"></a>00048         addExcep(<a class="code" href="MTParserException_8h.html#a39">MTPARSINGEXCEP_OverloadedFuncNotFound</a>, _T(<span class="stringliteral">"Invalid number of arguments for function '%itemName'"</span>));
<a name="l00049"></a>00049         addExcep(<a class="code" href="MTParserException_8h.html#a40">MTPARSINGEXCEP_InvalidSyntax</a>, _T(<span class="stringliteral">"Invalid syntax"</span>));                           
<a name="l00050"></a>00050         addExcep(<a class="code" href="MTParserException_8h.html#a41">MTPARSINGEXCEP_MissingOp</a>, _T(<span class="stringliteral">"Missing operator before: '%itemName'"</span>));         
<a name="l00051"></a>00051         addExcep(<a class="code" href="MTParserException_8h.html#a42">MTPARSINGEXCEP_UndefinedFunc</a>, _T(<span class="stringliteral">"Undefined function: '%itemName'"</span>));          
<a name="l00052"></a>00052         addExcep(<a class="code" href="MTParserException_8h.html#a43">MTPARSINGEXCEP_UndefinedVar</a>, _T(<span class="stringliteral">"Undefined variable: '%itemName'"</span>));           
<a name="l00053"></a>00053         addExcep(<a class="code" href="MTParserException_8h.html#a44">MTPARSINGEXCEP_UselessArgSeparator</a>, _T(<span class="stringliteral">"Invalid use of an argument separator"</span>));               
<a name="l00054"></a>00054         addExcep(<a class="code" href="MTParserException_8h.html#a45">MTPARSINGEXCEP_InternalError</a>, _T(<span class="stringliteral">"Internal error"</span>));           
<a name="l00055"></a>00055         addExcep(<a class="code" href="MTParserException_8h.html#a46">MTLOCEXCEP_MTParserInfoObjectNotFound</a>, _T(<span class="stringliteral">"The MTParserInfo COM object cannot be instanciated. Make sure it is registered."</span>));
<a name="l00056"></a>00056         addExcep(<a class="code" href="MTParserException_8h.html#a47">MTLOCEXCEP_InfoFileOpenFailed</a>, _T(<span class="stringliteral">"Unable to OPEN the library info file: '%itemName'. Make sure the file exists and that the schema and xml syntax are valids."</span>));             
<a name="l00057"></a>00057         addExcep(<a class="code" href="MTParserException_8h.html#a48">MTLOCEXCEP_InfoFileLoadFailed</a>, _T(<span class="stringliteral">"Unable to LOAD the library info file: '%itemName'. Make sure no xml attribute is missing."</span>));               
<a name="l00058"></a>00058         addExcep(<a class="code" href="MTParserException_8h.html#a49">MTLOCEXCEP_InfoFileLocaleFailed</a>, _T(<span class="stringliteral">"Locale information not available for the library info file: '%itemName'.  Locale: %conflictItemName"</span>));           
<a name="l00059"></a>00059         addExcep(<a class="code" href="MTParserException_8h.html#a50">MTLOCEXCEP_InfoFileBadVersion</a>, _T(<span class="stringliteral">"The library info file version is incorrect.  File: '%itemName'"</span>));          
<a name="l00060"></a>00060         addExcep(<a class="code" href="MTParserException_8h.html#a51">MTEXCEP_OutOfMemory</a>, _T(<span class="stringliteral">"Out of memory"</span>));
<a name="l00061"></a>00061 }
<a name="l00062"></a>00062 
<a name="l00063"></a>00063 <span class="keywordtype">void</span> MTParserExcepStrEng::addExcep(<span class="keyword">const</span> <a class="code" href="MTUnicodeANSIDefs_8h.html#a1">MTCHAR</a> *<span class="keywordtype">id</span>, <span class="keyword">const</span> <a class="code" href="MTUnicodeANSIDefs_8h.html#a1">MTCHAR</a> *msg)
<a name="l00064"></a>00064 {
<a name="l00065"></a>00065         SExcepInfo info;
<a name="l00066"></a>00066         info.id = id;
<a name="l00067"></a>00067         info.msg = msg;
<a name="l00068"></a>00068         m_exceps.push_back(info);
<a name="l00069"></a>00069 }
<a name="l00070"></a>00070 
<a name="l00071"></a>00071 <a class="code" href="MTUnicodeANSIDefs_8h.html#a0">MTSTRING</a> MTParserExcepStrEng::internalFormat(<span class="keyword">const</span> <a class="code" href="classMTExcepData.html">MTExcepData</a> &amp;data)
<a name="l00072"></a>00072 {
<a name="l00073"></a>00073         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> size = m_exceps.size();
<a name="l00074"></a>00074 
<a name="l00075"></a>00075         <span class="keywordflow">for</span>( <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> t=0; t&lt;size; t++ )
<a name="l00076"></a>00076         {
<a name="l00077"></a>00077                 <span class="keywordflow">if</span>( lstrcmp( m_exceps[t].<span class="keywordtype">id</span>.c_str(), data.<a class="code" href="classMTExcepData.html#a4">getID</a>()) == 0 )
<a name="l00078"></a>00078                 {
<a name="l00079"></a>00079                         <span class="keywordflow">return</span> <a class="code" href="classMTParserExcepStrEng.html#e0">MTParserExcepStrEng::format</a>(data, m_exceps[t].msg.c_str());
<a name="l00080"></a>00080                 }
<a name="l00081"></a>00081         }
<a name="l00082"></a>00082 
<a name="l00083"></a>00083         <span class="comment">// exception not found...</span>
<a name="l00084"></a>00084         <a class="code" href="MTUnicodeANSIDefs_8h.html#a0">MTSTRING</a> msg = _T(<span class="stringliteral">"Unknown exception: "</span>);
<a name="l00085"></a>00085         msg += data.<a class="code" href="classMTExcepData.html#a4">getID</a>();
<a name="l00086"></a>00086         <span class="keywordflow">return</span> msg;
<a name="l00087"></a>00087 }
<a name="l00088"></a>00088 
<a name="l00089"></a><a class="code" href="classMTParserExcepStrEng.html#e0">00089</a> <a class="code" href="MTUnicodeANSIDefs_8h.html#a0">MTSTRING</a> <a class="code" href="classMTParserExcepStrEng.html#e0">MTParserExcepStrEng::format</a>(<span class="keyword">const</span> <a class="code" href="classMTExcepData.html">MTExcepData</a> &amp;data)
<a name="l00090"></a>00090 {
<a name="l00091"></a>00091         <span class="keywordflow">return</span> m_instance.<a class="code" href="classMTParserExcepStrEng.html#d2">internalFormat</a>(data); 
<a name="l00092"></a>00092 }
<a name="l00093"></a>00093 
<a name="l00094"></a><a class="code" href="classMTParserExcepStrEng.html#e1">00094</a> <a class="code" href="MTUnicodeANSIDefs_8h.html#a0">MTSTRING</a> <a class="code" href="classMTParserExcepStrEng.html#e0">MTParserExcepStrEng::format</a>(<span class="keyword">const</span> <a class="code" href="classMTExcepData.html">MTExcepData</a> &amp;data, <span class="keyword">const</span> <a class="code" href="MTUnicodeANSIDefs_8h.html#a1">MTCHAR</a> *msg)
<a name="l00095"></a>00095 {
<a name="l00096"></a>00096         <a class="code" href="MTUnicodeANSIDefs_8h.html#a0">MTSTRING</a> formattedMsg = msg;    
<a name="l00097"></a>00097         <a class="code" href="MTUnicodeANSIDefs_8h.html#a0">MTSTRING</a> argName, argVal;
<a name="l00098"></a>00098 
<a name="l00099"></a>00099         <span class="comment">// replace all arguments by their values</span>
<a name="l00100"></a>00100         <span class="keywordflow">for</span>( <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> t=0; t&lt;data.<a class="code" href="classMTExcepData.html#a1">getNbArgs</a>(); t++ )
<a name="l00101"></a>00101         {
<a name="l00102"></a>00102                 argName = data.<a class="code" href="classMTExcepData.html#a2">getArgName</a>(t);
<a name="l00103"></a>00103                 argVal = data.<a class="code" href="classMTExcepData.html#a3">getArgVal</a>(t);
<a name="l00104"></a>00104 
<a name="l00105"></a>00105                 argName = _T(<span class="stringliteral">"%"</span>) + argName;
<a name="l00106"></a>00106                 <a class="code" href="namespaceMTTools.html#a2">MTTools::replaceSubStr</a>(formattedMsg, argName.c_str(), argVal.c_str());
<a name="l00107"></a>00107 
<a name="l00108"></a>00108         }       
<a name="l00109"></a>00109 
<a name="l00110"></a>00110         <span class="keywordflow">return</span> formattedMsg;
<a name="l00111"></a>00111 }
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Mar 9 17:39:36 2008 for MTParserLib by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.4 </small></address>
</body>
</html>

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