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/halorgium/treetop.git
commit  c4a300edf9ff2c5f36c20a186c8ccffe8fa1994b
tree    057fe737453002bbb77c0af0eaf15c1aee03e992
parent  28f98a7c3c4bccfc3f0de0db7930240698aec8ba
treetop / lib / treetop / parser_example_group.rb
100644 113 lines (94 sloc) 2.934 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
107
108
109
110
111
112
113
require 'rubygems'
require 'treetop'
require 'spec'
 
module Treetop
  # A custom RSpec example group which assists with the testing of the
  # behaviors of the single rules rather than the default root.
  #
  # Using it as the default example group:
  # require 'treetop/parser_example_group'
  # Spec::Example::ExampleGroupFactory.default(Treetop::ParserExampleGroup)
  #
  # Using it for a subset of specs:
  # require 'treetop/parser_example_group'
  # Spec::Example::ExampleGroupFactory.register(:my_parser, Treetop::ParserExampleGroup)
  class ParserExampleGroup < Spec::Example::ExampleGroup
    class << self
      attr_reader :default_klass, :default_root
 
      def parse_from(klass, root = nil)
        @default_klass, @default_root = klass, root
      end
    end
 
    attr_reader :parser
 
    def parse(input, options = {})
      @parser = (options[:klass] || self.class.default_klass).new
      if root = (options[:root] || self.class.default_root)
        parser.root = root
      end
      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
 
    class BeParsedAndEql
      def initialize(parser, value)
        @parser, @value = parser, value
      end
 
      def matches?(result)
        @result = result
        parsed? && correct_value?
      end
 
      def parsed?
        !@result.nil?
      end
 
      def correct_value?
        @result.value == @value
      end
 
      def failure_message
        unless parsed?
          "expected input to be parsed\nfailure_reason: #{@parser.failure_reason}"
        else
          "expected: #{@value.inspect},\n got: #{@result.value.inspect}"
        end
      end
 
      def negative_failure_message
        if parsed?
          "expected input not to be parsed"
        end
      end
    end
 
    def be_parsed_and_eql(value)
      BeParsedAndEql.new(parser, value)
    end
 
    class HaveFailedParsingBecause
      def initialize(parser, failure_reason)
        @parser, @failure_reason = parser, failure_reason
      end
 
      def matches?(result)
        @target = result
        not_parsed? && correct_reason?
      end
 
      def not_parsed?
        @result.nil?
      end
 
      def correct_reason?
        @parser.failure_reason == @failure_reason
      end
 
      def failure_message
        unless not_parsed?
          "expected input not to be parsed because #{@failure_reason}"
        else
          "failure message was incorrect\nexpected: #{@failure_reason},\n got: #{@parser.failure_reason}"
        end
      end
 
      def negative_failure_message
        raise RspecCommandError, "Cannot use this like so"
      end
    end
 
    def have_failed_parsing_because(failure_message)
      HaveFailedParsingBecause.new(parser, failure_message)
    end
  end
end