3 * Iter Vehemens ad Necem (IVAN)
4 * Copyright (C) Timo Kiviluoto
5 * Released under the GNU General
8 * See LICENSING which should be included
9 * along with this file for more details
12 #ifndef __FELIB_PARSE_H__
13 #define __FELIB_PARSE_H__
21 // ////////////////////////////////////////////////////////////////////////// //
23 struct InputFileSaved
;
26 using valuemap
= std::map
<festring
, sLong
>;
27 using InputFileGetVarFn
= festring (*) (TextInput
*fl
, cfestring
&name
);
30 // ////////////////////////////////////////////////////////////////////////// //
33 TextInput (const valuemap
*aValueMap
=0);
35 void setup (const valuemap
*aValueMap
);
38 virtual ~TextInput ();
40 virtual cfestring
&GetFileName () const = 0;
41 truth
IsOpen () { return isRealOpen(); }
43 virtual void Close (); // for dtor
46 void UngetChar (int ch
);
48 festring
ReadCode (truth AbortOnEOF
=true);
49 festring
ReadWord (truth AbortOnEOF
=true);
50 void ReadWord (festring
&str
, truth AbortOnEOF
=true, truth skipIt
=false);
51 char ReadLetter (truth AbortOnEOF
=true);
52 sLong
ReadNumber (int CallLevel
=HIGHEST
, truth PreserveTerminator
=false, truth
*wasCloseBrc
=0);
53 festring
ReadStringOrNumber (sLong
*num
, truth
*isString
, truth PreserveTerminator
=false, truth
*wasCloseBrc
=0);
54 sLong
ReadNumberKeepStr (int CallLevel
=HIGHEST
, truth PreserveTerminator
=false, truth
*wasCloseBrc
=0);
55 festring
ReadStringOrNumberKeepStr (sLong
*num
, truth
*isString
, truth PreserveTerminator
=false, truth
*wasCloseBrc
=0);
59 festring
getVar (cfestring
&name
);
60 void setVar (cfestring
&name
, cfestring
&value
);
61 truth
delVar (cfestring
&name
);
63 void setGetVarCB (InputFileGetVarFn cb
) { mGetVar
= cb
; }
65 void die (cfestring
&msg
);
67 void skipBlanks (); // comments too
69 int countArrayItems (char echar
);
71 inline int TokenLine () const { return mTokenLine
; }
72 inline int CurrentLine () const { return mCurrentLine
; }
74 // when reading a number, and `mCollectingNumStr` flag is set, this will be filled
75 inline cfestring
& numStr () const { return mNumStr
; }
78 festring
ReadNumberIntr (int CallLevel
, sLong
*num
, truth
*isString
, truth allowStr
, truth PreserveTerminator
, truth
*wasCloseBrc
);
79 int HandlePunct (festring
&String
, int Char
, int Mode
);
81 festring
findVar (cfestring
&name
, truth
*found
) const;
82 void readWordIntr (festring
&String
, truth AbortOnEOF
);
84 festring
readCondition (festring
&token
, int prio
, truth skipIt
);
87 virtual int realGetChar () = 0;
88 virtual bool isRealEof () = 0;
89 virtual bool isRealOpen () = 0;
90 virtual void realClearFlags () = 0;
92 // used to save/restore parsing positions
93 virtual sLong
realGetPos () = 0;
94 virtual void realSetPos (sLong apos
) = 0;
97 enum { MaxUngetChars
= 4 };
98 typedef std::map
<festring
, festring
> VarMap
;
101 const valuemap
*ValueMap
;
103 truth lastWordWasString
;
105 InputFileGetVarFn mGetVar
;
106 std::stack
<int> mIfStack
;
107 int mCharBuf
[MaxUngetChars
];
111 festring mNumStr
; // when reading a number, and `mCollectingNumStr` flag is set, this will be filled
112 truth mCollectingNumStr
;
114 friend InputFileSaved
;
118 // ////////////////////////////////////////////////////////////////////////// //
119 class TextInputFile
: public TextInput
{
121 TextInputFile (cfestring
&FileName
, const valuemap
*aValueMap
=0, truth AbortOnErr
=true);
122 virtual ~TextInputFile ();
124 virtual cfestring
&GetFileName () const override
{ return ifile
.GetFileName(); }
125 virtual void Close () override
;
128 virtual int realGetChar () override
;
129 virtual bool isRealEof () override
;
130 virtual bool isRealOpen () override
;
131 virtual void realClearFlags () override
;
133 virtual sLong
realGetPos () override
;
134 virtual void realSetPos (sLong apos
) override
;
141 // ////////////////////////////////////////////////////////////////////////// //
142 class MemTextFile
: public TextInput
{
144 MemTextFile (cfestring
&afname
, cfestring
&str
, const valuemap
*aValueMap
=0);
145 virtual ~MemTextFile ();
147 virtual cfestring
&GetFileName () const override
{ return tfname
; }
148 virtual void Close () override
;
151 virtual int realGetChar () override
;
152 virtual bool isRealEof () override
;
153 virtual bool isRealOpen () override
;
154 virtual void realClearFlags () override
;
156 virtual sLong
realGetPos () override
;
157 virtual void realSetPos (sLong apos
) override
;
167 // ////////////////////////////////////////////////////////////////////////// //
168 inline void ReadData (char &Type
, TextInput
&infile
) { Type
= infile
.ReadNumber(); }
169 inline void ReadData (uChar
&Type
, TextInput
&infile
) { Type
= infile
.ReadNumber(); }
170 inline void ReadData (short &Type
, TextInput
&infile
) { Type
= infile
.ReadNumber(); }
171 inline void ReadData (uShort
&Type
, TextInput
&infile
) { Type
= infile
.ReadNumber(); }
172 //inline void ReadData (sLong &Type, inputfile &SaveFile) { Type = SaveFile.ReadNumber(); } //k8:64
173 inline void ReadData (feuLong
&Type
, TextInput
&infile
) { Type
= infile
.ReadNumber(); }
174 inline void ReadData (int &Type
, TextInput
&infile
) { Type
= infile
.ReadNumber(); }
175 inline void ReadData (packv2
&Type
, TextInput
&infile
) { Type
= infile
.ReadVector2d(); }
176 inline void ReadData (v2
&Type
, TextInput
&infile
) { Type
= infile
.ReadVector2d(); }
177 inline void ReadData (rect
&Type
, TextInput
&infile
) { Type
= infile
.ReadRect(); }
179 void ReadData (festring
&, TextInput
&);
180 void ReadData (fearray
<sLong
> &, TextInput
&);
181 void ReadData (fearray
<festring
> &, TextInput
&);
183 template <class type
> inline void ReadData (fearray
<type
> &Array
, TextInput
&infile
) {
186 infile
.ReadWord(Word
);
190 ReadData(Array
.Data
[0], infile
);
193 // more than one element
194 typedef typename fearray
<type
>::sizetype sizetype
;
196 // unknown number of elements?
198 infile
.ReadWord(Word
);
199 if (Word
!= "{") ABORT("Array syntax error \"%s\" found in file %s, line %d!", Word
.CStr(), infile
.GetFileName().CStr(), infile
.TokenLine());
200 //fprintf(stderr, "counting array items; file: '%s'; line: %u\n", SaveFile.GetFileName().CStr(), SaveFile.TokenLine());
201 int count
= infile
.countArrayItems('}');
203 if (count
< 1) ABORT("Array count error found in file %s, line %d!", infile
.GetFileName().CStr(), infile
.TokenLine());
205 //fprintf(stderr, "counted %u array items; file: '%s'; line: %u\n", Size, SaveFile.GetFileName().CStr(), SaveFile.TokenLine());
207 if (Word
!= "=") ABORT("Array syntax error: '=', '==' or ':=' expected in file %s, line %u!", infile
.GetFileName().CStr(), infile
.TokenLine());
208 infile
.ReadWord(Word
);
209 if (Word
!= "{") ABORT("Array syntax error \"%s\" found in file %s, line %u!", Word
.CStr(), infile
.GetFileName().CStr(), infile
.TokenLine());
210 Size
= infile
.ReadNumber();
211 int count
= infile
.countArrayItems('}');
212 if (count
< 1) ABORT("Array count error found in file %s, line %d!", infile
.GetFileName().CStr(), infile
.TokenLine());
213 //HACK: 2 for vectors, 4 for rects
214 //if ((sizetype)count != Size && (sizetype)count/2 != Size && (sizetype)count/4 != Size) ABORT("Array count error (%u != %d) found in file %s, line %d!", Size, count, SaveFile.GetFileName().CStr(), SaveFile.TokenLine());
215 if ((sizetype
)count
!= Size
) ABORT("Array count error (%u != %d) found in file %s, line %d!", Size
, count
, infile
.GetFileName().CStr(), infile
.TokenLine());
217 Array
.Allocate(Size
);
218 for (sizetype c
= 0; c
< Size
; ++c
) ReadData(Array
.Data
[c
], infile
);
219 if (infile
.ReadWord() != "}") ABORT("Illegal array terminator \"%s\" encountered in file %s, line %u!", Word
.CStr(), infile
.GetFileName().CStr(), infile
.TokenLine());