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
Search Repo:
Added the example group for helping with parser speccing
halorgium (author)
Sun Apr 13 14:59:43 -0700 2008
commit  c4a300edf9ff2c5f36c20a186c8ccffe8fa1994b
tree    057fe737453002bbb77c0af0eaf15c1aee03e992
parent  28f98a7c3c4bccfc3f0de0db7930240698aec8ba
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -1 +1,113 @@
0
+require 'rubygems'
0
+require 'treetop'
0
+require 'spec'
0
+
0
+module Treetop
0
+ # A custom RSpec example group which assists with the testing of the
0
+ # behaviors of the single rules rather than the default root.
0
+ #
0
+ # Using it as the default example group:
0
+ # require 'treetop/parser_example_group'
0
+ # Spec::Example::ExampleGroupFactory.default(Treetop::ParserExampleGroup)
0
+ #
0
+ # Using it for a subset of specs:
0
+ # require 'treetop/parser_example_group'
0
+ # Spec::Example::ExampleGroupFactory.register(:my_parser, Treetop::ParserExampleGroup)
0
+ class ParserExampleGroup < Spec::Example::ExampleGroup
0
+ class << self
0
+ attr_reader :default_klass, :default_root
0
+
0
+ def parse_from(klass, root = nil)
0
+ @default_klass, @default_root = klass, root
0
+ end
0
+ end
0
+
0
+ attr_reader :parser
0
+
0
+ def parse(input, options = {})
0
+ @parser = (options[:klass] || self.class.default_klass).new
0
+ if root = (options[:root] || self.class.default_root)
0
+ parser.root = root
0
+ end
0
+ unless options[:consume_all_input].nil?
0
+ parser.consume_all_input = options.delete(:consume_all_input)
0
+ end
0
+ result = parser.parse(input, options)
0
+ yield result if block_given?
0
+ result
0
+ end
0
+
0
+ class BeParsedAndEql
0
+ def initialize(parser, value)
0
+ @parser, @value = parser, value
0
+ end
0
+
0
+ def matches?(result)
0
+ @result = result
0
+ parsed? && correct_value?
0
+ end
0
+
0
+ def parsed?
0
+ !@result.nil?
0
+ end
0
+
0
+ def correct_value?
0
+ @result.value == @value
0
+ end
0
+
0
+ def failure_message
0
+ unless parsed?
0
+ "expected input to be parsed\nfailure_reason: #{@parser.failure_reason}"
0
+ else
0
+ "expected: #{@value.inspect},\n got: #{@result.value.inspect}"
0
+ end
0
+ end
0
+
0
+ def negative_failure_message
0
+ if parsed?
0
+ "expected input not to be parsed"
0
+ end
0
+ end
0
+ end
0
+
0
+ def be_parsed_and_eql(value)
0
+ BeParsedAndEql.new(parser, value)
0
+ end
0
+
0
+ class HaveFailedParsingBecause
0
+ def initialize(parser, failure_reason)
0
+ @parser, @failure_reason = parser, failure_reason
0
+ end
0
+
0
+ def matches?(result)
0
+ @target = result
0
+ not_parsed? && correct_reason?
0
+ end
0
+
0
+ def not_parsed?
0
+ @result.nil?
0
+ end
0
+
0
+ def correct_reason?
0
+ @parser.failure_reason == @failure_reason
0
+ end
0
+
0
+ def failure_message
0
+ unless not_parsed?
0
+ "expected input not to be parsed because #{@failure_reason}"
0
+ else
0
+ "failure message was incorrect\nexpected: #{@failure_reason},\n got: #{@parser.failure_reason}"
0
+ end
0
+ end
0
+
0
+ def negative_failure_message
0
+ raise RspecCommandError, "Cannot use this like so"
0
+ end
0
+ end
0
+
0
+ def have_failed_parsing_because(failure_message)
0
+ HaveFailedParsingBecause.new(parser, failure_message)
0
+ end
0
+ end
0
+end

Comments

    No one has commented yet.