sweet-lang 0.2.2 → 0.3.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/lexer.rb +1 -0
- data/lib/lexer/core.rb +43 -28
- data/lib/lexer/files.rb +20 -0
- data/lib/lexer/strings.rb +21 -3
- data/lib/lexer/tags.rb +16 -11
- data/lib/sweet.rb +2 -2
- metadata +3 -2
data/lib/lexer.rb
CHANGED
data/lib/lexer/core.rb
CHANGED
@@ -3,7 +3,9 @@ class Lexer
|
|
3
3
|
|
4
4
|
def treat(code)
|
5
5
|
tags = Tags.new
|
6
|
-
|
6
|
+
strings = Strings.new
|
7
|
+
files = Files.new
|
8
|
+
code = strings.rollingback_strings(tags.implement_tag(files.includes(code)))
|
7
9
|
end
|
8
10
|
|
9
11
|
def tokenize(code)
|
@@ -14,11 +16,9 @@ class Lexer
|
|
14
16
|
code = code.gsub( /[ ]+\n/, "\n" )
|
15
17
|
code = code.gsub( "\n ", "\n\t" )
|
16
18
|
code = code.gsub( /^\s*$/m, '' )
|
17
|
-
|
18
|
-
tags = Tags.new
|
19
|
+
code = code.gsub( /^((\t)+)?\n/, '' )
|
19
20
|
|
20
21
|
code = treat(code)
|
21
|
-
|
22
22
|
# Current character position we're parsing
|
23
23
|
i = 0
|
24
24
|
|
@@ -66,37 +66,50 @@ class Lexer
|
|
66
66
|
|
67
67
|
# Here's the indentation magic!
|
68
68
|
elsif indent = chunk[/\A\n(\t+)/m, 1]
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
69
|
+
# p "-----------"
|
70
|
+
# p indent.size
|
71
|
+
# p current_indent
|
72
|
+
# Create a new block we expect the indent level to go up.
|
73
|
+
if indent.size < current_indent
|
74
|
+
while current_indent > indent.size
|
75
|
+
indent_stack.pop
|
76
|
+
current_indent = indent_stack.last || 0
|
77
|
+
tokens << [:DEDENT, indent.size]
|
78
|
+
tokens << [:NEWLINE, "\n"]
|
79
|
+
end
|
80
|
+
|
81
|
+
elsif indent.size == current_indent
|
82
|
+
tokens << [:NEWLINE, "\n"]
|
83
|
+
|
84
|
+
else
|
85
|
+
# Adjust the current indentation level.
|
86
|
+
current_indent = indent.size
|
87
|
+
indent_stack << current_indent
|
88
|
+
tokens << [:INDENT, indent.size]
|
89
|
+
end
|
90
|
+
# p tokens
|
91
|
+
# p "-----------"
|
92
|
+
# p "+++++#{current_indent}"
|
86
93
|
i += indent.size + 1
|
87
|
-
|
88
94
|
elsif indent = chunk[/\A\n( *)/m, 1]
|
95
|
+
# p "============"
|
96
|
+
# p indent.size
|
97
|
+
# p current_indent
|
89
98
|
if indent.size == current_indent
|
90
99
|
# Nothing to do, we're still in the same block
|
91
100
|
tokens << [:NEWLINE, "\n"]
|
92
101
|
elsif indent.size < current_indent
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
102
|
+
while current_indent > indent.size
|
103
|
+
indent_stack.pop
|
104
|
+
current_indent = indent_stack.last || 0
|
105
|
+
tokens << [:DEDENT, indent.size]
|
106
|
+
tokens << [:NEWLINE, "\n"]
|
107
|
+
end
|
97
108
|
end
|
109
|
+
# p tokens
|
110
|
+
# p "============"
|
111
|
+
# p "+++++#{current_indent}"
|
98
112
|
i += indent.size + 1
|
99
|
-
|
100
113
|
# Match long operators such as ||, &&, ==, !=, <= and >=.
|
101
114
|
# One character long operators are matched by the catch all `else` at the bottom.
|
102
115
|
elsif operator = chunk[/\A(\|\||&&|==|!=|<=|>=)/, 1]
|
@@ -122,7 +135,9 @@ class Lexer
|
|
122
135
|
while indent = indent_stack.pop
|
123
136
|
tokens << [:DEDENT, indent_stack.last || 0]
|
124
137
|
end
|
125
|
-
|
138
|
+
# p "================================"
|
139
|
+
# p tokens
|
140
|
+
# p "================================"
|
126
141
|
tokens
|
127
142
|
end
|
128
143
|
end
|
data/lib/lexer/files.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
class Files
|
2
|
+
|
3
|
+
def includes(code)
|
4
|
+
includes = code.scan(/(((\t)+)?<~([ .\/=-_a-zA-Z0-9]+))/)
|
5
|
+
includes.uniq!
|
6
|
+
includes.each do |include_|
|
7
|
+
result = ""
|
8
|
+
file_ = include_[3].gsub(" ", "")
|
9
|
+
indent = (include_[1].nil?) ? 0 : include_[1].count("\t")
|
10
|
+
indent = "\t" * indent
|
11
|
+
file_ = File.new("#{$path}#{file_}", "r")
|
12
|
+
while (line = file_.gets)
|
13
|
+
result += "#{indent}#{line}"
|
14
|
+
end
|
15
|
+
code = code.gsub(/^#{include_[0]}$/, result)
|
16
|
+
end
|
17
|
+
return code
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
data/lib/lexer/strings.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
class Strings
|
2
2
|
|
3
|
-
def convert_string(code, indent)
|
3
|
+
def convert_string(code, indent, defs=false)
|
4
4
|
strings = code.scan(/\t("[^"]+")/m)
|
5
|
+
defs_back = code.scan(/\t(\/("[^"]+")\/)/m)
|
5
6
|
strings.uniq!
|
6
7
|
strings.each do |string|
|
7
8
|
original_tabs = code.scan(/((\t)+)#{string[0]}/)
|
@@ -18,18 +19,35 @@ class Strings
|
|
18
19
|
n_string = n_string.sub(/(\t)+/, "#{ ' ' * (actual_tab - original_tabs) }")
|
19
20
|
end
|
20
21
|
end
|
21
|
-
new_string = "print(#{n_string})"
|
22
|
+
new_string = ( defs ) ? "/#{n_string}/" : "print(#{n_string})"
|
22
23
|
code = code.gsub(string[0], new_string)
|
23
24
|
end
|
24
25
|
return code
|
25
26
|
end
|
26
27
|
|
28
|
+
def treat_def(code)
|
29
|
+
defs = code.scan(/^(\t*)(def(()$| )(((\n)?[a-zA-Z0-9= _\-#.\\'\/\"]+)+)?(((\n)+?\1{1,}\t+)?(["#a= ()a-zA-Z0-9_.,\--\\'\/&]+)?)+)/)
|
30
|
+
defs.map! {|x| x[1]}
|
31
|
+
defs.each do |def_|
|
32
|
+
code = code.gsub(def_, convert_string( def_, 1, true ))
|
33
|
+
end
|
34
|
+
code
|
35
|
+
end
|
36
|
+
|
37
|
+
def rollingback_strings(code)
|
38
|
+
defs_back = code.scan(/\t(\/("[^"]+")\/)/m)
|
39
|
+
defs_back.each do |def_|
|
40
|
+
code = code.gsub(def_[0], def_[1])
|
41
|
+
end
|
42
|
+
code
|
43
|
+
end
|
44
|
+
|
27
45
|
def fix_atr(code, indent)
|
28
46
|
strings = code.scan(/print\(.*\)/m)
|
29
47
|
strings.uniq!
|
30
48
|
n_string = strings[0].gsub("\\=", "=")
|
31
49
|
code = code.gsub(strings[0], n_string)
|
32
|
-
|
50
|
+
code
|
33
51
|
end
|
34
52
|
|
35
53
|
end
|
data/lib/lexer/tags.rb
CHANGED
@@ -14,7 +14,7 @@ class Tags
|
|
14
14
|
"var"]
|
15
15
|
|
16
16
|
def convert_tag(code, index, indent, tag, atr, inline=false)
|
17
|
-
content = code.scan(/(\t+[a-zA-Z0-9]+.*)/)
|
17
|
+
content = code.scan(/(\t+[\/"a-zA-Z0-9]+.*)/)
|
18
18
|
tab = ""
|
19
19
|
p_tab = ""
|
20
20
|
for i in 0..indent
|
@@ -53,8 +53,8 @@ class Tags
|
|
53
53
|
end
|
54
54
|
orig.uniq!
|
55
55
|
orig.each do |_old|
|
56
|
-
_new = "#{a[0]}=#{_old[1]}"
|
57
|
-
code = code.gsub(_old[0]
|
56
|
+
_new = " #{a[0]}=#{_old[1]}"
|
57
|
+
code = code.gsub(/ #{_old[0]}/, _new)
|
58
58
|
end
|
59
59
|
end
|
60
60
|
return code
|
@@ -80,15 +80,17 @@ class Tags
|
|
80
80
|
end
|
81
81
|
end
|
82
82
|
@@tags.each do |tag|
|
83
|
-
general = code.scan(/(^(#{tag}(()$| )([
|
84
|
-
|
83
|
+
general = code.scan(/(^(#{tag}(()$| )([ :;\/\?\+#.=_\-a-zA-Z0-9]+)?((\n\t.*)+)?).*)/)
|
84
|
+
general_old = general
|
85
|
+
general.map! {|x| x[1]}
|
86
|
+
general.uniq!
|
85
87
|
general.each_with_index do |block, index|
|
86
88
|
_code_block = block
|
87
89
|
code_block = adding_attributes(block, tag)
|
88
|
-
code_block = @strings.
|
89
|
-
@@tags.each do |r_tag|
|
90
|
-
if !code_block.scan(/^(\t){#{indent}}(#{r_tag}(()$| )((\n)
|
91
|
-
r_code = code_block.scan(/^(\t){#{indent}}(#{r_tag}(()$| )((\n)
|
90
|
+
code_block = @strings.treat_def(code_block)
|
91
|
+
@@tags.each do |r_tag|
|
92
|
+
if !code_block.scan(/^(\t){#{indent}}(#{r_tag}(()$| )(((\n)?[a-zA-Z0-9= _\-#.\\'\/\"]+)+)?(((\n)+?\1{#{indent+1},})?(["#a= ()a-zA-Z0-9_.,\--\\'\/&]+)?)+)/m).empty?
|
93
|
+
r_code = code_block.scan(/^(\t){#{indent}}(#{r_tag}(()$| )(((\n)?[a-zA-Z0-9= _\-#.\\'\/\"]+)+)?(((\n)+?\1{#{indent+1},})?(["\!\?#a= ()a-zA-Z0-9_.,\--\\'\/&]+)?)+)/m)
|
92
94
|
r_code.each do |r|
|
93
95
|
new_indent = indent + 1
|
94
96
|
new_block = implement_tag(r[1], new_indent)
|
@@ -96,9 +98,12 @@ class Tags
|
|
96
98
|
end
|
97
99
|
end
|
98
100
|
end
|
99
|
-
atr = code_block.scan(/([a-zA-Z0-9_-]+=)([
|
101
|
+
atr = code_block.scan(/([a-zA-Z0-9_-]+=)([\.\/:;#a-zA-Z0-9_-]+)/)
|
102
|
+
code_block = @strings.convert_string( code_block, indent )
|
100
103
|
code_block = convert_tag( code_block , index, indent, tag, atr)
|
101
|
-
|
104
|
+
_code_block = _code_block.gsub("(", '\(')
|
105
|
+
_code_block = _code_block.gsub(")", '\)')
|
106
|
+
code = code.gsub(/^#{_code_block}$/m, code_block)
|
102
107
|
end
|
103
108
|
end
|
104
109
|
code = @strings.fix_atr( code, indent )
|
data/lib/sweet.rb
CHANGED
@@ -10,7 +10,7 @@ class Sweet
|
|
10
10
|
def parsing_options
|
11
11
|
|
12
12
|
opts = Trollop::options do
|
13
|
-
version "Sweet 0.
|
13
|
+
version "Sweet 0.3.7 (c) 2012 Joao Moura a.k.a D3TR1"
|
14
14
|
banner <<-EOS
|
15
15
|
A Sweet Language, that compiles for HTML.
|
16
16
|
|
@@ -49,7 +49,7 @@ class Sweet
|
|
49
49
|
end
|
50
50
|
|
51
51
|
def compile_file(file)
|
52
|
-
|
52
|
+
$path = file.split(/[ =-_a-zA-Z0-9]+\.s/)[0]
|
53
53
|
interpreter = Interpreter.new
|
54
54
|
@filename = file.split(".")[0]
|
55
55
|
File.new("#{@filename}.html", "w")
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sweet-lang
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-01-11 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: ! "Sweet is a pre-compiled language based on Ruby and Python. \n Sweet
|
15
15
|
makes possible to use all the class, defs, conditionals, global and \n local
|
@@ -31,6 +31,7 @@ files:
|
|
31
31
|
- lib/lexer/core.rb
|
32
32
|
- lib/lexer/strings.rb
|
33
33
|
- lib/lexer/tags.rb
|
34
|
+
- lib/lexer/files.rb
|
34
35
|
- lib/runtime/bootstrap.rb
|
35
36
|
- lib/runtime/class.rb
|
36
37
|
- lib/runtime/context.rb
|