class Peggy::Builder
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
.
Attributes
Current parent being built
Productions to build
Public Class Methods
Constructor
# File lib/parse/builder.rb, line 324 def initialize reset! end
Public Instance Methods
Reference
a production by its name index.
# File lib/parse/builder.rb, line 335 def [] index productions[index] end
Build an Alternatives
element.
# File lib/parse/builder.rb, line 365 def alt &blk build_piece Alternatives, blk end
Build or match the end of file element. If currently building, a Reference
to eof is built. Otherwise eof is matched.
Peggy::Parser#eof
# File lib/parse/builder.rb, line 373 def eof *args if @building method_missing :eof, *args else super args.first end end
Add an Literal
element to the parent.
# 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
Build an AnyNumber
element.
# File lib/parse/builder.rb, line 402 def many &blk build_piece AnyNumber, blk end
Create a production if at the top level, or a reference to a production a production is being built.
# 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
Build a negative predicate. Use when you want to make sure the enclosed element is not present. The cursor is not advanced for predicates.
# File lib/parse/builder.rb, line 418 def neg &blk build_piece Negative, blk end
Build an Optional
element.
# File lib/parse/builder.rb, line 407 def opt &blk build_piece Optional, blk end
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.
Peggy::Parser#parse?
# File lib/parse/builder.rb, line 431 def parse? goal, source=nil, index=0 @building = nil super end
Build a positive predicate. Use when you want to make sure the enclosed element is present. If matched the cursor is not advanced.
# File lib/parse/builder.rb, line 424 def pos &blk build_piece Positive, blk end
Clear the parser and prepare it for a new parse.
# File lib/parse/builder.rb, line 329 def reset! @building = true @productions = {} end
Build a Sequence
element.
# File lib/parse/builder.rb, line 382 def seq &blk build_piece Sequence, blk end
Build an AtLeastOne
element.
# File lib/parse/builder.rb, line 412 def some &blk build_piece AtLeastOne, blk end
Private Instance Methods
Add an object of klass to the parent and yield to its block. If value is specified it is passed to the klass constructor.
# File lib/parse/builder.rb, line 446 def build_piece klass, blk=nil, value=nil # puts "building #{klass.name} with #{value.inspect}" elem = value ? klass.new(value) : klass.new @parent << elem if blk parent = @parent @parent = elem blk.call @parent = parent end end