comparison trunk/src/dil/doc/Macro.d @ 723:5dd17d4568ce

Wrote code for expandMacros().
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Fri, 01 Feb 2008 20:51:44 +0100
parents ceaac6a24258
children 0b8a6e876b6d
comparison
equal deleted inserted replaced
722:ceaac6a24258 723:5dd17d4568ce
46 46
47 bool isRoot() 47 bool isRoot()
48 { return parent is null; } 48 { return parent is null; }
49 } 49 }
50 50
51 void skipWhitespace(ref char* p)
52 {
53 while (isspace(*p) || *p == '\n')
54 p++;
55 }
56
57 struct MacroParser 51 struct MacroParser
58 { 52 {
59 char* p; 53 char* p;
60 char* textEnd; 54 char* textEnd;
61 55
88 return macros; 82 return macros;
89 } 83 }
90 84
91 bool findNextMacroId(ref char* ref_idBegin, ref char* ref_idEnd, ref char* ref_bodyBegin) 85 bool findNextMacroId(ref char* ref_idBegin, ref char* ref_idEnd, ref char* ref_bodyBegin)
92 { 86 {
93 while (*p != '\0') 87 while (p < textEnd)
94 { 88 {
95 skipWhitespace(p); 89 skipWhitespace();
96 auto idBegin = p; 90 auto idBegin = p;
97 if (isidbeg(*p) || isUnicodeAlpha(p, textEnd)) // IdStart 91 if (isidbeg(*p) || isUnicodeAlpha(p, textEnd)) // IdStart
98 { 92 {
99 do // IdChar* 93 do // IdChar*
100 p++; 94 p++;
101 while (isident(*p) || isUnicodeAlpha(p, textEnd)) 95 while (isident(*p) || isUnicodeAlpha(p, textEnd))
102 auto idEnd = p; 96 auto idEnd = p;
103 97
104 skipWhitespace(p); 98 skipWhitespace();
105 if (*p == '=') 99 if (*p == '=')
106 { 100 {
101 p++;
102 skipWhitespace();
107 ref_idBegin = idBegin; 103 ref_idBegin = idBegin;
108 ref_idEnd = idEnd; 104 ref_idEnd = idEnd;
109 ref_bodyBegin = p + 1; 105 ref_bodyBegin = p;
110 return true; 106 return true;
111 } 107 }
112 } 108 }
113 skipLine(); 109 skipLine();
114 } 110 }
115 return false; 111 return false;
116 } 112 }
117 113
114 void skipWhitespace()
115 {
116 while (p < textEnd && (isspace(*p) || *p == '\n'))
117 p++;
118 }
119
118 void skipLine() 120 void skipLine()
119 { 121 {
120 while (*p != '\n') 122 while (p < textEnd && *p != '\n')
121 p++; 123 p++;
122 p++; 124 p++;
123 } 125 }
124 } 126 }
125 127
131 char[] expandMacros(MacroTable table, char[] text) 133 char[] expandMacros(MacroTable table, char[] text)
132 { 134 {
133 char[] result; 135 char[] result;
134 char* p = text.ptr; 136 char* p = text.ptr;
135 char* textEnd = p + text.length; 137 char* textEnd = p + text.length;
136 // while (p < text.length) 138 char* macroEnd = p;
137 // { 139 while (p+3 < textEnd) // minimum 4 chars: $(x)
138 140 {
139 // } 141 if (*p == '$' && p[1] == '(')
142 {
143 // Copy string between macros.
144 result ~= makeString(macroEnd, p);
145 p += 2;
146 auto idBegin = p;
147 if (isidbeg(*p) || isUnicodeAlpha(p, textEnd)) // IdStart
148 {
149 do // IdChar*
150 p++;
151 while (p < textEnd && (isident(*p) || isUnicodeAlpha(p, textEnd)))
152 auto macroName = makeString(idBegin, p);
153 if (*p == ')')
154 {
155 p++;
156 macroEnd = p;
157 }
158 auto macro_ = table.search(macroName);
159 if (macro_)
160 {
161 result ~= macro_.text;
162 }
163 }
164 }
165 p++;
166 }
140 return result; 167 return result;
141 } 168 }
142 169