QaDeS-lax 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/README.rdoc +39 -0
  2. data/Rakefile +51 -0
  3. data/bin/lax +6 -0
  4. data/lib/lax.rb +132 -0
  5. data/test/test.rb +0 -0
  6. metadata +60 -0
@@ -0,0 +1,39 @@
1
+ = Lax
2
+
3
+ Lax is a Ruby preprocessor for enabling a relaxed and more concise syntax: You can omit the keywords <tt>do</tt> and <tt>end</tt>.
4
+ To get Lax to work in your code, you have to <tt>require</tt> Lax and then load your <tt>.rbl</tt> code.
5
+
6
+ Example:
7
+ <tt>test.rb</tt>
8
+ require 'rubygems'
9
+ require 'lax'
10
+
11
+ require 'rbltest'
12
+ # or: load 'rbltest.rbl'
13
+
14
+ <tt>rbltest.rbl</tt>
15
+ lines = <<END
16
+ one
17
+ two
18
+ three
19
+ END
20
+
21
+ lines.each |line|
22
+ if line.match /t/
23
+ puts line
24
+ else
25
+ puts "-" + line
26
+
27
+
28
+ == Executable
29
+ Lax also includes an executable which lets you preprocess single <tt>.rbl</tt> files.
30
+
31
+ Example:
32
+ lax rbltest.rbl
33
+
34
+
35
+ == Disclaimer
36
+ This project is *not* meant to propagate pythonic syntax or spark another "indentation as syntax" war.
37
+ It was purely born out of pure laziness, as I got tired of searching missing <tt>do</tt>s and <tt>end</tt>s
38
+ in my code.
39
+ Lax was derived from <tt>pyrb.rb</tt> at http://xtargets.com/snippets/posts/show/68.
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ load 'lax.gemspec'
4
+
5
+
6
+ # ----- Packaging -----
7
+
8
+ require 'rake/gempackagetask'
9
+
10
+ Rake::GemPackageTask.new(LaxSpec) do |pkg|
11
+ end
12
+
13
+ desc "Install Lax as a gem."
14
+ task :install => [:package] do
15
+ sudo = RUBY_PLATFORM =~ /win32/ ? '' : 'sudo'
16
+ sh %{#{sudo} gem install --no-ri pkg/#{NAME}-#{GEM_VERSION}}
17
+ end
18
+
19
+ # ----- Documentation -----
20
+
21
+ require 'rake/rdoctask'
22
+
23
+ Rake::RDocTask.new do |rdoc|
24
+ rdoc.title = 'Lax'
25
+ rdoc.options << '--line-numbers' << '--inline-source'
26
+ rdoc.rdoc_files.include(*FileList.new('*') do |list|
27
+ list.exclude(/(^|[^.a-z])[a-z]+/)
28
+ list.exclude('TODO')
29
+ end.to_a)
30
+ rdoc.rdoc_files.include('lib/**/*.rb')
31
+ rdoc.rdoc_files.exclude('TODO')
32
+ rdoc.rdoc_dir = 'rdoc'
33
+ rdoc.main = 'README.rdoc'
34
+ end
35
+
36
+ # ----- Coverage -----
37
+
38
+ begin
39
+ require 'rcov/rcovtask'
40
+
41
+ Rcov::RcovTask.new do |t|
42
+ t.test_files = FileList['test/test.rb']
43
+ t.rcov_opts << '-x' << '"^\/"'
44
+ if ENV['NON_NATIVE']
45
+ t.rcov_opts << "--no-rcovrt"
46
+ end
47
+ t.verbose = true
48
+ end
49
+ rescue LoadError; end
50
+
51
+
data/bin/lax ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'lax'
5
+
6
+ Lax.main
@@ -0,0 +1,132 @@
1
+ module Lax
2
+ EXT_LAX_ONLY = [ '.rbl' ].freeze
3
+ EXT_ALL = (EXT_LAX_ONLY + [ '.rb', '.rbw' ]).freeze
4
+ extensions = EXT_LAX_ONLY
5
+
6
+ def self.extensions= value
7
+ @extensions = value
8
+ end
9
+
10
+ def self.extensions
11
+ @extensions
12
+ end
13
+
14
+ def self.find_file(path, extensions = [''])
15
+ $:.each do |search_path|
16
+ search_path = File.join(search_path, path)
17
+ for ext in extensions do
18
+ p = path + ext
19
+ return p if FileTest.exist?(p)
20
+ end
21
+ end
22
+ end
23
+
24
+ def self.load(file)
25
+ return false if not (file and extensiond.member? File.extname(file))
26
+
27
+ txt = Lax.convert(file)
28
+ Object.module_eval txt, file, 1
29
+ true
30
+ end
31
+
32
+ def self.convert(file)
33
+ txt = ""
34
+ stack = []
35
+ lines = File.open(file).readlines
36
+
37
+ indents = lines.collect do |l|
38
+ if l.match(/^\s+$/) # empty line
39
+ -1
40
+ else
41
+ m = l.match(/^(\s+)/)
42
+ last_indent = m ? m[1].gsub(/\t/, ' ').length : 0
43
+ end
44
+ end
45
+ indents << 0 # just append one "empty line" for the readahead
46
+
47
+ wait_for = nil
48
+ lines.each_with_index do |l, index|
49
+ if wait_for
50
+ txt << l
51
+ wait_for = nil if l.match(wait_for)
52
+ next
53
+ end
54
+
55
+ # lookahead to find needed ends early
56
+ indent = indents[index..-1].detect{|i| i > -1}
57
+ actual_line = lines[index..-1].detect{|line| not line.match /^\s*$/ }
58
+ next_indent = indents[(index+1)..-1].detect{|i| i > -1}
59
+
60
+ # detect needed ends
61
+ stack.reverse.each do |pindent|
62
+ if indent <= pindent
63
+ m = actual_line.match(/^(\s*)(else|end|rescue)/)
64
+ if (not m) or indent != pindent
65
+ txt << (" " * pindent) + "end\n"
66
+ end
67
+ stack.pop
68
+ end
69
+ end
70
+
71
+ if (indent < next_indent) and (indent != stack.last)
72
+ block_match = l.match(/<<(\w+)\s*$/)
73
+ if block_match
74
+ wait_for = /^#{block_match.captures[0]}\s*$/
75
+ else
76
+ # possibly we need to insert a "do"
77
+ m = l.match /^\s*(module|class|def|begin|for|while|if|unless|else|rescue)/
78
+ l.sub!(/( do)?(\s*\|.*\|)?(\s*)$/, ' do\2\3') unless m
79
+ stack.push indent
80
+ end
81
+ end
82
+ txt << l
83
+ end
84
+
85
+ while not stack.empty?
86
+ txt << (" " * stack.pop) + "end\n"
87
+ end
88
+
89
+ txt
90
+ end
91
+
92
+ def self.main
93
+ infile = ARGV[0]
94
+ unless infile
95
+ puts "Usage: lax <file.rbl>"
96
+ return
97
+ end
98
+
99
+ puts convert(infile)
100
+ end
101
+ end
102
+
103
+ module Kernel
104
+ req = respond_to?(:gem_original_require) ? 'gem_original_require' : 'require'
105
+ module_eval <<END
106
+ alias lax_original_require #{req}
107
+ def #{req}(path)
108
+ lax_require path
109
+ end
110
+ END
111
+
112
+ alias lax_original_load load
113
+ def load(path)
114
+ file = Lax.find_file(path)
115
+ Lax.load(file) || lax_original_load(path)
116
+ end
117
+
118
+ private
119
+ def lax_require(path)
120
+ return false if $".include? path
121
+
122
+ file = Lax.find_file(path, Lax.extensions)
123
+ if Lax.load(file)
124
+ $" << path
125
+ return true
126
+ else
127
+ lax_original_require path
128
+ end
129
+ end
130
+
131
+ end
132
+
File without changes
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: QaDeS-lax
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Klaus
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-27 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Lax is a preprocessor to relax Ruby syntax.
17
+ email: Michael.Klaus@gmx.net
18
+ executables:
19
+ - lax
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - Rakefile
26
+ - README.rdoc
27
+ - lib/lax.rb
28
+ has_rdoc: true
29
+ homepage: http://github.com/QaDeS/lax
30
+ post_install_message:
31
+ rdoc_options:
32
+ - --title
33
+ - Lax
34
+ - --main
35
+ - README.rdoc
36
+ - --line-numbers
37
+ - --inline-source
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.2.0
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: A preprocessor to relax Ruby's syntax.
59
+ test_files:
60
+ - test/test.rb