/
grammar.js
93 lines (79 loc) · 2.29 KB
/
grammar.js
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
/**
* Schema Reference
* - https://docs.fluentbit.io/manual/administration/configuring-fluent-bit/classic-mode/format-schema
* - https://github.com/chronosphereio/calyptia-fluent-bit-config-parser/blob/01349de7128eaf20ff4aa518502b853984c4c72f/src/parser.ts#L233
*/
module.exports = grammar({
name: 'fluentbit',
extras: $ => [],
rules: {
config: $ => repeat(
choice(
$._config_block,
seq(optional($.comment), $._LF),
),
),
_config_block: $ => choice(
$.section,
$.directive,
),
/***************************************************************************
* Section
**************************************************************************/
section: $ => seq(
field('header', $.section_header),
field('body', $.section_body),
),
section_header: $ => seq(
'[', field('name', $.section_header_type), ']',
$._LF,
),
section_body: $ => repeat1(
seq(
$._INDENT,
choice(
$.entry,
$.comment,
// $.group,
),
$._LF,
),
),
// TODO: group
/***************************************************************************
* Directives
**************************************************************************/
directive: $ => seq(
'@',
choice(
$.directive_set,
$.directive_include,
),
$._LF
),
directive_set: $ => seq('SET', $._WS, $._assign_expr),
directive_include: $ => seq('INCLUDE', $._WS, field('pattern', $.value_type)),
/***************************************************************************
* Commons
**************************************************************************/
_assign_expr: $ => seq(
field('key', $.key_type),
optional($._WS),
'=',
optional($._WS),
field('value', $.value_type),
),
entry: $ => seq(
field('key', $.key_type),
$._WS,
field('value', $.value_type),
),
section_header_type: $ => /[a-zA-Z0-9_]+/,
key_type: $ => /[a-zA-Z0-9_\.]+/,
value_type: $ => /[^\n]+/, // TODO: multiple value
_LF: $ => '\n',
_INDENT: $ => ' ', // 4 spaces
_WS: $ => / +/, // only space
comment: $ => token(seq('#', /.*/)),
},
});