babel_bridge 0.5.1 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/CHANGE_LOG +165 -0
  2. data/Gemfile +4 -0
  3. data/Guardfile +7 -0
  4. data/LICENCE +24 -0
  5. data/README.md +244 -0
  6. data/Rakefile +8 -2
  7. data/TODO +100 -0
  8. data/babel_bridge.gemspec +11 -3
  9. data/examples/json/json_parser.rb +23 -0
  10. data/examples/json/json_parser2.rb +37 -0
  11. data/lib/babel_bridge.rb +3 -2
  12. data/lib/{nodes.rb → babel_bridge/nodes.rb} +0 -0
  13. data/lib/{nodes → babel_bridge/nodes}/empty_node.rb +0 -0
  14. data/lib/{nodes → babel_bridge/nodes}/node.rb +1 -1
  15. data/lib/{nodes → babel_bridge/nodes}/non_terminal_node.rb +0 -8
  16. data/lib/{nodes → babel_bridge/nodes}/root_node.rb +0 -0
  17. data/lib/{nodes → babel_bridge/nodes}/rule_node.rb +0 -0
  18. data/lib/{nodes → babel_bridge/nodes}/terminal_node.rb +0 -0
  19. data/lib/{parser.rb → babel_bridge/parser.rb} +7 -14
  20. data/lib/{pattern_element.rb → babel_bridge/pattern_element.rb} +27 -25
  21. data/lib/babel_bridge/pattern_element_hash.rb +22 -0
  22. data/lib/{rule.rb → babel_bridge/rule.rb} +0 -0
  23. data/lib/{rule_variant.rb → babel_bridge/rule_variant.rb} +0 -4
  24. data/lib/{shell.rb → babel_bridge/shell.rb} +0 -0
  25. data/lib/{string.rb → babel_bridge/string.rb} +0 -0
  26. data/lib/{tools.rb → babel_bridge/tools.rb} +0 -0
  27. data/lib/babel_bridge/version.rb +3 -0
  28. data/spec/advanced_parsers_spec.rb +1 -0
  29. data/spec/basic_parsing_spec.rb +43 -0
  30. data/spec/bb_spec.rb +19 -0
  31. data/spec/compound_patterns_spec.rb +61 -0
  32. data/spec/node_spec.rb +3 -3
  33. data/spec/pattern_generators_spec.rb +4 -4
  34. data/spec/spec_helper.rb +3 -0
  35. metadata +115 -33
  36. data/README +0 -144
  37. data/examples/turing/examples.turing +0 -33
  38. data/examples/turing/notes.rb +0 -111
  39. data/examples/turing/turing_demo.rb +0 -71
  40. data/lib/version.rb +0 -4
File without changes
File without changes
File without changes
@@ -0,0 +1,3 @@
1
+ module BabelBridge
2
+ VERSION = "0.5.3"
3
+ end
@@ -21,6 +21,7 @@ describe "advanced parsing" do
21
21
  end
22
22
 
23
23
  test_parse("END this is in the middle END")
24
+ test_parse("FROG this is in the middle FROG")
24
25
  test_parse("END this is in END the middle END",:partial_match => true).text.should == "END this is in END"
25
26
  test_parse "END this is in the middle EN", :should_fail_at => 0
26
27
  test_parse " END this is in the middle END", :should_fail_at => 0
@@ -91,4 +91,47 @@ describe "basic parsing" do
91
91
  test_parse "boO"
92
92
  end
93
93
 
94
+ it "any() pattern should work" do
95
+ new_parser do
96
+ rule :foo, any("foo","bar")
97
+ end
98
+
99
+ test_parse "bar"
100
+ test_parse "foo"
101
+ test_parse "boo", :should_fail_at => 0
102
+ end
103
+
104
+ it "any!() should work" do
105
+ new_parser do
106
+ rule :foo, any!("foo","bar"), /[a-z]+/
107
+ end
108
+
109
+ test_parse "bar", :should_fail_at => 0
110
+ test_parse "foo", :should_fail_at => 0
111
+ test_parse "boo"
112
+ end
113
+
114
+ it "any?() should work" do
115
+ new_parser do
116
+ rule :foo, any?("foo","bar"), "baz"
117
+ end
118
+
119
+ test_parse "barbaz"
120
+ test_parse "foobaz"
121
+ test_parse "baz"
122
+ test_parse "foo", :should_fail_at => 3
123
+ end
124
+
125
+ it "[] (match in order) pattern should work" do
126
+ new_parser do
127
+ rule :foo, ["foo", "bar"], "baz"
128
+ rule :foo, "foo", "boo"
129
+ end
130
+
131
+ test_parse "foobarbaz"
132
+ test_parse "foo", :should_fail_at => 3
133
+ test_parse "foobar", :should_fail_at => 6
134
+ test_parse "fooboo"
135
+ end
136
+
94
137
  end
data/spec/bb_spec.rb CHANGED
@@ -52,4 +52,23 @@ describe BabelBridge do
52
52
  test_parse("barbar").bar.class.should == BabelBridge::MultiMatchesArray
53
53
  end
54
54
 
55
+ it "if a name can be optionally matched more than one time, it should always be an array of matchs" do
56
+ new_parser do
57
+ rule :file, :space, :constant
58
+ rule :space, /\s*/
59
+ rule :constant, /[A-Z][0-9_a-zA-Z]*/
60
+ end
61
+
62
+ p = test_parse("This")
63
+
64
+ p.constant.match_length.should == 4
65
+ p.space.match_length.should == 0
66
+
67
+ p.constant.match_range.should == (0..3)
68
+ p.space.match_range.should == (0..-1)
69
+
70
+ p.constant.to_s.should == "This"
71
+ p.space.to_s.should == ""
72
+ end
73
+
55
74
  end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ describe "compound pattern parsing" do
4
+ include TestParserGenerator
5
+
6
+ it "many + []" do
7
+ new_parser do
8
+ rule :foo, many(["bar","baz"])
9
+ end
10
+
11
+ test_parse("barbaz").matches.length.should == 2
12
+ test_parse("barbazbarbaz").matches.length.should == 4
13
+ test_parse "barbazbar", :should_fail_at => 9
14
+ end
15
+
16
+ it "many + any" do
17
+ new_parser do
18
+ rule :foo, many(any("foo","bar"))
19
+ end
20
+
21
+ test_parse "foo"
22
+ test_parse "foofoo"
23
+ test_parse "bar"
24
+ test_parse "barfoo"
25
+ test_parse "barfoofoofoobarbarbarbafoo", :should_fail_at => 21
26
+ end
27
+
28
+ it "any + []" do
29
+ new_parser do
30
+ rule :foo, any(["foo","bar"],"baz")
31
+ end
32
+
33
+ test_parse "foobar"
34
+ test_parse "foo", :should_fail_at => 3
35
+ test_parse "bar", :should_fail_at => 0
36
+ test_parse "baz"
37
+ end
38
+
39
+ it "[] + any" do
40
+ new_parser do
41
+ rule :foo, [any("foo", "bar"), "baz"], "boo"
42
+ end
43
+
44
+ test_parse "foobazboo"
45
+ test_parse "barbazboo"
46
+ test_parse "barbaz", :should_fail_at => 6
47
+ test_parse "fooboo", :should_fail_at => 3
48
+ end
49
+
50
+ it "many + [] + any" do
51
+ new_parser do
52
+ rule :foo, many(any(["foo",/[0-9]+/],"bar"))
53
+ end
54
+
55
+ test_parse "foo", :should_fail_at => 3
56
+ test_parse "foo123"
57
+ test_parse "bar123", :should_fail_at => 3
58
+ test_parse "barfoo123"
59
+ test_parse "barfoo123barbarbarfoo9foo0foo1barfoo8"
60
+ end
61
+ end
data/spec/node_spec.rb CHANGED
@@ -28,7 +28,7 @@ describe "basic parsing" do
28
28
  test_parse("123").number.should == 123
29
29
  end
30
30
 
31
- def test_adder
31
+ it "test_adder" do
32
32
  new_parser do
33
33
  rule :adder, :number, "+", :number do
34
34
  def answer;
@@ -41,10 +41,10 @@ describe "basic parsing" do
41
41
  end
42
42
 
43
43
 
44
- test_parser("123+654").answer.should == 777
44
+ test_parse("123+654").answer.should == 777
45
45
  end
46
46
 
47
- def test_adder_multiplier
47
+ it "test_adder_multiplier" do
48
48
  new_parser do
49
49
 
50
50
  rule :adder, :multiplier, "+", :adder do
@@ -11,10 +11,6 @@ describe "basic parsing" do
11
11
  BabelBridge::Parser.many?(";").hash.should == {:many=>";", :optionally=>true, :match=>true}
12
12
  end
13
13
 
14
- it "test_many!" do
15
- BabelBridge::Parser.many!(";").hash.should == {:many=>";", :dont=>true, :match=>true}
16
- end
17
-
18
14
  it "test_match" do
19
15
  BabelBridge::Parser.match(";").hash.should == {:match=>";"}
20
16
  end
@@ -38,4 +34,8 @@ describe "basic parsing" do
38
34
  it "test_could" do
39
35
  BabelBridge::Parser.could.match(";").hash.should == {:match=>";",:could=>true}
40
36
  end
37
+
38
+ it "test_any" do
39
+ BabelBridge::Parser.any(";").hash.should == {:any=>[";"]}
40
+ end
41
41
  end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
1
4
  require File.join(File.dirname(__FILE__),"..","lib","babel_bridge")
2
5
 
3
6
  module TestParserGenerator
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: babel_bridge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -43,6 +43,70 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: simplecov
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: guard-rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: guard-test
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rb-fsevent
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
46
110
  description: ! 'Babel Bridge is an object oriented parser generator for parsing expression
47
111
  grammars (PEG).
48
112
 
@@ -54,41 +118,19 @@ executables: []
54
118
  extensions: []
55
119
  extra_rdoc_files: []
56
120
  files:
57
- - README
121
+ - CHANGE_LOG
122
+ - Gemfile
123
+ - Guardfile
124
+ - LICENCE
125
+ - README.md
58
126
  - Rakefile
127
+ - TODO
59
128
  - babel_bridge.gemspec
60
- - spec/advanced_parsers_spec.rb
61
- - spec/basic_parsing_spec.rb
62
- - spec/bb_spec.rb
63
- - spec/ignore_whitespace_spec.rb
64
- - spec/inspect_spec.rb
65
- - spec/many_spec.rb
66
- - spec/node_spec.rb
67
- - spec/pattern_generators_spec.rb
68
- - spec/rule_parsing_spec.rb
69
- - spec/spec_helper.rb
70
- - spec/tools_spec.rb
71
- - lib/babel_bridge.rb
72
- - lib/nodes/empty_node.rb
73
- - lib/nodes/node.rb
74
- - lib/nodes/non_terminal_node.rb
75
- - lib/nodes/root_node.rb
76
- - lib/nodes/rule_node.rb
77
- - lib/nodes/terminal_node.rb
78
- - lib/nodes.rb
79
- - lib/parser.rb
80
- - lib/pattern_element.rb
81
- - lib/rule.rb
82
- - lib/rule_variant.rb
83
- - lib/shell.rb
84
- - lib/string.rb
85
- - lib/tools.rb
86
- - lib/version.rb
87
129
  - examples/indention_grouping.rb
88
130
  - examples/indention_grouping_test.txt
131
+ - examples/json/json_parser.rb
132
+ - examples/json/json_parser2.rb
89
133
  - examples/test.rb
90
- - examples/turing/examples.turing
91
- - examples/turing/notes.rb
92
134
  - examples/turing/turing++.rb
93
135
  - examples/turing/turing.rb
94
136
  - examples/turing/turing00.rb
@@ -104,7 +146,35 @@ files:
104
146
  - examples/turing/turing10.rb
105
147
  - examples/turing/turing11.rb
106
148
  - examples/turing/turing12.rb
107
- - examples/turing/turing_demo.rb
149
+ - lib/babel_bridge.rb
150
+ - lib/babel_bridge/nodes.rb
151
+ - lib/babel_bridge/nodes/empty_node.rb
152
+ - lib/babel_bridge/nodes/node.rb
153
+ - lib/babel_bridge/nodes/non_terminal_node.rb
154
+ - lib/babel_bridge/nodes/root_node.rb
155
+ - lib/babel_bridge/nodes/rule_node.rb
156
+ - lib/babel_bridge/nodes/terminal_node.rb
157
+ - lib/babel_bridge/parser.rb
158
+ - lib/babel_bridge/pattern_element.rb
159
+ - lib/babel_bridge/pattern_element_hash.rb
160
+ - lib/babel_bridge/rule.rb
161
+ - lib/babel_bridge/rule_variant.rb
162
+ - lib/babel_bridge/shell.rb
163
+ - lib/babel_bridge/string.rb
164
+ - lib/babel_bridge/tools.rb
165
+ - lib/babel_bridge/version.rb
166
+ - spec/advanced_parsers_spec.rb
167
+ - spec/basic_parsing_spec.rb
168
+ - spec/bb_spec.rb
169
+ - spec/compound_patterns_spec.rb
170
+ - spec/ignore_whitespace_spec.rb
171
+ - spec/inspect_spec.rb
172
+ - spec/many_spec.rb
173
+ - spec/node_spec.rb
174
+ - spec/pattern_generators_spec.rb
175
+ - spec/rule_parsing_spec.rb
176
+ - spec/spec_helper.rb
177
+ - spec/tools_spec.rb
108
178
  homepage: http://babel-bridge.rubyforge.org
109
179
  licenses: []
110
180
  post_install_message:
@@ -129,4 +199,16 @@ rubygems_version: 1.8.24
129
199
  signing_key:
130
200
  specification_version: 3
131
201
  summary: A Ruby-based parser-generator based on Parsing Expression Grammars.
132
- test_files: []
202
+ test_files:
203
+ - spec/advanced_parsers_spec.rb
204
+ - spec/basic_parsing_spec.rb
205
+ - spec/bb_spec.rb
206
+ - spec/compound_patterns_spec.rb
207
+ - spec/ignore_whitespace_spec.rb
208
+ - spec/inspect_spec.rb
209
+ - spec/many_spec.rb
210
+ - spec/node_spec.rb
211
+ - spec/pattern_generators_spec.rb
212
+ - spec/rule_parsing_spec.rb
213
+ - spec/spec_helper.rb
214
+ - spec/tools_spec.rb
data/README DELETED
@@ -1,144 +0,0 @@
1
- Summary
2
- -------
3
-
4
- Babel Bridge let's you generate parsers 100% in Ruby code. It is a memoizing Parsing Expression Grammar (PEG) generator like Treetop, but it doesn't require special file-types or new syntax. Overall focus is on simplicity and usability over performance.
5
-
6
- Example
7
- -------
8
-
9
- require "babel_bridge"
10
- class MyParser < BabelBridge::Parser
11
- rule :foo, "foo", :bar? # match "foo" optionally followed by the :bar
12
- rule :bar, "bar" # match "bar"
13
- end
14
-
15
- MyParser.new.parse("foo") # matches "foo"
16
- MyParser.new.parse("foobar") # matches "foobar"
17
-
18
- Babel Bridge is a parser-generator for Parsing Expression Grammars
19
-
20
- Goals
21
- -----
22
-
23
- Allow expression 100% in ruby
24
- Productivity through Simplicity and Understandability first
25
- Performance second
26
-
27
- Features
28
- --------
29
-
30
- rule=MyParser[:foo] # returns the BabelBridge::Rule instance for that rule
31
-
32
- rule.to_s
33
- nice human-readable view of the rule with extra info
34
-
35
- rule.inspect
36
- returns the code necessary for generating the rule and all its variants
37
- (minus any class_eval code)
38
-
39
- MyParser.node_class(rule)
40
- returns the Node class for a rule
41
-
42
- MyParser.node_class(rule) do
43
- # class_eval inside the rule's Node-class
44
- end
45
-
46
- MyParser.new.parse(text)
47
- # parses Text starting with the MyParser.root_rule
48
- # The root_rule is defined automatically by the first rule defined, but can be set by:
49
- # MyParser.root_rule=v # where v is the symbol name of the rule or the actual rule object from MyParser[rule]
50
- MyParser.new.parse(text,offset,rule) # only has to match the rule - it's ok if there is input left
51
- parser.parse uses the root_rule
52
-
53
- detailed parser_failure_info report
54
-
55
- Defining Rules
56
- --------------
57
-
58
- Inside the parser class, a rule is defined as follows:
59
-
60
- class MyParser < BabelBridge::Parser
61
- rule :rule_name, pattern
62
- end
63
-
64
- Where:
65
-
66
- :rule_name is a symbol
67
- pattern see Patterns below
68
-
69
- You can also add new rules outside the class definition by:
70
-
71
- MyParser.rule :rule_name, pattern
72
-
73
- Patterns
74
- --------
75
-
76
- Patterns are an Array of pattern elements, matched in order:
77
-
78
- Ex (both are equivelent):
79
- rule :my_rule, "match", "this", "in", "order" # matches "matchthisinorder"
80
- rule :my_rule, ["match", "this", "in", "order"] # matches "matchthisinorder"
81
-
82
- Pattern Elements
83
- ----------------
84
-
85
- Pattern elements are basic-pattern-element or extended-pattern-element ( expressed as a hash). Internally, they are "compiled" into instances of PatternElement with optimized lambda functions for parsing.
86
-
87
- basic-pattern-element:
88
- :my_rule matches the Rule named :my_rule
89
- :my_rule? optional: optionally matches Rule :my_rule
90
- :my_rule! negative: success only if it DOESN'T match Rule :my_rule
91
- "string" matches the string exactly
92
- /regex/ matches the regex exactly
93
- true always matches the empty string (useful as a no-op if you don't want to change the length of your pattern)
94
-
95
- extended-pattern-element:
96
-
97
- A Hash with :match or :parser set and zero or more additional options:
98
-
99
- :match => basic_element
100
- provide one of the basic elements above
101
- NOTE: Optional and Negative options are preserved, but they are overridden by any such directives in the Hash-Element
102
-
103
- :parser => lambda {|parent_node| ... }
104
- Custom lambda function for parsing the input.
105
- Return "nil" if could not find a parse, otherwise return a new Node, typically the TerminalNode
106
- Make sure the returned node.next value is the index where you wish parsing to resume
107
-
108
- :as => :my_name
109
- Assign a name to an element for later programatic reference:
110
- rule_variant_node_class_instance.my_name
111
-
112
- :optionally => true
113
- PEG equivelent: term?
114
- turn this into an optional-match element
115
- optional elements cannot be negative
116
-
117
- :dont => true
118
- PEG equivalent: !term
119
- turn this into a Negative-match element
120
- negative elements cannot be optional
121
-
122
- :could => true
123
- PEG equivalent: &term
124
-
125
- :many => PatternElement
126
- PEG equivalent: term+ (for "term*", use optionally + many)
127
- accept 1 or more reptitions of this element delimited by PatternElement
128
- NOTE: PatternElement can be "true" for no delimiter (since "true" matches the empty string)
129
-
130
- :delimiter => PatternElement
131
- pattern to match between the :many patterns
132
-
133
- :post_delimiter => true # use the :delimiter PatternElement for final match
134
- :post_delimiter => PatternElement # use custom post_delimiter PatternElement for final match
135
- if true, then poly will match a delimiter after the last poly-match
136
-
137
- Structure
138
- ---------
139
-
140
- Each Rule defines a subclass of Node
141
- Each RuleVariant defines a subclass of the parent Rule's node-class
142
-
143
- Therefor you can easily define code to be shared across all variants as well
144
- as define code specific to one variant.