libosmscout  1.1.1
Parser.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_PARSER_H)
22 #define osmscout_oss_PARSER_H
23 
24 #include <limits>
25 #include <list>
26 #include <sstream>
27 
28 #include <osmscout/Pixel.h>
29 
30 #include <osmscout/TypeConfig.h>
31 
32 #include <osmscout/util/String.h>
34 
35 
36 #include <osmscout/oss/Scanner.h>
37 
38 #include <osmscout/StyleConfig.h>
39 
40 #ifdef CONST
41  #undef CONST
42 #endif
43 
44 namespace osmscout {
45 namespace oss {
46 
47 
48 class Errors
49 {
50 public:
51  class Err
52  {
53  public:
54  enum Type {
59  };
60 
61  public:
63  int line;
64  int column;
65  std::string text;
66  };
67 
68 public:
69  std::list<Err> errors;
70  bool hasErrors;
71 
72 public:
73  Errors();
74  void SynErr(int line, int col, int n);
75  void Error(int line, int col, const char *s);
76  void Warning(int line, int col, const char *s);
77  void Warning(const char *s);
78  void Exception(const char *s);
79 
80 };
81 
82 class Parser
83 {
84 private:
85  enum {
86  _EOF=0,
87  _ident=1,
88  _number=2,
89  _double=3,
90  _color=4,
91  _variable=5,
92  _string=6
93  };
94  int maxT;
95 
96  TokenRef dummyToken;
97  int errDist;
98  int minErrDist;
99  Scanner *scanner;
100  TokenRef t; // last recognized token
101  TokenRef la; // lookahead token
102 
103  osmscout::ColorPostprocessor colorPostprocessor;
104 
105  void SynErr(int n);
106 
107  void Get();
108  void Expect(int n);
109  bool StartOf(int s);
110  void ExpectWeak(int n, int follow);
111  bool WeakSeparator(int n, int syFol, int repFol);
112 
113  osmscout::Color PostprocessColor(const osmscout::Color& color) const;
114 
115 public:
117 
120 bool state;
121 
122 enum class ValueType
123 {
124  NO_VALUE,
125  IDENT,
126  STRING,
127  COLOR,
128  NUMBER,
129  CONSTANT
130 };
131 
132 std::string Destring(const char* str)
133 {
134  std::string result(str);
135 
136  if (result.length()>=2 &&
137  result[0]=='"' &&
138  result[result.length()-1]=='"') {
139  result=result.substr(1,result.length()-2);
140  }
141 
142  return result;
143 }
144 
145 bool StringToDouble(const char* string, double& value)
146 {
147  std::istringstream buffer(string);
148 
149  buffer.imbue(std::locale::classic());
150 
151  buffer >> value;
152 
153  return !buffer.fail() && !buffer.bad() && buffer.eof();
154 }
155 
156 size_t GetHexDigitValue(char c)
157 {
158  if (c>='0' && c<='9') {
159  return c-'0';
160  }
161  else if (c>='a' && c<='f') {
162  return 10+(c-'a');
163  }
164 
165  assert(false);
166  return 0;
167 }
168 
170  const std::string& featureName,
171  const std::string& flagName,
172  TypeInfoSet& resultTypes)
173 {
174  FeatureRef feature=config.GetTypeConfig()->GetFeature(featureName);
175 
176  if (!feature) {
177  std::string e="Unknown feature '"+featureName+"'";
178 
179  SemErr(e.c_str());
180  return;
181  }
182 
183  size_t flagIndex=std::numeric_limits<size_t>::max();
184 
185  if (!flagName.empty() &&
186  !feature->GetFlagIndex(flagName,
187  flagIndex)) {
188  std::string e="Unknown feature flag '"+featureName+"."+flagName+"'";
189 
190  SemErr(e.c_str());
191  return;
192  }
193 
194  for (const auto& type : config.GetTypeConfig()->GetTypes()) {
195  if (type->HasFeature(featureName)) {
196  if (!filter.FiltersByType() ||
197  filter.HasType(type)) {
198  // Add type only if the filter either has no types or
199  // if the the type is already filtered
200  resultTypes.Set(type);
201  }
202  }
203  }
204 
205  if (!resultTypes.Empty()) {
206  size_t featureFilterIndex=config.GetFeatureFilterIndex(*feature);
207 
208  filter.AddFeature(featureFilterIndex,
209  flagIndex);
210  }
211 }
212 
213 
214 
215  Parser(Scanner *scanner,
217  osmscout::ColorPostprocessor colorPostprocessor=nullptr);
218  ~Parser();
219 
220  void SemErr(const char* msg);
221  void SemWarning(const char* msg);
222 
223  void OSS();
224  void FLAGSECTION();
225  void WAYORDER();
226  void CONSTSECTION();
227  void SYMBOLSECTION();
228  void STYLESECTION();
229  void FLAGBLOCK(bool state);
230  void FLAGDEF();
231  void FLAGCONDBLOCK(bool state);
232  void IFCOND(bool& state);
233  void IDENT(std::string& value);
234  void BOOL(bool& value);
235  void WAYGROUP(size_t priority);
236  void POLYGON(Symbol& symbol);
237  void RECTANGLE(Symbol& symbol);
238  void CIRCLE(Symbol& symbol);
239  void AREAFILLSYMSTYLE(FillPartialStyle& fillStyle);
240  void FILLSTYLEATTR(FillPartialStyle& style);
241  void AREABORDERSYMSTYLE(BorderPartialStyle& borderStyle);
242  void BORDERSTYLEATTR(BorderPartialStyle& style);
243  void AREASYMBOLSTYLE(FillPartialStyle& fillStyle, BorderPartialStyle& borderStyle);
244  void COORD(Vertex2D& coord);
245  void UDOUBLE(double& value);
246  void DOUBLE(double& value);
247  void CONSTBLOCK(bool state);
248  void CONSTCONDBLOCK(bool state);
249  void CONSTDEF();
250  void COLORCONSTDEF();
251  void MAGCONSTDEF();
252  void UINTCONSTDEF();
253  void WIDTHCONSTDEF();
254  void COLOR(Color& color);
255  void MAG(Magnification& magnification);
256  void UINT(size_t& value);
257  void STYLEBLOCK(StyleFilter filter, bool state);
258  void STYLE(StyleFilter filter, bool state);
259  void STYLECONDBLOCK(StyleFilter filter, bool state);
260  void STYLEFILTER(StyleFilter& filter);
261  void STYLEDEF(StyleFilter filter, bool state);
262  void STYLEFILTER_GROUP(StyleFilter& filter);
263  void STYLEFILTER_FEATURE(StyleFilter& filter);
264  void STYLEFILTER_PATH(StyleFilter& filter);
265  void STYLEFILTER_TYPE(StyleFilter& filter);
266  void STYLEFILTER_MAG(StyleFilter& filter);
267  void STYLEFILTER_ONEWAY(StyleFilter& filter);
268  void STYLEFILTER_SIZE(StyleFilter& filter);
269  void STYLEFILTER_FEATURE_ENTRY(StyleFilter& filter, TypeInfoSet& types);
270  void SIZECONDITION(SizeConditionRef& condition);
271  void UMAP(double& width);
272  void NODESTYLEDEF(StyleFilter filter, bool state);
273  void WAYSTYLEDEF(StyleFilter filter, bool state);
274  void AREASTYLEDEF(StyleFilter filter, bool state);
275  void ROUTESTYLEDEF(StyleFilter filter, bool state);
276  void NODETEXTSTYLE(StyleFilter filter, bool state);
277  void NODEICONSTYLE(StyleFilter filter, bool state);
278  void TEXTSTYLEATTR(TextPartialStyle& style);
279  void ICONSTYLEATTR(IconPartialStyle& style);
280  void WAYSTYLE(StyleFilter filter, bool state);
281  void WAYPATHTEXTSTYLE(StyleFilter filter, bool state);
282  void WAYPATHSYMBOLSTYLE(StyleFilter filter, bool state);
283  void WAYSHIELDSTYLE(StyleFilter filter, bool state);
284  void LINESTYLEATTR(LinePartialStyle& style);
285  void ROUTEPATHTEXTSTYLE(StyleFilter filter, bool state);
289  void AREASTYLE(StyleFilter filter, bool state);
290  void AREATEXTSTYLE(StyleFilter filter, bool state);
291  void AREAICONSTYLE(StyleFilter filter, bool state);
292  void AREABORDERSTYLE(StyleFilter filter, bool state);
293  void AREABORDERTEXTSTYLE(StyleFilter filter, bool state);
294  void AREABORDERSYMBOLSTYLE(StyleFilter filter, bool state);
295  void ROUTESTYLE(StyleFilter filter, bool state);
296  void ATTRIBUTE(PartialStyleBase& style, const StyleDescriptor& descriptor);
297  void ATTRIBUTEVALUE(PartialStyleBase& style, const StyleAttributeDescriptor& descriptor);
298  void COLOR_VALUE(Color& color);
299  void CONSTANT(StyleConstantRef& constant);
300  void STRING(std::string& value);
301 
302  void Parse();
303 
304 };
305 
306 } // namespace
307 } // namespace
308 
309 
310 #endif // !defined(COCO_PARSER_H__)
311 
void SIZECONDITION(SizeConditionRef &condition)
void CIRCLE(Symbol &symbol)
void AREAFILLSYMSTYLE(FillPartialStyle &fillStyle)
void PATHTEXTSTYLEATTR(PathTextPartialStyle &style)
void STYLEFILTER_FEATURE_ENTRY(StyleFilter &filter, TypeInfoSet &types)
void STYLEDEF(StyleFilter filter, bool state)
void STYLEFILTER_MAG(StyleFilter &filter)
void TEXTSTYLEATTR(TextPartialStyle &style)
void WAYPATHSYMBOLSTYLE(StyleFilter filter, bool state)
StyleFilter & AddFeature(size_t featureFilterIndex, size_t flagIndex)
void ATTRIBUTEVALUE(PartialStyleBase &style, const StyleAttributeDescriptor &descriptor)
void STYLEBLOCK(StyleFilter filter, bool state)
Definition: Parser.h:56
void FLAGCONDBLOCK(bool state)
osmscout::Color(*)(const osmscout::Color &) ColorPostprocessor
Definition: StyleConfig.h:52
Index selectors by type and level.
Definition: StyleConfig.h:553
void NODESTYLEDEF(StyleFilter filter, bool state)
void AREASTYLEDEF(StyleFilter filter, bool state)
void STYLEFILTER_PATH(StyleFilter &filter)
void STYLEFILTER(StyleFilter &filter)
void ROUTEPATHTEXTSTYLE(StyleFilter filter, bool state)
Definition: Parser.h:48
void MAG(Magnification &magnification)
std::shared_ptr< Token > TokenRef
Definition: Scanner.h:41
void BOOL(bool &value)
bool StringToDouble(const char *string, double &value)
Definition: Parser.h:145
Definition: StyleConfig.h:376
void ROUTESTYLE(StyleFilter filter, bool state)
void POLYGON(Symbol &symbol)
void AREAICONSTYLE(StyleFilter filter, bool state)
void CONSTBLOCK(bool state)
void AddFeatureToFilter(StyleFilter &filter, const std::string &featureName, const std::string &flagName, TypeInfoSet &resultTypes)
Definition: Parser.h:169
void STYLEFILTER_GROUP(StyleFilter &filter)
void NODETEXTSTYLE(StyleFilter filter, bool state)
void WAYSTYLE(StyleFilter filter, bool state)
std::string Destring(const char *str)
Definition: Parser.h:132
Definition: StyleConfig.h:354
void ATTRIBUTE(PartialStyleBase &style, const StyleDescriptor &descriptor)
void STYLEFILTER_ONEWAY(StyleFilter &filter)
void SemWarning(const char *msg)
void AREATEXTSTYLE(StyleFilter filter, bool state)
Definition: Parser.h:82
void Warning(int line, int col, const char *s)
void UINT(size_t &value)
void STYLEFILTER_FEATURE(StyleFilter &filter)
void Exception(const char *s)
void STYLEFILTER_SIZE(StyleFilter &filter)
Definition: StyleDescription.h:102
Errors * errors
Definition: Parser.h:116
void AREABORDERSYMBOLSTYLE(StyleFilter filter, bool state)
void STYLEFILTER_TYPE(StyleFilter &filter)
Definition: Area.h:38
Definition: Scanner.h:153
void SemErr(const char *msg)
void STYLECONDBLOCK(StyleFilter filter, bool state)
void IFCOND(bool &state)
std::shared_ptr< SizeCondition > SizeConditionRef
Definition: StyleConfig.h:218
std::string text
Definition: Parser.h:65
void ICONSTYLEATTR(IconPartialStyle &style)
void AREABORDERTEXTSTYLE(StyleFilter filter, bool state)
Definition: Styles.h:1135
Definition: StyleDescription.h:474
TypeConfigRef GetTypeConfig() const
void WAYSHIELDSTYLE(StyleFilter filter, bool state)
Definition: Parser.h:51
Definition: Parser.h:55
void CONSTANT(StyleConstantRef &constant)
void Error(int line, int col, const char *s)
size_t GetHexDigitValue(char c)
Definition: Parser.h:156
void IDENT(std::string &value)
std::shared_ptr< StyleConstant > StyleConstantRef
Definition: StyleConfig.h:102
void LINESTYLEATTR(LinePartialStyle &style)
bool FiltersByType() const
Definition: StyleConfig.h:267
void COLOR(Color &color)
void COLOR_VALUE(Color &color)
void AREABORDERSYMSTYLE(BorderPartialStyle &borderStyle)
bool HasType(const TypeInfoRef &type) const
Definition: StyleConfig.h:277
std::shared_ptr< Feature > FeatureRef
Definition: TypeFeature.h:219
void WAYSTYLEDEF(StyleFilter filter, bool state)
bool hasErrors
Definition: Parser.h:70
void PATHSHIELDSTYLEATTR(PathShieldPartialStyle &style)
int column
Definition: Parser.h:64
int line
Definition: Parser.h:63
void STYLE(StyleFilter filter, bool state)
void AREASTYLE(StyleFilter filter, bool state)
Definition: StyleConfig.h:241
void DOUBLE(double &value)
StyleConfig & config
Definition: Parser.h:118
void SynErr(int line, int col, int n)
MagnificationConverter magnificationConverter
Definition: Parser.h:119
void FLAGBLOCK(bool state)
Definition: Magnification.h:262
size_t GetFeatureFilterIndex(const Feature &feature) const
void BORDERSTYLEATTR(BorderPartialStyle &style)
void NODEICONSTYLE(StyleFilter filter, bool state)
void WAYPATHTEXTSTYLE(StyleFilter filter, bool state)
Type
Definition: Parser.h:54
void ROUTESTYLEDEF(StyleFilter filter, bool state)
std::list< Err > errors
Definition: Parser.h:69
void STRING(std::string &value)
void WAYGROUP(size_t priority)
void FILLSTYLEATTR(FillPartialStyle &style)
bool state
Definition: Parser.h:120
void COORD(Vertex2D &coord)
Type type
Definition: Parser.h:62
void UDOUBLE(double &value)
void UMAP(double &width)
void AREASYMBOLSTYLE(FillPartialStyle &fillStyle, BorderPartialStyle &borderStyle)
void CONSTCONDBLOCK(bool state)
Parser(Scanner *scanner, StyleConfig &config, osmscout::ColorPostprocessor colorPostprocessor=nullptr)
ValueType
Definition: Parser.h:122
void AREABORDERSTYLE(StyleFilter filter, bool state)
void RECTANGLE(Symbol &symbol)
void PATHSYMBOLSTYLEATTR(PathSymbolPartialStyle &style)