rdparser 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -5,3 +5,5 @@ test_files/
5
5
  Gemfile.lock
6
6
  .yardoc/
7
7
  pkg/
8
+ .*.swp
9
+ rdoc/
data/Rakefile CHANGED
@@ -29,12 +29,12 @@ end
29
29
 
30
30
  task :spec => 'spec:progress'
31
31
 
32
- desc 'Generate documentation for the a4-core plugin.'
33
- Rake::RDocTask.new(:rdoc) do |rdoc|
32
+ desc 'Generate documentation for the rdparser plugin.'
33
+ RDoc::Task.new(:rdoc) do |rdoc|
34
34
  rdoc.rdoc_dir = 'rdoc'
35
35
  rdoc.title = 'RDParser'
36
36
  rdoc.options << '--line-numbers' << '--inline-source' << '--charset=UTF-8'
37
- rdoc.rdoc_files.include('README')
37
+ #rdoc.rdoc_files.include('README')
38
38
  rdoc.rdoc_files.include('lib/**/*.rb')
39
39
  end
40
40
 
@@ -3,7 +3,10 @@ require File.expand_path('../patches/array.rb', __FILE__)
3
3
 
4
4
  # A Simple Recursive Descending (LL) Parser for Ruby
5
5
  module RDParser
6
- require File.expand_path('../rdparser/exceptions.rb', __FILE__)
6
+
7
+ # raised when Parser cannot get through the entire string.
8
+ class ParserError < StandardError; end
9
+
7
10
  autoload :RDParser, File.expand_path('../rdparser/rdparser.rb', __FILE__)
8
11
  autoload :Scanner, File.expand_path('../rdparser/scanner.rb', __FILE__)
9
12
 
@@ -14,15 +14,15 @@ class RDParser::RDParser
14
14
  @depth = -1
15
15
 
16
16
  # Create a string scanner object based on the content
17
- @content = RDParser::Scanner.new(content, :slurp_whitespace => !options[:manual_whitespace])
17
+ @content = ::RDParser::Scanner.new(content, :slurp_whitespace => !options[:manual_whitespace])
18
18
 
19
19
  # Kick off the show by parsing from the rule specified
20
- result = parse_section(rule.to_sym).flatten
20
+ results = parse_section(rule.to_sym)
21
21
 
22
22
  # Raises ParserError when cannot get parse the entire content and :partial is not set.
23
- raise ParserError, %{Cannot parse the entire content! (error was on position ##{@content.position}: "#{content[0...@content.position]}|#{content[@content.position..@content.position]}|#{content[@content.position+1..-1]}")} unless options[:partial] == true or @content.eos?
23
+ raise ::RDParser::ParserError, %{Cannot parse the entire content! (error was on position ##{@content.position}: "#{content[0...@content.position]}|#{content[@content.position..@content.position]}|#{content[@content.position+1..-1]}")} unless options[:partial] == true or @content.eos?
24
24
 
25
- result
25
+ [results].flatten
26
26
  end
27
27
 
28
28
  # Parse the content based on the rulesets for a particular grammar context
@@ -32,7 +32,7 @@ class RDParser::RDParser
32
32
 
33
33
  # For each distinct set of rules within a ruleset, get parsing..
34
34
  sub_rulesets(rule).each do |ruleset|
35
- RDParser::DEBUG && debug_message("RULE SET : #{ruleset}")
35
+ ::RDParser::DEBUG && debug_message("RULE SET : #{ruleset}")
36
36
 
37
37
  # By default, we assume failure!
38
38
  success = false
@@ -43,7 +43,7 @@ class RDParser::RDParser
43
43
 
44
44
  # Go through each rule in this ruleset
45
45
  sub_rules(ruleset).each do |r|
46
- RDParser::DEBUG && debug_message("Running rule '#{r}' against '#{@content.lookahead}'")
46
+ ::RDParser::DEBUG && debug_message("Running rule '#{r}' against '#{@content.lookahead}'")
47
47
  suboutput = []
48
48
 
49
49
  # Match a rule with 1 or more occurrences (e.g. "rule(s)")
@@ -95,22 +95,22 @@ class RDParser::RDParser
95
95
 
96
96
  # We've either processed all the rules for this ruleset, or.. it failed
97
97
  if success
98
- RDParser::DEBUG && debug_message("Success of all rules in #{ruleset}")
98
+ ::RDParser::DEBUG && debug_message("Success of all rules in #{ruleset}")
99
99
 
100
100
  # No need to check any more rulesets! We've just passed one,
101
101
  # so drop the depth a notch, we're headed back up the tree of recursion!
102
102
  @depth -= 1
103
- RDParser::DEBUG && debug_message("SUCCEED: #{ruleset}", :passback)
103
+ ::RDParser::DEBUG && debug_message("SUCCEED: #{ruleset}", :passback)
104
104
  return output
105
105
  else
106
- RDParser::DEBUG && debug_message("failed #{ruleset}.. moving on")
106
+ ::RDParser::DEBUG && debug_message("failed #{ruleset}.. moving on")
107
107
 
108
108
  # If the rule set failed, revert the position back to that we stored earlier
109
109
  @content.rollback_to(was_position)
110
110
 
111
111
  # And clean the output.. because any output we got from a broken rule is as useful
112
112
  # as an ashtray on a motorbike, a chocolate teapot, or ice cutlery.
113
- RDParser::DEBUG && debug_message("FAIL: #{ruleset}", :passback)
113
+ ::RDParser::DEBUG && debug_message("FAIL: #{ruleset}", :passback)
114
114
  end
115
115
  end
116
116
 
@@ -121,7 +121,7 @@ class RDParser::RDParser
121
121
 
122
122
  # Parse the content based on a single subrule
123
123
  def match_for_rule(rule)
124
- RDParser::DEBUG && debug_message("TRYING #{rule}")
124
+ ::RDParser::DEBUG && debug_message("TRYING #{rule}")
125
125
  output = []
126
126
  rule_data = @grammar[rule]
127
127
 
@@ -129,7 +129,7 @@ class RDParser::RDParser
129
129
  if rule.to_s =~ /^\"(.+)\"$/
130
130
  m = $1
131
131
  if @content.scan(m)
132
- RDParser::DEBUG && debug_message("GOT #{m}")
132
+ ::RDParser::DEBUG && debug_message("GOT #{m}")
133
133
  output << {rule => m}
134
134
  else
135
135
  return false
@@ -139,7 +139,7 @@ class RDParser::RDParser
139
139
  elsif rule_data.class == Regexp
140
140
  # If we get a match.. do stuff!
141
141
  if c = @content.scan(rule_data)
142
- RDParser::DEBUG && debug_message("GOT IT --> #{c}")
142
+ ::RDParser::DEBUG && debug_message("GOT IT --> #{c}")
143
143
  output << {rule => c}
144
144
  else
145
145
  # If we get no match, go and cry to mommy^H^H^H^H^Hhead up the recursion ladder
@@ -154,11 +154,11 @@ class RDParser::RDParser
154
154
  # But did it really work out as planned?
155
155
  if response
156
156
  # Yes.. so celebrate and process the response.
157
- RDParser::DEBUG && debug_message("GOT #{rule}")
157
+ ::RDParser::DEBUG && debug_message("GOT #{rule}")
158
158
  return {rule => response}
159
159
  else
160
160
  # No.. so throw a hissyfit
161
- RDParser::DEBUG && debug_message("NOT GOT #{rule}")
161
+ ::RDParser::DEBUG && debug_message("NOT GOT #{rule}")
162
162
  return false
163
163
  end
164
164
  end
@@ -174,7 +174,7 @@ class RDParser::RDParser
174
174
 
175
175
  # A 'pretty printer' for RDParser's syntax trees (pp just doesn't cut it for these beasties)
176
176
  # There's probably a nice iterative way of doing this but I'm tired.
177
- def RDParser.text_syntax_tree(v, depth = 0, output = '')
177
+ def self.text_syntax_tree(v, depth = 0, output = '')
178
178
  if v.class == Array
179
179
  v.each { |a| output = text_syntax_tree(a, depth, output) }
180
180
  elsif v.class == Hash
@@ -189,7 +189,7 @@ class RDParser::RDParser
189
189
  # Prints a debugging message if debug mode is enabled
190
190
  def debug_message(message, priority = true)
191
191
  # Let's different types of message through
192
- return if priority != RDParser::DEBUG
192
+ return if priority != ::RDParser::DEBUG
193
193
  puts "#{(" " * @depth.to_i) if @depth && @depth > -1 } #{message}"
194
194
  end
195
195
  end
@@ -1,3 +1,3 @@
1
1
  module RDParser
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -11,6 +11,7 @@ Gem::Specification.new do |s|
11
11
  s.description = "A Simple Recursive Descending (LL) Parser for Ruby"
12
12
  s.rubyforge_project = "rdparser"
13
13
 
14
+ s.add_development_dependency "rdoc"
14
15
  s.add_development_dependency "rake"
15
16
  s.add_development_dependency "bundler"
16
17
  s.add_development_dependency "rspec"
@@ -0,0 +1,5 @@
1
+ require File.expand_path('../support/spec_helper.rb', __FILE__)
2
+
3
+ describe "RDParser::ParserError" do
4
+ it { expect{ RDParser::ParserError }.should_not raise_error }
5
+ end
@@ -0,0 +1,15 @@
1
+ require File.expand_path('../support/spec_helper.rb', __FILE__)
2
+
3
+ describe RDParser::RDParser do
4
+ context "given a very simple grammar" do
5
+ include ParserHelper
6
+ parser do |p|
7
+ p.expression '"a" expression1'
8
+ p.expression1 /^.*$/
9
+ end
10
+
11
+ it { should parse("asdf") }
12
+ it { should_not parse("qwer") }
13
+ end
14
+ end
15
+
@@ -16,8 +16,17 @@ module ParserHelper
16
16
  def description
17
17
  %{should parse "#{@_expression}" starting with :#{@_root}}
18
18
  end
19
+ def failure_message
20
+ %{Grammar should parse "#{@_expression}"!}
21
+ end
22
+ def negative_failure_message
23
+ %{Grammar should not parse "#{@_expression}"!}
24
+ end
19
25
  def matches? parser
20
26
  parser.parse(@_root, @_expression).kind_of?(Array)
27
+ return true
28
+ rescue RDParser::ParserError
29
+ return false
21
30
  end
22
31
  end
23
32
  o.instance_variable_set('@_expression', expression)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdparser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,22 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-12-17 00:00:00.000000000Z
13
+ date: 2011-12-22 00:00:00.000000000Z
14
14
  dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rdoc
17
+ requirement: &19621620 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *19621620
15
26
  - !ruby/object:Gem::Dependency
16
27
  name: rake
17
- requirement: &15362520 !ruby/object:Gem::Requirement
28
+ requirement: &19621200 !ruby/object:Gem::Requirement
18
29
  none: false
19
30
  requirements:
20
31
  - - ! '>='
@@ -22,10 +33,10 @@ dependencies:
22
33
  version: '0'
23
34
  type: :development
24
35
  prerelease: false
25
- version_requirements: *15362520
36
+ version_requirements: *19621200
26
37
  - !ruby/object:Gem::Dependency
27
38
  name: bundler
28
- requirement: &15362100 !ruby/object:Gem::Requirement
39
+ requirement: &19620780 !ruby/object:Gem::Requirement
29
40
  none: false
30
41
  requirements:
31
42
  - - ! '>='
@@ -33,10 +44,10 @@ dependencies:
33
44
  version: '0'
34
45
  type: :development
35
46
  prerelease: false
36
- version_requirements: *15362100
47
+ version_requirements: *19620780
37
48
  - !ruby/object:Gem::Dependency
38
49
  name: rspec
39
- requirement: &15361680 !ruby/object:Gem::Requirement
50
+ requirement: &19620360 !ruby/object:Gem::Requirement
40
51
  none: false
41
52
  requirements:
42
53
  - - ! '>='
@@ -44,10 +55,10 @@ dependencies:
44
55
  version: '0'
45
56
  type: :development
46
57
  prerelease: false
47
- version_requirements: *15361680
58
+ version_requirements: *19620360
48
59
  - !ruby/object:Gem::Dependency
49
60
  name: simplecov
50
- requirement: &15361260 !ruby/object:Gem::Requirement
61
+ requirement: &19619940 !ruby/object:Gem::Requirement
51
62
  none: false
52
63
  requirements:
53
64
  - - ! '>='
@@ -55,10 +66,10 @@ dependencies:
55
66
  version: '0'
56
67
  type: :development
57
68
  prerelease: false
58
- version_requirements: *15361260
69
+ version_requirements: *19619940
59
70
  - !ruby/object:Gem::Dependency
60
71
  name: spork
61
- requirement: &15360840 !ruby/object:Gem::Requirement
72
+ requirement: &19619520 !ruby/object:Gem::Requirement
62
73
  none: false
63
74
  requirements:
64
75
  - - ! '>='
@@ -66,10 +77,10 @@ dependencies:
66
77
  version: '0'
67
78
  type: :development
68
79
  prerelease: false
69
- version_requirements: *15360840
80
+ version_requirements: *19619520
70
81
  - !ruby/object:Gem::Dependency
71
82
  name: watchr
72
- requirement: &15360420 !ruby/object:Gem::Requirement
83
+ requirement: &19619100 !ruby/object:Gem::Requirement
73
84
  none: false
74
85
  requirements:
75
86
  - - ! '>='
@@ -77,7 +88,7 @@ dependencies:
77
88
  version: '0'
78
89
  type: :development
79
90
  prerelease: false
80
- version_requirements: *15360420
91
+ version_requirements: *19619100
81
92
  description: A Simple Recursive Descending (LL) Parser for Ruby
82
93
  email:
83
94
  - golda@bitandpixel.hu
@@ -91,12 +102,13 @@ files:
91
102
  - Rakefile
92
103
  - lib/patches/array.rb
93
104
  - lib/rdparser.rb
94
- - lib/rdparser/exceptions.rb
95
105
  - lib/rdparser/rdparser.rb
96
106
  - lib/rdparser/scanner.rb
97
107
  - lib/rdparser/version.rb
98
108
  - rdparser.gemspec
99
109
  - spec/examples/a4_spec.rb
110
+ - spec/exceptions_spec.rb
111
+ - spec/parser_base_spec.rb
100
112
  - spec/support/parser_helper.rb
101
113
  - spec/support/spec_helper.rb
102
114
  homepage: http://www.rubyinside.com/recursive-descent-parser-for-ruby-300.html
@@ -114,7 +126,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
114
126
  version: '0'
115
127
  segments:
116
128
  - 0
117
- hash: 2323939059109042522
129
+ hash: 534404727448102245
118
130
  required_rubygems_version: !ruby/object:Gem::Requirement
119
131
  none: false
120
132
  requirements:
@@ -123,11 +135,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
135
  version: '0'
124
136
  segments:
125
137
  - 0
126
- hash: 2323939059109042522
138
+ hash: 534404727448102245
127
139
  requirements: []
128
140
  rubyforge_project: rdparser
129
141
  rubygems_version: 1.8.10
130
142
  signing_key:
131
143
  specification_version: 3
132
- summary: rdparser-0.1.1
144
+ summary: rdparser-0.1.2
133
145
  test_files: []
@@ -1,4 +0,0 @@
1
- module RDParser
2
- # raised when Parser cannot get through the entire string.
3
- class ParserError < StandardError; end
4
- end