Changeset 76 for branches/Transpascal/Compiler/Analyze/UParser.pas
- Timestamp:
- Oct 21, 2010, 1:20:57 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Transpascal/Compiler/Analyze/UParser.pas
r74 r76 10 10 11 11 type 12 TOnErrorMessage = procedure(Text: string; Position: TPoint; FileName: string) of object; 12 TErrorMessageEvent = procedure(Text: string; Position: TPoint; FileName: string) of object; 13 TDebugLogEvent = procedure(Text: string) of object; 13 14 14 15 TParserState = (psNone, psIdentifier, psConstantNumber, psConstantString, 15 16 psOperator, psEndOfFile, psLineComment, psBlockComment1, psBlockComment2, 16 psUnknown, psWhiteSpace, psConstantStringEnd); 17 psUnknown, psWhiteSpace, psConstantStringEnd, psBlockComment1First, 18 psCompilerDirective, psNoneShift, psConstantHexNumber); 17 19 18 20 TTokenType = (ttNone, ttIdentifier, ttConstantNumber, ttConstantString, 19 21 ttOperator, ttEndOfFile, ttLineComment, ttBlockComment1, ttBlockComment2, 20 ttUnknown, ttWhiteSpace );22 ttUnknown, ttWhiteSpace, ttCompilerDirective); 21 23 22 24 TToken = class … … 31 33 private 32 34 FFileName: string; 33 FOnErrorMessage: TOnErrorMessage; 35 FOnDebugLog: TDebugLogEvent; 36 FOnErrorMessage: TErrorMessageEvent; 34 37 FNextToken: string; 35 38 FNextTokenType: TTokenType; … … 49 52 destructor Destroy; override; 50 53 function IsAlphanumeric(Character: char): boolean; 54 function IsNumeric(Character: char): boolean; 55 function IsHex(Character: char): boolean; 51 56 function IsWhiteSpace(Character: char): boolean; 52 57 function IsAlphabetic(Character: char): boolean; … … 60 65 procedure ErrorMessage(const Text: string; const Arguments: array of const; 61 66 TokenOffset: Integer); 62 property OnErrorMessage: TOnErrorMessage read FOnErrorMessage write FOnErrorMessage; 67 property OnErrorMessage: TErrorMessageEvent read FOnErrorMessage write FOnErrorMessage; 68 property OnDebugLog: TDebugLogEvent read FOnDebugLog write FOnDebugLog; 63 69 procedure Process; 70 procedure Log(Text: string); 64 71 property FileName: string read FFileName write FFileName; 65 72 end; … … 84 91 procedure TBaseParser.Expect(Code: string); 85 92 begin 93 Log('Expect: ' + Code); 86 94 if NextToken <> Code then begin 87 95 ErrorMessage(SExpectedButFound, [Code, NextToken], 0); … … 112 120 function TBaseParser.IsAlphanumeric(Character: char): boolean; 113 121 begin 114 Result := IsAlphabetic(Character) or (Character in ['0'..'9']); 122 Result := IsAlphabetic(Character) or IsNumeric(Character); 123 end; 124 125 function TBaseParser.IsNumeric(Character: char): boolean; 126 begin 127 Result := Character in ['0'..'9']; 128 end; 129 130 function TBaseParser.IsHex(Character: char): boolean; 131 begin 132 Result := IsNumeric(Character) or (Character in ['A'..'F']); 115 133 end; 116 134 … … 179 197 end; 180 198 199 procedure TBaseParser.Log(Text: string); 200 begin 201 if Assigned(FOnDebugLog) then 202 FOnDebugLog(Text); 203 end; 204 181 205 procedure TBaseParser.GetNextToken; 182 206 var … … 204 228 end; 205 229 206 if FParserState = psNonethen begin230 if (FParserState = psNone) or (FParserState = psNoneShift) then begin 207 231 TokenCodePosition := CodePosition; 208 232 if IsWhiteSpace(CurrentChar) then … … 210 234 else 211 235 if CurrentChar = '{' then begin 212 FParserState := psBlockComment1 ;236 FParserState := psBlockComment1First; 213 237 end else 214 238 if CurrentChar = '''' then begin 215 239 FParserState := psConstantString; 240 end else 241 if CurrentChar = '$' then begin 242 FParserState := psConstantHexNumber; 216 243 end else 217 244 if CurrentChar in SpecChar then begin … … 219 246 FNextToken := FNextToken + CurrentChar; 220 247 end else 221 if IsAlpha numeric(CurrentChar) then begin248 if IsAlphabetic(CurrentChar) then begin 222 249 FParserState := psIdentifier; 223 250 FNextToken := FNextToken + CurrentChar; 251 end else 252 if IsNumeric(CurrentChar) then begin 253 FPArserSTate := psConstantNumber; 254 FNextToken := FNextToken + CurrentChar; 224 255 end else FParserState := psUnknown; 225 256 end else 226 257 if FParserState = psLineComment then begin 227 258 if (CurrentChar = #13) or (CurrentChar = #10) then 259 FParserState := psNoneShift; 260 end else 261 if FParserState = psBlockComment1First then begin 262 if CurrentChar = '$' then FParserState := psCompilerDirective 263 else FParserSTate := psBlockComment1; 264 end else 265 if FParserState = psBlockComment1 then begin 266 if (CurrentChar = '}') then begin 267 FParserState := psNoneShift; 268 end; 269 end else 270 if FParserState = psCompilerDirective then begin 271 if (CurrentChar = '}') then begin 228 272 FParserState := psNone; 229 end else 230 if FParserState = psBlockComment1 then begin 231 if (CurrentChar = '}') then 232 FParserState := psNone; 273 FNextTokenType := ttCompilerDirective; 274 Break; 275 end; 233 276 end else 234 277 if FParserState = psBlockComment2 then begin 235 278 if (PreviousChar = '*') and (CurrentChar = ')') then 236 FParserState := psNone ;279 FParserState := psNoneShift; 237 280 end else 238 281 if FParserState = psConstantString then … … 249 292 FNextTokenType := ttConstantString; 250 293 Break; 294 end else 295 if FParserState = psConstantHexNumber then 296 begin 297 if not IsHex(CurrentChar) then begin 298 FParserState := psNone; 299 FNextTokenType := ttConstantNumber; 300 Break; 301 end else FNextToken := FNextToken + CurrentChar; 302 end else 303 if FParserState = psConstantNumber then 304 begin 305 if not IsNumeric(CurrentChar) then begin 306 FParserState := psNone; 307 FNextTokenType := ttConstantNumber; 308 Break; 309 end else FNextToken := FNextToken + CurrentChar; 251 310 end else 252 311 if FParserState = psOperator then … … 281 340 if FParserState = psIdentifier then 282 341 begin 283 if not IsAlphanumeric(CurrentChar) then begin342 if (not IsAlphanumeric(CurrentChar)) and (CurrentChar <> '_') then begin 284 343 FNextTokenType := ttIdentifier; 285 344 Break; … … 311 370 Inc(TokenIndex); 312 371 end else Result := ''; 372 Log('ReadCode: ' + Result); 313 373 end; 314 374 … … 318 378 Result := TToken(Tokens[TokenIndex]).Token; 319 379 end else Result := ''; 380 Log('NextToken: ' + Result); 320 381 end; 321 382
Note:
See TracChangeset
for help on using the changeset viewer.