Class: Condenser::JSAnalyzer

Inherits:
Object
  • Object
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

Methods included from ParseHelpers

#current_line, #cursor, #eos?, #forward, #next_word, #pre_match, #rewind, #scan_until, #seek

Class Method Details

.call(environment, input) ⇒ Object



8
9
10
# File 'lib/condenser/processors/js_analyzer.rb', line 8

def self.call(environment, input)
  new.call(environment, input)
end

.setup(env) ⇒ Object



5
6
# File 'lib/condenser/processors/js_analyzer.rb', line 5

def self.setup(env)
end

Instance Method Details

#call(environment, input) ⇒ Object



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
# File 'lib/condenser/processors/js_analyzer.rb', line 12

def call(environment, input)
  seek(0)
  @sourcefile = input[:source_file]
  @source = input[:source]
  @stack =  [:main]

  input[:export_dependencies] ||= []

  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
  while !eos?
    case @stack.last

    when :tick_value
      scan_until(/(\$\{|\`)/)
      case matched
      when '`'
        @stack.pop
      when '${'
        @stack << :tick_statment
      end

    when :import
      scan_until(/[\"\'\`]/)
      input[:export_dependencies] << case matched
      when "\""
        double_quoted_value
      when "'"
        single_quoted_value
      when '`'
        tick_quoted_value
      end
      scan_until(/(;|\n)/)
      @stack.pop

    else
      scan_until(/(\/\/|\/\*|\/|\(|\)|\{|\}|\"|\'|\`|export|import|\z)/)

      case matched
      when '//'
        scan_until(/(\n|\z)/)
      when '/*'
        scan_until(/\*\//)
      when '"'
        double_quoted_value
      when "'"
        single_quoted_value
      when '`'
        @stack << :tick_value
      when '/'
        if match_index = @source.rindex(/(\w+|\)|\])\s*\//, @index)
          match = @source.match(/(\w+|\)|\])\s*\//, match_index)
          if match[0].length + match_index != @index
            regex_value
          end
        else
          regex_value
        end
      when '('
        @stack.push :parenthesis
      when ')'
        raise unexptected_token(")") if @stack.last != :parenthesis
        @stack.pop
      when '{'
        @stack.push :brackets
      when '}'
        case @stack.last
        when :brackets, :tick_statment
          @stack.pop
        else
          raise unexptected_token("}")
        end
      when 'export'
        if @stack.last == :main
          input[:exports] = true;
          input[:default_export] = true if next_word == 'default'
        end
      when 'import'
        if @stack.last == :main
          @stack << :import
        end
      else
        @stack.pop
      end
    end

    if last_postion == @index
      raise Condenser::SyntaxError, "Error parsing JS file with JSAnalyzer"
    else
      last_postion = @index
    end
  end
end

#double_quoted_valueObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/condenser/processors/js_analyzer.rb', line 127

def double_quoted_value
  ret_value = ""

  while scan_until(/[\"\n]/)
    if matched == "\n"
      raise unexptected_token("\\n")
    elsif matched == "\""
      if pre_match[-1] != "\\"
        ret_value << pre_match
        return ret_value
      else
        ret_value << pre_match << "\\\""
      end

      
    else
      ret_value << match
    end
  end
end

#regex_valueObject



176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/condenser/processors/js_analyzer.rb', line 176

def regex_value
  ret_value = ""

  while scan_until(/\//)
    if matched == "/" && pre_match[-1] != "\\"
      ret_value << pre_match
      return ret_value
    else
      ret_value << pre_match
    end
  end
end

#single_quoted_valueObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/condenser/processors/js_analyzer.rb', line 148

def single_quoted_value
  ret_value = ""

  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_valueObject



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/condenser/processors/js_analyzer.rb', line 163

def tick_quoted_value
  ret_value = ""

  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



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/condenser/processors/js_analyzer.rb', line 115

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)}#{'^'*(@matched.length)}"
  message << "\n"
  Condenser::SyntaxError.new(message)
end