Class: Peggy::Builder
- Defined in:
- lib/builder.rb,
lib/Copy of builder.rb
Overview
Parser builder. The built in methods create syntax elements. Any other method called on this object create references to production, or actual productions, if called at the top level. Todo: Change to a class and separate from Parser.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
Current parent being built.
-
#productions ⇒ Object
readonly
Productions to build.
Attributes inherited from Parser
#debug_flag, #ignore_productions, #parse_results, #source_text
Instance Method Summary collapse
-
#[](index) ⇒ Object
Reference a production by its name index.
-
#each(&blk) ⇒ Object
(also: #seq)
Add an Sequence element to the parent.
- #eof(*args) ⇒ Object
-
#initialize ⇒ Builder
constructor
A new instance of Builder.
-
#lit(*values) ⇒ Object
Add an Literal element to the parent.
-
#many(&blk) ⇒ Object
Add an AnyNumber element to the parent.
-
#method_missing(name, *args) ⇒ Object
Create a production if at the top level, or a reference to a production a production is being built.
- #neg(&blk) ⇒ Object
-
#one(&blk) ⇒ Object
(also: #alt)
Add an Alternatives element to the parent.
-
#opt(&blk) ⇒ Object
Add an Optional element to the parent.
- #parse?(goal, index = 0) ⇒ Boolean
- #pos(&blk) ⇒ Object
- #reset! ⇒ Object
-
#some(&blk) ⇒ Object
Add an AtLeastOne element to the parent.
Methods inherited from Parser
#allow?, #check?, #correct_regexp!, #dissallow?, #ignore?, #literal?, #match?, #query?, #regexp?, #string?
Constructor Details
#initialize ⇒ Builder
Returns a new instance of Builder.
246 247 248 |
# File 'lib/builder.rb', line 246 def initialize @building = true end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
Create a production if at the top level, or a reference to a production a production is being built.
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/builder.rb', line 257 def method_missing name, *args if @building if @parent ref = Reference.new name @parent << ref elsif block_given? @productions = {} unless @productions prod = Production.new name @parent = prod yield @parent = nil @productions[name] = prod else super end else prod = @productions[name] super unless prod # puts "matching #{name} at #{args.first}" prod.match self, args.first end end |
Instance Attribute Details
#parent ⇒ Object (readonly)
Current parent being built
244 245 246 |
# File 'lib/builder.rb', line 244 def parent @parent end |
#productions ⇒ Object (readonly)
Productions to build
242 243 244 |
# File 'lib/builder.rb', line 242 def productions @productions end |
Instance Method Details
#[](index) ⇒ Object
Reference a production by its name index.
251 252 253 |
# File 'lib/builder.rb', line 251 def [] index productions[index] end |
#each(&blk) ⇒ Object Also known as: seq
Add an Sequence element to the parent.
294 295 296 |
# File 'lib/builder.rb', line 294 def each &blk build_piece Sequence, blk end |
#eof(*args) ⇒ Object
287 288 289 290 291 |
# File 'lib/builder.rb', line 287 def eof *args if args.length == 1 then super args.first else method_missing :eof, *args end end |
#lit(*values) ⇒ Object
Add an Literal element to the parent.
301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/builder.rb', line 301 def lit *values if values.size == 1 build_piece Literal, nil, values.first else one{ for v in values build_piece Literal, nil, v end } end end |
#many(&blk) ⇒ Object
Add an AnyNumber element to the parent.
314 315 316 |
# File 'lib/builder.rb', line 314 def many &blk build_piece AnyNumber, blk end |
#neg(&blk) ⇒ Object
328 329 330 |
# File 'lib/builder.rb', line 328 def neg &blk build_piece Negative, blk end |
#one(&blk) ⇒ Object Also known as: alt
Add an Alternatives element to the parent.
281 282 283 |
# File 'lib/builder.rb', line 281 def one &blk build_piece Alternatives, blk end |
#opt(&blk) ⇒ Object
Add an Optional element to the parent.
319 320 321 |
# File 'lib/builder.rb', line 319 def opt &blk build_piece Optional, blk end |
#parse?(goal, index = 0) ⇒ Boolean
336 337 338 339 |
# File 'lib/builder.rb', line 336 def parse? goal, index=0 @building = nil super end |
#pos(&blk) ⇒ Object
332 333 334 |
# File 'lib/builder.rb', line 332 def pos &blk build_piece Positive, blk end |
#reset! ⇒ Object
250 251 252 253 |
# File 'lib/Copy of builder.rb', line 250 def reset! @building = true @productions = {} end |
#some(&blk) ⇒ Object
Add an AtLeastOne element to the parent.
324 325 326 |
# File 'lib/builder.rb', line 324 def some &blk build_piece AtLeastOne, blk end |