Menu

[r111]: / trunk / Src / UHilitePasParser.pas  Maximize  Restore  History

Download this file

264 lines (239 with data), 8.6 kB

  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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
{
* UHilitePasParser.pas
*
* Defines class that parses Pascal source files and splits into different
* highlighting elements.
*
* v0.1 of 10 Mar 2005 - Original version.
* v0.2 of 21 Apr 2005 - Added reference to new IntfHiliter unit that contains
* enumerated type formerly defined in this unit.
* v1.0 of 24 May 2006 - Improved and corrected comments.
* v1.1 of 24 Aug 2008 - Added Windows unit to enable inlining in Delphi 2006.
*
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* 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/MPL/
*
* 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.
*
* The Original Code is UHilitePasParser.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2005-2008 Peter
* Johnson. All Rights Reserved.
*
* ***** END LICENSE BLOCK *****
}
unit UHilitePasParser;
interface
uses
// Delphi
Classes,
// Project
IntfHiliter;
type
THilitePasParser = class;
{
TParseElementEvent:
Type of event raised when a code element has been parsed that needs to be
highlighted in a particular style. Should be handled by classes that render
the highlighted text.
@param Parser [in] Parser object that triggered event.
@param Element [in] Identifies highlight style the text should be rendered
in.
@param ElemText [in] Text to be rendered.
}
TParseElementEvent = procedure(Parser: THilitePasParser;
Element: THiliteElement; const ElemText: string) of object;
{
TParseLineEvent:
Type of event raised at start and end of lines of highlighted code when
parsing a Pascal file for highlighting.
@param Parser [in] Parser object that triggered event.
}
TParseLineEvent = procedure(Parser: THilitePasParser) of object;
{
THilitePasParser:
Parses pascal files and splits into different highlighting elements.
Notifies owning objects of each element and other key parsing events.
}
THilitePasParser = class(TObject)
private
fOnElement: TParseElementEvent;
{OnElement event handler}
fOnLineBegin: TParseLineEvent;
{OnLineBegin event handler}
fOnLineEnd: TParseLineEvent;
{OnLineEnd event handler}
protected
procedure DoElement(Elem: THiliteElement; const ElemText: string); virtual;
{Triggers OnElement event.
@param Elem [in] Element that has been parsed.
@param ElemText [in] Text of parsed element.
}
procedure DoLineBegin; virtual;
{Triggers OnLineBegin event.
}
procedure DoLineEnd; virtual;
{Triggers OnLineEnd event.
}
public
procedure Parse(const SrcStm: TStream);
{Parses the Pascal source on a stream, triggering events as each line and
token is parsed.
@param SrcStm [in] Stream containing Pascal source.
}
property OnElement: TParseElementEvent
read fOnElement write fOnElement;
{Event triggered when a highlight element has been parsed. Users should
render the element in response to this event}
property OnLineBegin: TParseLineEvent
read fOnLineBegin write fOnLineBegin;
{Event triggered just before first element on a new line is parsed. Users
should emit any output needed to open the new line}
property OnLineEnd: TParseLineEvent
read fOnLineEnd write fOnLineEnd;
{Event triggered at the end of each line of code. Users should close the
current line in response}
end;
implementation
uses
// Delphi
SysUtils, Windows {for inlining},
// Project
UHilitePasLexer;
{ THilitePasParser }
procedure THilitePasParser.DoElement(Elem: THiliteElement;
const ElemText: string);
{Triggers OnElement event.
@param Elem [in] Element that has been parsed.
@param ElemText [in] Text of parsed element.
}
begin
if Assigned(fOnElement) then
fOnElement(Self, Elem, ElemText);
end;
procedure THilitePasParser.DoLineBegin;
{Triggers OnLineBegin event.
}
begin
if Assigned(fOnLineBegin) then
fOnLineBegin(Self);
end;
procedure THilitePasParser.DoLineEnd;
{Triggers OnLineEnd event.
}
begin
if Assigned(fOnLineEnd) then
fOnLineEnd(Self);
end;
procedure THilitePasParser.Parse(const SrcStm: TStream);
{Parses the Pascal source on a stream, triggering events as each line and
token is parsed.
@param SrcStm [in] Stream containing Pascal source.
}
var
Lexer: THilitePasLexer; // object that tokenises Pascal source
Elem: THiliteElement; // identifies a parsed highlight element
InASM: Boolean; // flag true when parsing assembler code
BetweenLines: Boolean; // flag true after EOL before new line starts
begin
// Create lexical analyser that tokenises source code
Lexer := THilitePasLexer.Create(SrcStm);
try
// Intialise state: not in assembler and line not started
InASM := False;
BetweenLines := True;
// Read all tokens from source using analyser
while Lexer.NextToken <> tkEOF do
begin
// We treat end of line separately from other tokens
if Lexer.Token <> tkEOL then
begin
if InASM then
begin
// We are inside assembler code
// assume assembler style then handle special cases
Elem := heAssembler;
case Lexer.Token of
// we end assembler when an "end" keyword is encountered
tkKeyword:
begin
if AnsiSameText(Lexer.TokenStr, 'end') then
begin
InASM := False;
Elem := heReserved;
end;
end;
// handle tokens that are not formatted as assembler
// Note: we differ from Delphi editor here in that we don't format
// strings specially in assembler - they take on attributes of
// assembler code
tkWhiteSpace: Elem := heWhiteSpace;
tkComment: Elem := heComment;
tkCompilerDir: Elem := hePreProcessor;
tkError: Elem := heError;
end;
end
else
begin
// Normal case: in standard Pascal code
case Lexer.Token of
// Keywords are always reserved. We need to check for "asm" keyword
// and switch to assembler mode when found. "asm" keyword is
// rendered as reserved word.
tkKeyword:
begin
Elem := heReserved;
if AnsiSameText(Lexer.TokenStr, 'asm') then
InASM := True;
end;
// Map other tokens onto highlight elements
tkDirective: Elem := heReserved;
tkComment: Elem := heComment;
tkCompilerDir: Elem := hePreProcessor;
tkIdentifier: Elem := heIdentifier;
tkString,
tkChar: Elem := heString;
tkNumber: Elem := heNumber;
tkFloat: Elem := heFloat;
tkHex: Elem := heHex;
tkSymbol: Elem := heSymbol;
tkWhitespace: Elem := heWhitespace;
else Elem := heError;
end;
end;
// Emit the element
if BetweenLines then
begin
// we only start a new line when there's something to write on it
DoLineBegin;
BetweenLines := False;
end;
DoElement(Elem, Lexer.TokenStr);
end
else
begin
// End of line
if BetweenLines then
// we've never started this line: do it or we may skip blank line
DoLineBegin;
DoLineEnd;
// we are now between lines
BetweenLines := True;
end;
end;
finally
Lexer.Free;
end;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.