WSF
wizard/lib/source/Us/Scanner.h
Go to the documentation of this file.
1// ****************************************************************************
2// CUI
3//
4// The Advanced Framework for Simulation, Integration, and Modeling (AFSIM)
5//
6// Copyright 2003-2015 The Boeing Company. All rights reserved.
7//
8// The use, dissemination or disclosure of data in this file is subject to
9// limitation or restriction. See accompanying README and LICENSE for details.
10// ****************************************************************************
11
12// NOTE: DO NOT HAND-EDIT THIS FILE! It is generated by the COCO/R compiler generator.
13// See wizard/lib/source/Us/AAA_README.TXT.
14
15#if !defined(Us_COCO_SCANNER_H__)
16#define Us_COCO_SCANNER_H__
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22#include <limits.h>
23
24// io.h and fcntl are used to ensure binary read from streams on windows
25#if _MSC_VER >= 1300
26#include <fcntl.h>
27#include <io.h>
28#endif
29
30#define COCO_USE_WCHAR_T 0
31#if COCO_USE_WCHAR_T
32struct COCO_Str
33{
34 typedef wchar_t char_type;
35 static wchar_t* ncpy(wchar_t* dest, const wchar_t* src, size_t n) { return wcsncpy(dest, src, n); }
36 static wchar_t* cpy(wchar_t* dest, const wchar_t* src) { return wcscpy(dest, src); }
37 static int len(const wchar_t* s) { return (int)wcslen(s); }
38 static int cmp(const wchar_t* s1, const wchar_t* s2) { return wcscmp(s1, s2); }
39 static const wchar_t* rchr(const wchar_t* wcs, wchar_t wc) { return wcsrchr(wcs, wc); }
40 static wchar_t* rchr(wchar_t* wcs, wchar_t wc) { return wcsrchr(wcs, wc); }
41 static const wchar_t* chr(const wchar_t* wcs, wchar_t wc) { return wcschr(wcs, wc); }
42 static wchar_t* chr(wchar_t* wcs, wchar_t wc) { return wcschr(wcs, wc); }
43};
44#if _MSC_VER >= 1400
45#define coco_sprintf swprintf_s
46#elif _MSC_VER >= 1300
47#define coco_sprintf _snwprintf
48#elif defined __MINGW32__
49#define coco_sprintf _snwprintf
50#else
51// assume every other compiler knows swprintf
52#define coco_sprintf swprintf
53#endif
54#define COCO_STR(X) L##X
55#define coco_printf wprintf
56#else // NOT use wchar_t
58{
59 typedef char char_type;
60 static char* ncpy(char* dest, const char* src, size_t n) { return strncpy(dest, src, n); }
61 static char* cpy(char* dest, const char* src) { return strcpy(dest, src); }
62 static int len(const char* s) { return (int)strlen(s); }
63 static int cmp(const char* s1, const char* s2) { return strcmp(s1, s2); }
64 static const char* rchr(const char* wcs, char wc) { return strrchr(wcs, wc); }
65 static char* rchr(char* wcs, char wc) { return strrchr(wcs, wc); }
66 static const char* chr(const char* wcs, char wc) { return strchr(wcs, wc); }
67 static char* chr(char* wcs, char wc) { return strchr(wcs, wc); }
68};
69
70#define coco_sprintf sprintf
71#define coco_snprintf snprintf
72#define COCO_STR(X) X
73#define coco_printf printf
74#endif
75
77
78#define COCO_WCHAR_MAX 65535
79#define COCO_MIN_BUFFER_LENGTH 1024
80#define COCO_MAX_BUFFER_LENGTH (64 * COCO_MIN_BUFFER_LENGTH)
81#define COCO_HEAP_BLOCK_SIZE (64 * 1024)
82#define COCO_CPP_NAMESPACE_SEPARATOR COCO_STR(':')
83
84namespace Us
85{
86
87// string handling, wide character
89cocochar_t* coco_string_create(const cocochar_t* value, int startIndex);
90cocochar_t* coco_string_create(const cocochar_t* value, int startIndex, int length);
93cocochar_t* coco_string_create_lower(const cocochar_t* data, int startIndex, int dataLen);
97int coco_string_length(const cocochar_t* data);
98bool coco_string_endswith(const cocochar_t* data, const cocochar_t* value);
99int coco_string_indexof(const cocochar_t* data, const cocochar_t value);
100int coco_string_lastindexof(const cocochar_t* data, const cocochar_t value);
101void coco_string_merge(cocochar_t*& data, const cocochar_t* value);
102bool coco_string_equal(const cocochar_t* data1, const cocochar_t* data2);
103int coco_string_compareto(const cocochar_t* data1, const cocochar_t* data2);
104unsigned int coco_string_hash(const cocochar_t* data);
105
106// string handling, ascii character
107#if COCO_USE_WCHAR_T
108cocochar_t* coco_string_create(const char* value);
109void coco_string_delete(char*& data);
110#endif
111char* coco_string_create_char(const cocochar_t* value);
112
113class Token
114{
115public:
116 Token();
117 virtual ~Token();
118
119 int kind{0}; // token kind
120 int pos{0}; // token position in bytes in the source text (starting at 0)
121 int charPos{0}; // token position in characters in the source text (starting at 0)
122 int col{0}; // token column (starting at 1)
123 int line{0}; // token line (starting at 1)
124 cocochar_t* val{nullptr}; // token value
125 Token* next{nullptr}; // ML 2005-03-11 Peek tokens are kept in linked list
126 int len{0};
127};
128#define COCO_USE_INLINE_BUFFER 1
129#if COCO_USE_INLINE_BUFFER
130// Support for utf8 increases parse time, particularly because Read() is virtual.
131// This is an alternate implementation of Buffer which does not have virtual methods,
132// and only supports in-memory multi-byte files
133class Buffer
134{
135public:
136 static const int EoF = COCO_WCHAR_MAX + 1;
137
138 Buffer(const Buffer& aSrc) = delete;
139 Buffer& operator=(const Buffer& aRhs) = delete;
140
141 Buffer(const unsigned char* buf, int len)
142 {
143 this->buf = new unsigned char[len];
144 memcpy(this->buf, buf, len * sizeof(unsigned char));
145 bufCapacity = bufLen = len;
146 fileLen = len;
147 bufPos = 0;
148 }
149 Buffer(Buffer* b);
150 ~Buffer()
151 {
152 if (buf != nullptr)
153 {
154 delete[] buf;
155 buf = nullptr;
156 }
157 }
158
159 void Close() {}
160 int Read()
161 {
162 if (bufPos < bufLen)
163 {
164 return buf[bufPos++];
165 }
166 else
167 {
168 return EoF;
169 }
170 }
171 int Peek()
172 {
173 int curPos = GetPos();
174 int ch = Read();
175 SetPos(curPos);
176 return ch;
177 }
178
179 // beg .. begin, zero-based, inclusive, in byte
180 // end .. end, zero-based, exclusive, in byte
181 cocochar_t* GetString(int beg, int end)
182 {
183 int len = 0;
184 cocochar_t* buffer = new cocochar_t[end - beg];
185 int oldPos = GetPos();
186 SetPos(beg);
187 while (GetPos() < end)
188 buffer[len++] = (cocochar_t)Read();
189 SetPos(oldPos);
190 cocochar_t* res = coco_string_create(buffer, 0, len);
191 coco_string_delete(buffer);
192 return res;
193 }
194 int GetPos() { return bufPos; }
195 void SetPos(int value)
196 {
197 if ((value < 0) || (value > fileLen))
198 {
199 coco_printf(COCO_STR("--- buffer out of bounds access, position: %d\n"), value);
200 exit(1);
201 }
202 if ((value >= 0) && (value < (bufLen))) // already in buffer
203 {
204 bufPos = value;
205 }
206 else
207 {
208 bufPos = fileLen; // make Pos return fileLen
209 }
210 }
211
212private:
213 unsigned char* buf;
214 int bufCapacity; // capacity of buf
215 int bufLen; // length of buffer
216 int fileLen; // length of input stream (may change if the stream is no file)
217 int bufPos; // current position in buffer
218};
219
220#else
222{
223 // This Buffer supports the following cases:
224 // 1) seekable stream (file)
225 // a) whole stream in buffer
226 // b) part of stream in buffer
227 // 2) non seekable stream (network, console)
228public:
229 static const int EoF = COCO_WCHAR_MAX + 1;
230
231 Buffer(const Buffer& aSrc) = delete;
232 Buffer& operator=(const Buffer& aRhs) = delete;
233
234 Buffer(FILE* s, bool isUserStream);
235 Buffer(const unsigned char* buf, int len);
236 Buffer(Buffer* b);
237 virtual ~Buffer();
238
239 virtual void Close();
240 virtual int Read();
241 virtual int Peek();
242 virtual cocochar_t* GetString(int beg, int end);
243 virtual int GetPos();
244 virtual void SetPos(int value);
245
246private:
247 unsigned char* buf; // input buffer
248 int bufCapacity; // capacity of buf
249 int bufStart; // position of first byte in buffer relative to input stream
250 int bufLen; // length of buffer
251 int fileLen; // length of input stream (may change if the stream is no file)
252 int bufPos; // current position in buffer
253 FILE* stream; // input stream (seekable)
254 bool isUserStream; // was the stream opened by the user?
255
256 int ReadNextStreamChunk();
257 bool CanSeek(); // true if stream can be seeked otherwise false
258};
259
260class UTF8Buffer : public Buffer
261{
262public:
264 : Buffer(b){};
265 virtual int Read();
266};
267#endif
268
269//-----------------------------------------------------------------------------------
270// StartStates -- maps characters to start states of tokens
271//-----------------------------------------------------------------------------------
273{
274public:
276 {
277 tab = new Elem*[128];
278 memset(tab, 0, 128 * sizeof(Elem*));
279 }
280 virtual ~StartStates()
281 {
282 for (int i = 0; i < 128; ++i)
283 {
284 Elem* e = tab[i];
285 while (e != nullptr)
286 {
287 Elem* next = e->next;
288 delete e;
289 e = next;
290 }
291 }
292 delete[] tab;
293 }
294
295 StartStates(const StartStates& aSrc) = delete;
296 StartStates& operator=(const StartStates& aRhs) = delete;
297
298 void set(int key, int val)
299 {
300 Elem* e = new Elem(key, val);
301 int k = ((unsigned int)key) % 128;
302 e->next = tab[k];
303 tab[k] = e;
304 }
305
306 int state(int key)
307 {
308 Elem* e = tab[((unsigned int)key) % 128];
309 while (e != nullptr && e->key != key)
310 e = e->next;
311 return e == nullptr ? 0 : e->val;
312 }
313
314private:
315 class Elem
316 {
317 public:
318 int key, val;
319 Elem* next;
320 Elem(int key, int val)
321 {
322 this->key = key;
323 this->val = val;
324 next = nullptr;
325 }
326 };
327
328 Elem** tab;
329};
330
331//-------------------------------------------------------------------------------------------
332// KeywordMap -- maps strings to integers (identifiers to keyword kinds)
333//-------------------------------------------------------------------------------------------
335{
336public:
338 {
339 tab = new Elem*[128];
340 memset(tab, 0, 128 * sizeof(Elem*));
341 }
342 virtual ~KeywordMap()
343 {
344 for (int i = 0; i < 128; ++i)
345 {
346 Elem* e = tab[i];
347 while (e != nullptr)
348 {
349 Elem* next = e->next;
350 delete e;
351 e = next;
352 }
353 }
354 delete[] tab;
355 }
356
357 KeywordMap(const KeywordMap& aSrc) = delete;
358 KeywordMap& operator=(const KeywordMap& aRhs) = delete;
359
360 void set(const cocochar_t* key, int val)
361 {
362 Elem* e = new Elem(key, val);
363 unsigned int k = coco_string_hash(key) % 128;
364 e->next = tab[k];
365 tab[k] = e;
366 }
367
368 int get(const cocochar_t* key, int defaultVal)
369 {
370 Elem* e = tab[coco_string_hash(key) % 128];
371 while (e != nullptr && !coco_string_equal(e->key, key))
372 e = e->next;
373 return e == nullptr ? defaultVal : e->val;
374 }
375
376private:
377 class Elem
378 {
379 public:
380 cocochar_t* key;
381 int val;
382 Elem* next;
383 Elem(const cocochar_t* key, int val)
384 {
385 this->key = coco_string_create(key);
386 this->val = val;
387 next = nullptr;
388 }
389 virtual ~Elem() { coco_string_delete(key); }
390 };
391
392 Elem** tab;
393};
394
396{
397public:
398 Scanner(const unsigned char* buf, int len);
399#if !COCO_USE_INLINE_BUFFER
400 Scanner(const cocochar_t* fileName);
401 Scanner(FILE* s);
402#endif
403 virtual ~Scanner();
404
405 Scanner(const Scanner& aSrc) = delete;
406 Scanner& operator=(const Scanner& aRhs) = delete;
407
408 Token* Scan();
409 Token* Peek();
410 void ResetPeek();
411
412 void (*NewTokCb)(Token*, void*);
414
415 Buffer* buffer{nullptr}; // scanner buffer
416
417private:
418 void* firstHeap;
419 void* heap;
420 void* heapTop;
421 void** heapEnd;
422
423 unsigned char EOL;
424 int eofSym{0};
425 int noSym{0};
426 int maxT{0};
427 StartStates start;
428 KeywordMap keywords;
429
430 Token* t{nullptr}; // current token
431 cocochar_t* tval{nullptr}; // text of current token
432 int tvalLength{0}; // length of text of current token
433 int tlen{0}; // length of current token
434
435 Token* tokens{nullptr}; // list of tokens already peeked (first token is a dummy)
436 Token* pt{nullptr}; // current peek token
437
438 int ch{0}; // current input character
439 // ***Casing Begin
440
441 // ***Casing End
442 int pos{0}; // byte position of current character
443 int charPos{0}; // position by unicode characters starting with 0
444 int line{0}; // line number of current character
445 int col{0}; // column number of current character
446 int oldEols{0}; // EOLs that appeared in a comment;
447
448 void CreateHeapBlock();
449 Token* CreateToken();
450 void AppendVal(Token* t);
451 void SetScannerBehindT();
452
453 void Init();
454 void NextCh();
455 void AddCh();
456 Token* NextToken();
457
458 // ***Comments Begin
459 bool Comment0();
460 bool Comment1();
461 bool Comment2();
462
463 // ***Comments End
464
465}; // end Scanner
466
467} // namespace Us
468
469#endif
WsfComponentListT< T >::Iterator end(const WsfComponentListT< T > &aList)
Definition WsfComponentList.hpp:546
Definition wizard/lib/source/Us/Scanner.h:222
virtual int Peek()
Definition wizard/lib/source/Us/Scanner.cpp:386
virtual int Read()
Definition wizard/lib/source/Us/Scanner.cpp:365
virtual void Close()
Definition wizard/lib/source/Us/Scanner.cpp:356
Buffer & operator=(const Buffer &aRhs)=delete
static const int EoF
Definition wizard/lib/source/Us/Scanner.h:229
virtual void SetPos(int value)
Definition wizard/lib/source/Us/Scanner.cpp:415
virtual cocochar_t * GetString(int beg, int end)
Definition wizard/lib/source/Us/Scanner.cpp:396
Buffer(const Buffer &aSrc)=delete
virtual ~Buffer()
Definition wizard/lib/source/Us/Scanner.cpp:346
virtual int GetPos()
Definition wizard/lib/source/Us/Scanner.cpp:410
virtual ~KeywordMap()
Definition wizard/lib/source/Us/Scanner.h:342
int get(const cocochar_t *key, int defaultVal)
Definition wizard/lib/source/Us/Scanner.h:368
KeywordMap()
Definition wizard/lib/source/Us/Scanner.h:337
void set(const cocochar_t *key, int val)
Definition wizard/lib/source/Us/Scanner.h:360
KeywordMap & operator=(const KeywordMap &aRhs)=delete
KeywordMap(const KeywordMap &aSrc)=delete
Token * Scan()
Definition wizard/lib/source/Us/Scanner.cpp:1504
Token * Peek()
Definition wizard/lib/source/Us/Scanner.cpp:1518
Scanner(const Scanner &aSrc)=delete
void * mNewTokenDataPtr
Definition wizard/lib/source/Us/Scanner.h:413
void ResetPeek()
Definition wizard/lib/source/Us/Scanner.cpp:1533
void(* NewTokCb)(Token *, void *)
Definition wizard/lib/source/Us/Scanner.h:412
Scanner(const unsigned char *buf, int len)
Definition wizard/lib/source/Us/Scanner.cpp:531
virtual ~Scanner()
Definition wizard/lib/source/Us/Scanner.cpp:559
Scanner & operator=(const Scanner &aRhs)=delete
Buffer * buffer
Definition wizard/lib/source/Us/Scanner.h:415
StartStates(const StartStates &aSrc)=delete
virtual ~StartStates()
Definition wizard/lib/source/Us/Scanner.h:280
int state(int key)
Definition wizard/lib/source/Us/Scanner.h:306
StartStates()
Definition wizard/lib/source/Us/Scanner.h:275
StartStates & operator=(const StartStates &aRhs)=delete
void set(int key, int val)
Definition wizard/lib/source/Us/Scanner.h:298
Definition wizard/lib/source/Us/Scanner.h:114
int col
Definition wizard/lib/source/Us/Scanner.h:122
virtual ~Token()
Definition wizard/lib/source/Us/Scanner.cpp:285
Token()
Definition wizard/lib/source/Us/Scanner.cpp:283
int len
Definition wizard/lib/source/Us/Scanner.h:126
Token * next
Definition wizard/lib/source/Us/Scanner.h:125
cocochar_t * val
Definition wizard/lib/source/Us/Scanner.h:124
int line
Definition wizard/lib/source/Us/Scanner.h:123
int kind
Definition wizard/lib/source/Us/Scanner.h:119
int pos
Definition wizard/lib/source/Us/Scanner.h:120
int charPos
Definition wizard/lib/source/Us/Scanner.h:121
UTF8Buffer(Buffer *b)
Definition wizard/lib/source/Us/Scanner.h:263
virtual int Read()
Definition wizard/lib/source/Us/Scanner.cpp:484
#define COCO_WCHAR_MAX
Definition core/wsf_parser/source/grammar_parse/Scanner.h:42
Definition wizard/lib/source/Us/Parser.cpp:18
char * coco_string_create_char(const cocochar_t *value)
Definition wizard/lib/source/Us/Scanner.cpp:271
int coco_string_length(const cocochar_t *data)
Definition wizard/lib/source/Us/Scanner.cpp:171
int coco_string_lastindexof(const cocochar_t *data, const cocochar_t value)
Definition wizard/lib/source/Us/Scanner.cpp:198
unsigned int coco_string_hash(const cocochar_t *data)
Definition wizard/lib/source/Us/Scanner.cpp:230
int coco_string_compareto(const cocochar_t *data1, const cocochar_t *data2)
Definition wizard/lib/source/Us/Scanner.cpp:225
cocochar_t * coco_string_create_upper(const cocochar_t *data)
Definition wizard/lib/source/Us/Scanner.cpp:58
cocochar_t * coco_string_create(const cocochar_t *value)
Definition wizard/lib/source/Us/Scanner.cpp:24
cocochar_t * coco_string_create_lower(const cocochar_t *data)
Definition wizard/lib/source/Us/Scanner.cpp:89
bool coco_string_equal(const cocochar_t *data1, const cocochar_t *data2)
Definition wizard/lib/source/Us/Scanner.cpp:220
void coco_string_merge(cocochar_t *&target, const cocochar_t *appendix)
Definition wizard/lib/source/Us/Scanner.cpp:209
cocochar_t * coco_string_create_append(const cocochar_t *data1, const cocochar_t *data2)
Definition wizard/lib/source/Us/Scanner.cpp:124
int coco_string_indexof(const cocochar_t *data, const cocochar_t value)
Definition wizard/lib/source/Us/Scanner.cpp:187
bool coco_string_endswith(const cocochar_t *data, const cocochar_t *end)
Definition wizard/lib/source/Us/Scanner.cpp:180
void coco_string_delete(cocochar_t *&data)
Definition wizard/lib/source/Us/Scanner.cpp:165
Definition wizard/lib/source/Us/Scanner.h:58
static int cmp(const char *s1, const char *s2)
Definition wizard/lib/source/Us/Scanner.h:63
static int len(const char *s)
Definition wizard/lib/source/Us/Scanner.h:62
char char_type
Definition wizard/lib/source/Us/Scanner.h:59
static char * chr(char *wcs, char wc)
Definition wizard/lib/source/Us/Scanner.h:67
static char * ncpy(char *dest, const char *src, size_t n)
Definition wizard/lib/source/Us/Scanner.h:60
static const char * rchr(const char *wcs, char wc)
Definition wizard/lib/source/Us/Scanner.h:64
static const char * chr(const char *wcs, char wc)
Definition wizard/lib/source/Us/Scanner.h:66
static char * cpy(char *dest, const char *src)
Definition wizard/lib/source/Us/Scanner.h:61
static char * rchr(char *wcs, char wc)
Definition wizard/lib/source/Us/Scanner.h:65
COCO_Str::char_type cocochar_t
Definition wizard/lib/source/Us/Scanner.h:76
#define COCO_STR(X)
Definition wizard/lib/source/Us/Scanner.h:72
#define coco_printf
Definition wizard/lib/source/Us/Scanner.h:73
Copyrights Multiple, All Rights Reserved