From de83196633b28f06bb8c08ddf5780cc8049703f4 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 5 Jan 2012 00:41:22 -0700 Subject: Interface plugin basics. SML system parser. --- src/interface.cpp | 10 ++++ src/interface.h | 13 +++++ src/interfaceconsole.cpp | 10 ++++ src/interfaceconsole.h | 13 +++++ src/options.cpp | 24 +++++++++ src/options.h | 1 + src/smlnode.cpp | 123 +++++++++++++++++++++++++++++++++++++++++++++++ src/smlnode.h | 41 ++++++++++++++++ 8 files changed, 235 insertions(+) create mode 100644 src/interface.cpp create mode 100644 src/interface.h create mode 100644 src/interfaceconsole.cpp create mode 100644 src/interfaceconsole.h create mode 100644 src/smlnode.cpp create mode 100644 src/smlnode.h diff --git a/src/interface.cpp b/src/interface.cpp new file mode 100644 index 0000000..c75b682 --- /dev/null +++ b/src/interface.cpp @@ -0,0 +1,10 @@ +#include "interface.h" + +Interface::Interface() +{ +} + +Interface::~Interface() +{ +} + diff --git a/src/interface.h b/src/interface.h new file mode 100644 index 0000000..4103f93 --- /dev/null +++ b/src/interface.h @@ -0,0 +1,13 @@ +#ifndef INTERFACE_H +#define INTERFACE_H + +class Interface +{ +public: + Interface(); + virtual ~Interface(); + + void display(); +}; + +#endif diff --git a/src/interfaceconsole.cpp b/src/interfaceconsole.cpp new file mode 100644 index 0000000..0b1b43f --- /dev/null +++ b/src/interfaceconsole.cpp @@ -0,0 +1,10 @@ +#include "interfaceconsole.h" + +InterfaceConsole::InterfaceConsole() +{ +} + +InterfaceConsole::~InterfaceConsole() +{ +} + diff --git a/src/interfaceconsole.h b/src/interfaceconsole.h new file mode 100644 index 0000000..74f88d0 --- /dev/null +++ b/src/interfaceconsole.h @@ -0,0 +1,13 @@ +#ifndef INTERFACE_CONSOLE_H +#define INTERFACE_CONSOLE_H + +#include "interface.h" + +class InterfaceConsole : public Interface +{ +public: + InterfaceConsole(); + virtual ~InterfaceConsole(); +}; + +#endif diff --git a/src/options.cpp b/src/options.cpp index f5fb127..1cc76ce 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -2,8 +2,11 @@ #include "version.h" #include "game.h" +#include "smlnode.h" + #include +#include #include #include @@ -26,6 +29,8 @@ void Options::parse( int argc, char *argv[] ) opt.addHelpBanner("usage: " + Bu::String(argv[0]) + " [options] \n"); + opt.addOption( Bu::slot( this, &Options::smlTest ), "sml-test", + "Test SML parser." ); opt.addOption( Bu::slot( this, &Options::version ), "version", "Show full version info." ); opt.addOption( Bu::slot( this, &Options::builtins ), "builtins", @@ -62,6 +67,25 @@ int Options::builtins( Bu::StrArray aArgs ) return 0; } +int Options::smlTest( Bu::Array aArgs ) +{ + Bu::String sContent; + Bu::File fIn( aArgs[1], Bu::File::Read ); + while( !fIn.isEos() ) + { + char buf[4096]; + sContent.append( buf, fIn.read( buf, 4096 ) ); + } + fIn.close(); + + SmlNode *pRoot = SmlNode::parse( sContent ); + sio << *pRoot << sio.nl; + delete pRoot; + + exit( 0 ); + return 0; +} + int Options::nonOption( Bu::Array aArgs ) { sFile = aArgs[0]; diff --git a/src/options.h b/src/options.h index 5b3f413..b97285b 100644 --- a/src/options.h +++ b/src/options.h @@ -20,6 +20,7 @@ public: protected: int version( Bu::Array aArgs ); int builtins( Bu::Array aArgs ); + int smlTest( Bu::Array aArgs ); int nonOption( Bu::Array aArgs ); }; diff --git a/src/smlnode.cpp b/src/smlnode.cpp new file mode 100644 index 0000000..a9e533d --- /dev/null +++ b/src/smlnode.cpp @@ -0,0 +1,123 @@ +#include "smlnode.h" + +#include + +using namespace Bu; + +SmlNode::SmlNode( Type eType, const Bu::String &sText, SmlNode *pParent ) : + eType( eType ), + sText( sText ), + pParent( pParent ) +{ +} + +SmlNode::~SmlNode() +{ +} + +SmlNode *SmlNode::parse( const Bu::String &sSrc ) +{ + SmlNode *pRoot = new SmlNode( SmlNode::typeRoot ); + SmlNode *pCur = pRoot; + pRoot->eType = typeRoot; + + enum Mode + { + mText, + mStartTag, + mEndTag, + }; + Bu::String sTmp; + Mode mode = mText; + for( Bu::String::const_iterator i = sSrc.begin(); i; i++ ) + { + if( *i == '\\' ) + { + i++; + if( !i ) + sTmp += '\\'; + else if( *i == '[' || *i == ']' || *i == '<' || *i == '>' ) + sTmp += *i; + } + else if( *i == '[' && mode == mText ) + { + if( !sTmp.isEmpty() ) + pCur->append( typeText, sTmp ); + sTmp.clear(); + mode = mStartTag; + } + else if( *i == '<' && mode == mText ) + { + if( !sTmp.isEmpty() ) + pCur->append( typeText, sTmp ); + sTmp.clear(); + mode = mEndTag; + } + else if( *i == '>' && mode == mStartTag ) + { + pCur = pCur->append( typeTag, sTmp ); + sTmp.clear(); + mode = mText; + } + else if( *i == ']' && mode == mStartTag ) + { + pCur->append( typeTag, sTmp ); + sTmp.clear(); + mode = mText; + } + else if( *i == ']' && mode == mEndTag ) + { + if( sTmp != pCur->getText() ) + throw Bu::ExceptionBase("Tag mismatch: [%s> != <%s]\n", + pCur->getText().getStr(), sTmp.getStr() ); + pCur = pCur->getParent(); + sTmp.clear(); + mode = mText; + } + else + { + sTmp += *i; + } + } + if( !sTmp.isEmpty() ) + pCur->append( typeText, sTmp ); + + return pRoot; +} + +SmlNode *SmlNode::append( SmlNode::Type eType, const Bu::String &sText ) +{ + SmlNode *pChild = new SmlNode( eType, sText, this ); + lChildren.append( pChild ); + return pChild; +} + +Bu::Formatter &operator<<( Bu::Formatter &f, const SmlNode &n ) +{ + switch( n.eType ) + { + case SmlNode::typeRoot: + f.incIndent(); + f << "Root {" << f.nl; + break; + + case SmlNode::typeText: + return f << "Text: >>" << n.sText << "<<" << f.nl; + break; + + case SmlNode::typeTag: + f.incIndent(); + f << "Tag[" << n.sText << "] {" << f.nl; + break; + } + + for( SmlNode::SmlNodeList::const_iterator i = n.lChildren.begin(); i; i++ ) + { + f << *(*i); + } + f.decIndent(); + f << "}" << f.nl; + + return f; +} + diff --git a/src/smlnode.h b/src/smlnode.h new file mode 100644 index 0000000..92dd65e --- /dev/null +++ b/src/smlnode.h @@ -0,0 +1,41 @@ +#ifndef SML_NODE_H +#define SML_NODE_H + +#include +#include + +class SmlNode +{ +friend Bu::Formatter &operator<<( Bu::Formatter &f, const SmlNode &n ); +public: + enum Type + { + typeRoot, + typeText, + typeTag, + }; + + SmlNode( Type eType, const Bu::String &sText=Bu::String(), SmlNode *pParent=NULL ); + virtual ~SmlNode(); + + static SmlNode *parse( const Bu::String &sSrc ); + + typedef Bu::List SmlNodeList; + + SmlNode *append( Type type, const Bu::String &sText ); + SmlNode *getParent() { return pParent; } + + Type getType() const { return eType; } + Bu::String getText() const { return sText; } + const SmlNodeList &getChildren() const { return lChildren; } + +private: + Type eType; + Bu::String sText; + SmlNodeList lChildren; + SmlNode *pParent; +}; + +Bu::Formatter &operator<<( Bu::Formatter &f, const SmlNode &n ); + +#endif -- cgit v1.2.3