-
Notifications
You must be signed in to change notification settings - Fork 8
/
rules.pegjs
75 lines (61 loc) · 1.13 KB
/
rules.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
Start
= __ r:RuleSet __ {
return {
rules: r
};
}
RuleSet
= head:Rule le tail:(__ Rule)* {
return [head, ...tail.map(r => r[1])];
}
Rule
= allbut:("[all-but]\n")* ds:DeclarationSet __ "=" __ c:Condition {
const ret = {
condition: c
};
if (allbut.length) {
ret.allButDeclarations = ds;
} else {
ret.declarations = ds;
}
return ret;
}
DeclarationSet
= head:Declaration tail:(__ Declaration)* {
return [head, ...tail.map(d => d[1])];
}
Declaration
= name:NameOrValue value:("|" NameOrValue)? {
return {
name,
value: value ? value[1] : null
};
}
NameOrValue
= s:([a-zA-Z-*]+) ws* {
return s.join("").trim();
}
Condition
= left:ConditionHelper right:(ConditionOperator ConditionHelper)* {
return [left].concat(...right);
}
ConditionOperator
= ws* "&&" ws* {
return "and";
}
/ ws* "||" ws* {
return "or";
}
ConditionHelper
= not:("!")? name:[a-zA-Z]+ {
return {
name: name.join(""),
not: !!not
}
}
le = [\n\r]
Comment
= "//" (!le .)* le
ws = [ \t]
__
= (ws / le / Comment)*