-
Notifications
You must be signed in to change notification settings - Fork 0
/
lx.h
executable file
·390 lines (359 loc) · 14.8 KB
/
lx.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/////////////////////////////////////////////////
// Lexical-Char-Definition
typedef enum {lctUndef, lctSpace, lctNum, lctAlpha, lctSSym, lctTerm} TLxChTy;
typedef enum {lcdtUsAscii, lcdtYuAscii} TLxChDefTy;
ClassTP(TLxChDef, PLxChDef)//{
private:
TIntV ChTyV;
TChV UcChV;
void SetUcCh(const TStr& Str);
void SetChTy(const TLxChTy& ChTy, const TStr& Str);
public:
TLxChDef(const TLxChDefTy& ChDefTy);
static PLxChDef New(const TLxChDefTy& ChDefTy=lcdtUsAscii){
return PLxChDef(new TLxChDef(ChDefTy));}
TLxChDef(TSIn& SIn): ChTyV(SIn), UcChV(SIn){}
static PLxChDef Load(TSIn& SIn){return new TLxChDef(SIn);}
void Save(TSOut& SOut){ChTyV.Save(SOut); UcChV.Save(SOut);}
TLxChDef& operator=(const TLxChDef& ChDef){
ChTyV=ChDef.ChTyV; UcChV=ChDef.UcChV; return *this;}
int GetChTy(const char& Ch) const {return ChTyV[Ch-TCh::Mn];}
bool IsTerm(const char& Ch) const {return ChTyV[Ch-TCh::Mn]==TInt(lctTerm);}
bool IsSpace(const char& Ch) const {return ChTyV[Ch-TCh::Mn]==TInt(lctSpace);}
bool IsAlpha(const char& Ch) const {return ChTyV[Ch-TCh::Mn]==TInt(lctAlpha);}
bool IsNum(const char& Ch) const {return ChTyV[Ch-TCh::Mn]==TInt(lctNum);}
bool IsAlNum(const char& Ch) const {
return (ChTyV[Ch-TCh::Mn]==TInt(lctAlpha))||(ChTyV[Ch-TCh::Mn]==TInt(lctNum));}
char GetUc(const char& Ch) const {return UcChV[Ch-TCh::Mn];}
bool IsNmStr(const TStr& Str) const;
TStr GetUcStr(const TStr& Str) const;
// standard entry points
static PLxChDef GetChDef(const TLxChDefTy& ChDefTy=lcdtUsAscii);
// static TLxChDef& GetChDefRef(const TLxChDefTy& ChDefTy=lcdtUsAscii);
};
/////////////////////////////////////////////////
// Lexical-Symbols
typedef enum {
syUndef, syLn, syTab, syBool, syInt, syFlt, syStr, syIdStr, syQStr,
syPeriod, syDPeriod, syComma, syColon, syDColon, sySemicolon,
syPlus, syMinus, syAsterisk, sySlash, syPercent,
syExclamation, syVBar, syAmpersand, syQuestion, syHash,
syEq, syNEq, syLss, syGtr, syLEq, syGEq,
syLParen, syRParen, syLBracket, syRBracket, syLBrace, syRBrace,
syEoln, syEof,
syMnRw, syRw1, syRw2, syRw3, syRw4, syRw5, syRw6, syRw7, syRw8, syRw9,
syRw10, syRw11, syRw12, syRw13, syRw14, syRw15, syRw16, syRw17, syMxRw
} TLxSym;
class TLxSymStr{
public:
static const TStr UndefStr;
static const TStr LnStr;
static const TStr TabStr;
static const TStr BoolStr;
static const TStr IntStr;
static const TStr FltStr;
static const TStr StrStr;
static const TStr IdStrStr;
static const TStr QStrStr;
static const TStr PeriodStr;
static const TStr DPeriodStr;
static const TStr CommaStr;
static const TStr ColonStr;
static const TStr DColonStr;
static const TStr SemicolonStr;
static const TStr PlusStr;
static const TStr MinusStr;
static const TStr AsteriskStr;
static const TStr SlashStr;
static const TStr PercentStr;
static const TStr ExclamationStr;
static const TStr VBarStr;
static const TStr AmpersandStr;
static const TStr QuestionStr;
static const TStr HashStr;
static const TStr EqStr;
static const TStr NEqStr;
static const TStr LssStr;
static const TStr GtrStr;
static const TStr LEqStr;
static const TStr GEqStr;
static const TStr LParenStr;
static const TStr RParenStr;
static const TStr LBracketStr;
static const TStr RBracketStr;
static const TStr LBraceStr;
static const TStr RBraceStr;
static const TStr EolnStr;
static const TStr EofStr;
static TStr GetSymStr(const TLxSym& Sym);
static TLxSym GetSSym(const TStr& Str);
public:
static bool IsSep(const TLxSym& PrevSym, const TLxSym& Sym);
};
/////////////////////////////////////////////////
// Lexical-Input-Symbol-State
class TILx;
class TILxSymSt{
private:
TLxSym Sym;
TStr Str, UcStr, CmtStr;
bool Bool; int Int; double Flt;
int SymLnN, SymLnChN, SymChN;
public:
TILxSymSt();
TILxSymSt(const TILxSymSt& SymSt);
TILxSymSt(TILx& Lx);
TILxSymSt(TSIn&){Fail;}
void Save(TSOut&){Fail;}
void Restore(TILx& Lx);
};
/////////////////////////////////////////////////
// Lexical-Input
typedef enum {
iloCmtAlw, iloRetEoln, iloSigNum, iloUniStr, iloCsSens,
iloExcept, iloTabSep, iloList, iloMx} TILxOpt;
class TILx{
private:
PLxChDef ChDef;
PSIn SIn;
TSIn& RSIn;
char PrevCh, Ch;
int LnN, LnChN, ChN;
TSStack<TILxSymSt> PrevSymStStack;
TStrIntH RwStrH;
bool IsCmtAlw, IsRetEoln, IsSigNum, IsUniStr, IsCsSens;
bool IsExcept, IsTabSep, IsList;
char GetCh(){
Assert(Ch!=TCh::EofCh);
PrevCh=Ch; LnChN++; ChN++;
Ch=((RSIn.Eof()) ? TCh::EofCh : RSIn.GetCh());
if (IsList){putchar(Ch);}
return Ch;
}
char GetCh1(){char Ch=GetCh1(); printf("%c", Ch); return Ch;}
public: // symbol state
TLxSym Sym;
TChA Str, UcStr, CmtStr;
bool Bool; int Int; double Flt;
int SymLnN, SymLnChN, SymChN;
bool QuoteP;
char QuoteCh;
public:
TILx(const PSIn& _SIn, const TFSet& OptSet=TFSet(),
const TLxChDefTy& ChDefTy=lcdtUsAscii);
TILx& operator=(const TILx&){Fail; return *this;}
void SetOpt(const int& Opt, const bool& Val);
TLxSym AddRw(const TStr& Str);
TLxSym GetRw(const TStr& Str){
return TLxSym(int(RwStrH.GetDat(Str)));}
PSIn GetSIn(const char& SepCh);
int GetLnN() const {return LnN;}
bool IsBof() const {return ChN==-1;}
bool IsEof() const {return Ch==TCh::EofCh;}
TLxSym GetSym(const TFSet& Expect);
TLxSym GetSym(){return GetSym(TFSet());}
TLxSym GetSym(const TLxSym& Sym){return GetSym(TFSet()|Sym);}
TLxSym GetSym(const TLxSym& Sym1, const TLxSym& Sym2){
return GetSym(TFSet()|Sym1|Sym2);}
TLxSym GetSym(const TLxSym& Sym1, const TLxSym& Sym2, const TLxSym& Sym3){
return GetSym(TFSet()|Sym1|Sym2|Sym3);}
TLxSym GetSym(const TLxSym& Sym1, const TLxSym& Sym2, const TLxSym& Sym3,
const TLxSym& Sym4){
return GetSym(TFSet()|Sym1|Sym2|Sym3|Sym4);}
bool GetBool(){GetSym(TFSet()|syBool); return Bool;}
int GetInt(){GetSym(TFSet()|syInt); return Int;}
double GetFlt(){GetSym(TFSet()|syFlt); return Flt;}
TStr GetStr(const TStr& _Str=TStr()){
GetSym(TFSet()|syStr); IAssert(_Str.Empty()||(_Str==Str)); return Str;}
TStr GetIdStr(const TStr& IdStr=TStr()){
GetSym(TFSet()|syIdStr); IAssert(IdStr.Empty()||(IdStr==Str)); return Str;}
TStr GetQStr(const TStr& QStr=TStr()){
GetSym(TFSet()|syQStr); IAssert(QStr.Empty()||(Str==QStr)); return Str;}
void GetEoln(){GetSym(TFSet()|syEoln);}
TStr GetStrToCh(const char& ToCh);
TStr GetStrToEolnOrCh(const char& ToCh);
TStr GetStrToEoln(const bool& DoTrunc=false);
TStr GetStrToEolnAndCh(const char& ToCh);
void SkipToEoln();
void SkipToSym(const TLxSym& SkipToSym){
while (Sym!=SkipToSym){GetSym();}}
void PutSym(const TILxSymSt& SymSt){PrevSymStStack.Push(TILxSymSt(SymSt));}
void PutSym(){PrevSymStStack.Push(TILxSymSt(*this));}
TLxSym PeekSym(){TLxSym NextSym=GetSym(); PutSym(); return NextSym;}
TLxSym PeekSym(const int& Syms);
TStr GetSymStr() const;
TStr GetFPosStr() const;
static TStr GetQStr(const TStr& Str, const bool& QuoteP, const char& QuoteCh);
bool IsVar(const TStr& VarNm){
GetSym(); bool Var=((Sym==syIdStr)&&(Str==VarNm)); PutSym(); return Var;}
void GetVar(const TStr& VarNm,
const bool& LBracket=false, const bool& NewLn=false){
GetIdStr(VarNm); GetSym(syColon);
if (LBracket){GetSym(syLBracket);} if (NewLn){GetEoln();}}
void GetVarEnd(const bool& RBracket=false, const bool& NewLn=false){
if (RBracket){GetSym(syRBracket);}
if (NewLn){GetEoln();}}
bool PeekVarEnd(const bool& RBracket=false, const bool& NewLn=false){
if (RBracket){return PeekSym()==syRBracket;}
if (NewLn){return PeekSym()==syEoln;} Fail; return false;}
bool GetVarBool(const TStr& VarNm, const bool& NewLn=true){
GetIdStr(VarNm); GetSym(syColon); bool Bool=GetBool();
if (NewLn){GetEoln();} return Bool;}
int GetVarInt(const TStr& VarNm, const bool& NewLn=true){
GetIdStr(VarNm); GetSym(syColon); int Int=GetInt();
if (NewLn){GetEoln();} return Int;}
double GetVarFlt(const TStr& VarNm, const bool& NewLn=true){
GetIdStr(VarNm); GetSym(syColon); double Flt=GetFlt();
if (NewLn){GetEoln();} return Flt;}
TStr GetVarStr(const TStr& VarNm, const bool& NewLn=true){
GetIdStr(VarNm); GetSym(syColon); TStr Str=GetQStr();
if (NewLn){GetEoln();} return Str;}
TSecTm GetVarSecTm(const TStr& VarNm, const bool& NewLn=true){
GetIdStr(VarNm); GetSym(syColon); TSecTm SecTm=TSecTm::LoadTxt(*this);
if (NewLn){GetEoln();} return SecTm;}
void GetVarBoolV(const TStr& VarNm, TBoolV& BoolV, const bool& NewLn=true);
void GetVarIntV(const TStr& VarNm, TIntV& IntV, const bool& NewLn=true);
void GetVarFltV(const TStr& VarNm, TFltV& FltV, const bool& NewLn=true);
void GetVarStrV(const TStr& VarNm, TStrV& StrV, const bool& NewLn=true);
void GetVarStrPrV(const TStr& VarNm, TStrPrV& StrPrV, const bool& NewLn=true);
void GetVarStrVV(const TStr& VarNm, TVec<TStrV>& StrVV, const bool& NewLn=true);
// file-of-lines
static void GetLnV(const TStr& FNm, TStrV& LnV);
};
/////////////////////////////////////////////////
// Lexical-Output
typedef enum {
oloCmtAlw, oloFrcEoln, oloSigNum, oloUniStr,
oloCsSens, oloTabSep, oloVarIndent, oloMx} TOLxOpt;
class TOLx{
private:
PLxChDef ChDef;
PSOut SOut;
TSOut& RSOut;
bool IsCmtAlw, IsFrcEoln, IsSigNum, IsUniStr;
bool IsCsSens, IsTabSep, IsVarIndent;
int VarIndentLev;
TStrIntH RwStrH;
TIntStrH RwSymH;
TLxSym PrevSym;
void PutSep(const TLxSym& Sym);
public:
TOLx(const PSOut& _SOut, const TFSet& OptSet,
const TLxChDefTy& ChDefTy=lcdtUsAscii);
TOLx& operator=(const TOLx&){Fail; return *this;}
void SetOpt(const int& Opt, const bool& Val);
TLxSym AddRw(const TStr& Str);
PSOut GetSOut(const char& SepCh){
RSOut.PutCh(SepCh); return SOut;}
void PutSym(const TLxSym& Sym);
void PutBool(const TBool& Bool){
PutSep(syIdStr); RSOut.PutStr(TBool::GetStr(Bool));}
void PutInt(const TInt& Int){
if (!IsSigNum){Assert(int(Int)>=0);}
PutSep(syInt); RSOut.PutStr(TInt::GetStr(Int));}
void PutFlt(const TFlt& Flt, const int& Width=-1, const int& Prec=-1){
if (!IsSigNum){Assert(Flt>=0);}
PutSep(syFlt); RSOut.PutStr(TFlt::GetStr(Flt, Width, Prec));}
void PutStr(const TStr& Str){
if ((IsUniStr)&&(ChDef->IsNmStr(Str))){PutSep(syIdStr); RSOut.PutStr(Str);}
else {PutSep(syStr); RSOut.PutCh('"'); RSOut.PutStr(Str); RSOut.PutCh('"');}}
void PutIdStr(const TStr& Str, const bool& CheckIdStr=true){
if (CheckIdStr){Assert(ChDef->IsNmStr(Str));}
PutSep(syIdStr); RSOut.PutStr(Str);}
void PutQStr(const TStr& Str){
PutSep(syQStr); RSOut.PutCh('"'); RSOut.PutStr(Str); RSOut.PutCh('"');}
void PutQStr(const TChA& ChA){
PutSep(syQStr); RSOut.PutCh('"'); RSOut.PutStr(ChA); RSOut.PutCh('"');}
void PutUQStr(const TStr& Str){
PutSep(syIdStr); RSOut.PutStr(Str);}
void PutLnCmt(const TStr& Str, const int& IndentLev=0){
Assert(IsCmtAlw); PutStr(" // "); PutStr(Str); PutLn(IndentLev);}
void PutParCmt(const TStr& Str){
Assert(IsCmtAlw); PutStr(" /* "); PutStr(Str); PutStr(" */ ");}
void PutIndent(const int& IndentLev){
RSOut.PutCh(' ', IndentLev*2);}
void PutTab() const {RSOut.PutCh(TCh::TabCh);}
void PutLn(const int& IndentLev=0){
Assert(IsFrcEoln);
PutSep(syEoln); RSOut.PutLn(); RSOut.PutCh(' ', IndentLev*2);}
void PutDosLn(const int& IndentLev=0){
Assert(IsFrcEoln);
PutSep(syEoln); RSOut.PutDosLn(); RSOut.PutCh(' ', IndentLev*2);}
void PutVar(const TStr& VarNm, const bool& LBracket=false,
const bool& NewLn=false, const bool& CheckIdStr=true){
if (IsVarIndent){PutIndent(VarIndentLev);}
PutIdStr(VarNm, CheckIdStr); PutSym(syColon);
if (LBracket){PutSym(syLBracket);}
if (NewLn){PutLn(); VarIndentLev++;}}
void PutVarEnd(const bool& RBracket=false, const bool& NewLn=false){
if (IsVarIndent){PutIndent(VarIndentLev-1);}
if (RBracket){PutSym(syRBracket);}
if (NewLn){PutLn(); VarIndentLev--;}}
void PutVarBool(const TStr& VarNm, const bool& Bool,
const bool& NewLn=true, const bool& CheckIdStr=true){
if (IsVarIndent){PutIndent(VarIndentLev);}
PutIdStr(VarNm, CheckIdStr); PutSym(syColon); PutBool(Bool);
if (NewLn){PutLn();}}
void PutVarInt(const TStr& VarNm, const int& Int,
const bool& NewLn=true, const bool& CheckIdStr=true){
if (IsVarIndent){PutIndent(VarIndentLev);}
PutIdStr(VarNm, CheckIdStr); PutSym(syColon); PutInt(Int);
if (NewLn){PutLn();}}
void PutVarFlt(const TStr& VarNm, const double& Flt,
const bool& NewLn=true, const bool& CheckIdStr=true){
if (IsVarIndent){PutIndent(VarIndentLev);}
PutIdStr(VarNm, CheckIdStr); PutSym(syColon); PutFlt(Flt);
if (NewLn){PutLn();}}
void PutVarStr(const TStr& VarNm, const TStr& Str,
const bool& NewLn=true, const bool& CheckIdStr=true){
if (IsVarIndent){PutIndent(VarIndentLev);}
PutIdStr(VarNm, CheckIdStr); PutSym(syColon); PutQStr(Str);
if (NewLn){PutLn();}}
void PutVarSecTm(const TStr& VarNm, const TSecTm& SecTm,
const bool& NewLn=true, const bool& CheckIdStr=true){
if (IsVarIndent){PutIndent(VarIndentLev);}
PutIdStr(VarNm, CheckIdStr); PutSym(syColon); SecTm.SaveTxt(*this);
if (NewLn){PutLn();}}
void PutVarBoolV(const TStr& VarNm, const TBoolV& BoolV,
const bool& NewLn=true, const bool& CheckIdStr=true);
void PutVarIntV(const TStr& VarNm, const TIntV& IntV,
const bool& NewLn=true, const bool& CheckIdStr=true);
void PutVarFltV(const TStr& VarNm, const TFltV& FltV,
const bool& NewLn=true, const bool& CheckIdStr=true);
void PutVarStrV(const TStr& VarNm, const TStrV& StrV,
const bool& NewLn=true, const bool& CheckIdStr=true);
void PutVarStrPrV(const TStr& VarNm, const TStrPrV& StrPrV,
const bool& NewLn=true, const bool& CheckIdStr=true);
void PutVarStrVV(const TStr& VarNm, const TVec<TStrV>& StrVV,
const bool& NewLn=true, const bool& CheckIdStr=true);
};
/////////////////////////////////////////////////
// Preprocessor
class TPreproc{
private:
PSIn SIn;
TStrV SubstKeyIdV;
char PrevCh, Ch;
THash<TStr, TStrPrV> SubstIdToKeyIdValPrVH;
char GetCh();
bool IsSubstId(const TStr& SubstId, TStr& SubstValStr) const;
UndefDefaultCopyAssign(TPreproc);
public:
TPreproc(const TStr& InFNm, const TStr& OutFNm,
const TStr& SubstFNm, const TStrV& _SubstKeyIdV);
static void Execute(const TStr& InFNm, const TStr& OutFNm,
const TStr& InSubstFNm, const TStrV& SubstKeyIdV){
TPreproc Preproc(InFNm, OutFNm, InSubstFNm, SubstKeyIdV);}
};
/* Sample Subst-File
<SubstList>
<Subst Id="TId">
<Str Key="MSSQL">TId</Str>
<Str Key="Oracle">NUMBER(15) NOT NULL</Str>
</Subst>
<Subst Id="TStr2NN">
<Str Key="MSSQL">TStr2NN</Str>
<Str Key="Oracle">VARCHAR2(2) NOT NULL</Str>
</Subst>
</SubstList>
*/