1 unit PreProcessorExpressionTokens;
2 
3 {(*}
4 (*------------------------------------------------------------------------------
5  Delphi Code formatter source code
6 
7 The Original Code is PreProcessorExpressionTokens, released August 2003.
8 The Initial Developer of the Original Code is Anthony Steele.
9 Portions created by Anthony Steele are Copyright (C) 2003 Anthony Steele.
10 All Rights Reserved.
11 Contributor(s): Anthony Steele.
12 
13 The contents of this file are subject to the Mozilla Public License Version 1.1
14 (the "License"). you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at http://www.mozilla.org/NPL/
16 
17 Software distributed under the License is distributed on an "AS IS" basis,
18 WITHOUT WARRANTY OF ANY KIND, either express or implied.
19 See the License for the specific language governing rights and limitations
20 under the License.
21 
22 Alternatively, the contents of this file may be used under the terms of
23 the GNU General Public License Version 2 or later (the "GPL")
24 See http://www.gnu.org/licenses/gpl.html
25 ------------------------------------------------------------------------------*)
26 {*)}
27 
28 {$I JcfGlobal.inc}
29 
30 interface
31 
32 {
33   AFS 26 Aug 2003
34 
35   Delphi preprocessor $IF expression parsing
36 
37   tokens defined via an enum, an item class
38   and a list class
39 }
40 
41 uses Contnrs;
42 
43 type
44 
45   TPreProcessorSymbol =
46     (eNone, eIdentifier,
47     // symbols with fixed text
48     eOpenBracket, eCloseBracket, eDefined, eDeclared,
49     eAnd, eOr, eNot, eTrue, eFalse);
50 
51 { used to recognise tokens - all expect identifiers have fixed text }
52 const
53   SYMBOL_DATA: array[eOpenBracket .. eFalse] of string =
54     ('(', ')', 'defined', 'declared', 'and', 'or', 'not', 'true', 'false');
55 
56 type
57 
58   TPreProcessorExpressionToken = class(TObject)
59   private
60     feSymbol: TPreProcessorSymbol;
61     fsSourceCode: string;
62   public
63     property Symbol: TPreProcessorSymbol Read feSymbol Write feSymbol;
64     property SourceCode: string Read fsSOurceCode Write fsSourceCode;
65   end;
66 
67   TPreProcessorExpressionTokenList = class(TObject)
68   private
69     fcList: TObjectList;
70 
GetItemsnull71     function GetItems(const piIndex: integer): TPreProcessorExpressionToken;
GetCountnull72     function GetCount: integer;
73   public
74     constructor Create;
75     destructor Destroy; override;
76 
Addnull77     function Add(const peSymbol: TPreProcessorSymbol;
78       psText: string): TPreProcessorExpressionToken;
79     procedure Clear;
80 
81     property Items[const piIndex: integer]: TPreProcessorExpressionToken Read GetItems;
82     property Count: integer Read GetCount;
83   end;
84 
PreProcessorSymbolToStringnull85 function PreProcessorSymbolToString(const peSymbol: TPreProcessorSymbol): string;
86 
87 implementation
88 
89 uses SysUtils;
90 
91 
PreProcessorSymbolToStringnull92 function PreProcessorSymbolToString(const peSymbol: TPreProcessorSymbol): string;
93 begin
94   case peSymbol of
95     eNone:
96       Result := 'No symbol';
97     eIdentifier:
98       Result := 'identifier';
99     eOpenBracket:
100       Result := '(';
101     eCloseBracket:
102       Result := ')';
103     eDefined:
104       Result := 'defined';
105     eAnd:
106       Result := 'and';
107     eOr:
108       Result := 'or';
109     eNot:
110       Result := 'not';
111     eTrue:
112       Result := 'true';
113     eFalse:
114       Result := 'false';
115     else
116       Assert(False);
117   end;
118 
119 end;
120 
121 { TPreProcessorExpressionTokenList }
122 
Addnull123 function TPreProcessorExpressionTokenList.Add(const peSymbol: TPreProcessorSymbol;
124   psText: string): TPreProcessorExpressionToken;
125 begin
126   Result := TPreProcessorExpressionToken.Create;
127   Result.Symbol := peSymbol;
128   Result.SourceCode := psText;
129 
130   fcList.Add(Result);
131 end;
132 
133 procedure TPreProcessorExpressionTokenList.Clear;
134 begin
135   fcList.Clear;
136 end;
137 
138 constructor TPreProcessorExpressionTokenList.Create;
139 begin
140   inherited;
141   // thiws is an owning list
142   fcList := TObjectList.Create;
143 end;
144 
145 destructor TPreProcessorExpressionTokenList.Destroy;
146 begin
147   FreeAndNil(fcList);
148   inherited;
149 
150 end;
151 
TPreProcessorExpressionTokenList.GetCountnull152 function TPreProcessorExpressionTokenList.GetCount: integer;
153 begin
154   Result := fcList.Count;
155 end;
156 
TPreProcessorExpressionTokenList.GetItemsnull157 function TPreProcessorExpressionTokenList.GetItems(
158   const piIndex: integer): TPreProcessorExpressionToken;
159 begin
160   if piIndex < fcList.Count then
161     Result := TPreProcessorExpressionToken(fcList[piIndex])
162   else
163     Result := nil;
164 end;
165 
166 
167 end.
168