-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapcss.pegjs
180 lines (171 loc) · 5.64 KB
/
mapcss.pegjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Parser definition for the main MapCSS parser:
*
* <pre>
*
* rule
* _______________________|______________________________
* | |
* selector declaration
* _________|___________________ _________|____________
* | | | |
*
* way|z11-12[highway=residential] { color: red; width: 3 }
*
* |_____||___________________| |_________|
* | | |
* zoom condition instruction
*
* more general:
*
* way|z13-[a=b][c=d]::subpart, way|z-3[u=v]:closed::subpart2 { p1 : val; p2 : val; }
*
* 'val' can be a literal, or an expression like "prop(width, default) + 0.8".
*
* </pre>
*/
Rules = (_ @Rule _)|..|
Rule "rule"
= selectors:Selectors _ declaration:Declaration
{ return {type: "Rule", selectors, declaration}; }
Selectors
= ParentChildSelector|..,(_ "," _)|
ParentChildSelector
= head:Selector tail:(_ (ParentChildOp Condition*)? _ Selector)*
{ return tail.reduce((arg1, [, op = [undefined, undefined], , arg2]) => ({type: "ParentChildSelector", op: op?.[0], conditions: op?.[1], args: [arg1, arg2]}), head); }
ParentChildOp = child:">" / parent:"<" / sibling:"+" / memberOf:"∈" / subset:"⊆" / notSubset:"⊈" / superset:"⊇" / notSuperset:"⊉" / crossing:"⧉"
Selector "selector" =
base:Base
zoom:Zoom?
conditions:Condition*
subpart:Subpart?
{ return {type: "Selector", base, zoom, conditions, subpart}; }
Base "base selector" = "*" / "node" / "way" / "relation" / "line" / "area" / "meta" / "canvas" / "setting"
Subpart "subpart" = "::" @Identifier
Zoom "zoom" = "|z" @(
min:Integer "-" max:Integer { return {min, max}; }
/ min:Integer "-" { return {min}; }
/ "-" max:Integer { return {max}; }
/ min:Integer { return {min, max: min}; }
)
Condition "condition"
= PseudoClassCondition
/ ClassCondition
/ "[" _ @KeyCondition _ "]"
/ "[" _ @KeyValueCondition _ "]"
/ "[" _ @ExpressionCondition _ "]"
ExpressionCondition =
not:"!"?
exp:Expression
{ return {type: "ExpressionCondition", not, exp}; }
KeyCondition =
not:"!"?
key:(TagKey / RegExp)
matchFalse:"?!"?
matchTrue:"?"?
{ return {type: "KeyCondition", not, key, matchFalse, matchTrue}; }
KeyValueCondition =
key:(TagKey / RegExp)
_
op:(regex:"=~" / nregex:"!~" / neq:"!=" / eq:"="/ oneOf:"~=" / startsWith:"^=" / endsWith:"$=" / contains:"*=")
_
value:(TagKey / RegExp / Expression / "*")
{ return {type: "KeyValueCondition", key, op, value}; }
PseudoClassCondition
= not:"!"? ":" cls:Identifier
{ return {type: "PseudoClassCondition", not, cls}; }
ClassCondition
= not:"!"? "." cls:Identifier
{ return {type: "ClassCondition", not, cls}; }
Declaration "declaration"
= "{" _ @Instructions? _ "}"
Instructions
= @(SetInstruction / Instruction)|..,(_ ";" _)| _ ";"?
Instruction "instruction"
= key:Identifier _ ":" _ value:Expression
{ return {type: "Instruction", key, value}; }
SetInstruction
= "set" [ \t\n\r] _ "."? cls:Identifier
{ return {type: "SetInstruction", cls}; }
Identifier "identifier"
= [a-zA-Z_] [a-zA-Z_0-9-]*
{ return text(); }
String "string"
= "\"" string:( [ !#-[\]-~\u0080-\uFFFF] / "\\\"" / "\\\\" / "\\n" / "\\t" )* "\""
{ return JSON.parse(text()); }
RegExp "regex"
= "/" ("\\/" / [^/])* "/"
{ return new RegExp(text().slice(1, -1)); }
TagKey "tag key"
= String / Identifier (":" Identifier)*
{ return text(); }
Expression = CondExpression
CondExpression
= arg1:OrExpression _ "?" _ arg2:OrExpression _ ":" _ arg3:OrExpression
{ return {op: "cond", args: [arg1, arg2, arg3]}; }
/ OrExpression
OrExpression
= head:AndExpression tail:(_ ("||") _ AndExpression)*
{ return tail.reduce((arg1, [, op, , arg2]) => ({op, args: [arg1, arg2]}), head); }
AndExpression
= head:RelExpression tail:(_ ("&&") _ RelExpression)*
{ return tail.reduce((arg1, [, op, , arg2]) => ({op, args: [arg1, arg2]}), head); }
RelExpression
= arg1:AddExpression _ op:(">" / ">=" / "<=" / "<" / "=" / "==" / "!=") _ arg2:AddExpression
{ return {op, args: [arg1, arg2]}; }
/ AddExpression
AddExpression
= head:MulExpression tail:(_ ("+" / "-") _ MulExpression)*
{ return tail.reduce((arg1, [, op, , arg2]) => ({op, args: [arg1, arg2]}), head); }
MulExpression
= head:Factor tail:(_ ("*" / "/") _ Factor)*
{ return tail.reduce((arg1, [, op, , arg2]) => ({op, args: [arg1, arg2]}), head); }
Factor
= "(" _ @Expression _ ")"
/ @Function
/ @String
/ name:Identifier @HexColor
/ @HexColor
/ @Identifier
/ @FloatArray
/ @FloatUnit
Function
= op:Identifier _ "(" args:FunctionArgs? _ ")"
{ return {op, args}; }
FunctionArgs
= Expression|..,(_ "," _)|
Integer "integer"
= ("-" / "+")? [0-9]+
{ return parseInt(text()); }
Float "float"
= ("-" / "+")? [.0-9]+
{ return parseFloat(text()); }
FloatUnit
= value:Float unit:Unit?
{ return value * (unit ?? 1); }
FloatArray
= Float|2..,(_ "," _)|
Unit
= "1" { return 1; }
/ "deg" { return Math.PI / 180; }
/ "°" { return Math.PI / 180; }
/ "rad" { return 1.; }
/ "grad" { return Math.PI / 200; }
/ "turn" { return 2 * Math.PI; }
/ "%" { return 0.01; }
/ "px" { return 1.; }
/ "cm" { return 96 / 2.54; }
/ "mm" { return 9.6 / 2.54; }
/ "in" { return 96.; }
/ "q" { return 2.4 / 2.54; }
/ "pc" { return 16.; }
/ "pt" { return 96. / 72; }
HexColor
= "#" ([0-9a-fA-F])|8| { return text(); }
/ "#" ([0-9a-fA-F])|6| { return text(); }
/ "#" ([0-9a-fA-F])|3| { return text(); }
_ "whitespace"
= ( SingleComment / MultiComment / [ \t\n\r] )*
{ return undefined; }
SingleComment = "//" [^\n]*
MultiComment = "/*" ( !"*/" ( [^\n\r] / "\n" / "\r" ) )* "*/"