raf-parser 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,358 @@
1
+ # Copyright (C) garin <garin54@gmail.com> 2011
2
+ # See the included file COPYING for details.
3
+ class BlockParser
4
+ token HEADER WHITELINE HEADLINE PLAIN DESCLINE_TITLE DESCLINE QUOTE INDENT DEDENT ITEMLIST ITEMLISTCONTINUE NUMLIST TABLELINE
5
+ preclow
6
+ nonassoc DUMMY
7
+ prechigh
8
+ options no_result_var
9
+
10
+ rule
11
+ document : blocks{ val[0].compact }
12
+
13
+ blocks : block { val }
14
+ | blocks block { [val[0], val[1]].flatten }
15
+
16
+ block : header
17
+ | paragraph { val[0] }
18
+ | quote_block
19
+ | itemlist_blocks { ItemList.new(val[0].flatten) }
20
+ | numlist_blocks { NumList.new(val[0].flatten) }
21
+ | desc_block
22
+ | table_block
23
+ | headline
24
+ | WHITELINE { WhiteLine.new }
25
+
26
+ # ----- header
27
+ header : HEADER {
28
+ name, val = val[0].split(":",2)
29
+ if name.nil? or val.nil?
30
+ else
31
+ @metadata.update({name.strip.to_sym => val.strip })
32
+ end
33
+ nil }
34
+
35
+ # ----- headline
36
+ headline : HEADLINE { # val[0] is like [level, title, index]
37
+ title = val[0][1]
38
+ level = val[0][0]
39
+ if level == 1
40
+ @metadata[:subject] ||= title
41
+ else
42
+ @head_index.update(level)
43
+ end
44
+
45
+ @index[:head] ||= []
46
+ @index[:head] << {:title => title, :level => level, :index => @head_index.to_s}
47
+ HeadLine.new([level, title, @index[:head].size, @head_index.to_s]) }
48
+ # ----- paragraph
49
+ paragraph : plain_texts { Paragraph.new @inline_parser.parse(val) }
50
+
51
+ plain_texts : PLAIN { val[0] }
52
+ | plain_texts PLAIN { val[0] + val[1] }
53
+
54
+ # ----- desc
55
+ desc_block : DESCLINE_TITLE desclines {
56
+ if val[1].nil?
57
+ lines = [Plain.new("")]
58
+ else
59
+ lines = @inline_parser.parse(val[1])
60
+ end
61
+ Desc.new([val[0], lines])
62
+ }
63
+
64
+
65
+ desclines : DESCLINE { val[0] }
66
+ | desclines DESCLINE { val[0] + val[1] }
67
+ |
68
+
69
+
70
+ # ----- quote
71
+ quote_block : quotes { qu = val[0].strip ; Quote.new([qu]) unless qu.empty? }
72
+
73
+ quotes : QUOTE { val[0] }
74
+ | quotes QUOTE { val[0] + val[1] }
75
+
76
+ # ----- quote end
77
+ # ----- itemlist
78
+ itemlist_blocks : itemlist_block { val[0] }
79
+ | itemlist_blocks itemlist_block { val[0] << val[1] }
80
+
81
+ itemlist_block : itemlists { val[0] }
82
+ | itemlist_indent_blocks { val[0] }
83
+
84
+ itemlist_indent_blocks : INDENT itemlist_blocks DEDENT { val }
85
+
86
+ itemlists : itemlistitems {[PlainTextBlock.new(@inline_parser.parse(val[0]))]}
87
+ | itemlists itemlistitems { val[0] << PlainTextBlock.new(@inline_parser.parse(val[1])) }
88
+
89
+ itemlistitems : ITEMLIST { val[0] }
90
+ | ITEMLIST itemlist_continues { val[0] + val[1] }
91
+
92
+ itemlist_continues : ITEMLISTCONTINUE { "\n" + val[0] }
93
+ | itemlist_continues ITEMLISTCONTINUE { val[0] + "\n" + val[1] }
94
+
95
+ # ----- itemlist end
96
+ # ----- numlist
97
+ numlist_blocks : numlist_block { val[0] }
98
+ | numlist_blocks numlist_block { val[0] << val[1] }
99
+
100
+ numlist_block : numlists { val[0] }
101
+ | numlist_indent_blocks { val[0] }
102
+
103
+ numlist_indent_blocks : INDENT numlist_blocks DEDENT { val }
104
+
105
+ numlists : NUMLIST { [PlainTextBlock.new(@inline_parser.parse(val[0]))] }
106
+ | numlists NUMLIST { val[0] << PlainTextBlock.new(@inline_parser.parse(val[1])) }
107
+
108
+ # ----- numlist end
109
+
110
+ # ----- tableblock
111
+ table_block : tablelines { Table.new(val[0]) }
112
+
113
+ tablelines : TABLELINE { val }
114
+ | tablelines TABLELINE { val[0] << val[1] }
115
+
116
+ # ----- tableblock end
117
+ end # end of rule
118
+
119
+ ---- inner
120
+ include ParserUtility
121
+
122
+ class Line
123
+ def initialize(line)
124
+ @content = line
125
+ # @indent = get_line_indent(line)
126
+ # @type = nil
127
+ end
128
+ attr_reader :indent, :no
129
+ attr_accessor :type, :content
130
+ alias indent_size indent
131
+
132
+ def get_line_indent
133
+ return 0 if @content.nil?
134
+ @content =~ /(\s*)/
135
+ $1.size
136
+ end
137
+ alias indent get_line_indent
138
+ end
139
+
140
+
141
+ def initialize
142
+ @inline_parser = InlineParser.new
143
+ @metadata = {}
144
+ @inline_index = @inline_parser.index
145
+ @index = {}
146
+ @head_index = HeadIndex.new
147
+ end
148
+ attr_reader :metadata, :inline_index, :index
149
+
150
+ def parse(src)
151
+ @no = 0
152
+ @src = Array(src)
153
+ @line = Line.new("")
154
+ @line_pre = @line.dup
155
+ @indent_stack = []
156
+ @current_indent = 0
157
+ @current_type = :header
158
+ @yydebug = true
159
+ @view_token_type = false
160
+ do_parse
161
+ end
162
+
163
+ def on_error(token_id, value, stack)
164
+ lineno = @src[0..@no].to_s.split("\n").size
165
+ raise Racc::ParseError,
166
+ "rafblockpaser: line #{lineno}: syntax error on #{value.inspect}"
167
+ end
168
+
169
+ def next_token
170
+ @line_pre = @line.dup
171
+ @line = Line.new(@src[@no])
172
+ # puts "line: #{@line.content}" if @view_token_type
173
+ case @line.content
174
+ when nil
175
+ @line.content = ""
176
+ if_current_indent_equal("") do
177
+ puts "b: false: #{@line.content}" if @view_token_type
178
+ [false, false]
179
+ end
180
+ when /^$/
181
+ @line.content = ""
182
+ if_current_indent_equal("") do
183
+ if @current_type == :quote
184
+ puts "b: QUOTE: #{@line.content}" if @view_token_type
185
+ [:QUOTE, "\n"]
186
+ elsif @current_type == :descline
187
+ puts "b: DESCLINE: #{@line.content}" if @view_token_type
188
+ [:DESCLINE, " "]
189
+ else
190
+ puts "b: WHITELINE: #{@line.content}" if @view_token_type
191
+ @current_type = :whiteline
192
+ [:WHITELINE, :WHITELINE]
193
+ end
194
+ end
195
+ when /^\#(.*)/ # comment line
196
+ @no += 1
197
+ if @current_type == :header
198
+ puts "b: HEADER: #{@line.content}" if @view_token_type
199
+ [:HEADER, $1.strip]
200
+ else
201
+ puts "b: COMMENT(noop): #{@line.content}" if @view_token_type
202
+ next_token
203
+ end
204
+ when /^(={1,4})(?!=)\s*(?=\S)/, /^(\+{1,2})(?!\+)\s*(?=\S)/
205
+ rest = $' # '
206
+ rest.strip!
207
+ mark = $1
208
+ # if_current_indent_equal("") do
209
+ if_current_indent_equal(@line.indent) do
210
+ @current_type = :headline
211
+ puts "b: HEADLINE: #{@line.content}" if @view_token_type
212
+ [:HEADLINE, [mark_to_level(mark), rest]]
213
+ end
214
+ when /^\s\s+(.*)/ # type == quote
215
+ puts "b: 2 WHITE SPACE(#{@current_type}) : #{@line.content}" if @view_token_type
216
+ case @current_type
217
+ when :itemlist
218
+ if @line.content =~ /^(\s*)(\*)(\s+)(.*)/
219
+ line = $4.strip
220
+ if line.empty?
221
+ @no += 1
222
+ next_token
223
+ else
224
+ if_current_indent_equal(@line.indent) do
225
+ puts "b: ITEMLIST: [#{line}]" if @view_token_type
226
+ @current_type = :itemlist
227
+ [:ITEMLIST, line]
228
+ end
229
+ end
230
+ else
231
+ line = @line.content.strip
232
+ if line.empty?
233
+ @no += 1
234
+ next_token
235
+ else
236
+ puts "b: ITEMLISTCONTINUE: [#{line.empty?}] --" if @view_token_type
237
+ @no += 1
238
+ @current_type = :itemlist
239
+ [:ITEMLISTCONTINUE, line]
240
+ end
241
+ end
242
+ when :numlist
243
+ @line.content =~ /^(\s*)(\(\d+\))(\s+)(.*)/
244
+ if $4.nil?
245
+ @line.content =~ /^(\s*)(\d\.)(\s+)(.*)/
246
+ end
247
+ line = $4
248
+ line ||= @line.content.strip
249
+ if line.empty?
250
+ @no += 1
251
+ next_token
252
+ else
253
+ if_current_indent_equal(@line.indent) do
254
+ puts "b: NUMLIST: [#{line}]" if @view_token_type
255
+ @current_type = :numlist
256
+ [:NUMLIST, line]
257
+ end
258
+ end
259
+ else
260
+ @no += 1
261
+ if @current_type == :descline
262
+ @current_type = :descline
263
+ puts "b: DESCLINE: #{@line.content}" if @view_token_type
264
+ [:DESCLINE, $1 + "\n"]
265
+ else
266
+ @current_type = :quote
267
+ puts "b: QUOTE: #{$1}" if @view_token_type
268
+ [:QUOTE, @line.content.sub(" ","")]
269
+ end
270
+ end
271
+ when /^(\:)(.*)/ # type = desclist
272
+ if_current_indent_equal(@line.indent) do
273
+ @current_type = :descline
274
+ puts "b: DESCLINE_TILTE: #{$2.strip}" if @view_token_type
275
+ [:DESCLINE_TITLE, $2.strip]
276
+ end
277
+ when /^(\s*)(\*)(\s+)(.*)/ # type = itemlist
278
+ if_current_indent_equal(@line.indent) do
279
+ puts "b: ITEMLIST: #{@line.content}" if @view_token_type
280
+ @current_type = :itemlist
281
+ [:ITEMLIST, $4]
282
+ end
283
+ when /^(\s*)(\(\d+\))(\s+)(.*)/
284
+ if_current_indent_equal(@line.indent) do
285
+ puts "b: NUMLIST: #{@line.content}" if @view_token_type
286
+ @current_type = :numlist
287
+ [:NUMLIST, $4]
288
+ end
289
+ when /^(\s*)(\d+\.)(\s+)(.*)/ # type = numlist
290
+ if_current_indent_equal(@line.indent) do
291
+ puts "b: NUMLIST: #{@line.content}" if @view_token_type
292
+ @current_type = :numlist
293
+ [:NUMLIST, $4]
294
+ end
295
+ when /^\|.*/ # type = table
296
+ @no += 1
297
+ @current_type = :table
298
+ lines = @line.content.chomp.split("|")
299
+ lines.shift
300
+ [:TABLELINE, lines]
301
+ when /(.*)/ # type == plain
302
+ @current_type = :plain
303
+ if_current_indent_equal(@line.indent) do
304
+ puts "b: PLAIN: #{@line.content}" if @view_token_type
305
+ [:PLAIN, @line.content]
306
+ end
307
+ else
308
+ puts "raise : #{@line}"
309
+ end
310
+ end
311
+
312
+ def if_current_indent_equal(ident)
313
+ indent_space = 2
314
+ puts "current: #{@current_indent}, line: #{@line.indent}, stack #{@indent_stack.size}:" if @view_token_type
315
+ indent_sabun = @current_indent - @line.indent
316
+ if indent_sabun >= -1 and indent_sabun <= 1
317
+ @no += 1
318
+ yield
319
+ elsif @current_indent < @line.indent
320
+ ((@line.indent - @current_indent) / indent_space).times do
321
+ @indent_stack.push("")
322
+ end
323
+ @current_indent = @line.indent
324
+ puts "b: INDENT" if @view_token_type
325
+ [:INDENT, :INDENT]
326
+ else
327
+ @indent_stack.pop
328
+ @current_indent = @line.indent if @line.indent == @indent_stack.size * indent_space
329
+ puts "b: DEDENT" if @view_token_type
330
+ [:DEDENT, :DEDENT]
331
+ end
332
+ end
333
+
334
+ ---- header
335
+ require "parserutility"
336
+ require "rafinlineparser.tab"
337
+ require "rafelement"
338
+
339
+ module Raf
340
+
341
+ ---- footer
342
+ if __FILE__ == $0
343
+ raf = BlockParser.new
344
+ src = $stdin.readlines
345
+ nodes = raf.parse(src)
346
+ puts "----- index -----"
347
+ raf.index.each do |key,val|
348
+ puts key
349
+ val.each do |v| p v end
350
+ end
351
+ puts "----- info -----"
352
+ p raf.info
353
+ puts "----- output -----"
354
+ nodes.each do |n|
355
+ puts n.apply
356
+ end
357
+ end
358
+ end # end of module Raf
@@ -0,0 +1,715 @@
1
+ #
2
+ # DO NOT MODIFY!!!!
3
+ # This file is automatically generated by Racc 1.4.10
4
+ # from Racc grammer file "".
5
+ #
6
+
7
+ require 'racc/parser.rb'
8
+
9
+ require "parserutility"
10
+ require "rafinlineparser.tab"
11
+ require "rafelement"
12
+
13
+ module Raf
14
+
15
+ class BlockParser < Racc::Parser
16
+
17
+ module_eval(<<'...end rafblockparser.ry/module_eval...', 'rafblockparser.ry', 120)
18
+ include ParserUtility
19
+
20
+ class Line
21
+ def initialize(line)
22
+ @content = line
23
+ # @indent = get_line_indent(line)
24
+ # @type = nil
25
+ end
26
+ attr_reader :indent, :no
27
+ attr_accessor :type, :content
28
+ alias indent_size indent
29
+
30
+ def get_line_indent
31
+ return 0 if @content.nil?
32
+ @content =~ /(\s*)/
33
+ $1.size
34
+ end
35
+ alias indent get_line_indent
36
+ end
37
+
38
+
39
+ def initialize
40
+ @inline_parser = InlineParser.new
41
+ @metadata = {}
42
+ @inline_index = @inline_parser.index
43
+ @index = {}
44
+ @head_index = HeadIndex.new
45
+ end
46
+ attr_reader :metadata, :inline_index, :index
47
+
48
+ def parse(src)
49
+ @no = 0
50
+ @src = Array(src)
51
+ @line = Line.new("")
52
+ @line_pre = @line.dup
53
+ @indent_stack = []
54
+ @current_indent = 0
55
+ @current_type = :header
56
+ @yydebug = true
57
+ @view_token_type = false
58
+ do_parse
59
+ end
60
+
61
+ def on_error(token_id, value, stack)
62
+ lineno = @src[0..@no].to_s.split("\n").size
63
+ raise Racc::ParseError,
64
+ "rafblockpaser: line #{lineno}: syntax error on #{value.inspect}"
65
+ end
66
+
67
+ def next_token
68
+ @line_pre = @line.dup
69
+ @line = Line.new(@src[@no])
70
+ # puts "line: #{@line.content}" if @view_token_type
71
+ case @line.content
72
+ when nil
73
+ @line.content = ""
74
+ if_current_indent_equal("") do
75
+ puts "b: false: #{@line.content}" if @view_token_type
76
+ [false, false]
77
+ end
78
+ when /^$/
79
+ @line.content = ""
80
+ if_current_indent_equal("") do
81
+ if @current_type == :quote
82
+ puts "b: QUOTE: #{@line.content}" if @view_token_type
83
+ [:QUOTE, "\n"]
84
+ elsif @current_type == :descline
85
+ puts "b: DESCLINE: #{@line.content}" if @view_token_type
86
+ [:DESCLINE, " "]
87
+ else
88
+ puts "b: WHITELINE: #{@line.content}" if @view_token_type
89
+ @current_type = :whiteline
90
+ [:WHITELINE, :WHITELINE]
91
+ end
92
+ end
93
+ when /^\#(.*)/ # comment line
94
+ @no += 1
95
+ if @current_type == :header
96
+ puts "b: HEADER: #{@line.content}" if @view_token_type
97
+ [:HEADER, $1.strip]
98
+ else
99
+ puts "b: COMMENT(noop): #{@line.content}" if @view_token_type
100
+ next_token
101
+ end
102
+ when /^(={1,4})(?!=)\s*(?=\S)/, /^(\+{1,2})(?!\+)\s*(?=\S)/
103
+ rest = $' # '
104
+ rest.strip!
105
+ mark = $1
106
+ # if_current_indent_equal("") do
107
+ if_current_indent_equal(@line.indent) do
108
+ @current_type = :headline
109
+ puts "b: HEADLINE: #{@line.content}" if @view_token_type
110
+ [:HEADLINE, [mark_to_level(mark), rest]]
111
+ end
112
+ when /^\s\s+(.*)/ # type == quote
113
+ puts "b: 2 WHITE SPACE(#{@current_type}) : #{@line.content}" if @view_token_type
114
+ case @current_type
115
+ when :itemlist
116
+ if @line.content =~ /^(\s*)(\*)(\s+)(.*)/
117
+ line = $4.strip
118
+ if line.empty?
119
+ @no += 1
120
+ next_token
121
+ else
122
+ if_current_indent_equal(@line.indent) do
123
+ puts "b: ITEMLIST: [#{line}]" if @view_token_type
124
+ @current_type = :itemlist
125
+ [:ITEMLIST, line]
126
+ end
127
+ end
128
+ else
129
+ line = @line.content.strip
130
+ if line.empty?
131
+ @no += 1
132
+ next_token
133
+ else
134
+ puts "b: ITEMLISTCONTINUE: [#{line.empty?}] --" if @view_token_type
135
+ @no += 1
136
+ @current_type = :itemlist
137
+ [:ITEMLISTCONTINUE, line]
138
+ end
139
+ end
140
+ when :numlist
141
+ @line.content =~ /^(\s*)(\(\d+\))(\s+)(.*)/
142
+ if $4.nil?
143
+ @line.content =~ /^(\s*)(\d\.)(\s+)(.*)/
144
+ end
145
+ line = $4
146
+ line ||= @line.content.strip
147
+ if line.empty?
148
+ @no += 1
149
+ next_token
150
+ else
151
+ if_current_indent_equal(@line.indent) do
152
+ puts "b: NUMLIST: [#{line}]" if @view_token_type
153
+ @current_type = :numlist
154
+ [:NUMLIST, line]
155
+ end
156
+ end
157
+ else
158
+ @no += 1
159
+ if @current_type == :descline
160
+ @current_type = :descline
161
+ puts "b: DESCLINE: #{@line.content}" if @view_token_type
162
+ [:DESCLINE, $1 + "\n"]
163
+ else
164
+ @current_type = :quote
165
+ puts "b: QUOTE: #{$1}" if @view_token_type
166
+ [:QUOTE, @line.content.sub(" ","")]
167
+ end
168
+ end
169
+ when /^(\:)(.*)/ # type = desclist
170
+ if_current_indent_equal(@line.indent) do
171
+ @current_type = :descline
172
+ puts "b: DESCLINE_TILTE: #{$2.strip}" if @view_token_type
173
+ [:DESCLINE_TITLE, $2.strip]
174
+ end
175
+ when /^(\s*)(\*)(\s+)(.*)/ # type = itemlist
176
+ if_current_indent_equal(@line.indent) do
177
+ puts "b: ITEMLIST: #{@line.content}" if @view_token_type
178
+ @current_type = :itemlist
179
+ [:ITEMLIST, $4]
180
+ end
181
+ when /^(\s*)(\(\d+\))(\s+)(.*)/
182
+ if_current_indent_equal(@line.indent) do
183
+ puts "b: NUMLIST: #{@line.content}" if @view_token_type
184
+ @current_type = :numlist
185
+ [:NUMLIST, $4]
186
+ end
187
+ when /^(\s*)(\d+\.)(\s+)(.*)/ # type = numlist
188
+ if_current_indent_equal(@line.indent) do
189
+ puts "b: NUMLIST: #{@line.content}" if @view_token_type
190
+ @current_type = :numlist
191
+ [:NUMLIST, $4]
192
+ end
193
+ when /^\|.*/ # type = table
194
+ @no += 1
195
+ @current_type = :table
196
+ lines = @line.content.chomp.split("|")
197
+ lines.shift
198
+ [:TABLELINE, lines]
199
+ when /(.*)/ # type == plain
200
+ @current_type = :plain
201
+ if_current_indent_equal(@line.indent) do
202
+ puts "b: PLAIN: #{@line.content}" if @view_token_type
203
+ [:PLAIN, @line.content]
204
+ end
205
+ else
206
+ puts "raise : #{@line}"
207
+ end
208
+ end
209
+
210
+ def if_current_indent_equal(ident)
211
+ indent_space = 2
212
+ puts "current: #{@current_indent}, line: #{@line.indent}, stack #{@indent_stack.size}:" if @view_token_type
213
+ indent_sabun = @current_indent - @line.indent
214
+ if indent_sabun >= -1 and indent_sabun <= 1
215
+ @no += 1
216
+ yield
217
+ elsif @current_indent < @line.indent
218
+ ((@line.indent - @current_indent) / indent_space).times do
219
+ @indent_stack.push("")
220
+ end
221
+ @current_indent = @line.indent
222
+ puts "b: INDENT" if @view_token_type
223
+ [:INDENT, :INDENT]
224
+ else
225
+ @indent_stack.pop
226
+ @current_indent = @line.indent if @line.indent == @indent_stack.size * indent_space
227
+ puts "b: DEDENT" if @view_token_type
228
+ [:DEDENT, :DEDENT]
229
+ end
230
+ end
231
+
232
+ ...end rafblockparser.ry/module_eval...
233
+ ##### State transition tables begin ###
234
+
235
+ racc_action_table = [
236
+ 13, 12, 14, 16, 17, 46, 19, 23, 35, 25,
237
+ 25, 29, 31, 13, 12, 14, 16, 17, 25, 19,
238
+ 23, 47, 25, 37, 29, 31, 23, 29, 25, 37,
239
+ 29, 37, 52, 29, 48, 29, 35, 51, 25, 35,
240
+ 49, 25, 41, 40, 50, 38, 32, 53 ]
241
+
242
+ racc_action_check = [
243
+ 0, 0, 0, 0, 0, 25, 0, 0, 35, 0,
244
+ 35, 0, 0, 2, 2, 2, 2, 2, 21, 2,
245
+ 2, 27, 2, 8, 2, 2, 23, 8, 23, 37,
246
+ 23, 44, 44, 37, 30, 44, 43, 43, 43, 7,
247
+ 32, 7, 18, 17, 39, 15, 1, 45 ]
248
+
249
+ racc_action_pointer = [
250
+ -2, 46, 11, nil, nil, nil, nil, 30, 14, nil,
251
+ nil, nil, nil, nil, nil, 40, nil, 36, 34, nil,
252
+ nil, 7, nil, 17, nil, -7, nil, 8, nil, nil,
253
+ 20, nil, 40, nil, nil, -1, nil, 20, nil, 37,
254
+ nil, nil, nil, 27, 22, 35, nil, nil, nil, nil,
255
+ nil, nil, nil, nil ]
256
+
257
+ racc_action_default = [
258
+ -46, -46, -1, -2, -4, -5, -6, -7, -8, -9,
259
+ -10, -11, -12, -13, -14, -15, -16, -21, -22, -23,
260
+ -25, -27, -28, -46, -30, -32, -36, -38, -39, -41,
261
+ -43, -44, -46, -3, -26, -46, -37, -46, -17, -18,
262
+ -19, -24, -31, -46, -46, -33, -34, -42, -45, 54,
263
+ -20, -29, -40, -35 ]
264
+
265
+ racc_goto_table = [
266
+ 36, 34, 44, 43, 3, 39, 33, 2, 42, 45,
267
+ 1, nil, nil, nil, nil, 43, 44, nil, nil, nil,
268
+ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
269
+ nil, nil, nil, nil, nil, nil, 36, 34 ]
270
+
271
+ racc_goto_check = [
272
+ 20, 15, 8, 7, 3, 13, 3, 2, 18, 19,
273
+ 1, nil, nil, nil, nil, 7, 8, nil, nil, nil,
274
+ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
275
+ nil, nil, nil, nil, nil, nil, 20, 15 ]
276
+
277
+ racc_goto_pointer = [
278
+ nil, 10, 7, 4, nil, nil, nil, -20, -21, nil,
279
+ nil, nil, nil, -12, nil, -6, nil, nil, -13, -16,
280
+ -8, nil, nil, nil ]
281
+
282
+ racc_goto_default = [
283
+ nil, nil, nil, nil, 4, 5, 6, 7, 8, 9,
284
+ 10, 11, 15, nil, 18, 20, 21, 22, 24, nil,
285
+ 26, 27, 28, 30 ]
286
+
287
+ racc_reduce_table = [
288
+ 0, 0, :racc_error,
289
+ 1, 17, :_reduce_1,
290
+ 1, 18, :_reduce_2,
291
+ 2, 18, :_reduce_3,
292
+ 1, 19, :_reduce_none,
293
+ 1, 19, :_reduce_5,
294
+ 1, 19, :_reduce_none,
295
+ 1, 19, :_reduce_7,
296
+ 1, 19, :_reduce_8,
297
+ 1, 19, :_reduce_none,
298
+ 1, 19, :_reduce_none,
299
+ 1, 19, :_reduce_none,
300
+ 1, 19, :_reduce_12,
301
+ 1, 20, :_reduce_13,
302
+ 1, 27, :_reduce_14,
303
+ 1, 21, :_reduce_15,
304
+ 1, 28, :_reduce_16,
305
+ 2, 28, :_reduce_17,
306
+ 2, 25, :_reduce_18,
307
+ 1, 29, :_reduce_19,
308
+ 2, 29, :_reduce_20,
309
+ 0, 29, :_reduce_none,
310
+ 1, 22, :_reduce_22,
311
+ 1, 30, :_reduce_23,
312
+ 2, 30, :_reduce_24,
313
+ 1, 23, :_reduce_25,
314
+ 2, 23, :_reduce_26,
315
+ 1, 31, :_reduce_27,
316
+ 1, 31, :_reduce_28,
317
+ 3, 33, :_reduce_29,
318
+ 1, 32, :_reduce_30,
319
+ 2, 32, :_reduce_31,
320
+ 1, 34, :_reduce_32,
321
+ 2, 34, :_reduce_33,
322
+ 1, 35, :_reduce_34,
323
+ 2, 35, :_reduce_35,
324
+ 1, 24, :_reduce_36,
325
+ 2, 24, :_reduce_37,
326
+ 1, 36, :_reduce_38,
327
+ 1, 36, :_reduce_39,
328
+ 3, 38, :_reduce_40,
329
+ 1, 37, :_reduce_41,
330
+ 2, 37, :_reduce_42,
331
+ 1, 26, :_reduce_43,
332
+ 1, 39, :_reduce_44,
333
+ 2, 39, :_reduce_45 ]
334
+
335
+ racc_reduce_n = 46
336
+
337
+ racc_shift_n = 54
338
+
339
+ racc_token_table = {
340
+ false => 0,
341
+ :error => 1,
342
+ :HEADER => 2,
343
+ :WHITELINE => 3,
344
+ :HEADLINE => 4,
345
+ :PLAIN => 5,
346
+ :DESCLINE_TITLE => 6,
347
+ :DESCLINE => 7,
348
+ :QUOTE => 8,
349
+ :INDENT => 9,
350
+ :DEDENT => 10,
351
+ :ITEMLIST => 11,
352
+ :ITEMLISTCONTINUE => 12,
353
+ :NUMLIST => 13,
354
+ :TABLELINE => 14,
355
+ :DUMMY => 15 }
356
+
357
+ racc_nt_base = 16
358
+
359
+ racc_use_result_var = false
360
+
361
+ Racc_arg = [
362
+ racc_action_table,
363
+ racc_action_check,
364
+ racc_action_default,
365
+ racc_action_pointer,
366
+ racc_goto_table,
367
+ racc_goto_check,
368
+ racc_goto_default,
369
+ racc_goto_pointer,
370
+ racc_nt_base,
371
+ racc_reduce_table,
372
+ racc_token_table,
373
+ racc_shift_n,
374
+ racc_reduce_n,
375
+ racc_use_result_var ]
376
+
377
+ Racc_token_to_s_table = [
378
+ "$end",
379
+ "error",
380
+ "HEADER",
381
+ "WHITELINE",
382
+ "HEADLINE",
383
+ "PLAIN",
384
+ "DESCLINE_TITLE",
385
+ "DESCLINE",
386
+ "QUOTE",
387
+ "INDENT",
388
+ "DEDENT",
389
+ "ITEMLIST",
390
+ "ITEMLISTCONTINUE",
391
+ "NUMLIST",
392
+ "TABLELINE",
393
+ "DUMMY",
394
+ "$start",
395
+ "document",
396
+ "blocks",
397
+ "block",
398
+ "header",
399
+ "paragraph",
400
+ "quote_block",
401
+ "itemlist_blocks",
402
+ "numlist_blocks",
403
+ "desc_block",
404
+ "table_block",
405
+ "headline",
406
+ "plain_texts",
407
+ "desclines",
408
+ "quotes",
409
+ "itemlist_block",
410
+ "itemlists",
411
+ "itemlist_indent_blocks",
412
+ "itemlistitems",
413
+ "itemlist_continues",
414
+ "numlist_block",
415
+ "numlists",
416
+ "numlist_indent_blocks",
417
+ "tablelines" ]
418
+
419
+ Racc_debug_parser = false
420
+
421
+ ##### State transition tables end #####
422
+
423
+ # reduce 0 omitted
424
+
425
+ module_eval(<<'.,.,', 'rafblockparser.ry', 10)
426
+ def _reduce_1(val, _values)
427
+ val[0].compact
428
+ end
429
+ .,.,
430
+
431
+ module_eval(<<'.,.,', 'rafblockparser.ry', 12)
432
+ def _reduce_2(val, _values)
433
+ val
434
+ end
435
+ .,.,
436
+
437
+ module_eval(<<'.,.,', 'rafblockparser.ry', 13)
438
+ def _reduce_3(val, _values)
439
+ [val[0], val[1]].flatten
440
+ end
441
+ .,.,
442
+
443
+ # reduce 4 omitted
444
+
445
+ module_eval(<<'.,.,', 'rafblockparser.ry', 16)
446
+ def _reduce_5(val, _values)
447
+ val[0]
448
+ end
449
+ .,.,
450
+
451
+ # reduce 6 omitted
452
+
453
+ module_eval(<<'.,.,', 'rafblockparser.ry', 18)
454
+ def _reduce_7(val, _values)
455
+ ItemList.new(val[0].flatten)
456
+ end
457
+ .,.,
458
+
459
+ module_eval(<<'.,.,', 'rafblockparser.ry', 19)
460
+ def _reduce_8(val, _values)
461
+ NumList.new(val[0].flatten)
462
+ end
463
+ .,.,
464
+
465
+ # reduce 9 omitted
466
+
467
+ # reduce 10 omitted
468
+
469
+ # reduce 11 omitted
470
+
471
+ module_eval(<<'.,.,', 'rafblockparser.ry', 23)
472
+ def _reduce_12(val, _values)
473
+ WhiteLine.new
474
+ end
475
+ .,.,
476
+
477
+ module_eval(<<'.,.,', 'rafblockparser.ry', 27)
478
+ def _reduce_13(val, _values)
479
+ name, val = val[0].split(":",2)
480
+ if name.nil? or val.nil?
481
+ else
482
+ @metadata.update({name.strip.to_sym => val.strip })
483
+ end
484
+ nil
485
+ end
486
+ .,.,
487
+
488
+ module_eval(<<'.,.,', 'rafblockparser.ry', 35)
489
+ def _reduce_14(val, _values)
490
+ # val[0] is like [level, title, index]
491
+ title = val[0][1]
492
+ level = val[0][0]
493
+ if level == 1
494
+ @metadata[:subject] ||= title
495
+ else
496
+ @head_index.update(level)
497
+ end
498
+
499
+ @index[:head] ||= []
500
+ @index[:head] << {:title => title, :level => level, :index => @head_index.to_s}
501
+ HeadLine.new([level, title, @index[:head].size, @head_index.to_s])
502
+ end
503
+ .,.,
504
+
505
+ module_eval(<<'.,.,', 'rafblockparser.ry', 48)
506
+ def _reduce_15(val, _values)
507
+ Paragraph.new @inline_parser.parse(val)
508
+ end
509
+ .,.,
510
+
511
+ module_eval(<<'.,.,', 'rafblockparser.ry', 50)
512
+ def _reduce_16(val, _values)
513
+ val[0]
514
+ end
515
+ .,.,
516
+
517
+ module_eval(<<'.,.,', 'rafblockparser.ry', 51)
518
+ def _reduce_17(val, _values)
519
+ val[0] + val[1]
520
+ end
521
+ .,.,
522
+
523
+ module_eval(<<'.,.,', 'rafblockparser.ry', 55)
524
+ def _reduce_18(val, _values)
525
+ if val[1].nil?
526
+ lines = [Plain.new("")]
527
+ else
528
+ lines = @inline_parser.parse(val[1])
529
+ end
530
+ Desc.new([val[0], lines])
531
+
532
+ end
533
+ .,.,
534
+
535
+ module_eval(<<'.,.,', 'rafblockparser.ry', 64)
536
+ def _reduce_19(val, _values)
537
+ val[0]
538
+ end
539
+ .,.,
540
+
541
+ module_eval(<<'.,.,', 'rafblockparser.ry', 65)
542
+ def _reduce_20(val, _values)
543
+ val[0] + val[1]
544
+ end
545
+ .,.,
546
+
547
+ # reduce 21 omitted
548
+
549
+ module_eval(<<'.,.,', 'rafblockparser.ry', 70)
550
+ def _reduce_22(val, _values)
551
+ qu = val[0].strip ; Quote.new([qu]) unless qu.empty?
552
+ end
553
+ .,.,
554
+
555
+ module_eval(<<'.,.,', 'rafblockparser.ry', 72)
556
+ def _reduce_23(val, _values)
557
+ val[0]
558
+ end
559
+ .,.,
560
+
561
+ module_eval(<<'.,.,', 'rafblockparser.ry', 73)
562
+ def _reduce_24(val, _values)
563
+ val[0] + val[1]
564
+ end
565
+ .,.,
566
+
567
+ module_eval(<<'.,.,', 'rafblockparser.ry', 77)
568
+ def _reduce_25(val, _values)
569
+ val[0]
570
+ end
571
+ .,.,
572
+
573
+ module_eval(<<'.,.,', 'rafblockparser.ry', 78)
574
+ def _reduce_26(val, _values)
575
+ val[0] << val[1]
576
+ end
577
+ .,.,
578
+
579
+ module_eval(<<'.,.,', 'rafblockparser.ry', 80)
580
+ def _reduce_27(val, _values)
581
+ val[0]
582
+ end
583
+ .,.,
584
+
585
+ module_eval(<<'.,.,', 'rafblockparser.ry', 81)
586
+ def _reduce_28(val, _values)
587
+ val[0]
588
+ end
589
+ .,.,
590
+
591
+ module_eval(<<'.,.,', 'rafblockparser.ry', 83)
592
+ def _reduce_29(val, _values)
593
+ val
594
+ end
595
+ .,.,
596
+
597
+ module_eval(<<'.,.,', 'rafblockparser.ry', 85)
598
+ def _reduce_30(val, _values)
599
+ [PlainTextBlock.new(@inline_parser.parse(val[0]))]
600
+ end
601
+ .,.,
602
+
603
+ module_eval(<<'.,.,', 'rafblockparser.ry', 86)
604
+ def _reduce_31(val, _values)
605
+ val[0] << PlainTextBlock.new(@inline_parser.parse(val[1]))
606
+ end
607
+ .,.,
608
+
609
+ module_eval(<<'.,.,', 'rafblockparser.ry', 88)
610
+ def _reduce_32(val, _values)
611
+ val[0]
612
+ end
613
+ .,.,
614
+
615
+ module_eval(<<'.,.,', 'rafblockparser.ry', 89)
616
+ def _reduce_33(val, _values)
617
+ val[0] + val[1]
618
+ end
619
+ .,.,
620
+
621
+ module_eval(<<'.,.,', 'rafblockparser.ry', 91)
622
+ def _reduce_34(val, _values)
623
+ "\n" + val[0]
624
+ end
625
+ .,.,
626
+
627
+ module_eval(<<'.,.,', 'rafblockparser.ry', 92)
628
+ def _reduce_35(val, _values)
629
+ val[0] + "\n" + val[1]
630
+ end
631
+ .,.,
632
+
633
+ module_eval(<<'.,.,', 'rafblockparser.ry', 96)
634
+ def _reduce_36(val, _values)
635
+ val[0]
636
+ end
637
+ .,.,
638
+
639
+ module_eval(<<'.,.,', 'rafblockparser.ry', 97)
640
+ def _reduce_37(val, _values)
641
+ val[0] << val[1]
642
+ end
643
+ .,.,
644
+
645
+ module_eval(<<'.,.,', 'rafblockparser.ry', 99)
646
+ def _reduce_38(val, _values)
647
+ val[0]
648
+ end
649
+ .,.,
650
+
651
+ module_eval(<<'.,.,', 'rafblockparser.ry', 100)
652
+ def _reduce_39(val, _values)
653
+ val[0]
654
+ end
655
+ .,.,
656
+
657
+ module_eval(<<'.,.,', 'rafblockparser.ry', 102)
658
+ def _reduce_40(val, _values)
659
+ val
660
+ end
661
+ .,.,
662
+
663
+ module_eval(<<'.,.,', 'rafblockparser.ry', 104)
664
+ def _reduce_41(val, _values)
665
+ [PlainTextBlock.new(@inline_parser.parse(val[0]))]
666
+ end
667
+ .,.,
668
+
669
+ module_eval(<<'.,.,', 'rafblockparser.ry', 105)
670
+ def _reduce_42(val, _values)
671
+ val[0] << PlainTextBlock.new(@inline_parser.parse(val[1]))
672
+ end
673
+ .,.,
674
+
675
+ module_eval(<<'.,.,', 'rafblockparser.ry', 110)
676
+ def _reduce_43(val, _values)
677
+ Table.new(val[0])
678
+ end
679
+ .,.,
680
+
681
+ module_eval(<<'.,.,', 'rafblockparser.ry', 112)
682
+ def _reduce_44(val, _values)
683
+ val
684
+ end
685
+ .,.,
686
+
687
+ module_eval(<<'.,.,', 'rafblockparser.ry', 113)
688
+ def _reduce_45(val, _values)
689
+ val[0] << val[1]
690
+ end
691
+ .,.,
692
+
693
+ def _reduce_none(val, _values)
694
+ val[0]
695
+ end
696
+
697
+ end # class BlockParser
698
+
699
+ if __FILE__ == $0
700
+ raf = BlockParser.new
701
+ src = $stdin.readlines
702
+ nodes = raf.parse(src)
703
+ puts "----- index -----"
704
+ raf.index.each do |key,val|
705
+ puts key
706
+ val.each do |v| p v end
707
+ end
708
+ puts "----- info -----"
709
+ p raf.info
710
+ puts "----- output -----"
711
+ nodes.each do |n|
712
+ puts n.apply
713
+ end
714
+ end
715
+ end # end of module Raf