-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlogstash_config.peg
649 lines (547 loc) · 14.9 KB
/
logstash_config.peg
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
// Parsing rules based on https://github.com/elastic/logstash/blob/master/logstash-core/lib/logstash/config/grammar.treetop
{
package config
}
// Parsing entry point
init =
#{
return c.initState()
}
&{
return c.initParser()
} conf:(
conf:config EOF {
return c.ret(conf)
} / _ EOF {
return ast.NewConfig(nil, nil, nil), nil
}
) {
return c.retConfig(conf)
}
// rule config
// _ plugin_section _ (_ plugin_section)* _ <LogStash::Config::AST::Config>
// end
// Removed _ after initial pluginSection (ps), because it is not needed.
config =
psComment:_ ps:pluginSection pss:(
psComment:_ ps:pluginSection {
return c.configSection(ps, psComment)
}
)* footerComment:_ EOF {
return c.config(ps, pss, psComment, footerComment)
}
// rule comment
// (whitespace? "#" [^\r\n]* "\r"? "\n")+ <LogStash::Config::AST::Comment>
// end
comment =
(whitespace? "#" [^\r\n]* "\r"? ("\n" / EOF))+ {
return c.comment()
}
// rule _
// (comment / whitespace)* <LogStash::Config::AST::Whitespace>
// end
_ =
(comment / whitespace)*
// Add __ rule, which keeps compatibility, with Logstash, but allows to warn
// about comments in exceptional locations instead of just silently ignore them.
__ =
( (comment / whitespace)* {
return c.str()
} ) #{
return c.warnComment()
}
// rule whitespace
// [ \t\r\n]+ <LogStash::Config::AST::Whitespace>
// end
whitespace =
[ \t\r\n]+ {
return c.whitespace()
}
// rule plugin_section
// plugin_type _ "{"
// _ (branch_or_plugin _)*
// "}"
// <LogStash::Config::AST::PluginSection>
// end
// Moved _ after "{" to plugin / branch section to simplify comment handling
// Moved _ footerComment outside of ( ... )* seciont to simplify comment handling
pluginSection =
pt:pluginType __ "{" bops:(
bop:branchOrPlugin {
return c.ret(bop)
}
)* footerComment:_ (
"}" / &{
return c.pushError("expect closing curly bracket")
}
) {
return c.pluginSection(pt, bops, footerComment)
}
// rule branch_or_plugin
// branch / plugin
// end
branchOrPlugin =
branch / plugin
// rule plugin_type
// ("input" / "filter" / "output")
// end
pluginType =
"input" {
return ast.Input, nil
} / "filter" {
return ast.Filter, nil
} / "output" {
return ast.Output, nil
} / &{
return c.pushError("expect plugin type (input, filter, output)")
}
// rule plugins
// (plugin (_ plugin)*)?
// <LogStash::Config::AST::Plugins>
// end
// rule plugin
// name _ "{"
// _
// attributes:( attribute (whitespace _ attribute)*)?
// _
// "}"
// <LogStash::Config::AST::Plugin>
// end
// Moved _ after "{" inside block attributes before attribute to simplify comment handling
plugin =
comment:_ name:name __ "{" attributes:(
comment:_ attribute:attribute attrs:(
whitespace comment:_ attribute:attribute {
return c.attributeComment(attribute, comment, true)
}
)* {
return c.attributes(attribute, attrs, comment)
}
)? footerComment:_ (
"}" / &{
return c.fatalError("expect closing curly bracket")
}
) {
return c.plugin(name, attributes, comment, footerComment)
}
// rule name
// (
// ([A-Za-z0-9_-]+ <LogStash::Config::AST::Name>)
// / string
// )
// end
name =
( ([A-Za-z0-9_-]+) {
return c.string(ast.Bareword)
} / value:stringValue {
return c.ret(value)
} )
// rule attribute
// name _ "=>" _ value
// <LogStash::Config::AST::Attribute>
// end
attribute =
name:name __ "=>" __ value:value {
return c.attribute(name, value)
}
// rule value
// plugin / bareword / string / number / array / hash
// end
value =
plugin / bareword / stringValue / number / array / hash / &{
return c.pushError("invalid value")
}
// rule array_value
// bareword / string / number / array / hash
// end
arrayValue =
bareword / stringValue / number / array / hash / &{
return c.fatalError("invalid array value")
}
// rule bareword
// [A-Za-z_] [A-Za-z0-9_]+
// <LogStash::Config::AST::Bareword>
// end
bareword =
[A-Za-z_] [A-Za-z0-9_]+ {
return c.string(ast.Bareword)
}
// rule double_quoted_string
// ( '"' ( '\"' / !'"' . )* '"' <LogStash::Config::AST::String>)
// end
doubleQuotedString =
( `"` ( `\"` / !`"` . )* (
`"` / &{
return c.fatalError("expect closing double quotes (\")")
}
) ) {
return c.enclosedValue()
}
// rule single_quoted_string
// ( "'" ( "\\'" / !"'" . )* "'" <LogStash::Config::AST::String>)
// end
singleQuotedString =
( `'` ( `\'` / !`'` . )* (
`'` / &{
return c.fatalError("expect closing single quote (')")
}
) ) {
return c.enclosedValue()
}
// rule string
// double_quoted_string / single_quoted_string
// end
stringValue =
str:( str:doubleQuotedString {
return c.string(ast.DoubleQuoted)
} / str:singleQuotedString {
return c.string(ast.SingleQuoted)
} ) {
return c.ret(str)
}
// rule regexp
// ( '/' ( '\/' / !'/' . )* '/' <LogStash::Config::AST::RegExp>)
// end
regexp =
( `/` ( `\/` / !`/` . )* (
`/` / &{
return c.fatalError("expect closing slash (/) for regexp")
}
) ) {
return c.regexp()
}
// rule number
// "-"? [0-9]+ ("." [0-9]*)?
// <LogStash::Config::AST::Number>
// end
number =
"-"? [0-9]+ ("." [0-9]*)? {
return c.number()
}
// rule array
// "["
// _
// (
// value (_ "," _ value)*
// )?
// _
// "]"
// <LogStash::Config::AST::Array>
// end
// TODO: value should be changed to to array_value due to https://github.com/elastic/logstash/issues/6580
// Moved _ after "[" inside values infront of first value:value to simplify comment handling
array =
"[" values:(
comment:_ value:value values:(
__ "," comment:_ value:value {
return c.attributeComment(value, comment, true)
}
)* {
return c.attributes(value, values, comment)
}
)? footerComment:_ (
"]" / &{
return c.fatalError("expect closing square bracket")
}
) {
return c.array(values, footerComment)
}
// rule hash
// "{"
// _
// hashentries?
// _
// "}"
// <LogStash::Config::AST::Hash>
// end
// Moved _ after "{" to hashentry section to simplify comment handling
hash =
"{" entries:hashentries? footerComment:_ (
"}" / &{
return c.fatalError("expect closing curly bracket")
}
) {
return c.hash(entries, footerComment)
}
// rule hashentries
// hashentry (whitespace hashentry)*
// <LogStash::Config::AST::HashEntries>
// end
hashentries =
hashentry:hashentry hashentries1:(
whitespace hashentry:hashentry {
return c.ret(hashentry)
}
)* {
return c.hashentries(hashentry, hashentries1)
}
// rule hashentry
// name:(number / bareword / string) _ "=>" _ value
// <LogStash::Config::AST::HashEntry>
// end
hashentry =
comment:_ name:(number / bareword / stringValue) __ "=>" __ value:value {
return c.hashentry(name, value, comment)
}
// Conditions
// rule branch
// if (_ else_if)* (_ else)?
// <LogStash::Config::AST::Branch>
// end
// Added _ before ifBlock to simplify comment handling
branch =
ifComment:_ ifBlock:ifCond elseIfBlocks:(
eibComment:_ eib:elseIf {
return c.elseIfComment(eib, eibComment)
}
)* elseBlock:(
ebComment:_ eb:elseCond {
return c.elseComment(eb, ebComment)
}
)? {
return c.branch(ifBlock, elseIfBlocks, elseBlock, ifComment)
}
// rule if
// "if" _ condition _ "{" _ (branch_or_plugin _)* "}"
// <LogStash::Config::AST::If>
// end
// Moved _ after "{" into bops to simplify comment handling
// Moved _ after bop outside of bops to simplify comment handling
ifCond =
"if" __ cond:condition __ "{" bops:(
comment:_ bop:branchOrPlugin {
return c.branchOrPluginComment(bop, comment)
}
)* footerComment:_ (
"}" / &{
return c.fatalError("expect closing curly bracket")
}
) {
return c.ifBlock(cond, bops, footerComment)
}
// rule else_if
// "else" _ "if" _ condition _ "{" _ ( branch_or_plugin _)* "}"
// <LogStash::Config::AST::Elsif>
// end
// Moved _ after "{" into bops to simplify comment handling
// Moved _ after bop outside of bops to simplify comment handling
elseIf =
"else" __ "if" __ cond:condition __ "{" bops:(
comment:_ bop:branchOrPlugin {
return c.branchOrPluginComment(bop, comment)
}
)* footerComment:_ (
"}" / &{
return c.fatalError("expect closing curly bracket")
}
) {
return c.elseIfBlock(cond, bops, footerComment)
}
// rule else
// "else" _ "{" _ (branch_or_plugin _)* "}"
// <LogStash::Config::AST::Else>
// end
// Moved _ after "{" into bops to simplify comment handling
// Moved _ after bop outside of bops to simplify comment handling
elseCond =
"else" __ "{" bops:(
comment:_ bop:branchOrPlugin {
return c.branchOrPluginComment(bop, comment)
}
)* footerComment:_ (
"}" / &{
return c.fatalError("expect closing curly bracket")
}
) {
return c.elseBlock(bops, footerComment)
}
// rule condition
// expression (_ boolean_operator _ expression)*
// <LogStash::Config::AST::Condition>
// end
condition =
cond:expression conds:(
__ bo:booleanOperator __ cond:expression {
return c.expression(bo, cond)
}
)* {
return c.condition(cond, conds)
}
// rule expression
// (
// ("(" _ condition _ ")")
// / negative_expression
// / in_expression
// / not_in_expression
// / compare_expression
// / regexp_expression
// / rvalue
// ) <LogStash::Config::AST::Expression>
// end
expression =
(
(
"(" __ cond:condition __ ")" {
return c.conditionExpression(cond)
}
)
/ negativeExpression
/ inExpression
/ notInExpression
/ compareExpression
/ regexpExpression
/ rv:rvalue {
return c.rvalue(rv)
}
// &{ return c.pushError("expect valid expression") }
)
// rule negative_expression
// (
// ("!" _ "(" _ condition _ ")")
// / ("!" _ selector)
// ) <LogStash::Config::AST::NegativeExpression>
// end
negativeExpression =
(
(
"!" __ "(" __ cond:condition __ (
")" / &{
return c.fatalError("expect closing parenthesis")
}
) {
return c.negativeExpression(cond)
}
)
/ (
"!" __ sel:selector {
return c.negativeSelector(sel)
}
)
)
// rule in_expression
// rvalue _ in_operator _ rvalue
// <LogStash::Config::AST::InExpression>
// end
inExpression =
lv:rvalue __ inOperator __ rv:rvalue {
return c.inExpression(lv, rv)
}
// rule not_in_expression
// rvalue _ not_in_operator _ rvalue
// <LogStash::Config::AST::NotInExpression>
// end
notInExpression =
lv:rvalue __ notInOperator __ rv:rvalue {
return c.notInExpression(lv, rv)
}
// rule in_operator
// "in"
// end
inOperator =
"in" / &{
return c.pushError("expect in operator (in)")
}
// rule not_in_operator
// "not " _ "in"
// end
notInOperator =
"not " __ "in" / &{
return c.pushError("expect not in operator (not in)")
}
// rule rvalue
// string / number / selector / array / method_call / regexp
// end
// Removed method_call due to https://github.com/elastic/logstash/issues/6698
// rvalue =
// string_value / number / selector / array / method_call / regexp
rvalue =
stringValue / number / selector / array / regexp / &{
return c.pushError("invalid value for expression")
}
// rule method_call
// method _ "(" _
// (
// rvalue ( _ "," _ rvalue )*
// )?
// _ ")"
// <LogStash::Config::AST::MethodCall>
// end
// Removed method_call due to https://github.com/elastic/logstash/issues/6698
// method_call =
// method _ "(" _
// (
// rvalue ( _ "," _ rvalue )*
// )?
// _ ")"
// rule method
// bareword
// end
// Removed method_call due to https://github.com/elastic/logstash/issues/6698
// method =
// bareword
// rule compare_expression
// rvalue _ compare_operator _ rvalue
// <LogStash::Config::AST::ComparisonExpression>
// end
compareExpression =
lv:rvalue __ co:compareOperator __ rv:rvalue {
return c.compareExpression(lv, co, rv)
}
// rule compare_operator
// ("==" / "!=" / "<=" / ">=" / "<" / ">")
// <LogStash::Config::AST::ComparisonOperator>
// end
compareOperator =
("==" / "!=" / "<=" / ">=" / "<" / ">" / &{
return c.pushError("expect compare operator (==, !=, <=, >=, <, >)")
}) {
return c.compareOperator()
}
// rule regexp_expression
// rvalue _ regexp_operator _ (string / regexp)
// <LogStash::Config::AST::RegexpExpression>
// end
regexpExpression =
lv:rvalue __ ro:regexpOperator __ rv:(stringValue / regexp) {
return c.regexpExpression(lv, ro, rv)
}
// rule regexp_operator
// ("=~" / "!~") <LogStash::Config::AST::RegExpOperator>
// end
regexpOperator =
("=~" / "!~" / &{
return c.pushError("expect regexp comparison operator (=~, !~)")
}) {
return c.regexpOperator()
}
// rule boolean_operator
// ("and" / "or" / "xor" / "nand")
// <LogStash::Config::AST::BooleanOperator>
// end
booleanOperator =
("and" / "or" / "xor" / "nand" / &{
return c.pushError("expect boolean operator (and, or, xor, nand)")
}) {
return c.booleanOperator()
}
// rule selector
// selector_element+
// <LogStash::Config::AST::Selector>
// end
selector =
ses:selectorElement+ {
return c.selector(ses)
}
// rule selector_element
// "[" [^\]\[,]+ "]"
// <LogStash::Config::AST::SelectorElement>
// end
selectorElement =
"[" [^\][,]+ (
"]" / &{
return c.pushError("expect closing square bracket")
}
) {
return c.selectorElement()
}
EOF = !.