From: Mike D. Lowis Date: Wed, 27 Jun 2012 20:02:51 +0000 (-0400) Subject: Updated lexer class to support combining lexers together X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=e2b262f1c501ba87725fb00efaa97ad386b59ae9;p=archive%2Fparse-utils.git Updated lexer class to support combining lexers together --- diff --git a/tests/test_experimental.cpp b/tests/test_experimental.cpp index 25d7b99..79bb20b 100644 --- a/tests/test_experimental.cpp +++ b/tests/test_experimental.cpp @@ -49,14 +49,21 @@ class CharBuffer : public IBuffer } }; +//----------------------------------------------------------------------------- +// Lexer Class +//----------------------------------------------------------------------------- class Lexer { + protected: + Lexer* lex_sibling; + Lexer* lex_child; public: - Lexer() + // Construction/Destruction + Lexer() : lex_sibling(0), lex_child(0) { } - Lexer(Lexer& lxr) + Lexer(const Lexer& lxr) : lex_sibling(0), lex_child(0) { } @@ -64,38 +71,66 @@ class Lexer { } - Lexer* clone() + // Accessors + Lexer* sibling() + { + return lex_sibling; + } + + void sibling(Lexer* lxr) + { + lex_sibling = lxr; + } + + Lexer* child() + { + return lex_child; + } + + void child(Lexer* lxr) + { + lex_sibling = lxr; + } + + // Action methods + Lexer* clone() const { return new Lexer(*this); } - Token operator() (CharBuffer& input) + void addSibling(Lexer* lxr) { - return Token(); } - Lexer& operator= (const Lexer& rhs) + void addChild(Lexer* lxr) { - return *this; + } + + // Operators + Token operator() (CharBuffer& input) + { + std::cout << this << " "; + if( NULL != sibling() ) + { + (*sibling())( input ); + } + std::cout << std::endl; + return Token(); } Lexer& operator+ (const Lexer& rhs) { + addSibling( rhs.clone() ); return *this; } Lexer& operator| (const Lexer& rhs) { + addChild( rhs.clone() ); return *this; } }; -class Parser -{ - public: - //AST* operator() (CharBuffer& input) = 0; -}; - //----------------------------------------------------------------------------- // Begin Unit Tests //----------------------------------------------------------------------------- @@ -108,9 +143,7 @@ namespace { std::ifstream file; file.open( "input.txt" ); CharBuffer input( file ); - Lexer lexer; - lexer = (Lexer() + Lexer()) - | (Lexer() + Lexer() + Lexer()); + Lexer& lexer = (Lexer() + Lexer() + Lexer()); lexer( input ); } }