source: branches/Pascal Compiler 2/USyntaxHighlight.pas

Last change on this file was 16, checked in by george, 15 years ago
  • Přidáno: Další pokusný projekt překladače.
File size: 6.2 KB
Line 
1unit USyntaxHighlight;
2
3interface
4
5uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, StdCtrls, ComCtrls;
8
9
10type
11 TRuleIndex = (riNormal, riComment, riReservedWord, riNumber, riString,
12 riPreprocessor, riAssembler);
13
14 procedure ProcessHighlight(var RichEdit: TRichEdit);
15
16var
17 Rules: array[TRuleIndex] of TFont;
18
19implementation
20
21function IsAlfanumeric(Character: Char): Boolean;
22begin
23 Result := ((Ord(UpperCase(Character)[1]) >= Ord('A')) and (Ord(UpperCase(Character)[1]) <= Ord('Z')))
24 or ((Ord(Character) >= Ord('0')) and (Ord(Character) <= Ord('9')));
25end;
26
27procedure ProcessHighlight(var RichEdit: TRichEdit);
28type
29 TBlockState = (msNormalText, msString, msComment1, msComment2, msComment3,
30 msComment4, msComment5, msComment6);
31var
32 Lines: TStringList;
33 I: Integer;
34 II: Integer;
35 BlockState: TBlockState;
36// LastMachineState: TBlockState;
37 OneChar: Char;
38 LastPosition: TPoint;
39const
40 ReservedWordList: array[0..80] of string = ('asm', 'begin', 'end', 'procedure', 'function',
41 'program', 'while', 'do', 'type', 'var', 'const', 'property', 'object',
42 'class', 'constructor', 'destructor', 'virtual', 'abstract', 'override',
43 'overload', 'repeat', 'until', 'for', 'to', 'if', 'then', 'else', 'interface',
44 'unit', 'implementation', 'initialization', 'finalization', 'private',
45 'public', 'published', 'protected', 'absolute', 'default', 'finally', 'try',
46 'except', 'index', 'case', 'exports', 'and', 'or', 'xor', 'not', 'array',
47 'record', 'as', 'is', 'div', 'downto', 'goto', 'final', 'file', 'inherited',
48 'inline', 'label', 'library', 'mod', 'nil', 'of', 'out', 'in', 'packed',
49 'raise', 'resourcestring', 'sealed', 'set', 'shl', 'shr', 'static', 'string',
50 'threadvar', 'unsafe', 'uses', 'with', 'read', 'write');
51begin
52 Lines := TStringList.Create;
53 Lines.Assign(RichEdit.Lines);
54 LastPosition := RichEdit.CaretPos;
55 RichEdit.Lines.BeginUpdate;
56 RichEdit.Lines.Clear;
57 BlockState := msNormalText;
58 I := 1;
59 while (I < Length(Lines.Text)) do begin
60 case BlockState of
61 msNormalText: begin
62 II := 0;
63 while (II < Length(ReservedWordList)) do begin
64 if (ReservedWordList[II] = Copy(Lines.Text, I,
65 Length(ReservedWordList[II]))) and
66 (not IsAlfanumeric(Lines.Text[I + Length(ReservedWordList[II])])) and
67 (not IsAlfanumeric(Lines.Text[I - 1])) then begin
68 Break;
69 end;
70 Inc(II);
71 end;
72 if II < Length(ReservedWordList) then begin
73 RichEdit.SelAttributes.Assign(Rules[riReservedWord]);
74 RichEdit.SelText := ReservedWordList[II];
75 RichEdit.SelAttributes.Assign(Rules[riNormal]);
76 Inc(I, Length(ReservedWordList[II]));
77 end else begin
78 if (Lines.Text[I] = '{') then begin
79 RichEdit.SelAttributes.Assign(Rules[riComment]);
80 BlockState := msComment1;
81 RichEdit.SelText := Lines.Text[I];
82 Inc(I, 1);
83 end else
84 if (Copy(Lines.Text, I, 2) = '(*') then begin
85 RichEdit.SelAttributes.Assign(Rules[riComment]);
86 BlockState := msComment2;
87 RichEdit.SelText := '(*';
88 Inc(I, 2);
89 end else
90 if (Copy(Lines.Text, I, 2) = '//') then begin
91 RichEdit.SelAttributes.Assign(Rules[riComment]);
92 BlockState := msComment3;
93 RichEdit.SelText := '//';
94 Inc(I, 2);
95 end else
96 if Lines.Text[I] = '''' then begin
97 RichEdit.SelAttributes.Assign(Rules[riString]);
98 BlockState := msString;
99 RichEdit.SelText := Lines.Text[I];
100 Inc(I, 1);
101 end else begin
102 RichEdit.SelText := Lines.Text[I];
103 Inc(I);
104 end;
105 end;
106 end;
107 msString: begin
108 if Lines.Text[I] = '''' then begin
109 RichEdit.SelText := Lines.Text[I];
110 RichEdit.SelAttributes.Assign(Rules[riNormal]);
111 Inc(I, 1);
112 BlockState := msNormalText;
113 end else begin
114 RichEdit.SelText := Lines.Text[I];
115 Inc(I, 1);
116 end;
117 end;
118 msComment1: begin
119 if Lines.Text[I] = '}' then begin
120 RichEdit.SelText := '}';
121 Inc(I, 1);
122 RichEdit.SelAttributes.Assign(Rules[riNormal]);
123 BlockState := msNormalText;
124 end else begin
125 RichEdit.SelText := Lines.Text[I];
126 Inc(I, 1);
127 end;
128 end;
129 msComment2: begin
130 if (Copy(Lines.Text, I, 2) = '*)') then begin
131 BlockState := msNormalText;
132 RichEdit.SelText := '*)';
133 Inc(I, 2);
134 RichEdit.SelAttributes.Assign(Rules[riNormal]);
135 end else begin
136 RichEdit.SelText := Lines.Text[I];
137 Inc(I, 1);
138 end;
139 end;
140 msComment3: begin
141 if (Lines.Text[I] = #10) or (Lines.Text[I] = #13) then begin
142 RichEdit.SelText := Lines.Text[I];
143 RichEdit.SelAttributes.Assign(Rules[riNormal]);
144 BlockState := msNormalText;
145 Inc(I, 2);
146 end else begin
147 RichEdit.SelText := Lines.Text[I];
148 Inc(I, 1);
149 end;
150 end;
151 end;
152 end;
153 RichEdit.Lines.EndUpdate;
154 RichEdit.CaretPos := LastPosition;
155 Lines.Free;
156end;
157
158initialization
159
160Rules[riNormal] := TFont.Create;
161with Rules[riNormal] do begin
162 Name := 'Courier New';
163 Size := 10;
164end;
165Rules[riComment] := TFont.Create;
166with Rules[riComment] do begin
167 Assign(Rules[riNormal]);
168 Color := clBlue;
169 Style := [fsItalic];
170end;
171Rules[riString] := TFont.Create;
172with Rules[riString] do begin
173 Assign(Rules[riNormal]);
174 Color := clGreen;
175end;
176Rules[riNumber] := TFont.Create;
177with Rules[riNumber] do begin
178 Assign(Rules[riNormal]);
179end;
180Rules[riAssembler] := TFont.Create;
181with Rules[riAssembler] do begin
182 Assign(Rules[riNormal]);
183 Color := clRed;
184end;
185Rules[riPreprocessor] := TFont.Create;
186with Rules[riPreprocessor] do begin
187 Assign(Rules[riNormal]);
188 Style := [fsItalic];
189end;
190Rules[riReservedWord] := TFont.Create;
191with Rules[riReservedWord] do begin
192 Assign(Rules[riNormal]);
193 Style := [fsBold];
194end;
195
196end.
Note: See TracBrowser for help on using the repository browser.