raf 0.1.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,358 +0,0 @@
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