liquidscript 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -1
- data/Guardfile +0 -4
- data/Rakefile +2 -2
- data/lib/liquidscript/compiler/icr/expressions.rb +2 -1
- data/lib/liquidscript/compiler/icr/literals.rb +17 -3
- data/lib/liquidscript/generator/javascript/literals.rb +18 -2
- data/lib/liquidscript/scanner.rb +3 -31
- data/lib/liquidscript/scanner/base.rb +84 -0
- data/lib/liquidscript/scanner/base/builder.rb +38 -0
- data/lib/liquidscript/scanner/base/context.rb +83 -0
- data/lib/liquidscript/scanner/base/dsl.rb +27 -0
- data/lib/liquidscript/scanner/base/lexer.rb +80 -0
- data/lib/liquidscript/scanner/liquidscript.rb +128 -0
- data/lib/liquidscript/scanner/token.rb +2 -6
- data/lib/liquidscript/template.rb +0 -1
- data/lib/liquidscript/version.rb +1 -1
- data/spec/fixtures/complex.generate.yml +1 -0
- data/spec/fixtures/main.compile.yml +1 -1
- data/spec/fixtures/string.compile.yml +15 -0
- data/spec/fixtures/string.generate.yml +3 -1
- data/spec/{lib/liquidscript → liquidscript}/buffer_spec.rb +0 -0
- data/spec/{lib/liquidscript → liquidscript}/compiler/icr_spec.rb +0 -0
- data/spec/{lib/liquidscript → liquidscript}/generator/javascript_spec.rb +7 -2
- data/spec/{lib/liquidscript → liquidscript}/icr/code_spec.rb +0 -0
- data/spec/{lib/liquidscript → liquidscript}/icr/context_spec.rb +0 -0
- data/spec/{lib/liquidscript → liquidscript}/icr/set_spec.rb +0 -0
- data/spec/{lib/liquidscript → liquidscript}/scanner/lexer_spec.rb +13 -15
- data/spec/{lib/liquidscript → liquidscript}/scanner/token_spec.rb +0 -0
- data/spec/{lib/liquidscript → liquidscript}/scanner_spec.rb +2 -2
- data/spec/spec_helper.rb +1 -0
- data/spec/support/helpers/lexer_helper.rb +1 -1
- data/spec/support/matchers/compile.rb +1 -1
- data/spec/support/matchers/generate.rb +2 -2
- metadata +28 -22
- data/lib/liquidscript/scanner/lexer.rb +0 -805
- data/lib/liquidscript/scanner/lexer.rl +0 -118
@@ -0,0 +1,128 @@
|
|
1
|
+
module Liquidscript
|
2
|
+
module Scanner
|
3
|
+
class Liquidscript < Base
|
4
|
+
|
5
|
+
include Base::DSL
|
6
|
+
|
7
|
+
attr_reader :tokens
|
8
|
+
|
9
|
+
define do
|
10
|
+
default_context :main
|
11
|
+
|
12
|
+
context :main do
|
13
|
+
set :number, %r{
|
14
|
+
-? [0-9][1-9]* # the base of the number
|
15
|
+
(\.[0-9]+)? # decmial portion, if needed
|
16
|
+
([eE][+-]?[0-9]+)? # scientific notation
|
17
|
+
}x
|
18
|
+
|
19
|
+
set :string, %r{
|
20
|
+
'
|
21
|
+
[A-Za-z0-9_$\-]+
|
22
|
+
'?
|
23
|
+
}x
|
24
|
+
|
25
|
+
set :unops, %w(
|
26
|
+
!
|
27
|
+
++
|
28
|
+
--
|
29
|
+
~
|
30
|
+
new
|
31
|
+
return
|
32
|
+
typeof
|
33
|
+
)
|
34
|
+
|
35
|
+
set :binops, %w(
|
36
|
+
+ - * / & | ^
|
37
|
+
<< >> >>>
|
38
|
+
== ===
|
39
|
+
!= !==
|
40
|
+
> >=
|
41
|
+
< <=
|
42
|
+
&& ||
|
43
|
+
instanceof
|
44
|
+
or and
|
45
|
+
)
|
46
|
+
|
47
|
+
set :keywords, %w(
|
48
|
+
undefined
|
49
|
+
null
|
50
|
+
true
|
51
|
+
false
|
52
|
+
)
|
53
|
+
|
54
|
+
set :identifier, %r{[A-Za-z_$][A-Za-z0-9_$]*}
|
55
|
+
|
56
|
+
on("class") { emit :class }
|
57
|
+
on("module") { emit :module }
|
58
|
+
on("if") { emit :if }
|
59
|
+
on("unless") { emit :unless }
|
60
|
+
on("elsif") { emit :elsif }
|
61
|
+
on("else") { emit :else }
|
62
|
+
on(:number) { |m| emit :number, m }
|
63
|
+
on(:string) { |m| emit :sstring, m }
|
64
|
+
on(:keywords) { |m| emit :keyword, m }
|
65
|
+
on(:unops) { |m| emit :unop, m }
|
66
|
+
on("->") { emit :arrow }
|
67
|
+
on("=") { emit :equal }
|
68
|
+
on("{") { emit :lbrack }
|
69
|
+
on("(") { emit :lparen }
|
70
|
+
on("[") { emit :lbrace }
|
71
|
+
on("}") { emit :rbrack }
|
72
|
+
on(")") { emit :rparen }
|
73
|
+
on("]") { emit :rbrace }
|
74
|
+
on(":") { emit :colon }
|
75
|
+
on(".") { emit :prop }
|
76
|
+
on(",") { emit :comma }
|
77
|
+
on("\n") { line!; emit :newline}
|
78
|
+
on(:binops) { |m| emit :binop, m }
|
79
|
+
on(:identifier) { |m| emit :identifier, m }
|
80
|
+
|
81
|
+
on(/\"/ => :istring)
|
82
|
+
on(/#.*?\n/) { }
|
83
|
+
on(/[\s]/) { }
|
84
|
+
on(:_) { error }
|
85
|
+
end
|
86
|
+
|
87
|
+
context :istring do
|
88
|
+
init do
|
89
|
+
@buffer = []
|
90
|
+
end
|
91
|
+
|
92
|
+
on(/\\"/) { |m| @buffer << m }
|
93
|
+
on(/"/) do
|
94
|
+
emit :istring, @buffer.join
|
95
|
+
exit
|
96
|
+
end
|
97
|
+
|
98
|
+
on(/\#\{(.*?)\}/) do |_, b|
|
99
|
+
emit :istring_begin, @buffer.join
|
100
|
+
lex :main => b
|
101
|
+
@buffer = []
|
102
|
+
end
|
103
|
+
|
104
|
+
on(:_) { |m| @buffer << m }
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def initialize(*)
|
109
|
+
@line = 1
|
110
|
+
@cstart = 0
|
111
|
+
super
|
112
|
+
end
|
113
|
+
|
114
|
+
def line!
|
115
|
+
@line += 1
|
116
|
+
@cstart = @scanner.pos
|
117
|
+
end
|
118
|
+
|
119
|
+
def line
|
120
|
+
@line
|
121
|
+
end
|
122
|
+
|
123
|
+
def column
|
124
|
+
@scanner.pos - @cstart
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require "liquidscript/icr/representable"
|
2
2
|
|
3
3
|
module Liquidscript
|
4
|
-
|
4
|
+
module Scanner
|
5
5
|
class Token
|
6
6
|
|
7
7
|
attr_accessor :type
|
@@ -16,11 +16,7 @@ module Liquidscript
|
|
16
16
|
@type = type
|
17
17
|
@line = line
|
18
18
|
@column = column
|
19
|
-
@value =
|
20
|
-
value.pack("c*")
|
21
|
-
rescue NoMethodError, TypeError
|
22
|
-
value
|
23
|
-
end
|
19
|
+
@value = value
|
24
20
|
end
|
25
21
|
|
26
22
|
def to_a
|
data/lib/liquidscript/version.rb
CHANGED
@@ -3,9 +3,11 @@ data: |
|
|
3
3
|
foo = 'world
|
4
4
|
thing = "hello \" test
|
5
5
|
world"
|
6
|
+
interop = "hello #{foo}"
|
6
7
|
|
7
8
|
compiled: |
|
8
|
-
var test, foo, thing;
|
9
|
+
var test, foo, thing, interop;
|
9
10
|
test = "hello";
|
10
11
|
foo = 'world';
|
11
12
|
thing = "hello \" test \n world";
|
13
|
+
interop = ["hello ", foo, ""].join('');
|
File without changes
|
File without changes
|
@@ -1,14 +1,19 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
require "yaml"
|
3
3
|
|
4
|
-
describe
|
4
|
+
describe Generator::Javascript do
|
5
5
|
describe "with fixtures" do
|
6
6
|
Dir.glob("spec/fixtures/*.generate.yml") do |file|
|
7
7
|
content = YAML.load_file file
|
8
8
|
file =~ /spec\/fixtures\/(.*)\.generate\.yml/
|
9
9
|
|
10
10
|
it "generates #{$1}" do
|
11
|
-
|
11
|
+
begin
|
12
|
+
expect(content["data"]).to generate(content["compiled"])
|
13
|
+
rescue Error
|
14
|
+
p Scanner::Liquidscript.new(content["data"]).each.to_a
|
15
|
+
raise
|
16
|
+
end
|
12
17
|
end
|
13
18
|
end
|
14
19
|
end
|
File without changes
|
File without changes
|
File without changes
|
@@ -1,14 +1,6 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
describe Liquidscript::Scanner::
|
4
|
-
subject { described_class.new }
|
5
|
-
describe "#emit" do
|
6
|
-
it "pushes a token" do
|
7
|
-
subject.instance_exec { @data = "hello"; @ts = 0; @te = 6 }
|
8
|
-
subject.emit(:test)
|
9
|
-
expect(subject.tokens.first).to be_token(:test, "hello")
|
10
|
-
end
|
11
|
-
end
|
3
|
+
describe Liquidscript::Scanner::Liquidscript, :lexer_helper do
|
12
4
|
|
13
5
|
describe "#perform" do
|
14
6
|
it "scans a number" do
|
@@ -19,29 +11,35 @@ describe Liquidscript::Scanner::Lexer, :lexer_helper do
|
|
19
11
|
|
20
12
|
it "scans a string" do
|
21
13
|
scan('"hello world" ').should eq [
|
22
|
-
[:
|
14
|
+
[:istring, 'hello world']
|
23
15
|
]
|
24
16
|
|
25
17
|
scan(" 'foobar").should eq [
|
26
18
|
[:sstring, "'foobar"]
|
27
19
|
]
|
20
|
+
|
21
|
+
scan('"hello #{world}"').should eq [
|
22
|
+
[:istring_begin, "hello "],
|
23
|
+
[:identifier, "world"],
|
24
|
+
[:istring, ""]
|
25
|
+
]
|
28
26
|
end
|
29
27
|
|
30
28
|
it "scans an identifier" do
|
31
29
|
scan('test = 4').should eq [
|
32
30
|
[:identifier, "test"],
|
33
|
-
[:equal,
|
31
|
+
[:equal, nil],
|
34
32
|
[:number, "4"]
|
35
33
|
]
|
36
34
|
end
|
37
35
|
|
38
36
|
it "scans brackets" do
|
39
37
|
scan("{ test = 3 }").should eq [
|
40
|
-
[:lbrack,
|
38
|
+
[:lbrack, nil],
|
41
39
|
[:identifier, "test"],
|
42
|
-
[:equal,
|
40
|
+
[:equal, nil],
|
43
41
|
[:number, "3"],
|
44
|
-
[:rbrack,
|
42
|
+
[:rbrack, nil]
|
45
43
|
]
|
46
44
|
end
|
47
45
|
|
@@ -49,7 +47,7 @@ describe Liquidscript::Scanner::Lexer, :lexer_helper do
|
|
49
47
|
scan("return test = new foo").should eq [
|
50
48
|
[:unop, "return"],
|
51
49
|
[:identifier, "test"],
|
52
|
-
[:equal,
|
50
|
+
[:equal, nil],
|
53
51
|
[:unop, "new"],
|
54
52
|
[:identifier, "foo"]
|
55
53
|
]
|
File without changes
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
describe Liquidscript::Scanner do
|
4
|
-
subject { described_class.new("42") }
|
4
|
+
subject { described_class::Liquidscript.new("42") }
|
5
5
|
it { should be_a Enumerable }
|
6
6
|
|
7
7
|
describe "#each" do
|
@@ -10,7 +10,7 @@ describe Liquidscript::Scanner do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
context "with invalid input" do
|
13
|
-
subject { described_class.new("'
|
13
|
+
subject { described_class::Liquidscript.new("';") }
|
14
14
|
|
15
15
|
it "raises an error" do
|
16
16
|
expect {
|
data/spec/spec_helper.rb
CHANGED
@@ -29,13 +29,13 @@ RSpec::Matchers.define :generate do |v|
|
|
29
29
|
diffable
|
30
30
|
|
31
31
|
def generator(data)
|
32
|
-
compiler = Compiler::ICR.new(s = Scanner.new(data))
|
32
|
+
compiler = Compiler::ICR.new(s = Scanner::Liquidscript.new(data))
|
33
33
|
compiler.compile
|
34
34
|
Generator::Javascript.new(compiler.top)
|
35
35
|
end
|
36
36
|
|
37
37
|
def tree(data)
|
38
|
-
compiler = Compiler::ICR.new(Scanner.new(data))
|
38
|
+
compiler = Compiler::ICR.new(Scanner::Liquidscript.new(data))
|
39
39
|
compiler.compile
|
40
40
|
compiler.top.to_sexp
|
41
41
|
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.
|
4
|
+
version: 0.4.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-03-
|
11
|
+
date: 2014-03-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -130,8 +130,12 @@ files:
|
|
130
130
|
- lib/liquidscript/icr/sexp.rb
|
131
131
|
- lib/liquidscript/icr/variable.rb
|
132
132
|
- lib/liquidscript/scanner.rb
|
133
|
-
- lib/liquidscript/scanner/
|
134
|
-
- lib/liquidscript/scanner/
|
133
|
+
- lib/liquidscript/scanner/base.rb
|
134
|
+
- lib/liquidscript/scanner/base/builder.rb
|
135
|
+
- lib/liquidscript/scanner/base/context.rb
|
136
|
+
- lib/liquidscript/scanner/base/dsl.rb
|
137
|
+
- lib/liquidscript/scanner/base/lexer.rb
|
138
|
+
- lib/liquidscript/scanner/liquidscript.rb
|
135
139
|
- lib/liquidscript/scanner/token.rb
|
136
140
|
- lib/liquidscript/template.rb
|
137
141
|
- lib/liquidscript/version.rb
|
@@ -148,17 +152,18 @@ files:
|
|
148
152
|
- spec/fixtures/main.compile.yml
|
149
153
|
- spec/fixtures/operator.generate.yml
|
150
154
|
- spec/fixtures/set.generate.yml
|
155
|
+
- spec/fixtures/string.compile.yml
|
151
156
|
- spec/fixtures/string.generate.yml
|
152
157
|
- spec/fixtures/underscore.ls
|
153
|
-
- spec/
|
154
|
-
- spec/
|
155
|
-
- spec/
|
156
|
-
- spec/
|
157
|
-
- spec/
|
158
|
-
- spec/
|
159
|
-
- spec/
|
160
|
-
- spec/
|
161
|
-
- spec/
|
158
|
+
- spec/liquidscript/buffer_spec.rb
|
159
|
+
- spec/liquidscript/compiler/icr_spec.rb
|
160
|
+
- spec/liquidscript/generator/javascript_spec.rb
|
161
|
+
- spec/liquidscript/icr/code_spec.rb
|
162
|
+
- spec/liquidscript/icr/context_spec.rb
|
163
|
+
- spec/liquidscript/icr/set_spec.rb
|
164
|
+
- spec/liquidscript/scanner/lexer_spec.rb
|
165
|
+
- spec/liquidscript/scanner/token_spec.rb
|
166
|
+
- spec/liquidscript/scanner_spec.rb
|
162
167
|
- spec/spec_helper.rb
|
163
168
|
- spec/support/helpers/lexer_helper.rb
|
164
169
|
- spec/support/matchers/be_token.rb
|
@@ -200,17 +205,18 @@ test_files:
|
|
200
205
|
- spec/fixtures/main.compile.yml
|
201
206
|
- spec/fixtures/operator.generate.yml
|
202
207
|
- spec/fixtures/set.generate.yml
|
208
|
+
- spec/fixtures/string.compile.yml
|
203
209
|
- spec/fixtures/string.generate.yml
|
204
210
|
- spec/fixtures/underscore.ls
|
205
|
-
- spec/
|
206
|
-
- spec/
|
207
|
-
- spec/
|
208
|
-
- spec/
|
209
|
-
- spec/
|
210
|
-
- spec/
|
211
|
-
- spec/
|
212
|
-
- spec/
|
213
|
-
- spec/
|
211
|
+
- spec/liquidscript/buffer_spec.rb
|
212
|
+
- spec/liquidscript/compiler/icr_spec.rb
|
213
|
+
- spec/liquidscript/generator/javascript_spec.rb
|
214
|
+
- spec/liquidscript/icr/code_spec.rb
|
215
|
+
- spec/liquidscript/icr/context_spec.rb
|
216
|
+
- spec/liquidscript/icr/set_spec.rb
|
217
|
+
- spec/liquidscript/scanner/lexer_spec.rb
|
218
|
+
- spec/liquidscript/scanner/token_spec.rb
|
219
|
+
- spec/liquidscript/scanner_spec.rb
|
214
220
|
- spec/spec_helper.rb
|
215
221
|
- spec/support/helpers/lexer_helper.rb
|
216
222
|
- spec/support/matchers/be_token.rb
|