| 1 |
{ |
| 2 |
Copyright 2014-2019 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Simple macro system, to use in CGE build tool templates. } |
| 17 |
unit ToolMacros; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses CastleStringUtils; |
| 24 |
|
| 25 |
{ Replace macros in given string. |
| 26 |
Provide a Macros as a map, from uppercase keys. } |
| 27 |
function ReplaceMacros(const Macros: TStringStringMap; const Source: String): String; |
| 28 |
|
| 29 |
implementation |
| 30 |
|
| 31 |
uses SysUtils, StrUtils, |
| 32 |
CastleLog, CastleScript, CastleScriptParser, CastleUtils; |
| 33 |
|
| 34 |
function ReplaceMacros(const Macros: TStringStringMap; const Source: String): String; |
| 35 |
|
| 36 |
{ Create replacement map to perform the macros. } |
| 37 |
procedure CreateMacros(out Replacements, CastleScriptReplacements: TStringStringMap); |
| 38 |
|
| 39 |
function QuoteCastleScriptString(const S: String): String; |
| 40 |
begin |
| 41 |
Result := '''' + StringReplace(S, '''', '''''', [rfReplaceAll]) + ''''; |
| 42 |
end; |
| 43 |
|
| 44 |
var |
| 45 |
Macro: TStringStringMap.TDictionaryPair; |
| 46 |
begin |
| 47 |
Replacements := TStringStringMap.Create; |
| 48 |
CastleScriptReplacements := TStringStringMap.Create; |
| 49 |
|
| 50 |
for Macro in Macros do |
| 51 |
begin |
| 52 |
// add ${} around |
| 53 |
Replacements.Add('${' + Macro.Key + '}', Macro.Value); |
| 54 |
CastleScriptReplacements.Add('${' + Macro.Key + '}', QuoteCastleScriptString(Macro.Value)); |
| 55 |
end; |
| 56 |
end; |
| 57 |
|
| 58 |
const |
| 59 |
MacrosIgnoreCase = true; |
| 60 |
type |
| 61 |
TMacroExpression = ( |
| 62 |
meIf, |
| 63 |
meElse, |
| 64 |
meEndif, |
| 65 |
meCalculate |
| 66 |
); |
| 67 |
const |
| 68 |
ExpressionPrefix: array [TMacroExpression] of String = ( |
| 69 |
'${if ', |
| 70 |
'${else}', |
| 71 |
'${endif}', |
| 72 |
'${calculate ' |
| 73 |
); |
| 74 |
|
| 75 |
(* Nearest (from StartPositionToSearch) occurrence of expression begin, like '${IF...'. |
| 76 |
Sets also Expression. |
| 77 |
0 if not found (and leaves Expression undefined then). *) |
| 78 |
function NextExpressionPosition(const S, LowercaseS: String; |
| 79 |
const StartPositionToSearch: Integer; out Expression: TMacroExpression): Integer; |
| 80 |
var |
| 81 |
P: Integer; |
| 82 |
E: TMacroExpression; |
| 83 |
begin |
| 84 |
Result := 0; |
| 85 |
|
| 86 |
for E in TMacroExpression do |
| 87 |
begin |
| 88 |
P := PosEx(ExpressionPrefix[E], LowercaseS, StartPositionToSearch); |
| 89 |
if (P <> 0) and ((Result = 0) or (P < Result)) then |
| 90 |
begin |
| 91 |
Result := P; |
| 92 |
Expression := E; |
| 93 |
end; |
| 94 |
end; |
| 95 |
end; |
| 96 |
|
| 97 |
type |
| 98 |
TIfState = (isNone, isThen, isElse); |
| 99 |
var |
| 100 |
IfCondition: Boolean; |
| 101 |
IfState: TIfState; |
| 102 |
|
| 103 |
(* Process expression that starts as ExpressionBegin in S, ends at ExpressionEnd. |
| 104 |
Sets ExpressionEnd (last character of the expression, always '}') |
| 105 |
and returns the evaluated expression. *) |
| 106 |
function ProcessExpression(const S, LowercaseS: String; |
| 107 |
const Expression: TMacroExpression; |
| 108 |
const ExpressionBegin, ExpressionEnd: Integer; |
| 109 |
const CastleScriptReplacements: TStringStringMap): String; |
| 110 |
var |
| 111 |
ExpStr: String; |
| 112 |
Exp: TCasScriptExpression; |
| 113 |
{$ifdef DEBUG_MACROS} |
| 114 |
Pair: TStringStringMap.TDictionaryPair; |
| 115 |
{$endif} |
| 116 |
begin |
| 117 |
ExpStr := CopyPos(Source, ExpressionBegin + Length(ExpressionPrefix[Expression]), ExpressionEnd - 1); |
| 118 |
|
| 119 |
{$ifdef DEBUG_MACROS} |
| 120 |
Writeln('Replacements:'); |
| 121 |
Writeln(ExpStr); |
| 122 |
Writeln(CastleScriptReplacements.Count); |
| 123 |
for Pair in CastleScriptReplacements do |
| 124 |
begin |
| 125 |
Writeln(' ', Pair.Key, ' -> ', Pair.Value); |
| 126 |
end; |
| 127 |
{$endif} |
| 128 |
|
| 129 |
ExpStr := SReplacePatterns(ExpStr, CastleScriptReplacements, MacrosIgnoreCase); |
| 130 |
|
| 131 |
{$ifdef DEBUG_MACROS} |
| 132 |
Writeln('Output:'); |
| 133 |
Writeln(ExpStr); |
| 134 |
{$endif} |
| 135 |
|
| 136 |
case Expression of |
| 137 |
meIf: |
| 138 |
begin |
| 139 |
Result := ''; |
| 140 |
if IfState <> isNone then |
| 141 |
raise Exception.Create('Nesting ${IF} within ${IF} is not possible yet'); |
| 142 |
IfState := isThen; |
| 143 |
Exp := ParseBoolExpression(ExpStr, []); |
| 144 |
IfCondition := Exp.AsBool; |
| 145 |
end; |
| 146 |
meElse: |
| 147 |
begin |
| 148 |
Result := ''; |
| 149 |
if IfState <> isThen then |
| 150 |
raise Exception.Create('Unexpected ${ELSE}, not preceded by ${IF}'); |
| 151 |
IfState := isElse; |
| 152 |
end; |
| 153 |
meEndif: |
| 154 |
begin |
| 155 |
Result := ''; |
| 156 |
if not (IfState in [isThen, isElse]) then |
| 157 |
raise Exception.Create('Unexpected ${ENDIF}, not preceded by ${IF}'); |
| 158 |
IfState := isNone; |
| 159 |
end; |
| 160 |
meCalculate: |
| 161 |
begin |
| 162 |
Exp := ParseStringExpression(ExpStr, []); |
| 163 |
Result := Exp.AsString; |
| 164 |
end; |
| 165 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 166 |
else raise EInternalError.CreateFmt('Unknown expression %d', [Ord(Expression)]); |
| 167 |
{$endif} |
| 168 |
end; |
| 169 |
end; |
| 170 |
|
| 171 |
function ProcessText(const S: String; const Replacements: TStringStringMap): String; |
| 172 |
var |
| 173 |
Active: Boolean; |
| 174 |
begin |
| 175 |
case IfState of |
| 176 |
isNone: Active := true; |
| 177 |
isThen: Active := IfCondition; |
| 178 |
isElse: Active := not IfCondition; |
| 179 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 180 |
else raise EInternalError.Create('Unknown IfState'); |
| 181 |
{$endif} |
| 182 |
end; |
| 183 |
if Active then |
| 184 |
Result := SReplacePatterns(S, Replacements, MacrosIgnoreCase) |
| 185 |
else |
| 186 |
Result := ''; |
| 187 |
end; |
| 188 |
|
| 189 |
{ Return the content up to (excluding) the first newline |
| 190 |
(but with ellipsis instead of newline). } |
| 191 |
function FirstLine(const Source: String): String; |
| 192 |
var |
| 193 |
P: Integer; |
| 194 |
begin |
| 195 |
P := CharsPos([#13, #10], Source); |
| 196 |
if P <> 0 then |
| 197 |
Result := Copy(Source, 1, P - 1) + '...' |
| 198 |
else |
| 199 |
Result := Source; |
| 200 |
end; |
| 201 |
|
| 202 |
(* Find matching '}', assuming that at ExpressionBegin we see '{'. |
| 203 |
Raises exception if not found. *) |
| 204 |
function FindMatchingExpressionEnd(const Source: String; const ExpressionBegin: Integer): Integer; |
| 205 |
var |
| 206 |
LevelParen, LevelCurly, I: Integer; |
| 207 |
begin |
| 208 |
LevelCurly := 0; |
| 209 |
LevelParen := 0; |
| 210 |
(* Note that we know that the 1st loop iteration will encounter '{' |
| 211 |
and thus will set LevelCurly = 1. *) |
| 212 |
I := ExpressionBegin; |
| 213 |
repeat |
| 214 |
if I > Length(Source) then |
| 215 |
raise Exception.CreateFmt('Cannot find a matching closing "}" in the template source "%s"', |
| 216 |
[FirstLine(Source)]); |
| 217 |
case Source[I] of |
| 218 |
'{': Inc(LevelCurly); |
| 219 |
'}': |
| 220 |
begin |
| 221 |
Dec(LevelCurly); |
| 222 |
if (LevelCurly = 0) and (LevelParen = 0) then |
| 223 |
Exit(I); |
| 224 |
end; |
| 225 |
'(': Inc(LevelParen); |
| 226 |
')': Dec(LevelParen); |
| 227 |
end; |
| 228 |
Inc(I); |
| 229 |
until false; |
| 230 |
end; |
| 231 |
|
| 232 |
var |
| 233 |
Replacements, CastleScriptReplacements: TStringStringMap; |
| 234 |
Done, ExpressionBegin: Integer; |
| 235 |
Expression: TMacroExpression; |
| 236 |
LowercaseSource: String; |
| 237 |
begin |
| 238 |
CreateMacros(Replacements, CastleScriptReplacements); |
| 239 |
try |
| 240 |
LowercaseSource := AnsiLowerCase(Source); |
| 241 |
Done := 0; |
| 242 |
Result := ''; |
| 243 |
IfState := isNone; |
| 244 |
|
| 245 |
(* Split Source into chunks. |
| 246 |
One "chunk" here is either |
| 247 |
- an expression using macros, like '${CALCULATE xxx}' or '${IF xxx}' |
| 248 |
- not an expression, which is just a normal text a template where only simple |
| 249 |
macro variable replacement is performed. |
| 250 |
*) |
| 251 |
|
| 252 |
while Done < Length(Source) do |
| 253 |
begin |
| 254 |
ExpressionBegin := NextExpressionPosition(Source, LowercaseSource, |
| 255 |
Done + 1, Expression); |
| 256 |
if ExpressionBegin = 0 then |
| 257 |
begin |
| 258 |
{ chunk of normal text, up to the end of the Source } |
| 259 |
Result := Result + ProcessText(SEnding(Source, Done + 1), |
| 260 |
Replacements); |
| 261 |
Done := Length(Source); |
| 262 |
end else |
| 263 |
begin |
| 264 |
{ chunk of normal text, up to the beginning of nearest expression } |
| 265 |
Result := Result + ProcessText(CopyPos(Source, Done + 1, ExpressionBegin - 1), |
| 266 |
Replacements); |
| 267 |
{ chunk with one expression } |
| 268 |
Done := FindMatchingExpressionEnd(Source, ExpressionBegin); |
| 269 |
Result := Result + ProcessExpression(Source, LowercaseSource, Expression, |
| 270 |
ExpressionBegin, Done, CastleScriptReplacements); |
| 271 |
end; |
| 272 |
end; |
| 273 |
finally |
| 274 |
FreeAndNil(Replacements); |
| 275 |
FreeAndNil(CastleScriptReplacements); |
| 276 |
end; |
| 277 |
end; |
| 278 |
|
| 279 |
end. |