SweetTea 0.0.5
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.
- data/.gitignore +4 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/Rakefile +3 -0
- data/SweetTea.gemspec +26 -0
- data/lib/SweetTea.rb +5 -0
- data/lib/SweetTea/grammar.treetop +45 -0
- data/lib/SweetTea/nodes.rb +14 -0
- data/lib/SweetTea/parser.rb +95 -0
- data/lib/SweetTea/version.rb +3 -0
- data/spec/grammar_spec.rb +55 -0
- data/spec/spec_helper.rb +11 -0
- metadata +115 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
data/SweetTea.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "SweetTea/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "SweetTea"
|
7
|
+
s.version = SweetTea::VERSION
|
8
|
+
s.authors = ["James Birtles"]
|
9
|
+
s.email = ["itsme@jamesbirtles.com"]
|
10
|
+
s.homepage = "http://jamesbirtles.org/sweettea"
|
11
|
+
s.summary = %q{A language that compiles down to pure javascript}
|
12
|
+
s.description = %q{SweetTea is a new language that can be compiled into javascript.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "SweetTea"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency "rspec"
|
22
|
+
s.add_development_dependency "autotest"
|
23
|
+
s.add_development_dependency "autotest-fsevent"
|
24
|
+
s.add_development_dependency "test_notifier"
|
25
|
+
s.add_runtime_dependency "treetop"
|
26
|
+
end
|
data/lib/SweetTea.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
grammar SweetTea
|
2
|
+
rule body
|
3
|
+
(class / expression / space)* <BodyNode>
|
4
|
+
end
|
5
|
+
|
6
|
+
rule class
|
7
|
+
('class->' / 'c->') gap? specialName (expression)* ('<-class' / '<-c') <ClassNode>
|
8
|
+
end
|
9
|
+
|
10
|
+
rule function
|
11
|
+
('function->' / 'func->' / 'f->') gap? specialName (argument)* (expression)* ('<-function' / '<-func' / '<-f') <FunctionNode>
|
12
|
+
end
|
13
|
+
|
14
|
+
rule argument
|
15
|
+
specialName / gap
|
16
|
+
end
|
17
|
+
|
18
|
+
rule expression
|
19
|
+
variable / literal / function / space
|
20
|
+
end
|
21
|
+
|
22
|
+
rule variable
|
23
|
+
('variable->' / 'var->' / 'v->') gap? specialName gap? ('=' gap? literal)? <VariableNode>
|
24
|
+
end
|
25
|
+
|
26
|
+
rule literal
|
27
|
+
string
|
28
|
+
end
|
29
|
+
|
30
|
+
rule string
|
31
|
+
'"' ('\"' / !'"' .)* '"' <StringLiteral>
|
32
|
+
end
|
33
|
+
|
34
|
+
rule gap
|
35
|
+
[ \t]+
|
36
|
+
end
|
37
|
+
|
38
|
+
rule space
|
39
|
+
[\s]+
|
40
|
+
end
|
41
|
+
|
42
|
+
rule specialName
|
43
|
+
[a-zA-Z0-9_]+
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module SweetTea
|
2
|
+
class BodyNode < Treetop::Runtime::SyntaxNode
|
3
|
+
end
|
4
|
+
class ClassNode < Treetop::Runtime::SyntaxNode
|
5
|
+
end
|
6
|
+
class FunctionNode < Treetop::Runtime::SyntaxNode
|
7
|
+
end
|
8
|
+
class ExpressionNode < Treetop::Runtime::SyntaxNode
|
9
|
+
end
|
10
|
+
class VariableNode < Treetop::Runtime::SyntaxNode
|
11
|
+
end
|
12
|
+
class StringLiteral < Treetop::Runtime::SyntaxNode
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'treetop'
|
2
|
+
require 'SweetTea/nodes'
|
3
|
+
base_path = File.expand_path(File.dirname(__FILE__))
|
4
|
+
Treetop.load(File.join(base_path, 'grammar.treetop'))
|
5
|
+
|
6
|
+
module SweetTea
|
7
|
+
class Parser
|
8
|
+
def parse code
|
9
|
+
parser = SweetTeaParser.new
|
10
|
+
results = parser.parse code
|
11
|
+
@indent = 0
|
12
|
+
@currentClass = nil
|
13
|
+
to_js(results)
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_js node, inClass = false
|
17
|
+
case node.class.to_s
|
18
|
+
when "SweetTea::BodyNode"
|
19
|
+
all = []
|
20
|
+
node.elements.each {|n| all << to_js(n) }
|
21
|
+
return all.join
|
22
|
+
when "SweetTea::ClassNode"
|
23
|
+
className = node.elements[2].text_value
|
24
|
+
@currentClass = className
|
25
|
+
@indent += 1
|
26
|
+
lines = ""
|
27
|
+
node.elements[3].elements.each do |n|
|
28
|
+
val = to_js(n, true)
|
29
|
+
if val != ""
|
30
|
+
lines << "#{val}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
@indent -= 1
|
34
|
+
@currentClass = nil
|
35
|
+
return "#{ind}var #{className} = (function() {\n" +
|
36
|
+
"#{ind} function #{className}() {}\n" +
|
37
|
+
"#{lines}" +
|
38
|
+
"#{ind} return #{className};\n" +
|
39
|
+
"#{ind}})();\n"
|
40
|
+
when "SweetTea::FunctionNode"
|
41
|
+
funcName = node.elements[2].text_value
|
42
|
+
args = ""
|
43
|
+
node.elements[3].elements.each do |n|
|
44
|
+
val = n.text_value
|
45
|
+
if val != " "
|
46
|
+
if args == ""
|
47
|
+
args << "#{val}"
|
48
|
+
else
|
49
|
+
args << ", #{val}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
lines = ""
|
54
|
+
@indent += 1
|
55
|
+
node.elements[4].elements.each do |n|
|
56
|
+
val = to_js(n)
|
57
|
+
if val != ""
|
58
|
+
lines << val
|
59
|
+
end
|
60
|
+
end
|
61
|
+
@indent -= 1
|
62
|
+
if inClass
|
63
|
+
return "#{ind}#{@currentClass}.prototype.#{funcName} = function(#{args}) {\n" +
|
64
|
+
"#{lines}" +
|
65
|
+
"#{ind}};\n"
|
66
|
+
else
|
67
|
+
return "#{ind}var #{funcName} = function(#{args}) {\n" +
|
68
|
+
"#{lines}" +
|
69
|
+
"#{ind}};\n"
|
70
|
+
end
|
71
|
+
when "SweetTea::VariableNode"
|
72
|
+
varName = node.elements[2].text_value
|
73
|
+
if node.elements[4].elements.nil?
|
74
|
+
varValue = "nil"
|
75
|
+
else
|
76
|
+
varValue = to_js(node.elements[4].elements[2])
|
77
|
+
end
|
78
|
+
if inClass
|
79
|
+
return "#{ind}#{@currentClass}.prototype.#{varName} = #{varValue};\n"
|
80
|
+
else
|
81
|
+
return "#{ind}var #{varName} = #{varValue};\n"
|
82
|
+
end
|
83
|
+
when "SweetTea::StringLiteral"
|
84
|
+
return "\"#{node.elements[1].text_value}\""
|
85
|
+
else
|
86
|
+
raise "Syntax Error" if node.nil?
|
87
|
+
return ""
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def ind
|
92
|
+
" " * @indent
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require "spec_helper.rb"
|
2
|
+
require 'treetop'
|
3
|
+
require 'SweetTea/nodes'
|
4
|
+
Treetop.load(File.expand_path("../../lib/SweetTea/grammar", __FILE__))
|
5
|
+
|
6
|
+
describe "SweetTea grammer" do
|
7
|
+
subject { SweetTeaParser.new }
|
8
|
+
context "class" do
|
9
|
+
it "should recognise both ways of creating a class" do
|
10
|
+
subject.parse("class-> ClassName <-class").should_not be_nil
|
11
|
+
subject.parse("c-> ClassName <-c").should_not be_nil
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should allow content" do
|
15
|
+
subject.parse(
|
16
|
+
<<-CODE
|
17
|
+
class-> MyClass
|
18
|
+
function-> myFunction <-function
|
19
|
+
variable-> var2 = "value"
|
20
|
+
<-class
|
21
|
+
CODE
|
22
|
+
).should_not be_nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "function" do
|
27
|
+
it "should recognise all three ways of creating a function" do
|
28
|
+
subject.parse("function-> functionName <-function").should_not be_nil
|
29
|
+
subject.parse("func-> functionName <-func").should_not be_nil
|
30
|
+
subject.parse("f-> functionName <-f").should_not be_nil
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should allow content" do
|
34
|
+
subject.parse(
|
35
|
+
<<-CODE
|
36
|
+
function-> myFunction
|
37
|
+
variable-> myVar = "Hello World"
|
38
|
+
<-function
|
39
|
+
CODE
|
40
|
+
).should_not be_nil
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "variable" do
|
45
|
+
it "should recognise all three ways of creating a variable" do
|
46
|
+
subject.parse("variable-> variableName = \"value\"").should_not be_nil
|
47
|
+
subject.parse("var-> variableName = \"value\"").should_not be_nil
|
48
|
+
subject.parse("v-> variableName = \"value\"").should_not be_nil
|
49
|
+
end
|
50
|
+
|
51
|
+
it "shouldn't require a value to be created" do
|
52
|
+
subject.parse("variable-> variableName").should_not be_nil
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper.rb"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: SweetTea
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.5
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- James Birtles
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-02 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70115283002400 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70115283002400
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: autotest
|
27
|
+
requirement: &70115283001740 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70115283001740
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: autotest-fsevent
|
38
|
+
requirement: &70115283001280 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70115283001280
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: test_notifier
|
49
|
+
requirement: &70115283000580 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70115283000580
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: treetop
|
60
|
+
requirement: &70115282999940 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :runtime
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70115282999940
|
69
|
+
description: SweetTea is a new language that can be compiled into javascript.
|
70
|
+
email:
|
71
|
+
- itsme@jamesbirtles.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .rspec
|
78
|
+
- Gemfile
|
79
|
+
- Rakefile
|
80
|
+
- SweetTea.gemspec
|
81
|
+
- lib/SweetTea.rb
|
82
|
+
- lib/SweetTea/grammar.treetop
|
83
|
+
- lib/SweetTea/nodes.rb
|
84
|
+
- lib/SweetTea/parser.rb
|
85
|
+
- lib/SweetTea/version.rb
|
86
|
+
- spec/grammar_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
homepage: http://jamesbirtles.org/sweettea
|
89
|
+
licenses: []
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
none: false
|
102
|
+
requirements:
|
103
|
+
- - ! '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project: SweetTea
|
108
|
+
rubygems_version: 1.8.10
|
109
|
+
signing_key:
|
110
|
+
specification_version: 3
|
111
|
+
summary: A language that compiles down to pure javascript
|
112
|
+
test_files:
|
113
|
+
- spec/grammar_spec.rb
|
114
|
+
- spec/spec_helper.rb
|
115
|
+
has_rdoc:
|