forked from evanphx/kpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_kpeg_grammar_renderer.rb
276 lines (222 loc) · 5.55 KB
/
test_kpeg_grammar_renderer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
require 'minitest/autorun'
require 'kpeg'
require 'kpeg/grammar_renderer'
require 'stringio'
class TestKPegGrammarRenderer < MiniTest::Unit::TestCase
def test_escape
str = "hello\nbob"
assert_equal 'hello\nbob', KPeg::GrammarRenderer.escape(str)
str = "hello\tbob"
assert_equal 'hello\tbob', KPeg::GrammarRenderer.escape(str)
str = "\\"
assert_equal '\\\\', KPeg::GrammarRenderer.escape(str)
str = 'hello"bob"'
assert_equal 'hello\\"bob\\"', KPeg::GrammarRenderer.escape(str)
end
def test_invoke
gram = KPeg.grammar do |g|
g.root = g.invoke("greeting")
end
io = StringIO.new
gr = KPeg::GrammarRenderer.new(gram)
gr.render(io)
assert_equal "root = @greeting\n", io.string
end
def test_invoke_with_args
gram = KPeg.grammar do |g|
g.root = g.invoke("greeting", "(1,2)")
end
io = StringIO.new
gr = KPeg::GrammarRenderer.new(gram)
gr.render(io)
assert_equal "root = @greeting(1,2)\n", io.string
end
def test_foreign_invoke
gram = KPeg.grammar do |g|
g.root = g.foreign_invoke("blah", "greeting")
end
io = StringIO.new
gr = KPeg::GrammarRenderer.new(gram)
gr.render(io)
assert_equal "root = %blah.greeting\n", io.string
end
def test_foreign_invoke_with_args
gram = KPeg.grammar do |g|
g.root = g.foreign_invoke("blah", "greeting", "(1,2)")
end
io = StringIO.new
gr = KPeg::GrammarRenderer.new(gram)
gr.render(io)
assert_equal "root = %blah.greeting(1,2)\n", io.string
end
def test_dot_render
gram = KPeg.grammar do |g|
g.root = g.dot
end
io = StringIO.new
gr = KPeg::GrammarRenderer.new(gram)
gr.render(io)
assert_equal "root = .\n", io.string
end
def test_tag_render
gram = KPeg.grammar do |g|
g.root = g.seq("+", g.t("hello", "greeting"))
end
io = StringIO.new
gr = KPeg::GrammarRenderer.new(gram)
gr.render(io)
assert_equal "root = \"+\" \"hello\":greeting\n", io.string
end
def test_tag_render_parens
gram = KPeg.grammar do |g|
g.root = g.t(g.seq(:b, :c), "greeting")
end
io = StringIO.new
gr = KPeg::GrammarRenderer.new(gram)
gr.render(io)
assert_equal "root = (b c):greeting\n", io.string
end
def test_grammar_renderer
gram = KPeg.grammar do |g|
g.some = g.range('0', '9')
g.num = g.reg(/[0-9]/)
g.term = g.any(
[:term, "+", :term],
[:term, "-", :term],
:fact)
g.fact = g.any(
[:fact, "*", :fact],
[:fact, "/", :fact],
:num
)
g.root = g.term
end
io = StringIO.new
gr = KPeg::GrammarRenderer.new(gram)
gr.render(io)
expected = <<-GRAM
some = [0-9]
num = /[0-9]/
term = term "+" term
| term "-" term
| fact
fact = fact "*" fact
| fact "/" fact
| num
root = term
GRAM
assert_equal expected, io.string
end
def test_grammar_renderer2
gram = KPeg.grammar do |g|
g.num = g.reg(/[0-9]/)
g.term = g.any(
[:term, g.t("+"), :term],
[:term, g.any("-", "$"), :term],
:fact)
g.fact = g.any(
[:fact, g.t("*", "op"), :fact],
[:fact, "/", :fact],
:num
)
g.root = g.term
end
io = StringIO.new
gr = KPeg::GrammarRenderer.new(gram)
gr.render(io)
expected = <<-GRAM
num = /[0-9]/
term = term "+" term
| term ("-" | "$") term
| fact
fact = fact "*":op fact
| fact "/" fact
| num
root = term
GRAM
assert_equal expected, io.string
end
def test_action
gram = KPeg.grammar do |g|
g.root = g.seq("hello", g.action("3 + 4"))
end
io = StringIO.new
gr = KPeg::GrammarRenderer.new(gram)
gr.render(io)
expected = <<-GRAM
root = "hello" {3 + 4}
GRAM
assert_equal expected, io.string
end
def test_collect
gram = KPeg.grammar do |g|
g.root = g.collect(g.many(g.range("a", "z")))
end
io = StringIO.new
gr = KPeg::GrammarRenderer.new(gram)
gr.render(io)
expected = <<-GRAM
root = < [a-z]+ >
GRAM
assert_equal expected, io.string
end
def test_directives
gram = KPeg.grammar do |g|
g.root = g.dot
g.add_directive "header", g.action("\n# coding: UTF-8\n")
g.add_directive "footer", g.action("\nrequire 'something'\n")
end
io = StringIO.new
gr = KPeg::GrammarRenderer.new(gram)
gr.render(io)
expected = <<-TXT
%% footer {
require 'something'
}
%% header {
# coding: UTF-8
}
root = .
TXT
assert_equal expected, io.string
end
def test_setup_actions
gram = KPeg.grammar do |g|
g.root = g.dot
g.add_setup g.action(" attr_reader :foo ")
end
io = StringIO.new
gr = KPeg::GrammarRenderer.new(gram)
gr.render(io)
expected = <<-TXT
%% { attr_reader :foo }
root = .
TXT
assert_equal expected, io.string
end
def test_variables
gram = KPeg.grammar do |g|
g.root = g.dot
g.set_variable "name", "Foo"
g.set_variable "custom_initialize", "true"
end
io = StringIO.new
gr = KPeg::GrammarRenderer.new(gram)
gr.render(io)
expected = <<-TXT
%% custom_initialize = true
%% name = Foo
root = .
TXT
assert_equal expected, io.string
end
def test_multiple_render
gram = KPeg.grammar do |g|
g.root = g.multiple("a", 3, 5)
end
io = StringIO.new
gr = KPeg::GrammarRenderer.new(gram)
gr.render(io)
assert_equal "root = \"a\"[3, 5]\n", io.string
end
end