aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2007-12-03 19:51:09 +0000
committerMike Buland <eichlan@xagasoft.com>2007-12-03 19:51:09 +0000
commit96ca22c7a1f9c5990e136a6ccddc8a33319b9043 (patch)
treeb6223207babe8f22c600e9a0fde3e5b25143d3b7 /src
parent81e7d758bbb83fc1bdf9df2b9b17d4a7998aec5b (diff)
downloadlibbu++-96ca22c7a1f9c5990e136a6ccddc8a33319b9043.tar.gz
libbu++-96ca22c7a1f9c5990e136a6ccddc8a33319b9043.tar.bz2
libbu++-96ca22c7a1f9c5990e136a6ccddc8a33319b9043.tar.xz
libbu++-96ca22c7a1f9c5990e136a6ccddc8a33319b9043.zip
Hey, Bu::MiniMacro works, that's a funny name. There's still some more to add,
but it does everything I need it to at the moment.
Diffstat (limited to 'src')
-rw-r--r--src/minimacro.cpp117
-rw-r--r--src/minimacro.h68
2 files changed, 183 insertions, 2 deletions
diff --git a/src/minimacro.cpp b/src/minimacro.cpp
index 274c13b..374b7de 100644
--- a/src/minimacro.cpp
+++ b/src/minimacro.cpp
@@ -6,12 +6,129 @@
6 */ 6 */
7 7
8#include "bu/minimacro.h" 8#include "bu/minimacro.h"
9#include "bu/exceptions.h"
9 10
10Bu::MiniMacro::MiniMacro() 11Bu::MiniMacro::MiniMacro()
11{ 12{
13 hFuncs.insert("toupper", new FuncToUpper() );
14 hFuncs.insert("tolower", new FuncToLower() );
12} 15}
13 16
14Bu::MiniMacro::~MiniMacro() 17Bu::MiniMacro::~MiniMacro()
15{ 18{
16} 19}
17 20
21Bu::FString Bu::MiniMacro::parse( const Bu::FString &sIn )
22{
23 Bu::FString sOut;
24 for( sCur = sIn.getStr(); *sCur; sCur++ )
25 {
26 if( *sCur == '{' )
27 {
28 switch( sCur[1] )
29 {
30 case '=':
31 sCur += 2;
32 sOut += parseRepl();
33 break;
34
35 case '?':
36 sCur += 2;
37 sOut += parseCond();
38 break;
39
40 case ':':
41 sCur += 2;
42 sOut += parseCmd();
43 break;
44
45 default:
46 sOut += *sCur;
47 continue;
48 }
49 }
50 else
51 {
52 sOut += *sCur;
53 }
54 }
55
56 return sOut;
57}
58
59Bu::FString Bu::MiniMacro::parseRepl()
60{
61 Bu::FString sOut;
62 bool bIsFirst = true;
63 for( const char *sNext = sCur;;)
64 {
65 for(; *sNext != ':' && *sNext != '}' && *sNext != '\0'; sNext++ );
66 if( *sNext == '\0' )
67 break;
68 Bu::FString sName( sCur, (int)sNext-(int)sCur );
69 if( bIsFirst )
70 {
71 sOut = hVars[sName];
72 bIsFirst = false;
73 printf("Variable: \"%s\"\n", sName.getStr() );
74 }
75 else
76 {
77 sOut = callFunc( sOut, sName );
78 printf("Filter: \"%s\"\n", sName.getStr() );
79 }
80 if( *sNext == '}' )
81 {
82 sCur = sNext;
83 break;
84 }
85 else if( *sNext == ':' )
86 {
87 }
88 sNext++;
89 sCur = sNext;
90 }
91 return sOut;
92}
93
94Bu::FString Bu::MiniMacro::parseCond()
95{
96 Bu::FString sOut;
97 printf("%20s\n", sCur );
98 return sOut;
99}
100
101Bu::FString Bu::MiniMacro::parseCmd()
102{
103 Bu::FString sOut;
104 printf("%20s\n", sCur );
105 return sOut;
106}
107
108Bu::FString Bu::MiniMacro::callFunc(
109 const Bu::FString &sIn, const Bu::FString &sFunc )
110{
111 int i = sFunc.find('(');
112 if( i < 0 )
113 throw Bu::ExceptionBase("That doesn't look like a function call");
114 Bu::FString sName( sFunc.getStr(), i );
115 StrList lsParams;
116 for( const char *s = sFunc.getStr()+i+1; *s && *s != ')'; s++ )
117 {
118 for(; *s == ' ' || *s == '\t' || *s == '\r' || *s == '\n'; s++ );
119 const char *sNext;
120 for( sNext = s; *sNext && *sNext != ')' && *sNext != ','; sNext++ );
121 Bu::FString p( s, (int)sNext-(int)s );
122 lsParams.append( p );
123 sNext++;
124 s = sNext;
125 }
126 return hFuncs.get( sName )->call( sIn, lsParams );
127}
128
129void Bu::MiniMacro::addVar(
130 const Bu::FString &sName, const Bu::FString &sValue )
131{
132 hVars.insert( sName, sValue );
133}
134
diff --git a/src/minimacro.h b/src/minimacro.h
index e136015..105a117 100644
--- a/src/minimacro.h
+++ b/src/minimacro.h
@@ -8,6 +8,9 @@
8#ifndef BU_MINI_MACRO_H 8#ifndef BU_MINI_MACRO_H
9#define BU_MINI_MACRO_H 9#define BU_MINI_MACRO_H
10 10
11#include "bu/hash.h"
12#include "bu/fstring.h"
13
11namespace Bu 14namespace Bu
12{ 15{
13 /** 16 /**
@@ -32,14 +35,20 @@ namespace Bu
32 * and a string to compare to. This is then followed by a text segment 35 * and a string to compare to. This is then followed by a text segment
33 * that will be used if the test is true, and an optional text segment 36 * that will be used if the test is true, and an optional text segment
34 * to be used if the test is false. 37 * to be used if the test is false.
38 * - ':': command. The ':' is immediately followed by a command string,
39 * of which there's only one right now, but that's ok. These are not
40 * put into the output stream, but instead mark something for the
41 * parser. Currently supported:
42 * - {:end}: end of parsing, stop here, also make note of how many input
43 * characters were used.
35 * - Segments: 44 * - Segments:
36 * - Each segment is seperated by a colon. 45 * - Each segment is seperated by a colon.
37 * - Filter segments give the name of the filter, followed by 46 * - Filter segments give the name of the filter, followed by
38 * parenthesies. Parameters may be provided within the parenthesies. 47 * parenthesies. Parameters may be provided within the parenthesies.
39 * - Text segments should always be quoted, but may contain any characters 48 * - Text segments should always be quoted, but may contain any characters
40 * within the quotes, backslash is used as per C/ANSI/ISO standard. 49 * within the quotes, backslash is used as per C/ANSI/ISO standard.
41 * You can also quote any text using [" "] instead of quotes, which 50 * You can also quote any text using [' '] instead of quotes, which
42 * allows for nested strings. The [" token is only recognised within 51 * allows for nested strings. The [' token is only recognised within
43 * a macro. 52 * a macro.
44 * 53 *
45 *@verbatim 54 *@verbatim
@@ -50,13 +59,68 @@ namespace Bu
50 {?name="bob":"You're named bob!":"Who are you? I only know bob..."} 59 {?name="bob":"You're named bob!":"Who are you? I only know bob..."}
51 @endverbatim 60 @endverbatim
52 */ 61 */
62 typedef Bu::Hash<Bu::FString, Bu::FString> StrHash;
53 class MiniMacro 63 class MiniMacro
54 { 64 {
55 public: 65 public:
56 MiniMacro(); 66 MiniMacro();
57 virtual ~MiniMacro(); 67 virtual ~MiniMacro();
58 68
69 Bu::FString parse( const Bu::FString &sIn );
70 void addVar( const Bu::FString &sName, const Bu::FString &sValue );
71
72 private:
73 const char *sCur;
74 Bu::FString parseRepl();
75 Bu::FString parseCond();
76 Bu::FString parseCmd();
77 Bu::FString callFunc(
78 const Bu::FString &sIn, const Bu::FString &sFunc );
79
80 StrHash hVars;
81
82 public:
83 typedef Bu::List<Bu::FString> StrList;
84 class Func
85 {
86 public:
87 Func(){}
88 virtual ~Func(){}
89 virtual Bu::FString call(
90 const Bu::FString &sIn, StrList &lsParam )=0;
91 };
92
93 class FuncToUpper : public Func
94 {
95 public:
96 FuncToUpper(){}
97 virtual ~FuncToUpper(){}
98 virtual Bu::FString call(
99 const Bu::FString &sIn, StrList &lsParam )
100 {
101 Bu::FString sOut( sIn );
102 sOut.toUpper();
103 return sOut;
104 }
105 };
106
107 class FuncToLower : public Func
108 {
109 public:
110 FuncToLower(){}
111 virtual ~FuncToLower(){}
112 virtual Bu::FString call(
113 const Bu::FString &sIn, StrList &lsParam )
114 {
115 Bu::FString sOut( sIn );
116 sOut.toLower();
117 return sOut;
118 }
119 };
120
59 private: 121 private:
122 typedef Bu::Hash<Bu::FString,class Func *> FuncHash;
123 FuncHash hFuncs;
60 }; 124 };
61}; 125};
62 126