diff options
-rw-r--r-- | src/interface.cpp | 10 | ||||
-rw-r--r-- | src/interface.h | 13 | ||||
-rw-r--r-- | src/interfaceconsole.cpp | 10 | ||||
-rw-r--r-- | src/interfaceconsole.h | 13 | ||||
-rw-r--r-- | src/options.cpp | 24 | ||||
-rw-r--r-- | src/options.h | 1 | ||||
-rw-r--r-- | src/smlnode.cpp | 123 | ||||
-rw-r--r-- | src/smlnode.h | 41 |
8 files changed, 235 insertions, 0 deletions
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 @@ | |||
1 | #include "interface.h" | ||
2 | |||
3 | Interface::Interface() | ||
4 | { | ||
5 | } | ||
6 | |||
7 | Interface::~Interface() | ||
8 | { | ||
9 | } | ||
10 | |||
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 @@ | |||
1 | #ifndef INTERFACE_H | ||
2 | #define INTERFACE_H | ||
3 | |||
4 | class Interface | ||
5 | { | ||
6 | public: | ||
7 | Interface(); | ||
8 | virtual ~Interface(); | ||
9 | |||
10 | void display(); | ||
11 | }; | ||
12 | |||
13 | #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 @@ | |||
1 | #include "interfaceconsole.h" | ||
2 | |||
3 | InterfaceConsole::InterfaceConsole() | ||
4 | { | ||
5 | } | ||
6 | |||
7 | InterfaceConsole::~InterfaceConsole() | ||
8 | { | ||
9 | } | ||
10 | |||
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 @@ | |||
1 | #ifndef INTERFACE_CONSOLE_H | ||
2 | #define INTERFACE_CONSOLE_H | ||
3 | |||
4 | #include "interface.h" | ||
5 | |||
6 | class InterfaceConsole : public Interface | ||
7 | { | ||
8 | public: | ||
9 | InterfaceConsole(); | ||
10 | virtual ~InterfaceConsole(); | ||
11 | }; | ||
12 | |||
13 | #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 @@ | |||
2 | #include "version.h" | 2 | #include "version.h" |
3 | #include "game.h" | 3 | #include "game.h" |
4 | 4 | ||
5 | #include "smlnode.h" | ||
6 | |||
5 | #include <stdlib.h> | 7 | #include <stdlib.h> |
6 | 8 | ||
9 | #include <bu/file.h> | ||
7 | #include <bu/optparser.h> | 10 | #include <bu/optparser.h> |
8 | #include <bu/sio.h> | 11 | #include <bu/sio.h> |
9 | 12 | ||
@@ -26,6 +29,8 @@ void Options::parse( int argc, char *argv[] ) | |||
26 | opt.addHelpBanner("usage: " + Bu::String(argv[0]) + | 29 | opt.addHelpBanner("usage: " + Bu::String(argv[0]) + |
27 | " [options] <filename>\n"); | 30 | " [options] <filename>\n"); |
28 | 31 | ||
32 | opt.addOption( Bu::slot( this, &Options::smlTest ), "sml-test", | ||
33 | "Test SML parser." ); | ||
29 | opt.addOption( Bu::slot( this, &Options::version ), "version", | 34 | opt.addOption( Bu::slot( this, &Options::version ), "version", |
30 | "Show full version info." ); | 35 | "Show full version info." ); |
31 | opt.addOption( Bu::slot( this, &Options::builtins ), "builtins", | 36 | opt.addOption( Bu::slot( this, &Options::builtins ), "builtins", |
@@ -62,6 +67,25 @@ int Options::builtins( Bu::StrArray aArgs ) | |||
62 | return 0; | 67 | return 0; |
63 | } | 68 | } |
64 | 69 | ||
70 | int Options::smlTest( Bu::Array<Bu::String> aArgs ) | ||
71 | { | ||
72 | Bu::String sContent; | ||
73 | Bu::File fIn( aArgs[1], Bu::File::Read ); | ||
74 | while( !fIn.isEos() ) | ||
75 | { | ||
76 | char buf[4096]; | ||
77 | sContent.append( buf, fIn.read( buf, 4096 ) ); | ||
78 | } | ||
79 | fIn.close(); | ||
80 | |||
81 | SmlNode *pRoot = SmlNode::parse( sContent ); | ||
82 | sio << *pRoot << sio.nl; | ||
83 | delete pRoot; | ||
84 | |||
85 | exit( 0 ); | ||
86 | return 0; | ||
87 | } | ||
88 | |||
65 | int Options::nonOption( Bu::Array<Bu::String> aArgs ) | 89 | int Options::nonOption( Bu::Array<Bu::String> aArgs ) |
66 | { | 90 | { |
67 | sFile = aArgs[0]; | 91 | 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: | |||
20 | protected: | 20 | protected: |
21 | int version( Bu::Array<Bu::String> aArgs ); | 21 | int version( Bu::Array<Bu::String> aArgs ); |
22 | int builtins( Bu::Array<Bu::String> aArgs ); | 22 | int builtins( Bu::Array<Bu::String> aArgs ); |
23 | int smlTest( Bu::Array<Bu::String> aArgs ); | ||
23 | int nonOption( Bu::Array<Bu::String> aArgs ); | 24 | int nonOption( Bu::Array<Bu::String> aArgs ); |
24 | }; | 25 | }; |
25 | 26 | ||
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 @@ | |||
1 | #include "smlnode.h" | ||
2 | |||
3 | #include <bu/sio.h> | ||
4 | |||
5 | using namespace Bu; | ||
6 | |||
7 | SmlNode::SmlNode( Type eType, const Bu::String &sText, SmlNode *pParent ) : | ||
8 | eType( eType ), | ||
9 | sText( sText ), | ||
10 | pParent( pParent ) | ||
11 | { | ||
12 | } | ||
13 | |||
14 | SmlNode::~SmlNode() | ||
15 | { | ||
16 | } | ||
17 | |||
18 | SmlNode *SmlNode::parse( const Bu::String &sSrc ) | ||
19 | { | ||
20 | SmlNode *pRoot = new SmlNode( SmlNode::typeRoot ); | ||
21 | SmlNode *pCur = pRoot; | ||
22 | pRoot->eType = typeRoot; | ||
23 | |||
24 | enum Mode | ||
25 | { | ||
26 | mText, | ||
27 | mStartTag, | ||
28 | mEndTag, | ||
29 | }; | ||
30 | Bu::String sTmp; | ||
31 | Mode mode = mText; | ||
32 | for( Bu::String::const_iterator i = sSrc.begin(); i; i++ ) | ||
33 | { | ||
34 | if( *i == '\\' ) | ||
35 | { | ||
36 | i++; | ||
37 | if( !i ) | ||
38 | sTmp += '\\'; | ||
39 | else if( *i == '[' || *i == ']' || *i == '<' || *i == '>' ) | ||
40 | sTmp += *i; | ||
41 | } | ||
42 | else if( *i == '[' && mode == mText ) | ||
43 | { | ||
44 | if( !sTmp.isEmpty() ) | ||
45 | pCur->append( typeText, sTmp ); | ||
46 | sTmp.clear(); | ||
47 | mode = mStartTag; | ||
48 | } | ||
49 | else if( *i == '<' && mode == mText ) | ||
50 | { | ||
51 | if( !sTmp.isEmpty() ) | ||
52 | pCur->append( typeText, sTmp ); | ||
53 | sTmp.clear(); | ||
54 | mode = mEndTag; | ||
55 | } | ||
56 | else if( *i == '>' && mode == mStartTag ) | ||
57 | { | ||
58 | pCur = pCur->append( typeTag, sTmp ); | ||
59 | sTmp.clear(); | ||
60 | mode = mText; | ||
61 | } | ||
62 | else if( *i == ']' && mode == mStartTag ) | ||
63 | { | ||
64 | pCur->append( typeTag, sTmp ); | ||
65 | sTmp.clear(); | ||
66 | mode = mText; | ||
67 | } | ||
68 | else if( *i == ']' && mode == mEndTag ) | ||
69 | { | ||
70 | if( sTmp != pCur->getText() ) | ||
71 | throw Bu::ExceptionBase("Tag mismatch: [%s> != <%s]\n", | ||
72 | pCur->getText().getStr(), sTmp.getStr() ); | ||
73 | pCur = pCur->getParent(); | ||
74 | sTmp.clear(); | ||
75 | mode = mText; | ||
76 | } | ||
77 | else | ||
78 | { | ||
79 | sTmp += *i; | ||
80 | } | ||
81 | } | ||
82 | if( !sTmp.isEmpty() ) | ||
83 | pCur->append( typeText, sTmp ); | ||
84 | |||
85 | return pRoot; | ||
86 | } | ||
87 | |||
88 | SmlNode *SmlNode::append( SmlNode::Type eType, const Bu::String &sText ) | ||
89 | { | ||
90 | SmlNode *pChild = new SmlNode( eType, sText, this ); | ||
91 | lChildren.append( pChild ); | ||
92 | return pChild; | ||
93 | } | ||
94 | |||
95 | Bu::Formatter &operator<<( Bu::Formatter &f, const SmlNode &n ) | ||
96 | { | ||
97 | switch( n.eType ) | ||
98 | { | ||
99 | case SmlNode::typeRoot: | ||
100 | f.incIndent(); | ||
101 | f << "Root {" << f.nl; | ||
102 | break; | ||
103 | |||
104 | case SmlNode::typeText: | ||
105 | return f << "Text: >>" << n.sText << "<<" << f.nl; | ||
106 | break; | ||
107 | |||
108 | case SmlNode::typeTag: | ||
109 | f.incIndent(); | ||
110 | f << "Tag[" << n.sText << "] {" << f.nl; | ||
111 | break; | ||
112 | } | ||
113 | |||
114 | for( SmlNode::SmlNodeList::const_iterator i = n.lChildren.begin(); i; i++ ) | ||
115 | { | ||
116 | f << *(*i); | ||
117 | } | ||
118 | f.decIndent(); | ||
119 | f << "}" << f.nl; | ||
120 | |||
121 | return f; | ||
122 | } | ||
123 | |||
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 @@ | |||
1 | #ifndef SML_NODE_H | ||
2 | #define SML_NODE_H | ||
3 | |||
4 | #include <bu/string.h> | ||
5 | #include <bu/list.h> | ||
6 | |||
7 | class SmlNode | ||
8 | { | ||
9 | friend Bu::Formatter &operator<<( Bu::Formatter &f, const SmlNode &n ); | ||
10 | public: | ||
11 | enum Type | ||
12 | { | ||
13 | typeRoot, | ||
14 | typeText, | ||
15 | typeTag, | ||
16 | }; | ||
17 | |||
18 | SmlNode( Type eType, const Bu::String &sText=Bu::String(), SmlNode *pParent=NULL ); | ||
19 | virtual ~SmlNode(); | ||
20 | |||
21 | static SmlNode *parse( const Bu::String &sSrc ); | ||
22 | |||
23 | typedef Bu::List<SmlNode *> SmlNodeList; | ||
24 | |||
25 | SmlNode *append( Type type, const Bu::String &sText ); | ||
26 | SmlNode *getParent() { return pParent; } | ||
27 | |||
28 | Type getType() const { return eType; } | ||
29 | Bu::String getText() const { return sText; } | ||
30 | const SmlNodeList &getChildren() const { return lChildren; } | ||
31 | |||
32 | private: | ||
33 | Type eType; | ||
34 | Bu::String sText; | ||
35 | SmlNodeList lChildren; | ||
36 | SmlNode *pParent; | ||
37 | }; | ||
38 | |||
39 | Bu::Formatter &operator<<( Bu::Formatter &f, const SmlNode &n ); | ||
40 | |||
41 | #endif | ||