Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

bug fix

  • Loading branch information...
commit 4c3e59e93b037f7c3e93e9062cd7fb33ef0c4371 1 parent 74c591a
@pasberth authored
View
3  lib/EndlessRuby/extensions.rb
@@ -21,10 +21,12 @@ def require path
raise LoadError, "no such file to load -- #{path}"
end
end
+
if File.directory? path
$@ = at
raise LoadError, "Is a directory - #{path}"
end
+
open(path) do |file|
begin
EndlessRuby.ereval file.read, TOPLEVEL_BINDING, path
@@ -45,6 +47,7 @@ def require path
next
end
end
+
next is_that_dir = true if File.directory? real_path
open(real_path) do |file|
begin
View
18 lib/EndlessRuby/main.rb
@@ -12,16 +12,16 @@ def compile er, rb
open(er) do |erfile|
open(rb, "w") do |rbfile|
rbfile.write ER2PR(erfile.read)
-
- # EndlessRuby::Main.main と同じ動作をします。このモジュールをincludeした場合に使用します。
end
end
end
+
+ # EndlessRuby::Main.main と同じ動作をします。このモジュールをincludeした場合に使用します。
def endlessruby argv
EndlessRuby::Main.main argv
+ end
# $ endlessruby.rb args とまったく同じ動作をします。argvはARGVと同じ形式でなければなりません。
- end
def self.main argv
if argv.first && File.exist?(argv.first)
@@ -31,6 +31,7 @@ def self.main argv
end
return true
end
+
require 'optparse'
options = {
@@ -40,13 +41,16 @@ def self.main argv
opts.on '-o OUT' do |out|
options[:out] = out
end
+
opts.on '-c', '--compile' do |c|
options[:compile] = true
end
+
opts.on '-r' do |r|
options[:recursive] = true
end
end
+
parser.parse! argv
if options[:compile]
@@ -57,14 +61,16 @@ def self.main argv
puts "no such file to load -- #{er}"
next
end
+
if File.directory? er
unless options[:recursive]
puts "Is a directory - #{er}"
next
- # Unimolementation
end
+ # Unimolementation
next
end
+
rb = er
if er =~ /^(.*)\.er$/
rb = $1
@@ -73,7 +79,7 @@ def self.main argv
rb = File.join(out, "#{rb}.rb")
compile er, rb
end
- return
end
end
-end
+end
+ return
View
54 lib/endlessruby.rb
@@ -47,29 +47,29 @@ module EndlessRuby
def blank_line? line
return true unless line
(line.chomp.gsub /\s+?/, '') == ""
+ end
# 内部で使用します。インデントを取り除きます
- end
def unindent line
line =~ /^\s*?(\S.*?)$/
$1
- # 内部で使用します。インデントします
end
+ # 内部で使用します。インデントします
def indent line, level, indent=" "
"#{indent * level}#{line}"
+ end
# 内部で使用します。インデントの数を数えます。
- end
def indent_count line, indent=" "
return 0 unless line
if line =~ /^#{indent}(.*?)$/
1 + (indent_count $1, indent)
else
0
-
- # 内部で使用します。ブロックを作るキーワード群です。
end
end
+
+ # 内部で使用します。ブロックを作るキーワード群です。
BLOCK_KEYWORDS = [
[/^if(:?\s|\().*?$/, /^elsif(:?\s|\().*?$/, /^else(?:$|\s+)/],
[/^unless(:?\s|\().*?$/, /^elsif(:?\s|\().*?$/, /^else(?:$|\s+)/],
@@ -92,18 +92,18 @@ def ereval(src, binding=TOPLEVEL_BINDING, filename=__FILE__, lineno=1)
rescue Exception => e
$@ = at
raise e
+ end
# 廃止予定です
- end
def ercompile(er, rb)
open(er) do |erfile|
open(rb, "w") do |rbfile|
rbfile.write(endless_ruby_to_pure_ruby(erfile.read))
-
- # EndlessRubyの構文をピュアなRubyの構文に変換します。
end
end
end
+
+ # EndlessRubyの構文をピュアなRubyの構文に変換します。
def endless_ruby_to_pure_ruby src
endless = src.split "\n"
@@ -115,12 +115,13 @@ def endless_ruby_to_pure_ruby src
if currently_line =~ /(.*)(?:^|(?:(?!\\).))\#(?!\{).*$/
currently_line = $1
end
+
if blank_line? currently_line
i += 1
next
+ end
# ブロックを作らない構文なら単に無視する
- end
next i += 1 unless BLOCK_KEYWORDS.any? { |k| k[0] =~ unindent(currently_line) }
# ブロックに入る
@@ -130,19 +131,36 @@ def endless_ruby_to_pure_ruby src
base_indent_depth = currently_indent_depth
inner_statements = []
+ comment_count = 0
+ break_comment_flag = false
in_here_document = nil
while i < endless.length
inner_currently_line = endless[i + 1]
if inner_currently_line =~ /(.*)(?:^|(?:(?!\\).))\#(?!\{).*$/
- inner_currently_line = $1
+ if blank_line?($1) && currently_indent_depth >= indent_count(inner_currently_line)
+ comment_count += 1
+ break_comment_flag = false
+ end
+ inner_currently_line = $1
+ elsif blank_line? inner_currently_line
+ comment_count += 1
+ break_comment_flag = false
+ else
+ if break_comment_flag
+ comment_count = 0
+ else
+ break_comment_flag = true
+ end
end
+
if blank_line? inner_currently_line
inner_statements << endless[i + 1]
i += 1
next
end
+
just_after_indent_depth = indent_count inner_currently_line
if in_here_document
@@ -157,21 +175,33 @@ def endless_ruby_to_pure_ruby src
next
end
end
+
if inner_currently_line =~ /^.*?\<\<\-?(\w+)(?!\w).*$/
in_here_document = $1
-
end
+
if base_indent_depth > indent_count(inner_currently_line)
break
end
+
if base_indent_depth == indent_count(inner_currently_line)
unless keyword[1..-1].any? { |k| k =~ unindent(inner_currently_line) }
break
end
end
+
inner_statements << endless[i + 1]
i += 1
end
+
+ # endをコメントより上の行へ持ち上げる
+ if 0 < comment_count
+ comment_count.times do
+ inner_statements.pop
+ end
+ i -= comment_count
+ end
+
pure += ER2PR(inner_statements.join("\n")).split "\n"
# 次の行がendならばendを補完しない(ワンライナーのため)
unless endless[i + 1] && endless[i + 1] =~ /^\s*end(?!\w).*$/
@@ -181,10 +211,12 @@ def endless_ruby_to_pure_ruby src
end
pure.join "\n"
end
+
alias to_pure_ruby endless_ruby_to_pure_ruby
alias ER2PR endless_ruby_to_pure_ruby
alias ER2RB endless_ruby_to_pure_ruby
end
+
if __FILE__ == $PROGRAM_NAME
require 'endlessruby/main'
EndlessRuby::Main.main ARGV
View
21 src/endlessruby.er
@@ -120,13 +120,26 @@ module EndlessRuby
base_indent_depth = currently_indent_depth
inner_statements = []
+ comment_count = 0
+ break_comment_flag = false
in_here_document = nil
while i < endless.length
inner_currently_line = endless[i + 1]
if inner_currently_line =~ /(.*)(?:^|(?:(?!\\).))\#(?!\{).*$/
- inner_currently_line = $1
+ if blank_line?($1) && currently_indent_depth >= indent_count(inner_currently_line)
+ comment_count += 1
+ break_comment_flag = false
+ inner_currently_line = $1
+ elsif blank_line? inner_currently_line
+ comment_count += 1
+ break_comment_flag = false
+ else
+ if break_comment_flag
+ comment_count = 0
+ else
+ break_comment_flag = true
if blank_line? inner_currently_line
inner_statements << endless[i + 1]
@@ -159,6 +172,12 @@ module EndlessRuby
inner_statements << endless[i + 1]
i += 1
+ # endをコメントより上の行へ持ち上げる
+ if 0 < comment_count
+ comment_count.times do
+ inner_statements.pop
+ i -= comment_count
+
pure += ER2PR(inner_statements.join("\n")).split "\n"
# 次の行がendならばendを補完しない(ワンライナーのため)
unless endless[i + 1] && endless[i + 1] =~ /^\s*end(?!\w).*$/
Please sign in to comment.
Something went wrong with that request. Please try again.