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

parent[R]

Current parent being built

productions[R]

Productions to build

Public Class Methods

new() click to toggle source

Constructor

# File lib/parse/builder.rb, line 324
def initialize
  reset!
end

Public Instance Methods

[](index) click to toggle source

Reference a production by its name index.

# File lib/parse/builder.rb, line 335
def [] index
  productions[index]
end
alt(&blk) click to toggle source

Build an Alternatives element.

# File lib/parse/builder.rb, line 365
def alt &blk
  build_piece Alternatives, blk
end
Also aliased as: one
each(&blk)

Synonym for each()

Alias for: seq
eof(*args) click to toggle source

Build or match the end of file element. If currently building, a Reference to eof is built. Otherwise eof is matched.

Calls superclass method 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
lit(*values) click to toggle source

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
many(&blk) click to toggle source

Build an AnyNumber element.

# File lib/parse/builder.rb, line 402
def many &blk
  build_piece AnyNumber, blk
end
method_missing(name, *args) { || ... } click to toggle source

Create a production if at the top level, or a reference to a production a production is being built.

Calls superclass method
# 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
neg(&blk) click to toggle source

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
one(&blk)

Synonym for alt().

Alias for: alt
opt(&blk) click to toggle source

Build an Optional element.

# File lib/parse/builder.rb, line 407
def opt &blk
  build_piece Optional, blk
end
parse?(goal, source=nil, index=0) click to toggle source

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.

Calls superclass method Peggy::Parser#parse?
# File lib/parse/builder.rb, line 431
def parse? goal, source=nil, index=0
  @building = nil
  super
end
pos(&blk) click to toggle source

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
reset!() click to toggle source

Clear the parser and prepare it for a new parse.

# File lib/parse/builder.rb, line 329
def reset!
  @building = true
  @productions = {}
end
seq(&blk) click to toggle source

Build a Sequence element.

# File lib/parse/builder.rb, line 382
def seq &blk
  build_piece Sequence, blk
end
Also aliased as: each
some(&blk) click to toggle source

Build an AtLeastOne element.

# File lib/parse/builder.rb, line 412
def some &blk
  build_piece AtLeastOne, blk
end
to_s() click to toggle source

Convert productions to Peggy grammar. This is notable to out put any Ruby parse methods, only grammars built with Builder methods.

# File lib/parse/builder.rb, line 438
def to_s
  productions.values.join "\n"
end

Private Instance Methods

build_piece(klass, blk=nil, value=nil) click to toggle source

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