-
Notifications
You must be signed in to change notification settings - Fork 3
/
keyvalues2.jison
150 lines (134 loc) · 3.21 KB
/
keyvalues2.jison
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
%{
var valueHandler = function (type, value) {
if (type.endsWith('_array')) {
var arrayType = type.substring(0, type.length - 6);
return value.map(function (v) { return valueHandler(arrayType, v); });
}
else if (type.startsWith('vector')) {
return value.split(" ").map(function (v) { return parseFloat(v); });
}
else {
switch (type) {
case "float":
return parseFloat(value);
break;
case "int":
return parseInt(value);
break;
case "bool":
return !!parseInt(value);
break;
case "color":
return value.split(" ").map(function (v) { return parseInt(v); });
break;
case "qangle":
return value.split(" ").map(function (v) { return parseFloat(v); });
break;
case "CDmePolygonMesh":
case "CDmePolygonMeshDataArray":
case "CDmePolygonMeshSubdivisionData":
case "DmePlugList":
case "EditGameClassProps":
case "element":
case "elementid":
case "string":
case "uint64":
case "binary":
return value;
break;
default:
//console.log('unhandled', type, value);
return value;
break;
}
}
}
%}
%lex
%%
"<!--".*?"-->" { return 'HEADER'; }
\s*\n\s* {/* ignore */}
\"[a-z-_A-Z0-9.\/,:$\s\(\)]*\" { return 'STRING'; }
\s+ {/* ignore */}
"," { return 'COMMA'; }
\{ { return 'LBRACE'; }
\} { return 'RBRACE'; }
\[ { return 'LBRACKET'; }
\] { return 'RBRACKET'; }
<<EOF>> { return 'EOF'; }
/lex
%%
file
: HEADER keyvalues EOF
{ console.log(JSON.stringify($2)); return $2; }
| keyvalues EOF
{ console.log(JSON.stringify($1)); return $1; }
;
keyvalues
: keyvalue
{ $$ = [$1]; }
| keyvalue keyvalues
{ $$ = [$1].concat($2); }
;
keyvalue
: string object
{ $object.key = $string; $$ = $object; }
;
object
: LBRACE properties RBRACE
{ $$ = $properties; }
| LBRACE RBRACE
{ $$ = {}; }
;
properties
: property
{ $$ = $1; }
| property properties
{
for (var i in $2) {
if ($1.hasOwnProperty(i)) {
$1[i] = ($1[i] || []).concat($2[i]);
}
else {
$1[i] = $2[i];
}
}
$$ = $1;
}
;
property
: string string value
{ $$ = {}; $$[$1] = {type: $2, values: valueHandler($2, $value)}; }
;
value
: string
{ $$ = $1; }
| array
{ $$ = $1; }
| object
{ $$ = $1; }
;
array
: LBRACKET RBRACKET
{ $$ = []; }
| LBRACKET elements RBRACKET
{ $$ = $2; }
;
elements
: element
{ $$ = [$1]; }
| element COMMA elements
{ $$ = [$1].concat($3); }
;
element
: string
{ $$ = $1; }
| string string
{ $$ = {}; $$[$1] = $2; }
| keyvalue
{ $$ = $1; }
;
string
: STRING
{ $$ = $1.substring(1, $1.length - 1); }
;