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 function expectStrOrId (allowEmpty
: Boolean=false): AnsiString;
80 procedure expectTT (ttype
: Integer);
81 function eatTT (ttype
: Integer): Boolean;
83 function expectDelim (const ch
: AnsiChar): AnsiChar;
84 function eatDelim (const ch
: AnsiChar): Boolean;
87 property col
: Integer read mCol
;
88 property line
: Integer read mLine
;
90 property curChar
: AnsiChar read mCurChar
;
91 property nextChar
: AnsiChar read mNextChar
;
94 property tokCol
: Integer read mTokCol
;
95 property tokLine
: Integer read mTokLine
;
97 property tokType
: Integer read mTokType
; // see TTXXX constants
98 property tokStr
: AnsiString read mTokStr
; // string or identifier
99 property tokChar
: AnsiChar read mTokChar
; // for delimiters
100 property tokInt
: Integer read mTokInt
;
104 // ////////////////////////////////////////////////////////////////////////// //
106 TFileTextParser
= class(TTextParser
)
108 const BufSize
= 16384;
112 mStreamOwned
: Boolean;
118 procedure loadNextChar (); override; // loads next char into mNextChar; #0 means 'eof'
121 constructor Create (const fname
: AnsiString);
122 constructor Create (st
: TStream
; astOwned
: Boolean=true); // will take ownership on st by default
123 destructor Destroy (); override;
126 TStrTextParser
= class(TTextParser
)
132 procedure loadNextChar (); override; // loads next char into mNextChar; #0 means 'eof'
135 constructor Create (const astr
: AnsiString);
136 destructor Destroy (); override;
140 // ////////////////////////////////////////////////////////////////////////// //
147 procedure putBuf (constref buf
; len
: SizeUInt
); virtual; abstract;
150 constructor Create ();
152 procedure flush (); virtual;
154 procedure put (const s
: AnsiString); overload
;
155 procedure put (v
: Byte); overload
;
156 procedure put (v
: Integer); overload
;
157 procedure put (const fmt
: AnsiString; args
: array of const); overload
;
158 procedure putIndent ();
160 procedure unindent ();
164 // ////////////////////////////////////////////////////////////////////////// //
166 TFileTextWriter
= class(TTextWriter
)
168 const BufSize
= 16384;
172 mStreamOwned
: Boolean;
177 procedure putBuf (constref buf
; len
: SizeUInt
); override;
180 constructor Create (const fname
: AnsiString);
181 constructor Create (ast
: TStream
; astOwned
: Boolean=true); // will own the stream by default
182 destructor Destroy (); override;
184 procedure flush (); override;
187 TStrTextWriter
= class(TTextWriter
)
192 procedure putBuf (constref buf
; len
: SizeUInt
); override;
195 constructor Create ();
196 destructor Destroy (); override;
198 property str
: AnsiString read mStr
;
208 // ////////////////////////////////////////////////////////////////////////// //
209 function StrEqu (const a
, b
: AnsiString): Boolean; inline; begin result
:= (a
= b
); end;
212 // ////////////////////////////////////////////////////////////////////////// //
213 constructor TTextParser
.Create ();
223 mAllowSignedNumbers
:= true;
224 warmup(); // change `mAllowSignedNumbers` there, if necessary
229 destructor TTextParser
.Destroy ();
235 function TTextParser
.isEOF (): Boolean; inline; begin result
:= (mCurChar
= #0); end;
238 procedure TTextParser
.warmup ();
242 mCurChar
:= mNextChar
;
243 if (mNextChar
<> #0) then loadNextChar();
247 function TTextParser
.skipChar (): Boolean;
249 if (mCurChar
= #0) then begin result
:= false; exit
; end;
250 if (mCurChar
= #10) then begin mCol
:= 1; Inc(mLine
); end else Inc(mCol
);
251 mCurChar
:= mNextChar
;
252 if (mCurChar
= #0) then begin result
:= false; exit
; end;
255 if (mCurChar
= #13) then
257 if (mNextChar
= #10) then loadNextChar();
264 function TTextParser
.skipBlanks (): Boolean;
270 if (curChar
= '/') then
272 // single-line comment
273 if (nextChar
= '/') then
275 while not isEOF
and (curChar
<> #10) do skipChar();
276 skipChar(); // skip EOL
280 if (nextChar
= '*') then
282 // skip comment start
287 if (curChar
= '*') and (nextChar
= '/') then
298 // nesting multline comment
299 if (nextChar
= '+') then
301 // skip comment start
307 if (curChar
= '+') and (nextChar
= '/') then
313 if (level
= 0) then break
;
316 if (curChar
= '/') and (nextChar
= '+') then
318 // skip comment start
329 if (curChar
> ' ') then break
;
330 skipChar(); // skip blank
336 function TTextParser
.skipToken (): Boolean;
338 procedure parseInt ();
340 neg
: Boolean = false;
344 if mAllowSignedNumbers
then
346 if (curChar
= '+') or (curChar
= '-') then
348 neg
:= (curChar
= '-');
350 if (curChar
< '0') or (curChar
> '9') then
353 if (neg
) then mTokChar
:= '-' else mTokChar
:= '+';
358 if (curChar
= '0') then
374 if (base
< 0) then base
:= 10;
375 if (digitInBase(curChar
, base
) < 0) then raise Exception
.Create('invalid number');
377 mTokInt
:= 0; // just in case
380 n
:= digitInBase(curChar
, base
);
381 if (n
< 0) then break
;
383 if (n
< 0) or (n
< mTokInt
) then raise Exception
.Create('integer overflow');
387 // check for valid number end
390 if (curChar
= '.') then raise Exception
.Create('floating numbers aren''t supported yet');
391 if (curChar
= '_') or ((curChar
>= 'A') and (curChar
<= 'Z')) or ((curChar
>= 'a') and (curChar
<= 'z')) or (curChar
>= #128) then
393 raise Exception
.Create('invalid number');
396 if neg
then mTokInt
:= -mTokInt
;
399 procedure parseString ();
405 mTokStr
:= ''; // just in case
407 skipChar(); // skip starting quote
411 if (qch
= '"') and (curChar
= '\') then
413 if (nextChar
= #0) then raise Exception
.Create('unterminated string escape');
415 // skip backslash and escape type
424 'x', 'X': // hex escape
426 n
:= digitInBase(curChar
, 16);
427 if (n
< 0) then raise Exception
.Create('invalid hexstr escape');
429 if (digitInBase(curChar
, 16) > 0) then
431 n
:= n
*16+digitInBase(curChar
, 16);
434 mTokStr
+= AnsiChar(n
);
440 // duplicate single quote (pascal style)
441 if (qch
= '''') and (curChar
= '''') and (nextChar
= '''') then
449 if (curChar
= qch
) then
451 skipChar(); // skip ending quote
459 procedure parseId ();
462 mTokStr
:= ''; // just in case
463 while (curChar
= '_') or ((curChar
>= '0') and (curChar
<= '9')) or
464 ((curChar
>= 'A') and (curChar
<= 'Z')) or
465 ((curChar
>= 'a') and (curChar
<= 'z')) or
479 if not skipBlanks() then
493 if mAllowSignedNumbers
and ((curChar
= '+') or (curChar
= '-')) then begin parseInt(); exit
; end;
494 if (curChar
>= '0') and (curChar
<= '9') then begin parseInt(); exit
; end;
497 if (curChar
= '"') or (curChar
= '''') then begin parseString(); exit
; end;
500 if (curChar
= '_') or ((curChar
>= 'A') and (curChar
<= 'Z')) or ((curChar
>= 'a') and (curChar
<= 'z')) or (curChar
>= #128) then begin parseId(); exit
; end;
504 ',': mTokType
:= TTComma
;
505 ':': mTokType
:= TTColon
;
506 ';': mTokType
:= TTSemi
;
507 '{': mTokType
:= TTBegin
;
508 '}': mTokType
:= TTEnd
;
509 else mTokType
:= TTDelim
;
516 function TTextParser
.expectId (): AnsiString;
518 if (mTokType
<> TTId
) then raise Exception
.Create('identifier expected');
524 procedure TTextParser
.expectId (const aid
: AnsiString);
526 if (mTokType
<> TTId
) or (not StrEqu(mTokStr
, aid
)) then raise Exception
.Create('identifier '''+aid
+''' expected');
531 function TTextParser
.eatId (const aid
: AnsiString): Boolean;
534 if (mTokType
<> TTId
) or (not StrEqu(mTokStr
, aid
)) then exit
;
540 function TTextParser
.expectStr (allowEmpty
: Boolean=false): AnsiString;
542 if (mTokType
<> TTStr
) then raise Exception
.Create('string expected');
543 if (not allowEmpty
) and (Length(mTokStr
) = 0) then raise Exception
.Create('non-empty string expected');
549 function TTextParser
.expectStrOrId (allowEmpty
: Boolean=false): AnsiString;
553 if (not allowEmpty
) and (Length(mTokStr
) = 0) then raise Exception
.Create('non-empty string expected');
557 raise Exception
.Create('string or identifier expected');
564 function TTextParser
.expectInt (): Integer;
566 if (mTokType
<> TTInt
) then raise Exception
.Create('string expected');
572 procedure TTextParser
.expectTT (ttype
: Integer);
574 if (mTokType
<> ttype
) then raise Exception
.Create('unexpected token');
579 function TTextParser
.eatTT (ttype
: Integer): Boolean;
581 result
:= (mTokType
= ttype
);
582 if result
then skipToken();
586 function TTextParser
.expectDelim (const ch
: AnsiChar): AnsiChar;
588 if (mTokType
<> TTDelim
) then raise Exception
.Create(Format('delimiter ''%s'' expected', [ch
]));
594 function TTextParser
.eatDelim (const ch
: AnsiChar): Boolean;
597 if (mTokType
<> TTDelim
) or (mTokChar
<> ch
) then exit
;
603 // ////////////////////////////////////////////////////////////////////////// //
604 constructor TFileTextParser
.Create (const fname
: AnsiString);
607 mFile
:= openDiskFileRO(fname
);
608 mStreamOwned
:= true;
609 GetMem(mBuffer
, BufSize
);
611 mBufLen
:= mFile
.Read(mBuffer
^, BufSize
);
612 if (mBufLen
< 0) then raise Exception
.Create('TFileTextParser: read error');
617 constructor TFileTextParser
.Create (st
: TStream
; astOwned
: Boolean=true);
619 if (st
= nil) then raise Exception
.Create('cannot create parser for nil stream');
621 mStreamOwned
:= astOwned
;
622 GetMem(mBuffer
, BufSize
);
624 mBufLen
:= mFile
.Read(mBuffer
^, BufSize
);
625 if (mBufLen
< 0) then raise Exception
.Create('TFileTextParser: read error');
630 destructor TFileTextParser
.Destroy ();
632 if (mBuffer
<> nil) then FreeMem(mBuffer
);
636 if mStreamOwned
then mFile
.Free();
642 procedure TFileTextParser
.loadNextChar ();
644 if (mBufLen
= 0) then begin mNextChar
:= #0; exit
; end;
645 if (mBufPos
>= mBufLen
) then
647 mBufLen
:= mFile
.Read(mBuffer
^, BufSize
);
648 if (mBufLen
< 0) then raise Exception
.Create('TFileTextParser: read error');
649 if (mBufLen
= 0) then begin mNextChar
:= #0; exit
; end;
652 assert(mBufPos
< mBufLen
);
653 mNextChar
:= mBuffer
[mBufPos
];
655 if (mNextChar
= #0) then mNextChar
:= ' ';
659 // ////////////////////////////////////////////////////////////////////////// //
660 constructor TStrTextParser
.Create (const astr
: AnsiString);
668 destructor TStrTextParser
.Destroy ();
675 procedure TStrTextParser
.loadNextChar ();
678 if (mPos
> Length(mStr
)) then exit
;
679 mNextChar
:= mStr
[mPos
]; Inc(mPos
);
680 if (mNextChar
= #0) then mNextChar
:= ' ';
684 // ////////////////////////////////////////////////////////////////////////// //
685 constructor TTextWriter
.Create (); begin mIndent
:= 0; end;
686 procedure TTextWriter
.flush (); begin end;
687 procedure TTextWriter
.put (const s
: AnsiString); overload
; begin if (Length(s
) > 0) then putBuf((@(s
[1]))^, Length(s
)); end;
688 procedure TTextWriter
.put (v
: Byte); overload
; begin put('%d', [v
]); end;
689 procedure TTextWriter
.put (v
: Integer); overload
; begin put('%d', [v
]); end;
690 procedure TTextWriter
.put (const fmt
: AnsiString; args
: array of const); overload
; begin put(formatstrf(fmt
, args
)); end;
691 procedure TTextWriter
.putIndent (); var f
: Integer; begin for f
:= 1 to mIndent
do put(' '); end;
692 procedure TTextWriter
.indent (); begin Inc(mIndent
, 2); end;
693 procedure TTextWriter
.unindent (); begin Dec(mIndent
, 2); end;
696 // ////////////////////////////////////////////////////////////////////////// //
697 constructor TFileTextWriter
.Create (const fname
: AnsiString);
699 mFile
:= createDiskFile(fname
);
700 mStreamOwned
:= true;
702 GetMem(mBuffer
, BufSize
);
703 assert(mBuffer
<> nil);
708 constructor TFileTextWriter
.Create (ast
: TStream
; astOwned
: Boolean=true);
710 if (ast
= nil) then raise Exception
.Create('cannot write to nil stream');
712 mStreamOwned
:= astOwned
;
714 GetMem(mBuffer
, BufSize
);
715 assert(mBuffer
<> nil);
719 destructor TFileTextWriter
.Destroy ();
722 if (mBuffer
<> nil) then FreeMem(mBuffer
);
725 if (mStreamOwned
) then mFile
.Free();
731 procedure TFileTextWriter
.flush ();
733 if (mFile
<> nil) and (mBufUsed
> 0) then
735 mFile
.WriteBuffer(mBuffer
^, mBufUsed
);
741 procedure TFileTextWriter
.putBuf (constref buf
; len
: SizeUInt
);
746 if (len
= 0) then exit
;
750 left
:= BufSize
-mBufUsed
;
754 left
:= BufSize
-mBufUsed
;
757 if (left
> len
) then left
:= Integer(len
);
758 Move(pc
^, (mBuffer
+mBufUsed
)^, left
);
766 // ////////////////////////////////////////////////////////////////////////// //
767 constructor TStrTextWriter
.Create ();
773 destructor TStrTextWriter
.Destroy ();
780 procedure TStrTextWriter
.putBuf (constref buf
; len
: SizeUInt
);
786 SetLength(st
, Integer(len
));
787 Move(buf
, PChar(st
)^, Integer(len
));