libosmscout
1.1.1
Toggle main menu visibility
Loading...
Searching...
No Matches
libosmscout
include
osmscout
ost
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
28
#include <
osmscout/system/SystemTypes.h
>
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
//-----------------------------------------------------------------------------------
82
class
StartStates
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
//-------------------------------------------------------------------------------------------
111
class
KeywordMap
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
SystemTypes.h
osmscout::ost::Buffer
Definition
Scanner.h:61
osmscout::ost::Buffer::Read
int Read()
osmscout::ost::Buffer::GetPos
int GetPos()
osmscout::ost::Buffer::EoF
static const int EoF
Definition
Scanner.h:68
osmscout::ost::Buffer::Peek
int Peek()
osmscout::ost::Buffer::SetPos
void SetPos(int value)
osmscout::ost::Buffer::Buffer
Buffer(const unsigned char *buf, size_t len)
osmscout::ost::Buffer::~Buffer
virtual ~Buffer()
osmscout::ost::KeywordMap
Definition
Scanner.h:112
osmscout::ost::KeywordMap::set
void set(const char *key, int val)
Definition
Scanner.h:120
osmscout::ost::KeywordMap::get
int get(const char *key, int defaultVal)
Definition
Scanner.h:125
osmscout::ost::KeywordMap::~KeywordMap
virtual ~KeywordMap()=default
osmscout::ost::KeywordMap::KeywordMap
KeywordMap()=default
osmscout::ost::Scanner::buffer
Buffer * buffer
Definition
Scanner.h:176
osmscout::ost::Scanner::Scan
TokenRef Scan()
osmscout::ost::Scanner::Peek
TokenRef Peek()
osmscout::ost::Scanner::SetScannerBehindT
void SetScannerBehindT()
osmscout::ost::Scanner::Scanner
Scanner(const unsigned char *buf, size_t len)
osmscout::ost::Scanner::~Scanner
~Scanner()
osmscout::ost::Scanner::ResetPeek
void ResetPeek()
osmscout::ost::StartStates
Definition
Scanner.h:83
osmscout::ost::StartStates::~StartStates
virtual ~StartStates()=default
osmscout::ost::StartStates::StartStates
StartStates()=default
osmscout::ost::StartStates::set
void set(int key, int val)
Definition
Scanner.h:91
osmscout::ost::StartStates::state
int state(int key)
Definition
Scanner.h:96
osmscout::ost::Token
Definition
Scanner.h:46
osmscout::ost::Token::val
char * val
Definition
Scanner.h:53
osmscout::ost::Token::col
int col
Definition
Scanner.h:51
osmscout::ost::Token::Token
Token()
osmscout::ost::Token::charPos
int charPos
Definition
Scanner.h:50
osmscout::ost::Token::kind
int kind
Definition
Scanner.h:48
osmscout::ost::Token::~Token
~Token()
osmscout::ost::Token::line
int line
Definition
Scanner.h:52
osmscout::ost::Token::pos
int pos
Definition
Scanner.h:49
osmscout::ost::Token::next
TokenRef next
Definition
Scanner.h:54
COCO_WCHAR_MAX
#define COCO_WCHAR_MAX
Definition
Scanner.h:34
osmscout::ost
Definition
Parser.h:34
osmscout::ost::coco_string_create
char * coco_string_create(const char *value)
osmscout::ost::TokenRef
std::shared_ptr< Token > TokenRef
Definition
Scanner.h:43
osmscout::ost::coco_string_delete
void coco_string_delete(char *&data)
osmscout
Definition
Area.h:39
Generated by
1.17.0