1 (* Copyright (C) DooM 2D:Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 {$INCLUDE a_modes.inc}
25 // ////////////////////////////////////////////////////////////////////////// //
34 //TTFloat = 3; // not yet
39 TTBegin
= 8; // left curly
40 TTEnd
= 9; // right curly
41 TTDelim
= 10; // other delimiters
45 mCurChar
, mNextChar
: AnsiChar;
47 mAllowSignedNumbers
: Boolean; // internal control
49 mTokLine
, mTokCol
: Integer; // token start
51 mTokStr
: AnsiString; // string or identifier
52 mTokChar
: AnsiChar; // for delimiters
56 procedure warmup (); virtual; // called in constructor to warm up the system
57 procedure loadNextChar (); virtual; abstract; // loads next char into mNextChar; #0 means 'eof'
60 constructor Create ();
61 destructor Destroy (); override;
63 function isEOF (): Boolean; inline;
65 function skipChar (): Boolean; // returns `false` on eof
67 function skipBlanks (): Boolean; // ...and comments; returns `false` on eof
69 function skipToken (): Boolean; // returns `false` on eof
71 function expectId (): AnsiString;
72 procedure expectId (const aid
: AnsiString);
73 function eatId (const aid
: AnsiString): Boolean;
75 function expectStr (allowEmpty
: Boolean=false): AnsiString;
76 function expectInt (): Integer;
78 procedure expectTT (ttype
: Integer);
79 function eatTT (ttype
: Integer): Boolean;
81 function expectDelim (const ch
: AnsiChar): AnsiChar;
82 function eatDelim (const ch
: AnsiChar): Boolean;
85 property col
: Integer read mCol
;
86 property line
: Integer read mLine
;
88 property curChar
: AnsiChar read mCurChar
;
89 property nextChar
: AnsiChar read mNextChar
;
92 property tokCol
: Integer read mTokCol
;
93 property tokLine
: Integer read mTokLine
;
95 property tokType
: Integer read mTokType
; // see TTXXX constants
96 property tokStr
: AnsiString read mTokStr
; // string or identifier
97 property tokChar
: AnsiChar read mTokChar
; // for delimiters
98 property tokInt
: Integer read mTokInt
;
102 // ////////////////////////////////////////////////////////////////////////// //
104 TFileTextParser
= class(TTextParser
)
106 const BufSize
= 16384;
110 mStreamOwned
: Boolean;
116 procedure loadNextChar (); override; // loads next char into mNextChar; #0 means 'eof'
119 constructor Create (const fname
: AnsiString);
120 constructor Create (st
: TStream
; astOwned
: Boolean=true); // will take ownership on st by default
121 destructor Destroy (); override;
124 TStrTextParser
= class(TTextParser
)
130 procedure loadNextChar (); override; // loads next char into mNextChar; #0 means 'eof'
133 constructor Create (const astr
: AnsiString);
134 destructor Destroy (); override;
138 // ////////////////////////////////////////////////////////////////////////// //
145 procedure putBuf (constref buf
; len
: SizeUInt
); virtual; abstract;
148 constructor Create ();
150 procedure flush (); virtual;
152 procedure put (const s
: AnsiString); overload
;
153 procedure put (v
: Byte); overload
;
154 procedure put (v
: Integer); overload
;
155 procedure put (const fmt
: AnsiString; args
: array of const); overload
;
156 procedure putIndent ();
158 procedure unindent ();
162 // ////////////////////////////////////////////////////////////////////////// //
164 TFileTextWriter
= class(TTextWriter
)
166 const BufSize
= 16384;
170 mStreamOwned
: Boolean;
175 procedure putBuf (constref buf
; len
: SizeUInt
); override;
178 constructor Create (const fname
: AnsiString);
179 constructor Create (ast
: TStream
; astOwned
: Boolean=true); // will own the stream by default
180 destructor Destroy (); override;
182 procedure flush (); override;
192 // ////////////////////////////////////////////////////////////////////////// //
193 function StrEqu (const a
, b
: AnsiString): Boolean; inline; begin result
:= (a
= b
); end;
196 // ////////////////////////////////////////////////////////////////////////// //
197 constructor TTextParser
.Create ();
207 mAllowSignedNumbers
:= true;
208 warmup(); // change `mAllowSignedNumbers` there, if necessary
213 destructor TTextParser
.Destroy ();
219 function TTextParser
.isEOF (): Boolean; inline; begin result
:= (mCurChar
= #0); end;
222 procedure TTextParser
.warmup ();
226 mCurChar
:= mNextChar
;
227 if (mNextChar
<> #0) then loadNextChar();
231 function TTextParser
.skipChar (): Boolean;
233 if (mCurChar
= #0) then begin result
:= false; exit
; end;
234 if (mCurChar
= #10) then begin mCol
:= 1; Inc(mLine
); end else Inc(mCol
);
235 mCurChar
:= mNextChar
;
236 if (mCurChar
= #0) then begin result
:= false; exit
; end;
239 if (mCurChar
= #13) then
241 if (mNextChar
= #10) then loadNextChar();
248 function TTextParser
.skipBlanks (): Boolean;
254 if (curChar
= '/') then
256 // single-line comment
257 if (nextChar
= '/') then
259 while not isEOF
and (curChar
<> #10) do skipChar();
260 skipChar(); // skip EOL
264 if (nextChar
= '*') then
266 // skip comment start
271 if (curChar
= '*') and (nextChar
= '/') then
282 // nesting multline comment
283 if (nextChar
= '+') then
285 // skip comment start
291 if (curChar
= '+') and (nextChar
= '/') then
297 if (level
= 0) then break
;
300 if (curChar
= '/') and (nextChar
= '+') then
302 // skip comment start
313 if (curChar
> ' ') then break
;
314 skipChar(); // skip blank
320 function TTextParser
.skipToken (): Boolean;
322 procedure parseInt ();
324 neg
: Boolean = false;
328 if mAllowSignedNumbers
then
330 if (curChar
= '+') or (curChar
= '-') then
332 neg
:= (curChar
= '-');
334 if (curChar
< '0') or (curChar
> '9') then
337 if (neg
) then mTokChar
:= '-' else mTokChar
:= '+';
342 if (curChar
= '0') then
358 if (base
< 0) then base
:= 10;
359 if (digitInBase(curChar
, base
) < 0) then raise Exception
.Create('invalid number');
361 mTokInt
:= 0; // just in case
364 n
:= digitInBase(curChar
, base
);
365 if (n
< 0) then break
;
367 if (n
< 0) or (n
< mTokInt
) then raise Exception
.Create('integer overflow');
371 // check for valid number end
374 if (curChar
= '.') then raise Exception
.Create('floating numbers aren''t supported yet');
375 if (curChar
= '_') or ((curChar
>= 'A') and (curChar
<= 'Z')) or ((curChar
>= 'a') and (curChar
<= 'z')) or (curChar
>= #128) then
377 raise Exception
.Create('invalid number');
380 if neg
then mTokInt
:= -mTokInt
;
383 procedure parseString ();
389 mTokStr
:= ''; // just in case
391 skipChar(); // skip starting quote
395 if (qch
= '"') and (curChar
= '\') then
397 if (nextChar
= #0) then raise Exception
.Create('unterminated string escape');
399 // skip backslash and escape type
408 'x', 'X': // hex escape
410 n
:= digitInBase(curChar
, 16);
411 if (n
< 0) then raise Exception
.Create('invalid hexstr escape');
413 if (digitInBase(curChar
, 16) > 0) then
415 n
:= n
*16+digitInBase(curChar
, 16);
418 mTokStr
+= AnsiChar(n
);
424 // duplicate single quote (pascal style)
425 if (qch
= '''') and (curChar
= '''') and (nextChar
= '''') then
433 if (curChar
= qch
) then
435 skipChar(); // skip ending quote
443 procedure parseId ();
446 mTokStr
:= ''; // just in case
447 while (curChar
= '_') or ((curChar
>= '0') and (curChar
<= '9')) or
448 ((curChar
>= 'A') and (curChar
<= 'Z')) or
449 ((curChar
>= 'a') and (curChar
<= 'z')) or
463 if not skipBlanks() then
477 if mAllowSignedNumbers
and ((curChar
= '+') or (curChar
= '-')) then begin parseInt(); exit
; end;
478 if (curChar
>= '0') and (curChar
<= '9') then begin parseInt(); exit
; end;
481 if (curChar
= '"') or (curChar
= '''') then begin parseString(); exit
; end;
484 if (curChar
= '_') or ((curChar
>= 'A') and (curChar
<= 'Z')) or ((curChar
>= 'a') and (curChar
<= 'z')) or (curChar
>= #128) then begin parseId(); exit
; end;
488 ',': mTokType
:= TTComma
;
489 ':': mTokType
:= TTColon
;
490 ';': mTokType
:= TTSemi
;
491 '{': mTokType
:= TTBegin
;
492 '}': mTokType
:= TTEnd
;
493 else mTokType
:= TTDelim
;
500 function TTextParser
.expectId (): AnsiString;
502 if (mTokType
<> TTId
) then raise Exception
.Create('identifier expected');
508 procedure TTextParser
.expectId (const aid
: AnsiString);
510 if (mTokType
<> TTId
) or (not StrEqu(mTokStr
, aid
)) then raise Exception
.Create('identifier '''+aid
+''' expected');
515 function TTextParser
.eatId (const aid
: AnsiString): Boolean;
518 if (mTokType
<> TTId
) or (not StrEqu(mTokStr
, aid
)) then exit
;
524 function TTextParser
.expectStr (allowEmpty
: Boolean=false): AnsiString;
526 if (mTokType
<> TTStr
) then raise Exception
.Create('string expected');
527 if (not allowEmpty
) and (Length(mTokStr
) = 0) then raise Exception
.Create('non-empty string expected');
533 function TTextParser
.expectInt (): Integer;
535 if (mTokType
<> TTInt
) then raise Exception
.Create('string expected');
541 procedure TTextParser
.expectTT (ttype
: Integer);
543 if (mTokType
<> ttype
) then raise Exception
.Create('unexpected token');
548 function TTextParser
.eatTT (ttype
: Integer): Boolean;
550 result
:= (mTokType
= ttype
);
551 if result
then skipToken();
555 function TTextParser
.expectDelim (const ch
: AnsiChar): AnsiChar;
557 if (mTokType
<> TTDelim
) then raise Exception
.Create(Format('delimiter ''%s'' expected', [ch
]));
563 function TTextParser
.eatDelim (const ch
: AnsiChar): Boolean;
566 if (mTokType
<> TTDelim
) or (mTokChar
<> ch
) then exit
;
572 // ////////////////////////////////////////////////////////////////////////// //
573 constructor TFileTextParser
.Create (const fname
: AnsiString);
576 mFile
:= openDiskFileRO(fname
);
577 mStreamOwned
:= true;
578 GetMem(mBuffer
, BufSize
);
580 mBufLen
:= mFile
.Read(mBuffer
^, BufSize
);
581 if (mBufLen
< 0) then raise Exception
.Create('TFileTextParser: read error');
586 constructor TFileTextParser
.Create (st
: TStream
; astOwned
: Boolean=true);
588 if (st
= nil) then raise Exception
.Create('cannot create parser for nil stream');
590 mStreamOwned
:= astOwned
;
591 GetMem(mBuffer
, BufSize
);
593 mBufLen
:= mFile
.Read(mBuffer
^, BufSize
);
594 if (mBufLen
< 0) then raise Exception
.Create('TFileTextParser: read error');
599 destructor TFileTextParser
.Destroy ();
601 if (mBuffer
<> nil) then FreeMem(mBuffer
);
605 if mStreamOwned
then mFile
.Free();
611 procedure TFileTextParser
.loadNextChar ();
613 if (mBufLen
= 0) then begin mNextChar
:= #0; exit
; end;
614 if (mBufPos
>= mBufLen
) then
616 mBufLen
:= mFile
.Read(mBuffer
^, BufSize
);
617 if (mBufLen
< 0) then raise Exception
.Create('TFileTextParser: read error');
618 if (mBufLen
= 0) then begin mNextChar
:= #0; exit
; end;
621 assert(mBufPos
< mBufLen
);
622 mNextChar
:= mBuffer
[mBufPos
];
624 if (mNextChar
= #0) then mNextChar
:= ' ';
628 // ////////////////////////////////////////////////////////////////////////// //
629 constructor TStrTextParser
.Create (const astr
: AnsiString);
637 destructor TStrTextParser
.Destroy ();
644 procedure TStrTextParser
.loadNextChar ();
647 if (mPos
> Length(mStr
)) then exit
;
648 mNextChar
:= mStr
[mPos
]; Inc(mPos
);
649 if (mNextChar
= #0) then mNextChar
:= ' ';
653 // ////////////////////////////////////////////////////////////////////////// //
654 constructor TTextWriter
.Create (); begin mIndent
:= 0; end;
655 procedure TTextWriter
.flush (); begin end;
656 procedure TTextWriter
.put (const s
: AnsiString); overload
; begin if (Length(s
) > 0) then putBuf((@(s
[1]))^, Length(s
)); end;
657 procedure TTextWriter
.put (v
: Byte); overload
; begin put('%d', [v
]); end;
658 procedure TTextWriter
.put (v
: Integer); overload
; begin put('%d', [v
]); end;
659 procedure TTextWriter
.put (const fmt
: AnsiString; args
: array of const); overload
; begin put(formatstrf(fmt
, args
)); end;
660 procedure TTextWriter
.putIndent (); var f
: Integer; begin for f
:= 1 to mIndent
do put(' '); end;
661 procedure TTextWriter
.indent (); begin Inc(mIndent
, 2); end;
662 procedure TTextWriter
.unindent (); begin Dec(mIndent
, 2); end;
665 // ////////////////////////////////////////////////////////////////////////// //
666 constructor TFileTextWriter
.Create (const fname
: AnsiString);
668 mFile
:= createDiskFile(fname
);
669 mStreamOwned
:= true;
671 GetMem(mBuffer
, BufSize
);
672 assert(mBuffer
<> nil);
677 constructor TFileTextWriter
.Create (ast
: TStream
; astOwned
: Boolean=true);
679 if (ast
= nil) then raise Exception
.Create('cannot write to nil stream');
681 mStreamOwned
:= astOwned
;
683 GetMem(mBuffer
, BufSize
);
684 assert(mBuffer
<> nil);
688 destructor TFileTextWriter
.Destroy ();
691 if (mBuffer
<> nil) then FreeMem(mBuffer
);
694 if (mStreamOwned
) then mFile
.Free();
700 procedure TFileTextWriter
.flush ();
702 if (mFile
<> nil) and (mBufUsed
> 0) then
704 mFile
.WriteBuffer(mBuffer
^, mBufUsed
);
710 procedure TFileTextWriter
.putBuf (constref buf
; len
: SizeUInt
);
715 if (len
= 0) then exit
;
719 left
:= BufSize
-mBufUsed
;
723 left
:= BufSize
-mBufUsed
;
726 if (left
> len
) then left
:= Integer(len
);
727 Move(pc
^, (mBuffer
+mBufUsed
)^, left
);