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}
17 {.$DEFINE XPARSER_DEBUG}
23 SysUtils
, Classes
{$IFDEF USE_MEMPOOL}, mempool
{$ENDIF};
26 // ////////////////////////////////////////////////////////////////////////// //
30 TParserException
= class(Exception
)
32 tokLine
, tokCol
: Integer;
35 constructor Create (pr
: TTextParser
; const amsg
: AnsiString);
36 constructor CreateFmt (pr
: TTextParser
; const afmt
: AnsiString; const args
: array of const);
39 TTextParser
= class{$IFDEF USE_MEMPOOL}(TPoolObject
){$ENDIF}
46 //TTFloat = 3; // not yet
48 TTDelim
= 5; // one-char delimiters
53 TTGreatEqu
= 14; // >=
55 TTEqu
= 16; // == or <>
64 SignedNumbers
, // allow signed numbers; otherwise sign will be TTDelim
65 DollarIsId
, // allow dollar in identifiers; otherwise dollar will be TTDelim
66 DotIsId
, // allow dot in identifiers; otherwise dot will be TTDelim
67 PascalComments
// allow `{}` pascal comments
69 TOptions
= set of TOption
;
73 TAnsiCharSet
= set of AnsiChar;
77 mCurChar
, mNextChar
: AnsiChar;
81 mTokLine
, mTokCol
: Integer; // token start
83 mTokStr
: AnsiString; // string or identifier
84 mTokChar
: AnsiChar; // for delimiters
88 procedure warmup (); // called in constructor to warm up the system
89 procedure loadNextChar (); virtual; abstract; // loads next char into mNextChar; #0 means 'eof'
92 constructor Create (aopts
: TOptions
=[TOption
.SignedNumbers
]);
93 destructor Destroy (); override;
95 procedure error (const amsg
: AnsiString); noreturn
;
96 procedure errorfmt (const afmt
: AnsiString; const args
: array of const); noreturn
;
98 function isEOF (): Boolean; inline;
100 function skipChar (): Boolean; // returns `false` on eof
102 function skipBlanks (): Boolean; // ...and comments; returns `false` on eof
104 function skipToken (): Boolean; // returns `false` on eof
105 {$IFDEF XPARSER_DEBUG}
106 function skipToken1 (): Boolean;
109 function isIdOrStr (): Boolean; inline;
111 function expectId (): AnsiString;
112 procedure expectId (const aid
: AnsiString; caseSens
: Boolean=true);
113 function eatId (const aid
: AnsiString; caseSens
: Boolean=true): Boolean;
114 function eatIdOrStr (const aid
: AnsiString; caseSens
: Boolean=true): Boolean;
116 function expectStr (allowEmpty
: Boolean=false): AnsiString;
117 function expectInt (): Integer;
119 function expectStrOrId (allowEmpty
: Boolean=false): AnsiString;
121 procedure expectTT (ttype
: Integer);
122 function eatTT (ttype
: Integer): Boolean;
124 procedure expectDelim (const ch
: AnsiChar);
125 function expectDelims (const ch
: TAnsiCharSet
): AnsiChar;
126 function eatDelim (const ch
: AnsiChar): Boolean;
128 function isDelim (const ch
: AnsiChar): Boolean; inline;
131 property options
: TOptions read mOptions write mOptions
;
134 property col
: Integer read mCol
;
135 property line
: Integer read mLine
;
137 property curChar
: AnsiChar read mCurChar
;
138 property nextChar
: AnsiChar read mNextChar
;
141 property tokCol
: Integer read mTokCol
;
142 property tokLine
: Integer read mTokLine
;
144 property tokType
: Integer read mTokType
; // see TTXXX constants
145 property tokStr
: AnsiString read mTokStr
; // string or identifier
146 property tokChar
: AnsiChar read mTokChar
; // for delimiters
147 property tokInt
: Integer read mTokInt
;
151 // ////////////////////////////////////////////////////////////////////////// //
153 TFileTextParser
= class(TTextParser
)
155 const BufSize
= 16384;
159 mStreamOwned
: Boolean;
165 procedure loadNextChar (); override; // loads next char into mNextChar; #0 means 'eof'
168 constructor Create (const fname
: AnsiString; aopts
: TOptions
=[TOption
.SignedNumbers
]);
169 constructor Create (st
: TStream
; astOwned
: Boolean=true; aopts
: TOptions
=[TOption
.SignedNumbers
]);
170 destructor Destroy (); override;
173 TStrTextParser
= class(TTextParser
)
179 procedure loadNextChar (); override; // loads next char into mNextChar; #0 means 'eof'
182 constructor Create (const astr
: AnsiString; aopts
: TOptions
=[TOption
.SignedNumbers
]);
183 destructor Destroy (); override;
187 // ////////////////////////////////////////////////////////////////////////// //
194 procedure putBuf (constref buf
; len
: SizeUInt
); virtual; abstract;
197 constructor Create ();
199 procedure flush (); virtual;
201 procedure put (const s
: AnsiString); overload
;
202 procedure put (v
: Byte); overload
;
203 procedure put (v
: Integer); overload
;
204 procedure put (const fmt
: AnsiString; args
: array of const); overload
;
205 procedure putIndent ();
207 procedure unindent ();
210 property curIndent
: Integer read mIndent
;
214 // ////////////////////////////////////////////////////////////////////////// //
216 TFileTextWriter
= class(TTextWriter
)
218 const BufSize
= 16384;
222 mStreamOwned
: Boolean;
227 procedure putBuf (constref buf
; len
: SizeUInt
); override;
230 constructor Create (const fname
: AnsiString);
231 constructor Create (ast
: TStream
; astOwned
: Boolean=true); // will own the stream by default
232 destructor Destroy (); override;
234 procedure flush (); override;
237 TStrTextWriter
= class(TTextWriter
)
242 procedure putBuf (constref buf
; len
: SizeUInt
); override;
245 constructor Create ();
246 destructor Destroy (); override;
248 property str
: AnsiString read mStr
;
258 // ////////////////////////////////////////////////////////////////////////// //
259 constructor TParserException
.Create (pr
: TTextParser
; const amsg
: AnsiString);
261 if (pr
<> nil) then begin tokLine
:= pr
.tokLine
; tokCol
:= pr
.tokCol
; end;
262 inherited Create(amsg
);
265 constructor TParserException
.CreateFmt (pr
: TTextParser
; const afmt
: AnsiString; const args
: array of const);
267 if (pr
<> nil) then begin tokLine
:= pr
.tokLine
; tokCol
:= pr
.tokCol
; end;
268 inherited Create(formatstrf(afmt
, args
));
272 // ////////////////////////////////////////////////////////////////////////// //
273 constructor TTextParser
.Create (aopts
: TOptions
=[TOption
.SignedNumbers
]);
289 destructor TTextParser
.Destroy ();
295 procedure TTextParser
.error (const amsg
: AnsiString); noreturn
;
297 raise TParserException
.Create(self
, amsg
);
301 procedure TTextParser
.errorfmt (const afmt
: AnsiString; const args
: array of const); noreturn
;
303 raise TParserException
.CreateFmt(self
, afmt
, args
);
307 function TTextParser
.isEOF (): Boolean; inline; begin result
:= (mCurChar
= #0); end;
310 procedure TTextParser
.warmup ();
314 mCurChar
:= mNextChar
;
315 if (mNextChar
<> #0) then loadNextChar();
319 function TTextParser
.skipChar (): Boolean;
321 if (mCurChar
= #0) then begin result
:= false; exit
; end;
322 if (mCurChar
= #10) then begin mCol
:= 1; Inc(mLine
); end else Inc(mCol
);
323 mCurChar
:= mNextChar
;
324 if (mCurChar
= #0) then begin result
:= false; exit
; end;
327 if (mCurChar
= #13) then
329 if (mNextChar
= #10) then loadNextChar();
336 function TTextParser
.skipBlanks (): Boolean;
342 if (curChar
= '/') then
344 // single-line comment
345 if (nextChar
= '/') then
347 while not isEOF
and (curChar
<> #10) do skipChar();
348 skipChar(); // skip EOL
352 if (nextChar
= '*') then
354 // skip comment start
359 if (curChar
= '*') and (nextChar
= '/') then
370 // nesting multline comment
371 if (nextChar
= '+') then
373 // skip comment start
379 if (curChar
= '+') and (nextChar
= '/') then
385 if (level
= 0) then break
;
388 if (curChar
= '/') and (nextChar
= '+') then
390 // skip comment start
401 else if (curChar
= '(') and (nextChar
= '*') then
403 // pascal comment; skip comment start
408 if (curChar
= '*') and (nextChar
= ')') then
419 else if (curChar
= '{') and (TOption
.PascalComments
in mOptions
) then
421 // pascal comment; skip comment start
425 if (curChar
= '}') then
435 if (curChar
> ' ') then break
;
436 skipChar(); // skip blank
442 {$IFDEF XPARSER_DEBUG}
443 function TTextParser
.skipToken (): Boolean;
445 writeln('getting token...');
446 result
:= skipToken1();
447 writeln(' got token: ', mTokType
, ' <', mTokStr
, '> : <', mTokChar
, '>');
450 function TTextParser
.skipToken1 (): Boolean;
452 function TTextParser
.skipToken (): Boolean;
454 procedure parseInt ();
456 neg
: Boolean = false;
460 if (TOption
.SignedNumbers
in mOptions
) then
462 if (curChar
= '+') or (curChar
= '-') then
464 neg
:= (curChar
= '-');
466 if (curChar
< '0') or (curChar
> '9') then
469 if (neg
) then mTokChar
:= '-' else mTokChar
:= '+';
474 if (curChar
= '0') then
490 if (base
< 0) then base
:= 10;
491 if (digitInBase(curChar
, base
) < 0) then raise Exception
.Create('invalid number');
493 mTokInt
:= 0; // just in case
496 n
:= digitInBase(curChar
, base
);
497 if (n
< 0) then break
;
499 if (n
< 0) or (n
< mTokInt
) then raise Exception
.Create('integer overflow');
503 // check for valid number end
506 if (curChar
= '.') then raise Exception
.Create('floating numbers aren''t supported yet');
507 if (curChar
= '_') or ((curChar
>= 'A') and (curChar
<= 'Z')) or ((curChar
>= 'a') and (curChar
<= 'z')) or (curChar
>= #128) then
509 raise Exception
.Create('invalid number');
512 if neg
then mTokInt
:= -mTokInt
;
515 procedure parseString ();
521 mTokStr
:= ''; // just in case
523 skipChar(); // skip starting quote
527 if (qch
= '"') and (curChar
= '\') then
529 if (nextChar
= #0) then raise Exception
.Create('unterminated string escape');
531 // skip backslash and escape type
540 'x', 'X': // hex escape
542 n
:= digitInBase(curChar
, 16);
543 if (n
< 0) then raise Exception
.Create('invalid hexstr escape');
545 if (digitInBase(curChar
, 16) > 0) then
547 n
:= n
*16+digitInBase(curChar
, 16);
550 mTokStr
+= AnsiChar(n
);
556 // duplicate single quote (pascal style)
557 if (qch
= '''') and (curChar
= '''') and (nextChar
= '''') then
565 if (curChar
= qch
) then
567 skipChar(); // skip ending quote
575 procedure parseId ();
578 mTokStr
:= ''; // just in case
579 while (curChar
= '_') or ((curChar
>= '0') and (curChar
<= '9')) or
580 ((curChar
>= 'A') and (curChar
<= 'Z')) or
581 ((curChar
>= 'a') and (curChar
<= 'z')) or
583 ((TOption
.DollarIsId
in mOptions
) and (curChar
= '$')) or
584 ((TOption
.DotIsId
in mOptions
) and (curChar
= '.') and (nextChar
<> '.')) do
597 if not skipBlanks() then
611 if (TOption
.SignedNumbers
in mOptions
) and ((curChar
= '+') or (curChar
= '-')) then begin parseInt(); exit
; end;
612 if (curChar
>= '0') and (curChar
<= '9') then begin parseInt(); exit
; end;
615 if (curChar
= '"') or (curChar
= '''') then begin parseString(); exit
; end;
618 if (curChar
= '_') or ((curChar
>= 'A') and (curChar
<= 'Z')) or ((curChar
>= 'a') and (curChar
<= 'z')) or (curChar
>= #128) then begin parseId(); exit
; end;
619 if (TOption
.DollarIsId
in mOptions
) and (curChar
= '$') then begin parseId(); exit
; end;
620 if (TOption
.DotIsId
in mOptions
) and (curChar
= '.') and (nextChar
<> '.') then begin parseId(); exit
; end;
626 if (curChar
= '=') then
629 '<': begin mTokType
:= TTLessEqu
; mTokStr
:= '<='; skipChar(); exit
; end;
630 '>': begin mTokType
:= TTGreatEqu
; mTokStr
:= '>='; skipChar(); exit
; end;
631 '!': begin mTokType
:= TTNotEqu
; mTokStr
:= '!='; skipChar(); exit
; end;
632 '=': begin mTokType
:= TTEqu
; mTokStr
:= '=='; skipChar(); exit
; end;
633 ':': begin mTokType
:= TTAss
; mTokStr
:= ':='; skipChar(); exit
; end;
636 else if (mTokChar
= curChar
) then
639 '<': begin mTokType
:= TTShl
; mTokStr
:= '<<'; skipChar(); exit
; end;
640 '>': begin mTokType
:= TTShr
; mTokStr
:= '>>'; skipChar(); exit
; end;
641 '&': begin mTokType
:= TTLogAnd
; mTokStr
:= '&&'; skipChar(); exit
; end;
642 '|': begin mTokType
:= TTLogOr
; mTokStr
:= '||'; skipChar(); exit
; end;
648 '<': if (curChar
= '>') then begin mTokType
:= TTNotEqu
; mTokStr
:= '<>'; skipChar(); exit
; end;
649 '.': if (curChar
= '.') then begin mTokType
:= TTDotDot
; mTokStr
:= '..'; skipChar(); exit
; end;
655 function TTextParser
.isIdOrStr (): Boolean; inline;
657 result
:= (mTokType
= TTId
) or (mTokType
= TTStr
);
661 function TTextParser
.expectId (): AnsiString;
663 if (mTokType
<> TTId
) then raise Exception
.Create('identifier expected');
669 procedure TTextParser
.expectId (const aid
: AnsiString; caseSens
: Boolean=true);
673 if (mTokType
<> TTId
) or (mTokStr
<> aid
) then raise Exception
.Create('identifier '''+aid
+''' expected');
677 if (mTokType
<> TTId
) or (not strEquCI1251(mTokStr
, aid
)) then raise Exception
.Create('identifier '''+aid
+''' expected');
683 function TTextParser
.eatId (const aid
: AnsiString; caseSens
: Boolean=true): Boolean;
687 result
:= (mTokType
= TTId
) and (mTokStr
= aid
);
691 result
:= (mTokType
= TTId
) and strEquCI1251(mTokStr
, aid
);
693 if result
then skipToken();
697 function TTextParser
.eatIdOrStr (const aid
: AnsiString; caseSens
: Boolean=true): Boolean;
701 result
:= (mTokType
= TTId
) and (mTokStr
= aid
);
702 if not result
then result
:= (mTokType
= TTStr
) and (mTokStr
= aid
);
706 result
:= (mTokType
= TTId
) and strEquCI1251(mTokStr
, aid
);
707 if not result
then result
:= (mTokType
= TTStr
) and strEquCI1251(mTokStr
, aid
);
709 if result
then skipToken();
713 function TTextParser
.expectStr (allowEmpty
: Boolean=false): AnsiString;
715 if (mTokType
<> TTStr
) then raise Exception
.Create('string expected');
716 if (not allowEmpty
) and (Length(mTokStr
) = 0) then raise Exception
.Create('non-empty string expected');
722 function TTextParser
.expectStrOrId (allowEmpty
: Boolean=false): AnsiString;
726 if (not allowEmpty
) and (Length(mTokStr
) = 0) then raise Exception
.Create('non-empty string expected');
730 raise Exception
.Create('string or identifier expected');
737 function TTextParser
.expectInt (): Integer;
739 if (mTokType
<> TTInt
) then raise Exception
.Create('string expected');
745 procedure TTextParser
.expectTT (ttype
: Integer);
747 if (mTokType
<> ttype
) then raise Exception
.Create('unexpected token');
752 function TTextParser
.eatTT (ttype
: Integer): Boolean;
754 result
:= (mTokType
= ttype
);
755 if result
then skipToken();
759 procedure TTextParser
.expectDelim (const ch
: AnsiChar);
761 if (mTokType
<> TTDelim
) or (mTokChar
<> ch
) then raise Exception
.CreateFmt('delimiter ''%s'' expected', [ch
]);
766 function TTextParser
.expectDelims (const ch
: TAnsiCharSet
): AnsiChar;
768 if (mTokType
<> TTDelim
) then raise Exception
.Create('delimiter expected');
769 if not (mTokChar
in ch
) then raise Exception
.Create('delimiter expected');
775 function TTextParser
.eatDelim (const ch
: AnsiChar): Boolean;
777 result
:= (mTokType
= TTDelim
) and (mTokChar
= ch
);
778 if result
then skipToken();
782 function TTextParser
.isDelim (const ch
: AnsiChar): Boolean; inline;
784 result
:= (mTokType
= TTDelim
) and (mTokChar
= ch
);
788 // ////////////////////////////////////////////////////////////////////////// //
789 constructor TFileTextParser
.Create (const fname
: AnsiString; aopts
: TOptions
=[TOption
.SignedNumbers
]);
792 mFile
:= openDiskFileRO(fname
);
793 mStreamOwned
:= true;
794 GetMem(mBuffer
, BufSize
);
796 mBufLen
:= mFile
.Read(mBuffer
^, BufSize
);
797 if (mBufLen
< 0) then raise Exception
.Create('TFileTextParser: read error');
798 inherited Create(aopts
);
802 constructor TFileTextParser
.Create (st
: TStream
; astOwned
: Boolean=true; aopts
: TOptions
=[TOption
.SignedNumbers
]);
804 if (st
= nil) then raise Exception
.Create('cannot create parser for nil stream');
806 mStreamOwned
:= astOwned
;
807 GetMem(mBuffer
, BufSize
);
809 mBufLen
:= mFile
.Read(mBuffer
^, BufSize
);
810 if (mBufLen
< 0) then raise Exception
.Create('TFileTextParser: read error');
811 inherited Create(aopts
);
815 destructor TFileTextParser
.Destroy ();
817 if (mBuffer
<> nil) then FreeMem(mBuffer
);
821 if mStreamOwned
then mFile
.Free();
827 procedure TFileTextParser
.loadNextChar ();
829 if (mBufLen
= 0) then begin mNextChar
:= #0; exit
; end;
830 if (mBufPos
>= mBufLen
) then
832 mBufLen
:= mFile
.Read(mBuffer
^, BufSize
);
833 if (mBufLen
< 0) then raise Exception
.Create('TFileTextParser: read error');
834 if (mBufLen
= 0) then begin mNextChar
:= #0; exit
; end;
837 assert(mBufPos
< mBufLen
);
838 mNextChar
:= mBuffer
[mBufPos
];
840 if (mNextChar
= #0) then mNextChar
:= ' ';
844 // ////////////////////////////////////////////////////////////////////////// //
845 constructor TStrTextParser
.Create (const astr
: AnsiString; aopts
: TOptions
=[TOption
.SignedNumbers
]);
849 inherited Create(aopts
);
853 destructor TStrTextParser
.Destroy ();
860 procedure TStrTextParser
.loadNextChar ();
863 if (mPos
> Length(mStr
)) then exit
;
864 mNextChar
:= mStr
[mPos
]; Inc(mPos
);
865 if (mNextChar
= #0) then mNextChar
:= ' ';
869 // ////////////////////////////////////////////////////////////////////////// //
870 constructor TTextWriter
.Create (); begin mIndent
:= 0; end;
871 procedure TTextWriter
.flush (); begin end;
872 procedure TTextWriter
.put (const s
: AnsiString); overload
; begin if (Length(s
) > 0) then putBuf((@(s
[1]))^, Length(s
)); end;
873 procedure TTextWriter
.put (v
: Byte); overload
; begin put('%d', [v
]); end;
874 procedure TTextWriter
.put (v
: Integer); overload
; begin put('%d', [v
]); end;
875 procedure TTextWriter
.put (const fmt
: AnsiString; args
: array of const); overload
; begin put(formatstrf(fmt
, args
)); end;
876 procedure TTextWriter
.putIndent (); var f
: Integer; begin for f
:= 1 to mIndent
do put(' '); end;
877 procedure TTextWriter
.indent (); begin Inc(mIndent
, 2); end;
878 procedure TTextWriter
.unindent (); begin Dec(mIndent
, 2); end;
881 // ////////////////////////////////////////////////////////////////////////// //
882 constructor TFileTextWriter
.Create (const fname
: AnsiString);
884 mFile
:= createDiskFile(fname
);
885 mStreamOwned
:= true;
887 GetMem(mBuffer
, BufSize
);
888 assert(mBuffer
<> nil);
893 constructor TFileTextWriter
.Create (ast
: TStream
; astOwned
: Boolean=true);
895 if (ast
= nil) then raise Exception
.Create('cannot write to nil stream');
897 mStreamOwned
:= astOwned
;
899 GetMem(mBuffer
, BufSize
);
900 assert(mBuffer
<> nil);
904 destructor TFileTextWriter
.Destroy ();
907 if (mBuffer
<> nil) then FreeMem(mBuffer
);
910 if (mStreamOwned
) then mFile
.Free();
916 procedure TFileTextWriter
.flush ();
918 if (mFile
<> nil) and (mBufUsed
> 0) then
920 mFile
.WriteBuffer(mBuffer
^, mBufUsed
);
926 procedure TFileTextWriter
.putBuf (constref buf
; len
: SizeUInt
);
931 if (len
= 0) then exit
;
935 left
:= BufSize
-mBufUsed
;
939 left
:= BufSize
-mBufUsed
;
942 if (left
> len
) then left
:= Integer(len
);
943 Move(pc
^, (mBuffer
+mBufUsed
)^, left
);
951 // ////////////////////////////////////////////////////////////////////////// //
952 constructor TStrTextWriter
.Create ();
958 destructor TStrTextWriter
.Destroy ();
965 procedure TStrTextWriter
.putBuf (constref buf
; len
: SizeUInt
);
971 SetLength(st
, Integer(len
));
972 Move(buf
, PChar(st
)^, Integer(len
));