source: cprs/trunk/VA/HRParserPas.pas@ 1751

Last change on this file since 1751 was 829, checked in by Kevin Toppenberg, 14 years ago

Upgrade to version 27

File size: 6.4 KB
Line 
1// HRParserPas v1.0.1 (25.Sep.2000)
2// Fast Pascal source code parser.
3// by Colin A Ridgewell
4//
5// Copyright (C) 1999,2000 Hayden-R Ltd
6// http://www.haydenr.com
7//
8// This program is free software; you can redistribute it and/or modify it
9// under the terms of the GNU General Public License as published by the
10// Free Software Foundation; either version 2 of the License, or (at your
11// option) any later version.
12//
13// This program is distributed in the hope that it will be useful, but
14// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16// more details.
17//
18// You should have received a copy of the GNU General Public License along
19// with this program (gnu_license.htm); if not, write to the
20//
21// Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22//
23// To contact us via e-mail use the following addresses...
24//
25// bug@haydenr.u-net.com - to report a bug
26// support@haydenr.u-net.com - for general support
27// wishlist@haydenr.u-net.com - add new requirement to wish list
28//
29unit HRParserPas;
30
31interface
32
33uses
34 HRParser;
35
36const
37 {THRParserPas tokens.}
38 HR_TOKEN_PAS_COMMENT_BRACE_OPEN = 7; //e.g. {
39 HR_TOKEN_PAS_COMMENT_BRACE = 8; //e.g. { }
40 HR_TOKEN_PAS_COMMENT_BRACKET_OPEN = 9; //e.g. (*
41 HR_TOKEN_PAS_COMMENT_BRACKET = 10; //e.g. (* *)
42 HR_TOKEN_PAS_COMMENT_SLASH = 11; //e.g. //
43 HR_TOKEN_PAS_STRING_OPEN = 12; //e.g. '
44 HR_TOKEN_PAS_STRING = 13; //e.g. ' '
45 HR_TOKEN_PAS_EMBEDDEDCHAR = 14; //e.g. #10
46 HR_TOKEN_PAS_HEX = 15; //e.g. $A3
47
48 HR_TOKEN_PAS_DESC : Array[ 0..15 ] of PChar = (
49 'nil',
50 'eof',
51 'char',
52 'space',
53 'symbol',
54 'integer',
55 'float',
56 'comment brace open',
57 'comment brace',
58 'comment bracket open',
59 'comment bracket',
60 'comment slash',
61 'string open',
62 'string',
63 'embbeded char',
64 'hex');
65
66type
67 THRParserPas = class( THRParserText )
68 private
69 protected
70 procedure GetNextToken; override;
71 public
72 constructor Create; override;
73 destructor Destroy; override;
74 end;
75
76
77implementation
78
79
80{ T H R P a r s e r P a s }
81
82constructor THRParserPas.Create;
83begin
84 inherited Create;
85end;
86
87
88destructor THRParserPas.Destroy;
89begin
90 inherited Destroy;
91end;
92
93
94procedure THRParserPas.GetNextToken;
95begin
96 repeat
97
98 {comments} { } // - can go across multiple lines
99 if FSourceBuf[ FSourcePos ] = '{' then
100 begin
101 {move past open comment}
102 Inc( FSourcePos );
103 while True do
104 begin
105 case FSourceBuf[ FSourcePos ] of
106 #0 :
107 begin
108 FToken.TokenType := HR_TOKEN_PAS_COMMENT_BRACE_OPEN;
109 Break;{out of while}
110 end;
111 '}' :
112 begin
113 FToken.TokenType := HR_TOKEN_PAS_COMMENT_BRACE;
114 {move past close comment}
115 Inc( FSourcePos );
116 Break;{out of while}
117 end;
118 else
119 begin
120 FTokenBuf.Write( FSourceBuf[ FSourcePos ] );
121 if FSourceBuf[ FSourcePos ] = #10 then IncLine;
122 Inc( FSourcePos );
123 end;
124 end;
125 end;
126 Break;{out of repeat}
127 end;
128
129 {comments} (* *) // - can go across multiple lines
130 if ( FSourceBuf[ FSourcePos ] = '(' ) and ( FSourceBuf[ FSourcePos + 1 ] = '*' ) then
131 begin
132 {Move past open comment}
133 Inc( FSourcePos, 2 );
134 while True do
135 begin
136 if FSourceBuf[ FSourcePos ] = #0 then
137 begin
138 FToken.TokenType := HR_TOKEN_PAS_COMMENT_BRACKET_OPEN;
139 Break;{out of while}
140 end;
141 if ( ( FSourceBuf[ FSourcePos ] = '*' ) and ( FSourceBuf[ FSourcePos + 1 ] = ')' ) ) then
142 begin
143 FToken.TokenType := HR_TOKEN_PAS_COMMENT_BRACKET;
144 {move past close comment}
145 Inc( FSourcePos, 2 );
146 Break;{out of while}
147 end;
148 FTokenBuf.Write( FSourceBuf[ FSourcePos ] );
149 if FSourceBuf[ FSourcePos ] = #10 then IncLine;
150 Inc( FSourcePos );
151 end;
152 Break;{out of repeat}
153 end;
154
155 {comments} // - remainder of current line
156 if ( FSourceBuf[ FSourcePos ] = '/' ) and ( FSourceBuf[ FSourcePos + 1 ] = '/' ) then
157 begin
158 {move past open comment}
159 Inc( FSourcePos, 2 );
160 FToken.TokenType := HR_TOKEN_PAS_COMMENT_SLASH;
161 while FSourceBuf[ FSourcePos ] <> #13 do
162 begin
163 FTokenBuf.Write( FSourceBuf[ FSourcePos ] );
164 if FSourceBuf[ FSourcePos ] = #10 then IncLine;
165 Inc( FSourcePos );
166 end;
167 Break;{out of repeat}
168 end;
169
170 {quoted strings}
171 if FSourceBuf[ FSourcePos ] = '''' then
172 begin
173 {Move past open quote}
174 Inc( FSourcePos );
175 while True do
176 begin
177 case FSourceBuf[ FSourcePos ] of
178 #0, #10, #13 :
179 begin
180 FToken.TokenType := HR_TOKEN_PAS_STRING_OPEN;
181 Break;{out of while}
182 end;
183 '''' :
184 begin
185 FToken.TokenType := HR_TOKEN_PAS_STRING;
186 Break;{out of while}
187 end;
188 else
189 begin
190 FTokenBuf.Write( FSourceBuf[ FSourcePos ] );
191 Inc( FSourcePos );
192 end;
193 end;
194 end;
195 {move past close quote}
196 Inc( FSourcePos );
197 Break;{out of repeat}
198 end;
199
200 {embedded ascii eg #13}
201 if FSourceBuf[ FSourcePos ] = '#' then
202 begin
203 //FTokenBuf.Write(FSourceBuf[FSourcePos]);
204 Inc( FSourcePos );
205 while FSourceBuf[ FSourcePos ] in [ '0'..'9' ] do
206 begin
207 FTokenBuf.Write( FSourceBuf[ FSourcePos ] );
208 Inc( FSourcePos );
209 end;
210 FToken.TokenType := HR_TOKEN_PAS_EMBEDDEDCHAR;
211 Break;{out of repeat}
212 end;
213
214 {hex numbers}
215 if FSourceBuf[ FSourcePos ] = '$' then
216 begin
217 //FTokenBuf.Write(FSourceBuf[FSourcePos]);
218 Inc( FSourcePos );
219 while FSourceBuf[ FSourcePos ] in [ '0'..'9', 'A'..'F', 'a'..'f' ] do
220 begin
221 FTokenBuf.Write( FSourceBuf[ FSourcePos ] );
222 Inc( FSourcePos );
223 end;
224 FToken.TokenType := HR_TOKEN_PAS_HEX;
225 Break;{out of repeat}
226 end;
227
228 inherited GetNextToken;
229 {Break;}{out of repeat}
230
231 until( True );
232end;
233
234
235end.
Note: See TracBrowser for help on using the repository browser.