/
grammar.rl
198 lines (163 loc) · 4.52 KB
/
grammar.rl
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
require "rubygems"
require "logstash/namespace"
%%{
machine logstash_config;
action mark {
@tokenstack.push(p)
#puts "Mark: #{self.line(string, p)}##{self.column(string, p)}"
}
action stack_numeric {
startpos = @tokenstack.pop
endpos = p
token = string[startpos ... endpos]
#puts "numeric: #{token}"
#puts "numeric?: #{string[startpos,50]}"
#puts [startpos, endpos].join(",")
@stack << token.to_i
}
action stack_string {
startpos = @tokenstack.pop
endpos = p
token = string[startpos ... endpos]
#puts "string: #{token}"
@stack << token
}
action stack_quoted_string {
startpos = @tokenstack.pop
endpos = p
token = string[startpos + 1 ... endpos - 1] # Skip quotations
# Parse escapes.
token.gsub(/\\./) { |m| m[1,1] }
#puts "quotedstring: #{token}"
@stack << token
}
action array_init {
@array = []
@stack << :array_init
}
action array_push {
while @stack.last != :array_init
@array.unshift @stack.pop
end
@stack.pop # pop :array_init
@stack << @array
}
action parameter_init {
@parameters = Hash.new { |h,k| h[k] = [] }
}
action parameter {
value = @stack.pop
name = @stack.pop
#puts "parameter: #{name} => #{value}"
@parameters[name] << value
}
action plugin {
@components ||= []
name = @stack.pop
#@components << { :name => name, :parameters => @parameters }
@components << { name => @parameters }
@parameters = {}
}
action component_init {
puts "current component: " + @stack.last
@components = []
}
action component {
name = @stack.pop
@config ||= Hash.new { |h,k| h[k] = [] }
@config[name] += @components
puts "Config component: #{name}"
}
comment = "#" (any - [\n])* >mark %{ e = @tokenstack.pop; puts "Comment: #{string[e ... p]}" };
ws = ([ \t\n] | comment)** ;
#ws = ([ \t\n])** ;
# TODO(sissel): Support floating point values?
numeric = ( ("+" | "-")? [0-9] :>> [0-9]** ) >mark %stack_numeric;
quoted_string = (
( "\"" ( ( (any - [\\"\n]) | "\\" any )* ) "\"" )
| ( "'" ( ( (any - [\\'\n]) | "\\" any )* ) "'" )
) >mark %stack_quoted_string ;
naked_string = ( [A-Za-z_] :>> [A-Za-z0-9_]* ) >mark %stack_string ;
string = ( quoted_string | naked_string ) ;
# TODO(sissel): allow use of this.
regexp_literal = ( "/" ( ( (any - [\\'\n]) | "\\" any )* ) "/" ) ;
array = ( "[" ws string ws ("," ws string ws)* "]" ) >array_init %array_push;
parameter_value = ( numeric | string | array );
parameter = ( string ws "=>" ws parameter_value ) %parameter ;
parameters = ( parameter ( ws parameter )** ) >parameter_init ;
# Statement:
# component {
# plugin_name {
# bar => ...
# baz => ...
# }
# ...
# }
plugin = (
(
naked_string ws "{" ws
parameters
ws "}"
) | (
naked_string ws "{" ws "}"
)
) %plugin ;
component = (
naked_string ws "{"
>component_init
( ws plugin )**
ws "}"
) %component ;
config = (ws component? )** ;
main := config %{ puts "END" }
$err {
# Compute line and column of the cursor (p)
puts "Error at line #{self.line(string, p)}, column #{self.column(string, p)}: #{string[p .. -1].inspect}"
# TODO(sissel): Note what we were expecting?
} ;
}%%
class LogStash::Config::Grammar
attr_accessor :eof
attr_accessor :config
def initialize
# BEGIN RAGEL DATA
%% write data;
# END RAGEL DATA
@tokenstack = Array.new
@stack = Array.new
@types = Hash.new { |h,k| h[k] = [] }
@edges = []
end
def parse(string)
data = string.unpack("c*")
# BEGIN RAGEL INIT
%% write init;
# END RAGEL INIT
begin
# BEGIN RAGEL EXEC
%% write exec;
# END RAGEL EXEC
rescue => e
# Compute line and column of the cursor (p)
raise e
end
if cs < self.logstash_config_first_final
$stderr.puts "Error at line #{self.line(string, p)}, column #{self.column(string, p)}: #{string[p .. -1].inspect}"
raise "Invalid Configuration. Check syntax of config file."
end
return cs
end # def parse
def line(str, pos)
return str[0 .. pos].count("\n") + 1
end
def column(str, pos)
return str[0 .. pos].split("\n").last.length
end
end # class LogStash::Config::Grammar
#def parse(string)
#cfgparser = LogStash::Config::Grammar.new
#result = cfgparser.parse(string)
#puts "result %s" % result
#ap cfgparser.config
#end
#parse(File.open(ARGV[0]).read)