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.
- data/bin/mix-compile +4 -2
- data/bin/mixup +9 -0
- data/lib/includes/dom.js +6 -0
- data/lib/{javascript → includes}/dom/div.js +0 -0
- data/lib/{javascript → includes}/dom/drag.js +0 -0
- data/lib/{javascript → includes}/dom/element.js +0 -0
- data/lib/{javascript → includes}/dom/event.js +0 -0
- data/lib/{javascript → includes}/dom/mouse.js +0 -0
- data/lib/{javascript → includes}/dom/table.js +0 -0
- data/lib/{javascript → includes}/dom/td.js +0 -0
- data/lib/{javascript → includes}/dom/text.js +0 -0
- data/lib/{javascript → includes}/dom/tr.js +0 -0
- data/lib/includes/event.js +4 -0
- data/lib/javascript/{base/array.js → array.js} +0 -0
- data/lib/javascript/{base/env.js → env.js} +0 -0
- data/lib/javascript/{base/eval.js → eval.js} +0 -0
- data/lib/javascript/{base/false.js → false.js} +0 -0
- data/lib/javascript/{base/function.js → function.js} +0 -0
- data/lib/javascript/ie.js +30 -0
- data/lib/javascript/{base/null.js → null.js} +0 -0
- data/lib/javascript/{base/number.js → number.js} +0 -0
- data/lib/javascript/{base/object.js → object.js} +0 -0
- data/lib/javascript/{base/string.js → string.js} +0 -0
- data/lib/javascript/{base/true.js → true.js} +0 -0
- data/lib/mix-rack.rb +7 -0
- data/lib/rack/mix-rack.rb +24 -0
- data/lib/ruby/compiler.rb +18 -15
- data/lib/ruby/precompiler.rb +51 -59
- metadata +29 -21
data/bin/mix-compile
CHANGED
@@ -7,9 +7,11 @@ require 'mix-language'
|
|
7
7
|
|
8
8
|
Encoding.default_external = "UTF-8"
|
9
9
|
|
10
|
-
|
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.
|
17
|
+
MixCompiler.new.compile_to_file(absolute_mix_path)
|
data/bin/mixup
ADDED
data/lib/includes/dom.js
ADDED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
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
|
File without changes
|
File without changes
|
File without changes
|
data/lib/mix-rack.rb
ADDED
@@ -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
|
data/lib/ruby/compiler.rb
CHANGED
@@ -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
|
25
|
-
|
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
|
-
|
31
|
+
|
32
|
+
return js_output_string
|
31
33
|
|
32
34
|
rescue SyntaxError => e
|
33
|
-
|
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 (
|
41
|
-
|
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 (
|
56
|
-
|
57
|
-
File.
|
58
|
-
|
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
|
data/lib/ruby/precompiler.rb
CHANGED
@@ -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
|
10
|
-
false function
|
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
|
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
|
-
|
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
|
-
|
31
|
-
mix_result_should_be_undefined=function(){var SYMBOLS='...Replace with symbol table...';#{
|
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
|
37
|
-
|
38
|
-
|
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
|
-
|
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
|
44
|
-
@
|
45
|
-
@
|
46
|
-
@
|
47
|
-
|
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
|
51
|
-
|
52
|
-
|
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
|
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
|
-
|
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
|
-
|
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(
|
75
|
-
|
76
|
-
|
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
|
-
|
80
|
-
|
81
|
-
|
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.
|
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-
|
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
|
-
- ./
|
28
|
-
- ./lib/
|
29
|
-
- ./lib/
|
30
|
-
- ./lib/
|
31
|
-
- ./lib/
|
32
|
-
- ./lib/
|
33
|
-
- ./lib/
|
34
|
-
- ./lib/
|
35
|
-
- ./lib/
|
36
|
-
- ./lib/
|
37
|
-
- ./lib/
|
38
|
-
- ./lib/
|
39
|
-
- ./lib/javascript/
|
40
|
-
- ./lib/javascript/
|
41
|
-
- ./lib/javascript/
|
42
|
-
- ./lib/javascript/
|
43
|
-
- ./lib/javascript/
|
44
|
-
- ./lib/javascript/
|
45
|
-
- ./lib/javascript/
|
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
|
|