From 1ee5f374ed986333d5cdbbf41390f1c4c755a8e3 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 12 Oct 2010 06:09:48 +0000 Subject: This commit has a minor tweak to the variant class to make it easier to use, and introduces the parser and lexer classes. I also made a test for parser and put it in the tools directory. That is silly, it shouldn't be. However, it's necesarry right now, because I don't want to do a full build to compile all the parser tests. However, this commit doesn't actually build yet. It will soon, I just wanted to get it all committed. --- src/tools/parser.cpp | 164 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 src/tools/parser.cpp (limited to 'src/tools/parser.cpp') diff --git a/src/tools/parser.cpp b/src/tools/parser.cpp new file mode 100644 index 0000000..a70dfa4 --- /dev/null +++ b/src/tools/parser.cpp @@ -0,0 +1,164 @@ +#include +#include +#include +#include +#include +#include + +using namespace Bu; + +enum Tok +{ + tokNumber, + tokPlus, + tokMinus, + tokDivide, + tokMultiply, + tokOpenParen, + tokCloseParen, + tokCompute, + tokEndOfInput=-1 +}; + +Bu::Formatter &operator<<( Bu::Formatter &f, Tok e ) +{ + switch( e ) + { + case tokNumber: return f << "tokNumber"; + case tokPlus: return f << "tokPlus"; + case tokMinus: return f << "tokMinus"; + case tokDivide: return f << "tokDivide"; + case tokMultiply: return f << "tokMultiply"; + case tokOpenParen: return f << "tokOpenParen"; + case tokCloseParen: return f << "tokCloseParen"; + case tokCompute: return f << "tokCompute"; + case tokEndOfInput: return f << "tokEndOfInput"; + } + + return f << "***error***"; +} + +class MathLexer : public Lexer +{ +public: + MathLexer( Bu::Stream &rSrc ) : + rSrc( rSrc ) + { + } + + virtual ~MathLexer() + { + } + + enum TokenTypes + { + tokStuff + }; + + virtual Token *nextToken() + { + for(;;) + { + if( qbIn.getSize() == 0 ) + { + char buf[4096]; + qbIn.write( buf, rSrc.read( buf, 4096 ) ); + + if( rSrc.isEos() && qbIn.getSize() == 0 ) + return new Token( tokEndOfInput ); + } + + char b; + qbIn.peek( &b, 1 ); + switch( b ) + { + case '+': + qbIn.seek( 1 ); + return new Token( tokPlus ); + + case '-': + qbIn.seek( 1 ); + return new Token( tokMinus ); + + case '/': + qbIn.seek( 1 ); + return new Token( tokDivide ); + + case '*': + qbIn.seek( 1 ); + return new Token( tokMultiply ); + + case ' ': + case '\t': + case '\n': + qbIn.seek( 1 ); + break; + + case '=': + qbIn.seek( 1 ); + return new Token( tokCompute ); + + case '(': + qbIn.seek( 1 ); + return new Token( tokOpenParen ); + + case ')': + qbIn.seek( 1 ); + return new Token( tokCloseParen ); + + case '.': + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + { + Bu::FString sTmp; + sTmp += b; + qbIn.seek( 1 ); + for(;;) + { + qbIn.peek( &b, 1 ); + if( b != '.' && (b < '0' || b > '9') ) + { + sio << "!! Convert '" << sTmp << "' to " + << strtod( sTmp.getStr(), NULL ) << sio.nl; + return new Token( + tokNumber, strtod( sTmp.getStr(), NULL ) + ); + } + qbIn.seek( 1 ); + sTmp += b; + } + } + break; + + default: + throw Bu::ExceptionBase("Unexpected character '%c'.", b ); + } + } + } + +private: + Bu::Stream &rSrc; + QueueBuf qbIn; +}; + +int main( int argc, char *argv[] ) +{ + File fIn( argv[1], File::Read ); + + Parser p; + + p.pushLexer( new MathLexer( fIn ) ); + + p.parse(); + + return 0; +} + -- cgit v1.2.3