1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
unit PreProcessorExpressionParser;
{(*}
(*------------------------------------------------------------------------------
Delphi Code formatter source code
The Original Code is PreProcessorParse, released October 2003.
The Initial Developer of the Original Code is Anthony Steele.
Portions created by Anthony Steele are Copyright (C) 1999-2008 Anthony Steele.
All Rights Reserved.
Contributor(s): Anthony Steele.
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"). you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.mozilla.org/NPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied.
See the License for the specific language governing rights and limitations
under the License.
Alternatively, the contents of this file may be used under the terms of
the GNU General Public License Version 2 or later (the "GPL")
See http://www.gnu.org/licenses/gpl.html
------------------------------------------------------------------------------*)
{*)}
{$I JcfGlobal.inc}
interface
{
AFS 26 Aug 2003
Delphi preprocessor $IF expression parsing
an 'immediate' parser that evalutes the expression,
does not produce a parse tree except in the ephemera of the call stack
All these expression are in $IF preprocessor statements
and thus have boolean results
The entire expression is contained in the text string of a single token
Grammar:
expr -> term
expr -> term and expr
expr -> term or expr
term -> (expr)
term -> defined(identifier)
term -> not term
term -> true
term -> false
and that's all for now. Just "defined" checks, with brackets, negation and conjunctions
More will come later, such as '=' '<' etc.
But that will necessitate symbols with values and some kind of type inference
}
uses
{ celphi }
Classes, SysUtils,
{ local }
PreProcessorExpressionTokens;
type
{ distinguish these exceptions from others }
PreProcessorParseFailedException = class(Exception);
TPreProcessorExpressionParser = class(TObject)
private
fiCurrentIndex: integer;
// referenced data
fcTokens: TPreProcessorExpressionTokenList;
fcDefinedSymbols: TStrings;
function ParseExpr: boolean;
function ParseTerm: boolean;
function CurrentTokenType: TPreProcessorSymbol;
function MoreTokens: boolean;
procedure Consume(const peType: TPreProcessorSymbol);
function SymbolIsDefined(const psSymbol: string): boolean;
function SymbolIsDeclared(const psSymbol: string): boolean;
public
function Parse: boolean;
property Tokens: TPreProcessorExpressionTokenList Read fcTokens Write fcTokens;
property DefinedSymbols: TStrings Read fcDefinedSymbols Write fcDefinedSymbols;
end;
implementation
{ TPreProcessorExpressionParser }
procedure TPreProcessorExpressionParser.Consume(const peType: TPreProcessorSymbol);
begin
Assert(CurrentTokenType = peType,
'expected token ' + PreProcessorSymbolToString(peType) +
' got ' + PreProcessorSymbolToString(CurrentTokenType) +
' at position ' + IntToStr(fiCurrentIndex));
Inc(fiCurrentIndex)
end;
function TPreProcessorExpressionParser.CurrentTokenType: TPreProcessorSymbol;
begin
if fiCurrentIndex < fcTokens.Count then
Result := fcTokens.Items[fiCurrentIndex].Symbol
else
Result := eNone;
end;
function TPreProcessorExpressionParser.MoreTokens: boolean;
begin
Result := fcTokens.Count > fiCurrentIndex;
end;
function TPreProcessorExpressionParser.Parse: boolean;
begin
Assert(fcTokens <> nil);
Assert(fcTokens.Count > 0);
fiCurrentIndex := 0;
Result := ParseExpr;
if MoreTokens then
raise PreProcessorParseFailedException.Create('Expression has trailing tokens');
end;
function TPreProcessorExpressionParser.ParseExpr: boolean;
var
lbExprResult: boolean;
begin
Result := ParseTerm;
if MoreTokens then
begin
case CurrentTokenType of
eAnd:
begin
Consume(eAnd);
// always evaluate this
lbExprResult := ParseExpr;
Result := Result and lbExprResult;
end;
eOr:
begin
Consume(eOr);
// always evaluate this
lbExprResult := ParseExpr;
Result := Result or lbExprResult;
end;
eCloseBracket:
begin
// do nothing, should be matched to open bracket below
end;
else
begin
raise PreProcessorParseFailedException.Create(
'Preprocessor expression could not be parsed');
end;
end;
end;
end;
function TPreProcessorExpressionParser.ParseTerm: boolean;
begin
case CurrentTokenType of
eOpenBracket:
begin
Consume(eOpenBracket);
Result := ParseExpr;
Consume(eCloseBracket);
end;
eDefined:
begin
Consume(eDefined);
Consume(eOpenBracket);
Result := SymbolIsDefined(Tokens.Items[fiCurrentIndex].SourceCode);
Consume(eIdentifier);
Consume(eCloseBracket);
end;
eDeclared:
begin
Consume(eDeclared);
Consume(eOpenBracket);
Result := SymbolIsDeclared(Tokens.Items[fiCurrentIndex].SourceCode);
Consume(eIdentifier);
Consume(eCloseBracket);
end;
eNot:
begin
Consume(eNot);
Result := not ParseTerm;
end;
eTrue:
begin
Consume(eTrue);
Result := True;
end;
eFalse:
begin
Consume(eFalse);
Result := False;
end;
eIdentifier:
begin
Consume(eIdentifier);
// we don't know, so guess. One of us can't be wrong
Result := True;
end;
else
begin
CurrentTokenType;
raise PreProcessorParseFailedException.Create(
'Preprocessor term could not be parsed');
end;
end;
end;
function TPreProcessorExpressionParser.SymbolIsDefined(const psSymbol: string): boolean;
begin
Result := DefinedSymbols.IndexOf(psSymbol) >= 0;
end;
function TPreProcessorExpressionParser.SymbolIsDeclared(const psSymbol: string): boolean;
begin
{ 'Declared returns true if the argument passed to it
is a valid declared Delphi identifier visible within the current scope.'
we are faking it }
Result := (psSymbol <> '');
end;
end.
|