kpeg 0.8.4 → 0.8.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -170,12 +170,16 @@ module KPeg
170
170
  end
171
171
 
172
172
  def parse(rule=nil)
173
+ # We invoke the rules indirectly via apply
174
+ # instead of by just calling them as methods because
175
+ # if the rules use left recursion, apply needs to
176
+ # manage that.
177
+
173
178
  if !rule
174
- _root ? true : false
179
+ apply(:_root)
175
180
  else
176
- # This is not shared with code_generator.rb so this can be standalone
177
181
  method = rule.gsub("-","_hyphen_")
178
- __send__("_#{method}") ? true : false
182
+ apply :"_#{method}"
179
183
  end
180
184
  end
181
185
 
@@ -1,3 +1,3 @@
1
1
  module KPeg
2
- VERSION = "0.8.4"
2
+ VERSION = "0.8.5"
3
3
  end
@@ -0,0 +1,50 @@
1
+ require 'test/unit'
2
+ require 'kpeg'
3
+ require 'kpeg/format_parser'
4
+ require 'kpeg/code_generator'
5
+ require 'stringio'
6
+
7
+ class TestKPegLeftRecursion < Test::Unit::TestCase
8
+ GRAMMAR = <<-'STR'
9
+
10
+ name = name:n "[]" { [:array, n] }
11
+ | < /\w+/ > { [:word, text] }
12
+
13
+ root = name
14
+
15
+ STR
16
+
17
+ def test_invoke_rule_directly
18
+ parc = KPeg::FormatParser.new(GRAMMAR)
19
+ assert parc.parse, "Unable to parse"
20
+
21
+ gram = parc.grammar
22
+
23
+ # gr = KPeg::GrammarRenderer.new(gram)
24
+ # puts
25
+ # gr.render(STDOUT)
26
+
27
+ cg = KPeg::CodeGenerator.new "TestCalc", gram
28
+
29
+ code = cg.make("blah[]")
30
+ assert_equal true, code.parse("name")
31
+ assert_equal [:array, [:word, "blah"]], code.result
32
+ end
33
+
34
+ def test_invoke_rule_via_another
35
+ parc = KPeg::FormatParser.new(GRAMMAR)
36
+ assert parc.parse, "Unable to parse"
37
+
38
+ gram = parc.grammar
39
+
40
+ # gr = KPeg::GrammarRenderer.new(gram)
41
+ # puts
42
+ # gr.render(STDOUT)
43
+
44
+ cg = KPeg::CodeGenerator.new "TestCalc", gram
45
+
46
+ code = cg.make("blah[]")
47
+ assert_equal true, code.parse("root")
48
+ assert_equal [:array, [:word, "blah"]], code.result
49
+ end
50
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kpeg
3
3
  version: !ruby/object:Gem::Version
4
- hash: 55
4
+ hash: 53
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 8
9
- - 4
10
- version: 0.8.4
9
+ - 5
10
+ version: 0.8.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Evan Phoenix
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-01 00:00:00 Z
18
+ date: 2012-01-08 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rake
@@ -66,6 +66,7 @@ files:
66
66
  - test/test_kpeg_compiled_parser.rb
67
67
  - test/test_kpeg_format.rb
68
68
  - test/test_kpeg_grammar_renderer.rb
69
+ - test/test_left_recursion.rb
69
70
  homepage: https://github.com/evanphx/kpeg
70
71
  licenses: []
71
72
 
@@ -95,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
96
  requirements: []
96
97
 
97
98
  rubyforge_project:
98
- rubygems_version: 1.8.10
99
+ rubygems_version: 1.8.15
99
100
  signing_key:
100
101
  specification_version: 3
101
102
  summary: Peg-based Code Generator
@@ -107,3 +108,4 @@ test_files:
107
108
  - test/test_kpeg_compiled_parser.rb
108
109
  - test/test_kpeg_format.rb
109
110
  - test/test_kpeg_grammar_renderer.rb
111
+ - test/test_left_recursion.rb