Class: GhostWheel::ParserBuilder::GhostWheel

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ghost_wheel/parser_builder/ghost_wheel.rb

Constant Summary collapse

GRAMMER_PARSER =
::GhostWheel.build_parser do
  rule(:space_or_tab, opt(skip(/[ \t]+/)))
  rule(:white_space,  opt(skip(/\s+/)))
  
  rule(:single_string, /'(?:\\.|[^'\\]+)+'/)
  rule(:double_string, /"(?:\\.|[^"\\]+)+"/)
  rule(:string, alt(:single_string, :double_string)) { |str| eval str }
  
  rule(:regexp, %r{/(?:\\.|[^/\\]+)+/}) { |re| eval re }
  
  rule(:name, /[A-Za-z]\w*/) { |name| name.to_sym }
  
  rule( :block,
        seq( :white_space,
             "{",
             zplus(alt(/[^{}]+/, :block)),
             "}" ) { |blk| blk.flatten.join } )
  
  rule(:eof, /\bEOF\b/) { [:eof] }
  rule( :sequence,
        oplus(
          seq( :space_or_tab,
               alt(:eof, :string, :regexp, :name),
               opt(/[?*+]/) ) { |atom|
            atom.size == 1 ? atom[0] :
                             [ { "?" => :opt,
                                 "*" => :zplus,
                                 "+" => :oplus }[atom[1]], atom[0] ]
          } ) { |seq| seq.size == 1 ? seq[0] : [:seq, *seq] } )
  rule( :tranformation,
        seq(:sequence, opt(:block)) { |trans|
          trans.size == 1 ? trans[0] : [:trans, *trans]
        } )
  rule( :expression,
        seq( :tranformation,
             zplus( seq( skip(/\s*\|\s*/),
                    :tranformation ) { |alt| alt[0] } )
        ) { |alts|
          alts.size == 1 ? alts[0] : [:alt, alts[0], *alts[1]]
        } )
  
  rule( :rule, 
        seq( :name,
             skip(/\s*=\s*/),
             :expression ) ) { |r_def| [:rule, *r_def] }
  rule( :parser, 
        seq( :name,
             skip(/\s*:=\s*/),
             :expression ) ) { |p_def| [:parser, *p_def] }
  rule( :assignment,
        seq( :white_space,
             alt(:rule, :parser),
             :white_space ) { |assign| assign[0] } )
  
  parser(:grammar, oplus(:assignment))
end

Instance Method Summary collapse

Constructor Details

#initialize(grammar, optons = Hash.new) ⇒ GhostWheel

Returns a new instance of GhostWheel.



67
68
69
70
71
72
# File 'lib/ghost_wheel/parser_builder/ghost_wheel.rb', line 67

def initialize(grammar, optons = Hash.new)
  @parser  = ParserBuilder::Ruby.new
  @context = optons[:context] || Object.new.send(:binding)
  
  grammar_to_dsl(GRAMMER_PARSER.parse(grammar))
end