libosmscout 1.1.1
Loading...
Searching...
No Matches
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
30namespace osmscout {
31namespace ost {
32
33
34#define COCO_WCHAR_MAX 65535
35
36// string handling, wide character
37extern char* coco_string_create(const char* value);
38extern char* coco_string_create(const char* value, int startIndex, int length);
39extern void coco_string_delete(char* &data);
40
41class Token;
42
43using TokenRef = std::shared_ptr<Token>;
44
45class Token
46{
47public:
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
58};
59
60class Buffer
61{
62private:
63 unsigned char *buf; // input buffer
64 size_t bufLen; // length of buffer
65 int bufPos; // current position in buffer
66
67public:
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{
84private:
85 std::map<int,int> map;
86
87public:
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{
113private:
114 std::map<std::string,int> map;
115
116public:
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
139{
140private:
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
175public:
176 Buffer *buffer; // scanner buffer
177
178 Scanner(const unsigned char* buf, size_t len);
183 void ResetPeek();
184
185}; // end Scanner
186
187} // namespace
188} // namespace
189
190
191#endif
192
Definition Scanner.h:61
static const int EoF
Definition Scanner.h:68
void SetPos(int value)
Buffer(const unsigned char *buf, size_t len)
Definition Scanner.h:112
void set(const char *key, int val)
Definition Scanner.h:120
int get(const char *key, int defaultVal)
Definition Scanner.h:125
virtual ~KeywordMap()=default
Buffer * buffer
Definition Scanner.h:176
Scanner(const unsigned char *buf, size_t len)
Definition Scanner.h:83
virtual ~StartStates()=default
void set(int key, int val)
Definition Scanner.h:91
int state(int key)
Definition Scanner.h:96
Definition Scanner.h:46
char * val
Definition Scanner.h:53
int col
Definition Scanner.h:51
int charPos
Definition Scanner.h:50
int kind
Definition Scanner.h:48
int line
Definition Scanner.h:52
int pos
Definition Scanner.h:49
TokenRef next
Definition Scanner.h:54
#define COCO_WCHAR_MAX
Definition Scanner.h:34
Definition Parser.h:34
char * coco_string_create(const char *value)
std::shared_ptr< Token > TokenRef
Definition Scanner.h:43
void coco_string_delete(char *&data)
Definition Area.h:39