Class: Peggy::Builder
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.
-
#alt(&blk) ⇒ Object
(also: #one)
Build an Alternatives element.
-
#eof(*args) ⇒ Object
Build or match the end of file element.
-
#initialize ⇒ Builder
constructor
Constructor.
-
#lit(*values) ⇒ Object
Add an Literal element to the parent.
-
#many(&blk) ⇒ Object
Build an AnyNumber element.
-
#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
Build a negative predicate.
-
#opt(&blk) ⇒ Object
Build an Optional element.
-
#parse?(goal, source = nil, index = 0) ⇒ Boolean
Invokes the parser from the beginning of the source on the given production goal.
-
#pos(&blk) ⇒ Object
Build a positive predicate.
-
#reset! ⇒ Object
Clear the parser and prepare it for a new parse.
-
#seq(&blk) ⇒ Object
(also: #each)
Build a Sequence element.
-
#some(&blk) ⇒ Object
Build an AtLeastOne element.
-
#to_s ⇒ Object
Convert productions to Peggy grammar.
Methods inherited from Parser
#_memoize, #allow?, #ast?, #check?, #correct_regexp!, #dissallow?, #ignore?, #literal?, #match?, #query?, #regexp?, #string?
Constructor Details
#initialize ⇒ Builder
Constructor
324 325 326 |
# File 'lib/parse/builder.rb', line 324 def initialize reset! 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.
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/parse/builder.rb', line 341 def method_missing name, *args if @building if @parent ref = Reference.new name @parent << ref elsif block_given? prod = Production.new name @parent = prod yield @parent = nil @productions[name] = prod else super end else prod = @productions[name] # pp name.inspect, @productions.keys unless prod 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
321 322 323 |
# File 'lib/parse/builder.rb', line 321 def parent @parent end |
#productions ⇒ Object (readonly)
Productions to build
319 320 321 |
# File 'lib/parse/builder.rb', line 319 def productions @productions end |
Instance Method Details
#[](index) ⇒ Object
Reference a production by its name index.
335 336 337 |
# File 'lib/parse/builder.rb', line 335 def [] index productions[index] end |
#alt(&blk) ⇒ Object Also known as: one
Build an Alternatives element.
365 366 367 |
# File 'lib/parse/builder.rb', line 365 def alt &blk build_piece Alternatives, blk end |
#eof(*args) ⇒ Object
Build or match the end of file element. If currently building, a Reference to eof is built. Otherwise eof is matched.
373 374 375 376 377 378 379 |
# File 'lib/parse/builder.rb', line 373 def eof *args if @building method_missing :eof, *args else super args.first end end |
#lit(*values) ⇒ Object
Add an Literal element to the parent.
389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/parse/builder.rb', line 389 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
Build an AnyNumber element.
402 403 404 |
# File 'lib/parse/builder.rb', line 402 def many &blk build_piece AnyNumber, blk end |
#neg(&blk) ⇒ Object
Build a negative predicate. Use when you want to make sure the enclosed element is not present. The cursor is not advanced for predicates.
418 419 420 |
# File 'lib/parse/builder.rb', line 418 def neg &blk build_piece Negative, blk end |
#opt(&blk) ⇒ Object
Build an Optional element.
407 408 409 |
# File 'lib/parse/builder.rb', line 407 def opt &blk build_piece Optional, blk end |
#parse?(goal, source = nil, index = 0) ⇒ Boolean
Invokes the parser from the beginning of the source on the given production goal. You may provide the source here or you can set source_text prior to calling. If index is provided the parser will ignore characters previous to it.
431 432 433 434 |
# File 'lib/parse/builder.rb', line 431 def parse? goal, source=nil, index=0 @building = nil super end |
#pos(&blk) ⇒ Object
Build a positive predicate. Use when you want to make sure the enclosed element is present. If matched the cursor is not advanced.
424 425 426 |
# File 'lib/parse/builder.rb', line 424 def pos &blk build_piece Positive, blk end |
#reset! ⇒ Object
Clear the parser and prepare it for a new parse.
329 330 331 332 |
# File 'lib/parse/builder.rb', line 329 def reset! @building = true @productions = {} end |
#seq(&blk) ⇒ Object Also known as: each
Build a Sequence element.
382 383 384 |
# File 'lib/parse/builder.rb', line 382 def seq &blk build_piece Sequence, blk end |
#some(&blk) ⇒ Object
Build an AtLeastOne element.
412 413 414 |
# File 'lib/parse/builder.rb', line 412 def some &blk build_piece AtLeastOne, blk end |
#to_s ⇒ Object
Convert productions to Peggy grammar. This is notable to out put any Ruby parse methods, only grammars built with Builder methods.
438 439 440 |
# File 'lib/parse/builder.rb', line 438 def to_s productions.values.join "\n" end |