mix-language 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,9 +7,11 @@ require 'mix-language'
7
7
 
8
8
  Encoding.default_external = "UTF-8"
9
9
 
10
- unless filename = ARGV[0]
10
+ begin
11
+ absolute_mix_path = File.absolute_path(ARGV[0])
12
+ rescue
11
13
  puts "usage: mix-compile [filename]"
12
14
  exit
13
15
  end
14
16
 
15
- MixCompiler.new.compile(filename)
17
+ MixCompiler.new.compile_to_file(absolute_mix_path)
@@ -0,0 +1,9 @@
1
+ #! /usr/bin/env ruby
2
+ # encoding: UTF-8
3
+ #
4
+ # Copyright © 2011-2012 Jesse Sielaff
5
+ #
6
+ require 'mix-rack'
7
+
8
+ MixRack.root_dir = File.absolute_path('.')
9
+ Rack::Handler::Thin.run MixRack
@@ -0,0 +1,6 @@
1
+ /* dom/text.js */
2
+ /* dom/element.js */
3
+ /* dom/div.js */
4
+ /* dom/table.js */
5
+ /* dom/td.js */
6
+ /* dom/tr.js */
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,4 @@
1
+ /* dom/element.js */
2
+ /* dom/event.js */
3
+ /* dom/mouse.js */
4
+ /* dom/drag.js */
File without changes
File without changes
@@ -0,0 +1,30 @@
1
+ if (!String.prototype.trim)
2
+ {
3
+ String.prototype.trim = function ()
4
+ {
5
+ return this.replace(/^\s+/,'').replace(/\s+$/,'');
6
+ }
7
+ }
8
+
9
+ if (!Array.prototype.indexOf)
10
+ {
11
+ Array.prototype.indexOf = function (item)
12
+ {
13
+ var l = this.length;
14
+
15
+ if (l === 0)
16
+ {
17
+ return -1;
18
+ }
19
+
20
+ for (var i = 0; i < l; ++i)
21
+ {
22
+ if (this[i] === searchElement)
23
+ {
24
+ return i;
25
+ }
26
+ }
27
+
28
+ return -1;
29
+ }
30
+ }
File without changes
File without changes
@@ -0,0 +1,7 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright © 2011-2012 Jesse Sielaff
4
+ #
5
+ require 'rack'
6
+ require_relative './rack/mix-rack'
7
+ require_relative './mix-language'
@@ -0,0 +1,24 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright © 2011-2012 Jesse Sielaff
4
+ #
5
+ require_relative '../mix-language.rb'
6
+
7
+ class MixRack
8
+ COMPILER = MixCompiler.new
9
+
10
+ def MixRack.root_dir= (dir)
11
+ @@root = dir
12
+ end
13
+
14
+ def MixRack.call (env)
15
+ file_path = File.expand_path("./#{env["PATH_INFO"]}.mix", @@root)
16
+ js_output_string = COMPILER.compile_to_string(file_path)
17
+
18
+ if File.exists?(file_path)
19
+ return 200, {"Content-Type" => "application/javascript"}, [js_output_string]
20
+ else
21
+ return 404, {"Content-Type" => "text/plain"}, []
22
+ end
23
+ end
24
+ end
@@ -11,35 +11,36 @@ class MixCompiler
11
11
  @translator = MixTranslator.new
12
12
  end
13
13
 
14
- def basename (filename)
15
- filename.match(/(.*?)(\.mix$|$)/)[1]
16
- end
17
-
18
14
  def combine_strings (js_string, ast_string, symbol_string)
19
15
  js_string
20
16
  .sub(%%'...Replace with AST nodes...'%, ast_string)
21
17
  .sub(%%'...Replace with symbol table...'%, symbol_string)
22
18
  end
23
19
 
24
- def compile (filename)
25
- mix_files, js_string = precompile_js(filename)
20
+ def compile_to_file (absolute_mix_path)
21
+ js_output_string = compile_to_string(absolute_mix_path)
22
+ write_js_file(absolute_mix_path, js_output_string)
23
+ end
24
+
25
+ def compile_to_string (absolute_mix_path)
26
+ mix_files, js_string = precompile_js(absolute_mix_path)
26
27
  mix_tokens = tokenize_mix_files(mix_files)
27
28
  mix_node = parse_token_stream(mix_tokens)
28
29
  ast_string, symbol_string = translate_to_js(mix_node)
29
30
  js_output_string = combine_strings(js_string, ast_string, symbol_string)
30
- write_js_file(filename, js_output_string)
31
+
32
+ return js_output_string
31
33
 
32
34
  rescue SyntaxError => e
33
- write_js_file("window.onload = function () { if (console && console.log) console.log(\"SyntaxError: #{e.message}\"); };")
35
+ "window.onload = function () { if (console && console.log) console.log(\"SyntaxError: #{e.message}\"); };"
34
36
  end
35
37
 
36
38
  def parse_token_stream (mix_tokens)
37
39
  @parser.parse(mix_tokens)
38
40
  end
39
41
 
40
- def precompile_js (filename)
41
- mix_filename = basename(filename) + '.mix'
42
- @precompiler.precompile(mix_filename)
42
+ def precompile_js (absolute_mix_path)
43
+ @precompiler.precompile(absolute_mix_path)
43
44
  end
44
45
 
45
46
  def tokenize_mix_files (mix_files)
@@ -52,9 +53,11 @@ class MixCompiler
52
53
  @translator.translate(node)
53
54
  end
54
55
 
55
- def write_js_file (filename, js_output_string)
56
- js_filename = basename(filename) + '.js'
57
- File.open(js_filename,'w') {|f| f.write(js_output_string) }
58
- puts "Writing to #{js_filename}"
56
+ def write_js_file (absolute_mix_path, js_output_string)
57
+ filename = File.basename(absolute_mix_path, '.mix') + '.js'
58
+ absolute_js_path = File.expand_path(filename, File.dirname(absolute_mix_path))
59
+
60
+ File.open(absolute_js_path, 'w') {|f| f.write(js_output_string) }
61
+ puts "Writing to #{filename}"
59
62
  end
60
63
  end
@@ -5,12 +5,14 @@
5
5
  require 'strscan'
6
6
 
7
7
  class MixPrecompiler
8
+ GEM_INCLUDES_DIR = File.expand_path("../../includes", __FILE__)
9
+
8
10
  JS_BASE_FILES = %w{
9
- env eval object array
10
- false function null
11
- number string true }
11
+ env eval ie object
12
+ array false function
13
+ null number string true }
12
14
  .each_with_object({}) do |x,h|
13
- filename = File.expand_path("../../javascript/base/#{x}.js", __FILE__)
15
+ filename = File.expand_path("../../javascript/#{x}.js", __FILE__)
14
16
  h[filename] = File.read(filename)
15
17
  end
16
18
 
@@ -20,87 +22,77 @@ class MixPrecompiler
20
22
  NUMBER OBJECT STRING
21
23
  TEXT TRUE =
22
24
 
23
- def build_js_string
24
- @js_string = @js_files.map {|*,script| script }.join ?\n
25
+ def build_js_string (js_files)
26
+ native_js = js_files.map {|*,script| script }.join ?\n
25
27
 
26
- replace_type_variables
27
- replace_case_markers
28
- minify
28
+ replace_type_variables!(native_js)
29
+ replace_case_markers!(native_js)
30
+ minify!(native_js)
29
31
 
30
- @js_string = <<-JAVASCRIPT
31
- mix_result_should_be_undefined=function(){var SYMBOLS='...Replace with symbol table...';#{@js_string};
32
+ <<-JAVASCRIPT
33
+ mix_result_should_be_undefined=function(){var SYMBOLS='...Replace with symbol table...';#{native_js};
32
34
  window.onload=function(){startup();evaluate('...Replace with AST nodes...');}}();
33
35
  JAVASCRIPT
34
36
  end
35
37
 
36
- def collect_scripts
37
- @mix_files = {}
38
- @js_files = JS_BASE_FILES.dup
38
+ def locate_file (path)
39
+ absolute_path_local = File.expand_path(path, @local_mix_dir)
40
+ absolute_path_gem = File.expand_path(path, GEM_INCLUDES_DIR)
41
+
42
+ return absolute_path_local if File.exists?(absolute_path_local)
43
+ return absolute_path_gem if File.exists?(absolute_path_gem)
39
44
 
40
- scan_mix_file(@primary_filename)
45
+ puts "could not locate file #{path} in local directory or in gem includes"
46
+ exit
47
+ end
48
+
49
+ def minify! (string)
50
+ string.gsub!(/\s*\/\*.*?\*\/\s*/m, ?\n)
51
+ string.gsub!(/\s*\n\s*/, ?\n)
52
+ string.gsub!(/\s*([{;:,=])\s*/, '\1')
53
+ string.gsub!("\n}", ?})
41
54
  end
42
55
 
43
- def minify
44
- @js_string.gsub!(/\s*\/\*.*?\*\/\s*/m, ?\n)
45
- @js_string.gsub!(/\s*\n\s*/, ?\n)
46
- @js_string.gsub!(/\s*([{;:,=])\s*/, '\1')
47
- @js_string.gsub!("\n}", ?})
56
+ def precompile (absolute_mix_path)
57
+ @local_mix_dir = File.dirname(absolute_mix_path)
58
+ @paths_seen = []
59
+ @files = {"mix" => {}, "js" => {}.merge(JS_BASE_FILES)}
60
+
61
+ @files["mix"][absolute_mix_path] = read_file(absolute_mix_path)
62
+ js_string = build_js_string(@files["js"])
63
+
64
+ return @files["mix"], js_string
48
65
  end
49
66
 
50
- def precompile (filename)
51
- @primary_filename = filename
52
- collect_scripts
53
- build_js_string
67
+ def read_file (absolute_path)
68
+ script = File.read(absolute_path) + ?\n
69
+ scan_includes(script)
54
70
 
55
- return @mix_files, @js_string
71
+ return script
56
72
  end
57
73
 
58
- def replace_case_markers
74
+ def replace_case_markers! (string)
59
75
  MixTranslator::NODE_TYPES.each do |k,v|
60
- @js_string.gsub!("case #{k}:","case #{v}:")
76
+ string.gsub!("case #{k}:","case #{v}:")
61
77
  end
62
78
  end
63
79
 
64
- def replace_type_variables
80
+ def replace_type_variables! (string)
65
81
  JS_OBJECT_TYPES.each_with_index do |t,i|
66
- @js_string.gsub!("#{t}_TYPE", i.to_s)
82
+ string.gsub!("#{t}_TYPE", i.to_s)
67
83
  end
68
84
  end
69
85
 
70
86
  def scan_includes (script)
71
87
  ss = StringScanner.new(script)
72
- js_filenames = []
73
88
 
74
- while ss.scan(/~~\s*(.*)\.(js|mix)\s*\n/)
75
- basename = ss[1]
76
- type = ss[2]
77
- filename = "#{basename}.#{type}"
89
+ while ss.scan(/^\/\*\s*(.*)\.(js)\s*\*\/\s*\n/) || ss.scan(/^~~\s*(.*)\.(js|mix)\s*\n/)
90
+ path, ext = ss[1], ss[2]
91
+ absolute_path = locate_file("#{path}.#{ext}")
78
92
 
79
- if type == 'mix'
80
- unless [@primary_filename, *@mix_files.keys].include?(filename)
81
- scan_mix_file(filename)
82
- end
83
- else
84
- js_filenames << filename
85
- end
86
- end
87
-
88
- js_filenames.each do |name|
89
- filename = File.expand_path("../../javascript/#{name}", __FILE__)
90
- next if @js_files.has_key?(filename)
91
- @js_files[filename] = File.read(filename)
92
- end
93
- end
94
-
95
- def scan_mix_file (filename)
96
- unless File.exists? filename
97
- puts "no such file #{filename}"
98
- exit
93
+ @paths_seen.include?(absolute_path) ? next : (@paths_seen << absolute_path)
94
+
95
+ @files[ext][absolute_path] = read_file(absolute_path)
99
96
  end
100
-
101
- script = File.read(filename) + ?\n
102
- scan_includes(script)
103
-
104
- @mix_files[filename] = script
105
97
  end
106
98
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mix-language
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.2
5
+ version: 1.0.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jesse Sielaff
@@ -11,40 +11,47 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2012-01-07 00:00:00 Z
14
+ date: 2012-01-16 00:00:00 Z
15
15
  dependencies: []
16
16
 
17
17
  description: .
18
18
  email: jesse.sielaff@gmail.com
19
19
  executables:
20
20
  - mix-compile
21
+ - mixup
21
22
  extensions: []
22
23
 
23
24
  extra_rdoc_files: []
24
25
 
25
26
  files:
26
27
  - ./bin/mix-compile
27
- - ./lib/javascript/base/array.js
28
- - ./lib/javascript/base/env.js
29
- - ./lib/javascript/base/eval.js
30
- - ./lib/javascript/base/false.js
31
- - ./lib/javascript/base/function.js
32
- - ./lib/javascript/base/null.js
33
- - ./lib/javascript/base/number.js
34
- - ./lib/javascript/base/object.js
35
- - ./lib/javascript/base/string.js
36
- - ./lib/javascript/base/true.js
37
- - ./lib/javascript/dom/div.js
38
- - ./lib/javascript/dom/drag.js
39
- - ./lib/javascript/dom/element.js
40
- - ./lib/javascript/dom/event.js
41
- - ./lib/javascript/dom/mouse.js
42
- - ./lib/javascript/dom/table.js
43
- - ./lib/javascript/dom/td.js
44
- - ./lib/javascript/dom/text.js
45
- - ./lib/javascript/dom/tr.js
28
+ - ./bin/mixup
29
+ - ./lib/includes/dom/div.js
30
+ - ./lib/includes/dom/drag.js
31
+ - ./lib/includes/dom/element.js
32
+ - ./lib/includes/dom/event.js
33
+ - ./lib/includes/dom/mouse.js
34
+ - ./lib/includes/dom/table.js
35
+ - ./lib/includes/dom/td.js
36
+ - ./lib/includes/dom/text.js
37
+ - ./lib/includes/dom/tr.js
38
+ - ./lib/includes/dom.js
39
+ - ./lib/includes/event.js
40
+ - ./lib/javascript/array.js
41
+ - ./lib/javascript/env.js
42
+ - ./lib/javascript/eval.js
43
+ - ./lib/javascript/false.js
44
+ - ./lib/javascript/function.js
45
+ - ./lib/javascript/ie.js
46
+ - ./lib/javascript/null.js
47
+ - ./lib/javascript/number.js
48
+ - ./lib/javascript/object.js
49
+ - ./lib/javascript/string.js
50
+ - ./lib/javascript/true.js
46
51
  - ./lib/mix-language.rb
52
+ - ./lib/mix-rack.rb
47
53
  - ./lib/mix.rb
54
+ - ./lib/rack/mix-rack.rb
48
55
  - ./lib/ruby/compiler.rb
49
56
  - ./lib/ruby/mix.grammar
50
57
  - ./lib/ruby/mix.tab.rb
@@ -54,6 +61,7 @@ files:
54
61
  - ./lib/ruby/tokenizer.rb
55
62
  - ./lib/ruby/translator.rb
56
63
  - bin/mix-compile
64
+ - bin/mixup
57
65
  homepage:
58
66
  licenses: []
59
67