Warning, /libraries/kosmindoormap/src/map/style/mapcssparser.y is written in an unsupported language. File is not indexed.

0001 %{
0002 /*
0003     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "mapcssparser_impl.h"
0009 #include "mapcssscanner.h"
0010 
0011 #include "style/mapcssparser_p.h"
0012 #include "style/mapcssrule_p.h"
0013 #include "style/mapcssselector_p.h"
0014 #include "style/mapcssstyle.h"
0015 
0016 void yyerror(YYLTYPE *loc, KOSMIndoorMap::MapCSSParserPrivate *parser, yyscan_t scanner, char const* msg)
0017 {
0018     (void)scanner;
0019     qWarning() << "PARSER ERROR:" << msg << "in" << parser->m_currentFileName << "line:" << loc->first_line << "column:" << loc->first_column;
0020     parser->setError(QString::fromUtf8(msg), loc->first_line, loc->first_column);
0021 }
0022 
0023 using namespace KOSMIndoorMap;
0024 
0025 %}
0026 
0027 %code requires {
0028 
0029 #include "style/mapcsscondition_p.h"
0030 #include "style/mapcssselector_p.h"
0031 
0032 namespace KOSMIndoorMap {
0033 class MapCSSDeclaration;
0034 class MapCSSParserPrivate;
0035 class MapCSSRule;
0036 class MapCSSStyle;
0037 
0038 struct StringRef {
0039     const char *str;
0040     int len;
0041 };
0042 
0043 struct ZoomRange {
0044     int low;
0045     int high;
0046 };
0047 
0048 }
0049 
0050 #ifndef YY_TYPEDEF_YY_SCANNER_T
0051 #define YY_TYPEDEF_YY_SCANNER_T
0052 typedef void* yyscan_t;
0053 #endif
0054 
0055 using namespace KOSMIndoorMap;
0056 
0057 }
0058 
0059 %define api.pure
0060 %define parse.error verbose
0061 
0062 %locations
0063 %lex-param { yyscan_t scanner }
0064 %parse-param { KOSMIndoorMap::MapCSSParserPrivate *parser }
0065 %parse-param { yyscan_t scanner }
0066 
0067 %union {
0068     uint32_t uintVal;
0069     double doubleVal;
0070     bool boolVal;
0071     ZoomRange zoomRange;
0072 
0073     char *str;
0074     StringRef strRef;
0075 
0076     MapCSSRule *rule;
0077     MapCSSSelector *selector;
0078     MapCSSBasicSelector *basicSelector;
0079     MapCSSCondition *condition;
0080     MapCSSConditionHolder *conditionHolder;
0081     MapCSSCondition::Operator binaryOp;
0082     MapCSSDeclaration *declaration;
0083 }
0084 
0085 %token T_LBRACKET
0086 %token T_RBRACKET
0087 %token T_LBRACE
0088 %token T_RBRACE
0089 %token T_LPAREN
0090 %token T_RPAREN
0091 %token T_DOUBLE_COLON
0092 %token T_COLON
0093 %token T_SEMICOLON
0094 %token T_COMMA
0095 %token T_DASH
0096 %token T_PLUS
0097 %token T_STAR
0098 %token T_ZOOM
0099 %token T_EXCLAMATION_MARK
0100 %token T_EQUALS
0101 %token T_DOT
0102 %token <binaryOp> T_BINARY_OP
0103 %token T_KEYWORD_IMPORT
0104 %token T_KEYWORD_URL
0105 %token T_KEYWORD_RGBA
0106 %token T_KEYWORD_RGB
0107 %token T_KEYWORD_SET
0108 %token <strRef> T_IDENT
0109 %token <uintVal> T_HEX_COLOR
0110 %token <str> T_STRING
0111 %token <doubleVal> T_DOUBLE
0112 %token <boolVal> T_BOOLEAN_LITERAL
0113 
0114 %type <rule> Rule
0115 %type <selector> Selectors
0116 %type <selector> Selector
0117 %type <basicSelector> BasicSelector
0118 %type <strRef> ClassSelector
0119 %type <strRef> PseudoClassSelector
0120 %type <conditionHolder> Tests
0121 %type <condition> Test
0122 %type <zoomRange> ZoomRange
0123 %type <condition> Condition
0124 %type <binaryOp> BinaryOp
0125 %type <strRef> LayerSelector
0126 %type <strRef> Key
0127 %type <rule> Declarations
0128 %type <declaration> Declaration
0129 %type <strRef> PropertyName
0130 %type <declaration> PropertyValue
0131 %type <doubleVal> DoubleValue
0132 
0133 %destructor { free($$); } <str>
0134 %destructor { delete $$; } <rule>
0135 %destructor { delete $$; } <selector>
0136 %destructor { delete $$; } <conditionHolder>
0137 %destructor { delete $$; } <condition>
0138 %destructor { delete $$; } <declaration>
0139 
0140 %verbose
0141 
0142 %%
0143 // see https://wiki.openstreetmap.org/wiki/MapCSS/0.2/BNF
0144 
0145 Ruleset:
0146   Rule
0147 | Ruleset Rule { Q_UNUSED($2); }
0148 ;
0149 
0150 Rule:
0151   Selectors T_LBRACE Declarations T_RBRACE {
0152     $3->setSelector($1);
0153     parser->addRule($3);
0154     $$ = nullptr;
0155   }
0156 | Import { $$ = nullptr; }
0157 ;
0158 
0159 Import:
0160   T_KEYWORD_IMPORT T_KEYWORD_URL T_LPAREN T_STRING[I] T_RPAREN T_SEMICOLON {
0161     if (!parser->addImport($I, {})) {
0162         YYABORT;
0163     }
0164   }
0165 | T_KEYWORD_IMPORT T_KEYWORD_URL T_LPAREN T_STRING[I] T_RPAREN T_IDENT[C] T_SEMICOLON {
0166     if (!parser->addImport($I, parser->makeClassSelector($C.str, $C.len))) {
0167         YYABORT;
0168     }
0169   }
0170 ;
0171 
0172 Selectors:
0173   Selector { $$ = $1; }
0174 | Selectors T_COMMA Selector { if (auto u = dynamic_cast<MapCSSUnionSelector*>($1)) {
0175     u->addSelector(std::unique_ptr<MapCSSSelector>($3));
0176     $$ = $1;
0177   } else {
0178     auto s = new MapCSSUnionSelector;
0179     s->addSelector(std::unique_ptr<MapCSSSelector>($1));
0180     s->addSelector(std::unique_ptr<MapCSSSelector>($3));
0181     $$ = s;
0182   }}
0183 ;
0184 
0185 Selector:
0186   BasicSelector { $$ = $1; }
0187 | Selector BasicSelector { if (auto chain = dynamic_cast<MapCSSChainedSelector*>($1)) {
0188     chain->selectors.push_back(std::unique_ptr<MapCSSBasicSelector>($2));
0189     $$ = $1;
0190   } else {
0191     auto s = new MapCSSChainedSelector;
0192     s->selectors.push_back(std::unique_ptr<MapCSSBasicSelector>(static_cast<MapCSSBasicSelector*>($1)));
0193     s->selectors.push_back(std::unique_ptr<MapCSSBasicSelector>($2));
0194     $$ = s;
0195   }}
0196 ;
0197 
0198 BasicSelector:
0199   T_IDENT[I] ClassSelector[C] ZoomRange[Z] Tests[T] PseudoClassSelector[P] LayerSelector[L] {
0200     $$ = new MapCSSBasicSelector;
0201     if ($C.str) {
0202         $$->setClass(parser->makeClassSelector($C.str, $C.len));
0203     }
0204     $$->setObjectType($I.str, $I.len);
0205     $$->setZoomRange($Z.low, $Z.high);
0206     $$->setConditions($T);
0207     if ($P.str) {
0208         $$->setPseudoClass($P.str, $P.len);
0209     }
0210     $$->setLayer(parser->makeLayerSelector($L.str, $L.len));
0211   }
0212 | T_STAR ClassSelector[C] ZoomRange[Z] Tests[T] PseudoClassSelector[P] LayerSelector[L] {
0213     $$ = new MapCSSBasicSelector;
0214     if ($C.str) {
0215         $$->setClass(parser->makeClassSelector($C.str, $C.len));
0216     }
0217     $$->setZoomRange($Z.low, $Z.high);
0218     $$->setConditions($T);
0219     if ($P.str) {
0220         $$->setPseudoClass($P.str, $P.len);
0221     }
0222     $$->setLayer(parser->makeLayerSelector($L.str, $L.len));
0223   }
0224 ;
0225 
0226 ClassSelector:
0227   %empty { $$.str = nullptr; $$.len = 0; }
0228 | T_DOT T_IDENT[I] { $$.str = $I.str; $$.len = $I.len; }
0229 ;
0230 
0231 PseudoClassSelector:
0232   %empty { $$.str = nullptr; $$.len = 0; }
0233 | T_COLON T_IDENT[I] { $$.str = $I.str; $$.len = $I.len; }
0234 ;
0235 
0236 ZoomRange:
0237   %empty { $$.low = 0; $$.high = 0; }
0238 | T_ZOOM T_DOUBLE[Low] T_DASH T_DOUBLE[High] { $$.low = $Low; $$.high = $High; }
0239 | T_ZOOM T_DOUBLE[Low] T_DASH { $$.low = $Low; $$.high = 0; }
0240 | T_ZOOM T_DOUBLE { $$.low = $2; $$.high = $2; }
0241 | T_ZOOM T_DASH T_DOUBLE[High] { $$.low = 0; $$.high = $High; }
0242 
0243 Tests:
0244   %empty { $$ = nullptr; }
0245 | Tests Test { if ($1) { $1->addCondition($2); $$ = $1; } else { auto holder = new MapCSSConditionHolder; holder->addCondition($2); $$ = holder; }}
0246 ;
0247 
0248 Test: T_LBRACKET Condition T_RBRACKET { $$ = $2; };
0249 
0250 // TODO incomplete: quoted names, regexps
0251 Condition:
0252   Key BinaryOp T_IDENT { $$ = new MapCSSCondition; $$->setKey($1.str, $1.len); $$->setOperation($2); $$->setValue($3.str, $3.len); }
0253 | Key BinaryOp T_STRING { $$ = new MapCSSCondition; $$->setKey($1.str, $1.len); $$->setOperation($2); $$->setValue($3, std::strlen($3)); }
0254 | Key BinaryOp DoubleValue { $$ = new MapCSSCondition; $$->setKey($1.str, $1.len); $$->setOperation($2); $$->setValue($3); }
0255 | T_EXCLAMATION_MARK Key { $$ = new MapCSSCondition; $$->setOperation(MapCSSCondition::KeyNotSet); $$->setKey($2.str, $2.len); }
0256 | Key { $$ = new MapCSSCondition; $$->setOperation(MapCSSCondition::KeySet); $$->setKey($1.str, $1.len); }
0257 ;
0258 
0259 BinaryOp:
0260   T_BINARY_OP { $$ = $1; }
0261 | T_EQUALS    { $$ = MapCSSCondition::Equal; }
0262 
0263 LayerSelector:
0264   %empty { $$.str = nullptr; $$.len = 0; }
0265 | T_DOUBLE_COLON T_IDENT[L] { $$ = $L; }
0266 ;
0267 
0268 Key:
0269   T_IDENT { $$ = $1; }
0270 | Key T_COLON T_IDENT { $$.str = $1.str; $$.len = $3.str - $1.str + $3.len; }
0271 ;
0272 
0273 Declarations:
0274   %empty { $$ = new MapCSSRule; }
0275 | Declarations Declaration { $$ = $1; $$->addDeclaration($2); }
0276 ;
0277 
0278 Declaration:
0279   PropertyName T_COLON PropertyValue T_SEMICOLON { $$ = $3; $$->setPropertyName($1.str, $1.len); }
0280 | T_KEYWORD_SET Key[K] T_EQUALS T_STRING[V] T_SEMICOLON {
0281     $$ = new MapCSSDeclaration(MapCSSDeclaration::TagDeclaration);
0282     $$->setIdentifierValue($K.str, $K.len);
0283     $$->setStringValue($V);
0284   }
0285 | T_KEYWORD_SET Key[K] T_EQUALS T_DOUBLE[V] T_SEMICOLON {
0286     $$ = new MapCSSDeclaration(MapCSSDeclaration::TagDeclaration);
0287     $$->setIdentifierValue($K.str, $K.len);
0288     $$->setDoubleValue($V);
0289   }
0290 | T_KEYWORD_SET Key[K] T_EQUALS T_DASH T_DOUBLE[V] T_SEMICOLON {
0291     $$ = new MapCSSDeclaration(MapCSSDeclaration::TagDeclaration);
0292     $$->setIdentifierValue($K.str, $K.len);
0293     $$->setDoubleValue(-$V);
0294   }
0295 | T_KEYWORD_SET Key[K] T_SEMICOLON {
0296     $$ = new MapCSSDeclaration(MapCSSDeclaration::TagDeclaration);
0297     $$->setIdentifierValue($K.str, $K.len);
0298   }
0299 | T_KEYWORD_SET T_DOT T_IDENT[C] T_SEMICOLON {
0300     $$ = new MapCSSDeclaration(MapCSSDeclaration::ClassDeclaration);
0301     $$->setClassSelectorKey(parser->makeClassSelector($C.str, $C.len));
0302   }
0303 ;
0304 
0305 PropertyName:
0306   T_IDENT { $$ = $1; }
0307 | T_IDENT T_DASH PropertyName { $$.str = $1.str; $$.len = $3.str - $1.str + $3.len; }
0308 ;
0309 
0310 // TODO incomplete: missing size, eval
0311 // TODO url does not preserve type, and argument quoting differs from spec
0312 PropertyValue:
0313   Key { $$ = new MapCSSDeclaration(MapCSSDeclaration::PropertyDeclaration); $$->setIdentifierValue($1.str, $1.len); }
0314 | T_HEX_COLOR { $$ = new MapCSSDeclaration(MapCSSDeclaration::PropertyDeclaration); $$->setColorRgba($1); }
0315 | DoubleValue T_IDENT { $$ = new MapCSSDeclaration(MapCSSDeclaration::PropertyDeclaration); $$->setDoubleValue($1); $$->setUnit($2.str, $2.len); }
0316 | DoubleValue { $$ = new MapCSSDeclaration(MapCSSDeclaration::PropertyDeclaration); $$->setDoubleValue($1); }
0317 | T_DOUBLE T_COMMA T_DOUBLE { $$ = new MapCSSDeclaration(MapCSSDeclaration::PropertyDeclaration); $$->setDashesValue({$1, $3}); } // generalize to n dash distances
0318 | T_STRING { $$ = new MapCSSDeclaration(MapCSSDeclaration::PropertyDeclaration); $$->setStringValue($1); }
0319 | T_KEYWORD_RGBA T_LPAREN T_DOUBLE[R] T_COMMA T_DOUBLE[G] T_COMMA T_DOUBLE[B] T_COMMA T_DOUBLE[A] T_RPAREN {
0320     $$ = new MapCSSDeclaration(MapCSSDeclaration::PropertyDeclaration);
0321     uint32_t c = 0;
0322     c |= (uint32_t)($A * 255.0) << 24;
0323     c |= (uint32_t)($R * 255.0) << 16;
0324     c |= (uint32_t)($G * 255.0) << 8;
0325     c |= (uint32_t)($B * 255.0) << 0;
0326     $$->setColorRgba(c);
0327   }
0328 | T_KEYWORD_RGB T_LPAREN T_DOUBLE[R] T_COMMA T_DOUBLE[G] T_COMMA T_DOUBLE[B] T_RPAREN {
0329     $$ = new MapCSSDeclaration(MapCSSDeclaration::PropertyDeclaration);
0330     uint32_t c = 0;
0331     c |= 0xff << 24;
0332     c |= (uint32_t)($R * 255.0) << 16;
0333     c |= (uint32_t)($G * 255.0) << 8;
0334     c |= (uint32_t)($B * 255.0) << 0;
0335     $$->setColorRgba(c);
0336   }
0337 | T_KEYWORD_URL T_LPAREN T_STRING[S] T_RPAREN {
0338     $$ = new MapCSSDeclaration(MapCSSDeclaration::PropertyDeclaration);
0339     $$->setStringValue($S);
0340   }
0341 | T_BOOLEAN_LITERAL[B] {
0342     $$ = new MapCSSDeclaration(MapCSSDeclaration::PropertyDeclaration);
0343     $$->setBoolValue($B);
0344   }
0345 ;
0346 
0347 DoubleValue:
0348   T_DOUBLE { $$ = $1; }
0349 | T_DASH T_DOUBLE { $$ = -$2; }
0350 | T_PLUS T_DOUBLE { $$ = $2; }
0351 ;
0352 
0353 %%