-
Notifications
You must be signed in to change notification settings - Fork 3
/
grammar.pegjs
295 lines (243 loc) · 6.39 KB
/
grammar.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
line
= error_line / alert_line / stopped_line / syscall_line
// add one for strace error
error_line
= error:error {
return {
error: error,
type: 'ERROR'
}
}
alert_line
= pid:pid? alert:alert {
return {
pid: pid,
alert: alert,
type: 'ALERT'
}
}
stopped_line
= pid:pid? notice:stop_notice {
return {
pid: pid,
alert: notice,
type: 'STOPPED'
}
}
syscall_line
= pid:pid?
syscall:syscall args:arguments_list result:result timing:timing? {
return {
syscall: syscall,
args: args,
result: result,
timing: timing,
pid: pid,
type: 'SYSCALL'
}
}
syscall
= _ value:([_a-zA-Z0-9'"]+) { return value.join(''); }
data_structure
= socket_address_length_enclosed / socket_address_length /
array /
nested_struct / struct /
bitwise_array /
ip_address / address / ellipsis /
socket /
function_call /
int /
string /
struct_property /
null /
flags_alternate / flags / flag
array
= '['
values:array_elements
']'
{ return values !== null ? values : []; }
array_elements
= (
head:data_structure
tail:(',' _ value:data_structure { return value; })*
{ return [head].concat(tail); }
)?
bitwise_array
= values:(
operator:([~^]) '['
head:(value:bitwise_array_element { return value.join(''); })
tail:(',' _ value:bitwise_array_element { return value.join(''); })*
{
return {
operator:operator,
elements: [head].concat(tail)
};
}
)?
']'
{ return values !== null ? values : []; }
bitwise_array_element
= _ array_elements:flags _ { return array_elements; }
flag = _ value:[_A-Z0-9]+ _ { return value.join(''); }
flags
= values:(
head:flag
tail:(('|' / 'or') _ value:flag { return value; })*
{ return [head].concat(tail); }
)?
{ return values !== null ? values : []; }
flags_alternate
= operator:([~^])? '['
values:(
head:flag
tail:(_ value:flag { return value; })*
{
var elements = [head].concat(tail);
if(operator) {
return {
operator: operator,
elements: elements
}
}
return elements;
}
)?
']'
{ return values !== null ? values : []; }
nested_struct
= '{'
values:(
head:struct
tail:(',' _ value:data_structure { return value })? {
return [head].concat([tail]);
}
)?
'}' {
return values;
}
struct
= '{' _
values:(
head:data_structure
tail:(',' _ value:data_structure { return value })*
{
var result = {};
[head].concat(tail).forEach(function(element) {
if(!element.hasOwnProperty('name') || !element.hasOwnProperty('value')) {
result[element] = element;
} else {
result[element.name] = element.value;
}
});
return result;
})
_ '}'
{
return values !== null ? values : [];
}
struct_property
= key:(key / capitalised_key) _ ("=")? _ value:(function_call / quoted_value / data_structure / basic_value)? {
if(typeof value !== 'undefined' && value !== '') {
return {name: key, value: value}
}
return {name: key, value: key};
}
key "key"
= value:[_a-z0-9]+ { return value.join(''); }
capitalised_key "capitalised key"
= value:([A-Z][_a-z0-9]+) {
var flattened = [].concat.apply([], value);
return flattened.join('');
}
arguments_list
= '(' _ values:(
head:data_structure
tail:(("," / " ") _ value:(arguments_list_abbreviation / data_structure) { return value; })*
{
// if both the head and tail are empty arrays, don't return an array in an array
if ((tail === null || tail.length <= 0) && (head === null || head.length <= 0)) {
return [];
}
return [head].concat(tail);
}
)?
_ ')'
{
return values !== null ? values : [];
}
arguments_list_abbreviation
= ("/*" _ [0-9]+ _ ("vars" / "entries") _ "*/") { return '...'; }
int = [-0-9]+ { return parseInt(text()); }
ip_address "ip address"
= [0-9]+ "." [0-9]+ "." [0-9]+ "." [0-9]+ { return text() }
address = '0x' value:([0-9a-fA-F]*) { return parseInt(value.join(''), 16) }
null = _ "NULL" _ { return null; }
// string processing borrowed from https://github.com/pegjs/pegjs/blob/master/examples/json.pegjs
string "string"
= _ quotation_mark chars:char* quotation_mark ellipsis? { return chars.join(""); }
socket
= "@" path:string { return path }
socket_address_length_enclosed
= "[" data:socket_address_length "]" { return data }
socket_address_length
= ulen:([0-9]+) "->" rlen:([0-9]+) {
return {
ulen: parseInt(ulen.join('')),
rlen: parseInt(rlen.join(''))
}
}
char
= unescaped
/ escape
sequence:(
'"'
/ "'"
/ "\\"
/ "/"
/ digits:digit+ { return ["\\"].concat(digits).join('') }
/ value:[a-zA-Z] { return "\\" + value }
)
{ return sequence; }
escape
= "\\"
quotation_mark
= '"' / '"'
unescaped
= [^\0-\x1F\x22\x5C]
digit = [0-9]
hex_digit = [0-9a-f]i
// unquoted values should either start with a lowercase letter or number, or else be considered a flag
basic_value
= value:([_a-z0-9][_a-zA-Z0-9]+) {
var flattened = [].concat.apply([], value);
return flattened.join('');
}
quoted_value = value:string { return value; }
function_call
= values:(
function_name:basic_value
_ "(" params:array_elements
([^\)])*
")" { return {function: function_name, params: params} }
)
arithmetic_expression = [-0-9]+ _ [+-/*] _ [-0-9]+
result
= _ '=' _ value:([^<]+) _ {
value = value.join('').trim();
try {
var numeric = Number(value);
if(isNaN(numeric)) {
return value;
}
return numeric;
} catch(e) {
return value;
}
}
timing = _ '<' value:([\.\-0-9]+) '>' _ { return Number(value.join('')); }
pid = ('[pid' _)? _ value:([0-9]+) _ (']')? _ { return Number(value.join('')); }
ellipsis = _ '...' _ { return undefined }
alert = "+++" _ message:[^\+]+ _ "+++" { return message.join('').trim() }
stop_notice = "---" _ signal:flag _ data:data_structure _ "---" { return { signal: signal, data: data } }
error = "strace:" _ error:(.+) { return error.join('').trim() }
_ 'whitespace' = [ \t\n\r]* { return undefined }