libosmscout  1.1.1
Scanner.h
Go to the documentation of this file.
1 /*
2  This source is part of the libosmscout library
3  Copyright (C) 2011 Tim Teulings
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 
20 
21 #if !defined(osmscout_oss_SCANNER_H)
22 #define osmscout_oss_SCANNER_H
23 
24 #include <string>
25 #include <map>
26 #include <memory>
27 
29 
30 namespace osmscout {
31 namespace oss {
32 
33 
34 #define COCO_WCHAR_MAX 65535
35 
36 // string handling, wide character
37 extern char* coco_string_create(const char* value);
38 extern char* coco_string_create(const char* value, int startIndex, int length);
39 extern void coco_string_delete(char* &data);
40 
41 class Token;
42 
43 typedef std::shared_ptr<Token> TokenRef;
44 
45 class Token
46 {
47 public:
48  int kind; // token kind
49  size_t pos; // token position in the source text (starting at 0)
50  size_t charPos; // token position in characters in the source text (starting at 0)
51  int col; // token column (starting at 1)
52  int line; // token line (starting at 1)
53  char* val; // token value
54  TokenRef next; // ML 2005-03-11 Peek tokens are kept in linked list
55 
56  Token();
57  ~Token();
58 };
59 
60 class Buffer
61 {
62 private:
63  unsigned char *buf; // input buffer
64  size_t bufLen; // length of buffer
65  size_t bufPos; // current position in buffer
66 
67 public:
68  static const int EoF = COCO_WCHAR_MAX + 1;
69 
70  Buffer(const unsigned char* buf, size_t len);
71  virtual ~Buffer();
72 
73  int Read();
74  int Peek();
75  //wchar_t* GetString(int beg, int end);
76  size_t GetPos();
77  void SetPos(size_t value);
78 };
79 
80 //-----------------------------------------------------------------------------------
81 // StartStates -- maps characters to start states of tokens
82 //-----------------------------------------------------------------------------------
84 {
85 private:
86  std::map<int,int> map;
87 
88 public:
90  {
91  // no code
92  }
93 
94  virtual ~StartStates()
95  {
96  // no code
97  }
98 
99  void set(int key, int val)
100  {
101  map[key]=val;
102  }
103 
104  int state(int key) {
105  std::map<int,int>::const_iterator iter=map.find(key);
106 
107  if (iter!=map.end()) {
108  return iter->second;
109  }
110  else {
111  return 0;
112  }
113  }
114 };
115 
116 //-------------------------------------------------------------------------------------------
117 // KeywordMap -- maps strings to integers (identifiers to keyword kinds)
118 //-------------------------------------------------------------------------------------------
120 {
121 private:
122  std::map<std::string,int> map;
123 
124 public:
126  {
127  // no code
128  }
129 
130  virtual ~KeywordMap()
131  {
132  // no code
133  }
134 
135  void set(const char* key, int val)
136  {
137  map[std::string(key)]=val;
138  }
139 
140  int get(const char* key, int defaultVal)
141  {
142  std::map<std::string,int>::const_iterator iter=map.find(std::string(key));
143 
144  if (iter!=map.end()) {
145  return iter->second;
146  }
147  else {
148  return defaultVal;
149  }
150  }
151 };
152 
153 class Scanner
154 {
155 private:
156  unsigned char EOL;
157  int eofSym;
158  int noSym;
159  int maxT;
160  StartStates start;
161  KeywordMap keywords;
162 
163  TokenRef t; // current token
164  char *tval; // text of current token
165  size_t tvalLength; // length of text of current token
166  size_t tlen; // length of current token
167 
168  TokenRef tokens; // list of tokens already peeked (first token is a dummy)
169  TokenRef pt; // current peek token
170 
171  int ch; // current input character
172 
173  size_t pos; // byte position of current character
174  size_t charPos; // position by unicode characters starting with 0
175  int line; // line number of current character
176  int col; // column number of current character
177  int oldEols; // EOLs that appeared in a comment;
178 
179  TokenRef CreateToken();
180  void AppendVal(TokenRef& t);
181 
182  void Init();
183  void NextCh();
184  void AddCh();
185  bool Comment0();
186  bool Comment1();
187 
188  TokenRef NextToken();
189 
190 public:
191  Buffer *buffer; // scanner buffer
192 
193  Scanner(const unsigned char* buf, size_t len);
194  ~Scanner();
195  TokenRef Scan();
196  void SetScannerBehindT();
197  TokenRef Peek();
198  void ResetPeek();
199 
200 }; // end Scanner
201 
202 } // namespace
203 } // namespace
204 
205 
206 #endif
207 
Buffer * buffer
Definition: Scanner.h:191
int line
Definition: Scanner.h:52
void SetPos(size_t value)
KeywordMap()
Definition: Scanner.h:125
Scanner(const unsigned char *buf, size_t len)
char * coco_string_create(const char *value)
#define COCO_WCHAR_MAX
Definition: Scanner.h:34
int col
Definition: Scanner.h:51
std::shared_ptr< Token > TokenRef
Definition: Scanner.h:41
virtual ~StartStates()
Definition: Scanner.h:94
static const int EoF
Definition: Scanner.h:68
void coco_string_delete(char *&data)
Definition: Area.h:38
Definition: Scanner.h:153
Definition: Scanner.h:119
Definition: Scanner.h:60
virtual ~KeywordMap()
Definition: Scanner.h:130
StartStates()
Definition: Scanner.h:89
int kind
Definition: Scanner.h:48
Buffer(const unsigned char *buf, size_t len)
Definition: Scanner.h:45
TokenRef next
Definition: Scanner.h:54
char * val
Definition: Scanner.h:53
int state(int key)
Definition: Scanner.h:104
size_t charPos
Definition: Scanner.h:50
Definition: Scanner.h:83
size_t pos
Definition: Scanner.h:49