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 // ////////////////////////////////////////////////////////////////////////// //
27 TTextParser
= class(TPoolObject
)
34 //TTFloat = 3; // not yet
36 TTDelim
= 5; // one-char delimiters
41 TTGreatEqu
= 14; // >=
43 TTEqu
= 16; // == or <>
52 SignedNumbers
, // allow signed numbers; otherwise sign will be TTDelim
53 DollarIsId
, // allow dollar in identifiers; otherwise dollar will be TTDelim
54 DotIsId
// allow dot in identifiers; otherwise dot will be TTDelim
56 TOptions
= set of TOption
;
60 TAnsiCharSet
= set of AnsiChar;
64 mCurChar
, mNextChar
: AnsiChar;
68 mTokLine
, mTokCol
: Integer; // token start
70 mTokStr
: AnsiString; // string or identifier
71 mTokChar
: AnsiChar; // for delimiters
75 procedure warmup (); // called in constructor to warm up the system
76 procedure loadNextChar (); virtual; abstract; // loads next char into mNextChar; #0 means 'eof'
79 constructor Create (aopts
: TOptions
=[TOption
.SignedNumbers
]);
80 destructor Destroy (); override;
82 function isEOF (): Boolean; inline;
84 function skipChar (): Boolean; // returns `false` on eof
86 function skipBlanks (): Boolean; // ...and comments; returns `false` on eof
88 function skipToken (): Boolean; // returns `false` on eof
89 //function skipToken1 (): Boolean;
91 function expectId (): AnsiString;
92 procedure expectId (const aid
: AnsiString);
93 function eatId (const aid
: AnsiString): Boolean;
95 function expectStr (allowEmpty
: Boolean=false): AnsiString;
96 function expectInt (): Integer;
98 function expectStrOrId (allowEmpty
: Boolean=false): AnsiString;
100 procedure expectTT (ttype
: Integer);
101 function eatTT (ttype
: Integer): Boolean;
103 procedure expectDelim (const ch
: AnsiChar);
104 function expectDelims (const ch
: TAnsiCharSet
): AnsiChar;
105 function eatDelim (const ch
: AnsiChar): Boolean;
107 function isDelim (const ch
: AnsiChar): Boolean; inline;
110 property options
: TOptions read mOptions write mOptions
;
113 property col
: Integer read mCol
;
114 property line
: Integer read mLine
;
116 property curChar
: AnsiChar read mCurChar
;
117 property nextChar
: AnsiChar read mNextChar
;
120 property tokCol
: Integer read mTokCol
;
121 property tokLine
: Integer read mTokLine
;
123 property tokType
: Integer read mTokType
; // see TTXXX constants
124 property tokStr
: AnsiString read mTokStr
; // string or identifier
125 property tokChar
: AnsiChar read mTokChar
; // for delimiters
126 property tokInt
: Integer read mTokInt
;
130 // ////////////////////////////////////////////////////////////////////////// //
132 TFileTextParser
= class(TTextParser
)
134 const BufSize
= 16384;
138 mStreamOwned
: Boolean;
144 procedure loadNextChar (); override; // loads next char into mNextChar; #0 means 'eof'
147 constructor Create (const fname
: AnsiString; aopts
: TOptions
=[TOption
.SignedNumbers
]);
148 constructor Create (st
: TStream
; astOwned
: Boolean=true; aopts
: TOptions
=[TOption
.SignedNumbers
]);
149 destructor Destroy (); override;
152 TStrTextParser
= class(TTextParser
)
158 procedure loadNextChar (); override; // loads next char into mNextChar; #0 means 'eof'
161 constructor Create (const astr
: AnsiString; aopts
: TOptions
=[TOption
.SignedNumbers
]);
162 destructor Destroy (); override;
166 // ////////////////////////////////////////////////////////////////////////// //
173 procedure putBuf (constref buf
; len
: SizeUInt
); virtual; abstract;
176 constructor Create ();
178 procedure flush (); virtual;
180 procedure put (const s
: AnsiString); overload
;
181 procedure put (v
: Byte); overload
;
182 procedure put (v
: Integer); overload
;
183 procedure put (const fmt
: AnsiString; args
: array of const); overload
;
184 procedure putIndent ();
186 procedure unindent ();
189 property curIndent
: Integer read mIndent
;
193 // ////////////////////////////////////////////////////////////////////////// //
195 TFileTextWriter
= class(TTextWriter
)
197 const BufSize
= 16384;
201 mStreamOwned
: Boolean;
206 procedure putBuf (constref buf
; len
: SizeUInt
); override;
209 constructor Create (const fname
: AnsiString);
210 constructor Create (ast
: TStream
; astOwned
: Boolean=true); // will own the stream by default
211 destructor Destroy (); override;
213 procedure flush (); override;
216 TStrTextWriter
= class(TTextWriter
)
221 procedure putBuf (constref buf
; len
: SizeUInt
); override;
224 constructor Create ();
225 destructor Destroy (); override;
227 property str
: AnsiString read mStr
;
237 // ////////////////////////////////////////////////////////////////////////// //
238 constructor TTextParser
.Create (aopts
: TOptions
=[TOption
.SignedNumbers
]);
254 destructor TTextParser
.Destroy ();
260 function TTextParser
.isEOF (): Boolean; inline; begin result
:= (mCurChar
= #0); end;
263 procedure TTextParser
.warmup ();
267 mCurChar
:= mNextChar
;
268 if (mNextChar
<> #0) then loadNextChar();
272 function TTextParser
.skipChar (): Boolean;
274 if (mCurChar
= #0) then begin result
:= false; exit
; end;
275 if (mCurChar
= #10) then begin mCol
:= 1; Inc(mLine
); end else Inc(mCol
);
276 mCurChar
:= mNextChar
;
277 if (mCurChar
= #0) then begin result
:= false; exit
; end;
280 if (mCurChar
= #13) then
282 if (mNextChar
= #10) then loadNextChar();
289 function TTextParser
.skipBlanks (): Boolean;
295 if (curChar
= '/') then
297 // single-line comment
298 if (nextChar
= '/') then
300 while not isEOF
and (curChar
<> #10) do skipChar();
301 skipChar(); // skip EOL
305 if (nextChar
= '*') then
307 // skip comment start
312 if (curChar
= '*') and (nextChar
= '/') then
323 // nesting multline comment
324 if (nextChar
= '+') then
326 // skip comment start
332 if (curChar
= '+') and (nextChar
= '/') then
338 if (level
= 0) then break
;
341 if (curChar
= '/') and (nextChar
= '+') then
343 // skip comment start
354 else if (curChar
= '(') and (nextChar
= '*') then
356 // pascal comment; skip comment start
361 if (curChar
= '*') and (nextChar
= ')') then
372 if (curChar
> ' ') then break
;
373 skipChar(); // skip blank
380 function TTextParser.skipToken (): Boolean;
382 writeln('getting token...');
383 result := skipToken1();
384 writeln(' got token: ', mTokType, ' <', mTokStr, '> : <', mTokChar, '>');
389 function TTextParser
.skipToken (): Boolean;
391 procedure parseInt ();
393 neg
: Boolean = false;
397 if (TOption
.SignedNumbers
in mOptions
) then
399 if (curChar
= '+') or (curChar
= '-') then
401 neg
:= (curChar
= '-');
403 if (curChar
< '0') or (curChar
> '9') then
406 if (neg
) then mTokChar
:= '-' else mTokChar
:= '+';
411 if (curChar
= '0') then
427 if (base
< 0) then base
:= 10;
428 if (digitInBase(curChar
, base
) < 0) then raise Exception
.Create('invalid number');
430 mTokInt
:= 0; // just in case
433 n
:= digitInBase(curChar
, base
);
434 if (n
< 0) then break
;
436 if (n
< 0) or (n
< mTokInt
) then raise Exception
.Create('integer overflow');
440 // check for valid number end
443 if (curChar
= '.') then raise Exception
.Create('floating numbers aren''t supported yet');
444 if (curChar
= '_') or ((curChar
>= 'A') and (curChar
<= 'Z')) or ((curChar
>= 'a') and (curChar
<= 'z')) or (curChar
>= #128) then
446 raise Exception
.Create('invalid number');
449 if neg
then mTokInt
:= -mTokInt
;
452 procedure parseString ();
458 mTokStr
:= ''; // just in case
460 skipChar(); // skip starting quote
464 if (qch
= '"') and (curChar
= '\') then
466 if (nextChar
= #0) then raise Exception
.Create('unterminated string escape');
468 // skip backslash and escape type
477 'x', 'X': // hex escape
479 n
:= digitInBase(curChar
, 16);
480 if (n
< 0) then raise Exception
.Create('invalid hexstr escape');
482 if (digitInBase(curChar
, 16) > 0) then
484 n
:= n
*16+digitInBase(curChar
, 16);
487 mTokStr
+= AnsiChar(n
);
493 // duplicate single quote (pascal style)
494 if (qch
= '''') and (curChar
= '''') and (nextChar
= '''') then
502 if (curChar
= qch
) then
504 skipChar(); // skip ending quote
512 procedure parseId ();
515 mTokStr
:= ''; // just in case
516 while (curChar
= '_') or ((curChar
>= '0') and (curChar
<= '9')) or
517 ((curChar
>= 'A') and (curChar
<= 'Z')) or
518 ((curChar
>= 'a') and (curChar
<= 'z')) or
520 ((TOption
.DollarIsId
in mOptions
) and (curChar
= '$')) or
521 ((TOption
.DotIsId
in mOptions
) and (curChar
= '.') and (nextChar
<> '.')) do
534 if not skipBlanks() then
548 if (TOption
.SignedNumbers
in mOptions
) and ((curChar
= '+') or (curChar
= '-')) then begin parseInt(); exit
; end;
549 if (curChar
>= '0') and (curChar
<= '9') then begin parseInt(); exit
; end;
552 if (curChar
= '"') or (curChar
= '''') then begin parseString(); exit
; end;
555 if (curChar
= '_') or ((curChar
>= 'A') and (curChar
<= 'Z')) or ((curChar
>= 'a') and (curChar
<= 'z')) or (curChar
>= #128) then begin parseId(); exit
; end;
556 if (TOption
.DollarIsId
in mOptions
) and (curChar
= '$') then begin parseId(); exit
; end;
557 if (TOption
.DotIsId
in mOptions
) and (curChar
= '.') and (nextChar
<> '.') then begin parseId(); exit
; end;
563 if (curChar
= '=') then
566 '<': begin mTokType
:= TTLessEqu
; mTokStr
:= '<='; skipChar(); exit
; end;
567 '>': begin mTokType
:= TTGreatEqu
; mTokStr
:= '>='; skipChar(); exit
; end;
568 '!': begin mTokType
:= TTNotEqu
; mTokStr
:= '!='; skipChar(); exit
; end;
569 '=': begin mTokType
:= TTEqu
; mTokStr
:= '=='; skipChar(); exit
; end;
570 ':': begin mTokType
:= TTAss
; mTokStr
:= ':='; skipChar(); exit
; end;
573 else if (mTokChar
= curChar
) then
576 '<': begin mTokType
:= TTShl
; mTokStr
:= '<<'; skipChar(); exit
; end;
577 '>': begin mTokType
:= TTShr
; mTokStr
:= '>>'; skipChar(); exit
; end;
578 '&': begin mTokType
:= TTLogAnd
; mTokStr
:= '&&'; skipChar(); exit
; end;
579 '|': begin mTokType
:= TTLogOr
; mTokStr
:= '||'; skipChar(); exit
; end;
585 '<': if (curChar
= '>') then begin mTokType
:= TTNotEqu
; mTokStr
:= '<>'; skipChar(); exit
; end;
586 '.': if (curChar
= '.') then begin mTokType
:= TTDotDot
; mTokStr
:= '..'; skipChar(); exit
; end;
592 function TTextParser
.expectId (): AnsiString;
594 if (mTokType
<> TTId
) then raise Exception
.Create('identifier expected');
600 procedure TTextParser
.expectId (const aid
: AnsiString);
602 if (mTokType
<> TTId
) or (mTokStr
<> aid
) then raise Exception
.Create('identifier '''+aid
+''' expected');
607 function TTextParser
.eatId (const aid
: AnsiString): Boolean;
609 result
:= (mTokType
= TTId
) and (mTokStr
= aid
);
610 if result
then skipToken();
614 function TTextParser
.expectStr (allowEmpty
: Boolean=false): AnsiString;
616 if (mTokType
<> TTStr
) then raise Exception
.Create('string expected');
617 if (not allowEmpty
) and (Length(mTokStr
) = 0) then raise Exception
.Create('non-empty string expected');
623 function TTextParser
.expectStrOrId (allowEmpty
: Boolean=false): AnsiString;
627 if (not allowEmpty
) and (Length(mTokStr
) = 0) then raise Exception
.Create('non-empty string expected');
631 raise Exception
.Create('string or identifier expected');
638 function TTextParser
.expectInt (): Integer;
640 if (mTokType
<> TTInt
) then raise Exception
.Create('string expected');
646 procedure TTextParser
.expectTT (ttype
: Integer);
648 if (mTokType
<> ttype
) then raise Exception
.Create('unexpected token');
653 function TTextParser
.eatTT (ttype
: Integer): Boolean;
655 result
:= (mTokType
= ttype
);
656 if result
then skipToken();
660 procedure TTextParser
.expectDelim (const ch
: AnsiChar);
662 if (mTokType
<> TTDelim
) or (mTokChar
<> ch
) then raise Exception
.CreateFmt('delimiter ''%s'' expected', [ch
]);
667 function TTextParser
.expectDelims (const ch
: TAnsiCharSet
): AnsiChar;
669 if (mTokType
<> TTDelim
) then raise Exception
.Create('delimiter expected');
670 if not (mTokChar
in ch
) then raise Exception
.Create('delimiter expected');
676 function TTextParser
.eatDelim (const ch
: AnsiChar): Boolean;
678 result
:= (mTokType
= TTDelim
) and (mTokChar
= ch
);
679 if result
then skipToken();
683 function TTextParser
.isDelim (const ch
: AnsiChar): Boolean; inline;
685 result
:= (mTokType
= TTDelim
) and (mTokChar
= ch
);
689 // ////////////////////////////////////////////////////////////////////////// //
690 constructor TFileTextParser
.Create (const fname
: AnsiString; aopts
: TOptions
=[TOption
.SignedNumbers
]);
693 mFile
:= openDiskFileRO(fname
);
694 mStreamOwned
:= true;
695 GetMem(mBuffer
, BufSize
);
697 mBufLen
:= mFile
.Read(mBuffer
^, BufSize
);
698 if (mBufLen
< 0) then raise Exception
.Create('TFileTextParser: read error');
699 inherited Create(aopts
);
703 constructor TFileTextParser
.Create (st
: TStream
; astOwned
: Boolean=true; aopts
: TOptions
=[TOption
.SignedNumbers
]);
705 if (st
= nil) then raise Exception
.Create('cannot create parser for nil stream');
707 mStreamOwned
:= astOwned
;
708 GetMem(mBuffer
, BufSize
);
710 mBufLen
:= mFile
.Read(mBuffer
^, BufSize
);
711 if (mBufLen
< 0) then raise Exception
.Create('TFileTextParser: read error');
712 inherited Create(aopts
);
716 destructor TFileTextParser
.Destroy ();
718 if (mBuffer
<> nil) then FreeMem(mBuffer
);
722 if mStreamOwned
then mFile
.Free();
728 procedure TFileTextParser
.loadNextChar ();
730 if (mBufLen
= 0) then begin mNextChar
:= #0; exit
; end;
731 if (mBufPos
>= mBufLen
) then
733 mBufLen
:= mFile
.Read(mBuffer
^, BufSize
);
734 if (mBufLen
< 0) then raise Exception
.Create('TFileTextParser: read error');
735 if (mBufLen
= 0) then begin mNextChar
:= #0; exit
; end;
738 assert(mBufPos
< mBufLen
);
739 mNextChar
:= mBuffer
[mBufPos
];
741 if (mNextChar
= #0) then mNextChar
:= ' ';
745 // ////////////////////////////////////////////////////////////////////////// //
746 constructor TStrTextParser
.Create (const astr
: AnsiString; aopts
: TOptions
=[TOption
.SignedNumbers
]);
750 inherited Create(aopts
);
754 destructor TStrTextParser
.Destroy ();
761 procedure TStrTextParser
.loadNextChar ();
764 if (mPos
> Length(mStr
)) then exit
;
765 mNextChar
:= mStr
[mPos
]; Inc(mPos
);
766 if (mNextChar
= #0) then mNextChar
:= ' ';
770 // ////////////////////////////////////////////////////////////////////////// //
771 constructor TTextWriter
.Create (); begin mIndent
:= 0; end;
772 procedure TTextWriter
.flush (); begin end;
773 procedure TTextWriter
.put (const s
: AnsiString); overload
; begin if (Length(s
) > 0) then putBuf((@(s
[1]))^, Length(s
)); end;
774 procedure TTextWriter
.put (v
: Byte); overload
; begin put('%d', [v
]); end;
775 procedure TTextWriter
.put (v
: Integer); overload
; begin put('%d', [v
]); end;
776 procedure TTextWriter
.put (const fmt
: AnsiString; args
: array of const); overload
; begin put(formatstrf(fmt
, args
)); end;
777 procedure TTextWriter
.putIndent (); var f
: Integer; begin for f
:= 1 to mIndent
do put(' '); end;
778 procedure TTextWriter
.indent (); begin Inc(mIndent
, 2); end;
779 procedure TTextWriter
.unindent (); begin Dec(mIndent
, 2); end;
782 // ////////////////////////////////////////////////////////////////////////// //
783 constructor TFileTextWriter
.Create (const fname
: AnsiString);
785 mFile
:= createDiskFile(fname
);
786 mStreamOwned
:= true;
788 GetMem(mBuffer
, BufSize
);
789 assert(mBuffer
<> nil);
794 constructor TFileTextWriter
.Create (ast
: TStream
; astOwned
: Boolean=true);
796 if (ast
= nil) then raise Exception
.Create('cannot write to nil stream');
798 mStreamOwned
:= astOwned
;
800 GetMem(mBuffer
, BufSize
);
801 assert(mBuffer
<> nil);
805 destructor TFileTextWriter
.Destroy ();
808 if (mBuffer
<> nil) then FreeMem(mBuffer
);
811 if (mStreamOwned
) then mFile
.Free();
817 procedure TFileTextWriter
.flush ();
819 if (mFile
<> nil) and (mBufUsed
> 0) then
821 mFile
.WriteBuffer(mBuffer
^, mBufUsed
);
827 procedure TFileTextWriter
.putBuf (constref buf
; len
: SizeUInt
);
832 if (len
= 0) then exit
;
836 left
:= BufSize
-mBufUsed
;
840 left
:= BufSize
-mBufUsed
;
843 if (left
> len
) then left
:= Integer(len
);
844 Move(pc
^, (mBuffer
+mBufUsed
)^, left
);
852 // ////////////////////////////////////////////////////////////////////////// //
853 constructor TStrTextWriter
.Create ();
859 destructor TStrTextWriter
.Destroy ();
866 procedure TStrTextWriter
.putBuf (constref buf
; len
: SizeUInt
);
872 SetLength(st
, Integer(len
));
873 Move(buf
, PChar(st
)^, Integer(len
));