bel_parser 1.0.8 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gemspec +12 -2
- data/VERSION +1 -1
- data/bin/bel2_compatibility +12 -9
- data/bin/bel2_upgrade +18 -6
- data/bin/bel2_validator +6 -3
- data/bin/bel_script_reader +1 -0
- data/lib/bel_parser/completion.rb +984 -0
- data/lib/bel_parser/language/apply_namespace_encoding.rb +14 -3
- data/lib/bel_parser/language/expression_validator.rb +1 -2
- data/lib/bel_parser/language/function.rb +4 -0
- data/lib/bel_parser/language/relationship.rb +4 -0
- data/lib/bel_parser/language/semantics/function_deprecation.rb +1 -0
- data/lib/bel_parser/language/semantics/list_function_subject.rb +2 -0
- data/lib/bel_parser/language/semantics/multiple_subject_object.rb +4 -1
- data/lib/bel_parser/language/semantics/nested_statement_without_object.rb +43 -0
- data/lib/bel_parser/language/semantics/non_object_list.rb +3 -0
- data/lib/bel_parser/language/semantics/relationship_not_listable.rb +2 -0
- data/lib/bel_parser/language/semantics/signature_mapping.rb +2 -0
- data/lib/bel_parser/language/semantics_ast.rb +0 -7
- data/lib/bel_parser/language/syntax/invalid_function.rb +6 -1
- data/lib/bel_parser/language/syntax/invalid_relationship.rb +1 -0
- data/lib/bel_parser/language/version1_0/relationships/acts_in.rb +9 -9
- data/lib/bel_parser/language/version1_0/relationships/analogous.rb +6 -4
- data/lib/bel_parser/language/version1_0/relationships/association.rb +8 -7
- data/lib/bel_parser/language/version1_0/relationships/biomarker_for.rb +6 -5
- data/lib/bel_parser/language/version1_0/relationships/causes_no_change.rb +8 -7
- data/lib/bel_parser/language/version1_0/relationships/decreases.rb +13 -12
- data/lib/bel_parser/language/version1_0/relationships/directly_decreases.rb +8 -6
- data/lib/bel_parser/language/version1_0/relationships/directly_increases.rb +8 -6
- data/lib/bel_parser/language/version1_0/relationships/has_component.rb +14 -14
- data/lib/bel_parser/language/version1_0/relationships/has_components.rb +12 -11
- data/lib/bel_parser/language/version1_0/relationships/has_member.rb +10 -8
- data/lib/bel_parser/language/version1_0/relationships/has_members.rb +12 -11
- data/lib/bel_parser/language/version1_0/relationships/has_modification.rb +9 -7
- data/lib/bel_parser/language/version1_0/relationships/has_product.rb +10 -8
- data/lib/bel_parser/language/version1_0/relationships/has_variant.rb +9 -7
- data/lib/bel_parser/language/version1_0/relationships/includes.rb +11 -10
- data/lib/bel_parser/language/version1_0/relationships/increases.rb +12 -13
- data/lib/bel_parser/language/version1_0/relationships/is_a.rb +10 -8
- data/lib/bel_parser/language/version1_0/relationships/negative_correlation.rb +9 -7
- data/lib/bel_parser/language/version1_0/relationships/orthologous.rb +9 -8
- data/lib/bel_parser/language/version1_0/relationships/positive_correlation.rb +8 -5
- data/lib/bel_parser/language/version1_0/relationships/prognostic_biomarker_for.rb +8 -6
- data/lib/bel_parser/language/version1_0/relationships/rate_limiting_step_of.rb +10 -9
- data/lib/bel_parser/language/version1_0/relationships/reactant_in.rb +10 -8
- data/lib/bel_parser/language/version1_0/relationships/sub_process_of.rb +13 -12
- data/lib/bel_parser/language/version1_0/relationships/transcribed_to.rb +8 -7
- data/lib/bel_parser/language/version1_0/relationships/translated_to.rb +9 -7
- data/lib/bel_parser/language/version1_0/relationships/translocates.rb +10 -9
- data/lib/bel_parser/language/version2_0/functions/protein_modification.rb +1 -2
- data/lib/bel_parser/language/version2_0/functions/translocation.rb +1 -2
- data/lib/bel_parser/language/version2_0/functions/variant.rb +1 -2
- data/lib/bel_parser/language/version2_0/relationships/acts_in.rb +10 -9
- data/lib/bel_parser/language/version2_0/relationships/biomarker_for.rb +7 -5
- data/lib/bel_parser/language/version2_0/relationships/causes_no_change.rb +9 -7
- data/lib/bel_parser/language/version2_0/relationships/decreases.rb +13 -12
- data/lib/bel_parser/language/version2_0/relationships/directly_decreases.rb +8 -6
- data/lib/bel_parser/language/version2_0/relationships/directly_increases.rb +8 -6
- data/lib/bel_parser/language/version2_0/relationships/has_component.rb +15 -14
- data/lib/bel_parser/language/version2_0/relationships/has_components.rb +12 -11
- data/lib/bel_parser/language/version2_0/relationships/has_member.rb +9 -8
- data/lib/bel_parser/language/version2_0/relationships/has_members.rb +12 -11
- data/lib/bel_parser/language/version2_0/relationships/has_modification.rb +9 -7
- data/lib/bel_parser/language/version2_0/relationships/has_product.rb +10 -8
- data/lib/bel_parser/language/version2_0/relationships/has_variant.rb +9 -7
- data/lib/bel_parser/language/version2_0/relationships/includes.rb +11 -10
- data/lib/bel_parser/language/version2_0/relationships/increases.rb +13 -12
- data/lib/bel_parser/language/version2_0/relationships/is_a.rb +10 -8
- data/lib/bel_parser/language/version2_0/relationships/negative_correlation.rb +9 -7
- data/lib/bel_parser/language/version2_0/relationships/orthologous.rb +10 -8
- data/lib/bel_parser/language/version2_0/relationships/positive_correlation.rb +7 -5
- data/lib/bel_parser/language/version2_0/relationships/prognostic_biomarker_for.rb +8 -6
- data/lib/bel_parser/language/version2_0/relationships/rate_limiting_step_of.rb +10 -9
- data/lib/bel_parser/language/version2_0/relationships/reactant_in.rb +10 -8
- data/lib/bel_parser/language/version2_0/relationships/regulates.rb +9 -8
- data/lib/bel_parser/language/version2_0/relationships/sub_process_of.rb +13 -12
- data/lib/bel_parser/language/version2_0/relationships/transcribed_to.rb +8 -7
- data/lib/bel_parser/language/version2_0/relationships/translated_to.rb +9 -7
- data/lib/bel_parser/language/version2_0/relationships/translocates.rb +10 -9
- data/lib/bel_parser/mixin/levenshtein.rb +20 -0
- data/lib/bel_parser/parsers/ast/node.rb +49 -2
- data/lib/bel_parser/parsers/bel_script/define_annotation.rb +156 -156
- data/lib/bel_parser/parsers/bel_script/define_namespace.rb +60 -60
- data/lib/bel_parser/parsers/bel_script/set.rb +200 -200
- data/lib/bel_parser/parsers/bel_script/set_document.rb +188 -188
- data/lib/bel_parser/parsers/bel_script/unset.rb +14 -14
- data/lib/bel_parser/parsers/common/common.rl +1 -0
- data/lib/bel_parser/parsers/common/function.rb +8 -8
- data/lib/bel_parser/parsers/common/function.rl +6 -6
- data/lib/bel_parser/parsers/common/identifier.rb +3 -3
- data/lib/bel_parser/parsers/common/identifier.rl +4 -4
- data/lib/bel_parser/parsers/common/list.rb +78 -78
- data/lib/bel_parser/parsers/common/multi_identifier.rb +275 -0
- data/lib/bel_parser/parsers/common/multi_identifier.rl +141 -0
- data/lib/bel_parser/parsers/common/string.rb +6 -6
- data/lib/bel_parser/parsers/common/string.rl +4 -4
- data/lib/bel_parser/parsers/common.rb +1 -0
- data/lib/bel_parser/parsers/expression/nested_statement.rb +30180 -29055
- data/lib/bel_parser/parsers/expression/observed_term.rb +1456 -1099
- data/lib/bel_parser/parsers/expression/parameter.rb +192 -111
- data/lib/bel_parser/parsers/expression/parameter.rl +6 -3
- data/lib/bel_parser/parsers/expression/relationship.rb +43 -19
- data/lib/bel_parser/parsers/expression/relationship.rl +1 -0
- data/lib/bel_parser/parsers/expression/simple_statement.rb +17805 -17093
- data/lib/bel_parser/parsers/expression/statement_autocomplete.rb +1035 -0
- data/lib/bel_parser/parsers/expression/statement_autocomplete.rl +736 -0
- data/lib/bel_parser/parsers/expression/term.rb +960 -705
- data/lib/bel_parser/parsers/serializer.rb +8 -4
- data/lib/bel_parser/version.rb +33 -0
- data/lib/bel_parser.rb +3 -0
- metadata +25 -3
@@ -0,0 +1,736 @@
|
|
1
|
+
=begin
|
2
|
+
%%{
|
3
|
+
machine statement_autocomplete;
|
4
|
+
|
5
|
+
O_PAREN = '(';
|
6
|
+
C_PAREN = ')';
|
7
|
+
|
8
|
+
SPACES = [ \t]+;
|
9
|
+
COLON = ':';
|
10
|
+
COMMA = ',';
|
11
|
+
|
12
|
+
IDENT = [a-zA-Z0-9_]+;
|
13
|
+
STRING = '"' ('\\\"' | [^"\n])* '"'?;
|
14
|
+
RELATIONSHIP = ('!'..'~')+;
|
15
|
+
|
16
|
+
action IDENT {
|
17
|
+
# begin ruby
|
18
|
+
trace('IDENT')
|
19
|
+
@last_state = :IDENT
|
20
|
+
|
21
|
+
@value =
|
22
|
+
identifier(
|
23
|
+
utf8_string(data[ts...te]),
|
24
|
+
character_range: [ts, te])
|
25
|
+
if !@prefix.nil?
|
26
|
+
@param =
|
27
|
+
parameter(
|
28
|
+
@prefix,
|
29
|
+
value(
|
30
|
+
@value,
|
31
|
+
character_range: @value.character_range),
|
32
|
+
character_range: [@prefix.range_start, @value.range_end])
|
33
|
+
@value = nil
|
34
|
+
end
|
35
|
+
# end ruby
|
36
|
+
}
|
37
|
+
|
38
|
+
action RELATIONSHIP {
|
39
|
+
# begin ruby
|
40
|
+
trace('RELATIONSHIP')
|
41
|
+
@last_state = :RELATIONSHIP
|
42
|
+
|
43
|
+
@relationship =
|
44
|
+
relationship(
|
45
|
+
utf8_string(data[ts...te]),
|
46
|
+
character_range: [ts, te])
|
47
|
+
|
48
|
+
range_end =
|
49
|
+
if @statement_stack[-1].object?
|
50
|
+
@statement_stack[-1].object.range_end
|
51
|
+
else
|
52
|
+
@relationship.range_end
|
53
|
+
end
|
54
|
+
|
55
|
+
@statement_stack[-1] =
|
56
|
+
statement(
|
57
|
+
@statement_stack[-1].subject,
|
58
|
+
@relationship,
|
59
|
+
@statement_stack[-1].object,
|
60
|
+
character_range: [@statement_stack[-1].range_start, range_end])
|
61
|
+
|
62
|
+
# return to term scanner
|
63
|
+
fret;
|
64
|
+
|
65
|
+
# end ruby
|
66
|
+
}
|
67
|
+
|
68
|
+
action EOF_RELATIONSHIP {
|
69
|
+
# begin ruby
|
70
|
+
trace('EOF_RELATIONSHIP')
|
71
|
+
|
72
|
+
@relationship =
|
73
|
+
relationship(
|
74
|
+
nil,
|
75
|
+
character_range: [ts, ts])
|
76
|
+
|
77
|
+
range_end =
|
78
|
+
if @statement_stack[-1].object?
|
79
|
+
@statement_stack[-1].object.range_end
|
80
|
+
else
|
81
|
+
@relationship.range_end
|
82
|
+
end
|
83
|
+
|
84
|
+
@statement_stack[-1] =
|
85
|
+
statement(
|
86
|
+
@statement_stack[-1].subject,
|
87
|
+
@relationship,
|
88
|
+
@statement_stack[-1].object,
|
89
|
+
character_range: [@statement_stack[-1].range_start, range_end])
|
90
|
+
|
91
|
+
# go back one char to force EOF on return to term scanner
|
92
|
+
p -= 1
|
93
|
+
|
94
|
+
# return to term scanner
|
95
|
+
fret;
|
96
|
+
# end ruby
|
97
|
+
}
|
98
|
+
|
99
|
+
action STRING {
|
100
|
+
# begin ruby
|
101
|
+
trace('STRING')
|
102
|
+
@last_state = :STRING
|
103
|
+
|
104
|
+
@value =
|
105
|
+
string(
|
106
|
+
utf8_string(data[ts...te]),
|
107
|
+
character_range: [ts, te])
|
108
|
+
if !@prefix.nil?
|
109
|
+
@param =
|
110
|
+
parameter(
|
111
|
+
@prefix,
|
112
|
+
value(
|
113
|
+
@value,
|
114
|
+
character_range: @value.character_range),
|
115
|
+
character_range: [@prefix.range_start, @value.range_end])
|
116
|
+
@value = nil
|
117
|
+
end
|
118
|
+
# end ruby
|
119
|
+
}
|
120
|
+
|
121
|
+
action O_PAREN {
|
122
|
+
# begin ruby
|
123
|
+
trace('O_PAREN')
|
124
|
+
|
125
|
+
case @last_state
|
126
|
+
when :RELATIONSHIP
|
127
|
+
@statement_stack << @statement_stack[-1]
|
128
|
+
@statement_stack[-1] = statement(nil, nil, nil)
|
129
|
+
@relationship = nil
|
130
|
+
|
131
|
+
@term_stack <<
|
132
|
+
term(
|
133
|
+
function(
|
134
|
+
nil,
|
135
|
+
character_range: [ts, ts]),
|
136
|
+
character_range: [ts, te])
|
137
|
+
else
|
138
|
+
term =
|
139
|
+
if @value == nil
|
140
|
+
term(
|
141
|
+
function(
|
142
|
+
nil,
|
143
|
+
character_range: [ts, ts]),
|
144
|
+
character_range: [ts, te])
|
145
|
+
else
|
146
|
+
term(
|
147
|
+
function(
|
148
|
+
@value,
|
149
|
+
character_range: @value.character_range),
|
150
|
+
character_range: [@value.range_start, @value.range_end + 1])
|
151
|
+
end
|
152
|
+
|
153
|
+
@term_stack << term
|
154
|
+
@value = nil
|
155
|
+
end
|
156
|
+
|
157
|
+
@paren_counter += 1
|
158
|
+
@last_state = :O_PAREN
|
159
|
+
# end ruby
|
160
|
+
}
|
161
|
+
|
162
|
+
action C_PAREN {
|
163
|
+
# begin ruby
|
164
|
+
trace('C_PAREN')
|
165
|
+
@paren_counter -= 1
|
166
|
+
|
167
|
+
case @last_state
|
168
|
+
when :COMMA, :O_PAREN
|
169
|
+
function, *arguments = @term_stack[-1].children
|
170
|
+
empty_argument =
|
171
|
+
argument(
|
172
|
+
nil,
|
173
|
+
character_range: [ts, ts])
|
174
|
+
@term_stack[-1] =
|
175
|
+
term(
|
176
|
+
*[function, arguments, empty_argument].flatten.compact,
|
177
|
+
character_range: [function.range_start, te])
|
178
|
+
else
|
179
|
+
if !@param.nil?
|
180
|
+
function, *arguments = @term_stack[-1].children
|
181
|
+
arg_from_param =
|
182
|
+
argument(
|
183
|
+
@param,
|
184
|
+
character_range: @param.character_range)
|
185
|
+
@term_stack[-1] =
|
186
|
+
term(
|
187
|
+
*[function, arguments, arg_from_param].flatten.compact,
|
188
|
+
character_range: [function.range_start, te])
|
189
|
+
@param = nil
|
190
|
+
@prefix = nil
|
191
|
+
elsif !@value.nil?
|
192
|
+
function, *arguments = @term_stack[-1].children
|
193
|
+
arg_from_value =
|
194
|
+
argument(
|
195
|
+
parameter(
|
196
|
+
nil,
|
197
|
+
value(
|
198
|
+
@value,
|
199
|
+
character_range: @value.character_range),
|
200
|
+
character_range: @value.character_range),
|
201
|
+
character_range: @value.character_range)
|
202
|
+
@term_stack[-1] =
|
203
|
+
term(
|
204
|
+
*[function, arguments, arg_from_value].flatten.compact,
|
205
|
+
character_range: [function.range_start, @value.range_end + 1])
|
206
|
+
@value = nil
|
207
|
+
elsif !@prefix.nil?
|
208
|
+
function, *arguments = @term_stack[-1].children
|
209
|
+
arg_from_prefix =
|
210
|
+
argument(
|
211
|
+
parameter(
|
212
|
+
@prefix,
|
213
|
+
value(
|
214
|
+
identifier(
|
215
|
+
'',
|
216
|
+
character_range: [@prefix.range_end + 1, @prefix.range_end + 1]),
|
217
|
+
character_range: [@prefix.range_end + 1, @prefix.range_end + 1]),
|
218
|
+
character_range: @prefix.character_range),
|
219
|
+
character_range: @prefix.character_range)
|
220
|
+
@term_stack[-1] =
|
221
|
+
term(
|
222
|
+
*[function, arguments, arg_from_prefix].flatten.compact,
|
223
|
+
character_range: [function.range_start, @prefix.range_end + 1])
|
224
|
+
@prefix = nil
|
225
|
+
else
|
226
|
+
term = @term_stack[-1]
|
227
|
+
term.character_range = [term.range_start, term.range_end + 1]
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
if @term_stack.length > 1
|
232
|
+
# pop stack
|
233
|
+
inner = @term_stack.pop
|
234
|
+
outer = @term_stack[-1]
|
235
|
+
|
236
|
+
# reconstruct previous term on stack
|
237
|
+
@term_stack[-1] =
|
238
|
+
term(
|
239
|
+
outer.function,
|
240
|
+
*(outer.arguments << argument(inner, character_range: inner.character_range)),
|
241
|
+
character_range: [outer.range_start, inner.range_end + 1])
|
242
|
+
end
|
243
|
+
|
244
|
+
trace("C_PAREN: @term_stack is\n#{@term_stack}")
|
245
|
+
if @term_stack.length == 1
|
246
|
+
@bel_part = :relationship
|
247
|
+
trace("set @bel_part to #{@bel_part}")
|
248
|
+
end
|
249
|
+
|
250
|
+
@last_state = :C_PAREN
|
251
|
+
# end ruby
|
252
|
+
}
|
253
|
+
|
254
|
+
action COLON {
|
255
|
+
# begin ruby
|
256
|
+
trace('COLON')
|
257
|
+
@last_state = :COLON
|
258
|
+
|
259
|
+
if !@value.nil?
|
260
|
+
@prefix =
|
261
|
+
prefix(
|
262
|
+
@value,
|
263
|
+
character_range: @value.character_range)
|
264
|
+
@value = nil
|
265
|
+
else
|
266
|
+
@prefix =
|
267
|
+
prefix(
|
268
|
+
nil,
|
269
|
+
character_range: [ts, ts])
|
270
|
+
end
|
271
|
+
# end ruby
|
272
|
+
}
|
273
|
+
|
274
|
+
action COMMA {
|
275
|
+
# begin ruby
|
276
|
+
trace('COMMA')
|
277
|
+
@last_state = :COMMA
|
278
|
+
|
279
|
+
if !@term_stack.empty?
|
280
|
+
if !@param.nil?
|
281
|
+
function, *arguments = @term_stack[-1].children
|
282
|
+
arg_from_param =
|
283
|
+
argument(
|
284
|
+
@param,
|
285
|
+
character_range: @param.character_range)
|
286
|
+
@term_stack[-1] =
|
287
|
+
term(
|
288
|
+
*([function, arguments, arg_from_param].flatten.compact),
|
289
|
+
character_range: [function.range_start, @param.range_end + 1])
|
290
|
+
elsif !@value.nil?
|
291
|
+
function, *arguments = @term_stack[-1].children
|
292
|
+
arg_from_value =
|
293
|
+
argument(
|
294
|
+
parameter(
|
295
|
+
nil,
|
296
|
+
value(
|
297
|
+
@value,
|
298
|
+
character_range: @value.character_range),
|
299
|
+
character_range: @value.character_range),
|
300
|
+
character_range: @value.character_range)
|
301
|
+
@term_stack[-1] =
|
302
|
+
term(
|
303
|
+
*[function, arguments, arg_from_value].flatten.compact,
|
304
|
+
character_range: [function.range_start, arg_from_value.range_end + 1])
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
308
|
+
@param = nil
|
309
|
+
@prefix = nil
|
310
|
+
@value = nil
|
311
|
+
# end ruby
|
312
|
+
}
|
313
|
+
|
314
|
+
action SPACES {
|
315
|
+
# begin ruby
|
316
|
+
spaces = te-ts
|
317
|
+
trace("SPACES (#{spaces})")
|
318
|
+
|
319
|
+
case @bel_part
|
320
|
+
when :relationship
|
321
|
+
if @relationship
|
322
|
+
spaces -= 1
|
323
|
+
@bel_part = :term
|
324
|
+
|
325
|
+
# remove spaces and adjust pointers by the number of spaces removed
|
326
|
+
data.slice!(ts, spaces)
|
327
|
+
p -= spaces
|
328
|
+
pe -= spaces
|
329
|
+
eof -= spaces
|
330
|
+
|
331
|
+
if @original_caret > ts
|
332
|
+
if @original_caret < te
|
333
|
+
@space_adjusted_caret_position -= (@original_caret - ts)
|
334
|
+
else
|
335
|
+
@space_adjusted_caret_position -= spaces
|
336
|
+
end
|
337
|
+
end
|
338
|
+
else
|
339
|
+
spaces -= 1
|
340
|
+
# remove spaces and adjust pointers by the number of spaces removed
|
341
|
+
data.slice!(ts, spaces)
|
342
|
+
p -= spaces
|
343
|
+
pe -= spaces
|
344
|
+
eof -= spaces
|
345
|
+
|
346
|
+
if @original_caret > ts
|
347
|
+
if @original_caret < te
|
348
|
+
@space_adjusted_caret_position -= (@original_caret - ts)
|
349
|
+
else
|
350
|
+
@space_adjusted_caret_position -= spaces
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
354
|
+
# pop off term; add to statement_ast
|
355
|
+
completed_term = @term_stack[-1]
|
356
|
+
if @statement_stack[-1].subject.nil?
|
357
|
+
@statement_stack[-1] =
|
358
|
+
statement(
|
359
|
+
subject(
|
360
|
+
completed_term,
|
361
|
+
character_range: completed_term.character_range),
|
362
|
+
nil,
|
363
|
+
nil,
|
364
|
+
character_range: completed_term.character_range)
|
365
|
+
elsif @statement_stack[-1].object.nil?
|
366
|
+
object_node =
|
367
|
+
object(
|
368
|
+
completed_term,
|
369
|
+
character_range: completed_term.character_range)
|
370
|
+
@statement_stack[-1] =
|
371
|
+
statement(
|
372
|
+
@statement_stack[-1].subject,
|
373
|
+
@statement_stack[-1].relationship,
|
374
|
+
object_node,
|
375
|
+
character_range: [@statement_stack[-1].range_start, object_node.range_end])
|
376
|
+
end
|
377
|
+
|
378
|
+
@term_stack = []
|
379
|
+
|
380
|
+
# push the target state, jump to relationship scanner
|
381
|
+
# ...eventually to return
|
382
|
+
@relationship = nil
|
383
|
+
fcall relationship;
|
384
|
+
end
|
385
|
+
when :term
|
386
|
+
if @last_state == :COMMA
|
387
|
+
spaces -= 1
|
388
|
+
end
|
389
|
+
|
390
|
+
# remove spaces and adjust pointers by the number of spaces removed
|
391
|
+
data.slice!(ts, spaces)
|
392
|
+
p -= spaces
|
393
|
+
pe -= spaces
|
394
|
+
eof -= spaces
|
395
|
+
|
396
|
+
if @original_caret > ts
|
397
|
+
if @original_caret < te
|
398
|
+
@space_adjusted_caret_position -= (@original_caret - ts)
|
399
|
+
else
|
400
|
+
@space_adjusted_caret_position -= spaces
|
401
|
+
end
|
402
|
+
end
|
403
|
+
end
|
404
|
+
# end ruby
|
405
|
+
}
|
406
|
+
|
407
|
+
action EOF {
|
408
|
+
# begin ruby
|
409
|
+
trace('EOF')
|
410
|
+
|
411
|
+
if @term_stack.empty?
|
412
|
+
# coerce what has been completed into a term
|
413
|
+
if !@param.nil?
|
414
|
+
@term_stack[0] =
|
415
|
+
term(
|
416
|
+
nil,
|
417
|
+
argument(
|
418
|
+
@param,
|
419
|
+
character_range: @param.character_range),
|
420
|
+
character_range: @param.character_range)
|
421
|
+
elsif !@prefix.nil?
|
422
|
+
range = [@prefix.range_start, @prefix.range_end + 1]
|
423
|
+
@term_stack[0] =
|
424
|
+
term(
|
425
|
+
nil,
|
426
|
+
argument(
|
427
|
+
parameter(
|
428
|
+
@prefix,
|
429
|
+
nil,
|
430
|
+
character_range: range),
|
431
|
+
character_range: range),
|
432
|
+
character_range: range)
|
433
|
+
elsif !@value.nil?
|
434
|
+
@term_stack[0] =
|
435
|
+
term(
|
436
|
+
nil,
|
437
|
+
argument(
|
438
|
+
parameter(
|
439
|
+
nil,
|
440
|
+
value(
|
441
|
+
@value,
|
442
|
+
character_range: @value.character_range),
|
443
|
+
character_range: @value.character_range),
|
444
|
+
character_range: @value.character_range),
|
445
|
+
character_range: @value.character_range)
|
446
|
+
end
|
447
|
+
else
|
448
|
+
case @last_state
|
449
|
+
when :IDENT
|
450
|
+
if !@param.nil?
|
451
|
+
function, *arguments = @term_stack[-1].children
|
452
|
+
arg_from_param =
|
453
|
+
argument(
|
454
|
+
@param,
|
455
|
+
character_range: @param.character_range)
|
456
|
+
@term_stack[-1] =
|
457
|
+
term(
|
458
|
+
*[function, arguments, arg_from_param].flatten.compact,
|
459
|
+
character_range: [function.range_start, arg_from_param.range_end])
|
460
|
+
elsif !@value.nil?
|
461
|
+
function, *arguments = @term_stack[-1].children
|
462
|
+
arg_from_value =
|
463
|
+
argument(
|
464
|
+
parameter(
|
465
|
+
nil,
|
466
|
+
value(
|
467
|
+
@value,
|
468
|
+
character_range: @value.character_range),
|
469
|
+
character_range: @value.character_range),
|
470
|
+
character_range: @value.character_range)
|
471
|
+
@term_stack[-1] =
|
472
|
+
term(
|
473
|
+
*[function, arguments, arg_from_value].flatten.compact,
|
474
|
+
character_range: [function.range_start, @value.range_end])
|
475
|
+
end
|
476
|
+
when :STRING
|
477
|
+
if !@param.nil?
|
478
|
+
function, *arguments = @term_stack[-1].children
|
479
|
+
arg_from_param =
|
480
|
+
argument(
|
481
|
+
@param,
|
482
|
+
character_range: @param.character_range)
|
483
|
+
@term_stack[-1] =
|
484
|
+
term(
|
485
|
+
*[function, arguments, arg_from_param].flatten.compact,
|
486
|
+
character_range: [function.range_start, arg_from_param.range_end])
|
487
|
+
elsif !@value.nil?
|
488
|
+
function, *arguments = @term_stack[-1].children
|
489
|
+
arg_from_value =
|
490
|
+
argument(
|
491
|
+
parameter(
|
492
|
+
nil,
|
493
|
+
value(
|
494
|
+
@value,
|
495
|
+
character_range: @value.character_range),
|
496
|
+
character_range: @value.character_range),
|
497
|
+
character_range: @value.character_range)
|
498
|
+
@term_stack[-1] =
|
499
|
+
term(
|
500
|
+
*[function, *arguments, arg_from_value],
|
501
|
+
character_range: [function.range_start, arg_from_value.range_end])
|
502
|
+
end
|
503
|
+
when :COMMA, :O_PAREN
|
504
|
+
function, *arguments = @term_stack[-1].children
|
505
|
+
empty_argument =
|
506
|
+
argument(
|
507
|
+
nil,
|
508
|
+
character_range: [te - 1, te - 1])
|
509
|
+
@term_stack[-1] =
|
510
|
+
term(
|
511
|
+
*[function, *arguments, empty_argument],
|
512
|
+
character_range: [function.range_start, empty_argument.range_end])
|
513
|
+
when :COLON
|
514
|
+
function, *arguments = @term_stack[-1].children
|
515
|
+
empty_argument =
|
516
|
+
argument(
|
517
|
+
parameter(
|
518
|
+
@prefix,
|
519
|
+
nil,
|
520
|
+
character_range: [@prefix.range_start, @prefix.range_end + 1]),
|
521
|
+
character_range: [@prefix.range_start, @prefix.range_end + 1])
|
522
|
+
@term_stack[-1] =
|
523
|
+
term(
|
524
|
+
*[function, *arguments, empty_argument].flatten.compact,
|
525
|
+
character_range: [function.range_start, empty_argument.range_end])
|
526
|
+
end
|
527
|
+
end
|
528
|
+
|
529
|
+
# iff we have completed any term-related node
|
530
|
+
if !@term_stack.empty?
|
531
|
+
# combine terms
|
532
|
+
while @term_stack.length > 1
|
533
|
+
# pop stack
|
534
|
+
inner = @term_stack.pop
|
535
|
+
outer = @term_stack[-1]
|
536
|
+
|
537
|
+
# reconstruct previous term on stack
|
538
|
+
@term_stack[-1] =
|
539
|
+
term(
|
540
|
+
outer.function,
|
541
|
+
*(outer.arguments << argument(inner, character_range: inner.character_range)),
|
542
|
+
character_range: [outer.range_start, inner.range_end + 1])
|
543
|
+
end
|
544
|
+
|
545
|
+
# add to statement_ast
|
546
|
+
completed_term = @term_stack[-1]
|
547
|
+
if @statement_stack[-1].subject.nil?
|
548
|
+
@statement_stack[-1] =
|
549
|
+
statement(
|
550
|
+
subject(
|
551
|
+
completed_term,
|
552
|
+
character_range: completed_term.character_range),
|
553
|
+
nil,
|
554
|
+
nil,
|
555
|
+
character_range: completed_term.character_range)
|
556
|
+
elsif @statement_stack[-1].object.nil?
|
557
|
+
object_node =
|
558
|
+
object(
|
559
|
+
completed_term,
|
560
|
+
character_range: completed_term.character_range)
|
561
|
+
@statement_stack[-1] =
|
562
|
+
statement(
|
563
|
+
@statement_stack[-1].subject,
|
564
|
+
@statement_stack[-1].relationship,
|
565
|
+
object_node,
|
566
|
+
character_range: [@statement_stack[-1].range_start, object_node.range_end])
|
567
|
+
end
|
568
|
+
end
|
569
|
+
|
570
|
+
# yield statement
|
571
|
+
while @statement_stack.length > 1
|
572
|
+
# pop stack
|
573
|
+
nested = @statement_stack.pop
|
574
|
+
outer = @statement_stack[-1]
|
575
|
+
|
576
|
+
# reconstruct previous statement on stack
|
577
|
+
@statement_stack[-1] =
|
578
|
+
statement(
|
579
|
+
outer.subject,
|
580
|
+
outer.relationship,
|
581
|
+
object(
|
582
|
+
nested,
|
583
|
+
character_range: nested.character_range),
|
584
|
+
character_range: [outer.range_start, nested.range_end + 1])
|
585
|
+
end
|
586
|
+
yield @statement_stack.pop
|
587
|
+
# end ruby
|
588
|
+
}
|
589
|
+
|
590
|
+
relationship := |*
|
591
|
+
RELATIONSHIP => RELATIONSHIP;
|
592
|
+
any => EOF_RELATIONSHIP;
|
593
|
+
*|;
|
594
|
+
|
595
|
+
term := |*
|
596
|
+
IDENT => IDENT;
|
597
|
+
STRING => STRING;
|
598
|
+
O_PAREN => O_PAREN;
|
599
|
+
C_PAREN => C_PAREN;
|
600
|
+
COLON => COLON;
|
601
|
+
COMMA => COMMA;
|
602
|
+
SPACES => SPACES;
|
603
|
+
any => EOF;
|
604
|
+
*|;
|
605
|
+
}%%
|
606
|
+
=end
|
607
|
+
|
608
|
+
# begin ruby
|
609
|
+
require_relative '../../vendor/ast/processor/mixin'
|
610
|
+
require_relative '../ast/node'
|
611
|
+
require_relative '../mixin/buffer'
|
612
|
+
require_relative '../nonblocking_io_wrapper'
|
613
|
+
require_relative '../tracer'
|
614
|
+
|
615
|
+
module BELParser
|
616
|
+
module Parsers
|
617
|
+
module Expression
|
618
|
+
module StatementAutocomplete
|
619
|
+
|
620
|
+
class << self
|
621
|
+
|
622
|
+
MAX_LENGTH = 1024 * 128 # 128K
|
623
|
+
|
624
|
+
def parse(content, caret_position)
|
625
|
+
return nil unless content
|
626
|
+
|
627
|
+
if !content.end_with?("\n")
|
628
|
+
content = "#{content}\n"
|
629
|
+
end
|
630
|
+
|
631
|
+
parser = Parser.new(content, caret_position)
|
632
|
+
parser.each do |ast|
|
633
|
+
return [ast, parser.space_adjusted_caret_position]
|
634
|
+
end
|
635
|
+
end
|
636
|
+
end
|
637
|
+
|
638
|
+
private
|
639
|
+
|
640
|
+
class Parser
|
641
|
+
include Enumerable
|
642
|
+
include BELParser::Parsers::Buffer
|
643
|
+
include BELParser::Parsers::AST
|
644
|
+
include BELParser::Parsers::AST::Sexp
|
645
|
+
include BELParser::Parsers::Tracer
|
646
|
+
|
647
|
+
attr_reader :space_adjusted_caret_position
|
648
|
+
|
649
|
+
def initialize(content, caret_position)
|
650
|
+
@content = content
|
651
|
+
@original_caret = caret_position
|
652
|
+
@space_adjusted_caret_position = caret_position
|
653
|
+
%% write data;
|
654
|
+
end
|
655
|
+
|
656
|
+
def each
|
657
|
+
@last_state = nil
|
658
|
+
@spaces = 0
|
659
|
+
@value = nil
|
660
|
+
@prefix = nil
|
661
|
+
@param = nil
|
662
|
+
@term_stack = []
|
663
|
+
@statement_stack = [statement(nil, nil, nil)]
|
664
|
+
@paren_counter = 0
|
665
|
+
@relationship = nil
|
666
|
+
@bel_part = :term
|
667
|
+
|
668
|
+
stack = []
|
669
|
+
data = @content.unpack('C*')
|
670
|
+
p = 0
|
671
|
+
pe = data.length
|
672
|
+
eof = data.length
|
673
|
+
|
674
|
+
# begin: ragel
|
675
|
+
%% write init;
|
676
|
+
%% write exec;
|
677
|
+
# end: ragel
|
678
|
+
end
|
679
|
+
end
|
680
|
+
end
|
681
|
+
end
|
682
|
+
end
|
683
|
+
end
|
684
|
+
|
685
|
+
if __FILE__ == $0
|
686
|
+
require 'bel_parser/parsers/serializer'
|
687
|
+
class ::AST::Node
|
688
|
+
include BELParser::Parsers
|
689
|
+
|
690
|
+
def _metadata
|
691
|
+
ivars = instance_variables - [:@type, :@children, :@hash]
|
692
|
+
ivars.map { |iv| [iv, instance_variable_get(iv)] }.to_s
|
693
|
+
end
|
694
|
+
private :_metadata
|
695
|
+
|
696
|
+
def to_sexp(indent=0)
|
697
|
+
indented = " " * indent
|
698
|
+
sexp = "#{indented}(#{fancy_type} #{_metadata}"
|
699
|
+
|
700
|
+
first_node_child = children.index do |child|
|
701
|
+
child.is_a?(::AST::Node) || child.is_a?(Array)
|
702
|
+
end || children.count
|
703
|
+
|
704
|
+
children.each_with_index do |child, idx|
|
705
|
+
if child.is_a?(::AST::Node) && idx >= first_node_child
|
706
|
+
sexp << "\n#{child.to_sexp(indent + 1)}"
|
707
|
+
else
|
708
|
+
sexp << " #{child.inspect}"
|
709
|
+
end
|
710
|
+
end
|
711
|
+
|
712
|
+
sexp << ")"
|
713
|
+
|
714
|
+
sexp
|
715
|
+
end
|
716
|
+
|
717
|
+
def to_bel
|
718
|
+
serialize(self)
|
719
|
+
end
|
720
|
+
end
|
721
|
+
|
722
|
+
$stdin.each_line do |line|
|
723
|
+
ast, caret = BELParser::Parsers::Expression::StatementAutocomplete.parse(
|
724
|
+
line,
|
725
|
+
line.length-1 # adjust for newline
|
726
|
+
)
|
727
|
+
puts ast.to_sexp(1)
|
728
|
+
puts ast.to_bel
|
729
|
+
puts "#{' ' * caret}|"
|
730
|
+
puts "caret: #{caret}"
|
731
|
+
end
|
732
|
+
end
|
733
|
+
# end ruby
|
734
|
+
|
735
|
+
# vim: ft=ragel ts=2 sw=2 expandtab:
|
736
|
+
# encoding: utf-8
|