blob: d2046861152219b0d088b0d11b16db42b28f98cc (
plain)
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
|
# -*- coding: utf-8 -*- #
# frozen_string_literal: true
module Rouge
class InheritableHash < Hash
def initialize(parent=nil)
@parent = parent
end
def [](k)
value = super
return value if own_keys.include?(k)
value || parent[k]
end
def parent
@parent ||= {}
end
def include?(k)
super or parent.include?(k)
end
def each(&b)
keys.each do |k|
b.call(k, self[k])
end
end
alias own_keys keys
def keys
keys = own_keys.concat(parent.keys)
keys.uniq!
keys
end
end
class InheritableList
include Enumerable
def initialize(parent=nil)
@parent = parent
end
def parent
@parent ||= []
end
def each(&b)
return enum_for(:each) unless block_given?
parent.each(&b)
own_entries.each(&b)
end
def own_entries
@own_entries ||= []
end
def push(o)
own_entries << o
end
alias << push
end
# shared methods for some indentation-sensitive lexers
module Indentation
def reset!
super
@block_state = @block_indentation = nil
end
# push a state for the next indented block
def starts_block(block_state)
@block_state = block_state
@block_indentation = @last_indentation || ''
puts " starts_block: #{block_state.inspect}" if @debug
puts " block_indentation: #{@block_indentation.inspect}" if @debug
end
# handle a single indented line
def indentation(indent_str)
puts " indentation: #{indent_str.inspect}" if @debug
puts " block_indentation: #{@block_indentation.inspect}" if @debug
@last_indentation = indent_str
# if it's an indent and we know where to go next,
# push that state. otherwise, push content and
# clear the block state.
if (@block_state &&
indent_str.start_with?(@block_indentation) &&
indent_str != @block_indentation
)
push @block_state
else
@block_state = @block_indentation = nil
push :content
end
end
end
end
|