summaryrefslogtreecommitdiff
path: root/src/unstable/minimacro.h
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-03-25 20:00:08 +0000
committerMike Buland <eichlan@xagasoft.com>2012-03-25 20:00:08 +0000
commit469bbcf0701e1eb8a6670c23145b0da87357e178 (patch)
treeb5b062a16e46a6c5d3410b4e574cd0cc09057211 /src/unstable/minimacro.h
parentee1b79396076edc4e30aefb285fada03bb45e80d (diff)
downloadlibbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.gz
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.bz2
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.xz
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.zip
Code is all reorganized. We're about ready to release. I should write up a
little explenation of the arrangement.
Diffstat (limited to 'src/unstable/minimacro.h')
-rw-r--r--src/unstable/minimacro.h130
1 files changed, 130 insertions, 0 deletions
diff --git a/src/unstable/minimacro.h b/src/unstable/minimacro.h
new file mode 100644
index 0000000..b6c7c13
--- /dev/null
+++ b/src/unstable/minimacro.h
@@ -0,0 +1,130 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#ifndef BU_MINI_MACRO_H
9#define BU_MINI_MACRO_H
10
11#include "bu/hash.h"
12#include "bu/string.h"
13
14namespace Bu
15{
16 typedef Bu::Hash<Bu::String, Bu::String> StrHash;
17 /**
18 * A processor for Libbu++ brand Mini Macros. These are really simple, but
19 * still fairly flexible. It's mainly text replacement, but with a few
20 * extras like filter functions and conditional text segments. So far we
21 * don't support loops or anything, I'm not sure we ever will.
22 *
23 * Anatomy of a mini macro:
24 * - Every macro begins with a two character code, the first character is
25 * always '{', the second character determines the operation to perform.
26 * - If the '{' is followed by a character that is not valid it is not
27 * considered for expansion and the characters are copied to the output.
28 * - Every macro ends with a closing '}'
29 * - Macro types:
30 * - '=': variable replacement. The '=' is immediatley followed by the
31 * name of the variable to replace, then any number of optional filter
32 * segments.
33 * - '?': conditional text. The '?' is immediately followed by the
34 * variable to test. This works two ways, the variable can be alone, in
35 * which case it's existance is tested, or it can be followed by a "="
36 * and a string to compare to. This is then followed by a text segment
37 * that will be used if the test is true, and an optional text segment
38 * to be used if the test is false.
39 * - ':': command. The ':' is immediately followed by a command string,
40 * of which there's only one right now, but that's ok. These are not
41 * put into the output stream, but instead mark something for the
42 * parser. Currently supported:
43 * - {:end}: end of parsing, stop here, also make note of how many input
44 * characters were used.
45 * - Segments:
46 * - Each segment is seperated by a colon.
47 * - Filter segments give the name of the filter, followed by
48 * parenthesies. Parameters may be provided within the parenthesies.
49 * - Text segments should always be quoted, but may contain any characters
50 * within the quotes, backslash is used as per C/ANSI/ISO standard.
51 * You can also quote any text using [' '] instead of quotes, which
52 * allows for nested strings. The [' token is only recognised within
53 * a macro.
54 *
55 *@verbatim
56 {=name:tolower()}
57 {=name:ccsplit("_"):toupper()}
58 {?name:"name exists and is {=name}"}
59 {?name:"{=name}":"no name!"}
60 {?name="bob":"You're named bob!":"Who are you? I only know bob..."}
61 @endverbatim
62 */
63 class MiniMacro
64 {
65 public:
66 MiniMacro();
67 MiniMacro( const StrHash &sVarSrc );
68 virtual ~MiniMacro();
69
70 Bu::String parse( const Bu::String &sIn );
71 void addVar( const Bu::String &sName, const Bu::String &sValue );
72 bool hasVar( const Bu::String &sName );
73 const Bu::String &getVar( const Bu::String &sName );
74 const StrHash &getVars();
75 int getPosition();
76
77 private:
78 const char *sCur;
79 Bu::String parseRepl();
80 Bu::String parseCond();
81 Bu::String parseCmd();
82 Bu::String callFunc(
83 const Bu::String &sIn, const Bu::String &sFunc );
84
85 StrHash hVars;
86 bool bContinue;
87 int iLastPos;
88
89 public:
90 typedef Bu::List<Bu::String> StrList;
91 class Func
92 {
93 public:
94 Func(){}
95 virtual ~Func(){}
96 virtual Bu::String call(
97 const Bu::String &sIn, StrList &lsParam )=0;
98 };
99
100 class FuncToUpper : public Func
101 {
102 public:
103 FuncToUpper(){}
104 virtual ~FuncToUpper(){}
105 virtual Bu::String call(
106 const Bu::String &sIn, StrList & )
107 {
108 return sIn.toUpper();
109 }
110 };
111
112 class FuncToLower : public Func
113 {
114 public:
115 FuncToLower(){}
116 virtual ~FuncToLower(){}
117 virtual Bu::String call(
118 const Bu::String &sIn, StrList & )
119 {
120 return sIn.toLower();
121 }
122 };
123
124 private:
125 typedef Bu::Hash<Bu::String,class Func *> FuncHash;
126 FuncHash hFuncs;
127 };
128};
129
130#endif