Main Page | Class Hierarchy | Class List | Directories | File List | Class Members

number.h

00001 /***************************************************************************
00002  *   Copyright (C) 2004-2006 by Radko Mihal                                *
00003  *   rmihal@pobox.sk                                                       *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
00019  ***************************************************************************/
00020 #ifndef NUMBER_H
00021 #define NUMBER_H
00022 
00023 // use for other then decimal numbers
00024 template< class digit_choice, char prefix='x' >
00025 class number
00026 {
00027 public:
00028         grammar_to_parser::terminal<'0'>                m_0;
00029         grammar_to_parser::terminal<prefix>     m_prefix;
00030 
00031         //digits->digit&digits | digit
00032         class digits
00033         {
00034                 class digit_and_digits
00035                 {
00036                         grammar_to_parser::choice< digit_choice >       m_digit;
00037                         grammar_to_parser::choice< digits >                     m_digits;
00038                 public:
00039                         void push_parsers( grammar_to_parser::parsers& l )
00040                         {
00041                                 l.push_back( &m_digit );
00042                                 l.push_back( &m_digits );
00043                         }
00044                         unsigned long evaluate( unsigned long value, unsigned long &pow )
00045                         {
00046                                 value = m_digits->evaluate( value, pow );
00047                                 pow = pow*m_digit->get_base();
00048                                 value += pow*m_digit->evaluate();
00049                                 return value;
00050                         }
00051 
00052                 };
00053                 grammar_to_parser::non_terminal<digit_and_digits>
00054                                                                                                                 m_digit_and_digits;
00055                 grammar_to_parser::choice<digit_choice>                 m_digit;
00056         public:
00057                 void push_parsers( grammar_to_parser::parsers &l )
00058                 {
00059                         l.push_back( &m_digit_and_digits );
00060                         l.push_back( &m_digit );
00061                 }
00062                 unsigned long evaluate( unsigned long value, unsigned long &pow )
00063                 {
00064                         if( m_digit.is_parsed() ) 
00065                         {
00066                                 value = m_digit->evaluate();
00067                                 pow = 1;
00068                                 return value;
00069                         }
00070                         else
00071                         {
00072                                 return m_digit_and_digits->evaluate( value, pow );
00073                         }
00074                 }
00075         };
00076         grammar_to_parser::choice<digits>                                       m_digits;
00077 public:
00078         void push_parsers( grammar_to_parser::parsers &l )
00079         {
00080                 l.push_back( &m_0 );
00081                 l.push_back( &m_prefix );
00082                 l.push_back( &m_digits );
00083         }
00084         unsigned long evaluate()
00085         {
00086                 unsigned long value=0;
00087                 unsigned long pow=0;
00088                 return m_digits->evaluate( value, pow );
00089         }
00090 };
00091 
00092 #endif

Generated on Sun Jul 2 18:39:43 2006 for grammar2parser.kdevelop by  doxygen 1.4.1