liquidscript 0.7.12 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 81818718ba26d7134d542545b3b19e9008f88ef4
4
- data.tar.gz: 8f3bcb8c445c7fec88a98f99dce14afdcc064dd6
3
+ metadata.gz: 20d97cd63491fe33e34d37dac00eab8bb7f5f7dd
4
+ data.tar.gz: 8bed64d2660f46dd9f4de474e756d6325fbd1f95
5
5
  SHA512:
6
- metadata.gz: 59c2abf77dc9bc5f570e18ad6a1e9a87052eba96ee0053effff3181baeaffb6e8c4ecad979e6c622194e57143fcbffaa20f436d13de0690721be516127bbbf23
7
- data.tar.gz: a22a56199f840defcdc4c107468449e32f982a2590ae1f25602c99df860f045ffdda4672e13f9662d44616b5c4d17723054e4c55312d6c7b803fb5731ded1d0b
6
+ metadata.gz: b992528f87b397087159d786bf4b7fef4b23841413ab317e0d15e1079ceb75f94ea165cfef0bfa43cb17612641478321be5f5158e5f902ae2a56a92b563e1055
7
+ data.tar.gz: 6092259df275c92ac0ffbf39acc0ac65bba66fd2c68d21eb11f39e562705f3ff4a1a82d8adcfe38ff5074d34c9e227e0f97cbe791363105d787dbf7687fa3e7e
@@ -32,6 +32,23 @@ module Liquidscript
32
32
  end
33
33
  end
34
34
 
35
+ # This applies only as many arguments as the block or function
36
+ # needs. This is just so that calling a method is easier,
37
+ # so the developer doesn't have to worry about arity and
38
+ # such.
39
+ #
40
+ # @param args [Object] passed to the call.
41
+ # @return [Object] the result of the call.
42
+ def apply(*args)
43
+ return call if arity == 0
44
+
45
+ if block_given?
46
+ call(*yield[0..arity])
47
+ else
48
+ call(*args[0..arity])
49
+ end
50
+ end
51
+
35
52
  # How many arguments the call can take. If this represents a
36
53
  # block that has tricks enabled, then this isn't an issue; if
37
54
  # it's a method call, however, it becomes important.
@@ -4,6 +4,7 @@ require "liquidscript/compiler/icr/literals"
4
4
  require "liquidscript/compiler/icr/classes"
5
5
  require "liquidscript/compiler/icr/helpers"
6
6
  require "liquidscript/compiler/icr/heredoc"
7
+ require "liquidscript/compiler/icr/groups"
7
8
 
8
9
  module Liquidscript
9
10
  module Compiler
@@ -14,6 +15,7 @@ module Liquidscript
14
15
  include Literals
15
16
  include Classes
16
17
  include Helpers
18
+ include Groups
17
19
 
18
20
  # (see Base#initialize)
19
21
  def initialize(*)
@@ -145,82 +145,6 @@ module Liquidscript
145
145
  end)
146
146
  end
147
147
  end
148
-
149
- [:if, :elsif].each do |key|
150
- define_method(:"compile_#{key}") do
151
- shift key
152
- shift :lparen
153
- conditional = compile_vexpression
154
- shift :rparen
155
- shift :lbrace
156
-
157
- body = collect_compiles(:expression, :rbrace)
158
-
159
- if peek?(:elsif, :else)
160
- code key, conditional, body, expect(:elsif, :else)
161
- else
162
- code key, conditional, body
163
- end
164
- end
165
- end
166
-
167
- def compile_unless
168
- shift :unless
169
- shift :lparen
170
- conditional = compile_vexpression
171
- shift :rparen
172
- shift :lbrace
173
-
174
- body = collect_compiles(:expression, :rbrace)
175
- code :unless, conditional, body
176
- end
177
-
178
- def compile_else
179
- shift :else
180
- shift :lbrace
181
-
182
- body = collect_compiles(:expression, :rbrace)
183
-
184
- code :else, body
185
- end
186
-
187
- def compile_try
188
- shift :try
189
- shift :lbrace
190
- try_body = collect_compiles(:expression, :rbrace)
191
-
192
- next_part = if peek?(:catch)
193
- _compile_catch
194
- elsif peek?(:finally)
195
- _compile_finally
196
- end
197
-
198
- code :try, try_body, next_part
199
- end
200
-
201
- def _compile_catch
202
- shift :catch
203
- shift :lparen
204
- var = shift :identifier
205
- shift :rparen
206
- shift :lbrace
207
- catch_body = collect_compiles(:expression, :rbrace)
208
-
209
- next_part = if peek?(:finally)
210
- _compile_finally
211
- end
212
-
213
- code :catch, var, catch_body, next_part
214
- end
215
-
216
- def _compile_finally
217
- shift :finally
218
- shift :lbrace
219
- finally_body = collect_compiles(:expression, :rbrace)
220
-
221
- code :finally, finally_body
222
- end
223
-
224
148
  end
225
149
  end
226
150
  end
@@ -39,6 +39,40 @@ module Liquidscript
39
39
  call = code :call, subject, *arguments
40
40
  call
41
41
  end
42
+
43
+
44
+ def compile_function_with_parameters(parameters)
45
+ shift :arrow
46
+
47
+ expressions = _build_set(parameters)
48
+
49
+ if peek?(:lbrace)
50
+ shift :lbrace
51
+ collect_compiles(:rbrace) do
52
+ expressions << compile_expression
53
+ end
54
+ else
55
+ expressions << compile_expression
56
+ end
57
+
58
+ code :function, @set.pop
59
+ end
60
+
61
+ private
62
+
63
+ def _build_set(parameters)
64
+ expressions = Liquidscript::ICR::Set.new
65
+ expressions.context = Liquidscript::ICR::Context.new
66
+ expressions.context.parent = top.context
67
+ expressions[:arguments] = parameters
68
+ @set << expressions
69
+
70
+ parameters.each do |parameter|
71
+ set(parameter).parameter!
72
+ end
73
+
74
+ expressions
75
+ end
42
76
  end
43
77
  end
44
78
  end
@@ -0,0 +1,68 @@
1
+ module Liquidscript
2
+ module Compiler
3
+ class ICR < Base
4
+ module Groups
5
+
6
+ def _compile_group(type, cond = false, continue = false)
7
+ shift type
8
+
9
+ if cond ||= block_given?
10
+ shift :lparen
11
+ if block_given?
12
+ conditional = yield
13
+ else
14
+ conditional = compile_vexpression
15
+ end
16
+ shift :rparen
17
+ end
18
+
19
+ if continue && !continue.is_a?(Array)
20
+ continue = [:elsif, :else]
21
+ end
22
+
23
+ shift :lbrace
24
+ body = collect_compiles(:expression, :rbrace)
25
+
26
+ args = [type]
27
+ args << conditional if cond
28
+ args << body
29
+ args << expect(*continue) if continue and peek?(*continue)
30
+
31
+ code(*args)
32
+ end
33
+
34
+ private :_compile_group
35
+
36
+ def compile_if
37
+ _compile_group(:if, true, true)
38
+ end
39
+
40
+ def compile_elsif
41
+ _compile_group(:elsif, true, true)
42
+ end
43
+
44
+ def compile_unless
45
+ _compile_group(:unless)
46
+ end
47
+
48
+ def compile_else
49
+ _compile_group(:else, false)
50
+ end
51
+
52
+ def compile_try
53
+ _compile_group(:try, false, [:catch, :finally])
54
+ end
55
+
56
+ def compile_catch
57
+ _compile_group(:catch, false, [:finally]) do
58
+ shift :identifier
59
+ end
60
+ end
61
+
62
+ def compile_finally
63
+ _compile_group(:finally)
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -94,16 +94,7 @@ module Liquidscript
94
94
  def compile_iheredoc_begin
95
95
  start = shift :iheredoc_begin
96
96
  contents = [start]
97
-
98
- loop do
99
- contents << compile_vexpression
100
- if peek?(:iheredoc_begin)
101
- contents << shift(:iheredoc_begin)
102
- else
103
- contents << shift(:iheredoc)
104
- false
105
- end
106
- end
97
+ _compile_interop(:iheredoc, contents)
107
98
 
108
99
  top[:heredocs][top[:herenum]].body = contents
109
100
  top[:herenum] += 1
@@ -113,17 +104,7 @@ module Liquidscript
113
104
  def compile_istring_begin
114
105
  start = shift :istring_begin
115
106
  contents = [start]
116
-
117
- loop do
118
- contents << compile_vexpression
119
- if peek?(:istring_begin)
120
- contents << shift(:istring_begin)
121
- else
122
- contents << shift(:istring)
123
- false
124
- end
125
- end
126
-
107
+ _compile_interop(:istring, contents)
127
108
 
128
109
  code :interop, *contents
129
110
  end
@@ -183,35 +164,18 @@ module Liquidscript
183
164
  end
184
165
  end
185
166
 
186
- def compile_function_with_parameters(parameters)
187
- shift :arrow
188
-
189
- expressions = Liquidscript::ICR::Set.new
190
- expressions.context = Liquidscript::ICR::Context.new
191
- expressions.context.parent = top.context
192
- expressions[:arguments] = parameters
193
- @set << expressions
194
-
195
- parameters.each do |parameter|
196
- set(parameter).parameter!
197
- end
198
-
199
-
200
- expression = action do
201
- expressions << compile_expression
202
- end
167
+ private
203
168
 
204
- unless peek?(:lbrace)
205
- expression.call
206
- else
207
- shift :lbrace
208
- loop do
209
- expect :rbrace => action.end_loop,
210
- :_ => expression
169
+ def _compile_interop(type, contents)
170
+ loop do
171
+ contents << compile_vexpression
172
+ if peek?(:"#{type}_begin")
173
+ contents << shift(:"#{type}_begin")
174
+ else
175
+ contents << shift(type)
176
+ false
211
177
  end
212
178
  end
213
-
214
- code :function, @set.pop
215
179
  end
216
180
 
217
181
  end
@@ -1,4 +1,4 @@
1
- module Liquidscript
1
+ module Liquidscript
2
2
  module Generator
3
3
  class Javascript
4
4
  module Objects
@@ -35,113 +35,95 @@
35
35
  end
36
36
 
37
37
  def generate_class(code)
38
- body = buffer
39
- class_name = code[1].value
40
-
41
- in_module(class_name) do |last_module|
42
- body.block 7 - @indent, <<-JS
43
- #{class_name} = #{class_name} || function #{class_name}() {
44
- if(this.initialize) {
45
- this.initialize.apply(this, arguments);
46
- }
47
- }
48
- JS
49
-
50
- if code[2]
51
- body.block 8 - @indent, <<-JS
52
- #{class_name}.prototype.__proto__ = #{code[2].value};
53
- JS
54
- end
38
+ _context :name => code[1].value,
39
+ :inherit => code[2],
40
+ :parts => code[3],
41
+ :inheritance => "%{name}.prototype.__proto__ = %{inherit};\n",
42
+ :identifier => "%{name}.prototype.%{value} = %{replace};\n",
43
+ :istring => "%{name}.prototype[\"%{value}\"] = %{replace};\n",
44
+ :property => "%{name}.%{value} = %{replace};\n",
45
+ :head => "%{name} = %{name} || function %{name}() { " +
46
+ "if(this.initialize) { this.initialize.apply(this, " +
47
+ "arguments); } };\n"
48
+ end
55
49
 
56
- code[3].each do |part|
57
- k, v = part
58
- case k.type
59
- when :identifier
60
- body.block 8 - @indent, <<-JS
61
- #{class_name}.prototype.#{k.value} = #{replace(v)};
62
- JS
63
- when :istring
64
- body.block 8 - @indent, <<-JS
65
- #{class_name}.prototype[#{k.value}] = #{replace(v)};
66
- JS
67
- when :property
68
- if k[1].value != "this"
69
- raise InvalidCodeError.new(k[1].value)
70
- end
71
-
72
- body.block 8 - @indent, <<-JS
73
- #{class_name}.#{k[2]} = #{replace(v)};
74
- JS
75
- when :class
76
- body << generate_class(part)
77
- when :module
78
- body << generate_module(part)
79
- end
80
- end
50
+ def generate_module(code)
51
+ _context :name => code[1].value,
52
+ :parts => code[2],
53
+ :head => "%{name} = %{name} || {};\n",
54
+ :identifier => "%{name}.%{value} = %{replace};\n",
55
+ :istring => "%{name}[\"%{value}\"] = %{replace};\n",
56
+ :property => false
57
+ end
81
58
 
82
- if last_module
83
- body.block 7, <<-JS
84
- #{last_module}.#{class_name} = #{class_name}
85
- JS
86
- end
59
+ protected
87
60
 
88
- end
61
+ def current_module
62
+ @modules.last
63
+ end
89
64
 
90
- body
65
+ def in_module(module_name)
66
+ @modules << module_name
67
+ out = yield @modules[-2]
68
+ @modules.pop
69
+ out
91
70
  end
92
71
 
93
- def generate_module(code)
72
+ def _context(options)
94
73
  body = buffer
95
- module_name = code[1].value
96
-
97
- in_module(module_name) do |last_module|
98
- body << "#{module_name} = #{module_name} || {};"
99
-
100
- code[2].each do |part|
101
- k, v = part
102
- to_match = if k.is_a? Symbol
103
- k
104
- else
105
- k.type
106
- end
107
-
108
- case to_match
109
- when :identifier
110
- body.block 7, <<-JS
111
- #{module_name}.#{k.value} = #{replace(v)};
112
- JS
113
- when :istring
114
- body.block 7, <<-JS
115
- #{module_name}["#{k.value}"] = #{replace(v)};
116
- JS
117
- when :class
118
- body << generate_class(part)
119
- when :module
120
- body << generate_module(part)
121
- end
74
+ name = options[:name]
75
+ opts = { :name => name }
76
+
77
+ in_module(name) do |last_module|
78
+ opts[:last] = last_module
79
+ _build_header(body, options, opts)
80
+
81
+ options[:parts].each do |part|
82
+ _build_element(body, options, opts, part)
122
83
  end
123
84
 
124
85
  if last_module
125
- body.block 7, <<-JS
126
- #{last_module}.#{module_name} = #{module_name}
127
- JS
86
+ body << "#{last_module}.#{name} = #{name};\n"
128
87
  end
129
88
  end
130
89
 
131
90
  body
132
91
  end
133
92
 
134
- protected
93
+ def _build_element(body, options, opts, part)
94
+ k, v = part
135
95
 
136
- def current_module
137
- @modules.last
96
+ type = if k.is_a? Symbol then k else k.type end
97
+
98
+ case type
99
+ when :identifier, :istring
100
+ opts[:value] = k.value
101
+ opts[:replace] = replace(v)
102
+
103
+ body << sprintf(options[k.type], opts)
104
+ when :property
105
+ opts[:value] = k[2]
106
+ opts[:replace] = replace(v)
107
+
108
+ if k[1].value == "this" && options[k.type]
109
+ body << sprintf(options[k.type], opts)
110
+ else
111
+ raise InvalidCodeError.new(k[1].value)
112
+ end
113
+ when :class
114
+ body << generate_class(part)
115
+ when :module
116
+ body << generate_module(part)
117
+ end
138
118
  end
139
119
 
140
- def in_module(module_name)
141
- @modules << module_name
142
- out = yield @modules[-2]
143
- @modules.pop
144
- out
120
+ def _build_header(body, options, opts)
121
+ body << sprintf(options[:head], opts)
122
+
123
+ if options[:inherit]
124
+ opts[:inherit] = options[:inherit].value
125
+ body << sprintf(options[:inheritance], opts)
126
+ end
145
127
  end
146
128
 
147
129
  end
@@ -32,7 +32,7 @@ module Liquidscript
32
32
  else
33
33
  body = v.to_s.gsub(/\"/, "\\\"")
34
34
 
35
- if body.include? " "
35
+ if body.match(/[\s]/)
36
36
  "\"#{body}\""
37
37
  elsif body.length == 0
38
38
  nil
@@ -46,7 +46,7 @@ module Liquidscript
46
46
  )
47
47
 
48
48
  set :binops, %w(
49
- * / ^
49
+ * / ^ %
50
50
  << >> >>>
51
51
  === ==
52
52
  !== !=
@@ -1,5 +1,5 @@
1
1
  module Liquidscript
2
2
 
3
3
  # The current version of liquidscript.
4
- VERSION = "0.7.12".freeze
4
+ VERSION = "0.8.0".freeze
5
5
  end
@@ -25,7 +25,7 @@ compiled: |
25
25
  if(this.initialize) {
26
26
  this.initialize.apply(this, arguments);
27
27
  }
28
- }
28
+ };
29
29
 
30
30
  Test.prototype.wee = function() {
31
31
  console.log(2);
@@ -38,6 +38,6 @@ compiled: |
38
38
  Test.test = function() {
39
39
  "class method!";
40
40
  };
41
- Something.Test = Test;
41
+ Something.Test = Test;;
42
42
 
43
43
  module.exports = Something;
@@ -21,7 +21,7 @@ compiled: |
21
21
  if(this.initialize) {
22
22
  this.initialize.apply(this, arguments);
23
23
  }
24
- }
24
+ };
25
25
 
26
26
  Thing.prototype.initialize = function() {
27
27
  this.test = new Test();
@@ -30,4 +30,4 @@ compiled: |
30
30
  Thing.prototype.do = function(thing) {
31
31
  return this.test.do(thing);
32
32
  };
33
- SomeModule.Thing = Thing;
33
+ SomeModule.Thing = Thing;;
@@ -22,7 +22,7 @@ compiled: |
22
22
  if(this.initialize) {
23
23
  this.initialize.apply(this, arguments);
24
24
  }
25
- }
25
+ };
26
26
 
27
27
  Greeter.prototype.initialize = function(name) {
28
28
  this.name = name;
@@ -2,8 +2,8 @@ data: |
2
2
  console.log(<<TEST, <<-MAYBE)
3
3
  hello world
4
4
  TEST
5
- hello #{console} durr #{console.log}
5
+ hello #{console} durr #{console.log} brrr
6
6
  MAYBE
7
7
 
8
8
  compiled: |
9
- console.log("hello world", "hello " + console + " durr " + console.log + "");
9
+ console.log("hello world", "hello " + console + " durr " + console.log + " brrr");
@@ -0,0 +1,17 @@
1
+ data: |
2
+ string = "hello
3
+
4
+ world" # this'll translate to "hello\n\nworld"
5
+
6
+ single = 'test # there doesn't need to be an endquote here.
7
+
8
+ another-string = <<-TEST
9
+ #{string}, foo bar #{single} fffffuuu
10
+ TEST
11
+
12
+ compiled: |
13
+ var string,single,anotherString;
14
+
15
+ string = "hello\n\nworld";
16
+ single = 'test';
17
+ anotherString = " " + string + ", foo bar " + single + " fffffuuu";
@@ -7,6 +7,6 @@ RSpec::Matchers.define :run do
7
7
  end
8
8
 
9
9
  failure_message_for_should do
10
- "Expected to run, got:\n#{@message.stderr}"
10
+ "Expected to run, got:\n#{@message.stderr}\n\n#{@output}"
11
11
  end
12
12
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: liquidscript
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.12
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Rodi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-04 00:00:00.000000000 Z
11
+ date: 2014-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -139,6 +139,7 @@ files:
139
139
  - lib/liquidscript/compiler/icr/classes.rb
140
140
  - lib/liquidscript/compiler/icr/expressions.rb
141
141
  - lib/liquidscript/compiler/icr/functions.rb
142
+ - lib/liquidscript/compiler/icr/groups.rb
142
143
  - lib/liquidscript/compiler/icr/helpers.rb
143
144
  - lib/liquidscript/compiler/icr/heredoc.rb
144
145
  - lib/liquidscript/compiler/icr/literals.rb
@@ -193,6 +194,7 @@ files:
193
194
  - spec/fixtures/set.generate.yml
194
195
  - spec/fixtures/string.compile.yml
195
196
  - spec/fixtures/string.generate.yml
197
+ - spec/fixtures/string2.generate.yml
196
198
  - spec/fixtures/underscore.js
197
199
  - spec/fixtures/underscore.liq
198
200
  - spec/liquidscript/buffer_spec.rb
@@ -254,6 +256,7 @@ test_files:
254
256
  - spec/fixtures/set.generate.yml
255
257
  - spec/fixtures/string.compile.yml
256
258
  - spec/fixtures/string.generate.yml
259
+ - spec/fixtures/string2.generate.yml
257
260
  - spec/fixtures/underscore.js
258
261
  - spec/fixtures/underscore.liq
259
262
  - spec/liquidscript/buffer_spec.rb