Class: RubyPeg
- Inherits:
-
Object
- Object
- RubyPeg
- Defined in:
- lib/rubypeg.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#cache ⇒ Object
writeonly
Sets the attribute cache.
-
#index ⇒ Object
Returns the value of attribute index.
-
#sequences ⇒ Object
Returns the value of attribute sequences.
-
#text_to_parse ⇒ Object
Returns the value of attribute text_to_parse.
Class Method Summary collapse
-
.parse(text_to_parse) ⇒ Object
See #parse.
- .parse_and_dump(text_to_parse, dump_positive_matches_only = false) ⇒ Object
Instance Method Summary collapse
- #any_character ⇒ Object
- #any_number_of ⇒ Object
- #followed_by(&block) ⇒ Object
- #ignore(&block) ⇒ Object
- #node(t, &block) ⇒ Object
- #not_followed_by(&block) ⇒ Object
- #one_or_more ⇒ Object
- #optional ⇒ Object
- #parse(text_to_parse) ⇒ Object
- #pretty_print_cache(only_if_match = false) ⇒ Object
- #root ⇒ Object
- #sequence ⇒ Object
- #terminal(t) ⇒ Object
Instance Attribute Details
#cache=(value) ⇒ Object
Sets the attribute cache
151 152 153 |
# File 'lib/rubypeg.rb', line 151 def cache=(value) @cache = value end |
#index ⇒ Object
Returns the value of attribute index.
151 152 153 |
# File 'lib/rubypeg.rb', line 151 def index @index end |
#sequences ⇒ Object
Returns the value of attribute sequences.
151 152 153 |
# File 'lib/rubypeg.rb', line 151 def sequences @sequences end |
#text_to_parse ⇒ Object
Returns the value of attribute text_to_parse.
151 152 153 |
# File 'lib/rubypeg.rb', line 151 def text_to_parse @text_to_parse end |
Class Method Details
.parse(text_to_parse) ⇒ Object
See #parse
140 141 142 |
# File 'lib/rubypeg.rb', line 140 def self.parse(text_to_parse) self.new.parse(text_to_parse) end |
.parse_and_dump(text_to_parse, dump_positive_matches_only = false) ⇒ Object
144 145 146 147 148 149 |
# File 'lib/rubypeg.rb', line 144 def self.parse_and_dump(text_to_parse, dump_positive_matches_only = false) e = new r = e.parse(text_to_parse) e.pretty_print_cache(dump_positive_matches_only) r end |
Instance Method Details
#any_character ⇒ Object
171 172 173 |
# File 'lib/rubypeg.rb', line 171 def any_character terminal /./ end |
#any_number_of ⇒ Object
188 189 190 191 192 193 194 |
# File 'lib/rubypeg.rb', line 188 def any_number_of results = [] while result = yield results << result end results end |
#followed_by(&block) ⇒ Object
210 211 212 213 214 215 216 |
# File 'lib/rubypeg.rb', line 210 def followed_by(&block) start_index = self.index result = sequence(&block) self.index = start_index return :ignore if result return nil end |
#ignore(&block) ⇒ Object
165 166 167 168 169 |
# File 'lib/rubypeg.rb', line 165 def ignore(&block) result = sequence(&block) return :ignore if result nil end |
#node(t, &block) ⇒ Object
227 228 229 230 |
# File 'lib/rubypeg.rb', line 227 def node(t,&block) return put_in_sequence(cached(t)) if cached?(t) put_in_sequence(cache(t,self.index,uncached_node(t,&block))) end |
#not_followed_by(&block) ⇒ Object
218 219 220 |
# File 'lib/rubypeg.rb', line 218 def not_followed_by(&block) followed_by(&block) ? nil : :ignore end |
#one_or_more ⇒ Object
179 180 181 182 183 184 185 186 |
# File 'lib/rubypeg.rb', line 179 def one_or_more results = [] while result = yield results << result end return nil if results.empty? results end |
#optional ⇒ Object
175 176 177 |
# File 'lib/rubypeg.rb', line 175 def optional return yield || :ignore end |
#parse(text_to_parse) ⇒ Object
153 154 155 156 157 158 159 |
# File 'lib/rubypeg.rb', line 153 def parse(text_to_parse) self.index = 0 self.text_to_parse = text_to_parse self.cache = {} self.sequences = [[]] root end |
#pretty_print_cache(only_if_match = false) ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/rubypeg.rb', line 232 def pretty_print_cache(only_if_match = false) (0...text_to_parse.size).each do |i| print "#{text_to_parse[i].inspect[1...-1]}\t#{i}\t" @cache.each do |name,indexes| result = indexes[i] next unless result if only_if_match print "[#{name.inspect},#{result.first.inspect}] " if result.first else print "[#{name.inspect},#{result.first.inspect}] " end end print "\n" end end |
#root ⇒ Object
161 162 163 |
# File 'lib/rubypeg.rb', line 161 def root terminal(/.*/m) end |
#sequence ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/rubypeg.rb', line 196 def sequence start_index = self.index self.sequences.push([]) if yield results = self.sequences.pop results.delete_if {|r| r == :ignore } return results else self.sequences.pop self.index = start_index return nil end end |
#terminal(t) ⇒ Object
222 223 224 225 |
# File 'lib/rubypeg.rb', line 222 def terminal(t) return put_in_sequence(cached(t)) if cached?(t) put_in_sequence(cache(t,self.index,uncached_terminal(t))) end |