Class: Treetop::TreetopExampleGroup

Inherits:
Spec::Example::ExampleGroup
  • Object
show all
Defined in:
lib/vendor/treetop/spec/spec_helper.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.parser_class_under_testObject

Returns the value of attribute parser_class_under_test.



18
19
20
# File 'lib/vendor/treetop/spec/spec_helper.rb', line 18

def parser_class_under_test
  @parser_class_under_test
end

Instance Attribute Details

#parserObject (readonly)

Returns the value of attribute parser.



47
48
49
# File 'lib/vendor/treetop/spec/spec_helper.rb', line 47

def parser
  @parser
end

Class Method Details

.parse_with_metagrammar(input, root) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/vendor/treetop/spec/spec_helper.rb', line 37

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

.testing_expression(expression_under_test) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/vendor/treetop/spec/spec_helper.rb', line 20

def testing_expression(expression_under_test)
  testing_grammar(%{
    grammar Test
      rule expression_under_test
  }+expression_under_test+%{
end
    end
  }.tabto(0))
end

.testing_grammar(grammar_under_test) ⇒ Object



30
31
32
33
34
35
# File 'lib/vendor/treetop/spec/spec_helper.rb', line 30

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

Instance Method Details

#compiling_expression(expression_under_test) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/vendor/treetop/spec/spec_helper.rb', line 75

def compiling_expression(expression_under_test)
  compiling_grammar(%{
    grammar Test
      rule expression_under_test
        #{expression_under_test}
      end
    end
  }.tabto(0))
end

#compiling_grammar(grammar_under_test) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/vendor/treetop/spec/spec_helper.rb', line 67

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

#optionally_benchmark(&block) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/vendor/treetop/spec/spec_helper.rb', line 85

def optionally_benchmark(&block)
  if BENCHMARK
    Benchmark.bm do |x|
      x.report(&block)
    end
  else
    yield
  end
end

#parse(input, options = {}) {|result| ... } ⇒ Object

Yields:

  • (result)


57
58
59
60
61
62
63
64
65
# File 'lib/vendor/treetop/spec/spec_helper.rb', line 57

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

#parse_with_metagrammar(input, root) ⇒ Object



49
50
51
# File 'lib/vendor/treetop/spec/spec_helper.rb', line 49

def parse_with_metagrammar(input, root)
  self.class.parse_with_metagrammar(input, root)
end

#parser_class_under_testObject



53
54
55
# File 'lib/vendor/treetop/spec/spec_helper.rb', line 53

def parser_class_under_test
  self.class.parser_class_under_test
end