File indexing completed on 2024-05-12 17:22:29

0001 // cnumberparser.h
0002 // Complex number support : complex number parser using a recursive
0003 // descent approach.
0004 //
0005 // This file is part of the SpeedCrunch project
0006 // Copyright (C) 2013, 2015-2016 Hadrien Theveneau <theveneau@gmail.com>.
0007 //
0008 // This program is free software; you can redistribute it and/or
0009 // modify it under the terms of the GNU General Public License
0010 // as published by the Free Software Foundation; either version 2
0011 // of the License, or (at your option) any later version.
0012 //
0013 // This program is distributed in the hope that it will be useful,
0014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
0015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0016 // GNU General Public License for more details.
0017 //
0018 // You should have received a copy of the GNU General Public License
0019 // along with this program; see the file COPYING.  If not, write to
0020 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0021 // Boston, MA 02110-1301, USA.
0022 
0023 
0024 // This complex number parser currently recognizes the following forms :
0025 //
0026 //   a
0027 //   a+ib
0028 //   a+jb
0029 //   a+bi
0030 //   a+bj
0031 //   ib
0032 //   jb
0033 //   bi
0034 //   bj
0035 //
0036 // The grammar describing one complex number is:
0037 //
0038 //   complex_number -> part suite
0039 //   suite -> []
0040 //   suite -> + part suite
0041 //
0042 //   part -> prefixed_part
0043 //   part -> postfixed_part
0044 //
0045 //   part_prefixed -> i base_number
0046 //   part_prefixed -> j base_number
0047 //
0048 //   part_postfixed -> base_number postfix
0049 //
0050 //   postfix -> i
0051 //   postfix -> j
0052 //   postfix -> []
0053 
0054 
0055 #ifndef CNUMBER_PARSER_HXX
0056 #define CNUMBER_PARSER_HXX
0057 
0058 #include <ctype.h>
0059 #include <string.h>
0060 
0061 #include "hmath.h"
0062 #include "cmath.h"
0063 
0064 class CNumberParser {
0065   private:
0066     const char * str;  /* Remaining of the string to parse */
0067 
0068     /* Parsing functions. */
0069     void accept () {str++;}
0070     /* Each of the following function parses one producion of the grammar. */
0071     CNumber part ();
0072     CNumber suite ();
0073     CNumber part_prefixed ();
0074     HNumber base_number ();
0075     CNumber part_postfixed ();
0076     typedef enum {IMAG, REAL} postfix_t;
0077     postfix_t postfix ();
0078 
0079   public:
0080     CNumberParser (const char * _str);
0081     void parse (CNumber * number);
0082 };
0083 
0084 namespace CNumberParserExceptions {
0085 
0086   class Exception
0087       : public std::exception {};
0088 
0089   class UnexpectedSymbol
0090       : public Exception {
0091     private:
0092       char symbol;
0093     public:
0094       char get_symbol () {return symbol;}
0095       UnexpectedSymbol (char _symbol) : symbol(_symbol) {}
0096       /* FIXME ! Error message ! */
0097   };
0098 
0099   class LogicError
0100       : Exception {};
0101 
0102   class UnexpectedEnd
0103       : public Exception {};
0104 
0105 }
0106 
0107 #endif // CNUMBER_PARSER_HXX