liquidscript 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +0 -1
  3. data/Guardfile +0 -4
  4. data/Rakefile +2 -2
  5. data/lib/liquidscript/compiler/icr/expressions.rb +2 -1
  6. data/lib/liquidscript/compiler/icr/literals.rb +17 -3
  7. data/lib/liquidscript/generator/javascript/literals.rb +18 -2
  8. data/lib/liquidscript/scanner.rb +3 -31
  9. data/lib/liquidscript/scanner/base.rb +84 -0
  10. data/lib/liquidscript/scanner/base/builder.rb +38 -0
  11. data/lib/liquidscript/scanner/base/context.rb +83 -0
  12. data/lib/liquidscript/scanner/base/dsl.rb +27 -0
  13. data/lib/liquidscript/scanner/base/lexer.rb +80 -0
  14. data/lib/liquidscript/scanner/liquidscript.rb +128 -0
  15. data/lib/liquidscript/scanner/token.rb +2 -6
  16. data/lib/liquidscript/template.rb +0 -1
  17. data/lib/liquidscript/version.rb +1 -1
  18. data/spec/fixtures/complex.generate.yml +1 -0
  19. data/spec/fixtures/main.compile.yml +1 -1
  20. data/spec/fixtures/string.compile.yml +15 -0
  21. data/spec/fixtures/string.generate.yml +3 -1
  22. data/spec/{lib/liquidscript → liquidscript}/buffer_spec.rb +0 -0
  23. data/spec/{lib/liquidscript → liquidscript}/compiler/icr_spec.rb +0 -0
  24. data/spec/{lib/liquidscript → liquidscript}/generator/javascript_spec.rb +7 -2
  25. data/spec/{lib/liquidscript → liquidscript}/icr/code_spec.rb +0 -0
  26. data/spec/{lib/liquidscript → liquidscript}/icr/context_spec.rb +0 -0
  27. data/spec/{lib/liquidscript → liquidscript}/icr/set_spec.rb +0 -0
  28. data/spec/{lib/liquidscript → liquidscript}/scanner/lexer_spec.rb +13 -15
  29. data/spec/{lib/liquidscript → liquidscript}/scanner/token_spec.rb +0 -0
  30. data/spec/{lib/liquidscript → liquidscript}/scanner_spec.rb +2 -2
  31. data/spec/spec_helper.rb +1 -0
  32. data/spec/support/helpers/lexer_helper.rb +1 -1
  33. data/spec/support/matchers/compile.rb +1 -1
  34. data/spec/support/matchers/generate.rb +2 -2
  35. metadata +28 -22
  36. data/lib/liquidscript/scanner/lexer.rb +0 -805
  37. 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
- class Scanner
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 = begin
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
@@ -9,7 +9,6 @@ module Liquidscript
9
9
 
10
10
  def render
11
11
  @_render ||= begin
12
- p Scanner.new(@data).each
13
12
  compiler = Compiler::ICR.new(Scanner.new(@data))
14
13
  compiler.compile
15
14
  puts ICR::Sexp.new(compiler.top).output
@@ -1,5 +1,5 @@
1
1
  module Liquidscript
2
2
 
3
3
  # The current version of liquidscript.
4
- VERSION = "0.3.1".freeze
4
+ VERSION = "0.4.0".freeze
5
5
  end
@@ -1,4 +1,5 @@
1
1
  data: |
2
+ # comment
2
3
  thing = {
3
4
  test: -> {
4
5
  if(undefined) {
@@ -30,7 +30,7 @@ compiled:
30
30
  - console
31
31
  - - :identifier
32
32
  - log
33
- - - :dstring
33
+ - - :istring
34
34
  - hello world
35
35
  - - :newline
36
36
  - - :newline
@@ -0,0 +1,15 @@
1
+ data: |
2
+ "hello #{console}"
3
+ compiled:
4
+ - :exec
5
+ - - :_context
6
+ - []
7
+ - - :interop
8
+ - - :istring_begin
9
+ - "hello "
10
+ - - :get
11
+ - - :_variable
12
+ - :console
13
+ - - :istring
14
+ - ""
15
+ - - :newline
@@ -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('');
@@ -1,14 +1,19 @@
1
1
  require "spec_helper"
2
2
  require "yaml"
3
3
 
4
- describe Liquidscript::Generator::Javascript do
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
- expect(content["data"]).to generate(content["compiled"])
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
@@ -1,14 +1,6 @@
1
1
  require "spec_helper"
2
2
 
3
- describe Liquidscript::Scanner::Lexer, :lexer_helper do
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
- [:dstring, '"hello world"']
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
  ]
@@ -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("'test'") }
13
+ subject { described_class::Liquidscript.new("';") }
14
14
 
15
15
  it "raises an error" do
16
16
  expect {
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require "rubygems"
1
2
  require "bundler/setup"
2
3
  Bundler.setup
3
4
 
@@ -1,5 +1,5 @@
1
1
  module LexerHelper
2
2
  def scan(source)
3
- subject.perform(source)
3
+ described_class.new(source).scan.tokens
4
4
  end
5
5
  end
@@ -36,6 +36,6 @@ RSpec::Matchers.define :compile do
36
36
  end
37
37
 
38
38
  def compiler(data)
39
- Compiler::ICR.new(Scanner.new(data))
39
+ Compiler::ICR.new(Scanner::Liquidscript.new(data))
40
40
  end
41
41
  end
@@ -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.3.1
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-09 00:00:00.000000000 Z
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/lexer.rb
134
- - lib/liquidscript/scanner/lexer.rl
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/lib/liquidscript/buffer_spec.rb
154
- - spec/lib/liquidscript/compiler/icr_spec.rb
155
- - spec/lib/liquidscript/generator/javascript_spec.rb
156
- - spec/lib/liquidscript/icr/code_spec.rb
157
- - spec/lib/liquidscript/icr/context_spec.rb
158
- - spec/lib/liquidscript/icr/set_spec.rb
159
- - spec/lib/liquidscript/scanner/lexer_spec.rb
160
- - spec/lib/liquidscript/scanner/token_spec.rb
161
- - spec/lib/liquidscript/scanner_spec.rb
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/lib/liquidscript/buffer_spec.rb
206
- - spec/lib/liquidscript/compiler/icr_spec.rb
207
- - spec/lib/liquidscript/generator/javascript_spec.rb
208
- - spec/lib/liquidscript/icr/code_spec.rb
209
- - spec/lib/liquidscript/icr/context_spec.rb
210
- - spec/lib/liquidscript/icr/set_spec.rb
211
- - spec/lib/liquidscript/scanner/lexer_spec.rb
212
- - spec/lib/liquidscript/scanner/token_spec.rb
213
- - spec/lib/liquidscript/scanner_spec.rb
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