kpeg 0.9.0 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +15 -1
- data/Manifest.txt +2 -4
- data/README.rdoc +32 -0
- data/Rakefile +10 -3
- data/lib/hoe/kpeg.rb +6 -5
- data/lib/kpeg.rb +1 -1
- data/lib/kpeg/code_generator.rb +72 -39
- data/lib/kpeg/compiled_parser.rb +15 -11
- data/lib/kpeg/format_parser.kpeg +21 -9
- data/lib/kpeg/format_parser.rb +42 -39
- data/lib/kpeg/grammar.rb +1 -1
- data/lib/kpeg/grammar_renderer.rb +14 -0
- data/lib/kpeg/position.rb +1 -0
- data/lib/kpeg/string_escape.rb +355 -2
- data/test/test_kpeg_code_generator.rb +166 -0
- data/test/test_kpeg_format.rb +2 -2
- data/test/{test_file_parser_roundtrip.rb → test_kpeg_format_parser_round_trip.rb} +1 -1
- data/test/{test_gen_calc.rb → test_kpeg_grammar.rb} +48 -5
- data/test/test_kpeg_grammar_renderer.rb +46 -5
- metadata +17 -20
- data/Gemfile +0 -12
- data/test/test_left_recursion.rb +0 -50
data/test/test_kpeg_format.rb
CHANGED
@@ -58,7 +58,7 @@ a = c
|
|
58
58
|
b(p) = x
|
59
59
|
STR
|
60
60
|
|
61
|
-
|
61
|
+
assert match(str) # HACK what is this testing?
|
62
62
|
end
|
63
63
|
|
64
64
|
def test_assign_with_multiple_args
|
@@ -289,7 +289,7 @@ Value = NUMBER:i { i }
|
|
289
289
|
| ID:i !ASSIGN { vars[i] }
|
290
290
|
| OPEN Expr:i CLOSE { i }
|
291
291
|
STR
|
292
|
-
|
292
|
+
assert match(str) # HACK what is this testing?
|
293
293
|
end
|
294
294
|
|
295
295
|
def test_seq
|
@@ -5,7 +5,7 @@ require 'kpeg/grammar_renderer'
|
|
5
5
|
require 'kpeg/code_generator'
|
6
6
|
require 'stringio'
|
7
7
|
|
8
|
-
class
|
8
|
+
class TestKPegFormatParserRoundtrip < MiniTest::Unit::TestCase
|
9
9
|
PATH = File.expand_path("../../lib/kpeg/format_parser.kpeg", __FILE__)
|
10
10
|
def test_roundtrip
|
11
11
|
data = File.read(PATH)
|
@@ -4,8 +4,52 @@ require 'kpeg/format_parser'
|
|
4
4
|
require 'kpeg/code_generator'
|
5
5
|
require 'stringio'
|
6
6
|
|
7
|
-
class
|
8
|
-
|
7
|
+
class TestKpegGrammar < MiniTest::Unit::TestCase
|
8
|
+
LEFT_RECURSION = <<-'STR'
|
9
|
+
|
10
|
+
name = name:n "[]" { [:array, n] }
|
11
|
+
| < /\w+/ > { [:word, text] }
|
12
|
+
|
13
|
+
root = name
|
14
|
+
|
15
|
+
STR
|
16
|
+
|
17
|
+
def test_left_recursion_invoke_rule_directly
|
18
|
+
parc = KPeg::FormatParser.new(LEFT_RECURSION)
|
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_left_recursion_invoke_rule_via_another
|
35
|
+
parc = KPeg::FormatParser.new(LEFT_RECURSION)
|
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
|
+
|
51
|
+
def test_gen_calc
|
52
|
+
parc = KPeg::FormatParser.new(<<-'STR')
|
9
53
|
Stmt = - Expr:e EOL { @answers << e }
|
10
54
|
| ( !EOL . )* EOL { puts "error" }
|
11
55
|
|
@@ -41,9 +85,6 @@ EOL = ('\n' | '\r\n' | '\r' | ';') -
|
|
41
85
|
|
42
86
|
root = Stmt+
|
43
87
|
STR
|
44
|
-
|
45
|
-
def test_parse
|
46
|
-
parc = KPeg::FormatParser.new(GRAMMAR)
|
47
88
|
assert parc.parse, "Unable to parse"
|
48
89
|
|
49
90
|
gram = parc.grammar
|
@@ -60,4 +101,6 @@ root = Stmt+
|
|
60
101
|
assert_equal true, code.parse
|
61
102
|
assert_equal [7,56,119], code.instance_variable_get(:@answers)
|
62
103
|
end
|
104
|
+
|
63
105
|
end
|
106
|
+
|
@@ -115,8 +115,6 @@ class TestKPegGrammarRenderer < MiniTest::Unit::TestCase
|
|
115
115
|
g.root = g.term
|
116
116
|
end
|
117
117
|
|
118
|
-
m = KPeg.match "4*3-8/9", gram
|
119
|
-
|
120
118
|
io = StringIO.new
|
121
119
|
gr = KPeg::GrammarRenderer.new(gram)
|
122
120
|
gr.render(io)
|
@@ -151,8 +149,6 @@ root = term
|
|
151
149
|
g.root = g.term
|
152
150
|
end
|
153
151
|
|
154
|
-
m = KPeg.match "4*3-8/9", gram
|
155
|
-
|
156
152
|
io = StringIO.new
|
157
153
|
gr = KPeg::GrammarRenderer.new(gram)
|
158
154
|
gr.render(io)
|
@@ -203,6 +199,31 @@ root = < [a-z]+ >
|
|
203
199
|
assert_equal expected, io.string
|
204
200
|
end
|
205
201
|
|
202
|
+
def test_directives
|
203
|
+
gram = KPeg.grammar do |g|
|
204
|
+
g.root = g.dot
|
205
|
+
g.add_directive "header", g.action("\n# coding: UTF-8\n")
|
206
|
+
g.add_directive "footer", g.action("\nrequire 'something'\n")
|
207
|
+
end
|
208
|
+
|
209
|
+
io = StringIO.new
|
210
|
+
gr = KPeg::GrammarRenderer.new(gram)
|
211
|
+
gr.render(io)
|
212
|
+
|
213
|
+
expected = <<-TXT
|
214
|
+
%% footer {
|
215
|
+
require 'something'
|
216
|
+
}
|
217
|
+
|
218
|
+
%% header {
|
219
|
+
# coding: UTF-8
|
220
|
+
}
|
221
|
+
|
222
|
+
root = .
|
223
|
+
TXT
|
224
|
+
assert_equal expected, io.string
|
225
|
+
end
|
226
|
+
|
206
227
|
def test_setup_actions
|
207
228
|
gram = KPeg.grammar do |g|
|
208
229
|
g.root = g.dot
|
@@ -220,7 +241,27 @@ root = .
|
|
220
241
|
TXT
|
221
242
|
assert_equal expected, io.string
|
222
243
|
end
|
223
|
-
|
244
|
+
|
245
|
+
def test_variables
|
246
|
+
gram = KPeg.grammar do |g|
|
247
|
+
g.root = g.dot
|
248
|
+
g.set_variable "name", "Foo"
|
249
|
+
g.set_variable "custom_initialize", "true"
|
250
|
+
end
|
251
|
+
|
252
|
+
io = StringIO.new
|
253
|
+
gr = KPeg::GrammarRenderer.new(gram)
|
254
|
+
gr.render(io)
|
255
|
+
|
256
|
+
expected = <<-TXT
|
257
|
+
%% custom_initialize = true
|
258
|
+
%% name = Foo
|
259
|
+
|
260
|
+
root = .
|
261
|
+
TXT
|
262
|
+
assert_equal expected, io.string
|
263
|
+
end
|
264
|
+
|
224
265
|
def test_multiple_render
|
225
266
|
gram = KPeg.grammar do |g|
|
226
267
|
g.root = g.multiple("a", 3, 5)
|
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:
|
4
|
+
hash: 55
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 10
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.10.0
|
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: 2012-
|
18
|
+
date: 2012-04-16 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: minitest
|
@@ -23,14 +23,13 @@ dependencies:
|
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
24
|
none: false
|
25
25
|
requirements:
|
26
|
-
- -
|
26
|
+
- - ~>
|
27
27
|
- !ruby/object:Gem::Version
|
28
|
-
hash:
|
28
|
+
hash: 27
|
29
29
|
segments:
|
30
30
|
- 2
|
31
|
-
-
|
32
|
-
|
33
|
-
version: 2.11.3
|
31
|
+
- 12
|
32
|
+
version: "2.12"
|
34
33
|
type: :development
|
35
34
|
version_requirements: *id001
|
36
35
|
- !ruby/object:Gem::Dependency
|
@@ -56,11 +55,11 @@ dependencies:
|
|
56
55
|
requirements:
|
57
56
|
- - ~>
|
58
57
|
- !ruby/object:Gem::Version
|
59
|
-
hash:
|
58
|
+
hash: 35
|
60
59
|
segments:
|
61
60
|
- 2
|
62
|
-
-
|
63
|
-
version: "2.
|
61
|
+
- 16
|
62
|
+
version: "2.16"
|
64
63
|
type: :development
|
65
64
|
version_requirements: *id003
|
66
65
|
description: |-
|
@@ -80,9 +79,9 @@ extensions: []
|
|
80
79
|
extra_rdoc_files:
|
81
80
|
- History.txt
|
82
81
|
- Manifest.txt
|
82
|
+
- README.rdoc
|
83
83
|
files:
|
84
84
|
- .autotest
|
85
|
-
- Gemfile
|
86
85
|
- History.txt
|
87
86
|
- LICENSE
|
88
87
|
- Manifest.txt
|
@@ -118,14 +117,13 @@ files:
|
|
118
117
|
- lib/kpeg/string_escape.kpeg
|
119
118
|
- lib/kpeg/string_escape.rb
|
120
119
|
- test/inputs/comments.kpeg
|
121
|
-
- test/test_file_parser_roundtrip.rb
|
122
|
-
- test/test_gen_calc.rb
|
123
120
|
- test/test_kpeg.rb
|
124
121
|
- test/test_kpeg_code_generator.rb
|
125
122
|
- test/test_kpeg_compiled_parser.rb
|
126
123
|
- test/test_kpeg_format.rb
|
124
|
+
- test/test_kpeg_format_parser_round_trip.rb
|
125
|
+
- test/test_kpeg_grammar.rb
|
127
126
|
- test/test_kpeg_grammar_renderer.rb
|
128
|
-
- test/test_left_recursion.rb
|
129
127
|
- vim/syntax_kpeg/ftdetect/kpeg.vim
|
130
128
|
- vim/syntax_kpeg/syntax/kpeg.vim
|
131
129
|
- .gemtest
|
@@ -159,16 +157,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
159
157
|
requirements: []
|
160
158
|
|
161
159
|
rubyforge_project: kpeg
|
162
|
-
rubygems_version: 1.8.
|
160
|
+
rubygems_version: 1.8.22
|
163
161
|
signing_key:
|
164
162
|
specification_version: 3
|
165
163
|
summary: KPeg is a simple PEG library for Ruby
|
166
164
|
test_files:
|
167
|
-
- test/test_file_parser_roundtrip.rb
|
168
|
-
- test/test_gen_calc.rb
|
169
165
|
- test/test_kpeg.rb
|
170
166
|
- test/test_kpeg_code_generator.rb
|
171
167
|
- test/test_kpeg_compiled_parser.rb
|
172
168
|
- test/test_kpeg_format.rb
|
169
|
+
- test/test_kpeg_format_parser_round_trip.rb
|
170
|
+
- test/test_kpeg_grammar.rb
|
173
171
|
- test/test_kpeg_grammar_renderer.rb
|
174
|
-
- test/test_left_recursion.rb
|
data/Gemfile
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
# -*- ruby -*-
|
2
|
-
|
3
|
-
# DO NOT EDIT THIS FILE. Instead, edit Rakefile, and run `rake bundler:gemfile`.
|
4
|
-
|
5
|
-
source :gemcutter
|
6
|
-
|
7
|
-
|
8
|
-
gem "minitest", "~>2.11", :group => [:development, :test]
|
9
|
-
gem "rdoc", "~>3.10", :group => [:development, :test]
|
10
|
-
gem "hoe", "~>2.15", :group => [:development, :test]
|
11
|
-
|
12
|
-
# vim: syntax=ruby
|
data/test/test_left_recursion.rb
DELETED
@@ -1,50 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'kpeg'
|
3
|
-
require 'kpeg/format_parser'
|
4
|
-
require 'kpeg/code_generator'
|
5
|
-
require 'stringio'
|
6
|
-
|
7
|
-
class TestKPegLeftRecursion < MiniTest::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
|