Class: MixPrecompiler
- Inherits:
-
Object
- Object
- MixPrecompiler
- Defined in:
- lib/ruby/precompiler.rb
Constant Summary collapse
- JS_BASE_FILES =
%w{ env eval object array false function null number string true } .each_with_object({}) do |x,h| filename = File.("../../javascript/base/#{x}.js", __FILE__) h[filename] = File.read(filename) end
- JS_OBJECT_TYPES =
%w= ARRAY ELEMENT EVENT FALSE FUNCTION NULL NUMBER OBJECT STRING TEXT TRUE =
Instance Method Summary collapse
- #build_js_string ⇒ Object
- #collect_scripts ⇒ Object
- #minify ⇒ Object
- #precompile(filename) ⇒ Object
- #replace_case_markers ⇒ Object
- #replace_type_variables ⇒ Object
- #scan_includes(script) ⇒ Object
- #scan_mix_file(filename) ⇒ Object
Instance Method Details
#build_js_string ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/ruby/precompiler.rb', line 23 def build_js_string @js_string = @js_files.map {|*,script| script }.join ?\n replace_type_variables replace_case_markers minify @js_string = "mix_result_should_be_undefined=function(){var SYMBOLS='...Replace with symbol table...';\#{@js_string};\nwindow.onload=function(){startup();evaluate('...Replace with AST nodes...');}}();\n" end |
#collect_scripts ⇒ Object
36 37 38 39 40 41 |
# File 'lib/ruby/precompiler.rb', line 36 def collect_scripts @mix_files = {} @js_files = JS_BASE_FILES.dup scan_mix_file(@primary_filename) end |
#minify ⇒ Object
43 44 45 46 47 48 |
# File 'lib/ruby/precompiler.rb', line 43 def minify @js_string.gsub!(/\s*\/\*.*?\*\/\s*/m, ?\n) @js_string.gsub!(/\s*\n\s*/, ?\n) @js_string.gsub!(/\s*([{;:,=])\s*/, '\1') @js_string.gsub!("\n}", ?}) end |
#precompile(filename) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/ruby/precompiler.rb', line 50 def precompile (filename) @primary_filename = filename collect_scripts build_js_string return @mix_files, @js_string end |
#replace_case_markers ⇒ Object
58 59 60 61 62 |
# File 'lib/ruby/precompiler.rb', line 58 def replace_case_markers MixTranslator::NODE_TYPES.each do |k,v| @js_string.gsub!("case #{k}:","case #{v}:") end end |
#replace_type_variables ⇒ Object
64 65 66 67 68 |
# File 'lib/ruby/precompiler.rb', line 64 def replace_type_variables JS_OBJECT_TYPES.each_with_index do |t,i| @js_string.gsub!("#{t}_TYPE", i.to_s) end end |
#scan_includes(script) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/ruby/precompiler.rb', line 70 def scan_includes (script) ss = StringScanner.new(script) js_filenames = [] while ss.scan(/~~\s*(.*)\.(js|mix)\s*\n/) basename = ss[1] type = ss[2] filename = "#{basename}.#{type}" if type == 'mix' unless [@primary_filename, *@mix_files.keys].include?(filename) scan_mix_file(filename) end else js_filenames << filename end end js_filenames.each do |name| filename = File.("../../javascript/#{name}", __FILE__) next if @js_files.has_key?(filename) @js_files[filename] = File.read(filename) end end |
#scan_mix_file(filename) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ruby/precompiler.rb', line 95 def scan_mix_file (filename) unless File.exists? filename puts "no such file #{filename}" exit end script = File.read(filename) + ?\n scan_includes(script) @mix_files[filename] = script end |