-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathparse_code.rb
213 lines (160 loc) · 5.46 KB
/
parse_code.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
module SnippetExtractor
module Extended
class ParseCode
include Mandate
def initialize(code, syntax_trie, arguments = [])
@code = code
@syntax_trie = syntax_trie
@stop_at_first_loc = arguments.include?(STOP_AT_FIRST_LINE_OF_CODE)
@scan_index = 0
@current_action = nil
@queued_multiline = nil
@saved_lines = []
generate_new_line!
end
def call
action, skipped = try_match_first_word
execute_action(action, skipped) if action
follow_strategy until is_finished?
save_last_line!
saved_lines.map(&:content)
end
protected
attr_accessor :code, :syntax_trie, :scan_index, :current_syntax_node, :current_line,
:current_action, :queued_multiline, :saved_lines
def try_match_first_word
return unless syntax_trie.root.has_match?(' ')
try_match(syntax_trie.root.get_match_node(' '))
end
def try_match(from_node = nil)
return if stop_at_first_loc? && !(saved_lines.empty? && current_line.empty?)
current_syntax_node = from_node || syntax_trie.root
scan_lookahead = 0
until is_finished?(scan_lookahead)
node = scan_char!(scan_lookahead)
break unless current_syntax_node.has_match?(node)
current_syntax_node = current_syntax_node.get_match_node(node)
scan_lookahead += 1
end
[current_syntax_node.action, scan_lookahead]
end
def execute_action(action, skipped)
return if action.nil?
if action.instance_of?(Multi)
self.queued_multiline = action
execute_action(action.start_action, skipped)
return
end
save_if_newline_reached!(skipped)
current_line.skip!
if action.instance_of?(Just) ||
(action.instance_of?(Line) && code[scan_index - 1] == "\n")
self.current_action = self.queued_multiline
self.queued_multiline = nil
else
self.current_action = action
end
end
def save_if_newline_reached!(size = 1)
if code[scan_index, size].include?("\n")
save_current_line!
generate_new_line!
end
increment_scan_index!(size)
end
def follow_strategy
case current_action
when Line then follow_line_strategy
when Multi then follow_multi_strategy
else
looking_for_match_strategy
end
end
def follow_line_strategy
if code[scan_index] == "\n"
self.current_action = self.queued_multiline
self.queued_multiline = nil
end
# We have to check for matches even though the line was skipped.
# The newline could be the blank space of the next whole word keyword.
return save_if_newline_reached! if current_action
looking_for_match_strategy
end
def follow_multi_strategy
action, skipped = try_match(current_action.syntax_trie.root)
return execute_action(action, skipped) if action
save_if_newline_reached!
end
def looking_for_match_strategy
action, skipped = try_match
return execute_action(action, skipped) if action
current_line.append(code[scan_index])
save_if_newline_reached!
end
def start_at_new_word
self.current_syntax_node = current_syntax_node.mapping[' '] if current_syntax_node.mapping.key?(' ')
end
def stop_at_first_loc?
!!@stop_at_first_loc
end
def is_finished?(lookahead = 0)
scan_index + lookahead >= code.length
end
def scan_char!(lookahead = 0)
character = code[scan_index + lookahead]
character.strip.empty? ? ' ' : character.downcase
end
def save_current_line!
# If the current line is not empty, then save it and move on
add_newline_and_save_line! and return unless current_line.empty?(strip: true)
# The current line is empty. Sometimes we want to save it, sometimes we don't.
return if current_line.skip? || saved_lines.empty? || saved_lines.last.empty?(strip: true)
current_line.strip!
add_newline_and_save_line!
end
def generate_new_line!
self.current_line = CurrentLine.new(current_action.instance_of?(Multi) && queued_multiline.nil?)
end
def add_newline_and_save_line!
current_line.append("\n") unless current_line.ends_with_newline?
saved_lines.append(current_line)
end
def save_last_line!
saved_lines.append(current_line) unless current_line.empty?(strip: true)
end
def increment_scan_index!(size = 1)
self.scan_index += size
end
STOP_AT_FIRST_LINE_OF_CODE = "stop_at_first_loc".freeze
private_constant :STOP_AT_FIRST_LINE_OF_CODE
CurrentLine = Struct.new(:skip) do
def initialize(skip)
@content = ""
@skip = skip
end
def content
@content.freeze
end
def skip?
!!@skip
end
def skip!
@skip = true
end
def empty?(strip: false)
strip ? @content.strip.empty? : @content.empty?
end
def strip!
@content.strip!
end
def append(extra)
@content += extra
end
def ends_with_newline?
@content[-1, 1] == "\n"
end
end
private_constant :CurrentLine
end
end
end