public
Fork of nathansobo/treetop
Description: A Ruby-based parsing DSL based on parsing expression grammars.
Homepage: http://treetop.rubyforge.org
Clone URL: git://github.com/juretta/treetop.git
Search Repo:
Petteri Räty (author)
Thu Mar 13 07:46:17 -0700 2008
Nathan Sobo (committer)
Thu Mar 13 21:31:39 -0700 2008
commit  28f98a7c3c4bccfc3f0de0db7930240698aec8ba
tree    fac482c5693e82454788b35c3da83c2864527944
parent  be4848611026d224ce106ff3249214f957dcb63c
treetop / spec / spec_helper.rb
100644 106 lines (90 sloc) 2.57 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
dir = File.dirname(__FILE__)
require 'rubygems'
require 'benchmark'
require 'spec'
 
unless $bootstrapped_gen_1_metagrammar
  load File.join(dir, '..', 'lib', 'treetop', 'bootstrap_gen_1_metagrammar.rb')
end
include Treetop
 
Spec::Runner.configure do |config|
  config.mock_with :rr
end
 
module Treetop
  class TreetopExampleGroup < Spec::Example::ExampleGroup
    class << self
      attr_accessor :parser_class_under_test
 
      def testing_expression(expression_under_test)
        testing_grammar(%{
grammar Test
rule expression_under_test
   }+expression_under_test+%{
   end
end
}.tabto(0))
      end
 
      def testing_grammar(grammar_under_test)
        grammar_node = parse_with_metagrammar(grammar_under_test.strip, :grammar)
        parser_code = grammar_node.compile
        class_eval(parser_code)
        self.parser_class_under_test = const_get(grammar_node.parser_name.to_sym)
      end
 
      def parse_with_metagrammar(input, root)
        parser = Treetop::Compiler::MetagrammarParser.new
        parser.root = root
        node = parser.parse(input)
        raise parser.failure_reason unless node
        node
      end
 
    end
 
    attr_reader :parser
 
    def parse_with_metagrammar(input, root)
      self.class.parse_with_metagrammar(input, root)
    end
 
    def parser_class_under_test
      self.class.parser_class_under_test
    end
 
    def parse(input, options = {})
      @parser = parser_class_under_test.new
      unless options[:consume_all_input].nil?
        parser.consume_all_input = options.delete(:consume_all_input)
      end
      result = parser.parse(input, options)
      yield result if block_given?
      result
    end
 
    def compiling_grammar(grammar_under_test)
      lambda {
        grammar_node = parse_with_metagrammar(grammar_under_test.strip, :grammar)
        parser_code = grammar_node.compile
        [grammar_node, parser_code]
      }
    end
 
    def compiling_expression(expression_under_test)
      compiling_grammar(%{
grammar Test
rule expression_under_test
#{expression_under_test}
end
end
}.tabto(0))
    end
 
    def optionally_benchmark(&block)
      if BENCHMARK
        Benchmark.bm do |x|
          x.report(&block)
        end
      else
        yield
      end
    end
 
    Spec::Example::ExampleGroupFactory.register(:compiler, self)
    Spec::Example::ExampleGroupFactory.register(:runtime, self)
  end
end
 
class Symbol
  def to_proc
    lambda do |x|
      x.send(self)
    end
  end
end