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_ost_SCANNER_H)
22 #define osmscout_ost_SCANNER_H
23 
24 #include <string>
25 #include <map>
26 #include <memory>
27 
29 
30 namespace osmscout {
31 namespace ost {
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 using TokenRef = std::shared_ptr<Token>;
44 
45 class Token
46 {
47 public:
48  int kind; // token kind
49  int pos; // token position in the source text (starting at 0)
50  int 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  int 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  int GetPos();
76  void SetPos(int value);
77 };
78 
79 //-----------------------------------------------------------------------------------
80 // StartStates -- maps characters to start states of tokens
81 //-----------------------------------------------------------------------------------
83 {
84 private:
85  std::map<int,int> map;
86 
87 public:
88  StartStates() = default;
89  virtual ~StartStates() = default;
90 
91  void set(int key, int val)
92  {
93  map[key]=val;
94  }
95 
96  int state(int key) {
97  std::map<int,int>::const_iterator iter=map.find(key);
98 
99  if (iter!=map.end()) {
100  return iter->second;
101  }
102  else {
103  return 0;
104  }
105  }
106 };
107 
108 //-------------------------------------------------------------------------------------------
109 // KeywordMap -- maps strings to integers (identifiers to keyword kinds)
110 //-------------------------------------------------------------------------------------------
112 {
113 private:
114  std::map<std::string,int> map;
115 
116 public:
117  KeywordMap() = default;
118  virtual ~KeywordMap() = default;
119 
120  void set(const char* key, int val)
121  {
122  map[std::string(key)]=val;
123  }
124 
125  int get(const char* key, int defaultVal)
126  {
127  std::map<std::string,int>::const_iterator iter=map.find(std::string(key));
128 
129  if (iter!=map.end()) {
130  return iter->second;
131  }
132  else {
133  return defaultVal;
134  }
135  }
136 };
137 
138 class Scanner
139 {
140 private:
141  unsigned char EOL;
142  int eofSym;
143  int noSym;
144  int maxT;
145  StartStates start;
146  KeywordMap keywords;
147 
148  TokenRef t; // current token
149  char *tval; // text of current token
150  size_t tvalLength; // length of text of current token
151  size_t tlen; // length of current token
152 
153  TokenRef tokens; // list of tokens already peeked (first token is a dummy)
154  TokenRef pt; // current peek token
155 
156  int ch; // current input character
157 
158  int pos; // byte position of current character
159  int charPos; // position by unicode characters starting with 0
160  int line; // line number of current character
161  int col; // column number of current character
162  int oldEols; // EOLs that appeared in a comment
163 
164  TokenRef CreateToken();
165  void AppendVal(TokenRef& t);
166 
167  void Init();
168  void NextCh();
169  void AddCh();
170  bool Comment0();
171  bool Comment1();
172 
173  TokenRef NextToken();
174 
175 public:
176  Buffer *buffer; // scanner buffer
177 
178  Scanner(const unsigned char* buf, size_t len);
179  ~Scanner();
180  TokenRef Scan();
181  void SetScannerBehindT();
182  TokenRef Peek();
183  void ResetPeek();
184 
185 }; // end Scanner
186 
187 } // namespace
188 } // namespace
189 
190 
191 #endif
192 
std::shared_ptr< Token > TokenRef
Definition: Scanner.h:43
void SetPos(int value)
Definition: Scanner.h:60
Definition: Scanner.h:138
Scanner(const unsigned char *buf, size_t len)
Definition: Scanner.h:111
Buffer * buffer
Definition: Scanner.h:176
int line
Definition: Scanner.h:52
virtual ~StartStates()=default
void coco_string_delete(char *&data)
static const int EoF
Definition: Scanner.h:68
Definition: Area.h:38
char * coco_string_create(const char *value)
int col
Definition: Scanner.h:51
int charPos
Definition: Scanner.h:50
int state(int key)
Definition: Scanner.h:96
char * val
Definition: Scanner.h:53
Definition: Scanner.h:82
Buffer(const unsigned char *buf, size_t len)
TokenRef next
Definition: Scanner.h:54
int kind
Definition: Scanner.h:48
#define COCO_WCHAR_MAX
Definition: Scanner.h:34
virtual ~KeywordMap()=default
Definition: Scanner.h:45
int pos
Definition: Scanner.h:49