rdparser 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rdparser.rb +4 -4
- data/lib/rdparser/parse_error.rb +9 -0
- data/lib/rdparser/parse_matcher.rb +56 -0
- data/lib/rdparser/rdparser.rb +1 -1
- data/lib/rdparser/version.rb +1 -1
- data/spec/examples/a4_spec.rb +0 -2
- data/spec/exceptions_spec.rb +2 -2
- data/spec/parser_base_spec.rb +9 -3
- data/spec/support/spec_helper.rb +1 -0
- metadata +21 -20
- data/spec/support/parser_helper.rb +0 -37
data/lib/rdparser.rb
CHANGED
@@ -4,11 +4,11 @@ require File.expand_path('../patches/array.rb', __FILE__)
|
|
4
4
|
# A Simple Recursive Descending (LL) Parser for Ruby
|
5
5
|
module RDParser
|
6
6
|
|
7
|
-
|
8
|
-
|
7
|
+
autoload :ParseError, File.expand_path('../rdparser/parse_error.rb', __FILE__)
|
8
|
+
autoload :RDParser, File.expand_path('../rdparser/rdparser.rb', __FILE__)
|
9
|
+
autoload :Scanner, File.expand_path('../rdparser/scanner.rb', __FILE__)
|
9
10
|
|
10
|
-
autoload :
|
11
|
-
autoload :Scanner, File.expand_path('../rdparser/scanner.rb', __FILE__)
|
11
|
+
autoload :ParseMatcher, File.expand_path('../rdparser/parse_matcher.rb', __FILE__)
|
12
12
|
|
13
13
|
# change to true to get flooded with debug messages during parsing
|
14
14
|
DEBUG = false
|
@@ -0,0 +1,9 @@
|
|
1
|
+
# raised when Parser cannot get through the entire string.
|
2
|
+
class RDParser::ParseError < StandardError
|
3
|
+
attr_reader :content, :position
|
4
|
+
def initialize content, position
|
5
|
+
super(%{Cannot parse the entire content! (error was on position ##{position}: "#{content[0...position]}|#{content[position..position]}|#{content[position+1..-1]}")})
|
6
|
+
@content = content
|
7
|
+
@position = position
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module RDParser::ParseMatcher
|
2
|
+
class Parse
|
3
|
+
def initialize expression, root
|
4
|
+
@expression = expression
|
5
|
+
@root = root
|
6
|
+
end
|
7
|
+
|
8
|
+
def matches? parser
|
9
|
+
parser.parse(@root, @expression).kind_of?(Array)
|
10
|
+
return true
|
11
|
+
rescue RDParser::ParseError
|
12
|
+
@error_on_position = $!.position
|
13
|
+
@error_in_content = $!.content
|
14
|
+
return false
|
15
|
+
end
|
16
|
+
|
17
|
+
def failure_message
|
18
|
+
%{Grammar should parse "#{@expression}" starting with :#{@root} but error was catched on position #{@error_on_position}! "#{@expression[0...@error_on_position]}|#{@expression[@error_on_position..@error_on_position]}|#{@expression[@error_on_position+1..-1]}"}
|
19
|
+
end
|
20
|
+
|
21
|
+
def negative_failure_message
|
22
|
+
%{Grammar should not parse "#{@expression}" starting with :#{@root}} + (@expected_error_on_position && %{ and first error should appear on position #{@expected_error_on_position}} || '') + %{!}
|
23
|
+
end
|
24
|
+
|
25
|
+
def description
|
26
|
+
%{parse "#{@expression}" starting with :#{@root}} + (@expected_error_on_position && %{ and with first error on position #{@expected_error_on_position.to_s}} || '')
|
27
|
+
end
|
28
|
+
|
29
|
+
def with position
|
30
|
+
@expected_error_on_position = position
|
31
|
+
self
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.included base
|
36
|
+
base.extend const_get("ClassMethods") if const_defined?("ClassMethods")
|
37
|
+
base.send :include, const_get("InstanceMethods") if const_defined?("InstanceMethods")
|
38
|
+
end
|
39
|
+
|
40
|
+
module ClassMethods
|
41
|
+
def parser &block
|
42
|
+
subject { RDParser::RDParser.new(&block) }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
module InstanceMethods
|
47
|
+
def parse expression, root=:expression
|
48
|
+
RDParser::ParseMatcher::Parse.new expression, root
|
49
|
+
end
|
50
|
+
|
51
|
+
def error_on_position position
|
52
|
+
position
|
53
|
+
end
|
54
|
+
alias :first_error_on_position :error_on_position
|
55
|
+
end
|
56
|
+
end
|
data/lib/rdparser/rdparser.rb
CHANGED
@@ -20,7 +20,7 @@ class RDParser::RDParser
|
|
20
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
|
23
|
+
raise RDParser::ParseError.new(content, @content.position) unless options[:partial] == true or @content.eos?
|
24
24
|
|
25
25
|
[results].flatten
|
26
26
|
end
|
data/lib/rdparser/version.rb
CHANGED
data/spec/examples/a4_spec.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
require File.expand_path('../../support/spec_helper', __FILE__)
|
2
2
|
|
3
3
|
describe 'A4::Authorization::Parser' do
|
4
|
-
include ParserHelper
|
5
|
-
|
6
4
|
parser do |p|
|
7
5
|
p.expression 'expression_u binary expression | expression_u'
|
8
6
|
p.expression_u 'unary expression_u | "(" expression ")" | role'
|
data/spec/exceptions_spec.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require File.expand_path('../support/spec_helper.rb', __FILE__)
|
2
2
|
|
3
|
-
describe "RDParser::
|
4
|
-
it { expect{ RDParser::
|
3
|
+
describe "RDParser::ParseError" do
|
4
|
+
it { expect{ RDParser::ParseError }.should_not raise_error }
|
5
5
|
end
|
data/spec/parser_base_spec.rb
CHANGED
@@ -1,15 +1,21 @@
|
|
1
1
|
require File.expand_path('../support/spec_helper.rb', __FILE__)
|
2
2
|
|
3
|
-
describe RDParser::RDParser do
|
3
|
+
describe RDParser::RDParser, :focused => true do
|
4
4
|
context "given a very simple grammar" do
|
5
|
-
include ParserHelper
|
6
5
|
parser do |p|
|
7
6
|
p.expression '"a" expression1'
|
8
|
-
p.expression1
|
7
|
+
p.expression1 '"s" expression2'
|
8
|
+
p.expression2 /^.*$/
|
9
9
|
end
|
10
10
|
|
11
|
+
# TODO test failed messages somehow -- cucumber?
|
12
|
+
#it { should_not parse("asdf").with error_on_position(1) }
|
13
|
+
#it { should_not parse("asdf") }
|
14
|
+
#it { should parse("qwer") }
|
15
|
+
|
11
16
|
it { should parse("asdf") }
|
12
17
|
it { should_not parse("qwer") }
|
18
|
+
it { should_not parse("awer").with error_on_position(1) }
|
13
19
|
end
|
14
20
|
end
|
15
21
|
|
data/spec/support/spec_helper.rb
CHANGED
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.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2012-01-27 00:00:00.000000000Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rdoc
|
17
|
-
requirement: &
|
17
|
+
requirement: &28082060 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: '0'
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *28082060
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: rake
|
28
|
-
requirement: &
|
28
|
+
requirement: &28081640 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ! '>='
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *28081640
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: bundler
|
39
|
-
requirement: &
|
39
|
+
requirement: &28081220 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ! '>='
|
@@ -44,10 +44,10 @@ dependencies:
|
|
44
44
|
version: '0'
|
45
45
|
type: :development
|
46
46
|
prerelease: false
|
47
|
-
version_requirements: *
|
47
|
+
version_requirements: *28081220
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: rspec
|
50
|
-
requirement: &
|
50
|
+
requirement: &28080800 !ruby/object:Gem::Requirement
|
51
51
|
none: false
|
52
52
|
requirements:
|
53
53
|
- - ! '>='
|
@@ -55,10 +55,10 @@ dependencies:
|
|
55
55
|
version: '0'
|
56
56
|
type: :development
|
57
57
|
prerelease: false
|
58
|
-
version_requirements: *
|
58
|
+
version_requirements: *28080800
|
59
59
|
- !ruby/object:Gem::Dependency
|
60
60
|
name: simplecov
|
61
|
-
requirement: &
|
61
|
+
requirement: &28080380 !ruby/object:Gem::Requirement
|
62
62
|
none: false
|
63
63
|
requirements:
|
64
64
|
- - ! '>='
|
@@ -66,10 +66,10 @@ dependencies:
|
|
66
66
|
version: '0'
|
67
67
|
type: :development
|
68
68
|
prerelease: false
|
69
|
-
version_requirements: *
|
69
|
+
version_requirements: *28080380
|
70
70
|
- !ruby/object:Gem::Dependency
|
71
71
|
name: spork
|
72
|
-
requirement: &
|
72
|
+
requirement: &28079960 !ruby/object:Gem::Requirement
|
73
73
|
none: false
|
74
74
|
requirements:
|
75
75
|
- - ! '>='
|
@@ -77,10 +77,10 @@ dependencies:
|
|
77
77
|
version: '0'
|
78
78
|
type: :development
|
79
79
|
prerelease: false
|
80
|
-
version_requirements: *
|
80
|
+
version_requirements: *28079960
|
81
81
|
- !ruby/object:Gem::Dependency
|
82
82
|
name: watchr
|
83
|
-
requirement: &
|
83
|
+
requirement: &28079540 !ruby/object:Gem::Requirement
|
84
84
|
none: false
|
85
85
|
requirements:
|
86
86
|
- - ! '>='
|
@@ -88,7 +88,7 @@ dependencies:
|
|
88
88
|
version: '0'
|
89
89
|
type: :development
|
90
90
|
prerelease: false
|
91
|
-
version_requirements: *
|
91
|
+
version_requirements: *28079540
|
92
92
|
description: A Simple Recursive Descending (LL) Parser for Ruby
|
93
93
|
email:
|
94
94
|
- golda@bitandpixel.hu
|
@@ -102,6 +102,8 @@ files:
|
|
102
102
|
- Rakefile
|
103
103
|
- lib/patches/array.rb
|
104
104
|
- lib/rdparser.rb
|
105
|
+
- lib/rdparser/parse_error.rb
|
106
|
+
- lib/rdparser/parse_matcher.rb
|
105
107
|
- lib/rdparser/rdparser.rb
|
106
108
|
- lib/rdparser/scanner.rb
|
107
109
|
- lib/rdparser/version.rb
|
@@ -109,7 +111,6 @@ files:
|
|
109
111
|
- spec/examples/a4_spec.rb
|
110
112
|
- spec/exceptions_spec.rb
|
111
113
|
- spec/parser_base_spec.rb
|
112
|
-
- spec/support/parser_helper.rb
|
113
114
|
- spec/support/spec_helper.rb
|
114
115
|
homepage: http://www.rubyinside.com/recursive-descent-parser-for-ruby-300.html
|
115
116
|
licenses: []
|
@@ -126,7 +127,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
126
127
|
version: '0'
|
127
128
|
segments:
|
128
129
|
- 0
|
129
|
-
hash:
|
130
|
+
hash: -288882667666033840
|
130
131
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
132
|
none: false
|
132
133
|
requirements:
|
@@ -135,11 +136,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
136
|
version: '0'
|
136
137
|
segments:
|
137
138
|
- 0
|
138
|
-
hash:
|
139
|
+
hash: -288882667666033840
|
139
140
|
requirements: []
|
140
141
|
rubyforge_project: rdparser
|
141
142
|
rubygems_version: 1.8.10
|
142
143
|
signing_key:
|
143
144
|
specification_version: 3
|
144
|
-
summary: rdparser-0.
|
145
|
+
summary: rdparser-0.2.0
|
145
146
|
test_files: []
|
@@ -1,37 +0,0 @@
|
|
1
|
-
module ParserHelper
|
2
|
-
def self.included base
|
3
|
-
base.extend const_get("ClassMethods") if const_defined?("ClassMethods")
|
4
|
-
base.send :include, const_get("InstanceMethods") if const_defined?("InstanceMethods")
|
5
|
-
end
|
6
|
-
|
7
|
-
module ClassMethods
|
8
|
-
def parser &block
|
9
|
-
subject { RDParser::RDParser.new(&block) }
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
module InstanceMethods
|
14
|
-
def parse expression, root=:expression
|
15
|
-
class << (o=Object.new)
|
16
|
-
def description
|
17
|
-
%{should parse "#{@_expression}" starting with :#{@_root}}
|
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
|
25
|
-
def matches? parser
|
26
|
-
parser.parse(@_root, @_expression).kind_of?(Array)
|
27
|
-
return true
|
28
|
-
rescue RDParser::ParserError
|
29
|
-
return false
|
30
|
-
end
|
31
|
-
end
|
32
|
-
o.instance_variable_set('@_expression', expression)
|
33
|
-
o.instance_variable_set('@_root', root)
|
34
|
-
o
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|