Class: Bade::Parser
- Inherits:
-
Object
show all
- Defined in:
- lib/bade/parser.rb
Defined Under Namespace
Modules: LineIndicatorRegexps, MixinRegexps, ParseRubyCodeRegexps, TagRegexps
Classes: ParserInternalError, SyntaxError
Constant Summary
collapse
- WORD_RE =
''.respond_to?(:encoding) ? '\p{Word}' : '\w'
- NAME_RE_STRING =
"(#{WORD_RE}(?:#{WORD_RE}|:|-|_)*)"
- ATTR_NAME_RE_STRING =
"\\A\\s*#{NAME_RE_STRING}"
- CODE_ATTR_RE =
/#{ATTR_NAME_RE_STRING}\s*&?:\s*/
- TAG_RE =
/\A#{NAME_RE_STRING}/
- CLASS_TAG_RE =
/\A\.#{NAME_RE_STRING}/
- ID_TAG_RE =
/\A##{NAME_RE_STRING}/
- RUBY_DELIMITERS_REVERSE =
{
'(' => ')',
'[' => ']',
'{' => '}'
}.freeze
- RUBY_QUOTES =
%w(' ").freeze
- RUBY_NOT_NESTABLE_DELIMITERS =
RUBY_QUOTES
- RUBY_START_DELIMITERS =
(%w(\( [ {) + RUBY_NOT_NESTABLE_DELIMITERS).freeze
- RUBY_END_DELIMITERS =
(%w(\) ] }) + RUBY_NOT_NESTABLE_DELIMITERS).freeze
- RUBY_ALL_DELIMITERS =
(RUBY_START_DELIMITERS + RUBY_END_DELIMITERS).uniq.freeze
- RUBY_START_DELIMITERS_RE =
/\A[#{Regexp.escape RUBY_START_DELIMITERS.join}]/
- RUBY_END_DELIMITERS_RE =
/\A[#{Regexp.escape RUBY_END_DELIMITERS.join}]/
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(tabsize: 4, file_path: nil) ⇒ Parser
Returns a new instance of Parser.
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/bade/parser.rb', line 49
def initialize(tabsize: 4, file_path: nil)
@line = ''
@tabsize = tabsize
@file_path = file_path
@tab_re = /\G((?: {#{tabsize}})*) {0,#{tabsize-1}}\t/
@tab = '\1' + ' ' * tabsize
reset
end
|
Instance Attribute Details
40
41
42
|
# File 'lib/bade/parser.rb', line 40
def dependency_paths
@dependency_paths
end
|
#file_path ⇒ String
44
45
46
|
# File 'lib/bade/parser.rb', line 44
def file_path
@file_path
end
|
Instance Method Details
#append_node(type, indent: @indents.length, add: false, value: nil) ⇒ Object
Append element to stacks and result tree
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/bade/parser.rb', line 153
def append_node(type, indent: @indents.length, add: false, value: nil)
while indent >= @stacks.length
@stacks << @stacks.last.dup
end
parent = @stacks[indent].last
node = AST::NodeRegistrator.create(type, @lineno)
parent.children << node
node.value = value unless value.nil?
if add
@stacks[indent] << node
end
node
end
|
#fixed_trailing_colon(value) ⇒ Object
503
504
505
506
507
508
509
510
|
# File 'lib/bade/parser.rb', line 503
def fixed_trailing_colon(value)
if String === value && value.end_with?(':')
value = value.remove_last
@line.prepend(':')
end
value
end
|
#get_indent(line) ⇒ Int
Calculate indent for line
145
146
147
|
# File 'lib/bade/parser.rb', line 145
def get_indent(line)
line.get_indent(@tabsize)
end
|
#next_line ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/bade/parser.rb', line 124
def next_line
if @lines.empty?
@orig_line = @line = nil
last_newlines = remove_last_newlines
@root.children += last_newlines
nil
else
@orig_line = @lines.shift
@lineno += 1
@line = @orig_line.dup
end
end
|
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/bade/parser.rb', line 64
def parse(str)
@document = AST::Document.new(file_path: file_path)
@root = @document.root
@dependency_paths = []
if str.kind_of? Array
reset(str, [[@root]])
else
reset(str.split(/\r?\n/, -1), [[@root]]) end
parse_line while next_line
reset
@document
end
|
#parse_import ⇒ Object
345
346
347
348
349
350
|
# File 'lib/bade/parser.rb', line 345
def parse_import
path = eval(@line)
append_node(:import, value: path)
@dependency_paths << path unless @dependency_paths.include?(path)
end
|
#parse_line ⇒ Object
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
|
# File 'lib/bade/parser.rb', line 179
def parse_line
if @line.strip.length == 0
append_node(:newline) unless @lines.empty?
return
end
indent = get_indent(@line)
@line.remove_indent!(indent, @tabsize)
expecting_indentation = @stacks.length > @indents.length
if indent > @indents.last
@indents << indent
else
if expecting_indentation
last_newlines = remove_last_newlines
@stacks.pop
new_node = @stacks.last.last
new_node.children += last_newlines
end
while indent < @indents.last
last_newlines = remove_last_newlines
@indents.pop
@stacks.pop
new_node = @stacks.last.last
new_node.children += last_newlines
end
while not @stacks[indent].nil? and indent < @stacks[indent].length - 1
last_newlines = remove_last_newlines
@stacks[indent].pop
new_node = @stacks.last.last
new_node.children += last_newlines
end
syntax_error('Malformed indentation') if indent != @indents.last
end
parse_line_indicators
end
|
#parse_line_indicators(add_newline: true) ⇒ Object
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
# File 'lib/bade/parser.rb', line 264
def parse_line_indicators(add_newline: true)
case @line
when LineIndicatorRegexps::IMPORT
@line = $'
parse_import
when LineIndicatorRegexps::MIXIN_DECL
@line = $'
parse_mixin_declaration($1)
when LineIndicatorRegexps::MIXIN_CALL
@line = $'
parse_mixin_call($1)
when LineIndicatorRegexps::BLOCK_DECLARATION
@line = $'
if @stacks.last.last.type == :mixin_call
node = append_node(:mixin_block, add: true)
node.name = $1
else
parse_tag($&)
end
when LineIndicatorRegexps::HTML_COMMENT
append_node(:html_comment, add: true)
parse_text_block $', @indents.last + @tabsize
when LineIndicatorRegexps::NORMAL_COMMENT
append_node(:comment, add: true)
parse_text_block $', @indents.last + @tabsize
when LineIndicatorRegexps::TEXT_BLOCK_START
parse_text_block $', @indents.last + @tabsize
when LineIndicatorRegexps::INLINE_HTML
append_node(:text, value: @line)
when LineIndicatorRegexps::CODE_BLOCK
append_node(:code, value: $'.strip)
when LineIndicatorRegexps::OUTPUT_BLOCK
@line = $'
output_node = append_node(:output)
output_node.conditional = $1.length == 1
output_node.escaped = $2.length == 1
output_node.value = parse_ruby_code(ParseRubyCodeRegexps::END_NEW_LINE)
when LineIndicatorRegexps::DOCTYPE
append_node(:doctype, value: $'.strip)
when TAG_RE
@line = $' if $1
parse_tag($&)
when LineIndicatorRegexps::TAG_CLASS_START_BLOCK
parse_tag 'div'
when LineIndicatorRegexps::TAG_ID_START_BLOCK
parse_tag 'div'
else
syntax_error 'Unknown line indicator'
end
append_node(:newline) if add_newline && !@lines.empty?
end
|
#parse_mixin_call(mixin_name) ⇒ Object
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
# File 'lib/bade/parser.rb', line 367
def parse_mixin_call(mixin_name)
mixin_name = fixed_trailing_colon(mixin_name)
mixin_node = append_node(:mixin_call, add: true)
mixin_node.name = mixin_name
parse_mixin_call_params
case @line
when MixinRegexps::TEXT_START
@line = $'
parse_text
when MixinRegexps::BLOCK_EXPANSION
@line = $'
parse_line_indicators(add_newline: false)
when MixinRegexps::OUTPUT_CODE
parse_line_indicators(add_newline: false)
when ''
else
syntax_error "Unknown symbol after mixin calling, line = `#{@line}'"
end
end
|
#parse_mixin_call_params ⇒ Object
#parse_mixin_declaration(mixin_name) ⇒ Object
436
437
438
439
440
441
|
# File 'lib/bade/parser.rb', line 436
def parse_mixin_declaration(mixin_name)
mixin_node = append_node(:mixin_decl, add: true)
mixin_node.name = mixin_name
parse_mixin_declaration_params
end
|
#parse_mixin_declaration_params ⇒ Object
#parse_ruby_code(outer_delimiters) ⇒ Void
Parse ruby code, ended with outer delimiters
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
|
# File 'lib/bade/parser.rb', line 648
def parse_ruby_code(outer_delimiters)
code = String.new
end_re = if Regexp === outer_delimiters
outer_delimiters
else
/\A\s*[#{Regexp.escape outer_delimiters.to_s}]/
end
delimiters = []
until @line.empty? or (delimiters.count == 0 and @line =~ end_re)
char = @line[0]
if char == '\\' && RUBY_ALL_DELIMITERS.include?(@line[1])
code << @line.slice!(0, 2)
next
end
case char
when RUBY_START_DELIMITERS_RE
if RUBY_NOT_NESTABLE_DELIMITERS.include?(char) && delimiters.last == char
delimiters.pop
else
delimiters << char
end
when RUBY_END_DELIMITERS_RE
if char == RUBY_DELIMITERS_REVERSE[delimiters.last]
delimiters.pop
end
end
code << @line.slice!(0)
end
unless delimiters.empty?
syntax_error('Unexpected end of ruby code')
end
code.strip
end
|
#parse_tag(tag) ⇒ Object
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
|
# File 'lib/bade/parser.rb', line 515
def parse_tag(tag)
tag = fixed_trailing_colon(tag)
if tag.is_a?(AST::Node)
tag_node = tag
else
tag_node = append_node(:tag, add: true)
tag_node.name = tag
end
parse_tag_attributes
case @line
when TagRegexps::BLOCK_EXPANSION
@line = $'
parse_line_indicators(add_newline: false)
when TagRegexps::OUTPUT_CODE
parse_line_indicators(add_newline: false)
when CLASS_TAG_RE
@line = $'
attr_node = append_node(:tag_attr)
attr_node.name = 'class'
attr_node.value = fixed_trailing_colon($1).single_quote
parse_tag tag_node
when ID_TAG_RE
@line = $'
attr_node = append_node(:tag_attr)
attr_node.name = 'id'
attr_node.value = fixed_trailing_colon($1).single_quote
parse_tag tag_node
when TagRegexps::TEXT_START
@line = $'
parse_text
when ''
else
syntax_error "Unknown symbol after tag definition #{@line}"
end
end
|
#parse_tag_attributes ⇒ Object
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
|
# File 'lib/bade/parser.rb', line 570
def parse_tag_attributes
if @line.start_with?('(')
@line.remove_first!
else
return
end
while true
case @line
when CODE_ATTR_RE
@line = $'
attr_node = append_node(:tag_attr)
attr_node.name = $1
attr_node.value = parse_ruby_code(ParseRubyCodeRegexps::END_PARAMS_ARG)
when TagRegexps::PARAMS_ARGS_DELIMITER
@line = $'
next
when TagRegexps::PARAMS_END
@line = $'
break
else
@line.lstrip!
syntax_error('Expected attribute') unless @line.empty?
append_node(:newline)
syntax_error('Expected closing tag attributes delimiter `)`') if @lines.empty?
next_line
end
end
end
|
#parse_text ⇒ Object
485
486
487
488
489
|
# File 'lib/bade/parser.rb', line 485
def parse_text
text = @line
text = text.gsub(/&\{/, '#{ __html_escaped ')
append_node(:text, value: text)
end
|
#parse_text_block(first_line, text_indent = nil) ⇒ Object
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
|
# File 'lib/bade/parser.rb', line 613
def parse_text_block(first_line, text_indent = nil)
if !first_line || first_line.empty?
text_indent = nil
else
@line = first_line
parse_text
end
until @lines.empty?
if @lines.first.blank?
next_line
append_node(:newline)
else
indent = get_indent(@lines.first)
break if indent <= @indents.last
next_line
@line.remove_indent!(text_indent ? text_indent : indent, @tabsize)
parse_text
text_indent ||= indent
end
end
end
|
173
174
175
176
177
|
# File 'lib/bade/parser.rb', line 173
def remove_last_newlines
last_node = @stacks.last.last
last_newlines_count = last_node.children.rcount_matching { |n| n.type == :newline }
last_node.children.pop(last_newlines_count)
end
|
#reset(lines = nil, stacks = nil) ⇒ Object
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
|
# File 'lib/bade/parser.rb', line 95
def reset(lines = nil, stacks = nil)
@indents = [0]
@stacks = stacks
@lineno = 0
@lines = lines
@line = @orig_line = nil
end
|
#syntax_error(message) ⇒ Object
717
718
719
720
|
# File 'lib/bade/parser.rb', line 717
def syntax_error(message)
raise SyntaxError.new(message, file_path, @orig_line, @lineno,
@orig_line && @line ? @orig_line.size - @line.size : 0)
end
|