Class: Condenser::JSAnalyzer
- Inherits:
-
Object
- Object
- Condenser::JSAnalyzer
show all
- Includes:
- ParseHelpers
- Defined in:
- lib/condenser/processors/js_analyzer.rb
Instance Attribute Summary
Attributes included from ParseHelpers
#matched
Class Method Summary
collapse
Instance Method Summary
collapse
#current_line, #cursor, #eos?, #forward, #gobble, #next_word, #peek, #pre_match, #rewind, #scan_until, #seek
Class Method Details
.call(environment, input) ⇒ Object
10
11
12
|
# File 'lib/condenser/processors/js_analyzer.rb', line 10
def self.call(environment, input)
new.call(environment, input)
end
|
.setup(env) ⇒ Object
7
8
|
# File 'lib/condenser/processors/js_analyzer.rb', line 7
def self.setup(env)
end
|
Instance Method Details
#call(environment, input) ⇒ Object
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
|
# File 'lib/condenser/processors/js_analyzer.rb', line 14
def call(environment, input)
seek(0)
@sourcefile = input[:source_file]
@source = input[:source]
@stack = [:main]
@previous = [[]]
input[:linked_assets] ||= Set.new
input[:export_dependencies] ||= Set.new
scan_until(/\A(\/\/[^\n]*(\n|\z))*/)
if matched
directives = matched.split(/\n/).map { |l| l.delete_prefix("//").strip }
directives.each do |directive|
if directive.start_with?('depends_on')
input[:process_dependencies] << directive.sub(/\Adepends_on\s+/, '')
end
end
end
last_postion = nil
last_stack = nil
while !eos?
case @stack.last
when :tick_value
scan_until(/(\$\{|\`)/)
case matched
when '`'
@stack.pop if pre_match[-1] != "\\" && pre_match[-1] != "\\"
when '${'
@stack << :tick_statment
end
when :import
scan_until(/[\"\'\`\(]/)
dynamic = if matched == "("
scan_until(/[\"\'\`]/)
true
end
filename = case matched
when "\""
double_quoted_value
when "'"
single_quoted_value
when '`'
tick_quoted_value
end
input[:type] = "module"
if dynamic
input[:process_dependencies] << filename
input[:linked_assets] << filename
else
input[:export_dependencies] << filename
end
scan_until(/(;|\n|\))/)
@stack.pop
when :export
input[:type] = "module"
input[:exports] = true;
input[:default_export] = true if gobble(/\s+default/)
gobble(/\s+/)
if gobble(/\{/)
@stack << :brackets
@previous << []
elsif gobble(/\*/)
@stack << :export_from
else
@stack.pop
end
when :export_from
if gobble(/\s+from\s+/)
scan_until(/\"|\'/)
input[:export_dependencies] << case matched
when '"'
double_quoted_value
when "'"
single_quoted_value
end
end
@stack.pop
@stack.pop
else
scan_until(/(\/\/|\/\*|\/|\(|\)|\{|\}|\"|\'|\`|export(?![[:alnum:]])|import(?![[:alnum:]])|\z)/)
case matched
when '//'
scan_until(/(\n|\z)/)
@previous.last << :single_line_comment
when '/*'
scan_until(/\*\//)
@previous.last << :multi_line_comment
when '"'
double_quoted_value
@previous.last << :double_quoted_value
when "'"
single_quoted_value
@previous.last << :single_quoted_value
when '`'
@stack << :tick_value
when '/'
if pre_match.match(/(\W|\A)(void|typeof|return|export)\z/)
regex_value
elsif match_index = @source.rindex(/(\w+|\)|\])(\s*)\//m, @index)
match = @source.match(/(\w+|\)|\])(\s*)\//, match_index)
x = @previous.last.dup
while match[2]&.index("\n") && [:single_line_comment, :multi_line_comment].include?(x.last)
case x.last
when :single_line_comment
match_index = @source.rindex(/[^\n]*\/\/[^\n]*\s*/m, match.begin(2))
match = @source.match(/(\w+|\)|\])\s*\//, match_index)
when :multi_line_comment
match_index = @source.rindex(/\/\*/m, match.begin(2))
match = @source.match(/(\w+|\)|\])\s*\//, match_index)
end
end
if @previous.last.last == :loop && match[1] =~ /\)\z/
regex_value
elsif %w(void typeof return export).include?(match[1])
regex_value
elsif match[0].length + match_index != @index
regex_value
elsif match[1].strip.empty?
regex_value
end
else
regex_value
end
when '('
@stack << if pre_match =~ /\W*(for|while)\s*\z/
:loop
else
:parenthesis
end
when ')'
raise unexptected_token(")") if @stack.last != :parenthesis && @stack.last != :loop
@previous.last << @stack.pop
when '{'
@stack.push :brackets
@previous << []
when '}'
case @stack.last
when :tick_statment
@stack.pop
when :brackets
@stack.pop
@previous.pop
@previous.last << :brackets
if @stack.last == :export
@stack.pop
@stack << :export_from if peek(/\s+from/i)
end
else
raise unexptected_token("}")
end
when 'export'
if @stack.last == :main
@stack << :export
end
when 'import'
@stack << :import
else
@stack.pop
end
end
if last_postion == @index && last_stack == @stack.last
syntax_error = Condenser::SyntaxError.new("Error parsing JS file with JSAnalyzer")
syntax_error.instance_variable_set(:@path, @sourcefile)
raise Condenser::SyntaxError, "Error parsing JS file with JSAnalyzer"
else
last_postion = @index
last_stack = @stack.last
end
end
raise Condenser::SyntaxError, "Unexpected EOF" if !@stack.empty? && @stack.last != :main
end
|
#double_quoted_value ⇒ Object
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
# File 'lib/condenser/processors/js_analyzer.rb', line 216
def double_quoted_value
ret_value = String.new
while scan_until(/[\"\n\\]/)
case matched
when "\n"
raise unexptected_token("\\n")
when "\\"
ret_value << pre_match << matched << gobble(1)
when "\""
ret_value << pre_match
return ret_value
else
ret_value << match
end
end
end
|
#regex_value ⇒ Object
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
|
# File 'lib/condenser/processors/js_analyzer.rb', line 262
def regex_value
ret_value = String.new
regex_stack = ['/']
while !regex_stack.empty?
scan_until(/[\/\[\]]/)
escaped = pre_match[-1] == "\\" && pre_match[-2] != "\\"
ret_value << pre_match
case matched
when "["
regex_stack << '[' if !escaped && regex_stack.last != "["
ret_value << matched
when "]"
regex_stack.pop if !escaped && regex_stack.last == "["
ret_value << matched
when "/"
if !escaped && regex_stack.last != "[" && regex_stack.pop != "/"
raise unexptected_token("/")
else
ret_value << matched
end
end
end
ret_value
end
|
#single_quoted_value ⇒ Object
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
# File 'lib/condenser/processors/js_analyzer.rb', line 234
def single_quoted_value
ret_value = String.new
while scan_until(/[\'\n]/)
if matched == "\n"
raise unexptected_token("\\n")
elsif matched == "\'" && pre_match[-1] != "\\"
ret_value << pre_match
return ret_value
else
ret_value << pre_match
end
end
end
|
#tick_quoted_value ⇒ Object
249
250
251
252
253
254
255
256
257
258
259
260
|
# File 'lib/condenser/processors/js_analyzer.rb', line 249
def tick_quoted_value
ret_value = String.new
while scan_until(/[\`]/)
if matched == "\`" && pre_match[-1] != "\\"
ret_value << pre_match
return ret_value
else
ret_value << pre_match
end
end
end
|
#unexptected_token(token) ⇒ Object
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
# File 'lib/condenser/processors/js_analyzer.rb', line 201
def unexptected_token(token)
start = (@source.rindex("\n", @old_index) || 0) + 1
uptop = @source.index("\n", @index) || (@old_index + @matched.length)
lineno = @source[0..start].count("\n") + 1
message = "Unexpected token #{token} #{@sourcefile} #{lineno.to_s.rjust(4)}:#{(@index-start)}"
message << "\n#{lineno.to_s.rjust(4)}: " << @source[start..uptop]
message << "\n #{'-'* ([@index-1-start,1].max)}#{'^'*([@matched.length,1].max)}"
message << "\n"
syntax_error = Condenser::SyntaxError.new(message)
syntax_error.instance_variable_set(:@path, @sourcefile)
syntax_error
end
|