public
Description: A library for safe evaluation of Ruby code based on ParseTree/RubyParser and Ruby2Ruby. Provides Rails ActionView template handlers for ERB and Haml.
Homepage: http://www.artweb-design.de
Clone URL: git://github.com/svenfuchs/safemode.git
safemode / lib / ruby_parser_string_io_patch.diff
100644 195 lines (174 sloc) 4.775 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
--- lib/ruby_lexer.rb  2008-04-27 01:07:24.000000000 +0200
+++ lib/ruby_lexer.rb  2008-04-27 01:07:03.000000000 +0200
@@ -45,7 +45,7 @@
     raise "bad val: #{str.inspect}" unless String === str
 
     self.file = file
- self.lexer.src = StringIO.new(str)
+ self.lexer.src = RubyParser::StringIO.new(str)
 
     @yydebug = ENV.has_key? 'DEBUG'
 
@@ -2604,104 +2604,106 @@
   end
 end
 
-class StringIO # HACK: everything in here is a hack
- attr_accessor :begin_of_line, :was_begin_of_line
- alias :begin_of_line? :begin_of_line
- alias :read_all :read
+class RubyParser
+ class StringIO < StringIO # HACK: everything in here is a hack
+ attr_accessor :begin_of_line, :was_begin_of_line
+ alias :begin_of_line? :begin_of_line
+ alias :read_all :read
 
- alias :old_initialize :initialize
+ alias :old_initialize :initialize
   
- def initialize(*args)
- self.begin_of_line = true
- self.was_begin_of_line = false
- old_initialize(*args)
- @original_string = self.string.dup
- end
+ def initialize(*args)
+ self.begin_of_line = true
+ self.was_begin_of_line = false
+ old_initialize(*args)
+ @original_string = self.string.dup
+ end
 
- def rest
- self.string[self.pos..-1]
- end
+ def rest
+ self.string[self.pos..-1]
+ end
 
- def current_line # HAHA fuck you
- @original_string[0..self.pos][/\A.*__LINE__/m].split(/\n/).size
- end
+ def current_line # HAHA fuck you
+ @original_string[0..self.pos][/\A.*__LINE__/m].split(/\n/).size
+ end
 
- def read
- c = self.getc
+ def read
+ c = self.getc
 
- if c == ?\r then
- d = self.getc
- self.ungetc d if d and d != ?\n
- c = ?\n
- end
+ if c == ?\r then
+ d = self.getc
+ self.ungetc d if d and d != ?\n
+ c = ?\n
+ end
     
- self.was_begin_of_line = self.begin_of_line
- self.begin_of_line = c == ?\n
- if c and c != 0 then
- c.chr
- else
- ::RubyLexer::EOF
+ self.was_begin_of_line = self.begin_of_line
+ self.begin_of_line = c == ?\n
+ if c and c != 0 then
+ c.chr
+ else
+ ::RubyLexer::EOF
+ end
     end
- end
 
- def match_string term, indent=false # TODO: add case insensitivity, or just remove
- buffer = []
+ def match_string term, indent=false # TODO: add case insensitivity, or just remove
+ buffer = []
 
- if indent
- while c = self.read do
- if c !~ /\s/ or c == "\n" or c == "\r" then
- self.unread c
- break
+ if indent
+ while c = self.read do
+ if c !~ /\s/ or c == "\n" or c == "\r" then
+ self.unread c
+ break
+ end
+ buffer << c
         end
- buffer << c
       end
- end
 
- term.each_byte do |c2|
- c = self.read
- c = self.read if c and c == "\r"
- buffer << c
- if c and c2 != c[0] then
- self.unread_many buffer.join # HACK omg
- return false
+ term.each_byte do |c2|
+ c = self.read
+ c = self.read if c and c == "\r"
+ buffer << c
+ if c and c2 != c[0] then
+ self.unread_many buffer.join # HACK omg
+ return false
+ end
       end
+
+ return true
     end
 
- return true
- end
+ def read_line
+ self.begin_of_line = true
+ self.was_begin_of_line = false
+ gets.sub(/\r\n?$/, "\n") # HACK
+ end
 
- def read_line
- self.begin_of_line = true
- self.was_begin_of_line = false
- gets.sub(/\r\n?$/, "\n") # HACK
- end
-
- def peek expected = nil # FIX: barf
- c = self.getc
- return RubyLexer::EOF if c.nil?
- self.ungetc c if c
- c = c.chr if c
- if expected then
- c == expected
- else
- c
+ def peek expected = nil # FIX: barf
+ c = self.getc
+ return RubyLexer::EOF if c.nil?
+ self.ungetc c if c
+ c = c.chr if c
+ if expected then
+ c == expected
+ else
+ c
+ end
     end
- end
 
- def unread(c)
- return if c.nil? # UGH
+ def unread(c)
+ return if c.nil? # UGH
 
- # HACK: only depth is 2... who cares? really I want to remove all of this
- self.begin_of_line = self.was_begin_of_line || true
- self.was_begin_of_line = nil
+ # HACK: only depth is 2... who cares? really I want to remove all of this
+ self.begin_of_line = self.was_begin_of_line || true
+ self.was_begin_of_line = nil
 
- c = c[0] if String === c
- self.ungetc c
- end
+ c = c[0] if String === c
+ self.ungetc c
+ end
 
- def unread_many str
- str.split(//).reverse.each do |c|
- unread c
+ def unread_many str
+ str.split(//).reverse.each do |c|
+ unread c
+ end
     end
   end
 end