Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/rake_tasks/crazy_fun/main.rb
2867 views
1
require 'rake_tasks/crazy_fun/build_grammar'
2
3
class OutputType
4
attr_accessor :name
5
attr_reader :args
6
7
def initialize
8
@args = {}
9
end
10
11
def push(value)
12
if value.is_a? NameType
13
@name = value.to_native
14
elsif value.is_a? ArgType
15
@args[value.key] = value.value
16
end
17
end
18
19
def [](key)
20
@args[key]
21
end
22
23
def length
24
@args.length
25
end
26
27
def to_s
28
str = "#{@name}(\n"
29
@args.each do |arg|
30
str << " :#{arg[0]} => "
31
if arg[1].is_a? Symbol
32
str << ":#{arg[1]}"
33
elsif arg[1].is_a? String
34
str << '"' + arg[1] + '"'
35
elsif arg[1].is_a? Array
36
str << "[ "
37
arg[1].each do |item|
38
if item.is_a? Symbol
39
str << ":#{item}"
40
elsif item.is_a? String
41
str << '"' + item + '"'
42
end
43
str << ", "
44
end
45
str << " ]"
46
end
47
48
str << ",\n"
49
end
50
str << ")"
51
str
52
end
53
end
54
55
class StringType
56
def initialize
57
@data = ""
58
end
59
60
def <<(data)
61
@data << data
62
end
63
64
def to_native
65
@data
66
end
67
end
68
69
class SymbolType < StringType
70
def to_native
71
@data.to_sym
72
end
73
end
74
75
class NameType < StringType; end
76
77
class ArrayType
78
def initialize
79
@ary = []
80
end
81
82
def push(value)
83
@ary.push value.to_native
84
end
85
86
def to_native
87
@ary
88
end
89
end
90
91
class MapEntry
92
attr_accessor :key, :value
93
94
def push(value)
95
if @read_key
96
@value = value.to_native
97
else
98
@key = value.to_native
99
@read_key = true
100
end
101
end
102
103
def to_native
104
{ @key => @value }
105
end
106
end
107
108
class MapType
109
def initialize
110
@map = {}
111
end
112
113
def push(value)
114
@map = @map.merge(value.to_native)
115
end
116
117
def to_native
118
@map
119
end
120
end
121
122
class ArgType < MapEntry; end
123
124
class BuildFile
125
attr :type
126
attr_reader :types
127
attr_accessor :debug
128
129
def initialize
130
@lhs = []
131
@types = []
132
end
133
134
def leave
135
# Get the top of the stack, pop it, then push the old top into the new.
136
current = @lhs[-1]
137
@lhs.pop
138
139
if current.is_a? OutputType
140
@types.push(current)
141
elsif !@lhs[-1].nil?
142
@lhs[-1].push(current)
143
end
144
145
puts "Leaving #{current}" if @debug
146
end
147
148
def parse_file(file_name)
149
@file_name = file_name
150
data = IO.read(file_name)
151
parse(data)
152
end
153
154
def show_bad_line
155
line = 1
156
column = 1
157
current_line = ""
158
159
for n in 0 ... @p
160
char = @data[n].chr
161
162
if char == "\n"
163
line += 1
164
column = 1
165
current_line = ""
166
else
167
column += 1
168
current_line << char
169
end
170
end
171
172
n += 1
173
174
while @data[n] && @data[n].chr != "\n"
175
current_line << @data[n].chr
176
n += 1
177
end
178
179
error_msg = "Parse error (#{line}, #{column}) "
180
error_msg << "in file '#{@file_name}'" unless @file_name.nil?
181
error_msg << "\n\n #{current_line}"
182
183
error_msg
184
end
185
end
186
187