moved almost all hardcoded constants to "define.dat"
[k8-i-v-a-n.git] / src / felib / feparse.h
bloba73d58f216106d79fbf9bc3d9a70cd502f6c50e9
1 /*
3 * Iter Vehemens ad Necem (IVAN)
4 * Copyright (C) Timo Kiviluoto
5 * Released under the GNU General
6 * Public License
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__
15 #include "feerror.h"
16 #include "festring.h"
17 #include "fearray.h"
18 #include "fesave.h"
21 // ////////////////////////////////////////////////////////////////////////// //
22 class TextInput;
23 struct InputFileSaved;
26 using valuemap = std::map<festring, sLong>;
27 using InputFileGetVarFn = festring (*) (TextInput *fl, cfestring &name);
30 // ////////////////////////////////////////////////////////////////////////// //
31 class TextInput {
32 protected:
33 TextInput (const valuemap *aValueMap=0);
35 void setup (const valuemap *aValueMap);
37 public:
38 virtual ~TextInput ();
40 virtual cfestring &GetFileName () const = 0;
41 truth IsOpen () { return isRealOpen(); }
42 truth Eof ();
43 virtual void Close (); // for dtor
45 int GetChar ();
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);
56 v2 ReadVector2d ();
57 rect ReadRect ();
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; }
77 protected:
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);
86 protected:
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;
96 protected:
97 enum { MaxUngetChars = 4 };
98 typedef std::map<festring, festring> VarMap;
100 protected:
101 const valuemap *ValueMap;
102 truth lastWasNL;
103 truth lastWordWasString;
104 VarMap mVars;
105 InputFileGetVarFn mGetVar;
106 std::stack<int> mIfStack;
107 int mCharBuf[MaxUngetChars];
108 int mCharBufPos;
109 int mCurrentLine;
110 int mTokenLine;
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 {
120 public:
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;
127 protected:
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;
136 protected:
137 inputfile ifile;
141 // ////////////////////////////////////////////////////////////////////////// //
142 class MemTextFile : public TextInput {
143 public:
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;
150 protected:
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;
159 protected:
160 unsigned char *buf;
161 int bufSize;
162 int bufPos;
163 festring tfname;
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) {
184 Array.Clear();
185 festring Word;
186 infile.ReadWord(Word);
187 // one element?
188 if (Word == "==") {
189 Array.Allocate(1);
190 ReadData(Array.Data[0], infile);
191 return;
193 // more than one element
194 typedef typename fearray<type>::sizetype sizetype;
195 sizetype Size;
196 // unknown number of elements?
197 if (Word == ":=") {
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('}');
202 // HACK
203 if (count < 1) ABORT("Array count error found in file %s, line %d!", infile.GetFileName().CStr(), infile.TokenLine());
204 Size = count;
205 //fprintf(stderr, "counted %u array items; file: '%s'; line: %u\n", Size, SaveFile.GetFileName().CStr(), SaveFile.TokenLine());
206 } else {
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());
223 #endif