rubypp 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rubypp.rb +102 -0
- metadata +31 -46
- data.tar.gz.sum +0 -1
- metadata.gz.sum +0 -1
data/lib/rubypp.rb
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
class RubyPP
|
2
|
+
class Environment
|
3
|
+
def initialize(preprocessor)
|
4
|
+
@preprocessor = preprocessor
|
5
|
+
end
|
6
|
+
|
7
|
+
def puts(*lines)
|
8
|
+
@preprocessor.output.puts(*lines)
|
9
|
+
end
|
10
|
+
|
11
|
+
def binding
|
12
|
+
return super
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
attr_reader :input
|
17
|
+
attr_reader :output
|
18
|
+
attr_reader :filename
|
19
|
+
|
20
|
+
def initialize(input, output, filename)
|
21
|
+
@input = input
|
22
|
+
@output = output
|
23
|
+
@filename = filename
|
24
|
+
@linenum = 1
|
25
|
+
@environment = Environment.new(self)
|
26
|
+
@binding = @environment.binding
|
27
|
+
end
|
28
|
+
|
29
|
+
def getline
|
30
|
+
line = @input.gets
|
31
|
+
@linenum += 1 if not line.nil?
|
32
|
+
return line
|
33
|
+
end
|
34
|
+
|
35
|
+
def preprocess
|
36
|
+
success = false
|
37
|
+
begin
|
38
|
+
loop do
|
39
|
+
line = getline
|
40
|
+
break if line.nil?
|
41
|
+
case line
|
42
|
+
when /(.*[^\\]|^)\#\{(.*?)\}(.*)/
|
43
|
+
@output.puts "#{$1}#{evaluate($2, @linenum)}#{$3}"
|
44
|
+
when /^\#ruby\s+<<(.*)/
|
45
|
+
marker = $1
|
46
|
+
str = ''
|
47
|
+
evalstart = @linenum
|
48
|
+
loop do
|
49
|
+
line = getline
|
50
|
+
if line.nil? then
|
51
|
+
raise "End of input without #{marker}"
|
52
|
+
end
|
53
|
+
break if line.chomp == marker
|
54
|
+
str << line
|
55
|
+
end
|
56
|
+
result = evaluate(str, evalstart)
|
57
|
+
@output.puts result if not result.nil?
|
58
|
+
when /^\#ruby\s+(.*)/
|
59
|
+
str = line = $1
|
60
|
+
while line[-1] == ?\\
|
61
|
+
str.chop!
|
62
|
+
line = getline
|
63
|
+
break if line.nil?
|
64
|
+
line.chomp!
|
65
|
+
str << line
|
66
|
+
end
|
67
|
+
result = evaluate(str, @linenum)
|
68
|
+
@output.puts result if not result.nil?
|
69
|
+
else
|
70
|
+
@output.puts line
|
71
|
+
end
|
72
|
+
end
|
73
|
+
success = true
|
74
|
+
ensure
|
75
|
+
if not success then
|
76
|
+
$stderr.puts "Error on line #{@linenum}:"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def evaluate(str, linenum)
|
82
|
+
result = eval(str, @binding, @filename, linenum)
|
83
|
+
return result
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def rubypp(input_file, output_file)
|
88
|
+
input = input_file ? File.open(input_file) : $stdin
|
89
|
+
output = output_file ? File.open(output_file, 'w') : $stdout
|
90
|
+
|
91
|
+
success = false
|
92
|
+
begin
|
93
|
+
preprocessor = RubyPP.new(input, output, input_file || "(stdin)")
|
94
|
+
preprocessor.preprocess()
|
95
|
+
success = true
|
96
|
+
ensure
|
97
|
+
if not success then
|
98
|
+
File.unlink(output_file) rescue Errno::ENOENT
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
metadata
CHANGED
@@ -1,69 +1,54 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubypp
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
|
-
authors:
|
7
|
+
authors:
|
7
8
|
- Paul Brannan
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
|
12
|
-
date: 2012-05-03 00:00:00 Z
|
12
|
+
date: 2012-05-03 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
#include <stdio.h>
|
20
|
-
|
21
|
-
#ruby <<END
|
22
|
-
a = 42
|
23
|
-
nil # the last value of the block gets inserted into the output stream
|
24
|
-
END
|
25
|
-
|
26
|
-
int main()
|
27
|
-
{
|
28
|
-
printf("The answer is: #{a}\n");
|
29
|
-
}
|
30
|
-
|
14
|
+
description: ! "Rubypp is a preprocessor that uses ruby to transform text. Syntax
|
15
|
+
is\nsimilar to the C preprocessor, e.g.:\n\n#include <stdio.h>\n\n#ruby <<END\n
|
16
|
+
\ a = 42\n nil # the last value of the block gets inserted into the output stream\nEND\n\nint
|
17
|
+
main()\n{\n printf(\"The answer is: #{a}\\n\");\n}\n"
|
31
18
|
email: curlypaul924@gmail.com
|
32
|
-
executables:
|
19
|
+
executables:
|
33
20
|
- rubypp
|
34
21
|
extensions: []
|
35
|
-
|
36
|
-
extra_rdoc_files:
|
22
|
+
extra_rdoc_files:
|
37
23
|
- README.markdown
|
38
|
-
files:
|
24
|
+
files:
|
25
|
+
- lib/rubypp.rb
|
26
|
+
- bin/rubypp
|
39
27
|
- README.markdown
|
40
28
|
- test/test_rubypp.rb
|
41
|
-
- bin/rubypp
|
42
29
|
homepage: http://github.com/cout/rubypp/
|
43
30
|
licenses: []
|
44
|
-
|
45
|
-
metadata: {}
|
46
|
-
|
47
31
|
post_install_message:
|
48
32
|
rdoc_options: []
|
49
|
-
|
50
|
-
require_paths:
|
33
|
+
require_paths:
|
51
34
|
- lib
|
52
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
- !ruby/object:Gem::Version
|
57
|
-
version:
|
58
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
-
|
60
|
-
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
61
47
|
requirements: []
|
62
|
-
|
63
48
|
rubyforge_project:
|
64
|
-
rubygems_version:
|
49
|
+
rubygems_version: 1.8.21
|
65
50
|
signing_key:
|
66
|
-
specification_version:
|
51
|
+
specification_version: 3
|
67
52
|
summary: A preprocessor that uses ruby to transform text
|
68
|
-
test_files:
|
53
|
+
test_files:
|
69
54
|
- test/test_rubypp.rb
|
data.tar.gz.sum
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
SHA1 b9d6fb9613127054a91fb221fd4815fa08ca1880
|
metadata.gz.sum
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
SHA1 7758beb87d47c9c5c133d9cb0dfb90bd7f09744f
|